OSDN Git Service

c3f85daca4fea59247cefba4ee430617467c90fe
[gokigen/mangle.git] / app / src / main / java / jp / osdn / gokigen / gokigenassets / camera / vendor / omds / status / OmdsCameraStatusWatcher.kt
1 package jp.osdn.gokigen.gokigenassets.camera.vendor.omds.status
2
3 import android.graphics.Color
4 import android.util.Log
5 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatus
6 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatusUpdateNotify
7 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatusWatcher
8 import jp.osdn.gokigen.gokigenassets.liveview.message.IMessageDrawer
9 import java.lang.Exception
10 import kotlin.collections.ArrayList
11
12 class OmdsCameraStatusWatcher() : ICameraStatusWatcher, ICameraStatus
13 {
14     private var buffer: ByteArray? = null
15     private var isWatching = false
16     private var statusReceived = false
17     private var notifier: ICameraStatusUpdateNotify? = null
18     private var focusingStatus = 0
19
20     fun setRtpHeader(byteBuffer: ByteArray?)
21     {
22         try
23         {
24             //buffer = byteBuffer?.copyOf()
25             if (byteBuffer != null)
26             {
27                 buffer = byteBuffer
28                 statusReceived = true
29             }
30             else
31             {
32                 statusReceived = false
33             }
34         }
35         catch (e: Exception)
36         {
37             e.printStackTrace()
38             statusReceived = false
39         }
40     }
41
42     override fun startStatusWatch(indicator: IMessageDrawer?, notifier: ICameraStatusUpdateNotify?)
43     {
44         try
45         {
46             this.notifier = notifier
47             val thread = Thread {
48                 val waitMs = SLEEP_TIME_MS
49                 isWatching = true
50                 while (isWatching)
51                 {
52                     if (statusReceived)
53                     {
54                         // データを解析する
55                         parseRtpHeader()
56                         statusReceived = false
57                     }
58                     sleep(waitMs)
59                 }
60             }
61             thread.start()
62         }
63         catch (e: Exception)
64         {
65             e.printStackTrace()
66         }
67     }
68
69     private fun sleep(waitMs: Int)
70     {
71         try
72         {
73             Thread.sleep(waitMs.toLong())
74         }
75         catch (e: Exception)
76         {
77             e.printStackTrace()
78         }
79     }
80
81     private fun parseRtpHeader()
82     {
83         try
84         {
85             if (buffer == null)
86             {
87                 Log.v(TAG, " parseRtpHeader() : null")
88                 return
89             }
90             var position = 16
91             val maxLength = buffer?.size ?: 0
92             while (position + 4 < maxLength)
93             {
94                 val id: Int = ((buffer?.get(position) ?: 0).toInt() and 0xff) * 256 + ((buffer?.get(position + 1) ?: 0).toInt() and 0xff)
95                 val length: Int = ((buffer?.get(position + 2) ?: 0).toInt() and 0xff) * 256 + ((buffer?.get(position + 3) ?: 0).toInt() and 0xff)
96                 when (id)
97                 {
98                     ID_AF_FRAME_INFO -> { checkFocused(buffer, position, length) }
99                     ID_ZOOM_LENS_INFO, ID_FRAME_SIZE -> { }
100                     else -> { }
101                 }
102                 position += 4 + length * 4 // header : 4bytes , data : length * 4 bytes
103             }
104         }
105         catch (e: Exception)
106         {
107             e.printStackTrace()
108         }
109     }
110
111     private fun checkFocused(buffer: ByteArray?, position: Int, length: Int)
112     {
113         if ((length != 5)||(buffer == null))
114         {
115             // データがそろっていないので何もしない
116             return
117         }
118         val status: Int = buffer[position + 7].toInt() and 0xff
119         if (status != focusingStatus)
120         {
121             // ドライブ停止時には、マーカの色は消さない
122             if (status > 0)
123             {
124                 val focus = status == 1
125                 val isError = status == 2
126                 notifier?.updateFocusedStatus(focus, isError)
127             }
128             focusingStatus = status
129         }
130     }
131
132     override fun stopStatusWatch()
133     {
134         isWatching = false
135     }
136
137     override fun getStatusList(key: String): List<String>
138     {
139         return (ArrayList())
140     }
141
142     override fun getStatus(key: String): String
143     {
144         return ("")
145     }
146
147     override fun getStatusColor(key: String): Int
148     {
149         return (Color.WHITE)
150     }
151
152     override fun setStatus(key: String, value: String)
153     {
154
155     }
156
157     companion object
158     {
159         private val TAG = OmdsCameraStatusWatcher::class.java.simpleName
160
161         private const val SLEEP_TIME_MS = 250
162         private const val ID_FRAME_SIZE = 1
163         private const val ID_AF_FRAME_INFO = 2
164         private const val ID_ZOOM_LENS_INFO = 18
165     }
166
167 }