From 1efd9ce93064098d37f47b2e632f72bae0ac4031 Mon Sep 17 00:00:00 2001 From: MRSa Date: Sun, 3 Oct 2021 23:11:03 +0900 Subject: [PATCH] =?utf8?q?OPC=E6=A9=9F=E7=94=A8=E3=81=AE=E3=82=A4=E3=83=99?= =?utf8?q?=E3=83=B3=E3=83=88=E5=8F=97=E4=BF=A1=E3=83=AD=E3=82=B8=E3=83=83?= =?utf8?q?=E3=82=AF=E3=82=92=E5=85=A5=E3=82=8C=E3=81=A6=E3=81=BF=E3=81=9F?= =?utf8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../omds/connection/OmdsCameraConnectSequence.kt | 2 + .../vendor/omds/status/IOmdsCommunicationInfo.kt | 1 + .../vendor/omds/status/OmdsCameraStatusWatcher.kt | 154 ++++++++++++++++++++- .../liveview/image/CameraLiveViewListenerImpl.kt | 7 +- 4 files changed, 159 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/connection/OmdsCameraConnectSequence.kt b/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/connection/OmdsCameraConnectSequence.kt index 2977f1f..f09ee02 100644 --- a/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/connection/OmdsCameraConnectSequence.kt +++ b/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/connection/OmdsCameraConnectSequence.kt @@ -51,6 +51,8 @@ class OmdsCameraConnectSequence(private val context: AppCompatActivity, private { Log.v(TAG, " -=-=-=-=-=- DETECTED OPC CAMERA -=-=-=-=-=-") useOpcProtocolNotify.detectedOpcProtocol(true) + + communicationInfo.startReceiveOpcEvent() } } diff --git a/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/IOmdsCommunicationInfo.kt b/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/IOmdsCommunicationInfo.kt index feca2cf..aa62688 100644 --- a/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/IOmdsCommunicationInfo.kt +++ b/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/IOmdsCommunicationInfo.kt @@ -3,4 +3,5 @@ package jp.osdn.gokigen.gokigenassets.camera.vendor.omds.status interface IOmdsCommunicationInfo { fun setOmdsCommandList(commandList: String) + fun startReceiveOpcEvent() } diff --git a/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/OmdsCameraStatusWatcher.kt b/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/OmdsCameraStatusWatcher.kt index ef63f4f..127a5ea 100644 --- a/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/OmdsCameraStatusWatcher.kt +++ b/app/src/main/java/jp/osdn/gokigen/gokigenassets/camera/vendor/omds/status/OmdsCameraStatusWatcher.kt @@ -9,7 +9,13 @@ import jp.osdn.gokigen.gokigenassets.camera.vendor.omds.IOmdsProtocolNotify import jp.osdn.gokigen.gokigenassets.liveview.message.IMessageDrawer import jp.osdn.gokigen.gokigenassets.utils.communication.SimpleHttpClient import jp.osdn.gokigen.gokigenassets.utils.communication.SimpleLogDumper +import java.io.ByteArrayOutputStream +import java.io.InputStream import java.lang.Exception +import java.net.HttpURLConnection +import java.net.ServerSocket +import java.net.Socket +import java.net.URL import java.util.* import kotlin.collections.ArrayList @@ -17,11 +23,13 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va { private val headerMap: MutableMap = HashMap() private val http = SimpleHttpClient() + private var useOpcProtocol = false private var buffer: ByteArray? = null private var isWatching = false private var isWatchingEvent = false + private var whileEventReceive = false private var statusReceived = false private var omdsCommandList : String = "" private var latestEventResponse : String = "" @@ -51,7 +59,6 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va private var opcExpRevSelectionList = "" private var opcFocusModeSelectionList = "" - override fun setOmdsCommandList(commandList: String) { omdsCommandList = commandList @@ -62,6 +69,12 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va startStatusWatch(null, null) } + override fun startReceiveOpcEvent() + { + // OPCの場合は、、イベントも監視する + startEventWatch() + } + fun setRtpHeader(byteBuffer: ByteArray?) { try @@ -83,13 +96,142 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va } } + + private fun startEventWatch(portNumber: Int = 65000) + { + if (whileEventReceive) + { + Log.v(TAG, "startReceiveStream() : already starting.") + return + } + + // イベント受信用の準備... + finishEventReceiverThread() + requestOpcEventWatch() + + // 受信スレッドを動かす + val thread = Thread { eventReceiverThread(portNumber) } + try + { + thread.start() + } + catch (e: Exception) + { + e.printStackTrace() + } + } + + private fun requestOpcEventWatch(portNo: Int = 65000) + { + try + { + // OPC機のイベント通知開始 + val eventWatchUrl = "$executeUrl/start_pushevent.cgi?port=$portNo" + Log.v(TAG, " requestOpcEventWatch : $eventWatchUrl") + val response = http.httpGetWithHeader(eventWatchUrl, headerMap, null, TIMEOUT_MS) ?: "" + if (response.isNotEmpty()) + { + dumpLog(eventWatchUrl, response) + } + } + catch (e: Exception) + { + e.printStackTrace() + } + } + + private fun eventReceiverThread(portNumber: Int) + { + try + { + //finishEventReceiverThread() + //requestOpcEventWatch() + whileEventReceive = true + val bufferSize = RECEIVE_BUFFER_SIZE + val byteArray = ByteArray(bufferSize) + val hostName = executeUrl.substring("http://".length) + Log.v(TAG, " OPC: EVENT LISTEN : $hostName, $portNumber") + val eventReceiveSocket = Socket(hostName, portNumber) + val inputStream = eventReceiveSocket.getInputStream() + while (whileEventReceive) + { + try + { + sleep(SLEEP_EVENT_TIME_MS) + val dataBytes = inputStream.available() + if (dataBytes > 0) + { + // データがあった...受信する + Log.v(TAG, " RECEIVE OPC EVENT : $dataBytes bytes") + val byteStream = ByteArrayOutputStream() + var readIndex = 0 + while (readIndex < dataBytes) + { + val readBytes = inputStream.read(byteArray, 0, bufferSize) + if (readBytes <= 0) + { + Log.v(TAG, " RECEIVED MESSAGE FINISHED ($dataBytes)") + break + } + readIndex += readBytes + byteStream.write(byteArray, 0, readBytes) + } + if (isDumpLog) + { + SimpleLogDumper.dumpBytes("[RX EVT(OPC):$dataBytes]", byteStream.toByteArray()) + } + } + else + { + if (isDumpLog) + { + Log.v(TAG, " NOT RECEIVE OPC EVENT ...WAIT AGAIN...") + } + } + sleep(SLEEP_EVENT_TIME_MS) + } + catch (e: Exception) + { + e.printStackTrace() + whileEventReceive = false + finishEventReceiverThread() + } + } + //finishEventReceiverThread() + System.gc() + } + catch (e: Exception) + { + e.printStackTrace() + } + } + + private fun finishEventReceiverThread() + { + try + { + Log.v(TAG, "finishEventReceiverThread()") + + // OPC機のイベント通知開始 + val eventWatchUrl = "$executeUrl/stop_pushevent.cgi" + val response = http.httpGetWithHeader(eventWatchUrl, headerMap, null, TIMEOUT_MS) ?: "" + if (response.isNotEmpty()) + { + dumpLog(eventWatchUrl, response) + } + } + catch (e: Exception) + { + e.printStackTrace() + } + } + override fun startStatusWatch(indicator: IMessageDrawer?, notifier: ICameraStatusUpdateNotify?) { try { startRtpStatusWatch() startEventStatusWatch() - } catch (e: Exception) { @@ -127,7 +269,7 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va { try { - Log.v(TAG, " startEventStatusWatch()") + Log.v(TAG, " startEventStatusWatch() : $useOpcProtocol") val thread = Thread { isWatchingEvent = true while (isWatchingEvent) @@ -703,7 +845,9 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va { isWatching = false isWatchingEvent = false + whileEventReceive = false + finishEventReceiverThread() } override fun getStatusList(key: String): List @@ -930,6 +1074,8 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va { headerMap["User-Agent"] = userAgent // "OlympusCameraKit" // "OI.Share" headerMap["X-Protocol"] = userAgent // "OlympusCameraKit" // "OI.Share" + + //startEventWatch() } interface IPropertyListCallback @@ -946,6 +1092,8 @@ class OmdsCameraStatusWatcher(userAgent: String = "OlympusCameraKit", private va private const val SLEEP_EVENT_TIME_MS = 350 private const val TIMEOUT_MS = 2500 + private const val RECEIVE_BUFFER_SIZE = 16384 + // RTP HEADER IDs private const val ID_FRAME_SIZE = 0x01 private const val ID_AF_FRAME_INFO = 0x02 diff --git a/app/src/main/java/jp/osdn/gokigen/gokigenassets/liveview/image/CameraLiveViewListenerImpl.kt b/app/src/main/java/jp/osdn/gokigen/gokigenassets/liveview/image/CameraLiveViewListenerImpl.kt index 5130ddf..29443af 100644 --- a/app/src/main/java/jp/osdn/gokigen/gokigenassets/liveview/image/CameraLiveViewListenerImpl.kt +++ b/app/src/main/java/jp/osdn/gokigen/gokigenassets/liveview/image/CameraLiveViewListenerImpl.kt @@ -267,8 +267,11 @@ class CameraLiveViewListenerImpl(private val context: Context, private val info val rotationMatrix = Matrix() rotationMatrix.postRotate(image.rotationDegrees.toFloat()) imageBitmap = BitmapFactory.decodeByteArray(image.imageData, 0, image.imageData.size) - imageBitmap = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.width, imageBitmap.height, rotationMatrix, true) - System.gc() + if (imageBitmap != null) + { + imageBitmap = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.width, imageBitmap.height, rotationMatrix, true) + System.gc() + } } catch (t: Throwable) { -- 2.11.0