-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOpenNI.h
executable file
·2750 lines (2421 loc) · 91.9 KB
/
OpenNI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* *
* OpenNI 2.x Alpha *
* Copyright (C) 2012 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*****************************************************************************/
#ifndef _OPENNI_H_
#define _OPENNI_H_
#include "OniPlatform.h"
#include "OniProperties.h"
#include "OniEnums.h"
#include "OniCAPI.h"
#include "OniCProperties.h"
/**
openni is the namespace of the entire C++ API of OpenNI
*/
namespace openni
{
/** Pixel type used to store depth images. */
typedef uint16_t DepthPixel;
/** Pixel type used to store IR images. */
typedef uint16_t Grayscale16Pixel;
// structs
/** Holds an OpenNI version number, which consists of four separate numbers in the format: @c major.minor.maintenance.build. For example: 2.0.0.20. */
typedef struct
{
/** Major version number, incremented for major API restructuring. */
int major;
/** Minor version number, incremented when significant new features added. */
int minor;
/** Maintenance build number, incremented for new releases that primarily provide minor bug fixes. */
int maintenance;
/** Build number. Incremented for each new API build. Generally not shown on the installer and download site. */
int build;
} Version;
/** Holds the value of a single color image pixel in 24-bit RGB format. */
typedef struct
{
/* Red value of this pixel. */
uint8_t r;
/* Green value of this pixel. */
uint8_t g;
/* Blue value of this pixel. */
uint8_t b;
} RGB888Pixel;
/**
Holds the value of two pixels in YUV422 format (Luminance/Chrominance,16-bits/pixel).
The first pixel has the values y1, u, v.
The second pixel has the values y2, u, v.
*/
typedef struct
{
/** First chrominance value for two pixels, stored as blue luminance difference signal. */
uint8_t u;
/** Overall luminance value of first pixel. */
uint8_t y1;
/** Second chrominance value for two pixels, stored as red luminance difference signal. */
uint8_t v;
/** Overall luminance value of second pixel. */
uint8_t y2;
} YUV422DoublePixel;
/** This special URI can be passed to @ref Device::open() when the application has no concern for a specific device. */
#if ONI_PLATFORM != ONI_PLATFORM_WIN32
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic push
#endif
static const char* ANY_DEVICE = NULL;
#if ONI_PLATFORM != ONI_PLATFORM_WIN32
#pragma GCC diagnostic pop
#endif
/**
Provides a simple array class used throughout the API. Wraps a primitive array
of objects, holding the elements and their count.
*/
template<class T>
class Array
{
public:
/**
Default constructor. Creates an empty Array and sets the element count to zero.
*/
Array() : m_data(NULL), m_count(0), m_owner(false) {}
/**
Constructor. Creates new Array from an existing primitive array of known size.
@tparam [in] T Object type this Array will contain.
@param [in] data Pointer to a primitive array of objects of type T.
@param [in] count Number of elements in the primitive array pointed to by data.
*/
Array(const T* data, int count) : m_owner(false) { _setData(data, count); }
/**
Destructor. Destroys the Array object.
*/
~Array()
{
clear();
}
/**
Getter function for the Array size.
@returns Current number of elements in the Array.
*/
int getSize() const { return m_count; }
/**
Implements the array indexing operator for the Array class.
*/
const T& operator[](int index) const {return m_data[index];}
/**
@internal
Setter function for data. Causes this array to wrap an existing primitive array
of specified type. The optional data ownership flag controls whether the primitive
array this Array wraps will be destroyed when this Array is deconstructed.
@param [in] T Type of objects array will contain.
@param [in] data Pointer to first object in list.
@param [in] count Number of objects in list.
@param [in] isOwner Optional flag to indicate data ownership
*/
void _setData(const T* data, int count, bool isOwner = false)
{
clear();
m_count = count;
m_owner = isOwner;
if (!isOwner)
{
m_data = data;
}
else
{
m_data = new T[count];
memcpy((void*)m_data, data, count*sizeof(T));
}
}
private:
Array(const Array<T>&);
Array<T>& operator=(const Array<T>&);
void clear()
{
if (m_owner && m_data != NULL)
delete []m_data;
m_owner = false;
m_data = NULL;
m_count = 0;
}
const T* m_data;
int m_count;
bool m_owner;
};
// Forward declaration of all
class SensorInfo;
class VideoStream;
class VideoFrameRef;
class Device;
class OpenNI;
class CameraSettings;
class PlaybackControl;
/**
Encapsulates a group of settings for a @ref VideoStream. Settings stored include
frame rate, resolution, and pixel format.
This class is used as an input for changing the settings of a @ref VideoStream,
as well as an output for reporting the current settings of that class. It is also used
by @ref SensorInfo to report available video modes of a stream.
Recommended practice is to use @ref SensorInfo::getSupportedVideoModes()
to obtain a list of valid video modes, and then to use items from that list to pass
new settings to @ref VideoStream. This is much less likely to produce an
invalid video mode than instantiating and manually changing objects of this
class.
*/
class VideoMode : private OniVideoMode
{
public:
/**
Default constructor, creates an empty VideoMode object. Application programs should, in most
cases, use the copy constructor to copy an existing valid video mode. This is much less
error prone that creating and attempting to configure a new VideoMode from scratch.
*/
VideoMode()
{}
/**
Copy constructor, creates a new VideoMode identical to an existing VideoMode.
@param [in] other Existing VideoMode to copy.
*/
VideoMode(const VideoMode& other)
{
*this = other;
}
/**
Assignment operator. Sets the pixel format, frame rate, and resolution of this
VideoMode to equal that of a different VideoMode.
@param [in] other Existing VideoMode to copy settings from.
*/
VideoMode& operator=(const VideoMode& other)
{
setPixelFormat(other.getPixelFormat());
setResolution(other.getResolutionX(), other.getResolutionY());
setFps(other.getFps());
return *this;
}
/**
Getter function for the pixel format of this VideoMode.
@returns Current pixel format setting of this VideoMode.
*/
PixelFormat getPixelFormat() const { return (PixelFormat)pixelFormat; }
/**
Getter function for the X resolution of this VideoMode.
@returns Current horizontal resolution of this VideoMode, in pixels.
*/
int getResolutionX() const { return resolutionX; }
/**
Getter function for the Y resolution of this VideoMode.
@returns Current vertical resolution of this VideoMode, in pixels.
*/
int getResolutionY() const {return resolutionY;}
/**
Getter function for the frame rate of this VideoMode.
@returns Current frame rate, measured in frames per second.
*/
int getFps() const { return fps; }
/**
Setter function for the pixel format of this VideoMode. Application use of this
function is not recommended. Instead, use @ref SensorInfo::getSupportedVideoModes()
to obtain a list of valid video modes.
@param [in] format Desired new pixel format for this VideoMode.
*/
void setPixelFormat(PixelFormat format) { this->pixelFormat = (OniPixelFormat)format; }
/**
Setter function for the resolution of this VideoMode. Application use of this
function is not recommended. Instead, use @ref SensorInfo::getSupportedVideoModes() to
obtain a list of valid video modes.
@param [in] resolutionX Desired new horizontal resolution in pixels.
@param [in] resolutionY Desired new vertical resolution in pixels.
*/
void setResolution(int resolutionX, int resolutionY)
{
this->resolutionX = resolutionX;
this->resolutionY = resolutionY;
}
/**
Setter function for the frame rate. Application use of this function is not recommended.
Instead, use @ref SensorInfo::getSupportedVideoModes() to obtain a list of valid
video modes.
@param [in] fps Desired new frame rate, measured in frames per second.
*/
void setFps(int fps) { this->fps = fps; }
friend class SensorInfo;
friend class VideoStream;
friend class VideoFrameRef;
};
/**
The SensorInfo class encapsulates all info related to a specific sensor in a specific
device.
A @ref Device object holds a SensorInfo object for each sensor it contains.
A @ref VideoStream object holds one SensorInfo object, describing the sensor used to produce that stream.
A given SensorInfo object will contain the type of the sensor (Depth, IR or Color), and
a list of all video modes that the sensor can support. Each available video mode will have a single
VideoMode object that can be queried to get the details of that mode.
SensorInfo objects should be the only source of VideoMode objects for the vast majority of
application programs.
Application programs will never directly instantiate objects of type SensorInfo. In fact, no
public constructors are provided. SensorInfo objects should be obtained either from a Device or @ref VideoStream,
and in turn be used to provide available video modes for that sensor.
*/
class SensorInfo
{
public:
/**
Provides the sensor type of the sensor this object is associated with.
@returns Type of the sensor.
*/
SensorType getSensorType() const { return (SensorType)m_pInfo->sensorType; }
/**
Provides a list of video modes that this sensor can support. This function is the
recommended method to be used by applications to obtain @ref VideoMode objects.
@returns Reference to an array of @ref VideoMode objects, one for each supported
video mode.
*/
const Array<VideoMode>& getSupportedVideoModes() const { return m_videoModes; }
private:
SensorInfo(const SensorInfo&);
SensorInfo& operator=(const SensorInfo&);
SensorInfo() : m_pInfo(NULL), m_videoModes(NULL, 0) {}
SensorInfo(const OniSensorInfo* pInfo) : m_pInfo(NULL), m_videoModes(NULL, 0)
{
_setInternal(pInfo);
}
void _setInternal(const OniSensorInfo* pInfo)
{
m_pInfo = pInfo;
if (pInfo == NULL)
{
m_videoModes._setData(NULL, 0);
}
else
{
m_videoModes._setData(static_cast<VideoMode*>(pInfo->pSupportedVideoModes), pInfo->numSupportedVideoModes);
}
}
const OniSensorInfo* m_pInfo;
Array<VideoMode> m_videoModes;
friend class VideoStream;
friend class Device;
};
/**
The DeviceInfo class encapsulates info related to a specific device.
Applications will generally obtain objects of this type via calls to @ref OpenNI::enumerateDevices() or
@ref openni::Device::getDeviceInfo(), and then use the various accessor functions to obtain specific
information on that device.
There should be no reason for application code to instantiate this object directly.
*/
class DeviceInfo : private OniDeviceInfo
{
public:
/**
Returns the device URI. URI can be used by @ref Device::open to open a specific device.
The URI string format is determined by the driver.
*/
const char* getUri() const { return uri; }
/** Returns a the vendor name for this device. */
const char* getVendor() const { return vendor; }
/** Returns the device name for this device. */
const char* getName() const { return name; }
/** Returns the USB VID code for this device. */
uint16_t getUsbVendorId() const { return usbVendorId; }
/** Returns the USB PID code for this device. */
uint16_t getUsbProductId() const { return usbProductId; }
friend class Device;
friend class OpenNI;
};
/**
The @ref VideoFrameRef class encapsulates a single video frame - the output of a @ref VideoStream at a specific time.
The data contained will be a single frame of color, IR, or depth video, along with associated meta data.
An object of type @ref VideoFrameRef does not actually hold the data of the frame, but only a reference to it. The
reference can be released by destroying the @ref VideoFrameRef object, or by calling the @ref release() method. The
actual data of the frame is freed when the last reference to it is released.
The usual way to obtain @ref VideoFrameRef objects is by a call to @ref VideoStream.:readFrame().
All data references by a @ref VideoFrameRef is stored as a primitive array of pixels. Each pixel will be
of a type according to the configured pixel format (see @ref VideoMode).
*/
class VideoFrameRef
{
public:
/**
Default constructor. Creates a new empty @ref VideoFrameRef object.
This object will be invalid until initialized by a call to @ref VideoStream::readFrame().
*/
VideoFrameRef()
{
m_pFrame = NULL;
}
/**
Destroy this object and release the reference to the frame.
*/
~VideoFrameRef()
{
release();
}
/**
Copy constructor. Creates a new @ref VideoFrameRef object. The newly created
object will reference the same frame current object references.
@param [in] other Another @ref VideoFrameRef object.
*/
VideoFrameRef(const VideoFrameRef& other) : m_pFrame(NULL)
{
_setFrame(other.m_pFrame);
}
/**
Make this @ref VideoFrameRef object reference the same frame that the @c other frame references.
If this object referenced another frame before calling this method, the previous frame will be released.
@param [in] other Another @ref VideoFrameRef object.
*/
VideoFrameRef& operator=(const VideoFrameRef& other)
{
_setFrame(other.m_pFrame);
return *this;
}
/**
Getter function for the size of the data contained by this object. Useful primarily
when allocating buffers.
@returns Current size of data pointed to by this object, measured in bytes.
*/
inline int getDataSize() const
{
return m_pFrame->dataSize;
}
/**
Getter function for the array of data pointed to by this object.
@returns Pointer to the actual frame data array. Type of data
pointed to can be determined according to the pixel format (can be obtained by calling @ref getVideoMode()).
*/
inline const void* getData() const
{
return m_pFrame->data;
}
/**
Getter function for the sensor type used to produce this frame. Used to determine whether
this is an IR, Color or Depth frame. See the @ref SensorType enumeration for all possible return
values from this function.
@returns The type of sensor used to produce this frame.
*/
inline SensorType getSensorType() const
{
return (SensorType)m_pFrame->sensorType;
}
/**
Returns a reference to the @ref VideoMode object assigned to this frame. This object describes
the video mode the sensor was configured to when the frame was produced and can be used
to determine the pixel format and resolution of the data. It will also provide the frame rate
that the sensor was running at when it recorded this frame.
@returns Reference to the @ref VideoMode assigned to this frame.
*/
inline const VideoMode& getVideoMode() const
{
return static_cast<const VideoMode&>(m_pFrame->videoMode);
}
/**
Provides a timestamp for the frame. The 'zero' point for this stamp
is implementation specific, but all streams from the same device are guaranteed to use the same zero.
This value can therefore be used to compute time deltas between frames from the same device,
regardless of whether they are from the same stream.
@returns Timestamp of frame, measured in microseconds from an arbitrary zero
*/
inline uint64_t getTimestamp() const
{
return m_pFrame->timestamp;
}
/**
Frames are provided sequential frame ID numbers by the sensor that produced them. If frame
synchronization has been enabled for a device via @ref Device::setDepthColorSyncEnabled(), then frame
numbers for corresponding frames of depth and color are guaranteed to match.
If frame synchronization is not enabled, then there is no guarantee of matching frame indexes between
@ref VideoStream "VideoStreams". In the latter case, applications should use timestamps instead of frame indexes to
align frames in time.
@returns Index number for this frame.
*/
inline int getFrameIndex() const
{
return m_pFrame->frameIndex;
}
/**
Gives the current width of this frame, measured in pixels. If cropping is enabled, this will be
the width of the cropping window. If cropping is not enabled, then this will simply be equal to
the X resolution of the @ref VideoMode used to produce this frame.
@returns Width of this frame in pixels.
*/
inline int getWidth() const
{
return m_pFrame->width;
}
/**
Gives the current height of this frame, measured in pixels. If cropping is enabled, this will
be the length of the cropping window. If cropping is not enabled, then this will simply be equal
to the Y resolution of the @ref VideoMode used to produce this frame.
*/
inline int getHeight() const
{
return m_pFrame->height;
}
/**
Indicates whether cropping was enabled when the frame was produced.
@return true if cropping is enabled, false otherwise
*/
inline bool getCroppingEnabled() const
{
return m_pFrame->croppingEnabled == TRUE;
}
/**
Indicates the X coordinate of the upper left corner of the crop window.
@return Distance of crop origin from left side of image, in pixels.
*/
inline int getCropOriginX() const
{
return m_pFrame->cropOriginX;
}
/**
Indicates the Y coordinate of the upper left corner of the crop window.
@return Distance of crop origin from top of image, in pixels.
*/
inline int getCropOriginY() const
{
return m_pFrame->cropOriginY;
}
/**
Gives the length of one row of pixels, measured in bytes. Primarily useful
for indexing the array which contains the data.
@returns Stride of the array which contains the image for this frame, in bytes
*/
inline int getStrideInBytes() const
{
return m_pFrame->stride;
}
/**
Check if this object references an actual frame.
*/
inline bool isValid() const
{
return m_pFrame != NULL;
}
/**
Release the reference to the frame. Once this method is called, the object becomes invalid, and no method
should be called other than the assignment operator, or passing this object to a @ref VideoStream::readFrame() call.
*/
void release()
{
if (m_pFrame != NULL)
{
oniFrameRelease(m_pFrame);
m_pFrame = NULL;
}
}
/** @internal */
void _setFrame(OniFrame* pFrame)
{
setReference(pFrame);
if (pFrame != NULL)
{
oniFrameAddRef(pFrame);
}
}
/** @internal */
OniFrame* _getFrame()
{
return m_pFrame;
}
private:
friend class VideoStream;
inline void setReference(OniFrame* pFrame)
{
// Initial - don't addref. This is the reference from OpenNI
release();
m_pFrame = pFrame;
}
OniFrame* m_pFrame; // const!!?
};
/**
The @ref VideoStream object encapsulates a single video stream from a device. Once created, it is used to start data flow
from the device, and to read individual frames of data. This is the central class used to obtain data in OpenNI. It
provides the ability to manually read data in a polling loop, as well as providing events and a Listener class that can be
used to implement event-driven data acquisition.
Aside from the video data frames themselves, the class offers a number of functions used for obtaining information about a
@ref VideoStream. Field of view, available video modes, and minimum and maximum valid pixel values can all be obtained.
In addition to obtaining data, the @ref VideoStream object is used to set all configuration properties that apply to a specific
stream (rather than to an entire device). In particular, it is used to control cropping, mirroring, and video modes.
A pointer to a valid, initialized device that provides the desired stream type is required to create a stream.
Several video streams can be created to stream data from the same sensor. This is useful if several components of an application
need to read frames separately.
While some device might allow different streams
from the same sensor to have different configurations, most devices will have a single configuration for the sensor,
shared by all streams.
*/
class VideoStream
{
public:
/**
The @ref VideoStream::NewFrameListener class is provided to allow the implementation of event driven frame reading. To use
it, create a class that inherits from it and implement override the onNewFrame() method. Then, register
your created class with an active @ref VideoStream using the @ref VideoStream::addNewFrameListener() function. Once this is done, the
event handler function you implemented will be called whenever a new frame becomes available. You may call
@ref VideoStream::readFrame() from within the event handler.
*/
class NewFrameListener
{
public:
/**
Default constructor.
*/
NewFrameListener() : m_callbackHandle(NULL)
{
}
virtual ~NewFrameListener()
{
}
/**
Derived classes should implement this function to handle new frames.
*/
virtual void onNewFrame(VideoStream&) = 0;
private:
friend class VideoStream;
static void ONI_CALLBACK_TYPE callback(OniStreamHandle streamHandle, void* pCookie)
{
NewFrameListener* pListener = (NewFrameListener*)pCookie;
VideoStream stream;
stream._setHandle(streamHandle);
pListener->onNewFrame(stream);
stream._setHandle(NULL);
}
OniCallbackHandle m_callbackHandle;
};
class FrameAllocator
{
public:
virtual ~FrameAllocator() {}
virtual void* allocateFrameBuffer(int size) = 0;
virtual void freeFrameBuffer(void* data) = 0;
private:
friend class VideoStream;
static void* ONI_CALLBACK_TYPE allocateFrameBufferCallback(int size, void* pCookie)
{
FrameAllocator* pThis = (FrameAllocator*)pCookie;
return pThis->allocateFrameBuffer(size);
}
static void ONI_CALLBACK_TYPE freeFrameBufferCallback(void* data, void* pCookie)
{
FrameAllocator* pThis = (FrameAllocator*)pCookie;
pThis->freeFrameBuffer(data);
}
};
/**
Default constructor. Creates a new, non-valid @ref VideoStream object. The object created will be invalid until its create() function
is called with a valid Device.
*/
VideoStream() : m_stream(NULL), m_sensorInfo(), m_pCameraSettings(NULL), m_isOwner(true)
{}
/**
Handle constructor. Creates a VideoStream object based on the given initialized handle.
This object will not destroy the underlying handle when @ref destroy() or destructor is called
*/
explicit VideoStream(OniStreamHandle handle) : m_stream(NULL), m_sensorInfo(), m_pCameraSettings(NULL), m_isOwner(false)
{
_setHandle(handle);
}
/**
Destructor. The destructor calls the destroy() function, but it is considered a best practice for applications to
call destroy() manually on any @ref VideoStream that they run create() on.
*/
~VideoStream()
{
destroy();
}
/**
Checks to see if this object has been properly initialized and currently points to a valid stream.
@returns true if this object has been previously initialized, false otherwise.
*/
bool isValid() const
{
return m_stream != NULL;
}
/**
Creates a stream of frames from a specific sensor type of a specific device. You must supply a reference to a
Device that supplies the sensor type requested. You can use @ref Device::hasSensor() to check whether a
given sensor is available on your target device before calling create().
@param [in] device A reference to the @ref Device you want to create the stream on.
@param [in] sensorType The type of sensor the stream should produce data from.
@returns Status code indicating success or failure for this operation.
*/
inline Status create(const Device& device, SensorType sensorType);
/**
Destroy this stream. This function is currently called automatically by the destructor, but it is
considered a best practice for applications to manually call this function on any @ref VideoStream that they
call create() for.
*/
inline void destroy();
/**
Provides the @ref SensorInfo object associated with the sensor that is producing this @ref VideoStream. Note that
this function will return NULL if the stream has not yet been initialized with the create() function.
@ref SensorInfo is useful primarily as a means of learning which video modes are valid for this VideoStream.
@returns Reference to the SensorInfo object associated with the sensor providing this stream.
*/
const SensorInfo& getSensorInfo() const
{
return m_sensorInfo;
}
/**
Starts data generation from this video stream.
*/
Status start()
{
if (!isValid())
{
return STATUS_ERROR;
}
return (Status)oniStreamStart(m_stream);
}
/**
Stops data generation from this video stream.
*/
void stop()
{
if (!isValid())
{
return;
}
oniStreamStop(m_stream);
}
/**
Read the next frame from this video stream, delivered as a @ref VideoFrameRef. This is the primary
method for manually obtaining frames of video data.
If no new frame is available, the call will block until one is available.
To avoid blocking, use @ref VideoStream::Listener to implement an event driven architecture. Another
alternative is to use @ref OpenNI::waitForAnyStream() to wait for new frames from several streams.
@param [out] pFrame Pointer to a @ref VideoFrameRef object to hold the reference to the new frame.
@returns Status code to indicated success or failure of this function.
*/
Status readFrame(VideoFrameRef* pFrame)
{
if (!isValid())
{
return STATUS_ERROR;
}
OniFrame* pOniFrame;
Status rc = (Status)oniStreamReadFrame(m_stream, &pOniFrame);
pFrame->setReference(pOniFrame);
return rc;
}
/**
Adds a new Listener to receive this VideoStream onNewFrame event. See @ref VideoStream::NewFrameListener for
more information on implementing an event driven frame reading architecture. An instance of a listener can be added to only one source.
@param [in] pListener Pointer to a @ref VideoStream::NewFrameListener object (or a derivative) that will respond to this event.
@returns Status code indicating success or failure of the operation.
*/
Status addNewFrameListener(NewFrameListener* pListener)
{
if (!isValid())
{
return STATUS_ERROR;
}
return (Status)oniStreamRegisterNewFrameCallback(m_stream, pListener->callback, pListener, &pListener->m_callbackHandle);
}
/**
Removes a Listener from this video stream list. The listener removed will no longer receive new frame events from this stream.
@param [in] pListener Pointer to the listener object to be removed.
*/
void removeNewFrameListener(NewFrameListener* pListener)
{
if (!isValid())
{
return;
}
oniStreamUnregisterNewFrameCallback(m_stream, pListener->m_callbackHandle);
pListener->m_callbackHandle = NULL;
}
/**
Sets the frame buffers allocator for this video stream.
@param [in] pAllocator Pointer to the frame buffers allocator object. Pass NULL to return to default frame allocator.
@returns ONI_STATUS_OUT_OF_FLOW The frame buffers allocator cannot be set while stream is streaming.
*/
Status setFrameBuffersAllocator(FrameAllocator* pAllocator)
{
if (!isValid())
{
return STATUS_ERROR;
}
if (pAllocator == NULL)
{
return (Status)oniStreamSetFrameBuffersAllocator(m_stream, NULL, NULL, NULL);
}
else
{
return (Status)oniStreamSetFrameBuffersAllocator(m_stream, pAllocator->allocateFrameBufferCallback, pAllocator->freeFrameBufferCallback, pAllocator);
}
}
/**
@internal
Get an internal handle. This handle can be used via the C API.
*/
OniStreamHandle _getHandle() const
{
return m_stream;
}
/**
Gets an object through which several camera settings can be configured.
@returns NULL if the stream doesn't support camera settings.
*/
CameraSettings* getCameraSettings() {return m_pCameraSettings;}
/**
General function for obtaining the value of stream specific properties.
There are convenience functions available for all commonly used properties, so it is not
expected that applications will make direct use of the getProperty function very often.
@param [in] propertyId The numerical ID of the property to be queried.
@param [out] data Place to store the value of the property.
@param [in,out] dataSize IN: Size of the buffer passed in the @c data argument. OUT: the actual written size.
@returns Status code indicating success or failure of this operation.
*/
Status getProperty(int propertyId, void* data, int* dataSize) const
{
if (!isValid())
{
return STATUS_ERROR;
}
return (Status)oniStreamGetProperty(m_stream, propertyId, data, dataSize);
}
/**
General function for setting the value of stream specific properties.
There are convenience functions available for all commonly used properties, so it is not
expected that applications will make direct use of the setProperty function very often.
@param [in] propertyId The numerical ID of the property to be set.
@param [in] data Place to store the data to be written to the property.
@param [in] dataSize Size of the data to be written to the property.
@returns Status code indicating success or failure of this operation.
*/
Status setProperty(int propertyId, const void* data, int dataSize)
{
if (!isValid())
{
return STATUS_ERROR;
}
return (Status)oniStreamSetProperty(m_stream, propertyId, data, dataSize);
}
/**
Get the current video mode information for this video stream.
This includes its resolution, fps and stream format.
@returns Current video mode information for this video stream.
*/
VideoMode getVideoMode() const
{
VideoMode videoMode;
getProperty<OniVideoMode>(STREAM_PROPERTY_VIDEO_MODE, static_cast<OniVideoMode*>(&videoMode));
return videoMode;
}
/**
Changes the current video mode of this stream. Recommended practice is to use @ref Device::getSensorInfo(), and
then @ref SensorInfo::getSupportedVideoModes() to obtain a list of valid video mode settings for this stream. Then,
pass a valid @ref VideoMode to @ref setVideoMode to ensure correct operation.
@param [in] videoMode Desired new video mode for this stream.
returns Status code indicating success or failure of this operation.
*/
Status setVideoMode(const VideoMode& videoMode)
{
return setProperty<OniVideoMode>(STREAM_PROPERTY_VIDEO_MODE, static_cast<const OniVideoMode&>(videoMode));
}
/**
Provides the maximum possible value for pixels obtained by this stream. This is most useful for
getting the maximum possible value of depth streams.
@returns Maximum possible pixel value.
*/
int getMaxPixelValue() const
{
int maxValue;
Status rc = getProperty<int>(STREAM_PROPERTY_MAX_VALUE, &maxValue);
if (rc != STATUS_OK)
{
return 0;
}
return maxValue;
}
/**
Provides the smallest possible value for pixels obtains by this VideoStream. This is most useful
for getting the minimum possible value that will be reported by a depth stream.
@returns Minimum possible pixel value that can come from this stream.
*/
int getMinPixelValue() const
{
int minValue;
Status rc = getProperty<int>(STREAM_PROPERTY_MIN_VALUE, &minValue);
if (rc != STATUS_OK)
{
return 0;
}
return minValue;
}
/**
Checks whether this stream supports cropping.
@returns true if the stream supports cropping, false if it does not.
*/
bool isCroppingSupported() const
{
return isPropertySupported(STREAM_PROPERTY_CROPPING);
}