1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.liveview
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
10 class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallback) : IPtpIpCommandCallback
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()
18 override fun receivedMessage(id: Int, rx_body: ByteArray?)
22 Log.v(TAG, " MSG BODY IS NULL.")
27 receivedMessage_multi(id, rx_body)
31 receivedMessage_single(id, rx_body)
35 override fun onReceiveProgress(currentBytes: Int, totalBytes: Int, rx_body: ByteArray?)
39 Log.v(TAG, " MSG BODY IS NULL.")
42 Log.v(TAG, " onReceiveProgress() $currentBytes/$totalBytes LENGTH: ${rx_body.size} bytes.")
44 // 受信したデータから、通信のヘッダ部分を削除する
48 private fun receivedMessage_single(id: Int, rx_body: ByteArray)
52 Log.v(TAG, "receivedMessage_single() : " + rx_body.size + " bytes.")
53 if ((isDumpLog)&&(rx_body.size > 64))
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))
58 callback.onCompleted(rx_body, null)
66 private fun receivedMessage_multi(id: Int, rx_body: ByteArray)
70 Log.v(TAG, " receivedMessage_multi() id[$id] size : ${rx_body.size} ")
72 callback.onCompleted(byteStream.toByteArray(), null)
73 receivedFirstData = false
74 received_remain_bytes = 0
75 received_total_bytes = 0
81 run { callback.onErrorOccurred(e) }
85 private fun cutHeader(rx_body: ByteArray)
87 val length = rx_body.size
89 if (!receivedFirstData)
91 // データを最初に読んだとき。ヘッダ部分を読み飛ばす
92 receivedFirstData = true
93 data_position = rx_body[0].toUByte().toInt()
96 Log.v(TAG, " FIRST DATA POS. : $data_position len: $length ");
97 SimpleLogDumper.dump_bytes(" [sXXs]", Arrays.copyOfRange(rx_body, 0, (32)));
103 if (received_remain_bytes > 0)
105 // データの読み込みが途中だった場合...
106 if (length < received_remain_bytes)
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)
116 byteStream.write(rx_body, data_position, received_remain_bytes)
117 data_position = received_remain_bytes
118 received_remain_bytes = 0
122 while (data_position <= length - 12)
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)
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))
130 Log.v(TAG, " ----- BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.size + " ")
134 // 受信データ(のヘッダ部分)をダンプする
135 Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")");
136 if (data_position + body_size > length)
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);
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);
155 Log.v(TAG, " pos : $data_position size : $body_size length : $length")
161 override fun isReceiveMulti(): Boolean
168 private val TAG = "NikonLiveViewImageReceiver"