1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.liveview;
3 import android.app.Activity;
4 import android.util.Log;
6 import androidx.annotation.NonNull;
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;
21 import java.util.Arrays;
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;
29 public class NikonLiveViewControl implements ILiveViewControl, ILiveViewListener, IPtpIpCommunication, IPtpIpLiveViewImageCallback, IPtpIpCommandCallback
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;
40 public NikonLiveViewControl(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs)
42 this.commandIssuer = interfaceProvider.getCommandPublisher();
43 this.delayMs = delayMs;
44 this.imageReceiver = new NikonLiveViewImageReceiver(this);
47 public ILiveViewListener getLiveViewListener()
53 public void changeLiveViewSize(String size)
59 public void startLiveView()
61 Log.v(TAG, " startLiveView() ");
64 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_START_LIVEVIEW, 20, isDumpLog, 0, 0x9201, 0, 0x00, 0x00, 0x00, 0x00));
73 public void stopLiveView()
75 Log.v(TAG, " stopLiveView() ");
78 if (liveViewIsReceiving)
80 liveViewIsReceiving = false;
81 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(new PtpIpResponseReceiver(null), SEQ_STOP_LIVEVIEW, 30, isDumpLog, 0, 0x9202, 0, 0x00, 0x00, 0x00, 0x00));
90 private void startLiveviewImpl()
92 liveViewIsReceiving = true;
95 Thread thread = new Thread(new Runnable() {
101 while (liveViewIsReceiving)
105 commandIssued = true;
106 commandIssuer.enqueueCommand(new NikonLiveViewRequestMessage(imageReceiver, 90, isDumpLog));
110 Thread.sleep(delayMs);
133 public void updateDigitalZoom()
135 Log.v(TAG, " updateDigitalZoom() ");
140 public void updateMagnifyingLiveViewScale(boolean isChangeScale)
142 Log.v(TAG, " updateMagnifyingLiveViewScale() ");
146 public float getMagnifyingLiveViewScale()
152 public float getDigitalZoomScale()
158 public void setCameraLiveImageView(IImageDataReceiver target)
160 Log.v(TAG, " setCameraLiveImageView() ");
161 this.dataReceiver = target;
165 public boolean connect()
167 Log.v(TAG, " connect() ");
172 public void disconnect()
174 Log.v(TAG, " disconnect() ");
178 public void onCompleted(byte[] data, Map<String, Object> metadata)
180 //Log.v(TAG, " ---+++--- RECEIVED LV IMAGE ---+++--- ");
183 if ((dataReceiver != null)&&(data != null)&&(data.length > 0))
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);
191 dataReceiver.setImageData(Arrays.copyOfRange(data, offset, data.length), metadata); // ヘッダ部分を切り取って送る
199 commandIssued = false;
203 public void onErrorOccurred(Exception e)
205 Log.v(TAG, " onErrorOccurred () : " + e.getLocalizedMessage());
206 commandIssued = false;
210 public void receivedMessage(int id, byte[] rx_body)
212 Log.v(TAG, " NikonLiveViewControl::receivedMessage() : ");
215 if (rx_body.length < 10)
217 retrySendMessage(id);
220 int responseCode = (rx_body[8] & 0xff) + ((rx_body[9] & 0xff) * 256);
221 if (responseCode != 0x2001)
223 // NG応答を受信...同じコマンドを再送する
224 Log.v(TAG, String.format(" RECEIVED NG REPLY ID : %d, RESPONSE CODE : 0x%04x ", id, responseCode));
225 retrySendMessage(id);
229 Log.v(TAG, String.format(" OK REPLY (ID : %d) ", id));
230 if (id == SEQ_START_LIVEVIEW)
232 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_READY, 20, isDumpLog, 0, 0x90c8, 0, 0x00, 0x00, 0x00, 0x00));
234 else if (id == SEQ_DEVICE_READY)
236 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_AFDRIVE, 20, isDumpLog, 0, 0x90c1, 0, 0x00, 0x00, 0x00, 0x00));
250 private void retrySendMessage(int id)
254 if (id == SEQ_START_LIVEVIEW)
256 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_START_LIVEVIEW, 20, isDumpLog, 0, 0x9201, 0, 0x00, 0x00, 0x00, 0x00));
258 else if (id == SEQ_DEVICE_READY)
260 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_READY, 20, isDumpLog, 0, 0x90c8, 0, 0x00, 0x00, 0x00, 0x00));
264 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_AFDRIVE, 20, isDumpLog, 0, 0x90c1, 0, 0x00, 0x00, 0x00, 0x00));
274 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] rx_body)
280 public boolean isReceiveMulti()