OSDN Git Service

1d781d7f8fa72c7d7ee646395f90f06504bfb09f
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / nikon / wrapper / liveview / NikonLiveViewImageReceiver.kt
1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.liveview
2
3 import android.util.Log
4 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback
5 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback
6 import net.osdn.gokigen.a01d.camera.utils.SimpleLogDumper
7 import java.io.ByteArrayOutputStream
8 import java.util.*
9
10 class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallback) : IPtpIpCommandCallback
11 {
12     private val isDumpLog = true
13     private var received_total_bytes = 0
14     private var received_remain_bytes = 0
15     private var receivedFirstData = false
16     private var byteStream = ByteArrayOutputStream()
17
18     override fun receivedMessage(id: Int, rx_body: ByteArray?)
19     {
20         if (rx_body == null)
21         {
22             Log.v(TAG, " MSG BODY IS NULL.")
23             return
24         }
25         if (isReceiveMulti)
26         {
27             receivedMessage_multi(id, rx_body)
28         }
29         else
30         {
31             receivedMessage_single(id, rx_body)
32         }
33     }
34
35     override fun onReceiveProgress(currentBytes: Int, totalBytes: Int, rx_body: ByteArray?)
36     {
37         if (rx_body == null)
38         {
39             Log.v(TAG, " MSG BODY IS NULL.")
40             return
41         }
42         Log.v(TAG, " onReceiveProgress() $currentBytes/$totalBytes LENGTH: ${rx_body.size} bytes.")
43
44         // 受信したデータから、通信のヘッダ部分を削除する
45         cutHeader(rx_body)
46     }
47
48     private fun receivedMessage_single(id: Int, rx_body: ByteArray)
49     {
50         try
51         {
52             Log.v(TAG, "receivedMessage_single() : " + rx_body.size + " bytes.")
53             if ((isDumpLog)&&(rx_body.size > 64))
54             {
55                 SimpleLogDumper.dump_bytes(" LV (FIRST) : ", Arrays.copyOfRange(rx_body, 0, 64))
56                 SimpleLogDumper.dump_bytes(" LV (-END-) : ", Arrays.copyOfRange(rx_body, rx_body.size - 64, rx_body.size))
57             }
58             callback.onCompleted(rx_body, null)
59         }
60         catch (e: Exception)
61         {
62             e.printStackTrace()
63         }
64     }
65
66     private fun receivedMessage_multi(id: Int, rx_body: ByteArray)
67     {
68         try
69         {
70             Log.v(TAG, " receivedMessage_multi()  id[$id] size : ${rx_body.size} ")
71
72             callback.onCompleted(byteStream.toByteArray(), null)
73             receivedFirstData = false
74             received_remain_bytes = 0
75             received_total_bytes = 0
76             byteStream.reset()
77         }
78         catch (e: Exception)
79         {
80             e.printStackTrace()
81             run { callback.onErrorOccurred(e) }
82         }
83     }
84
85     private fun cutHeader(rx_body: ByteArray)
86     {
87         val length = rx_body.size
88         var data_position = 0
89         if (!receivedFirstData)
90         {
91             // データを最初に読んだとき。ヘッダ部分を読み飛ばす
92             receivedFirstData = true
93             data_position = rx_body[0].toUByte().toInt()
94             if (isDumpLog)
95             {
96                 Log.v(TAG, " FIRST DATA POS. : $data_position len: $length ");
97                 SimpleLogDumper.dump_bytes(" [sXXs]", Arrays.copyOfRange(rx_body, 0, (32)));
98             }
99         }
100         else
101         {
102             // 2回目以降の受信データ
103             if (received_remain_bytes > 0)
104             {
105                 // データの読み込みが途中だった場合...
106                 if (length < received_remain_bytes)
107                 {
108                     // 全部コピーする、足りないバイト数は残す
109                     received_remain_bytes = received_remain_bytes - length
110                     received_total_bytes = received_total_bytes + rx_body.size
111                     byteStream.write(rx_body, 0, rx_body.size)
112                     return
113                 }
114                 else
115                 {
116                     byteStream.write(rx_body, data_position, received_remain_bytes)
117                     data_position = received_remain_bytes
118                     received_remain_bytes = 0
119                 }
120             }
121         }
122         while (data_position <= length - 12)
123         {
124             val body_size = (rx_body[data_position].toUByte()).toInt() + ((rx_body[data_position + 1].toUByte()).toInt() * 256) + ((rx_body[data_position + 2].toUByte()).toInt() * 256 * 256) + ((rx_body[data_position + 3].toUByte()).toInt() * 256 * 256 * 256)
125
126             Log.v(TAG, " XX body_size : ${body_size} [$data_position] ($length)  aa: ${rx_body[data_position].toUByte().toInt()}  ${rx_body[data_position + 1].toUByte().toInt()} + ${rx_body[data_position + 2].toUByte().toInt()}")
127             SimpleLogDumper.dump_bytes("XX", Arrays.copyOfRange(rx_body, 0, 32))
128             if (body_size <= 12)
129             {
130                 Log.v(TAG, " ----- BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.size + " ")
131                 break
132             }
133
134             // 受信データ(のヘッダ部分)をダンプする
135             Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")");
136             if (data_position + body_size > length)
137             {
138                 // データがすべてバッファ内になかったときは、バッファすべてコピーして残ったサイズを記憶しておく。
139                 val copysize = length - (data_position + 12)
140                 byteStream.write(rx_body, data_position + 12, copysize)
141                 received_remain_bytes = body_size - copysize - 12 // マイナス12は、ヘッダ分
142                 received_total_bytes = received_total_bytes + copysize
143                 //Log.v(TAG, " ----- copy : " + (data_position + (12)) + " " + copysize + " remain : " + received_remain_bytes + "  body size : " + body_size);
144                 break
145             }
146             try
147             {
148                 byteStream.write(rx_body, data_position + 12, body_size - 12)
149                 data_position = data_position + body_size
150                 received_total_bytes = received_total_bytes + 12
151                 //Log.v(TAG, " --- COPY : " + (data_position + 12) + " " + (body_size - (12)) + " remain : " + received_remain_bytes);
152             }
153             catch (e: Exception)
154             {
155                 Log.v(TAG, "  pos : $data_position  size : $body_size length : $length")
156                 e.printStackTrace()
157             }
158         }
159     }
160
161     override fun isReceiveMulti(): Boolean
162     {
163         return (true)
164     }
165
166     companion object
167     {
168         private val TAG = "NikonLiveViewImageReceiver"
169     }
170 }