summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services/Queue/ActiveJob.cs
blob: d433e1625eb0d6b7d51ded30308de28fd1850eb4 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ActiveJob.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   Defines the ActiveJob type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.Services.Queue
{
    using System;
    
    using HandBrakeWPF.Services.Encode;
    using HandBrakeWPF.Services.Encode.EventArgs;
    using HandBrakeWPF.Services.Encode.Interfaces;
    using HandBrakeWPF.Services.Interfaces;
    using HandBrakeWPF.Services.Logging.Interfaces;
    using HandBrakeWPF.Services.Queue.JobEventArgs;
    using HandBrakeWPF.Services.Queue.Model;

    public class ActiveJob : IDisposable
    {
        private readonly QueueTask job;
        private readonly IEncode encodeService;

        public ActiveJob(QueueTask task, IUserSettingService userSettingService, ILogInstanceManager logInstanceManager, int jobId, IPortService portService)
        {
            this.job = task;
            this.encodeService = new LibEncode(userSettingService, logInstanceManager, jobId, portService);
        }

        public event EventHandler<ActiveJobCompletedEventArgs> JobFinished;

        public event EventHandler<EncodeProgressEventArgs> JobStatusUpdated;

        public QueueTask Job => this.job;

        public bool IsPaused { get; private set; }

        public bool IsEncoding { get; set; }
        
        public void Start()
        {
            this.IsPaused = false;
            this.IsEncoding = true;

            if (this.encodeService.IsPasued)
            {
                this.encodeService.Resume();
                this.job.Statistics.SetPaused(false);
                this.job.Status = QueueItemStatus.InProgress;
            }
            else if (!this.encodeService.IsEncoding)
            {
                this.job.Status = QueueItemStatus.InProgress;
                this.job.Statistics.StartTime = DateTime.Now;

                this.encodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted;
                this.encodeService.EncodeStatusChanged += this.EncodeStatusChanged;
                
                this.encodeService.Start(this.job.Task, this.job.Configuration, this.job.SelectedPresetKey);
            }
        }

        public void Pause()
        {
            if (this.encodeService.IsEncoding && !this.encodeService.IsPasued)
            {
                this.IsPaused = true;
                this.encodeService.Pause();
                this.job.Statistics.SetPaused(true);
                this.job.Status = QueueItemStatus.Paused;
                this.IsEncoding = false;
            }
        }

        public void Stop()
        {
            if (this.encodeService.IsEncoding)
            {
                this.encodeService.Stop();
            }

            this.IsEncoding = false;
            this.IsPaused = false;
            this.encodeService.EncodeStatusChanged -= this.EncodeStatusChanged;
        }

        public void Dispose()
        {
            this.encodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;
            this.encodeService.EncodeStatusChanged -= this.EncodeStatusChanged;
        }

        private void EncodeStatusChanged(object sender, EncodeProgressEventArgs e)
        {
            this.job?.JobProgress.Update(e);
            this.OnJobStatusUpdated(e);
        }

        private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)
        {
            this.IsEncoding = false;
            this.IsPaused = false;

            this.job.Status = !e.Successful ? QueueItemStatus.Error : QueueItemStatus.Completed;
            this.job.Statistics.EndTime = DateTime.Now;
            this.job.Statistics.CompletedActivityLogPath = e.ActivityLogPath;
            this.job.Statistics.FinalFileSize = e.FinalFilesizeInBytes;

            this.job.JobProgress.ClearStatusDisplay();
            
            this.encodeService.EncodeStatusChanged -= this.EncodeStatusChanged;
            this.encodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;

            this.OnJobFinished(e);
        }

        private void OnJobFinished(EncodeCompletedEventArgs e)
        {
            this.JobFinished?.Invoke(this, new ActiveJobCompletedEventArgs(this, e));
        }

        private void OnJobStatusUpdated(EncodeProgressEventArgs e)
        {
            this.JobStatusUpdated?.Invoke(this, e);
        }
    }
}