summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services/Queue/QueueResourceService.cs
blob: cbd97029d1d0cc4f83a77920505d0ec3d331d253 (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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="QueueResourceService.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 QueueResourceService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.Services.Queue
{
    using System;
    using System.Collections.Generic;

    using HandBrake.Interop.Interop.Model.Encoding;

    public class QueueResourceService
    {
        private readonly object lockOjb = new object();

        private HashSet<Guid> qsvInstances = new HashSet<Guid>();
        private HashSet<Guid> nvencInstances = new HashSet<Guid>();
        private HashSet<Guid> vceInstances = new HashSet<Guid>();

        private List<int> qsvGpus = new List<int>();
        
        public void Init()
        {
            // TODO Coming Soon!
            //   List<int> qsvGpus = hbFunctions.hb_qsv_adapters_list();
        }

        public Guid? GetHardwareLock(VideoEncoder encoder)
        {
            lock (this.lockOjb)
            {
                switch (encoder)
                {
                    case VideoEncoder.QuickSync:
                    case VideoEncoder.QuickSyncH265:
                    case VideoEncoder.QuickSyncH26510b:
                        if (this.qsvInstances.Count < 3)
                        {
                            Guid guid = Guid.NewGuid();
                            this.qsvInstances.Add(guid);
                            return guid;
                        }
                        else
                        {
                            return Guid.Empty;
                        }

                    case VideoEncoder.NvencH264:
                    case VideoEncoder.NvencH265:
                        if (this.nvencInstances.Count < 3)
                        {
                            Guid guid = Guid.NewGuid();
                            this.nvencInstances.Add(guid);
                            return guid;
                        }
                        else
                        {
                            return Guid.Empty;
                        }

                    case VideoEncoder.VceH264:
                    case VideoEncoder.VceH265:
                        if (this.vceInstances.Count < 1)
                        {
                            Guid guid = Guid.NewGuid();
                            this.vceInstances.Add(guid);
                            return guid;
                        }
                        else
                        {
                            return Guid.Empty;
                        }
                }
            }

            return null;
        }

        public void UnlockHardware(VideoEncoder encoder, Guid? unlockKey)
        {
            if (unlockKey == null)
            {
                return;
            }

            lock (this.lockOjb)
            {
                switch (encoder)
                {
                    case VideoEncoder.QuickSync:
                    case VideoEncoder.QuickSyncH265:
                    case VideoEncoder.QuickSyncH26510b:
                        if (this.qsvInstances.Contains(unlockKey.Value))
                        {
                            this.qsvInstances.Remove(unlockKey.Value);
                        }

                        break;
                    case VideoEncoder.NvencH264:
                    case VideoEncoder.NvencH265:
                        if (this.nvencInstances.Contains(unlockKey.Value))
                        {
                            this.nvencInstances.Remove(unlockKey.Value);
                        }

                        break;

                    case VideoEncoder.VceH264:
                    case VideoEncoder.VceH265:
                        if (this.vceInstances.Contains(unlockKey.Value))
                        {
                            this.vceInstances.Remove(unlockKey.Value);
                        }

                        break;
                }
            }
        }
    }
}