aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Transcoding/TranscodingJob.cs
blob: e504fec095106f2feb15dd9c31d4f56ca1791569 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using MediaBrowser.Common.Logging;

namespace MediaBrowser.Api.Transcoding
{
    /// <summary>
    /// Represents an active transcoding job
    /// </summary>
    public class TranscodingJob
    {
        public string InputFile { get; set; }
        public string OutputFile { get; set; }
        public string TranscoderPath { get; set; }
        public string Arguments { get; set; }

        public TranscoderJobStatus Status { get; private set; }

        /// <summary>
        /// Starts the job
        /// </summary>
        public void Start()
        {
            ApiService.AddTranscodingJob(this);
            
            ProcessStartInfo startInfo = new ProcessStartInfo();

            startInfo.CreateNoWindow = true;

            startInfo.UseShellExecute = false;

            startInfo.FileName = TranscoderPath;
            startInfo.WorkingDirectory = Path.GetDirectoryName(TranscoderPath);
            startInfo.Arguments = Arguments;

            Logger.LogInfo("TranscodingJob.Start: " + TranscoderPath + " " + Arguments);

            Process process = new Process();

            process.StartInfo = startInfo;

            process.EnableRaisingEvents = true;

            process.Start();

            process.Exited += process_Exited;
        }

        void process_Exited(object sender, EventArgs e)
        {
            ApiService.RemoveTranscodingJob(this);
            
            Process process = sender as Process;

            // If it terminated with an error
            if (process.ExitCode != 0)
            {
                Status = TranscoderJobStatus.Error;

                // Delete this since it won't be valid
                if (File.Exists(OutputFile))
                {
                    File.Delete(OutputFile);
                }
            }
            else
            {
                Status = TranscoderJobStatus.Completed;
            }

            process.Dispose();
        }

        /// <summary>
        /// Provides a helper to wait for the job to exit
        /// </summary>
        public void WaitForExit()
        {
            while (true)
            {
                TranscoderJobStatus status = Status;

                if (status == TranscoderJobStatus.Completed || status == TranscoderJobStatus.Error)
                {
                    break;
                }

                Thread.Sleep(500);
            }
        }
    }

    public enum TranscoderJobStatus
    {
        Queued,
        Started,
        Completed,
        Error
    }
}