1 package net.osdn.gokigen.a01d.camera.fujix.wrapper.liveview
3 import android.app.Activity
4 import android.util.Log
5 import androidx.preference.PreferenceManager
6 import net.osdn.gokigen.a01d.camera.ILiveViewControl
7 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.IFujiXCommunication
8 import net.osdn.gokigen.a01d.camera.utils.SimpleLogDumper
9 import net.osdn.gokigen.a01d.liveview.liveviewlistener.CameraLiveViewListenerImpl
10 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener
11 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor
12 import java.net.Socket
15 class FujiXLiveViewControl(activity: Activity, private val ipAddress: String, private val portNumber: Int) : ILiveViewControl, IFujiXCommunication
17 private val TAG = toString()
18 private val liveViewListener = CameraLiveViewListenerImpl()
19 private var waitMs = 0
20 private var isStart = false
21 private val logcat = false
22 private val preferences = PreferenceManager.getDefaultSharedPreferences(activity)
28 val waitMsStr = preferences.getString(IPreferencePropertyAccessor.FUJIX_LIVEVIEW_WAIT, IPreferencePropertyAccessor.FUJIX_LIVEVIEW_WAIT_DEFAULT_VALUE)
29 logcat(" waitMS : $waitMsStr")
30 if (waitMsStr != null)
32 val wait = waitMsStr.toInt()
33 if (wait in 20 .. 800)
44 Log.v(TAG, " LOOP WAIT : $waitMs ms")
47 override fun startLiveView()
52 Log.v(TAG, " LiveView IS ALREADY STARTED")
61 startReceive(Socket(ipAddress, portNumber))
65 Log.v(TAG, " IP : $ipAddress port : $portNumber")
76 override fun stopLiveView()
81 override fun updateDigitalZoom()
86 override fun updateMagnifyingLiveViewScale(isChangeScale: Boolean)
91 override fun getMagnifyingLiveViewScale(): Float
96 override fun changeLiveViewSize(size: String)
101 override fun getDigitalZoomScale(): Float
106 fun getLiveViewListener(): ILiveViewListener
108 return (liveViewListener)
111 override fun connect(): Boolean
116 override fun disconnect()
121 private fun logcat(message: String)
129 private fun dump_bytes(header : String, byteArray: ByteArray, size : Int = 24)
133 SimpleLogDumper.dump_bytes(header, byteArray.copyOf(size))
137 private fun startReceive(socket: Socket)
140 val isr = socket.getInputStream()
141 val byteArray = ByteArray(BUFFER_SIZE + 32)
148 var length_bytes: Int
149 var read_bytes = isr.read(byteArray, 0, BUFFER_SIZE)
150 if (read_bytes > DATA_HEADER_OFFSET)
152 // メッセージボディの先頭にあるメッセージ長分は読み込む
153 length_bytes = (byteArray[3].toInt() and 0xff shl 24) + (byteArray[2].toInt() and 0xff shl 16) + (byteArray[1].toInt() and 0xff shl 8) + (byteArray[0].toInt() and 0xff)
154 if (byteArray[18] == 0xff.toByte() && byteArray[19] == 0xd8.toByte())
157 while (read_bytes < length_bytes && read_bytes < BUFFER_SIZE && length_bytes <= BUFFER_SIZE)
159 val append_bytes = isr.read(byteArray, read_bytes, length_bytes - read_bytes)
160 logcat("READ AGAIN : $append_bytes [$read_bytes]")
161 if (append_bytes < 0)
165 read_bytes = read_bytes + append_bytes
167 logcat("READ BYTES : " + read_bytes + " (" + length_bytes + " bytes, " + waitMs + "ms)")
171 // ウェイトを短めに入れてマーカーを拾うまで待つ
172 Thread.sleep(waitMs / 4.toLong())
173 logcat(" --- wait LiveView ---")
179 dump_bytes("[LV]", byteArray)
182 liveViewListener.onUpdateLiveView(Arrays.copyOfRange(byteArray, DATA_HEADER_OFFSET, read_bytes - DATA_HEADER_OFFSET), null)
185 Thread.sleep(waitMs.toLong())
193 if (errorCount > ERROR_LIMIT)
195 // エラーが連続でたくさん出たらループをストップ(ライブビューを停止)させる
211 private fun startReceiveAlter(socket: Socket)
214 val isr = socket.getInputStream()
215 val byteArray = ByteArray(BUFFER_SIZE + 32)
222 var length_bytes: Int
223 var read_bytes = isr.read(byteArray, 0, BUFFER_SIZE)
226 // dump_bytes("[lv]", byteArray, 48)
227 if (read_bytes > DATA_HEADER_OFFSET)
229 // メッセージボディの先頭にあるメッセージ長分は読み込む
230 length_bytes = (byteArray[3].toInt() and 0xff shl 24) + (byteArray[2].toInt() and 0xff shl 16) + (byteArray[1].toInt() and 0xff shl 8) + (byteArray[0].toInt() and 0xff)
231 if (byteArray[18] == 0xff.toByte() && byteArray[19] == 0xd8.toByte())
234 while (read_bytes < length_bytes && read_bytes < BUFFER_SIZE && length_bytes <= BUFFER_SIZE)
236 val append_bytes = isr.read(byteArray, read_bytes, length_bytes - read_bytes)
237 logcat("READ AGAIN : $append_bytes [$read_bytes]")
238 if (append_bytes < 0)
242 read_bytes = read_bytes + append_bytes
244 logcat("READ BYTES : " + read_bytes + " (" + length_bytes + " bytes, " + waitMs + "ms)")
248 // ウェイトを短めに入れてマーカーを拾うまで待つ
249 Thread.sleep(waitMs / 4.toLong())
250 logcat(" --- wait LiveView ---")
256 dump_bytes("[LV]", byteArray)
259 liveViewListener.onUpdateLiveView(Arrays.copyOfRange(byteArray, DATA_HEADER_OFFSET, read_bytes - DATA_HEADER_OFFSET), null)
262 Thread.sleep(waitMs.toLong())
270 if (errorCount > ERROR_LIMIT)
272 // エラーが連続でたくさん出たらループをストップ(ライブビューを停止)させる
291 private const val DATA_HEADER_OFFSET = 18
292 private const val BUFFER_SIZE = 2048 * 1280
293 private const val ERROR_LIMIT = 30