OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ImagingInterfaceC.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
28 //#include <osvr/Connection/ImagingServerInterface.h>
33 #include "HandleNullContext.h"
34 #include <osvr/Util/Verbosity.h>
35 
36 // Library/third-party includes
37 // - none
38 
39 // Standard includes
40 // - none
41 
42 // @todo This is a hack. expect this to be moved to a separate osvrJniBridge
43 // library and encapsulated behind a proper API.
44 
45 #if defined(__ANDROID__)
46 #include <jni.h>
47 OSVR_ImageBufferElement *gLastFrame = NULL;
48 OSVR_ImageBufferElement *gLastFrameBuffer = NULL;
49 OSVR_ImagingMetadata gLastFrameMetadata;
50 
51 extern "C" {
52  JNIEXPORT void JNICALL Java_com_osvr_android_jni_JNIBridge_reportFrame(JNIEnv * env, jclass clazz,
53  jbyteArray data, jlong width, jlong height);
54 }
55 
56 JNIEXPORT void JNICALL Java_com_osvr_android_jni_JNIBridge_reportFrame(JNIEnv * env, jclass clazz,
57  jbyteArray data, jlong width, jlong height) {
58 
59  gLastFrameMetadata.height = (OSVR_ImageDimension)height;
60  gLastFrameMetadata.width = (OSVR_ImageDimension)width;
61  gLastFrameMetadata.channels = (OSVR_ImageChannels)4;
62  gLastFrameMetadata.depth = (OSVR_ImageDepth)1;
63 
64  // @todo determine whether the current metadata matches the last metadata,
65  // and if so, reuse the last frame buffer instead of deleting and recreating.
66  // better yet, use a ring buffer so that image reports aren't lost if update
67  // isn't called frequently enough.
68  int size = env->GetArrayLength(data);
69 
70  if(gLastFrameBuffer == NULL) {
71  gLastFrameBuffer = new OSVR_ImageBufferElement[size];
72  }
73  gLastFrame = gLastFrameBuffer;
74  env->GetByteArrayRegion(data, 0, size, reinterpret_cast<jbyte*>(gLastFrameBuffer));
75 }
76 #endif
77 
81 };
82 
83 OSVR_ReturnCode
85  OSVR_OUT_PTR OSVR_ImagingDeviceInterface *iface,
86  OSVR_IN OSVR_ChannelCount numSensors) {
87 
88  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceImagingConfigure", opts);
89  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceImagingConfigure", iface);
91  opts->makeInterfaceObject<OSVR_ImagingDeviceInterfaceObject>();
92  *iface = ifaceObj;
93 
94  auto imaging = osvr::common::ImagingComponent::create(numSensors);
95  ifaceObj->imaging = imaging.get();
96  opts->addComponent(imaging);
97  return OSVR_RETURN_SUCCESS;
98 }
99 
100 OSVR_ReturnCode
102  OSVR_IN_PTR OSVR_ImagingDeviceInterface iface,
103  OSVR_IN OSVR_ImagingMetadata metadata,
104  OSVR_IN_PTR OSVR_ImageBufferElement *imageData,
105  OSVR_IN OSVR_ChannelCount sensor,
106  OSVR_IN_PTR OSVR_TimeValue const *timestamp) {
107  auto guard = iface->getSendGuard();
108  if (guard->lock()) {
109  iface->imaging->sendImageData(metadata, imageData, sensor, *timestamp);
110  return OSVR_RETURN_SUCCESS;
111  }
112 
113  return OSVR_RETURN_FAILURE;
114 }
OSVR_ImageDepth depth
the depth (size) in bytes of each channel - valid values are 1, 2, 4, and 8
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
A DeviceToken connects the generic device interaction code in PluginKit's C API with the workings of ...
Definition: DeviceToken.h:56
typedefOSVR_EXTERN_C_BEGIN struct OSVR_ImagingDeviceInterfaceObject * OSVR_ImagingDeviceInterface
Opaque type used in conjunction with a device token to send data on an imaging interface.
static shared_ptr< ImagingComponent > create(OSVR_ChannelCount numSensor=0)
Factory method.
Structure used internally to construct the desired type of device.
OSVR_ImageDimension width
width in pixels
detail::size< coerce_list< Ts...>> size
Get the size of a list (number of elements.)
Definition: Size.h:56
#define OSVR_PLUGIN_HANDLE_NULL_CONTEXT(FUNC, CONTEXT_NAME)
Internal macro for use in C API function implementations to check the validity of a context parameter...
#define OSVR_RETURN_FAILURE
The "failure" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:47
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
OSVR_ImageChannels channels
number of channels of data for each pixel
Internal, configured header file for verbosity macros.
OSVR_ImageDimension height
height in pixels
OSVR_ReturnCode osvrDeviceImagingConfigure(OSVR_DeviceInitOptions opts, OSVR_ImagingDeviceInterface *iface, OSVR_ChannelCount numSensors)
Specify that your device will implement the Imaging interface.
BaseDevice component.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
unsigned char OSVR_ImageBufferElement
Type for raw buffer access to image data.
Base class for the DeviceInterfaceObjects retrieved by plugins to let them send data on an interface...
OSVR_ReturnCode osvrDeviceImagingReportFrame(OSVR_DeviceToken, OSVR_ImagingDeviceInterface iface, OSVR_ImagingMetadata metadata, OSVR_ImageBufferElement *imageData, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report a frame for a sensor. Takes ownership of the buffer and frees it with the osvrAlignedFree func...