private void PublishGetSensorStatus(KinectSensor newSensor, ChooserStatus newStatus) { if (Kinect != newSensor) { if (Kinect != null) { Kinect.Stop(); Kinect.AudioSource.Stop(); } Kinect = newSensor; if (this.PropertyCallback != null) { PropertyCallback(new KeyValuePair() { key = "Kinect", value = Kinect }); } if (this.SensorCallback != null) { SensorCallback(new KinectSensorChangedEventData() { sensor = Kinect, status = newStatus }); } } if (Status != newStatus) { if (this.PropertyCallback != null) { PropertyCallback(new KeyValuePair() { key = "Status", value = newStatus }); } } }
/// <summary> /// Display the sensor status on the MainWindow and update the status being sent out over the network /// </summary> /// <param name="kinectStatus"></param> private void ShowStatus(ChooserStatus kinectStatus) { string statusText = "No Kinect"; switch (kinectStatus) { case ChooserStatus.None: statusText = "Error"; break; case ChooserStatus.NoAvailableSensors: statusText = "No Kinect"; break; case ChooserStatus.SensorConflict: statusText = "AppConflict"; break; case ChooserStatus.SensorError: statusText = "Sensor Err"; break; case ChooserStatus.SensorInitializing: statusText = "Init"; break; case ChooserStatus.SensorInsufficientBandwidth: statusText = "No USB BW"; break; case ChooserStatus.SensorNotGenuine: statusText = "Bad Sensor"; break; case ChooserStatus.SensorNotPowered: statusText = "No Power"; break; case ChooserStatus.SensorNotSupported: statusText = "Bad Sensor"; break; case ChooserStatus.SensorStarted: statusText = "OK"; break; } manager.SetKinectStatus(statusText); sensorStatusChanges.Text += statusText + "\n"; }
/// <summary> /// Event fired when the sensor is changed. /// </summary> /// <param name="oldSensor"></param> /// <param name="newSensor"></param> private void OnSensorChanged(object sender, KinectChangedEventArgs args) { oldSensor = args.OldSensor; sensor = args.NewSensor; status = chooser.Status; if (oldSensor != null) { oldSensor.AllFramesReady -= this.OnAllFramesReady; this.ResetFaceTracking(); } if (args.NewSensor != null) { sensor.AllFramesReady += this.OnAllFramesReady; //sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); // Turn on the skeleton stream to receive skeleton frames sensor.SkeletonStream.Enable(); sensor.SkeletonStream.EnableTrackingInNearRange = true; sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); sensor.ColorStream.Enable(); //sensor.DepthStream.Range = DepthRange.Near; //Listen to the AllFramesReady event to receive KinectSensor's data sensor.AllFramesReady += this.OnAllFramesReady; // Start the sensor! try { sensor.Start(); } catch (IOException) { sensor = null; //System.Console.WriteLine("IOException " + e); throw new Exception(); } } KinectChanged(this, EventArgs.Empty); }
/// <summary> /// Helper to update our external status. /// </summary> /// <param name="callbackLock">Used to delay notifications to the client until after we exit the lock.</param> /// <param name="newKinect">The new sensor</param> /// <param name="newChooserStatus">The status we want to report to clients</param> private void SetSensorAndStatus(CallbackLock callbackLock, KinectSensor newKinect, ChooserStatus newChooserStatus) { KinectSensor oldKinect = Kinect; if (oldKinect != newKinect) { if (oldKinect != null) { oldKinect.Stop(); } Kinect = newKinect; callbackLock.LockExit += () => this.kinectChangedContextWrapper.Invoke(this, new KinectChangedEventArgs(oldKinect, newKinect)); callbackLock.LockExit += () => RaisePropertyChanged("Kinect"); } if (Status != newChooserStatus) { Status = newChooserStatus; callbackLock.LockExit += () => RaisePropertyChanged("Status"); } }
/// <summary> /// Called when we don't have a sensor or possibly have the wrong sensor /// and we want to see if we can get one. /// </summary> private void TryFindAndStartKinect(CallbackLock callbackLock) { if (!isStarted) { // We aren't started so we don't need to be finding anything. Debug.Assert(Status == ChooserStatus.None, "isStarted and Status out of sync"); return; } if (Kinect != null && Kinect.Status == KinectStatus.Connected) { if (requiredConnectionId == null) { // we already have an appropriate sensor Debug.Assert(Status == ChooserStatus.SensorStarted, "Chooser in unexpected state"); return; } if (Kinect.DeviceConnectionId == requiredConnectionId) { // we already have the requested sensor Debug.Assert(Status == ChooserStatus.SensorStarted, "Chooser in unexpected state"); return; } } KinectSensor newSensor = null; ChooserStatus newStatus = 0; if (KinectSensor.KinectSensors.Count == 0) { newStatus = ChooserStatus.NoAvailableSensors; } else { foreach (KinectSensor sensor in KinectSensor.KinectSensors) { if (requiredConnectionId != null && sensor.DeviceConnectionId != requiredConnectionId) { // client has set a required connection Id and this // sensor does not have that Id newStatus |= ChooserStatus.NoAvailableSensors; continue; } if (sensor.Status != KinectStatus.Connected) { // Sensor is in some unusable state newStatus |= GetErrorStatusFromSensor(sensor); continue; } if (sensor.IsRunning) { // Sensor is already in use by this application newStatus |= ChooserStatus.NoAvailableSensors; continue; } // There is a potentially available sensor, try to start it try { sensor.Start(); } catch (IOException) { // some other app has this sensor. newStatus |= ChooserStatus.SensorConflict; continue; } catch (InvalidOperationException) { // TODO: In multi-proc scenarios, this is getting thrown at the start before we see IOException. Need to understand. // some other app has this sensor. newStatus |= ChooserStatus.SensorConflict; continue; } // Woo hoo, we have a started sensor. newStatus = ChooserStatus.SensorStarted; newSensor = sensor; break; } } SetSensorAndStatus(callbackLock, newSensor, newStatus); }