Exemplo n.º 1
0
        public VideoEncoder ScheduleEncode(Video video)
        {
            VideoEncoder encoder = null;

            lock (instanceLock)
            {
                lock (instanceLock)
                {
                    if (!runningEncoders.ContainsKey(video.node.id))
                    {
                        encoder = new VideoEncoder(this, video);
                        LongTask task = new LongTask(() => {
                            encoder.Run();
                            lock (instanceLock)
                            {
                                runningEncoders.Remove(encoder.Video.node.id);
                                runningEncodersById.Remove(encoder.Id);
                            }
                        }, video.node.owner, $"Build MP4 for '{video.node.name}' (id: {video.node.id})");
                        scheduler.Start(task);
                        runningEncoders[video.node.id]  = encoder;
                        runningEncodersById[encoder.Id] = encoder;
                    }
                    else
                    {
                        encoder = runningEncoders[video.node.id];
                    }
                }
            }
            return(encoder);
        }
Exemplo n.º 2
0
        public VideoEncoderService(PriorityTaskScheduler scheduler)
        {
            this.scheduler = scheduler;

            Get["/{id}"] = (p, c) =>
            {
                var          id      = (string)p["id"];
                VideoEncoder encoder = null;
                lock (instanceLock)
                {
                    if (runningEncodersById.ContainsKey(id))
                    {
                        encoder = runningEncodersById[id];
                    }
                }
                if (encoder != null)
                {
                    c.Response.StatusCode = 200;
                    c.Response.Content    = encoder.ToJson();
                }
            };

            PostAsync["/{id}/progress"] = async(p, c) =>
            {
                var values = new Dictionary <string, string>();

                string       id      = (string)p["id"];
                VideoEncoder encoder = null;
                lock (instanceLock)
                {
                    if (runningEncodersById.ContainsKey(id))
                    {
                        encoder = runningEncodersById[id];
                    }
                }
                if (encoder == null)
                {
                    return;
                }

                using (StreamReader sr = new StreamReader(c.Request.InputStream))
                {
                    var end = false;
                    while (!sr.EndOfStream && !end)
                    {
                        var line = await sr.ReadLineAsync();

                        var pos = line.IndexOf('=');
                        if (pos > 0)
                        {
                            var cmd   = line.Substring(0, pos);
                            var value = line.Substring(pos + 1);
                            if (cmd == "progress")
                            {
                                end |= value == "end";
                                // get the total micro secondes position in the video
                                if (values.ContainsKey("out_time_ms") && long.TryParse(values["out_time_ms"], out long outTimeMs))
                                {
                                    encoder.TimeProgress = ((double)outTimeMs) / 1000000.0d;
                                }
                                // get current frame position in the video
                                if (values.ContainsKey("frame") && long.TryParse(values["frame"], out long frame))
                                {
                                    encoder.Frame = frame;
                                }
                                values.Clear();
                            }
                            else
                            {
                                values[cmd] = value;
                            }
                        }
                    }
                }
            };
        }