Пример #1
0
        /// <summary>
        /// Start video source.
        /// </summary>
        /// 
        /// <remarks>Starts video source and returns execution to caller. Video camera will be started
        /// and will provide new video frames through the <see cref="NewFrame"/> event.</remarks>
        /// 
        /// <exception cref="ArgumentException">The specified resolution is not supported for the selected
        /// mode of the Kinect video camera.</exception>
        /// <exception cref="ConnectionFailedException">Could not connect to Kinect's video camera.</exception>
        /// <exception cref="DeviceBusyException">Another connection to the specified video camera is already running.</exception>
        /// 
        public void Start( )
        {
            lock ( sync )
            {
                lock ( runningCameras )
                {
                    if ( device == null )
                    {
                        bool success = false;

                        try
                        {
                            if ( runningCameras.Contains( deviceID ) )
                            {
                                throw new DeviceBusyException( "Another connection to the specified video camera is already running." );
                            }

                            // get Kinect device
                            device = Kinect.GetDevice( deviceID );

                            KinectNative.VideoCameraFormat dataFormat = KinectNative.VideoCameraFormat.RGB;

                            if ( cameraMode == VideoCameraMode.Bayer )
                            {
                                dataFormat = KinectNative.VideoCameraFormat.Bayer;
                            }
                            else if ( cameraMode == VideoCameraMode.InfraRed )
                            {
                                dataFormat = KinectNative.VideoCameraFormat.IR8Bit;
                            }

                            // find video format parameters
                            videoModeInfo = KinectNative.freenect_find_video_mode( resolution, dataFormat );

                            if ( videoModeInfo.IsValid == 0 )
                            {
                                throw new ArgumentException( "The specified resolution is not supported for the selected mode of the Kinect video camera." );
                            }

                            // set video format
                            if ( KinectNative.freenect_set_video_mode( device.RawDevice, videoModeInfo ) != 0 )
                            {
                                throw new VideoException( "Could not switch to the specified video format." );
                            }

                            // allocate video buffer and provide it freenect
                            imageBuffer = Marshal.AllocHGlobal( (int) videoModeInfo.Bytes );
                            KinectNative.freenect_set_video_buffer( device.RawDevice, imageBuffer );

                            // set video callback
                            videoCallback = new KinectNative.FreenectVideoDataCallback( HandleDataReceived );
                            KinectNative.freenect_set_video_callback( device.RawDevice, videoCallback );

                            // start the camera
                            if ( KinectNative.freenect_start_video( device.RawDevice ) != 0 )
                            {
                                throw new ConnectionFailedException( "Could not start video stream." );
                            }

                            success = true;
                            runningCameras.Add( deviceID );
                        }
                        finally
                        {
                            if ( !success )
                            {
                                if ( device != null )
                                {
                                    device.Dispose( );
                                    device = null;
                                }

                                if ( imageBuffer != IntPtr.Zero )
                                {
                                    Marshal.FreeHGlobal( imageBuffer );
                                    imageBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Stop video source.
        /// </summary>
        /// 
        /// <remarks><para>The method stops the video source, so it no longer provides new video frames
        /// and does not consume any resources.</para>
        /// </remarks>
        /// 
        public void Stop( )
        {
            lock ( sync )
            {
                lock ( runningCameras )
                {
                    if ( device != null )
                    {
                        KinectNative.freenect_stop_video( device.RawDevice );

                        device.Dispose( );
                        device = null;
                        runningCameras.Remove( deviceID );

                        if ( PlayingFinished != null )
                        {
                            PlayingFinished( this, ReasonToFinishPlaying.StoppedByUser );
                        }
                    }

                    if ( imageBuffer != IntPtr.Zero )
                    {
                        Marshal.FreeHGlobal( imageBuffer );
                        imageBuffer = IntPtr.Zero;
                    }

                    videoCallback = null;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Start video source.
        /// </summary>
        ///
        /// <remarks>Starts video source and returns execution to caller. Video camera will be started
        /// and will provide new video frames through the <see cref="NewFrame"/> event.</remarks>
        ///
        /// <exception cref="ArgumentException">The specified resolution is not supported for the selected
        /// mode of the Kinect video camera.</exception>
        /// <exception cref="ConnectionFailedException">Could not connect to Kinect's video camera.</exception>
        /// <exception cref="DeviceBusyException">Another connection to the specified video camera is already running.</exception>
        ///
        public void Start()
        {
            lock (sync)
            {
                lock (runningCameras)
                {
                    if (device == null)
                    {
                        bool success = false;

                        try
                        {
                            if (runningCameras.Contains(deviceID))
                            {
                                throw new DeviceBusyException("Another connection to the specified video camera is already running.");
                            }

                            // get Kinect device
                            device = Kinect.GetDevice(deviceID);

                            KinectNative.VideoCameraFormat dataFormat = KinectNative.VideoCameraFormat.RGB;

                            if (cameraMode == VideoCameraMode.Bayer)
                            {
                                dataFormat = KinectNative.VideoCameraFormat.Bayer;
                            }
                            else if (cameraMode == VideoCameraMode.InfraRed)
                            {
                                dataFormat = KinectNative.VideoCameraFormat.IR8Bit;
                            }

                            // find video format parameters
                            videoModeInfo = KinectNative.freenect_find_video_mode(resolution, dataFormat);

                            if (videoModeInfo.IsValid == 0)
                            {
                                throw new ArgumentException("The specified resolution is not supported for the selected mode of the Kinect video camera.");
                            }

                            // set video format
                            if (KinectNative.freenect_set_video_mode(device.RawDevice, videoModeInfo) != 0)
                            {
                                throw new VideoException("Could not switch to the specified video format.");
                            }

                            // allocate video buffer and provide it freenect
                            imageBuffer = Marshal.AllocHGlobal((int)videoModeInfo.Bytes);
                            KinectNative.freenect_set_video_buffer(device.RawDevice, imageBuffer);

                            // set video callback
                            videoCallback = new KinectNative.FreenectVideoDataCallback(HandleDataReceived);
                            KinectNative.freenect_set_video_callback(device.RawDevice, videoCallback);

                            // start the camera
                            if (KinectNative.freenect_start_video(device.RawDevice) != 0)
                            {
                                throw new ConnectionFailedException("Could not start video stream.");
                            }

                            success = true;
                            runningCameras.Add(deviceID);

                            device.AddFailureHandler(deviceID, Stop);
                        }
                        finally
                        {
                            if (!success)
                            {
                                if (device != null)
                                {
                                    device.Dispose();
                                    device = null;
                                }

                                if (imageBuffer != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(imageBuffer);
                                    imageBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                }
            }
        }