OSDN Git Service

PとSの制御コードを a01d からマージ。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / sony / wrapper / SonyLiveViewControl.java
1 package net.osdn.gokigen.pkremote.camera.vendor.sony.wrapper;
2
3 import android.util.Log;
4
5 import androidx.annotation.NonNull;
6
7 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.ILiveViewControl;
8 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.ILiveViewListener;
9 import net.osdn.gokigen.pkremote.camera.liveview.CameraLiveViewListenerImpl;
10 import net.osdn.gokigen.pkremote.camera.utils.SimpleLiveviewSlicer;
11
12 import org.json.JSONArray;
13 import org.json.JSONObject;
14
15 public class SonyLiveViewControl implements ILiveViewControl
16 {
17     private final String TAG = toString();
18     private final ISonyCameraApi cameraApi;
19     //private final BlockingQueue<byte[]> mJpegQueue = new ArrayBlockingQueue<>(2);
20     private final CameraLiveViewListenerImpl liveViewListener;
21     private boolean whileFetching = false;
22     private static final int FETCH_ERROR_MAX = 30;
23
24     SonyLiveViewControl(@NonNull ISonyCameraApi cameraApi)
25     {
26         this.cameraApi = cameraApi;
27         liveViewListener = new CameraLiveViewListenerImpl();
28     }
29
30     @Override
31     public void changeLiveViewSize(String size)
32     {
33
34     }
35
36     @Override
37     public void startLiveView(boolean isCameraScreen)
38     {
39         Log.v(TAG, "startLiveView() : " + isCameraScreen);
40         try
41         {
42             Thread thread = new Thread(new Runnable()
43             {
44                 @Override
45                 public void run()
46                 {
47                     try
48                     {
49                         JSONObject replyJson;
50                         replyJson = cameraApi.startLiveview();
51                         if (!SonyCameraApi.isErrorReply(replyJson))
52                         {
53                             try
54                             {
55                                 JSONArray resultsObj = replyJson.getJSONArray("result");
56                                 if (1 <= resultsObj.length())
57                                 {
58                                     // Obtain liveview URL from the result.
59                                     final String liveviewUrl = resultsObj.getString(0);
60                                     start(liveviewUrl);
61                                 }
62                             }
63                             catch (Exception e)
64                             {
65                                 e.printStackTrace();
66                             }
67                         }
68                     }
69                     catch (Exception e)
70                     {
71                         e.printStackTrace();
72                     }
73                 }
74             });
75             thread.start();
76         }
77         catch (Exception e)
78         {
79             e.printStackTrace();
80         }
81     }
82
83     @Override
84     public void stopLiveView()
85     {
86         Log.v(TAG, "stopLiveView()");
87         try
88         {
89             Thread thread = new Thread(new Runnable()
90             {
91                 @Override
92                 public void run()
93                 {
94                     try
95                     {
96                         JSONObject resultsObj = cameraApi.stopLiveview();
97                         if (resultsObj == null)
98                         {
99                             Log.v(TAG, "stopLiveview() reply is null.");
100                         }
101                     }
102                     catch (Exception e)
103                     {
104                         e.printStackTrace();
105                     }
106                 }
107             });
108             thread.start();
109         }
110         catch (Exception e)
111         {
112             e.printStackTrace();
113         }
114     }
115
116     @Override
117     public void updateDigitalZoom()
118     {
119
120     }
121
122     @Override
123     public void updateMagnifyingLiveViewScale(boolean isChangeScale)
124     {
125
126     }
127
128     @Override
129     public float getMagnifyingLiveViewScale()
130     {
131         return (1.0f);
132     }
133
134     @Override
135     public float getDigitalZoomScale()
136     {
137         return (1.0f);
138     }
139
140
141
142     public boolean start(final String streamUrl)
143     {
144         if (streamUrl == null)
145         {
146             Log.e(TAG, "start() streamUrl is null.");
147             return (false);
148         }
149         if (whileFetching)
150         {
151             Log.v(TAG, "start() already starting.");
152         }
153         whileFetching = true;
154
155         // A thread for retrieving liveview data from server.
156         try
157         {
158             Thread thread = new Thread(new Runnable()
159             {
160                 @Override
161                 public void run()
162                 {
163                     Log.d(TAG, "Starting retrieving streaming data from server.");
164                     SimpleLiveviewSlicer slicer = null;
165                     int continuousNullDataReceived = 0;
166                     try
167                     {
168                         // Create Slicer to open the stream and parse it.
169                         slicer = new SimpleLiveviewSlicer();
170                         slicer.open(streamUrl);
171
172                         while (whileFetching)
173                         {
174                             final SimpleLiveviewSlicer.Payload payload = slicer.nextPayload();
175                             if (payload == null)
176                             {
177                                 //Log.v(TAG, "Liveview Payload is null.");
178                                 continuousNullDataReceived++;
179                                 if (continuousNullDataReceived > FETCH_ERROR_MAX)
180                                 {
181                                     Log.d(TAG, " FETCH ERROR MAX OVER ");
182                                     break;
183                                 }
184                                 continue;
185                             }
186                             //if (mJpegQueue.size() == 2)
187                             //{
188                             //    mJpegQueue.remove();
189                             //}
190                             //mJpegQueue.add(payload.getJpegData());
191                             liveViewListener.onUpdateLiveView(payload.getJpegData(), null);
192                             continuousNullDataReceived = 0;
193                         }
194                     }
195                     catch (Exception e)
196                     {
197                         e.printStackTrace();
198                     }
199                     finally
200                     {
201                         try
202                         {
203                             if (slicer != null)
204                             {
205                                 slicer.close();
206                             }
207                         }
208                         catch (Exception e)
209                         {
210                             e.printStackTrace();
211                         }
212                         //mJpegQueue.clear();
213                         if ((!whileFetching)&&(continuousNullDataReceived > FETCH_ERROR_MAX))
214                         {
215                             // 再度ライブビューのスタートをやってみる。
216                             whileFetching = false;
217                             //continuousNullDataReceived = 0;
218                             start(streamUrl);
219                         }
220                     }
221                 }
222             });
223             thread.start();
224         }
225         catch (Exception e)
226         {
227             e.printStackTrace();
228         }
229         return (true);
230     }
231
232     public ILiveViewListener getLiveViewListener()
233     {
234         return (liveViewListener);
235     }
236 }