OSDN Git Service

androidx に置き換えた。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / ricohgr2 / wrapper / RicohGr2LiveViewControl.java
1 package net.osdn.gokigen.gr2control.camera.ricohgr2.wrapper;
2
3 import android.util.Log;
4
5 import net.osdn.gokigen.gr2control.camera.ILiveViewControl;
6 import net.osdn.gokigen.gr2control.camera.utils.SimpleHttpClient;
7 import net.osdn.gokigen.gr2control.camera.utils.SimpleLiveviewSlicer;
8 import net.osdn.gokigen.gr2control.liveview.liveviewlistener.CameraLiveViewListenerImpl;
9 import net.osdn.gokigen.gr2control.liveview.liveviewlistener.ILiveViewListener;
10
11 import androidx.annotation.NonNull;
12
13 /**
14  *
15  *
16  */
17 public class RicohGr2LiveViewControl implements ILiveViewControl
18 {
19     private final String TAG = toString();
20     private final CameraLiveViewListenerImpl liveViewListener;
21     private final boolean useGrCommand;
22     private String cameraDisplayUrl = "http://192.168.0.1/v1/display";    //  カメラの画面をコピーする場合...
23     private String liveViewUrl = "http://192.168.0.1/v1/liveview";         //  何も表示しない(ライブビューモード)の場合...
24     private float cropScale = 1.0f;
25     private boolean whileFetching = false;
26     private static final int FETCH_ERROR_MAX = 30;
27
28     /**
29      *
30      *
31      */
32     RicohGr2LiveViewControl(boolean useGrControl)
33     {
34         this.useGrCommand = useGrControl;
35         liveViewListener = new CameraLiveViewListenerImpl();
36     }
37
38 /*
39     public void setLiveViewAddress(@NonNull String address, @NonNull String page)
40     {
41         cameraDisplayUrl = "http://" + address + "/" + page;
42     }
43 */
44
45     @Override
46     public void changeLiveViewSize(String size)
47     {
48         //
49     }
50
51     @Override
52     public void startLiveView(final boolean useCameraScreen)
53     {
54         final boolean isCameraScreen = useGrCommand && useCameraScreen;
55         Log.v(TAG, "startLiveView()");
56         try
57         {
58             Thread thread = new Thread(new Runnable()
59             {
60                 @Override
61                 public void run()
62                 {
63                     try
64                     {
65                         if (isCameraScreen)
66                         {
67                             start(cameraDisplayUrl);
68                         }
69                         else
70                         {
71                             start(liveViewUrl);
72                         }
73                     }
74                     catch (Exception e)
75                     {
76                         e.printStackTrace();
77                     }
78                 }
79             });
80             thread.start();
81         }
82         catch (Exception e)
83         {
84             e.printStackTrace();
85         }
86
87     }
88
89     @Override
90     public void stopLiveView()
91     {
92         Log.v(TAG, "stopLiveView()");
93         whileFetching = false;
94     }
95
96
97     private void start(@NonNull final String streamUrl)
98     {
99         if (whileFetching)
100         {
101             Log.v(TAG, "start() already starting.");
102         }
103         whileFetching = true;
104
105         // A thread for retrieving liveview data from server.
106         try
107         {
108             Thread thread = new Thread(new Runnable()
109             {
110                 @Override
111                 public void run()
112                 {
113                     Log.d(TAG, "Starting retrieving streaming data from server.");
114                     SimpleLiveviewSlicer slicer = null;
115                     int continuousNullDataReceived = 0;
116                     try
117                     {
118                         // Create Slicer to open the stream and parse it.
119                         slicer = new SimpleLiveviewSlicer();
120                         slicer.open(streamUrl);
121
122                         while (whileFetching)
123                         {
124                             final SimpleLiveviewSlicer.Payload payload = slicer.nextPayloadForMotionJpeg();
125                             if (payload == null)
126                             {
127                                 //Log.v(TAG, "Liveview Payload is null.");
128                                 continuousNullDataReceived++;
129                                 if (continuousNullDataReceived > FETCH_ERROR_MAX)
130                                 {
131                                     Log.d(TAG, " FETCH ERROR MAX OVER ");
132                                     break;
133                                 }
134                                 continue;
135                             }
136                             //if (mJpegQueue.size() == 2)
137                             //{
138                             //    mJpegQueue.remove();
139                             //}
140                             //mJpegQueue.add(payload.getJpegData());
141                             liveViewListener.onUpdateLiveView(payload.getJpegData(), null);
142                             continuousNullDataReceived = 0;
143                         }
144                     }
145                     catch (Exception e)
146                     {
147                         e.printStackTrace();
148                     }
149                     finally
150                     {
151                         try
152                         {
153                             if (slicer != null)
154                             {
155                                 slicer.close();
156                             }
157                         }
158                         catch (Exception e)
159                         {
160                             e.printStackTrace();
161                         }
162                         //mJpegQueue.clear();
163                         if ((!whileFetching)&&(continuousNullDataReceived > FETCH_ERROR_MAX))
164                         {
165                             // 再度ライブビューのスタートをやってみる。
166                             whileFetching = false;
167                             //continuousNullDataReceived = 0;
168                             start(streamUrl);
169                         }
170                     }
171                 }
172             });
173             thread.start();
174         }
175         catch (Exception e)
176         {
177             e.printStackTrace();
178         }
179     }
180
181
182     @Override
183     public void updateDigitalZoom()
184     {
185
186     }
187
188     /**
189      *   デジタルズーム倍率の設定値を応答する
190      *
191      */
192     @Override
193     public float getDigitalZoomScale()
194     {
195         return (1.0f);
196     }
197
198     /**
199      *   クロップサイズを変更する
200      *
201      */
202     @Override
203     public void updateMagnifyingLiveViewScale(final boolean isChangeScale)
204     {
205         //
206         try
207         {
208             if (isChangeScale)
209             {
210                 if (cropScale == 1.0f)
211                 {
212                     cropScale = 1.25f;
213                 }
214                 else if (cropScale == 1.25f)
215                 {
216                     cropScale = 1.68f;
217                 }
218                 else
219                 {
220                     cropScale = 1.0f;
221                 }
222             }
223             Thread thread = new Thread(new Runnable()
224             {
225                 @Override
226                 public void run()
227                 {
228                     try
229                     {
230                         String cropSize = "CROP_SIZE_ORIGINAL";
231                         int timeoutMs = 5000;
232                         String grCmdUrl = "http://192.168.0.1/_gr";
233                         String postData;
234                         String result;
235                         if (isChangeScale)
236                         {
237                             postData = "mpget=CROP_SHOOTING";
238                             result = SimpleHttpClient.httpPost(grCmdUrl, postData, timeoutMs);
239                             if ((result == null) || (result.length() < 1))
240                             {
241                                 Log.v(TAG, "reply is null.");
242                                 cropScale = 1.0f;
243                             } else if (result.contains("SIZE_M")) {
244                                 cropSize = "CROP_SIZE_S";
245                                 cropScale = 1.68f;
246                             } else if (result.contains("SIZE_S")) {
247                                 cropSize = "CROP_SIZE_ORIGINAL";
248                                 cropScale = 1.0f;
249                             } else {
250                                 cropSize = "CROP_SIZE_M";
251                                 cropScale = 1.25f;
252                             }
253                         }
254                         postData = "mpset=CROP_SHOOTING " + cropSize;
255                         result = SimpleHttpClient.httpPost(grCmdUrl, postData, timeoutMs);
256                         Log.v(TAG, "RESULT1 : " + result);
257
258                         postData = "cmd=mode refresh";
259                         result = SimpleHttpClient.httpPost(grCmdUrl, postData, timeoutMs);
260                         Log.v(TAG, "RESULT2 : " + result);
261                     }
262                     catch (Exception e)
263                     {
264                         e.printStackTrace();
265                     }
266                 }
267             });
268             thread.start();
269         }
270         catch (Exception e)
271         {
272             e.printStackTrace();
273         }
274     }
275
276     /**
277      *   ライブビュー拡大倍率の設定値を応答する
278      *
279      */
280     @Override
281     public float getMagnifyingLiveViewScale()
282     {
283         return (cropScale);
284     }
285
286     public ILiveViewListener getLiveViewListener()
287     {
288         return (liveViewListener);
289     }
290 }