Exemplo n.º 1
0
        private async Task BackgroundStream()
        {
            Console.WriteLine("Started background stream for " + Id);
            DataReceivedEventHandler Handler = null;
            var ProcessStartInfo             = new ProcessStartInfo
            {
                FileName              = "Includes/ffmpeg",
                UseShellExecute       = false,
                RedirectStandardInput = true,
                RedirectStandardError = true
            };

            while (!Stop.IsCancellationRequested)
            {
                try
                {
                    await WaitAdd.WaitAsync(Stop.Token);

                    Queue.Next();

                    using (Ffmpeg = new Process())
                    {
                        ProcessStartInfo.Arguments = $"-re -i \"{await Queue.StreamUrl(false)}\" -vn -content_type audio/aac -f adts ";
                        if (Queue.Playing.Type == SongType.YouTube || Queue.Playing.Type == SongType.Uploaded)
                        {
                            ProcessStartInfo.Arguments += "-c:a copy ";
                        }
                        else
                        {
                            ProcessStartInfo.Arguments += $"-c:a aac -b:a 128k -ac 2 -ar 48k ";
                        }
                        ProcessStartInfo.Arguments += $"icecast://*****:*****@localhost:80/{Id}";

                        ProcessWaiter = new TaskCompletionSource <bool>();

                        Ffmpeg.StartInfo           = ProcessStartInfo;
                        Ffmpeg.EnableRaisingEvents = true;
                        Ffmpeg.Exited += (s, e) =>
                        {
                            ProcessWaiter.TrySetResult(true);
                        };

                        Handler = async(s, e) =>
                        {
                            Console.WriteLine(e.Data ?? "");
                            if (e.Data?.StartsWith("size=") ?? false)
                            {
                                Ffmpeg.ErrorDataReceived -= Handler;
                                await Task.Delay(250).ContinueWith(delegate
                                {
                                    Chat.Home.ById(Id).Distribute(new Cloud.Json.Event
                                    {
                                        Chat = Id,
                                        Type = "start",
                                        Text = Serialize(Id)
                                    });
                                });
                            }
                        };

                        Ffmpeg.ErrorDataReceived += Handler;

                        Ffmpeg.Start();
                        Ffmpeg.BeginErrorReadLine();
                        Ffmpeg.PriorityClass = ProcessPriorityClass.BelowNormal;
                        await ProcessWaiter.Task;

                        Ffmpeg.CancelErrorRead();
                        await Ffmpeg.StandardInput.WriteAsync("q");
                    }
                }
                catch (Exception Ex)
                {
                    Console.WriteLine(Ex.ToString());
                }
                finally
                {
                    Queue.Invalidate();
                    Console.WriteLine("Stopped Playing");
                }

                if (Queue.Count == 0)
                {
                    Chat.Home.ById(Id).Distribute(new Cloud.Json.Event
                    {
                        Chat = Id,
                        Type = "start"
                    });
                }
            }
        }