aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Dto/MediaSourceInfo.cs
blob: 48f1e26c38b4cb5cd4c9d4f44c63b800cb993a00 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma warning disable CS1591
#pragma warning disable SA1600

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Session;

namespace MediaBrowser.Model.Dto
{
    public class MediaSourceInfo
    {
        public MediaProtocol Protocol { get; set; }
        public string Id { get; set; }

        public string Path { get; set; }

        public string EncoderPath { get; set; }
        public MediaProtocol? EncoderProtocol { get; set; }

        public MediaSourceType Type { get; set; }

        public string Container { get; set; }
        public long? Size { get; set; }

        public string Name { get; set; }

        /// <summary>
        /// Differentiate internet url vs local network
        /// </summary>
        public bool IsRemote { get; set; }

        public string ETag { get; set; }
        public long? RunTimeTicks { get; set; }
        public bool ReadAtNativeFramerate { get; set; }
        public bool IgnoreDts { get; set; }
        public bool IgnoreIndex { get; set; }
        public bool GenPtsInput { get; set; }
        public bool SupportsTranscoding { get; set; }
        public bool SupportsDirectStream { get; set; }
        public bool SupportsDirectPlay { get; set; }
        public bool IsInfiniteStream { get; set; }
        public bool RequiresOpening { get; set; }
        public string OpenToken { get; set; }
        public bool RequiresClosing { get; set; }
        public string LiveStreamId { get; set; }
        public int? BufferMs { get; set; }

        public bool RequiresLooping { get; set; }

        public bool SupportsProbing { get; set; }

        public VideoType? VideoType { get; set; }

        public IsoType? IsoType { get; set; }

        public Video3DFormat? Video3DFormat { get; set; }

        public List<MediaStream> MediaStreams { get; set; }

        public IReadOnlyList<MediaAttachment> MediaAttachments { get; set; }

        public string[] Formats { get; set; }

        public int? Bitrate { get; set; }

        public TransportStreamTimestamp? Timestamp { get; set; }
        public Dictionary<string, string> RequiredHttpHeaders { get; set; }

        public string TranscodingUrl { get; set; }
        public string TranscodingSubProtocol { get; set; }
        public string TranscodingContainer { get; set; }

        public int? AnalyzeDurationMs { get; set; }

        public MediaSourceInfo()
        {
            Formats = Array.Empty<string>();
            MediaStreams = new List<MediaStream>();
            MediaAttachments = Array.Empty<MediaAttachment>();
            RequiredHttpHeaders = new Dictionary<string, string>();
            SupportsTranscoding = true;
            SupportsDirectStream = true;
            SupportsDirectPlay = true;
            SupportsProbing = true;
        }

        public void InferTotalBitrate(bool force = false)
        {
            if (MediaStreams == null)
            {
                return;
            }

            if (!force && Bitrate.HasValue)
            {
                return;
            }

            var bitrate = 0;
            foreach (var stream in MediaStreams)
            {
                if (!stream.IsExternal)
                {
                    bitrate += stream.BitRate ?? 0;
                }
            }

            if (bitrate > 0)
            {
                Bitrate = bitrate;
            }
        }

        [JsonIgnore]
        public TranscodeReason[] TranscodeReasons { get; set; }

        public int? DefaultAudioStreamIndex { get; set; }
        public int? DefaultSubtitleStreamIndex { get; set; }

        public MediaStream GetDefaultAudioStream(int? defaultIndex)
        {
            if (defaultIndex.HasValue)
            {
                var val = defaultIndex.Value;

                foreach (var i in MediaStreams)
                {
                    if (i.Type == MediaStreamType.Audio && i.Index == val)
                    {
                        return i;
                    }
                }
            }

            foreach (var i in MediaStreams)
            {
                if (i.Type == MediaStreamType.Audio && i.IsDefault)
                {
                    return i;
                }
            }

            foreach (var i in MediaStreams)
            {
                if (i.Type == MediaStreamType.Audio)
                {
                    return i;
                }
            }

            return null;
        }

        [JsonIgnore]
        public MediaStream VideoStream
        {
            get
            {
                foreach (var i in MediaStreams)
                {
                    if (i.Type == MediaStreamType.Video)
                    {
                        return i;
                    }
                }

                return null;
            }
        }

        public MediaStream GetMediaStream(MediaStreamType type, int index)
        {
            foreach (var i in MediaStreams)
            {
                if (i.Type == type && i.Index == index)
                {
                    return i;
                }
            }

            return null;
        }

        public int? GetStreamCount(MediaStreamType type)
        {
            int numMatches = 0;
            int numStreams = 0;

            foreach (var i in MediaStreams)
            {
                numStreams++;
                if (i.Type == type)
                {
                    numMatches++;
                }
            }

            if (numStreams == 0)
            {
                return null;
            }

            return numMatches;
        }

        public bool? IsSecondaryAudio(MediaStream stream)
        {
            // Look for the first audio track marked as default
            foreach (var currentStream in MediaStreams)
            {
                if (currentStream.Type == MediaStreamType.Audio && currentStream.IsDefault)
                {
                    return currentStream.Index != stream.Index;
                }
            }

            // Look for the first audio track
            foreach (var currentStream in MediaStreams)
            {
                if (currentStream.Type == MediaStreamType.Audio)
                {
                    return currentStream.Index != stream.Index;
                }
            }

            return null;
        }
    }
}