OSDN Git Service

タッチフォーカスの制御。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / olympuspen / wrapper / status / OlympusPenCameraStatusWatcher.java
1 package net.osdn.gokigen.a01d.camera.olympuspen.wrapper.status;
2
3 import android.util.Log;
4
5 import androidx.annotation.NonNull;
6
7 import net.osdn.gokigen.a01d.camera.ICameraStatus;
8 import net.osdn.gokigen.a01d.camera.ICameraStatusWatcher;
9 import net.osdn.gokigen.a01d.liveview.ICameraStatusUpdateNotify;
10
11 import java.util.List;
12
13 import static net.osdn.gokigen.a01d.camera.utils.SimpleLogDumper.dump_bytes;
14
15 public class OlympusPenCameraStatusWatcher implements ICameraStatusWatcher, ICameraStatus
16 {
17     private final String TAG = toString();
18
19     private byte[] buffer = null;
20     private boolean isWatching = false;
21     private boolean statusReceived = false;
22     private final int SLEEP_TIME_MS = 250;
23     private ICameraStatusUpdateNotify notifier = null;
24     private int focusingStatus = 0;
25     private static final int ID_FRAME_SIZE = 1;
26     private static final int ID_AF_FRAME_INFO = 2;
27     private static final int ID_ZOOM_LENS_INFO = 18;
28
29
30     public OlympusPenCameraStatusWatcher()
31     {
32
33     }
34
35     public void setRtpHeader(byte[] byteBuffer)
36     {
37         try
38         {
39             buffer = byteBuffer;
40             statusReceived = true;
41         }
42         catch (Exception e)
43         {
44             e.printStackTrace();
45             statusReceived = false;
46         }
47     }
48
49     @Override
50     public void startStatusWatch(@NonNull ICameraStatusUpdateNotify notifier)
51     {
52         try
53         {
54             this.notifier = notifier;
55             Thread thread = new Thread(new Runnable() {
56                 @Override
57                 public void run()
58                 {
59                     int waitMs = SLEEP_TIME_MS;
60                     isWatching = true;
61                     while (isWatching)
62                     {
63                         if (statusReceived)
64                         {
65                             // データを解析する
66                             parseRtpHeader();
67                             statusReceived = false;
68                         }
69                         sleep(waitMs);
70                     }
71                 }
72             });
73             thread.start();
74         }
75         catch (Exception e)
76         {
77             e.printStackTrace();
78         }
79     }
80
81     private void sleep(int waitMs)
82     {
83         try
84         {
85             Thread.sleep(waitMs);
86         }
87         catch (Exception e)
88         {
89             e.printStackTrace();
90         }
91     }
92
93     private void parseRtpHeader()
94     {
95         try
96         {
97             if (buffer == null)
98             {
99                 Log.v(TAG, " parseRtpHeader() : null");
100                 return;
101             }
102             int position = 16;
103             int maxLength = buffer.length;
104             while ((position + 4) < maxLength)
105             {
106                 int id = ((buffer[position] & 0xff) * 256) + (buffer[position + 1] & 0xff);
107                 int length = ((buffer[position + 2] & 0xff) * 256) + (buffer[position + 3] & 0xff);
108                 switch (id)
109                 {
110                     case ID_AF_FRAME_INFO:
111                         // 合焦状況の把握
112                         checkFocused(buffer, position, length);
113                         break;
114
115                     case ID_ZOOM_LENS_INFO:
116                         // レンズのズーム情報
117                     case ID_FRAME_SIZE:
118                     default:
119                         // Log.v(TAG, " ID : " + id + "  LENGTH : " + length);
120                         break;
121                 }
122                 position = position + 4 + (length * 4);  // header : 4bytes , data : length * 4 bytes
123             }
124         }
125         catch (Exception e)
126         {
127             e.printStackTrace();
128         }
129     }
130
131     private void checkFocused(byte[] buffer, int position, int length)
132     {
133         if (length != 5)
134         {
135             // データがそろっていないので何もしない
136             return;
137         }
138         int status = (buffer[position + 7] & 0xff);
139         if (status != focusingStatus)
140         {
141             boolean focus = (status == 1);
142             boolean isError = (status == 2);
143             //Log.v(TAG, "FOCUS : " + focus + "(" + isError + ") " + status);
144             notifier.updateFocusedStatus(focus, isError);
145             focusingStatus = status;
146         }
147     }
148
149     @Override
150     public void stopStatusWatch()
151     {
152         isWatching = false;
153     }
154
155     @Override
156     public List<String> getStatusList(String key)
157     {
158         return null;
159     }
160
161     @Override
162     public String getStatus(String key)
163     {
164         return null;
165     }
166
167     @Override
168     public void setStatus(String key, String value)
169     {
170
171     }
172 }