OSDN Git Service

JPEGヘッダの探し方(のデフォルト方式)を変更。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / canon / wrapper / liveview / CanonLiveViewControlPrev.java
1 package net.osdn.gokigen.a01d.camera.canon.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.messages.PtpIpCommandGenericWithRetry;
13 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
14 import net.osdn.gokigen.a01d.liveview.liveviewlistener.IImageDataReceiver;
15 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener;
16
17 import java.util.Arrays;
18 import java.util.Map;
19
20 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_GET_VIEWFRAME;
21
22 public class CanonLiveViewControlPrev implements ILiveViewControl, ILiveViewListener, IPtpIpCommunication, IPtpIpLiveViewImageCallback
23 {
24     private final String TAG = this.toString();
25     private final IPtpIpCommandPublisher commandIssuer;
26     private final int delayMs;
27     private final boolean isDumpLog = false;
28     private final boolean isSearchJpegHeader;
29     private final int retryCount = 1200;
30     private final CanonLiveViewImageReceiver imageReceiver;
31     private IImageDataReceiver dataReceiver = null;
32     private boolean liveViewIsReceiving = false;
33     private boolean commandIssued = false;
34
35     public CanonLiveViewControlPrev(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs, boolean isSearchJpegHeader)
36     {
37         this.commandIssuer = interfaceProvider.getCommandPublisher();
38         this.isSearchJpegHeader = isSearchJpegHeader;
39         this.delayMs = delayMs;
40         this.imageReceiver = new CanonLiveViewImageReceiver(context,this);
41         Log.v(TAG, " -=-=-=-=-=- CanonLiveViewControl : delay " + delayMs + " ms");
42     }
43
44     public ILiveViewListener getLiveViewListener()
45     {
46         return (this);
47     }
48
49     @Override
50     public void changeLiveViewSize(String size)
51     {
52
53     }
54
55     @Override
56     public void startLiveView()
57     {
58         Log.v(TAG, " startLiveView() ");
59         liveViewIsReceiving = true;
60         try
61         {
62             Thread thread = new Thread(new Runnable() {
63                 @Override
64                 public void run()
65                 {
66                     try
67                     {
68                         while (liveViewIsReceiving)
69                         {
70                             if (!commandIssued)
71                             {
72                                 if (isDumpLog)
73                                 {
74                                     Log.v(TAG, " enqueueCommand() ");
75                                 }
76                                 if (commandIssuer.getCurrentQueueSize() < 3)
77                                 {
78                                     commandIssued = true;
79                                     commandIssuer.enqueueCommand(new PtpIpCommandGenericWithRetry(imageReceiver, SEQ_GET_VIEWFRAME, delayMs, retryCount, false, false, 0, 0x9153, 12, 0x00200000, 0x01, 0x00, 0x00));
80                                 }
81                             }
82                             try
83                             {
84                                 Thread.sleep(delayMs);
85                             }
86                             catch (Exception e)
87                             {
88                                 e.printStackTrace();
89                             }
90                         }
91                     }
92                     catch (Exception e)
93                     {
94                         e.printStackTrace();
95                     }
96                 }
97             });
98             thread.start();
99         }
100         catch (Exception e)
101         {
102             e.printStackTrace();
103         }
104     }
105
106     @Override
107     public void stopLiveView()
108     {
109         Log.v(TAG, " stopLiveView() ");
110         liveViewIsReceiving = false;
111
112     }
113
114     @Override
115     public void updateDigitalZoom()
116     {
117         Log.v(TAG, " updateDigitalZoom() ");
118
119     }
120
121     @Override
122     public void updateMagnifyingLiveViewScale(boolean isChangeScale)
123     {
124         Log.v(TAG, " updateMagnifyingLiveViewScale() ");
125     }
126
127     @Override
128     public float getMagnifyingLiveViewScale()
129     {
130         return 0;
131     }
132
133     @Override
134     public float getDigitalZoomScale()
135     {
136         return 0;
137     }
138
139     @Override
140     public void setCameraLiveImageView(IImageDataReceiver target)
141     {
142         Log.v(TAG, " setCameraLiveImageView() ");
143         this.dataReceiver = target;
144     }
145
146     @Override
147     public boolean connect()
148     {
149         Log.v(TAG, " connect() ");
150         return (true);
151     }
152
153     @Override
154     public void disconnect()
155     {
156         Log.v(TAG, " disconnect() ");
157     }
158
159     @Override
160     public void onCompleted(byte[] data, Map<String, Object> metadata)
161     {
162         //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- ");
163         try
164         {
165             if ((dataReceiver != null)&&(data != null))
166             {
167                 if (isDumpLog)
168                 {
169                     Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
170                 }
171                 int headerSize = searchJpegHeader(data);
172                 if (headerSize >= 0)
173                 {
174                     dataReceiver.setImageData(Arrays.copyOfRange(data, headerSize, data.length), metadata);  // ヘッダ部分を切り取って送る
175                 }
176             }
177         }
178         catch (Exception e)
179         {
180             e.printStackTrace();
181         }
182         commandIssued = false;
183     }
184
185     private int searchJpegHeader(byte[] data)
186     {
187         if (data.length <= 8)
188         {
189             return (-1);
190         }
191         if (!isSearchJpegHeader)
192         {
193             // JPEG ヘッダを探さない場合は、8バイト固定とする
194             return (8);
195         }
196         try
197         {
198             int size = data.length - 1;
199             int index = 0;
200             while (index < size)
201             {
202                 if ((data[index] == (byte) 0xff)&&(data[index + 1] == (byte) 0xd8))
203                 {
204                     return (index);
205                 }
206                 index++;
207             }
208         }
209         catch (Exception e)
210         {
211             e.printStackTrace();
212         }
213
214         // 見つからなかったときは 8 を返す
215         return (8);
216     }
217
218     @Override
219     public void onErrorOccurred(Exception e)
220     {
221         Log.v(TAG, " onErrorOccurred () : " + e.getLocalizedMessage());
222         commandIssued = false;
223     }
224 }