OSDN Git Service

NIKON対応途中。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / nikon / wrapper / liveview / NikonLiveViewImageReceiver.java
1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.liveview;
2
3 import android.util.Log;
4
5 import androidx.annotation.NonNull;
6 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
7 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
8 import net.osdn.gokigen.a01d.camera.utils.SimpleLogDumper;
9
10 import java.io.ByteArrayOutputStream;
11 import java.util.Arrays;
12
13 public class NikonLiveViewImageReceiver implements IPtpIpCommandCallback
14 {
15     private final String TAG = toString();
16
17     private IPtpIpLiveViewImageCallback callback;
18
19     private int received_total_bytes = 0;
20     private int received_remain_bytes = 0;
21
22     private int target_image_size = 0;
23     private boolean receivedFirstData = false;
24     private ByteArrayOutputStream byteStream;
25
26     NikonLiveViewImageReceiver(@NonNull IPtpIpLiveViewImageCallback callback)
27     {
28         this.callback = callback;
29         byteStream = new ByteArrayOutputStream();
30     }
31
32     @Override
33     public void receivedMessage(int id, byte[] rx_body)
34     {
35         try
36         {
37             // end of receive sequence.
38             //byte [] thumbnail = byteStream.toByteArray();
39             //Log.v(TAG, " TransferComplete() RECEIVED  : " + id + " size : " + target_image_size + " (" + thumbnail.length + ")");
40             //SimpleLogDumper.dump_bytes(" [xxxxx]", Arrays.copyOfRange(thumbnail, 0, (64)));
41             //SimpleLogDumper.dump_bytes(" [zzzzz]", Arrays.copyOfRange(thumbnail, (thumbnail.length - 64), (thumbnail.length)));
42             callback.onCompleted(byteStream.toByteArray(), null);
43             receivedFirstData = false;
44             received_remain_bytes = 0;
45             received_total_bytes = 0;
46             target_image_size = 0;
47
48             byteStream.reset();
49         }
50         catch (Exception e)
51         {
52             e.printStackTrace();
53             {
54                 callback.onErrorOccurred(e);
55             }
56         }
57     }
58
59     @Override
60     public void onReceiveProgress(final int currentBytes, final int totalBytes, byte[] rx_body)
61     {
62         // Log.v(TAG, " onReceiveProgress() " + currentBytes + "/" + totalBytes);
63
64         // 受信したデータから、通信のヘッダ部分を削除する
65         cutHeader(rx_body);
66     }
67
68     private void cutHeader(byte[] rx_body)
69     {
70         if (rx_body == null)
71         {
72             return;
73         }
74         int length = rx_body.length;
75         int data_position = 0;
76         if (!receivedFirstData)
77         {
78             // データを最初に読んだとき。ヘッダ部分を読み飛ばす
79             receivedFirstData = true;
80             data_position = (int) rx_body[0] & (0xff);
81             //Log.v(TAG, " FIRST DATA POS. : " + data_position);
82             //SimpleLogDumper.dump_bytes(" [sssss]", Arrays.copyOfRange(rx_body, 0, (64)));
83         }
84         else if (received_remain_bytes > 0)
85         {
86             // データの読み込みが途中だった場合...
87             if (length < received_remain_bytes)
88             {
89                 // 全部コピーする、足りないバイト数は残す
90                 received_remain_bytes = received_remain_bytes - length;
91                 received_total_bytes = received_total_bytes + rx_body.length;
92                 byteStream.write(rx_body, 0, rx_body.length);
93                 return;
94             }
95             else
96             {
97                 byteStream.write(rx_body, data_position, received_remain_bytes);
98                 data_position = received_remain_bytes;
99                 received_remain_bytes = 0;
100             }
101         }
102
103         while (data_position <= (length - 12)) {
104             int body_size = (rx_body[data_position] & 0xff) + ((rx_body[data_position + 1] & 0xff) << 8) +
105                     ((rx_body[data_position + 2] & 0xff) << 16) + ((rx_body[data_position + 3] & 0xff) << 24);
106             if (body_size <= 12) {
107                 Log.v(TAG, " --- BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.length + "  (" + target_image_size + ")");
108                 //int startpos = (data_position > 48) ? (data_position - 48) : 0;
109                 //SimpleLogDumper.dump_bytes(" [xxx]", Arrays.copyOfRange(rx_body, startpos, (data_position + 48)));
110                 break;
111             }
112
113             // 受信データ(のヘッダ部分)をダンプする
114             Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")");
115             SimpleLogDumper.dump_bytes(" [zzz] " + data_position + ": ", Arrays.copyOfRange(rx_body, data_position, (data_position + 48)));
116
117             if ((data_position + body_size) > length) {
118                 // データがすべてバッファ内になかったときは、バッファすべてコピーして残ったサイズを記憶しておく。
119                 int copysize = (length - ((data_position + 12)));
120                 byteStream.write(rx_body, (data_position + 12), copysize);
121                 received_remain_bytes = body_size - copysize - 12;  // マイナス12は、ヘッダ分
122                 received_total_bytes = received_total_bytes + copysize;
123                 //Log.v(TAG, " ----- copy : " + (data_position + 12) + " " + copysize + " remain : " + received_remain_bytes + "  body size : " + body_size);
124                 break;
125             }
126             try {
127                 byteStream.write(rx_body, (data_position + 12), (body_size - 12));
128                 data_position = data_position + body_size;
129                 received_total_bytes = received_total_bytes + 12;
130                 //Log.v(TAG, " --- COPY : " + (data_position + 12) + " " + (body_size - 12) + " remain : " + received_remain_bytes);
131
132             } catch (Exception e) {
133                 Log.v(TAG, "  pos : " + data_position + "  size : " + body_size + " length : " + length);
134                 e.printStackTrace();
135             }
136         }
137     }
138
139     @Override
140     public boolean isReceiveMulti()
141     {
142         return (true);
143     }
144
145 }