Ejemplo n.º 1
0
 public ThumbnailGenerator(string token, string workingDir, FileDownload downloader)
 {
     this.workingDir = workingDir;
     this.id         = token;
     this.downloader = downloader;
     this.complete   = false;
 }
Ejemplo n.º 2
0
 public ThumbnailGenerator(string token, string workingDir, FileDownload downloader)
 {
     this.workingDir = workingDir;
     this.id = token;
     this.downloader = downloader;
     this.complete = false;
 }
Ejemplo n.º 3
0
        public void AddDownload(FileDownload download)
        {
            try
            {
                log.Debug("Adding download to job: " + this.jobToken);
                this.status   = "Downloading";
                this.download = download;


                DirectoryInfo di = Directory.CreateDirectory(this.WorkingDir);

                foreach (var file in di.GetFiles())
                {
                    file.Delete();
                }

                if (File.Exists(FFRest.config["workingdir"] + download.Destination))
                {
                    File.Move(FFRest.config["workingdir"] + download.Destination, this.WorkingDir + Download.Destination);
                }

                results = new List <TranscoderResult>();
                if (!this.failed)
                {
                    taskSet.EnqueSimpleTask(download);
                }
            }
            catch (Exception ex)
            {
                log.Error("An error occured AddDownload: " + this.jobToken, ex);
            }
        }
Ejemplo n.º 4
0
 public TranscodeJob(ThreadPool pool, string jobToken, string callbackUrl)
 {
     this.taskSet = new TaskSet(pool,this, WaitStrategy.MODERATE);
     this.jobToken = jobToken;
     this.flaggedComplete = false;
     this.download = null;
     this.failed = false;
     this.cleaned = false;
     this.startTime = DateTime.Now;
     this.callbackUrl = callbackUrl;
     this.markedTasks = 0;
     this.tag = null;
     this.passes = new ConcurrentDictionary<string, Multipass>();
     this.workingDirectory =  FFRest.config["workingdir"] + Path.DirectorySeparatorChar + this.jobToken;
     if (this.workingDirectory[this.workingDirectory.Length-1] != Path.DirectorySeparatorChar)
     {
         this.workingDirectory += Path.DirectorySeparatorChar;
     }
 }
Ejemplo n.º 5
0
 public TranscodeJob(ThreadPool pool, string jobToken, string callbackUrl)
 {
     this.taskSet          = new TaskSet(pool, this, WaitStrategy.MODERATE);
     this.jobToken         = jobToken;
     this.flaggedComplete  = false;
     this.download         = null;
     this.failed           = false;
     this.cleaned          = false;
     this.startTime        = DateTime.Now;
     this.callbackUrl      = callbackUrl;
     this.markedTasks      = 0;
     this.tag              = null;
     this.passes           = new ConcurrentDictionary <string, Multipass>();
     this.workingDirectory = FFRest.config["workingdir"] + Path.DirectorySeparatorChar + this.jobToken;
     if (this.workingDirectory[this.workingDirectory.Length - 1] != Path.DirectorySeparatorChar)
     {
         this.workingDirectory += Path.DirectorySeparatorChar;
     }
 }
Ejemplo n.º 6
0
 public void CleanUp()
 {
     try
     {
         DirectoryInfo di = new DirectoryInfo(this.workingDirectory);
         foreach (var file in di.GetFiles())
         {
             file.Delete();
         }
         di.Delete();
         this.passes.Clear();
         this.taskSet        = null;
         this.download       = null;
         this.thumbGenerator = null;
         this.callbackUrl    = null;
         this.cleaned        = true;
     }
     catch (Exception ex)
     {
         log.Error("Failed to cleanup working directory", ex);
     }
 }
Ejemplo n.º 7
0
 public void CleanUp()
 {
     try
     {
         DirectoryInfo di = new DirectoryInfo(this.workingDirectory);
         foreach (var file in di.GetFiles())
         {
             file.Delete();
         }
         di.Delete();
         this.passes.Clear();
         this.taskSet = null;
         this.download = null;
         this.thumbGenerator = null;
         this.callbackUrl = null;
         this.cleaned = true;
     }
     catch (Exception ex)
     {
         log.Error("Failed to cleanup working directory", ex);
     }
 }
Ejemplo n.º 8
0
        public void AddDownload(FileDownload download)
        {
            try
            {
                log.Debug("Adding download to job: " + this.jobToken);
                this.status = "Downloading";
                this.download = download;

                 DirectoryInfo di =   Directory.CreateDirectory(this.WorkingDir);

                 foreach (var file in di.GetFiles())
                 {
                     file.Delete();
                 }

                if (File.Exists(FFRest.config["workingdir"] + download.Destination))
                {
                    File.Move(FFRest.config["workingdir"] + download.Destination, this.WorkingDir + Download.Destination);
                }

                results = new List<TranscoderResult>();
                if (!this.failed)
                {
                    taskSet.EnqueSimpleTask(download);
                }
            }
            catch (Exception ex)
            {
                log.Error("An error occured AddDownload: " + this.jobToken, ex);
            }
        }
Ejemplo n.º 9
0
        public void HandlePost(HttpListenerRequest request, HttpListenerResponse response, Server.RequestData data)
        {
            try
            {
                string jobID              = data.PostParameters.GetFirstValue("jobid");
                string taskID             = data.PostParameters.GetFirstValue("taskid");
                string complete           = data.PostParameters.GetFirstValue("complete");
                string videoTag           = data.PostParameters.GetFirstValue("tag");
                string videoUrl           = data.PostParameters.GetFirstValue("video");
                string preset             = data.PostParameters.GetFirstValue("preset");
                string options            = data.PostParameters.GetFirstValue("encoding-options");
                string callbackUrl        = data.PostParameters.GetFirstValue("callback");
                string outputExtension    = data.PostParameters.GetFirstValue("extension");
                string multiPass          = data.PostParameters.GetFirstValue("multipass");
                string multiPassExtension = data.PostParameters.GetFirstValue("multipass-extension");
                string multiPassOptions   = data.PostParameters.GetFirstValue("multipass-options");
                string segment            = data.PostParameters.GetFirstValue("segment");

                if (string.IsNullOrEmpty(jobID))
                {
                    response.WriteResponse(400, "Missing parameter jobid");
                    return;
                }
                if (!Regex.Match(jobID, "^[a-zA-Z0-9_-]+$").Success)
                {
                    response.WriteResponse(400, "Job parameter container invalid characters [allowed characters a-Z, 0-9, _ -]");
                    return;
                }
                if (!string.IsNullOrEmpty(complete))
                {
                    if (jobs.ContainsKey(jobID))
                    {
                        jobs[jobID].Complete();
                        response.WriteResponse(200, jobs[jobID]);
                        return;
                    }
                    response.WriteResponse(400, "No such job");
                    return;
                }


                if (!string.IsNullOrEmpty(taskID))
                {
                    if (!Regex.Match(taskID, "^[a-zA-Z0-9_-]+$").Success)
                    {
                        response.WriteResponse(400, "TaskID parameter container invalid characters [allowed characters a-Z, 0-9, _ -]");
                        return;
                    }
                }
                else
                {
                    taskID = rGenerator.Next().ToString();
                }


                if (string.IsNullOrEmpty(outputExtension))
                {
                    response.WriteResponse(400, "Missing parameter extension");
                    return;
                }
                if (!Regex.Match(outputExtension, "^[a-zA-Z0-9]+$").Success)
                {
                    response.WriteResponse(400, "Extension contains invalid characters [allowed characters a-Z, 0-9]");
                    return;
                }
                bool multiPassVal = false;
                if (!string.IsNullOrEmpty(multiPass))
                {
                    if (string.IsNullOrEmpty(multiPassOptions))
                    {
                        response.WriteResponse(400, "MultiPass enabled but multipass-options not provided");
                        return;
                    }
                    if (string.IsNullOrEmpty(multiPassExtension))
                    {
                        response.WriteResponse(400, "MultiPass enabled but multipass-extension not provided");
                        return;
                    }
                    if (!Regex.Match(multiPassExtension, "^[a-zA-Z0-9]+$").Success)
                    {
                        response.WriteResponse(400, "Multipass extension contains invalid characters [allowed characters a-Z, 0-9]");
                        return;
                    }
                    multiPassVal     = true;
                    multiPassOptions = Uri.UnescapeDataString(multiPassOptions.Replace("+", " "));
                }
                else
                {
                    multiPassOptions   = null;
                    multiPassExtension = null;
                }
                if (!string.IsNullOrEmpty(segment))
                {
                    switch (segment.ToLower())
                    {
                    case "hls":
                        segment = "hls";
                        break;

                    default:
                        response.WriteResponse(400, "Invalid segment type valid types are [hls]");
                        return;
                    }
                }

                /*if (string.IsNullOrEmpty(callbackUrl))
                 * {
                 *  response.WriteResponse(400, "Missing parameter callbackUrl");
                 *  return;
                 * }*/
                if (string.IsNullOrEmpty(preset) && string.IsNullOrEmpty(options))
                {
                    response.WriteResponse(400, "Job must specify ffmpeg parameters or a preset");
                    return;
                }
                if (!string.IsNullOrEmpty(options))
                {
                    options = Uri.UnescapeDataString(options.Replace("+", " "));
                }
                if (data.Files.Count == 0)
                {
                    if (string.IsNullOrEmpty(videoUrl))
                    {
                        response.WriteResponse(400, "Missing parameter video");
                        return;
                    }
                    videoUrl = Uri.UnescapeDataString(videoUrl);
                }
                else
                {
                    if (!string.IsNullOrEmpty(videoUrl))
                    {
                        response.WriteResponse(400, "A video was uploaded but a video url parameter was also provided");
                        return;
                    }
                }
                if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32S || Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.WinCE)
                {
                    if (options != null)
                    {
                        options = Regex.Replace(options, " '([a-zA-Z0-9{}:=_-]+)'", " \"$1\"");
                    }
                    if (multiPassOptions != null)
                    {
                        multiPassOptions = Regex.Replace(multiPassOptions, " '([a-zA-Z0-9{}:=_-]+)'", " \"$1\"");
                    }
                }
                if (!string.IsNullOrEmpty(callbackUrl))
                {
                    callbackUrl = Uri.UnescapeDataString(callbackUrl);
                }
                TranscodeJob tj    = null;
                bool         added = jobs.TryAdd(jobID, tj);
                if (added)
                {
                    tj = new TranscodeJob(this.jobPool, jobID, callbackUrl);
                    if (!string.IsNullOrEmpty(videoTag))
                    {
                        tj.Tag = Uri.UnescapeDataString(videoTag);
                    }
                    jobs[jobID] = tj;
                    FFRest.stats.AddJob();
                    try
                    {
                        if (data.Files.Count > 0)
                        {
                            var download = new FileDownload(jobID, tj.WorkingDir, data.Files[0].Name);

                            tj.AddDownload(download);
                        }
                        else
                        {
                            var download = new FileDownload(jobID, videoUrl, tj.WorkingDir, jobID + ".vod");

                            tj.AddDownload(download);
                        }
                    }
                    catch (Exception ex)
                    {
                        tj.Fail("Unable to perform transcoding, are your sure your video file is a valid format?");
                    }
                    tj.GenerateThumbnails();
                }
                else
                {
                    tj = jobs[jobID];
                    if (!string.IsNullOrEmpty(videoTag))
                    {
                        tj.Tag = Uri.UnescapeDataString(videoTag);
                    }
                    // We should already have a file for this job on hand
                    if (tj.IsComplete && (tj.CanExpire || tj.IsFailed))
                    {
                        tj          = new TranscodeJob(this.jobPool, jobID, callbackUrl);
                        jobs[jobID] = tj;
                        FFRest.stats.AddJob();
                        try
                        {
                            if (data.Files.Count > 0)
                            {
                                var download = new FileDownload(jobID, tj.WorkingDir, data.Files[0].Name);
                                tj.AddDownload(download);
                            }
                            else
                            {
                                var download = new FileDownload(jobID, videoUrl, tj.WorkingDir, jobID + ".vod");
                                tj.AddDownload(download);
                            }
                        }
                        catch (Exception ex)
                        {
                            tj.Fail("Unable to perform transcoding, are your sure your video file is a valid format?");
                        }
                        tj.GenerateThumbnails();
                    }
                    else
                    {
                        if (data.Files.Count > 0)
                        {
                            foreach (var file in data.Files)
                            {
                                File.Delete(file.Path);
                            }
                        }
                    }
                }

                if (multiPassVal)
                {
                    var mpass = new Multipass(tj, jobID, tj.WorkingDir, multiPassExtension, multiPassOptions);
                    tj.AddFirstPass(mpass);
                }


                TranscoderResult tr = new TranscoderResult(tj.JobToken, taskID, tj.WorkingDir);
                TranscodeTask    tt = new TranscodeTask(tj, tr, taskID, segment, options, outputExtension, multiPassVal);
                tj.AddTask(tt, tr);
                response.WriteResponse(200, new TaskSubmittedResponse()
                {
                    Job = tj, Task = tt
                });
            }
            catch (Exception ex)
            {
                log.Debug("Failed to process request", ex);
                response.WriteResponse(500, "Server Error");
            }
        }