OSDN Git Service

LiveViewを動かすところ。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / nikon / wrapper / liveview / NikonLiveViewControl.java
1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.liveview;
2
3 import android.app.Activity;
4 import android.util.Log;
5
6 import androidx.annotation.NonNull;
7
8 import net.osdn.gokigen.a01d.camera.ILiveViewControl;
9 import net.osdn.gokigen.a01d.camera.nikon.wrapper.command.messages.specific.NikonLiveViewRequestMessage;
10 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
11 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
12 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
13 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommunication;
14 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpResponseReceiver;
15 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.PtpIpResponseReceiver;
16 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
17 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
18 import net.osdn.gokigen.a01d.liveview.liveviewlistener.IImageDataReceiver;
19 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener;
20
21 import java.util.Arrays;
22 import java.util.Map;
23
24 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_AFDRIVE;
25 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_DEVICE_READY;
26 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_START_LIVEVIEW;
27 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_STOP_LIVEVIEW;
28
29 public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListener, IPtpIpCommunication, IPtpIpLiveViewImageCallback, IPtpIpCommandCallback
30 {
31     private final String TAG = this.toString();
32     private final IPtpIpCommandPublisher commandIssuer;
33     private final int delayMs;
34     private NikonLiveViewImageReceiver imageReceiver;
35     private IImageDataReceiver dataReceiver = null;
36     private boolean liveViewIsReceiving = false;
37     private boolean commandIssued = false;
38     private boolean isDumpLog = true;
39
40     public NikonLiveViewControl(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs)
41     {
42         this.commandIssuer = interfaceProvider.getCommandPublisher();
43         this.delayMs = delayMs;
44         this.imageReceiver = new NikonLiveViewImageReceiver(this);
45     }
46
47     public ILiveViewListener getLiveViewListener()
48     {
49         return (this);
50     }
51
52     @Override
53     public void changeLiveViewSize(String size)
54     {
55
56     }
57
58     @Override
59     public void startLiveView()
60     {
61         Log.v(TAG, " startLiveView() ");
62         try
63         {
64             commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_START_LIVEVIEW, 20, isDumpLog, 0, 0x9201, 0, 0x00, 0x00, 0x00, 0x00));
65         }
66         catch (Exception e)
67         {
68             e.printStackTrace();
69         }
70     }
71
72     @Override
73     public void stopLiveView()
74     {
75         Log.v(TAG, " stopLiveView() ");
76         try
77         {
78             if (liveViewIsReceiving)
79             {
80                 liveViewIsReceiving = false;
81                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(new PtpIpResponseReceiver(null), SEQ_STOP_LIVEVIEW, 30, isDumpLog, 0, 0x9202, 0, 0x00, 0x00, 0x00, 0x00));
82             }
83         }
84         catch (Exception e)
85         {
86             e.printStackTrace();
87         }
88     }
89
90     private void startLiveviewImpl()
91     {
92         liveViewIsReceiving = true;
93         try
94         {
95             Thread thread = new Thread(new Runnable() {
96                 @Override
97                 public void run()
98                 {
99                     try
100                     {
101                         while (liveViewIsReceiving)
102                         {
103                             if (!commandIssued)
104                             {
105                                 commandIssued = true;
106                                 commandIssuer.enqueueCommand(new NikonLiveViewRequestMessage(imageReceiver, 90, isDumpLog));
107                             }
108                             try
109                             {
110                                 Thread.sleep(delayMs);
111                             }
112                             catch (Exception e)
113                             {
114                                 e.printStackTrace();
115                             }
116                         }
117                     }
118                     catch (Exception e)
119                     {
120                         e.printStackTrace();
121                     }
122                 }
123             });
124             thread.start();
125         }
126         catch (Exception e)
127         {
128             e.printStackTrace();
129         }
130     }
131
132     @Override
133     public void updateDigitalZoom()
134     {
135         Log.v(TAG, " updateDigitalZoom() ");
136
137     }
138
139     @Override
140     public void updateMagnifyingLiveViewScale(boolean isChangeScale)
141     {
142         Log.v(TAG, " updateMagnifyingLiveViewScale() ");
143     }
144
145     @Override
146     public float getMagnifyingLiveViewScale()
147     {
148         return 0;
149     }
150
151     @Override
152     public float getDigitalZoomScale()
153     {
154         return 0;
155     }
156
157     @Override
158     public void setCameraLiveImageView(IImageDataReceiver target)
159     {
160         Log.v(TAG, " setCameraLiveImageView() ");
161         this.dataReceiver = target;
162     }
163
164     @Override
165     public boolean connect()
166     {
167         Log.v(TAG, " connect() ");
168         return (true);
169     }
170
171     @Override
172     public void disconnect()
173     {
174         Log.v(TAG, " disconnect() ");
175     }
176
177     @Override
178     public void onCompleted(byte[] data, Map<String, Object> metadata)
179     {
180         //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- ");
181         try
182         {
183             if ((dataReceiver != null)&&(data != null)&&(data.length > 0))
184             {
185                 //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
186                 //SimpleLogDumper.dump_bytes(" [LVLV] " + ": ", Arrays.copyOfRange(data, 0, (0 + 512)));
187                 //dataReceiver.setImageData(data, metadata);
188                 int offset = 384;
189                 if (data.length > 8)
190                 {
191                     dataReceiver.setImageData(Arrays.copyOfRange(data, offset, data.length), metadata);  // ヘッダ部分を切り取って送る
192                 }
193             }
194         }
195         catch (Exception e)
196         {
197             e.printStackTrace();
198         }
199         commandIssued = false;
200     }
201
202     @Override
203     public void onErrorOccurred(Exception e)
204     {
205         Log.v(TAG, " onErrorOccurred () : " + e.getLocalizedMessage());
206         commandIssued = false;
207     }
208
209     @Override
210     public void receivedMessage(int id, byte[] rx_body)
211     {
212         Log.v(TAG, " NikonLiveViewControl::receivedMessage() : ");
213         try
214         {
215             if (rx_body.length < 10)
216             {
217                 retrySendMessage(id);
218                 return;
219             }
220             int responseCode = (rx_body[8] & 0xff) + ((rx_body[9] & 0xff) * 256);
221             if (responseCode != 0x2001)
222             {
223                 // NG応答を受信...同じコマンドを再送する
224                 Log.v(TAG, String.format(" RECEIVED NG REPLY ID : %d, RESPONSE CODE : 0x%04x ", id, responseCode));
225                 retrySendMessage(id);
226                 return;
227             }
228
229             Log.v(TAG, String.format(" OK REPLY (ID : %d) ", id));
230             if (id == SEQ_START_LIVEVIEW)
231             {
232                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_READY, 20, isDumpLog, 0, 0x90c8, 0, 0x00, 0x00, 0x00, 0x00));
233             }
234             else if (id == SEQ_DEVICE_READY)
235             {
236                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_AFDRIVE, 20, isDumpLog, 0, 0x90c1, 0, 0x00, 0x00, 0x00, 0x00));
237             }
238             else
239             {
240                 // ライブビューの開始。
241                 startLiveviewImpl();
242             }
243         }
244         catch (Exception e)
245         {
246             e.printStackTrace();
247         }
248     }
249
250     private void retrySendMessage(int id)
251     {
252         try
253         {
254             if (id == SEQ_START_LIVEVIEW)
255             {
256                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_START_LIVEVIEW, 20, isDumpLog, 0, 0x9201, 0, 0x00, 0x00, 0x00, 0x00));
257             }
258             else if (id == SEQ_DEVICE_READY)
259             {
260                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_READY, 20, isDumpLog, 0, 0x90c8, 0, 0x00, 0x00, 0x00, 0x00));
261             }
262             else
263             {
264                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_AFDRIVE, 20, isDumpLog, 0, 0x90c1, 0, 0x00, 0x00, 0x00, 0x00));
265             }
266         }
267         catch (Exception e)
268         {
269             e.printStackTrace();
270         }
271     }
272
273     @Override
274     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] rx_body)
275     {
276         //
277     }
278
279     @Override
280     public boolean isReceiveMulti()
281     {
282         return (false);
283     }
284 }