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.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;
19 import java.util.Arrays;
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;
28 public class NikonLiveViewControl implements ILiveViewControl, ILiveViewListener, IPtpIpCommunication, IPtpIpLiveViewImageCallback, IPtpIpResponseReceiver
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;
39 public NikonLiveViewControl(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs)
41 this.commandIssuer = interfaceProvider.getCommandPublisher();
42 this.delayMs = delayMs;
43 this.imageReceiver = new NikonLiveViewImageReceiver(this);
46 public ILiveViewListener getLiveViewListener()
52 public void changeLiveViewSize(String size)
58 public void startLiveView()
60 Log.v(TAG, " startLiveView() ");
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));
74 public void stopLiveView()
76 Log.v(TAG, " stopLiveView() ");
79 if (liveViewIsReceiving)
81 liveViewIsReceiving = false;
82 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(new PtpIpResponseReceiver(null), SEQ_STOP_LIVEVIEW, 20, isDumpLog, 0, 0x9202, 0, 0x00, 0x00, 0x00, 0x00));
91 private void startLiveviewImpl()
93 liveViewIsReceiving = true;
96 Thread thread = new Thread(new Runnable() {
102 while (liveViewIsReceiving)
106 commandIssued = true;
107 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(imageReceiver, SEQ_GET_VIEWFRAME, 20, false, 0, 0x9203, 0, 0x00, 0x00, 0x00, 0x00));
111 Thread.sleep(delayMs);
134 public void updateDigitalZoom()
136 Log.v(TAG, " updateDigitalZoom() ");
141 public void updateMagnifyingLiveViewScale(boolean isChangeScale)
143 Log.v(TAG, " updateMagnifyingLiveViewScale() ");
147 public float getMagnifyingLiveViewScale()
153 public float getDigitalZoomScale()
159 public void setCameraLiveImageView(IImageDataReceiver target)
161 Log.v(TAG, " setCameraLiveImageView() ");
162 this.dataReceiver = target;
166 public boolean connect()
168 Log.v(TAG, " connect() ");
173 public void disconnect()
175 Log.v(TAG, " disconnect() ");
179 public void onCompleted(byte[] data, Map<String, Object> metadata)
181 //Log.v(TAG, " ---+++--- RECEIVED LV IMAGE ---+++--- ");
184 if ((dataReceiver != null)&&(data != null))
186 //Log.v(TAG, " ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
187 //dataReceiver.setImageData(data, metadata);
190 dataReceiver.setImageData(Arrays.copyOfRange(data, 8, data.length), metadata); // ヘッダ部分を切り取って送る
198 commandIssued = false;
202 public void onErrorOccurred(Exception e)
204 Log.v(TAG, " onErrorOccurred () : " + e.getLocalizedMessage());
205 commandIssued = false;
209 public void response(int id, int responseCode)
212 if ((id == SEQ_AFDRIVE)&&(responseCode == 0x2001))
218 Log.v(TAG, String.format(" ===== NikonLiveViewControl::response() ID : %d, RESPONSE CODE : 0x%04x ", id, responseCode));