OSDN Git Service

NIKON対応途中。
[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.ptpip.IPtpIpInterfaceProvider;
10 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
11 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommunication;
12 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpResponseReceiver;
13 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.PtpIpResponseReceiver;
14 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
15 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
16 import net.osdn.gokigen.a01d.liveview.liveviewlistener.IImageDataReceiver;
17 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener;
18
19 import java.util.Arrays;
20 import java.util.Map;
21
22 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_AFDRIVE;
23 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_DEVICE_READY;
24 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_GET_VIEWFRAME;
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 = false;
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, 20, 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 PtpIpCommandGeneric(imageReceiver, SEQ_GET_VIEWFRAME, 20, false, 0, 0x9203, 0, 0x00, 0x00, 0x00, 0x00));
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))
185             {
186                 //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
187                 //dataReceiver.setImageData(data, metadata);
188                 if (data.length > 8)
189                 {
190                     dataReceiver.setImageData(Arrays.copyOfRange(data, 8, data.length), metadata);  // ヘッダ部分を切り取って送る
191                 }
192             }
193         }
194         catch (Exception e)
195         {
196             e.printStackTrace();
197         }
198         commandIssued = false;
199     }
200
201     @Override
202     public void onErrorOccurred(Exception e)
203     {
204         Log.v(TAG, " onErrorOccurred () : " + e.getLocalizedMessage());
205         commandIssued = false;
206     }
207
208     @Override
209     public void response(int id, int responseCode)
210     {
211         // 応答OKなら LV開始。
212         if ((id == SEQ_AFDRIVE)&&(responseCode == 0x2001))
213         {
214             startLiveviewImpl();
215         }
216         else
217         {
218             Log.v(TAG, String.format(" ===== NikonLiveViewControl::response() ID : %d, RESPONSE CODE : 0x%04x ", id, responseCode));
219         }
220     }
221 }