OSDN Git Service

59119c64f4e335966a9ea8c8c20ca5dedf645b5e
[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.IPtpIpCommandPublisher;
12 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommunication;
13 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpResponseReceiver;
14 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.PtpIpResponseReceiver;
15 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
16 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
17 import net.osdn.gokigen.a01d.liveview.liveviewlistener.IImageDataReceiver;
18 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener;
19
20 import java.util.Arrays;
21 import java.util.Map;
22
23 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_AFDRIVE;
24 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_DEVICE_READY;
25 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_START_LIVEVIEW;
26 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_STOP_LIVEVIEW;
27
28 public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListener, IPtpIpCommunication, IPtpIpLiveViewImageCallback, IPtpIpResponseReceiver
29 {
30     private final String TAG = this.toString();
31     private final IPtpIpCommandPublisher commandIssuer;
32     private final int delayMs;
33     private NikonLiveViewImageReceiver imageReceiver;
34     private IImageDataReceiver dataReceiver = null;
35     private boolean liveViewIsReceiving = false;
36     private boolean commandIssued = false;
37     private boolean isDumpLog = true;
38
39     public NikonLiveViewControl(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs)
40     {
41         this.commandIssuer = interfaceProvider.getCommandPublisher();
42         this.delayMs = delayMs;
43         this.imageReceiver = new NikonLiveViewImageReceiver(this);
44     }
45
46     public ILiveViewListener getLiveViewListener()
47     {
48         return (this);
49     }
50
51     @Override
52     public void changeLiveViewSize(String size)
53     {
54
55     }
56
57     @Override
58     public void startLiveView()
59     {
60         Log.v(TAG, " startLiveView() ");
61         try
62         {
63             commandIssuer.enqueueCommand(new PtpIpCommandGeneric(new PtpIpResponseReceiver(null), SEQ_START_LIVEVIEW, 20, isDumpLog, 0, 0x9201, 0, 0x00, 0x00, 0x00, 0x00));
64             commandIssuer.enqueueCommand(new PtpIpCommandGeneric(new PtpIpResponseReceiver(null), SEQ_DEVICE_READY, 20, isDumpLog, 0, 0x90c8, 0, 0x00, 0x00, 0x00, 0x00));
65             commandIssuer.enqueueCommand(new PtpIpCommandGeneric(new PtpIpResponseReceiver(this), SEQ_AFDRIVE, 20, isDumpLog, 0, 0x90c1, 0, 0x00, 0x00, 0x00, 0x00));
66         }
67         catch (Exception e)
68         {
69             e.printStackTrace();
70         }
71     }
72
73     @Override
74     public void stopLiveView()
75     {
76         Log.v(TAG, " stopLiveView() ");
77         try
78         {
79             if (liveViewIsReceiving)
80             {
81                 liveViewIsReceiving = false;
82                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(new PtpIpResponseReceiver(null), SEQ_STOP_LIVEVIEW, 30, isDumpLog, 0, 0x9202, 0, 0x00, 0x00, 0x00, 0x00));
83             }
84         }
85         catch (Exception e)
86         {
87             e.printStackTrace();
88         }
89     }
90
91     private void startLiveviewImpl()
92     {
93         liveViewIsReceiving = true;
94         try
95         {
96             Thread thread = new Thread(new Runnable() {
97                 @Override
98                 public void run()
99                 {
100                     try
101                     {
102                         while (liveViewIsReceiving)
103                         {
104                             if (!commandIssued)
105                             {
106                                 commandIssued = true;
107                                 commandIssuer.enqueueCommand(new NikonLiveViewRequestMessage(imageReceiver, 90, isDumpLog));
108                             }
109                             try
110                             {
111                                 Thread.sleep(delayMs);
112                             }
113                             catch (Exception e)
114                             {
115                                 e.printStackTrace();
116                             }
117                         }
118                     }
119                     catch (Exception e)
120                     {
121                         e.printStackTrace();
122                     }
123                 }
124             });
125             thread.start();
126         }
127         catch (Exception e)
128         {
129             e.printStackTrace();
130         }
131     }
132
133     @Override
134     public void updateDigitalZoom()
135     {
136         Log.v(TAG, " updateDigitalZoom() ");
137
138     }
139
140     @Override
141     public void updateMagnifyingLiveViewScale(boolean isChangeScale)
142     {
143         Log.v(TAG, " updateMagnifyingLiveViewScale() ");
144     }
145
146     @Override
147     public float getMagnifyingLiveViewScale()
148     {
149         return 0;
150     }
151
152     @Override
153     public float getDigitalZoomScale()
154     {
155         return 0;
156     }
157
158     @Override
159     public void setCameraLiveImageView(IImageDataReceiver target)
160     {
161         Log.v(TAG, " setCameraLiveImageView() ");
162         this.dataReceiver = target;
163     }
164
165     @Override
166     public boolean connect()
167     {
168         Log.v(TAG, " connect() ");
169         return (true);
170     }
171
172     @Override
173     public void disconnect()
174     {
175         Log.v(TAG, " disconnect() ");
176     }
177
178     @Override
179     public void onCompleted(byte[] data, Map<String, Object> metadata)
180     {
181         //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- ");
182         try
183         {
184             if ((dataReceiver != null)&&(data != null)&&(data.length > 0))
185             {
186                 //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
187                 //SimpleLogDumper.dump_bytes(" [LVLV] " + ": ", Arrays.copyOfRange(data, 0, (0 + 512)));
188                 //dataReceiver.setImageData(data, metadata);
189                 int offset = 384;
190                 if (data.length > 8)
191                 {
192                     dataReceiver.setImageData(Arrays.copyOfRange(data, offset, data.length), metadata);  // ヘッダ部分を切り取って送る
193                 }
194             }
195         }
196         catch (Exception e)
197         {
198             e.printStackTrace();
199         }
200         commandIssued = false;
201     }
202
203     @Override
204     public void onErrorOccurred(Exception e)
205     {
206         Log.v(TAG, " onErrorOccurred () : " + e.getLocalizedMessage());
207         commandIssued = false;
208     }
209
210     @Override
211     public void response(int id, int responseCode)
212     {
213         // 応答OKなら LV開始。
214         if ((id == SEQ_AFDRIVE)&&(responseCode == 0x2001))
215         {
216             startLiveviewImpl();
217         }
218         else
219         {
220             Log.v(TAG, String.format(" ===== NikonLiveViewControl::response() ID : %d, RESPONSE CODE : 0x%04x ", id, responseCode));
221         }
222     }
223 }