示例#1
0
        private void StartProcess()
        {
            var startinfo = new System.Diagnostics.ProcessStartInfo();
            var cmd       = this.Description.CommandLine;
            var args      =
                System.Text.RegularExpressions.Regex.Matches(cmd, @"(?:""[^""]+?"")|(?:\S+)")
                .Cast <System.Text.RegularExpressions.Match>()
                .Select(match => match.ToString())
                .ToArray();

            startinfo.WorkingDirectory       = this.Description.BasePath;
            startinfo.UseShellExecute        = false;
            startinfo.FileName               = args.First();
            startinfo.Arguments              = String.Join(" ", args.Skip(1));
            startinfo.CreateNoWindow         = true;
            startinfo.ErrorDialog            = false;
            startinfo.RedirectStandardInput  = true;
            startinfo.RedirectStandardOutput = true;
            startinfo.RedirectStandardError  = true;
            startinfo.StandardOutputEncoding = System.Text.Encoding.ASCII;
            startinfo.StandardErrorEncoding  = System.Text.Encoding.Default;
            processCancellationToken         = new CancellationTokenSource();
            var cancel = processCancellationToken.Token;

            process     = System.Diagnostics.Process.Start(startinfo);
            pipePackets = new WaitableQueue <byte[]>();
            var stdout = process.StandardOutput.BaseStream;
            var stdin  = process.StandardInput.BaseStream;
            var stderr = process.StandardError;

            stdErrorTask = Task.Run(async() => {
                try {
                    Logger logger = new Logger(typeof(CustomFilterContentSink), this.Description.Name);
                    while (!cancel.IsCancellationRequested)
                    {
                        var line = await stderr.ReadLineAsync().ConfigureAwait(false);
                        if (line == null)
                        {
                            break;
                        }
                        if (Description.Logging)
                        {
                            logger.Debug(line);
                        }
                    }
                    stderr.Close();
                }
                catch (System.IO.IOException) {
                }
                catch (ObjectDisposedException) {
                }
            });
            stdOutTask = Task.Run(async() => {
                try {
                    long pos   = 0;
                    var buffer = new byte[1024 * 15];
                    while (!cancel.IsCancellationRequested)
                    {
                        var len = await stdout.ReadAsync(buffer, 0, buffer.Length, cancel).ConfigureAwait(false);
                        System.Console.WriteLine("stdout {0}", len);
                        if (len <= 0)
                        {
                            break;
                        }
                        Sink.OnContent(new Content(lastContent.Stream, lastContent.Timestamp, pos, buffer, 0, len));
                        pos += len;
                    }
                    stdout.Close();
                }
                catch (System.IO.IOException) {
                }
                catch (ObjectDisposedException) {
                }
                finally {
                    System.Console.WriteLine("stdoutclosed");
                }
            });
            writable  = true;
            stdInTask = Task.Run(async() => {
                try {
                    while (!cancel.IsCancellationRequested)
                    {
                        var packet = await pipePackets.DequeueAsync(cancel).ConfigureAwait(false);
                        if (packet != null)
                        {
                            await stdin.WriteAsync(packet, 0, packet.Length, cancel).ConfigureAwait(false);
                            if (pipePackets.IsEmpty)
                            {
                                await stdin.FlushAsync().ConfigureAwait(false);
                            }
                        }
                        else
                        {
                            stdin.Close();
                            break;
                        }
                    }
                }
                catch (System.IO.IOException) {
                }
                catch (ObjectDisposedException) {
                }
                finally {
                    writable = false;
                    System.Console.WriteLine("stdinclosed");
                }
                //TODO:子プロセスが死んだ時に上流か下流かに伝える必要がある
            });
        }
 public Task <ChannelMessage> DequeueAsync(CancellationToken ct)
 {
     return(queue.DequeueAsync(ct));
 }
示例#3
0
            private async Task ProcessMessagesAsync(IContentSink targetSink, CancellationToken cancellationToken)
            {
                var bufferStream  = new MemoryStream();
                var context       = new FLVToMPEG2TS.Context(bufferStream);
                var contentBuffer = new MemoryStream();
                var fileParser    = new FLVFileParser();
                var msg           = await msgQueue.DequeueAsync(cancellationToken).ConfigureAwait(false);

                while (msg.Type != ContentMessage.MessageType.Stop)
                {
                    switch (msg.Type)
                    {
                    case ContentMessage.MessageType.ChannelInfo:
                        targetSink.OnChannelInfo(msg.ChannelInfo);
                        break;

                    case ContentMessage.MessageType.ChannelTrack:
                        targetSink.OnChannelTrack(msg.ChannelTrack);
                        break;

                    case ContentMessage.MessageType.ContentHeader:
                    {
                        var buffer = contentBuffer;
                        var pos    = buffer.Position;
                        buffer.Seek(0, SeekOrigin.End);
                        buffer.Write(msg.Content.Data.Span);
                        buffer.Position = pos;
                        fileParser.Read(buffer, context);
                        if (buffer.Position != 0)
                        {
                            var new_buf  = new MemoryStream();
                            var trim_pos = buffer.Position;
                            buffer.Close();
                            var buf = buffer.ToArray();
                            new_buf.Write(buf, (int)trim_pos, (int)(buf.Length - trim_pos));
                            new_buf.Position = 0;
                            contentBuffer    = new_buf;
                        }
                        if (bufferStream.Position != 0)
                        {
                            targetSink.OnContentHeader(
                                new Content(
                                    msg.Content.Stream,
                                    msg.Content.Timestamp,
                                    msg.Content.Position,
                                    bufferStream.ToArray(),
                                    msg.Content.ContFlag
                                    )
                                );
                            bufferStream.SetLength(0);
                        }
                    }
                    break;

                    case ContentMessage.MessageType.ContentBody:
                    {
                        var buffer = contentBuffer;
                        var pos    = buffer.Position;
                        buffer.Seek(0, SeekOrigin.End);
                        buffer.Write(msg.Content.Data.Span);
                        buffer.Position = pos;
                        fileParser.Read(buffer, context);
                        if (buffer.Position != 0)
                        {
                            var new_buf  = new MemoryStream();
                            var trim_pos = buffer.Position;
                            buffer.Close();
                            var buf = buffer.ToArray();
                            new_buf.Write(buf, (int)trim_pos, (int)(buf.Length - trim_pos));
                            new_buf.Position = 0;
                            contentBuffer    = new_buf;
                        }
                        if (bufferStream.Position != 0)
                        {
                            targetSink.OnContent(
                                new Content(
                                    msg.Content.Stream,
                                    msg.Content.Timestamp,
                                    msg.Content.Position,
                                    bufferStream.ToArray(),
                                    msg.Content.ContFlag
                                    )
                                );
                            bufferStream.SetLength(0);
                        }
                    }
                    break;
                    }
                    msg = await msgQueue.DequeueAsync(cancellationToken).ConfigureAwait(false);
                }
                targetSink.OnStop(msg.StopReason);
            }
示例#4
0
 private Task <Packet> GetPacket(CancellationToken cancel_token)
 {
     return(contentPacketQueue.DequeueAsync(cancel_token));
 }