Playback class, which supports playing an event file to the Kinect service
示例#1
0
        /// <summary>
        /// Handles all command-line arguments
        /// </summary>
        /// <param name="args">Arguments array</param>
        /// <returns>CommandLineResult to indicate if the operation was successful or not</returns>
        private CommandLineResult ProcessCommandLine(string[] args)
        {
            string        logFile                        = string.Empty;
            List <string> streamList                     = new List <string>();
            bool          updatePersonalMetadata         = false;
            uint          loopCount                      = 0;
            Dictionary <string, List <string> > commands = new Dictionary <string, List <string> >();

            try
            {
                if (args == null)
                {
                    return(CommandLineResult.Succeeded);
                }

                if (args.Length == 0)
                {
                    return(CommandLineResult.Succeeded);
                }

                // gather commands and support parameters
                for (int i = 0; i < args.Length; i++)
                {
                    string arg = args[i];
                    if (this.IsArgument(arg))
                    {
                        string key = arg.Substring(1).ToUpperInvariant();

                        if (!commands.ContainsKey(key))
                        {
                            commands.Add(key, new List <string>());

                            for (int j = i + 1; j < args.Length; j++)
                            {
                                string param = args[j];
                                if (!this.IsArgument(param))
                                {
                                    commands[key].Add(param);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                // process all command args
                if (commands.ContainsKey("?") || commands.ContainsKey(Strings.Command_Help))
                {
                    this.RunCommandLine();
                    this.ShowCommandLineHelp();
                    return(CommandLineResult.SucceededExit);
                }

                // -log <filename>
                if (commands.ContainsKey(Strings.Command_Log))
                {
                    if (commands[Strings.Command_Log].Count != 1)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Log));
                        return(CommandLineResult.Invalid);
                    }

                    logFile = commands[Strings.Command_Log][0];
                }

                // -loop <count>
                if (commands.ContainsKey(Strings.Command_Loop))
                {
                    if (commands[Strings.Command_Loop].Count != 1)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Loop));
                        return(CommandLineResult.Invalid);
                    }

                    if (!uint.TryParse(commands[Strings.Command_Loop][0], out loopCount))
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Loop));
                        return(CommandLineResult.Invalid);
                    }
                }

                // -pii
                if (commands.ContainsKey(Strings.Command_PII))
                {
                    updatePersonalMetadata = true;
                }

                // -stream <stream1> <stream2> <stream3> ...
                if (commands.ContainsKey(Strings.Command_Stream))
                {
                    if (commands[Strings.Command_Stream].Count == 0)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Stream));
                        return(CommandLineResult.Invalid);
                    }

                    streamList = commands[Strings.Command_Stream];
                }

                // -view <filename>
                if (commands.ContainsKey(Strings.Command_View))
                {
                    this.RunCommandLine();
                    if (commands[Strings.Command_View].Count != 1)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_View));
                        return(CommandLineResult.Invalid);
                    }

                    string fileInfo = string.Empty;
                    string filePath = commands[Strings.Command_View][0];
                    this.CheckFile(filePath);
                    Console.WriteLine(Strings.WaitForViewFile);

                    using (KStudioClient client = KStudio.CreateClient())
                    {
                        if (filePath.ToUpperInvariant().StartsWith(Strings.ConsoleClipRepository.ToUpperInvariant()))
                        {
                            client.ConnectToService();
                        }

                        using (KStudioEventFile eventFile = client.OpenEventFile(filePath))
                        {
                            FileData data = new FileData(eventFile);
                            fileInfo = data.GetFileDataAsText();
                            Console.WriteLine(fileInfo);
                        }
                    }

                    if (!string.IsNullOrEmpty(logFile))
                    {
                        Logger.Start(logFile, true);
                        Logger.Log(fileInfo);
                        Logger.Stop();
                    }

                    Console.WriteLine(Strings.Done);
                    return(CommandLineResult.SucceededExit);
                }

                // -compare <filename1> <filename2>
                if (commands.ContainsKey(Strings.Command_Compare))
                {
                    this.RunCommandLine();
                    if (commands[Strings.Command_Compare].Count != 2)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Compare));
                        return(CommandLineResult.Invalid);
                    }

                    string fileInfo = string.Empty;
                    string file1    = commands[Strings.Command_Compare][0];
                    string file2    = commands[Strings.Command_Compare][1];
                    this.CheckFile(file1);
                    this.CheckFile(file2);
                    Console.WriteLine(Strings.WaitForCompareFiles);

                    using (KStudioClient client = KStudio.CreateClient())
                    {
                        if (file1.ToUpperInvariant().StartsWith(Strings.ConsoleClipRepository.ToUpperInvariant()) || file2.ToUpperInvariant().StartsWith(Strings.ConsoleClipRepository.ToUpperInvariant()))
                        {
                            client.ConnectToService();
                        }

                        using (KStudioEventFile eventFile1 = client.OpenEventFile(file1))
                            using (KStudioEventFile eventFile2 = client.OpenEventFile(file2))
                            {
                                FileData        fileData1   = new FileData(eventFile1);
                                FileData        fileData2   = new FileData(eventFile2);
                                CompareFileData compareData = new CompareFileData(fileData1, fileData2);
                                fileInfo = compareData.GetCompareFileDataAsText();
                                Console.WriteLine(fileInfo);
                            }
                    }

                    if (!string.IsNullOrEmpty(logFile))
                    {
                        Logger.Start(logFile, true);
                        Logger.Log(fileInfo);
                        Logger.Stop();
                    }

                    Console.WriteLine(Strings.Done);
                    return(CommandLineResult.SucceededExit);
                }

                // -update <filename> <metadata key> <metadata value>
                if (commands.ContainsKey(Strings.Command_Update))
                {
                    this.RunCommandLine();
                    if (commands[Strings.Command_Update].Count != 3)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Update));
                        return(CommandLineResult.Invalid);
                    }

                    string filePath = commands[Strings.Command_Update][0];
                    string key      = commands[Strings.Command_Update][1];
                    object value    = commands[Strings.Command_Update][2];
                    this.CheckFile(filePath);
                    string metadataText = string.Empty;

                    if (streamList.Count > 0)
                    {
                        // update stream metadata
                        foreach (string streamName in streamList)
                        {
                            Console.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.UpdatingStreamMetadata, streamName));
                            metadataText = this.EditMetadata(filePath, key, value, streamName, updatePersonalMetadata, true);
                            Console.WriteLine(metadataText);
                        }
                    }
                    else
                    {
                        // update file metadata
                        Console.WriteLine(Strings.UpdatingFileMetadata);
                        metadataText = this.EditMetadata(filePath, key, value, string.Empty, updatePersonalMetadata, false);
                        Console.WriteLine(metadataText);
                    }

                    Console.WriteLine(Strings.Done);
                    return(CommandLineResult.SucceededExit);
                }

                // -remove <filename> <metadata key>
                if (commands.ContainsKey(Strings.Command_Remove))
                {
                    this.RunCommandLine();
                    if (commands[Strings.Command_Remove].Count != 2)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Remove));
                        return(CommandLineResult.Invalid);
                    }

                    string filePath = commands[Strings.Command_Remove][0];
                    string key      = commands[Strings.Command_Remove][1];
                    this.CheckFile(filePath);
                    string metadataText = string.Empty;

                    if (streamList.Count > 0)
                    {
                        // update stream metadata
                        foreach (string streamName in streamList)
                        {
                            Console.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.UpdatingStreamMetadata, streamName));
                            metadataText = this.EditMetadata(filePath, key, null, streamName, updatePersonalMetadata, true);
                            Console.WriteLine(metadataText);
                        }
                    }
                    else
                    {
                        // update file metadata
                        Console.WriteLine(Strings.UpdatingFileMetadata);
                        metadataText = this.EditMetadata(filePath, key, null, string.Empty, updatePersonalMetadata, false);
                        Console.WriteLine(metadataText);
                    }

                    Console.WriteLine(Strings.Done);
                    return(CommandLineResult.SucceededExit);
                }

                // -play <filename>
                if (commands.ContainsKey(Strings.Command_Play))
                {
                    this.RunCommandLine();
                    if (commands[Strings.Command_Play].Count != 1)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Play));
                        return(CommandLineResult.Invalid);
                    }

                    string filePath = commands[Strings.Command_Play][0];
                    this.CheckFile(filePath);

                    using (KStudioClient client = KStudio.CreateClient())
                    {
                        Console.WriteLine(Strings.WaitToConnect);
                        client.ConnectToService();

                        Console.WriteLine(Strings.StartPlayback);
                        Playback.PlaybackClip(client, filePath, streamList, loopCount);
                        Console.WriteLine(Strings.StopPlayback);

                        client.DisconnectFromService();
                    }

                    Console.WriteLine(Strings.Done);
                    return(CommandLineResult.SucceededExit);
                }

                // -record <filename> <duration>
                if (commands.ContainsKey(Strings.Command_Record))
                {
                    this.RunCommandLine();
                    if (commands[Strings.Command_Record].Count != 2)
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Record));
                        return(CommandLineResult.Invalid);
                    }

                    string filePath = commands[Strings.Command_Record][0];
                    this.CheckDirectory(filePath);

                    double time = 0;
                    if (!double.TryParse(commands[Strings.Command_Record][1], out time))
                    {
                        Console.Error.WriteLine(string.Format(Strings.ErrorInvalidArgs, Strings.Command_Record));
                        return(CommandLineResult.Invalid);
                    }

                    TimeSpan duration = TimeSpan.FromSeconds(time);
                    string   errorMsg = string.Empty;
                    string   fileInfo = string.Empty;

                    using (KStudioClient client = KStudio.CreateClient())
                    {
                        Console.WriteLine(Strings.WaitToConnect);
                        client.ConnectToService();

                        Console.WriteLine(Strings.StartRecording);
                        Recording.RecordClip(client, filePath, duration, streamList);
                        Console.WriteLine(Strings.StopRecording);

                        using (KStudioEventFile eventFile = client.OpenEventFile(filePath))
                        {
                            FileData fileData = new FileData(eventFile);
                            fileInfo = fileData.GetFileDataAsText();
                            Console.WriteLine(fileInfo);
                        }

                        client.DisconnectFromService();
                    }

                    if (!string.IsNullOrEmpty(logFile))
                    {
                        Logger.Start(logFile, true);
                        Logger.Log(fileInfo);
                        Logger.Stop();
                    }

                    Console.WriteLine(Strings.Done);
                    return(CommandLineResult.SucceededExit);
                }
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format(Strings.ErrorPrepend, ex.Message);
                Console.Error.WriteLine(errorMsg);
                return(CommandLineResult.Failed);
            }

            return(CommandLineResult.Invalid);
        }
示例#2
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);
                }
            }
        }