From ac2055c4dd1521fb617242418df3cdb3c9383278 Mon Sep 17 00:00:00 2001 From: Hyunseok Yang Date: Wed, 22 Jul 2020 15:51:36 +0900 Subject: [PATCH 1/2] Supporting camera Info message for multicamera plugin change camera info request message format --- Assets/Scripts/DevicePlugins/CameraPlugin.cs | 34 +++++++++++++--- Assets/Scripts/DevicePlugins/DevicePlugin.cs | 2 +- .../DevicePlugins/MultiCameraPlugin.cs | 40 ++++++++++++++++++- Assets/Scripts/Devices/MultiCamera.cs | 30 +++++++++++++- 4 files changed, 96 insertions(+), 10 deletions(-) diff --git a/Assets/Scripts/DevicePlugins/CameraPlugin.cs b/Assets/Scripts/DevicePlugins/CameraPlugin.cs index 04b06151..e23cca50 100644 --- a/Assets/Scripts/DevicePlugins/CameraPlugin.cs +++ b/Assets/Scripts/DevicePlugins/CameraPlugin.cs @@ -5,7 +5,6 @@ */ using System.IO; -using System.Text; using UnityEngine; using Stopwatch = System.Diagnostics.Stopwatch; using ProtoBuf; @@ -43,6 +42,11 @@ protected override void OnStart() AddThread(Sender); } + void OnDestroy() + { + memoryStreamForCameraInfo.Dispose(); + } + private void Sender() { Stopwatch sw = new Stopwatch(); @@ -67,19 +71,37 @@ private void Response() { var receivedBuffer = ReceiveRequest(); - var receivedString = Encoding.UTF8.GetString(receivedBuffer, 0, receivedBuffer.Length); + var requestMessage = ParsingCameraInfoRequest(ref memoryStreamForCameraInfo, receivedBuffer); // Debug.Log(subPartName + receivedString); - if (receivedBuffer != null && receivedString.Equals("request_camera_info")) + if (requestMessage != null && requestMessage.Name.Equals("request_camera_info")) { var cameraInfoMessage = cam.GetCameraInfo(); - ClearMemoryStream(ref memoryStreamForCameraInfo); - - Serializer.Serialize(memoryStreamForCameraInfo, cameraInfoMessage); + SetCameraInfoResponse(ref memoryStreamForCameraInfo, cameraInfoMessage); SendResponse(memoryStreamForCameraInfo); } } } + + public static messages.Param ParsingCameraInfoRequest(ref MemoryStream msCameraInfo, in byte[] receivedBuffer) + { + ClearMemoryStream(ref msCameraInfo); + + msCameraInfo.Write(receivedBuffer, 0, receivedBuffer.Length); + msCameraInfo.Position = 0; + + return Serializer.Deserialize(msCameraInfo); + } + + public static void SetCameraInfoResponse(ref MemoryStream msCameraInfo,in messages.CameraSensor sensorInfo) + { + if (sensorInfo != null) + { + ClearMemoryStream(ref msCameraInfo); + + Serializer.Serialize(msCameraInfo, sensorInfo); + } + } } \ No newline at end of file diff --git a/Assets/Scripts/DevicePlugins/DevicePlugin.cs b/Assets/Scripts/DevicePlugins/DevicePlugin.cs index 2d3d0518..636147ac 100644 --- a/Assets/Scripts/DevicePlugins/DevicePlugin.cs +++ b/Assets/Scripts/DevicePlugins/DevicePlugin.cs @@ -197,7 +197,7 @@ public void Reset() OnReset(); } - protected void ClearMemoryStream(ref MemoryStream ms) + protected static void ClearMemoryStream(ref MemoryStream ms) { if (ms != null) { diff --git a/Assets/Scripts/DevicePlugins/MultiCameraPlugin.cs b/Assets/Scripts/DevicePlugins/MultiCameraPlugin.cs index 7f794646..caa89b0a 100644 --- a/Assets/Scripts/DevicePlugins/MultiCameraPlugin.cs +++ b/Assets/Scripts/DevicePlugins/MultiCameraPlugin.cs @@ -4,21 +4,29 @@ * SPDX-License-Identifier: MIT */ +using System.IO; using UnityEngine; using Stopwatch = System.Diagnostics.Stopwatch; public class MultiCameraPlugin : DevicePlugin { + private MemoryStream memoryStreamForCameraInfo = null; private SensorDevices.MultiCamera cam = null; protected override void OnAwake() { + memoryStreamForCameraInfo = new MemoryStream(); cam = gameObject.GetComponent(); + partName = DeviceHelper.GetPartName(gameObject); } protected override void OnStart() { - partName = DeviceHelper.GetPartName(gameObject); + var hashServiceKey = MakeHashKey(partName, "Info"); + if (!RegisterServiceDevice(hashServiceKey)) + { + Debug.LogError("Failed to register ElevatorSystem service - " + hashServiceKey); + } var hashKey = MakeHashKey(partName); if (!RegisterTxDevice(hashKey)) @@ -27,6 +35,12 @@ protected override void OnStart() } AddThread(Sender); + AddThread(Response); + } + + void OnDestroy() + { + memoryStreamForCameraInfo.Dispose(); } private void Sender() @@ -46,4 +60,28 @@ private void Sender() cam.SetTransportTime((float)sw.Elapsed.TotalSeconds); } } + + private void Response() + { + while (true) + { + var receivedBuffer = ReceiveRequest(); + + var requestMessage = CameraPlugin.ParsingCameraInfoRequest(ref memoryStreamForCameraInfo, receivedBuffer); + + if (requestMessage != null && requestMessage.Name.Equals("request_camera_info")) + { + var cameraName = requestMessage.Value.StringValue; + + if (cameraName != null) + { + var cameraInfoMessage = cam.GetCameraInfo(cameraName); + + CameraPlugin.SetCameraInfoResponse(ref memoryStreamForCameraInfo, cameraInfoMessage); + } + + SendResponse(memoryStreamForCameraInfo); + } + } + } } \ No newline at end of file diff --git a/Assets/Scripts/Devices/MultiCamera.cs b/Assets/Scripts/Devices/MultiCamera.cs index 6aa37042..c6893e84 100644 --- a/Assets/Scripts/Devices/MultiCamera.cs +++ b/Assets/Scripts/Devices/MultiCamera.cs @@ -67,7 +67,7 @@ protected override void GenerateMessage() { var image = images[index]; var imageData = cameras[index].GetCamImageData(); - if (image.Data.Length == imageData.Length) + if (imageData != null && image.Data.Length == imageData.Length) { image.Data = imageData; } @@ -81,7 +81,7 @@ protected override void GenerateMessage() private void AddCamera(in SDF.Camera parameters) { var newCamObject = new GameObject(); - newCamObject.name = "Camera::" + parameters.name; + newCamObject.name = parameters.name; var newCamTransform = newCamObject.transform; newCamTransform.position = Vector3.zero; @@ -107,5 +107,31 @@ private void InitializeCamMessages(in SDF.Camera parameters) image.Data = new byte[image.Height * image.Step]; imagesStamped.Images.Add(image); } + + public messages.CameraSensor GetCameraInfo(in string cameraName) + { + for (var index = 0; index < cameras.Count; index++) + { + if (cameras[index].deviceName.Equals(("MultiCamera::" + cameraName))) + { + return GetCameraInfo(index); + } + } + + return null; + } + + public messages.CameraSensor GetCameraInfo(in int cameraIndex) + { + if (cameraIndex < cameras.Count) + { + return cameras[cameraIndex].GetCameraInfo(); + } + else + { + Debug.LogWarning("unavailable camera index: " + cameraIndex); + return null; + } + } } } \ No newline at end of file From bac9e3800ede0907a219b9921ccdd951a6b32c20 Mon Sep 17 00:00:00 2001 From: Hyunseok Yang Date: Wed, 22 Jul 2020 16:02:46 +0900 Subject: [PATCH 2/2] Update app version info in Project settings : 1.3.2 -> 1.4.0 --- ProjectSettings/ProjectSettings.asset | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 183471e3..8ff13b39 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -121,7 +121,7 @@ PlayerSettings: 16:10: 1 16:9: 1 Others: 1 - bundleVersion: 1.3.2 + bundleVersion: 1.4.0 preloadedAssets: [] metroInputSource: 0 wsaTransparentSwapchain: 0