Tilt Five™ Unity API  1.4.1
CameraImage.cs
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020-2023 Tilt Five, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using UnityEngine;
21 
22 using TiltFive;
23 using TiltFive.Logging;
24 
25 namespace TiltFive
26 {
30  public class CameraImage : Singleton<CameraImage>
31  {
32 
36  private Dictionary<GlassesHandle, CameraImageCore> cameraImageCores = new Dictionary<GlassesHandle, CameraImageCore>();
37 
38  #region Public Functions
39 
40  internal static void Initialize(GlassesHandle glassesHandle)
41  {
42  Instance.cameraImageCores[glassesHandle] = new CameraImageCore(glassesHandle);
43  }
44 
45  internal static void RemoveCore(GlassesHandle glassesHandle){
46  if(!Instance.cameraImageCores.TryGetValue(glassesHandle, out var cameraImageCore)) { return; }
47  cameraImageCore.Dispose();
48  Instance.cameraImageCores.Remove(glassesHandle);
49  }
50 
58  public static bool TryGetFilledCameraImageBuffer(PlayerIndex playerIndex, ref T5_CamImage camImage)
59  {
60  if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
61  {
62  return false;
63  }
64 
65  return TryGetFilledCameraImageBuffer(glassesHandle, ref camImage);
66  }
67 
68  internal static bool TryGetFilledCameraImageBuffer(GlassesHandle glassesHandle, ref T5_CamImage camImage)
69  {
70  if(!Glasses.IsConnected(glassesHandle) || !Instance.cameraImageCores.TryGetValue(glassesHandle, out var cameraImageCore))
71  {
72  return false;
73  }
74 
75  return cameraImageCore.TryGetFilledCameraImageBuffer(ref camImage);
76  }
77 
86 
87  public static bool TrySubmitEmptyCameraImageBuffer(PlayerIndex playerIndex, IntPtr imageBuffer, UInt32 bufferSize)
88  {
89  if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
90  {
91  return false;
92  }
93 
94  return TrySubmitEmptyCameraImageBuffer(glassesHandle, imageBuffer, bufferSize);
95  }
96 
97  internal static bool TrySubmitEmptyCameraImageBuffer(GlassesHandle glassesHandle, IntPtr imageBuffer, UInt32 bufferSize)
98  {
99  if(!Glasses.IsConnected(glassesHandle) || !Instance.cameraImageCores.TryGetValue(glassesHandle, out var cameraImageCore))
100  {
101  return false;
102  }
103 
104  return cameraImageCore.TrySubmitEmptyCameraImageBuffer(imageBuffer, bufferSize);
105  }
106 
115 
116  public static bool TryCancelCameraImageBuffer(PlayerIndex playerIndex, byte[] imageBuffer)
117  {
118  if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
119  {
120  return false;
121  }
122 
123  return TryCancelCameraImageBuffer(glassesHandle, imageBuffer);
124  }
125 
126  internal static bool TryCancelCameraImageBuffer(GlassesHandle glassesHandle, byte[] imageBuffer)
127  {
128  if(!Glasses.IsConnected(glassesHandle) || !Instance.cameraImageCores.TryGetValue(glassesHandle, out var cameraImageCore))
129  {
130  return false;
131  }
132 
133  return cameraImageCore.TryCancelCameraImageBuffer(imageBuffer);
134  }
135 
141 
142  public static bool TryConfigureCameraImageStream(PlayerIndex playerIndex, T5_CameraStreamConfig config)
143  {
144  if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
145  {
146  return false;
147  }
148 
149  return TryConfigureCameraImageStream(glassesHandle, config);
150  }
151 
152  internal static bool TryConfigureCameraImageStream(GlassesHandle glassesHandle, T5_CameraStreamConfig config)
153  {
154  if(!Glasses.IsConnected(glassesHandle) || !Instance.cameraImageCores.TryGetValue(glassesHandle, out var cameraImageCore))
155  {
156  return false;
157  }
158 
159  return cameraImageCore.TryConfigureCameraImageStream(config);
160  }
161 
162  #endregion Public Functions
163 
164 
168  private class CameraImageCore : IDisposable
169  {
170  public GlassesHandle glassesHandle;
171 
172  public CameraImageCore(GlassesHandle glassesId)
173  {
174  this.glassesHandle = glassesId;
175  }
176 
177  public virtual void Dispose(){
178  Log.Info($"CameraImageCore for {glassesHandle} disconnected");
179  return;
180  }
181 
183  {
184  int result = NativePlugin.T5_RESULT_UNKNOWN_ERROR;
185  try
186  {
187  result = NativePlugin.ConfigureCameraStream(glassesHandle, config);
188  }
189  catch (System.Exception e)
190  {
191  Log.Error($"Error enabling Camera Stream: {e.Message}");
192  }
193  return result == NativePlugin.T5_RESULT_SUCCESS;
194  }
195 
196  public bool TryGetFilledCameraImageBuffer(ref T5_CamImage camImage)
197  {
198  int result = NativePlugin.T5_RESULT_UNKNOWN_ERROR;
199 
200  try
201  {
202  result = NativePlugin.GetFilledCamImageBuffer(glassesHandle, ref camImage);
203  }
204  catch (System.Exception e)
205  {
206  Log.Error($"Error getting Camera Image: {e.Message}");
207  }
208  return result == NativePlugin.T5_RESULT_SUCCESS;
209  }
210 
211  public bool TrySubmitEmptyCameraImageBuffer(IntPtr imageBuffer, UInt32 bufferSize)
212  {
213  int result = NativePlugin.T5_RESULT_UNKNOWN_ERROR;
214  try
215  {
216  result = NativePlugin.SubmitEmptyCamImageBuffer(glassesHandle, imageBuffer, bufferSize);
217  }
218  catch (System.Exception e)
219  {
220  Log.Error($"Error submitting Camera Image Buffer: {e.Message}");
221  }
222 
223  return result == NativePlugin.T5_RESULT_SUCCESS;
224  }
225 
226  public bool TryCancelCameraImageBuffer(byte[] imageBuffer)
227  {
228  int result = NativePlugin.T5_RESULT_UNKNOWN_ERROR;
229  try
230  {
231  GCHandle handle = GCHandle.Alloc(imageBuffer, GCHandleType.Pinned);
232  var ImageBufferHandle = handle.AddrOfPinnedObject();
233  result = NativePlugin.CancelCamImageBuffer(glassesHandle, ImageBufferHandle);
234  handle.Free();
235  }
236  catch (System.Exception e)
237  {
238  Log.Error($"Error clearing Camera Image Buffers: {e.Message}");
239  }
240 
241  return result == NativePlugin.T5_RESULT_SUCCESS;
242  }
243  }
244  }
245 }
Internal Camera Image core.
Definition: CameraImage.cs:169
bool TryGetFilledCameraImageBuffer(ref T5_CamImage camImage)
Definition: CameraImage.cs:196
CameraImageCore(GlassesHandle glassesId)
Definition: CameraImage.cs:172
bool TryConfigureCameraImageStream(T5_CameraStreamConfig config)
Definition: CameraImage.cs:182
bool TrySubmitEmptyCameraImageBuffer(IntPtr imageBuffer, UInt32 bufferSize)
Definition: CameraImage.cs:211
bool TryCancelCameraImageBuffer(byte[] imageBuffer)
Definition: CameraImage.cs:226
The Camera Frame API and runtime.
Definition: CameraImage.cs:31
static bool TrySubmitEmptyCameraImageBuffer(PlayerIndex playerIndex, IntPtr imageBuffer, UInt32 bufferSize)
Submit a camera image buffer to be used by the camera image stream. As images become available the bu...
Definition: CameraImage.cs:87
static bool TryCancelCameraImageBuffer(PlayerIndex playerIndex, byte[] imageBuffer)
Specify an image buffer to be released from the Tilt Five Service. If true is returned,...
Definition: CameraImage.cs:116
Dictionary< GlassesHandle, CameraImageCore > cameraImageCores
The glasses core runtimes.
Definition: CameraImage.cs:36
static bool TryConfigureCameraImageStream(PlayerIndex playerIndex, T5_CameraStreamConfig config)
Attempt to configure the Camera Stream
Definition: CameraImage.cs:142
static bool TryGetFilledCameraImageBuffer(PlayerIndex playerIndex, ref T5_CamImage camImage)
Attempt to request a new filled buffer from the camera image stream. If an empty buffer has been subm...
Definition: CameraImage.cs:58
The Glasses API and runtime.
Definition: Glasses.cs:35
static bool IsConnected(PlayerIndex playerIndex=PlayerIndex.One)
Indicate if the specified glasses are connected.
Definition: Glasses.cs:353
The Logger.
Definition: Log.cs:42
static void Info(string m, params object[] list)
INFO logging function call.
Definition: Log.cs:140
static void Error(string m, params object[] list)
ERROR logging function call.
Definition: Log.cs:127
Provides access to player settings and functionality.
Definition: Player.cs:31
Definition: Log.cs:21
PlayerIndex
The Player index (e.g. Player One, Player Two, etc)
Represents a wrapper on a buffer comtaining a camera image.
Camera Stream Configuration.