OSDN Git Service

PZの制御を追加。
[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
28
29     public OlympusPenCameraStatusWatcher()
30     {
31
32     }
33
34     public void setRtpHeader(byte[] byteBuffer)
35     {
36         try
37         {
38             buffer = byteBuffer;
39             statusReceived = true;
40         }
41         catch (Exception e)
42         {
43             e.printStackTrace();
44             statusReceived = false;
45         }
46     }
47
48     @Override
49     public void startStatusWatch(@NonNull ICameraStatusUpdateNotify notifier)
50     {
51         try
52         {
53             this.notifier = notifier;
54             Thread thread = new Thread(new Runnable() {
55                 @Override
56                 public void run()
57                 {
58                     int waitMs = SLEEP_TIME_MS;
59                     isWatching = true;
60                     while (isWatching)
61                     {
62                         if (statusReceived)
63                         {
64                             // データを解析する
65                             parseRtpHeader();
66                             statusReceived = false;
67                         }
68                         sleep(waitMs);
69                     }
70                 }
71             });
72             thread.start();
73         }
74         catch (Exception e)
75         {
76             e.printStackTrace();
77         }
78     }
79
80     private void sleep(int waitMs)
81     {
82         try
83         {
84             Thread.sleep(waitMs);
85         }
86         catch (Exception e)
87         {
88             e.printStackTrace();
89         }
90     }
91
92     private void parseRtpHeader()
93     {
94         try
95         {
96             if (buffer == null)
97             {
98                 Log.v(TAG, " parseRtpHeader() : null");
99                 return;
100             }
101             int position = 16;
102             int maxLength = buffer.length;
103             while ((position + 4) < maxLength)
104             {
105                 int id = ((buffer[position] & 0xff) * 256) + (buffer[position + 1] & 0xff);
106                 int length = ((buffer[position + 2] & 0xff) * 256) + (buffer[position + 3] & 0xff);
107                 switch (id)
108                 {
109                     case ID_AF_FRAME_INFO:
110                         // 合焦状況の把握
111                         checkFocused(buffer, position, length);
112                         break;
113
114                     case ID_FRAME_SIZE:
115                     default:
116                         // Log.v(TAG, " ID : " + id + "  LENGTH : " + length);
117                         break;
118                 }
119                 position = position + 4 + (length * 4);  // header : 4bytes , data : length * 4 bytes
120             }
121         }
122         catch (Exception e)
123         {
124             e.printStackTrace();
125         }
126     }
127
128     private void checkFocused(byte[] buffer, int position, int length)
129     {
130         if (length != 5)
131         {
132             // データがそろっていないので何もしない
133             return;
134         }
135         int status = (buffer[position + 7] & 0xff);
136         if (status != focusingStatus)
137         {
138             boolean focus = (status == 1);
139             boolean isError = (status == 2);
140             notifier.updateFocusedStatus(focus, isError);
141             focusingStatus = status;
142         }
143     }
144
145     @Override
146     public void stopStatusWatch()
147     {
148         isWatching = false;
149     }
150
151     @Override
152     public List<String> getStatusList(String key)
153     {
154         return null;
155     }
156
157     @Override
158     public String getStatus(String key)
159     {
160         return null;
161     }
162
163     @Override
164     public void setStatus(String key, String value)
165     {
166
167     }
168 }