/// <summary>
        /// Function used to store raw data from kinect
        /// </summary>
        /// <param name="filePath">path of the recording file</param>
        /// <param name="duration">duration of the recording in seconds</param>
        public void RecordData(string filePath, TimeSpan duration)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Body);

                using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                {
                    recording.StartTimed(duration);
                    while (recording.State == KStudioRecordingState.Recording)
                    {
                        Thread.Sleep(500);
                    }

                    if (recording.State == KStudioRecordingState.Error)
                    {
                        throw new InvalidOperationException("Error: Recording failed!");
                    }
                }

                client.DisconnectFromService();
            }
        }
Esempio n. 2
0
        public int GetNumOfFrames(Guid typeId)
        {
            int numFrames = 0;

            try
            {
                using (KStudioClient client = KStudio.CreateClient(KStudioClientFlags.ProcessNotifications))
                {
                    ManualResetEvent mr = new ManualResetEvent(false);
                    KStudioEventStreamSelectorCollection selEvents = new KStudioEventStreamSelectorCollection();
                    selEvents.Add(typeId);
                    //This is where you will intercept steps in the XEF file
                    using (var reader = client.CreateEventReader(_path, selEvents))
                    {
                        KStudioEvent ev;
                        reader.GetNextEvent();
                        while ((ev = reader.GetNextEvent()) != null)
                        {
                            numFrames++;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (e.Message == "Operation is not valid due to the current state of the object.")
                {
                    throw new Exception($"No events of type {typeId} in this XEF file!");
                }
            }
            return(numFrames);
        }
Esempio n. 3
0
        public T[] LoadFrame <T>(int desiredFrameNum, Guid eventType, Func <KStudioEvent, T[]> pixelCopyOperation)
        {
            T[] pixels = null;

            using (KStudioClient client = KStudio.CreateClient(KStudioClientFlags.ProcessNotifications))
            {
                KStudioEventStreamSelectorCollection selEvents = new KStudioEventStreamSelectorCollection();
                selEvents.Add(eventType);

                //This is where you will intercept steps in the XEF file
                using (var reader = client.CreateEventReader(_path, selEvents))
                {
                    KStudioEvent ev;
                    reader.GetNextEvent();
                    int frameNum = 0;
                    while ((ev = reader.GetNextEvent()) != null)
                    {
                        if (frameNum == desiredFrameNum)
                        {
                            pixels = pixelCopyOperation(ev);
                            break;
                        }
                        frameNum++;
                    }
                }
            }
            return(pixels);
        }
Esempio n. 4
0
        public static void Play(object filePathObj)
        {
            using (var client = KStudio.CreateClient())
            {
                string filePath = (string)filePathObj;
                //Console.WriteLine("Connecting to a service");
                client.ConnectToService();
                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
                using (var playback = client.CreatePlayback(filePath))
                {
                    //Console.WriteLine("Playback created");
                    playback.EndBehavior = KStudioPlaybackEndBehavior.Stop;
                    playback.Start();

                    //for (var j = 0; j < 700; j++)
                    //{
                    //    playback.StepOnce();
                    //    Thread.Sleep(500);
                    //}
                    while (playback.State == KStudioPlaybackState.Playing)
                    {
                        Thread.Sleep(500);
                    }

                    if (playback.State == KStudioPlaybackState.Error)
                    {
                        throw new InvalidOperationException("Error: Playback failed!");
                    }
                }
                //return true;
                //Console.WriteLine("Disconnecting");
                client.DisconnectFromService();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Converts a collection of stream GUIDs to a KStudioEventStreamSelectorCollection object that can be used for playback and record
        /// </summary>
        /// <param name="streamGuids">collection of stream Guids to add</param>
        /// <param name="swapColor">true, if the uncompressed and compressed color streams should be swapped</param>
        /// <returns>A KStudioEventStreamSelectorCollection object which contains the requested streams</returns>
        internal static KStudioEventStreamSelectorCollection CreateStreamCollection(IEnumerable <Guid> streamGuids, bool swapColor)
        {
            KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
            HashSet <Guid> streamDataTypeIds = streamGuids as HashSet <Guid>;

            if (swapColor)
            {
                if (streamDataTypeIds.Contains(KStudioEventStreamDataTypeIds.UncompressedColor))
                {
                    streamDataTypeIds.Remove(KStudioEventStreamDataTypeIds.UncompressedColor);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.CompressedColor);
                }
                else if (streamDataTypeIds.Contains(KStudioEventStreamDataTypeIds.CompressedColor))
                {
                    streamDataTypeIds.Remove(KStudioEventStreamDataTypeIds.CompressedColor);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.UncompressedColor);
                }
            }

            foreach (Guid dataTypeId in streamDataTypeIds)
            {
                streamCollection.Add(dataTypeId);
            }

            return(streamCollection);
        }
Esempio n. 6
0
        /// <summary>
        /// Records a new .xef file
        /// </summary>
        /// <param name="filePath">Full path to where the file should be saved to</param>
        private void RecordClip(string filePath)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                // Specify which streams should be recorded
                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
                streamCollection.Add(KStudioEventStreamDataTypeIds.BodyIndex);

                // Create the recording object
                using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                {
                    recording.StartTimed(this.duration);
                    while (recording.State == KStudioRecordingState.Recording)
                    {
                        Thread.Sleep(500);
                    }
                }

                client.DisconnectFromService();
            }

            // Update UI after the background recording task has completed
            this.isRecording = false;
            this.Dispatcher.BeginInvoke(new NoArgDelegate(UpdateState));
        }
Esempio n. 7
0
        ///// <summary>
        ///// Function used to store raw data from kinect
        ///// </summary>
        ///// <param name="filePath">path of the recording file</param>
        ///// <param name="duration">duration of the recording in seconds</param>
        //public void RecordData(string filePath, TimeSpan duration)
        //{

        //    using (KStudioClient client = KStudio.CreateClient())
        //    {
        //        client.ConnectToService();

        //        KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
        //        streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
        //        streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
        //        streamCollection.Add(KStudioEventStreamDataTypeIds.Body);

        //        using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
        //        {

        //            recording.StartTimed(duration);
        //            while (recording.State == KStudioRecordingState.Recording)
        //            {
        //                Thread.Sleep(500);
        //            }

        //            if (recording.State == KStudioRecordingState.Error)
        //            {
        //                throw new InvalidOperationException("Error: Recording failed!");
        //            }
        //        }

        //        client.DisconnectFromService();
        //    }
        //}

        /// <summary>
        /// Function used to store raw data from kinect
        /// </summary>
        /// <param name="filePath">path of the recording file</param>
        public void RecordData(string filePath)
        {
            _keepRecording = true;
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
                //streamCollection.Add(KStudioEventStreamDataTypeIds.CompressedColor);

                using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                {
                    recording.Start();


                    //stopRecording.WaitOne();
                    while (_keepRecording)
                    {
                        Thread.Sleep(500);
                    }
                    recording.Stop();

                    if (recording.State == KStudioRecordingState.Error)
                    {
                        throw new InvalidOperationException("Error: Recording failed!");
                    }
                }

                client.DisconnectFromService();
            }
        }
        public Task RecordClip(string filePath, TimeSpan duration)
        {
            if (IsRecording || IsPlaying)
            {
                return(null);
            }

            if (duration == TimeSpan.Zero)
            {
                duration = _maxRecordingDuration;
            }

            _stopRequested = false;
            Task t = Task.Run(() =>
            {
                _recording = true;
                using (KStudioClient client = KStudio.CreateClient())
                {
                    client.ConnectToService();

                    KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                    //streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
                    streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
                    streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
                    streamCollection.Add(KStudioEventStreamDataTypeIds.BodyIndex);
                    streamCollection.Add(KStudioEventStreamDataTypeIds.UncompressedColor);

                    using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                    {
                        recording.StartTimed(duration);
                        while (recording.State == KStudioRecordingState.Recording)
                        {
                            if (_stopRequested)
                            {
                                recording.Stop();
                            }
                            Thread.Sleep(50);
                        }

                        if (recording.State == KStudioRecordingState.Error)
                        {
                            throw new InvalidOperationException("Error: Recording failed!");
                        }
                    }

                    client.DisconnectFromService();
                }
                Thread.Sleep(500);
                _recording = false;
            });

            return(t);
        }
        private void RecordClip(string filePath)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();

                if (FInputIRStream[0])
                {
                    streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
                }
                if (FInputDepthStream[0])
                {
                    streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
                }
                streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
                streamCollection.Add(KStudioEventStreamDataTypeIds.BodyIndex);
                if (FInputRGBStream[0])
                {
                    streamCollection.Add(KStudioEventStreamDataTypeIds.UncompressedColor);
                }

                recordingBufferSizeMB = (uint)FInputRecordingBufferSizeMB[0];

                recording = client.CreateRecording(filePath, streamCollection, recordingBufferSizeMB, KStudioRecordingFlags.IgnoreOptionalStreams);
                using (recording)
                {
                    recording.PropertyChanged += Recording_PropertyChanged;
                    recording.Start();

                    FRecordBufferSizeMegabytes[0] = (uint)recording.BufferSizeMegabytes;

                    while ((recording.State == KStudioRecordingState.Recording) && (doRecord))
                    {
                        Thread.Sleep(100);
                        //recording reports no values :(
                        FRecordBufferInUseSizeMegabytes[0] = recording.BufferInUseSizeMegabytes;
                    }
                    recording.Stop();

                    //wait until the recording buffer is empty
                    while (recording.BufferInUseSizeMegabytes > 0)
                    {
                        Thread.Sleep(50);
                        FRecordBufferInUseSizeMegabytes[0] = recording.BufferInUseSizeMegabytes;
                    }

                    recording.PropertyChanged -= Recording_PropertyChanged;
                }
                client.DisconnectFromService();
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Playback a list of clips
        /// </summary>
        /// <param name="filename">ObservableCollection of VideoModel objects that contains Filename of the clip to be played.</param>
        private void PlaybackClip(ObservableCollection <VideoModel> videos)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();
                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                VideoModel video;
                int        i = 0;

                while (i < videos.Count)
                {
                    video = videos[i];

                    if (!string.IsNullOrEmpty(video.Filename))
                    {
                        _status = "Playing " + video.Filename + " - " + (i + 1) + "/" + videos.Count;

                        using (KStudioPlayback playback = client.CreatePlayback(video.Filename))
                        {
                            _isPlaying = true;
                            _stop      = false;

                            // We can use playback.StartPaused() and SeekRelativeTime to start the clip at
                            // a specific time.
                            playback.Start();


                            while (playback.State == KStudioPlaybackState.Playing)
                            {
                                Thread.Sleep(150);

                                if (_stop)
                                {
                                    playback.Stop();
                                    break;
                                }
                            }

                            _isPlaying = false;

                            if (_stop)
                            {
                                return;
                            }
                        }
                    }
                    i++;
                }
                client.DisconnectFromService();
            }
        }
Esempio n. 11
0
 /// <summary>
 /// 开始录制xef文件
 /// </summary>
 /// <param name="filePath">xef文件路径</param>
 public void StartRecordClip(object filePath)
 {
     try
     {
         using (KStudioClient client = KStudio.CreateClient())
         {
             client.ConnectToService();
             KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
             //streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
             streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
             streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
             streamCollection.Add(KStudioEventStreamDataTypeIds.BodyIndex);
             //streamCollection.Add(KStudioEventStreamDataTypeIds.UncompressedColor);
             using (KStudioRecording recording = client.CreateRecording((string)filePath, streamCollection))
             {
                 recording.Start();
                 SharpAvi.IsCreateRecord = true;
                 SharpAvi.IsRecording    = true;
                 while (recording.State == KStudioRecordingState.Recording)
                 {
                     //Flag为1时调出循环,结束录像
                     if (_flag == 1)
                     {
                         break;
                     }
                 }
                 SharpAvi.IsStopRecord = true;
                 recording.Stop();
                 recording.Dispose();
             }
             client.DisconnectFromService();
             client.Dispose();
             _flag = 0;
         }
     }
     catch
     {
         _flag  = 0;
         _flag1 = 1;
         SharpAvi.IsStopRecord = true;
         MessageBox.Show("视频录制出现异常");
     }
 }
Esempio n. 12
0
        public void StartRecording(string filePath)
        {
            client = KStudio.CreateClient();

            client.ConnectToService();

            KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();

            streamCollection.Add(KStudioEventStreamDataTypeIds.UncompressedColor);
            streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
            // The enum value for Audio is missing. The GUID below was taken from Kinect Studio.
            var Audio = new Guid(0x787c7abd, 0x9f6e, 0x4a85, 0x8d, 0x67, 0x63, 0x65, 0xff, 0x80, 0xcc, 0x69);

            streamCollection.Add(Audio);

            recording = client.CreateRecording(filePath, streamCollection, KStudioRecordingFlags.IgnoreOptionalStreams);
            recording.Start();

            LogConsole.WriteLine("File opened and recording ...");
        }
Esempio n. 13
0
        public void OpenRecording(string filePath)
        {
            client = KStudio.CreateClient();

            client.ConnectToService();

            KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();

            streamCollection.Add(KStudioEventStreamDataTypeIds.UncompressedColor);
            streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
            Guid Audio = new Guid(0x787c7abd, 0x9f6e, 0x4a85, 0x8d, 0x67, 0x63, 0x65, 0xff, 0x80, 0xcc, 0x69);

            streamCollection.Add(Audio);

            playback = client.CreatePlayback(filePath, streamCollection);
            playback.StateChanged += KStudioClient_Playback_StateChanged;
            playback.Start();

            OnKinectAvailabilityChanged(this, true);

            LogConsole.WriteLine("Recording opened and playing ...");
        }
        /// <summary>
        /// Playback a clip
        /// </summary>
        /// <param name="filename">Path/name of the clip to be played.</param>
        private void PlaybackClip(string filename)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();

                using (KStudioPlayback playback = client.CreatePlayback(filename))
                {
                    // If the clip should not be evaluated from the begining.
                    // It should start paused.
                    if (_initialTime.Milliseconds > 0)
                    {
                        playback.StartPaused();
                        playback.SeekByRelativeTime(_initialTime);
                        playback.Resume();
                    }
                    else
                    {
                        playback.Start();
                    }

                    while (playback.State == KStudioPlaybackState.Playing && !finished)
                    {
                        Thread.Sleep(150);
                    }

                    // Finished the read of frames for calibration.
                    if (finished)
                    {
                        playback.Stop();
                    }
                }

                client.DisconnectFromService();
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Records a new .xef file and saves body data to a .txt file
        /// </summary>
        /// <param name="filePath">full path to where the file should be saved to</param>
        private void RecordClip(string filePath)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection
                {
                    KStudioEventStreamDataTypeIds.Ir,
                    KStudioEventStreamDataTypeIds.Depth,
                    KStudioEventStreamDataTypeIds.Body,
                    KStudioEventStreamDataTypeIds.BodyIndex,
                    KStudioEventStreamDataTypeIds.UncompressedColor
                };

                using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                {
                    recording.StartTimed(this.duration);
                    while (recording.State == KStudioRecordingState.Recording)
                    {
                        Thread.Sleep(500);
                    }
                }

                client.DisconnectFromService();
            }

            if (this.trackedBodies.Count > 0)
            {
                this.SaveBodiesToFile(this.trackedBodies);
            }

            // Update UI after the background recording task has completed
            this.isRecording = false;
            this.Dispatcher.BeginInvoke(new NoArgDelegate(UpdateState));
        }
Esempio n. 16
0
        public DepthProcessor LoadDepthFrame(int desiredFrameNum, int numberOfDepthSmooths)
        {
            var dProcessor = new DepthProcessor();

            dProcessor.FrameSelector = new FrameSelector(desiredFrameNum, numberOfDepthSmooths);

            using (KStudioClient client = KStudio.CreateClient(KStudioClientFlags.ProcessNotifications))
            {
                ManualResetEvent mr = new ManualResetEvent(false);
                KStudioEventStreamSelectorCollection selEvents = new KStudioEventStreamSelectorCollection();
                selEvents.Add(KStudioEventStreamDataTypeIds.Depth);

                //This is where you will intercept steps in the XEF file
                KStudioEventReader reader = client.CreateEventReader(_path, selEvents);

                KStudioEvent ev;
                while ((ev = reader.GetNextEvent()) != null && !dProcessor.FrameSelector.AreAllFramesCaptured)
                {
                    dProcessor.ProcessEvent(ev);
                }
                reader.Dispose();
            }
            return(dProcessor);
        }
Esempio n. 17
0
        /// <summary>
        /// Plays an event file to the Kinect service
        /// </summary>
        /// <param name="client">KStudioClient which is connected to the Kinect service</param>
        /// <param name="filePath">Path to event file which is targeted for playback</param>
        /// <param name="streamNames">Collection of streams to include in the playback session</param>
        /// <param name="loopCount">Number of times the playback should be repeated before stopping</param>
        public static void PlaybackClip(KStudioClient client, string filePath, IEnumerable <string> streamNames, uint loopCount)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            if (!client.IsServiceConnected)
            {
                throw new InvalidOperationException(Strings.ErrorNotConnected);
            }

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            KStudioPlayback playback = null;

            // determine if all specified streams are valid for playback
            if (streamNames.Count <string>() > 0)
            {
                HashSet <Guid> playbackDataTypeIds = StreamSupport.ConvertStreamsToPlaybackGuids(streamNames);
                StreamSupport.VerifyStreamsForRecordAndPlayback(playbackDataTypeIds);
                Playback.VerifyStreamsForPlayback(client, filePath, playbackDataTypeIds);

                try
                {
                    KStudioEventStreamSelectorCollection streams = StreamSupport.CreateStreamCollection(playbackDataTypeIds, false);
                    playback = client.CreatePlayback(filePath, streams);
                }
                catch (Exception)
                {
                    //K4W supports uncompressed and compressed color, so if we get an error, try playing the other type
                    KStudioEventStreamSelectorCollection streams = StreamSupport.CreateStreamCollection(playbackDataTypeIds, true);
                    playback = client.CreatePlayback(filePath, streams);
                }
            }
            else
            {
                playback = client.CreatePlayback(filePath);
            }

            // begin playback
            using (playback)
            {
                playback.EndBehavior = KStudioPlaybackEndBehavior.Stop;   // this is the default behavior
                playback.Mode        = KStudioPlaybackMode.TimingEnabled; // this is the default behavior
                playback.LoopCount   = loopCount;
                playback.Start();

                while (playback.State == KStudioPlaybackState.Playing)
                {
                    Thread.Sleep(500);
                }

                if (playback.State == KStudioPlaybackState.Error)
                {
                    throw new InvalidOperationException(Strings.ErrorPlaybackFailed);
                }
            }
        }
        public void StartMonitor()
        {
            DebugHelper.AssertUIThread();

            if ((this.client != null) && (this.monitor == null))
            {
                KStudioEventStreamSelectorCollection streamSelectorCollection = new KStudioEventStreamSelectorCollection();
                foreach (KStudioEventStream s in this.targetMonitorableStreams.OfType<KStudioEventStream>())
                {
                    EventStreamState streamState = s.UserState as EventStreamState;
                    if (streamState != null)
                    {
                        if (streamState.IsSelectedForTargetMonitor)
                        {
                            streamSelectorCollection.Add(s.DataTypeId, s.SemanticId);

                            this.lastMonitoredStreams.HashSet.Add(new KStudioEventStreamIdentifier(s.DataTypeId, s.SemanticId));
                        }
                        else
                        {
                            this.lastMonitoredStreams.HashSet.Remove(new KStudioEventStreamIdentifier(s.DataTypeId, s.SemanticId));
                        }
                    }
                }

                if (streamSelectorCollection.Count > 0)
                {
                    try
                    {
                        this.MonitorTime = TimeSpan.Zero;

                        this.monitor = this.client.CreateMonitor(streamSelectorCollection, KStudioMonitorFlags.IgnoreOptionalStreams);

                        this.monitor.PropertyChanged += this.Monitor_PropertyChanged;

                        this.MonitorState = this.monitor.State;

                        KStudioMonitor temp = this.monitor;
                        this.RunAsync(() => temp.Start());

                        if (this.pluginService != null)
                        {
                            this.pluginService.ClearEvents(EventType.Monitor);
                        }

                        this.RaisePropertyChanged("MonitorViewStateTitle");
                        this.RaisePropertyChanged("ComboViewStateTitle");

                        this.monitorTimer.Start();
                    }
                    catch (Exception ex)
                    {
                        this.StopMonitor();

                        this.HandleMonitorError(ex);
                        return;
                    }
                }
            }
        }
        public void StartRecording()
        {
            DebugHelper.AssertUIThread();

            this.ClosePlayback();

            if ((this.client != null) && (this.recording == null) && (this.playback == null))
            {
                bool rawRecording = false;

                KStudioEventStreamSelectorCollection streamSelectorCollection = new KStudioEventStreamSelectorCollection();
                foreach (KStudioEventStream s in this.targetRecordableStreams.OfType<KStudioEventStream>().Where(s => ((EventStreamState)s.UserState).IsSelectedForTargetRecording))
                {
                    if (s.DataTypeId == KStudioEventStreamDataTypeIds.RawIr)
                    {
                        rawRecording = true;
                    }

                    streamSelectorCollection.Add(s.DataTypeId, s.SemanticId);
                }

                string filePath = null;

                try
                {
                    if ((this.settings != null) && this.settings.AutoStopMonitorOnRecord)
                    {
                        this.StopMonitor();
                    }

                    this.RecordingTime = TimeSpan.Zero;

                    KStudioRecordingFlags flags = KStudioRecordingFlags.GenerateFileName | KStudioRecordingFlags.IgnoreOptionalStreams;
                    if (rawRecording)
                    {
                        flags |= KStudioRecordingFlags.XrfFileName;
                    }

                    this.recording = this.client.CreateRecording(this.TargetFilePath, streamSelectorCollection, this.settings.RecordingBufferSizeMB, flags);

                    filePath = recording.FilePath;

                    this.RecordingState = this.recording.State;
                    this.RecordingFilePath = filePath;
                    this.RecordingFileSizeBytes = this.recording.FileSizeBytes;
                    this.RecordingBufferSizeMegabytes = this.recording.BufferSizeMegabytes;
                    this.RecordingBufferInUseMegabytes = this.recording.BufferInUseSizeMegabytes;

                    this.RecordingMetadata = new MetadataInfo(false, Strings.RecordingMetadata_Title,
                        this.recordingFilePath,
                        new WritableMetadataProxy(null, this.recording.GetMetadata(KStudioMetadataType.Public)),
                        new WritableMetadataProxy(null, this.recording.GetMetadata(KStudioMetadataType.PersonallyIdentifiableInformation)));

                    foreach (KStudioEventStream s in this.targetRecordableStreams.OfType<KStudioEventStream>())
                    {
                        EventStreamState streamState = s.UserState as EventStreamState;
                        if (streamState != null)
                        {
                            if (streamState.IsSelectedForTargetRecording)
                            {
                                streamState.Metadata = new MetadataInfo(false, streamState.ShortName,
                                    this.recordingFilePath + "\n" + streamState.LongName,
                                    new WritableMetadataProxy(null, this.recording.GetMetadataForStream(KStudioMetadataType.Public, s.DataTypeId, s.SemanticId)),
                                    new WritableMetadataProxy(null, this.recording.GetMetadataForStream(KStudioMetadataType.PersonallyIdentifiableInformation, s.DataTypeId, s.SemanticId)));

                                this.lastRecordedStreams.HashSet.Add(new KStudioEventStreamIdentifier(s.DataTypeId, s.SemanticId));
                            }
                            else
                            {
                                this.lastRecordedStreams.HashSet.Remove(new KStudioEventStreamIdentifier(s.DataTypeId, s.SemanticId));
                            }
                        }
                    }

                    this.recording.PropertyChanged += this.Recording_PropertyChanged;

                    this.SetDefaultMetadataViews(this.recordingMetadata);

                    KStudioRecording temp = this.recording;
                    this.RunAsync(() => temp.Start());
                }
                catch (Exception ex)
                {
                    this.CloseRecording(false);

                    this.HandleFileCreateError(ex, filePath);
                }
            }
        }
Esempio n. 20
0
        public void RecordClip()
        {
            //Console.WriteLine("the thing filepath is"+thing.FilePath);
            string xefname = filePath + MainWindow.textbox + ".xef";

            // Make sure to have enough disk space for the recording
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            //const string c = @"G:\";
            try
            {
                foreach (DriveInfo d in allDrives)
                {
                    if (d.IsReady && d.Name == c)
                    {
                        long totfreespace = d.TotalFreeSpace;
                        if (totfreespace < minimum_size)
                        {
                            string size = (minimum_size / 1E9).ToString();
                            mes = "Not enough disk space to record Kinect video, Available memory less than "
                                  + size + " GB";
                            throw new System.ArgumentException(mes);
                        }
                    }
                }
            }
            catch (ArgumentException exx)
            {
                Console.WriteLine("{0} Exception caught.", exx);
                done = true;
                //act_rec.Reset();
                rec_error = true;

                if (exx.Message != mes)
                {
                    // Let's restart the Kinect video service
                    // This service once stopped does not seem to restart automatically
                    Process[] kstu = Process.GetProcessesByName("KStudioHostService");
                    ///  kstu[0].Kill(); // Kill the process
                    kstu = Process.GetProcessesByName("KinectService");
                    kstu[0].Kill();
                }

                while (MessageBox.Show(exx.Message, "ERROR", MessageBoxButton.OK,
                                       MessageBoxImage.Error) != MessageBoxResult.OK)
                {
                    Thread.Sleep(20);
                }

                // Reset the recording error before exiting
                rec_error = false;
                return;
            }

            // try
            //  {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
                streamCollection.Add(KStudioEventStreamDataTypeIds.BodyIndex);
                streamCollection.Add(KStudioEventStreamDataTypeIds.UncompressedColor);
                //streamCollection.Add(KStudioEventStreamDataTypeIds.CompressedColor);

                try
                {
                    Console.WriteLine("now the filepath is " + filePath);
                    using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                    {
                        // Introduce a timer to make sure that the recording is never longer than expected
                        //  act_rec.Start();

                        //recording.StartTimed(duration);
                        recording.Start();

                        while (recording.Duration.TotalMilliseconds < dur && done == false)
                        {
                            Thread.Sleep(30);
                            //int si = (int)recording.BufferInUseSizeMegabytes;
                            // Console.WriteLine("Recording Buffer in Megabytes {0} ", si);
                        }

                        recording.Stop();
                        recording.Dispose();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception caught.", e);
                    done      = true;
                    rec_error = true;

                    while (MessageBox.Show("ERROR", e.Message, MessageBoxButton.OK, MessageBoxImage.Error) != MessageBoxResult.OK)
                    {
                        Thread.Sleep(20);
                    }


                    // Reset the recording error before exiting
                    rec_error = false;

                    return;
                }
                finally
                {
                    Thread.Sleep(100);
                    client.DisconnectFromService();
                }
            }

            // Make sure to reset the bool done variable once recording is done
        }
        private void StartPlayback(bool startPaused)
        {
            DebugHelper.AssertUIThread();

            if ((this.playback == null) && (this.playbackFile != null))
            {
                if (!this.IsPlaybackFileOnTarget)
                {
                    this.StopMonitor();
                }

                KStudioEventStreamSelectorCollection streamSelectorCollection = null;
                KStudioPlaybackFlags flags = KStudioPlaybackFlags.IgnoreOptionalStreams;

                this.playbackStep = null;

                if (this.playbackFileLocal)
                {
                    flags |= KStudioPlaybackFlags.ToClient;

                    streamSelectorCollection = new KStudioEventStreamSelectorCollection();

                    foreach (KStudioEventStream stream in this.playbackFile.EventStreams)
                    {
                        if ((this.pluginService == null) || this.pluginService.IsInterestedInEventsFrom(EventType.Inspection, stream.DataTypeId, stream.SemanticId))
                        {
                            streamSelectorCollection.Add(stream.DataTypeId, stream.SemanticId);

                            // last one always wins
                            if ((stream.DataTypeId == KStudioEventStreamDataTypeIds.LongExposureIr) ||
                                (stream.DataTypeId == KStudioEventStreamDataTypeIds.Ir) ||
                                (stream.DataTypeId == KStudioEventStreamDataTypeIds.Depth) ||
                                (stream.DataTypeId == KStudioEventStreamDataTypeIds.RawIr))
                            {
                                this.playbackStep = new KStudioEventStreamIdentifier(stream.DataTypeId, stream.SemanticId);
                            }
                        }
                    }
                }
                else
                {
                    streamSelectorCollection = new KStudioEventStreamSelectorCollection();

                    foreach (KStudioEventStream stream in this.targetPlaybackableStreams.OfType<KStudioEventStream>())
                    {
                        EventStreamState ess = stream.UserState as EventStreamState;
                        if ((ess != null) && ess.IsSelectedForTargetPlayback && (ess.SelectedFilePlaybackStream != null))
                        {
                            streamSelectorCollection.Add(stream.DataTypeId, ess.SelectedFilePlaybackStream.SemanticId, stream.SemanticId);

                            if (stream.SemanticId == KStudioEventStreamSemanticIds.KinectDefaultSensorConsumer)
                            {
                                if ((stream.DataTypeId == KStudioEventStreamDataTypeIds.LongExposureIr))
                                {
                                    // LEIR always wins because of the way mapping order works
                                    this.playbackStep = new KStudioEventStreamIdentifier(stream.DataTypeId, ess.SelectedFilePlaybackStream.SemanticId);
                                }
                                else if ((stream.DataTypeId == KStudioEventStreamDataTypeIds.Ir) ||
                                         (stream.DataTypeId == KStudioEventStreamDataTypeIds.RawIr))
                                {
                                    if (this.playbackStep == null)
                                    {
                                        this.playbackStep = new KStudioEventStreamIdentifier(stream.DataTypeId, ess.SelectedFilePlaybackStream.SemanticId);
                                    }
                                }
                            }
                        }
                    }
                }

                this.playback = this.client.CreatePlayback(playbackFile, streamSelectorCollection, flags);

                this.playback.PropertyChanged += this.Playback_PropertyChanged;

                this.PlaybackState = this.playback.State;

                PlaybackFileSettings playbackFileSettings = this.playbackFile.UserState as PlaybackFileSettings;
                if (playbackFileSettings != null)
                {
                    playbackFileSettings.SaveStreamSelection();
                }

                if (this.playbackPausePoints != null)
                {
                    this.playbackPausePoints.Playback = this.playback;
                }

                if (this.playbackInOutPoints != null)
                {
                    this.playbackInOutPoints.Playback = this.playback;
                }

                this.playback.LoopCount = this.playbackLoopCount;
                this.playback.CurrentLoopIteration = this.playbackLoopIteration;

                KStudioPlayback temp = this.playback;

                if (this.playbackStartTime != TimeSpan.Zero)
                {
                    temp.SeekByRelativeTime(this.playbackStartTime);

                    this.playbackStartTime = TimeSpan.Zero;
                }

                if (this.IsPlaybackFileOnTarget && (this.pluginService != null))
                {
                    this.pluginService.ClearEvents(EventType.Inspection);
                }

                this.RaisePropertyChanged("MonitorViewStateTitle");
                this.RaisePropertyChanged("ComboViewStateTitle");

                if (startPaused)
                {
                    this.RunAsync(() => temp.StartPaused());
                }
                else
                {
                    this.RunAsync(() => temp.Start());
                }

                if (this.IsPlaybackFileOnTarget && (this.settings != null) && this.settings.AutoMonitorOnTargetPlayback)
                {
                    this.StartMonitor();
                }
            }
            else if (this.playback != null)
            {
                KStudioPlayback temp = this.playback;

                if (this.playback.State == KStudioPlaybackState.Paused)
                {
                    this.RunAsync(() => temp.Resume());
                }
                else
                {
                    this.RunAsync(() => temp.Start());
                }
            }
        }
Esempio n. 22
0
        public static void RecordClip(string filePath, TimeSpan duration)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection
                {
                    KStudioEventStreamDataTypeIds.Ir,
                    KStudioEventStreamDataTypeIds.Depth,
                    KStudioEventStreamDataTypeIds.Body,
                    KStudioEventStreamDataTypeIds.BodyIndex
                };

                using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                {
                    recording.StartTimed(duration);
                    while (recording.State == KStudioRecordingState.Recording)
                    {
                        Thread.Sleep(500);
                    }

                    if (recording.State == KStudioRecordingState.Error)
                    {
                        throw new InvalidOperationException("Error: Recording failed!");
                    }
                }

                client.DisconnectFromService();
            }
        }
        /// <summary>
        /// Records a new .xef file
        /// </summary>
        /// <param name="filePath">Full path to where the file should be saved to</param>
        private void RecordClip(string filePath)
        {
            using (KStudioClient client = KStudio.CreateClient())
            {
                client.ConnectToService();

                // Specify which streams should be recorded
                KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
                streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
                streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
                streamCollection.Add(KStudioEventStreamDataTypeIds.BodyIndex);

                // Create the recording object
                using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
                {
                    recording.StartTimed(this.duration);
                    while (recording.State == KStudioRecordingState.Recording)
                    {
                        Thread.Sleep(500);
                    }
                }

                client.DisconnectFromService();
            }

            // Update UI after the background recording task has completed
            this.isRecording = false;
            this.Dispatcher.BeginInvoke(new NoArgDelegate(UpdateState));
        }
Esempio n. 24
0
        /// <summary>
        /// Records streams from the Kinect sensor to an event file
        /// </summary>
        /// <param name="client">KStudioClient which is connected to the Kinect service</param>
        /// <param name="filePath">Path to new event file which will be created for recording</param>
        /// <param name="duration">How long the recording should last before being stopped</param>
        /// <param name="streamNames">Collection of streams to include in the recording</param>
        public static void RecordClip(KStudioClient client, string filePath, TimeSpan duration, IEnumerable<string> streamNames)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            if (!client.IsServiceConnected)
            {
                throw new InvalidOperationException(Strings.ErrorNotConnected);
            }

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            HashSet<Guid> streamDataTypeIds = new HashSet<Guid>();
            KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
            KStudioRecording recording = null;

            // decide which streams to record
            if (streamNames != null && streamNames.Count<string>() > 0)
            {
                streamDataTypeIds = StreamSupport.ConvertStreamsToGuids(streamNames);
                StreamSupport.VerifyStreamsForRecordAndPlayback(streamDataTypeIds);
            }
            else
            {
                if (Path.GetExtension(filePath).ToLower().Equals(Strings.XrfExtension))
                {
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.RawIr);
                }
                else
                {
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.Ir);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.Depth);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.Body);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.BodyIndex);
                }
            }

            // verify streams are recordable by the Kinect sensor
            foreach (Guid stream in streamDataTypeIds)
            {
                KStudioEventStream eventStream = client.GetEventStream(stream, KStudioEventStreamSemanticIds.KinectDefaultSensorProducer);
                if (!eventStream.IsRecordable)
                {
                    throw new InvalidOperationException(string.Format(Strings.ErrorRecordingStreamNotSupported, StreamSupport.ConvertStreamGuidToString(stream)));
                }

                streamCollection.Add(stream);
            }

            // fix file extension, if necessary
            if (streamDataTypeIds.Contains(KStudioEventStreamDataTypeIds.RawIr) && Path.GetExtension(filePath).ToUpperInvariant().Equals(Strings.XefExtension.ToUpperInvariant()))
            {
                Path.ChangeExtension(filePath, Strings.XrfExtension);
            }

            // attempt to record streams for the specified duration
            try
            {
                recording = client.CreateRecording(filePath, streamCollection);
            }
            catch (Exception)
            {
                //K4W supports uncompressed and compressed color, so if we get an error, try recording the other type
                streamCollection = StreamSupport.CreateStreamCollection(streamDataTypeIds, true);
                recording = client.CreateRecording(filePath, streamCollection);
            }

            using (recording)
            {
                recording.StartTimed(duration);
                while (recording.State == KStudioRecordingState.Recording)
                {
                    Thread.Sleep(500);
                }

                if (recording.State == KStudioRecordingState.Error)
                {
                    throw new InvalidOperationException(Strings.ErrorRecordingFailed);
                }
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Records streams from the Kinect sensor to an event file
        /// </summary>
        /// <param name="client">KStudioClient which is connected to the Kinect service</param>
        /// <param name="filePath">Path to new event file which will be created for recording</param>
        /// <param name="duration">How long the recording should last before being stopped</param>
        /// <param name="streamNames">Collection of streams to include in the recording</param>
        public static void RecordClip(KStudioClient client, string filePath, TimeSpan duration, IEnumerable <string> streamNames)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            if (!client.IsServiceConnected)
            {
                throw new InvalidOperationException(Strings.ErrorNotConnected);
            }

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            HashSet <Guid> streamDataTypeIds = new HashSet <Guid>();
            KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
            KStudioRecording recording = null;

            // decide which streams to record
            if (streamNames != null && streamNames.Count <string>() > 0)
            {
                streamDataTypeIds = StreamSupport.ConvertStreamsToGuids(streamNames);
                StreamSupport.VerifyStreamsForRecordAndPlayback(streamDataTypeIds);
            }
            else
            {
                if (Path.GetExtension(filePath).ToLower().Equals(Strings.XrfExtension))
                {
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.RawIr);
                }
                else
                {
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.Ir);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.Depth);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.Body);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.BodyIndex);
                }
            }

            // verify streams are recordable by the Kinect sensor
            foreach (Guid stream in streamDataTypeIds)
            {
                KStudioEventStream eventStream = client.GetEventStream(stream, KStudioEventStreamSemanticIds.KinectDefaultSensorProducer);
                if (!eventStream.IsRecordable)
                {
                    throw new InvalidOperationException(string.Format(Strings.ErrorRecordingStreamNotSupported, StreamSupport.ConvertStreamGuidToString(stream)));
                }

                streamCollection.Add(stream);
            }

            // fix file extension, if necessary
            if (streamDataTypeIds.Contains(KStudioEventStreamDataTypeIds.RawIr) && Path.GetExtension(filePath).ToUpperInvariant().Equals(Strings.XefExtension.ToUpperInvariant()))
            {
                Path.ChangeExtension(filePath, Strings.XrfExtension);
            }

            // attempt to record streams for the specified duration
            try
            {
                recording = client.CreateRecording(filePath, streamCollection);
            }
            catch (Exception)
            {
                //K4W supports uncompressed and compressed color, so if we get an error, try recording the other type
                streamCollection = StreamSupport.CreateStreamCollection(streamDataTypeIds, true);
                recording        = client.CreateRecording(filePath, streamCollection);
            }

            using (recording)
            {
                recording.StartTimed(duration);
                while (recording.State == KStudioRecordingState.Recording)
                {
                    Thread.Sleep(500);
                }

                if (recording.State == KStudioRecordingState.Error)
                {
                    throw new InvalidOperationException(Strings.ErrorRecordingFailed);
                }
            }
        }
Esempio n. 26
0
        /// <summary>
        /// Converts a collection of stream GUIDs to a KStudioEventStreamSelectorCollection object that can be used for playback and record
        /// </summary>
        /// <param name="streamGuids">collection of stream Guids to add</param>
        /// <param name="swapColor">true, if the uncompressed and compressed color streams should be swapped</param>
        /// <returns>A KStudioEventStreamSelectorCollection object which contains the requested streams</returns>
        internal static KStudioEventStreamSelectorCollection CreateStreamCollection(IEnumerable<Guid> streamGuids, bool swapColor)
        {
            KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
            HashSet<Guid> streamDataTypeIds = streamGuids as HashSet<Guid>;

            if (swapColor)
            {
                if (streamDataTypeIds.Contains(KStudioEventStreamDataTypeIds.UncompressedColor))
                {
                    streamDataTypeIds.Remove(KStudioEventStreamDataTypeIds.UncompressedColor);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.CompressedColor);
                }
                else if (streamDataTypeIds.Contains(KStudioEventStreamDataTypeIds.CompressedColor))
                {
                    streamDataTypeIds.Remove(KStudioEventStreamDataTypeIds.CompressedColor);
                    streamDataTypeIds.Add(KStudioEventStreamDataTypeIds.UncompressedColor);
                }
            }

            foreach (Guid dataTypeId in streamDataTypeIds)
            {
                streamCollection.Add(dataTypeId);
            }

            return streamCollection;
        }