| | | 1 | | #pragma warning disable CA1819 // Properties should not return arrays |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Text; |
| | | 9 | | using Jellyfin.Data.Enums; |
| | | 10 | | using Jellyfin.Extensions; |
| | | 11 | | using MediaBrowser.Model.Drawing; |
| | | 12 | | using MediaBrowser.Model.Dto; |
| | | 13 | | using MediaBrowser.Model.Entities; |
| | | 14 | | using MediaBrowser.Model.MediaInfo; |
| | | 15 | | using MediaBrowser.Model.Session; |
| | | 16 | | |
| | | 17 | | namespace MediaBrowser.Model.Dlna; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Class holding information on a stream. |
| | | 21 | | /// </summary> |
| | | 22 | | public class StreamInfo |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="StreamInfo"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | public StreamInfo() |
| | | 28 | | { |
| | | 29 | | AudioCodecs = []; |
| | | 30 | | VideoCodecs = []; |
| | | 31 | | SubtitleCodecs = []; |
| | | 32 | | StreamOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the item id. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <value>The item id.</value> |
| | | 39 | | public Guid ItemId { get; set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets or sets the play method. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <value>The play method.</value> |
| | | 45 | | public PlayMethod PlayMethod { get; set; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets or sets the encoding context. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <value>The encoding context.</value> |
| | | 51 | | public EncodingContext Context { get; set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets or sets the media type. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <value>The media type.</value> |
| | | 57 | | public DlnaProfileType MediaType { get; set; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets or sets the container. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <value>The container.</value> |
| | | 63 | | public string? Container { get; set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets or sets the sub protocol. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <value>The sub protocol.</value> |
| | | 69 | | public MediaStreamProtocol SubProtocol { get; set; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets or sets the start position ticks. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <value>The start position ticks.</value> |
| | | 75 | | public long StartPositionTicks { get; set; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets or sets the segment length. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <value>The segment length.</value> |
| | | 81 | | public int? SegmentLength { get; set; } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets or sets the minimum segments count. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <value>The minimum segments count.</value> |
| | | 87 | | public int? MinSegments { get; set; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets or sets a value indicating whether the stream requires AVC. |
| | | 91 | | /// </summary> |
| | | 92 | | public bool RequireAvc { get; set; } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets or sets a value indicating whether the stream requires AVC. |
| | | 96 | | /// </summary> |
| | | 97 | | public bool RequireNonAnamorphic { get; set; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets or sets a value indicating whether timestamps should be copied. |
| | | 101 | | /// </summary> |
| | | 102 | | public bool CopyTimestamps { get; set; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets or sets a value indicating whether timestamps should be copied. |
| | | 106 | | /// </summary> |
| | | 107 | | public bool EnableMpegtsM2TsMode { get; set; } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets or sets a value indicating whether the subtitle manifest is enabled. |
| | | 111 | | /// </summary> |
| | | 112 | | public bool EnableSubtitlesInManifest { get; set; } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Gets or sets the audio codecs. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <value>The audio codecs.</value> |
| | | 118 | | public IReadOnlyList<string> AudioCodecs { get; set; } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Gets or sets the video codecs. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <value>The video codecs.</value> |
| | | 124 | | public IReadOnlyList<string> VideoCodecs { get; set; } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets or sets the audio stream index. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <value>The audio stream index.</value> |
| | | 130 | | public int? AudioStreamIndex { get; set; } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets or sets the video stream index. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <value>The subtitle stream index.</value> |
| | | 136 | | public int? SubtitleStreamIndex { get; set; } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets or sets the maximum transcoding audio channels. |
| | | 140 | | /// </summary> |
| | | 141 | | /// <value>The maximum transcoding audio channels.</value> |
| | | 142 | | public int? TranscodingMaxAudioChannels { get; set; } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Gets or sets the global maximum audio channels. |
| | | 146 | | /// </summary> |
| | | 147 | | /// <value>The global maximum audio channels.</value> |
| | | 148 | | public int? GlobalMaxAudioChannels { get; set; } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets or sets the audio bitrate. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <value>The audio bitrate.</value> |
| | | 154 | | public int? AudioBitrate { get; set; } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets or sets the audio sample rate. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <value>The audio sample rate.</value> |
| | | 160 | | public int? AudioSampleRate { get; set; } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets or sets the video bitrate. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <value>The video bitrate.</value> |
| | | 166 | | public int? VideoBitrate { get; set; } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Gets or sets the maximum output width. |
| | | 170 | | /// </summary> |
| | | 171 | | /// <value>The output width.</value> |
| | | 172 | | public int? MaxWidth { get; set; } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Gets or sets the maximum output height. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <value>The maximum output height.</value> |
| | | 178 | | public int? MaxHeight { get; set; } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Gets or sets the maximum framerate. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <value>The maximum framerate.</value> |
| | | 184 | | public float? MaxFramerate { get; set; } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Gets or sets the device profile. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <value>The device profile.</value> |
| | | 190 | | public required DeviceProfile DeviceProfile { get; set; } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Gets or sets the device profile id. |
| | | 194 | | /// </summary> |
| | | 195 | | /// <value>The device profile id.</value> |
| | | 196 | | public string? DeviceProfileId { get; set; } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Gets or sets the device id. |
| | | 200 | | /// </summary> |
| | | 201 | | /// <value>The device id.</value> |
| | | 202 | | public string? DeviceId { get; set; } |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Gets or sets the runtime ticks. |
| | | 206 | | /// </summary> |
| | | 207 | | /// <value>The runtime ticks.</value> |
| | | 208 | | public long? RunTimeTicks { get; set; } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Gets or sets the transcode seek info. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <value>The transcode seek info.</value> |
| | | 214 | | public TranscodeSeekInfo TranscodeSeekInfo { get; set; } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Gets or sets a value indicating whether content length should be estimated. |
| | | 218 | | /// </summary> |
| | | 219 | | public bool EstimateContentLength { get; set; } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Gets or sets the media source info. |
| | | 223 | | /// </summary> |
| | | 224 | | /// <value>The media source info.</value> |
| | | 225 | | public MediaSourceInfo? MediaSource { get; set; } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Gets or sets the subtitle codecs. |
| | | 229 | | /// </summary> |
| | | 230 | | /// <value>The subtitle codecs.</value> |
| | | 231 | | public IReadOnlyList<string> SubtitleCodecs { get; set; } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Gets or sets the subtitle delivery method. |
| | | 235 | | /// </summary> |
| | | 236 | | /// <value>The subtitle delivery method.</value> |
| | | 237 | | public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Gets or sets the subtitle format. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <value>The subtitle format.</value> |
| | | 243 | | public string? SubtitleFormat { get; set; } |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// Gets or sets the play session id. |
| | | 247 | | /// </summary> |
| | | 248 | | /// <value>The play session id.</value> |
| | | 249 | | public string? PlaySessionId { get; set; } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Gets or sets the transcode reasons. |
| | | 253 | | /// </summary> |
| | | 254 | | /// <value>The transcode reasons.</value> |
| | | 255 | | public TranscodeReason TranscodeReasons { get; set; } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Gets the stream options. |
| | | 259 | | /// </summary> |
| | | 260 | | /// <value>The stream options.</value> |
| | | 261 | | public Dictionary<string, string> StreamOptions { get; private set; } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Gets the media source id. |
| | | 265 | | /// </summary> |
| | | 266 | | /// <value>The media source id.</value> |
| | 201396 | 267 | | public string? MediaSourceId => MediaSource?.Id; |
| | | 268 | | |
| | | 269 | | /// <summary> |
| | | 270 | | /// Gets or sets a value indicating whether audio VBR encoding is enabled. |
| | | 271 | | /// </summary> |
| | | 272 | | public bool EnableAudioVbrEncoding { get; set; } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Gets or sets a value indicating whether always burn in subtitles when transcoding. |
| | | 276 | | /// </summary> |
| | | 277 | | public bool AlwaysBurnInSubtitleWhenTranscoding { get; set; } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Gets a value indicating whether the stream is direct. |
| | | 281 | | /// </summary> |
| | 603096 | 282 | | public bool IsDirectStream => MediaSource?.VideoType is not (VideoType.Dvd or VideoType.BluRay) |
| | 603096 | 283 | | && PlayMethod is PlayMethod.DirectStream or PlayMethod.DirectPlay; |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Gets the audio stream that will be used in the output stream. |
| | | 287 | | /// </summary> |
| | | 288 | | /// <value>The audio stream.</value> |
| | 692 | 289 | | public MediaStream? TargetAudioStream => MediaSource?.GetDefaultAudioStream(AudioStreamIndex); |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// Gets the video stream that will be used in the output stream. |
| | | 293 | | /// </summary> |
| | | 294 | | /// <value>The video stream.</value> |
| | 1006 | 295 | | public MediaStream? TargetVideoStream => MediaSource?.VideoStream; |
| | | 296 | | |
| | | 297 | | /// <summary> |
| | | 298 | | /// Gets the audio sample rate that will be in the output stream. |
| | | 299 | | /// </summary> |
| | | 300 | | /// <value>The target audio sample rate.</value> |
| | | 301 | | public int? TargetAudioSampleRate |
| | | 302 | | { |
| | | 303 | | get |
| | | 304 | | { |
| | 0 | 305 | | var stream = TargetAudioStream; |
| | 0 | 306 | | return AudioSampleRate.HasValue && !IsDirectStream |
| | 0 | 307 | | ? AudioSampleRate |
| | 0 | 308 | | : stream?.SampleRate; |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Gets the audio bit depth that will be in the output stream. |
| | | 314 | | /// </summary> |
| | | 315 | | /// <value>The target bit depth.</value> |
| | | 316 | | public int? TargetAudioBitDepth |
| | | 317 | | { |
| | | 318 | | get |
| | | 319 | | { |
| | 0 | 320 | | if (IsDirectStream) |
| | | 321 | | { |
| | 0 | 322 | | return TargetAudioStream?.BitDepth; |
| | | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | var targetAudioCodecs = TargetAudioCodec; |
| | 0 | 326 | | var audioCodec = targetAudioCodecs.Count == 0 ? null : targetAudioCodecs[0]; |
| | 0 | 327 | | if (!string.IsNullOrEmpty(audioCodec)) |
| | | 328 | | { |
| | 0 | 329 | | return GetTargetAudioBitDepth(audioCodec); |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | return TargetAudioStream?.BitDepth; |
| | | 333 | | } |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Gets the video bit depth that will be in the output stream. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <value>The target video bit depth.</value> |
| | | 340 | | public int? TargetVideoBitDepth |
| | | 341 | | { |
| | | 342 | | get |
| | | 343 | | { |
| | 96 | 344 | | if (IsDirectStream) |
| | | 345 | | { |
| | 0 | 346 | | return TargetVideoStream?.BitDepth; |
| | | 347 | | } |
| | | 348 | | |
| | 96 | 349 | | var targetVideoCodecs = TargetVideoCodec; |
| | 96 | 350 | | var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0]; |
| | 96 | 351 | | if (!string.IsNullOrEmpty(videoCodec)) |
| | | 352 | | { |
| | 96 | 353 | | return GetTargetVideoBitDepth(videoCodec); |
| | | 354 | | } |
| | | 355 | | |
| | 0 | 356 | | return TargetVideoStream?.BitDepth; |
| | | 357 | | } |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | /// <summary> |
| | | 361 | | /// Gets the target reference frames that will be in the output stream. |
| | | 362 | | /// </summary> |
| | | 363 | | /// <value>The target reference frames.</value> |
| | | 364 | | public int? TargetRefFrames |
| | | 365 | | { |
| | | 366 | | get |
| | | 367 | | { |
| | 0 | 368 | | if (IsDirectStream) |
| | | 369 | | { |
| | 0 | 370 | | return TargetVideoStream?.RefFrames; |
| | | 371 | | } |
| | | 372 | | |
| | 0 | 373 | | var targetVideoCodecs = TargetVideoCodec; |
| | 0 | 374 | | var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0]; |
| | 0 | 375 | | if (!string.IsNullOrEmpty(videoCodec)) |
| | | 376 | | { |
| | 0 | 377 | | return GetTargetRefFrames(videoCodec); |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | return TargetVideoStream?.RefFrames; |
| | | 381 | | } |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Gets the target framerate that will be in the output stream. |
| | | 386 | | /// </summary> |
| | | 387 | | /// <value>The target framerate.</value> |
| | | 388 | | public float? TargetFramerate |
| | | 389 | | { |
| | | 390 | | get |
| | | 391 | | { |
| | 0 | 392 | | var stream = TargetVideoStream; |
| | 0 | 393 | | return MaxFramerate.HasValue && !IsDirectStream |
| | 0 | 394 | | ? MaxFramerate |
| | 0 | 395 | | : stream?.ReferenceFrameRate; |
| | | 396 | | } |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | /// <summary> |
| | | 400 | | /// Gets the target video level that will be in the output stream. |
| | | 401 | | /// </summary> |
| | | 402 | | /// <value>The target video level.</value> |
| | | 403 | | public double? TargetVideoLevel |
| | | 404 | | { |
| | | 405 | | get |
| | | 406 | | { |
| | 96 | 407 | | if (IsDirectStream) |
| | | 408 | | { |
| | 0 | 409 | | return TargetVideoStream?.Level; |
| | | 410 | | } |
| | | 411 | | |
| | 96 | 412 | | var targetVideoCodecs = TargetVideoCodec; |
| | 96 | 413 | | var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0]; |
| | 96 | 414 | | if (!string.IsNullOrEmpty(videoCodec)) |
| | | 415 | | { |
| | 96 | 416 | | return GetTargetVideoLevel(videoCodec); |
| | | 417 | | } |
| | | 418 | | |
| | 0 | 419 | | return TargetVideoStream?.Level; |
| | | 420 | | } |
| | | 421 | | } |
| | | 422 | | |
| | | 423 | | /// <summary> |
| | | 424 | | /// Gets the target packet length that will be in the output stream. |
| | | 425 | | /// </summary> |
| | | 426 | | /// <value>The target packet length.</value> |
| | | 427 | | public int? TargetPacketLength |
| | | 428 | | { |
| | | 429 | | get |
| | | 430 | | { |
| | 0 | 431 | | var stream = TargetVideoStream; |
| | 0 | 432 | | return !IsDirectStream |
| | 0 | 433 | | ? null |
| | 0 | 434 | | : stream?.PacketLength; |
| | | 435 | | } |
| | | 436 | | } |
| | | 437 | | |
| | | 438 | | /// <summary> |
| | | 439 | | /// Gets the target video profile that will be in the output stream. |
| | | 440 | | /// </summary> |
| | | 441 | | /// <value>The target video profile.</value> |
| | | 442 | | public string? TargetVideoProfile |
| | | 443 | | { |
| | | 444 | | get |
| | | 445 | | { |
| | 96 | 446 | | if (IsDirectStream) |
| | | 447 | | { |
| | 0 | 448 | | return TargetVideoStream?.Profile; |
| | | 449 | | } |
| | | 450 | | |
| | 96 | 451 | | var targetVideoCodecs = TargetVideoCodec; |
| | 96 | 452 | | var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0]; |
| | 96 | 453 | | if (!string.IsNullOrEmpty(videoCodec)) |
| | | 454 | | { |
| | 96 | 455 | | return GetOption(videoCodec, "profile"); |
| | | 456 | | } |
| | | 457 | | |
| | 0 | 458 | | return TargetVideoStream?.Profile; |
| | | 459 | | } |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Gets the target video range type that will be in the output stream. |
| | | 464 | | /// </summary> |
| | | 465 | | /// <value>The video range type.</value> |
| | | 466 | | public VideoRangeType TargetVideoRangeType |
| | | 467 | | { |
| | | 468 | | get |
| | | 469 | | { |
| | 0 | 470 | | if (IsDirectStream) |
| | | 471 | | { |
| | 0 | 472 | | return TargetVideoStream?.VideoRangeType ?? VideoRangeType.Unknown; |
| | | 473 | | } |
| | | 474 | | |
| | 0 | 475 | | var targetVideoCodecs = TargetVideoCodec; |
| | 0 | 476 | | var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0]; |
| | 0 | 477 | | if (!string.IsNullOrEmpty(videoCodec) |
| | 0 | 478 | | && Enum.TryParse(GetOption(videoCodec, "rangetype"), true, out VideoRangeType videoRangeType)) |
| | | 479 | | { |
| | 0 | 480 | | return videoRangeType; |
| | | 481 | | } |
| | | 482 | | |
| | 0 | 483 | | return TargetVideoStream?.VideoRangeType ?? VideoRangeType.Unknown; |
| | | 484 | | } |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | /// <summary> |
| | | 488 | | /// Gets the target video codec tag. |
| | | 489 | | /// </summary> |
| | | 490 | | /// <value>The video codec tag.</value> |
| | | 491 | | public string? TargetVideoCodecTag |
| | | 492 | | { |
| | | 493 | | get |
| | | 494 | | { |
| | 0 | 495 | | var stream = TargetVideoStream; |
| | 0 | 496 | | return !IsDirectStream |
| | 0 | 497 | | ? null |
| | 0 | 498 | | : stream?.CodecTag; |
| | | 499 | | } |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | /// <summary> |
| | | 503 | | /// Gets the audio bitrate that will be in the output stream. |
| | | 504 | | /// </summary> |
| | | 505 | | /// <value>The audio bitrate.</value> |
| | | 506 | | public int? TargetAudioBitrate |
| | | 507 | | { |
| | | 508 | | get |
| | | 509 | | { |
| | 0 | 510 | | var stream = TargetAudioStream; |
| | 0 | 511 | | return AudioBitrate.HasValue && !IsDirectStream |
| | 0 | 512 | | ? AudioBitrate |
| | 0 | 513 | | : stream?.BitRate; |
| | | 514 | | } |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | /// <summary> |
| | | 518 | | /// Gets the amount of audio channels that will be in the output stream. |
| | | 519 | | /// </summary> |
| | | 520 | | /// <value>The target audio channels.</value> |
| | | 521 | | public int? TargetAudioChannels |
| | | 522 | | { |
| | | 523 | | get |
| | | 524 | | { |
| | 0 | 525 | | if (IsDirectStream) |
| | | 526 | | { |
| | 0 | 527 | | return TargetAudioStream?.Channels; |
| | | 528 | | } |
| | | 529 | | |
| | 0 | 530 | | var targetAudioCodecs = TargetAudioCodec; |
| | 0 | 531 | | var codec = targetAudioCodecs.Count == 0 ? null : targetAudioCodecs[0]; |
| | 0 | 532 | | if (!string.IsNullOrEmpty(codec)) |
| | | 533 | | { |
| | 0 | 534 | | return GetTargetRefFrames(codec); |
| | | 535 | | } |
| | | 536 | | |
| | 0 | 537 | | return TargetAudioStream?.Channels; |
| | | 538 | | } |
| | | 539 | | } |
| | | 540 | | |
| | | 541 | | /// <summary> |
| | | 542 | | /// Gets the audio codec that will be in the output stream. |
| | | 543 | | /// </summary> |
| | | 544 | | /// <value>The audio codec.</value> |
| | | 545 | | public IReadOnlyList<string> TargetAudioCodec |
| | | 546 | | { |
| | | 547 | | get |
| | | 548 | | { |
| | 394 | 549 | | var stream = TargetAudioStream; |
| | | 550 | | |
| | 394 | 551 | | string? inputCodec = stream?.Codec; |
| | | 552 | | |
| | 394 | 553 | | if (IsDirectStream) |
| | | 554 | | { |
| | 248 | 555 | | return string.IsNullOrEmpty(inputCodec) ? [] : [inputCodec]; |
| | | 556 | | } |
| | | 557 | | |
| | 784 | 558 | | foreach (string codec in AudioCodecs) |
| | | 559 | | { |
| | 286 | 560 | | if (string.Equals(codec, inputCodec, StringComparison.OrdinalIgnoreCase)) |
| | | 561 | | { |
| | 80 | 562 | | return string.IsNullOrEmpty(codec) ? [] : [codec]; |
| | | 563 | | } |
| | | 564 | | } |
| | | 565 | | |
| | 66 | 566 | | return AudioCodecs; |
| | 80 | 567 | | } |
| | | 568 | | } |
| | | 569 | | |
| | | 570 | | /// <summary> |
| | | 571 | | /// Gets the video codec that will be in the output stream. |
| | | 572 | | /// </summary> |
| | | 573 | | /// <value>The target video codec.</value> |
| | | 574 | | public IReadOnlyList<string> TargetVideoCodec |
| | | 575 | | { |
| | | 576 | | get |
| | | 577 | | { |
| | 728 | 578 | | var stream = TargetVideoStream; |
| | | 579 | | |
| | 728 | 580 | | string? inputCodec = stream?.Codec; |
| | | 581 | | |
| | 728 | 582 | | if (IsDirectStream) |
| | | 583 | | { |
| | 248 | 584 | | return string.IsNullOrEmpty(inputCodec) ? [] : [inputCodec]; |
| | | 585 | | } |
| | | 586 | | |
| | 2280 | 587 | | foreach (string codec in VideoCodecs) |
| | | 588 | | { |
| | 900 | 589 | | if (string.Equals(codec, inputCodec, StringComparison.OrdinalIgnoreCase)) |
| | | 590 | | { |
| | 480 | 591 | | return string.IsNullOrEmpty(codec) ? [] : [codec]; |
| | | 592 | | } |
| | | 593 | | } |
| | | 594 | | |
| | 0 | 595 | | return VideoCodecs; |
| | 480 | 596 | | } |
| | | 597 | | } |
| | | 598 | | |
| | | 599 | | /// <summary> |
| | | 600 | | /// Gets the target size of the output stream. |
| | | 601 | | /// </summary> |
| | | 602 | | /// <value>The target size.</value> |
| | | 603 | | public long? TargetSize |
| | | 604 | | { |
| | | 605 | | get |
| | | 606 | | { |
| | 0 | 607 | | if (IsDirectStream) |
| | | 608 | | { |
| | 0 | 609 | | return MediaSource?.Size; |
| | | 610 | | } |
| | | 611 | | |
| | 0 | 612 | | if (RunTimeTicks.HasValue) |
| | | 613 | | { |
| | 0 | 614 | | int? totalBitrate = TargetTotalBitrate; |
| | | 615 | | |
| | 0 | 616 | | double totalSeconds = RunTimeTicks.Value; |
| | | 617 | | // Convert to ms |
| | 0 | 618 | | totalSeconds /= 10000; |
| | | 619 | | // Convert to seconds |
| | 0 | 620 | | totalSeconds /= 1000; |
| | | 621 | | |
| | 0 | 622 | | return totalBitrate.HasValue ? |
| | 0 | 623 | | Convert.ToInt64(totalBitrate.Value * totalSeconds) : |
| | 0 | 624 | | null; |
| | | 625 | | } |
| | | 626 | | |
| | 0 | 627 | | return null; |
| | | 628 | | } |
| | | 629 | | } |
| | | 630 | | |
| | | 631 | | /// <summary> |
| | | 632 | | /// Gets the target video bitrate of the output stream. |
| | | 633 | | /// </summary> |
| | | 634 | | /// <value>The video bitrate.</value> |
| | | 635 | | public int? TargetVideoBitrate |
| | | 636 | | { |
| | | 637 | | get |
| | | 638 | | { |
| | 0 | 639 | | var stream = TargetVideoStream; |
| | | 640 | | |
| | 0 | 641 | | return VideoBitrate.HasValue && !IsDirectStream |
| | 0 | 642 | | ? VideoBitrate |
| | 0 | 643 | | : stream?.BitRate; |
| | | 644 | | } |
| | | 645 | | } |
| | | 646 | | |
| | | 647 | | /// <summary> |
| | | 648 | | /// Gets the target timestamp of the output stream. |
| | | 649 | | /// </summary> |
| | | 650 | | /// <value>The target timestamp.</value> |
| | | 651 | | public TransportStreamTimestamp TargetTimestamp |
| | | 652 | | { |
| | | 653 | | get |
| | | 654 | | { |
| | 0 | 655 | | var defaultValue = string.Equals(Container, "m2ts", StringComparison.OrdinalIgnoreCase) |
| | 0 | 656 | | ? TransportStreamTimestamp.Valid |
| | 0 | 657 | | : TransportStreamTimestamp.None; |
| | | 658 | | |
| | 0 | 659 | | return !IsDirectStream |
| | 0 | 660 | | ? defaultValue |
| | 0 | 661 | | : MediaSource is null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None; |
| | | 662 | | } |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | /// <summary> |
| | | 666 | | /// Gets the target total bitrate of the output stream. |
| | | 667 | | /// </summary> |
| | | 668 | | /// <value>The target total bitrate.</value> |
| | 0 | 669 | | public int? TargetTotalBitrate => (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0); |
| | | 670 | | |
| | | 671 | | /// <summary> |
| | | 672 | | /// Gets a value indicating whether the output stream is anamorphic. |
| | | 673 | | /// </summary> |
| | | 674 | | public bool? IsTargetAnamorphic |
| | | 675 | | { |
| | | 676 | | get |
| | | 677 | | { |
| | 0 | 678 | | if (IsDirectStream) |
| | | 679 | | { |
| | 0 | 680 | | return TargetVideoStream?.IsAnamorphic; |
| | | 681 | | } |
| | | 682 | | |
| | 0 | 683 | | return false; |
| | | 684 | | } |
| | | 685 | | } |
| | | 686 | | |
| | | 687 | | /// <summary> |
| | | 688 | | /// Gets a value indicating whether the output stream is interlaced. |
| | | 689 | | /// </summary> |
| | | 690 | | public bool? IsTargetInterlaced |
| | | 691 | | { |
| | | 692 | | get |
| | | 693 | | { |
| | 0 | 694 | | if (IsDirectStream) |
| | | 695 | | { |
| | 0 | 696 | | return TargetVideoStream?.IsInterlaced; |
| | | 697 | | } |
| | | 698 | | |
| | 0 | 699 | | var targetVideoCodecs = TargetVideoCodec; |
| | 0 | 700 | | var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0]; |
| | 0 | 701 | | if (!string.IsNullOrEmpty(videoCodec)) |
| | | 702 | | { |
| | 0 | 703 | | if (string.Equals(GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase)) |
| | | 704 | | { |
| | 0 | 705 | | return false; |
| | | 706 | | } |
| | | 707 | | } |
| | | 708 | | |
| | 0 | 709 | | return TargetVideoStream?.IsInterlaced; |
| | | 710 | | } |
| | | 711 | | } |
| | | 712 | | |
| | | 713 | | /// <summary> |
| | | 714 | | /// Gets a value indicating whether the output stream is AVC. |
| | | 715 | | /// </summary> |
| | | 716 | | public bool? IsTargetAVC |
| | | 717 | | { |
| | | 718 | | get |
| | | 719 | | { |
| | 0 | 720 | | if (IsDirectStream) |
| | | 721 | | { |
| | 0 | 722 | | return TargetVideoStream?.IsAVC; |
| | | 723 | | } |
| | | 724 | | |
| | 0 | 725 | | return true; |
| | | 726 | | } |
| | | 727 | | } |
| | | 728 | | |
| | | 729 | | /// <summary> |
| | | 730 | | /// Gets the target width of the output stream. |
| | | 731 | | /// </summary> |
| | | 732 | | /// <value>The target width.</value> |
| | | 733 | | public int? TargetWidth |
| | | 734 | | { |
| | | 735 | | get |
| | | 736 | | { |
| | 0 | 737 | | var videoStream = TargetVideoStream; |
| | | 738 | | |
| | 0 | 739 | | if (videoStream is not null && videoStream.Width.HasValue && videoStream.Height.HasValue) |
| | | 740 | | { |
| | 0 | 741 | | ImageDimensions size = new ImageDimensions(videoStream.Width.Value, videoStream.Height.Value); |
| | | 742 | | |
| | 0 | 743 | | size = DrawingUtils.Resize(size, 0, 0, MaxWidth ?? 0, MaxHeight ?? 0); |
| | | 744 | | |
| | 0 | 745 | | return size.Width; |
| | | 746 | | } |
| | | 747 | | |
| | 0 | 748 | | return MaxWidth; |
| | | 749 | | } |
| | | 750 | | } |
| | | 751 | | |
| | | 752 | | /// <summary> |
| | | 753 | | /// Gets the target height of the output stream. |
| | | 754 | | /// </summary> |
| | | 755 | | /// <value>The target height.</value> |
| | | 756 | | public int? TargetHeight |
| | | 757 | | { |
| | | 758 | | get |
| | | 759 | | { |
| | 0 | 760 | | var videoStream = TargetVideoStream; |
| | | 761 | | |
| | 0 | 762 | | if (videoStream is not null && videoStream.Width.HasValue && videoStream.Height.HasValue) |
| | | 763 | | { |
| | 0 | 764 | | ImageDimensions size = new ImageDimensions(videoStream.Width.Value, videoStream.Height.Value); |
| | | 765 | | |
| | 0 | 766 | | size = DrawingUtils.Resize(size, 0, 0, MaxWidth ?? 0, MaxHeight ?? 0); |
| | | 767 | | |
| | 0 | 768 | | return size.Height; |
| | | 769 | | } |
| | | 770 | | |
| | 0 | 771 | | return MaxHeight; |
| | | 772 | | } |
| | | 773 | | } |
| | | 774 | | |
| | | 775 | | /// <summary> |
| | | 776 | | /// Gets the target video stream count of the output stream. |
| | | 777 | | /// </summary> |
| | | 778 | | /// <value>The target video stream count.</value> |
| | | 779 | | public int? TargetVideoStreamCount |
| | | 780 | | { |
| | | 781 | | get |
| | | 782 | | { |
| | 0 | 783 | | if (IsDirectStream) |
| | | 784 | | { |
| | 0 | 785 | | return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue); |
| | | 786 | | } |
| | | 787 | | |
| | 0 | 788 | | return GetMediaStreamCount(MediaStreamType.Video, 1); |
| | | 789 | | } |
| | | 790 | | } |
| | | 791 | | |
| | | 792 | | /// <summary> |
| | | 793 | | /// Gets the target audio stream count of the output stream. |
| | | 794 | | /// </summary> |
| | | 795 | | /// <value>The target audio stream count.</value> |
| | | 796 | | public int? TargetAudioStreamCount |
| | | 797 | | { |
| | | 798 | | get |
| | | 799 | | { |
| | 0 | 800 | | if (IsDirectStream) |
| | | 801 | | { |
| | 0 | 802 | | return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue); |
| | | 803 | | } |
| | | 804 | | |
| | 0 | 805 | | return GetMediaStreamCount(MediaStreamType.Audio, 1); |
| | | 806 | | } |
| | | 807 | | } |
| | | 808 | | |
| | | 809 | | /// <summary> |
| | | 810 | | /// Sets a stream option. |
| | | 811 | | /// </summary> |
| | | 812 | | /// <param name="qualifier">The qualifier.</param> |
| | | 813 | | /// <param name="name">The name.</param> |
| | | 814 | | /// <param name="value">The value.</param> |
| | | 815 | | public void SetOption(string? qualifier, string name, string value) |
| | | 816 | | { |
| | 1388 | 817 | | if (string.IsNullOrEmpty(qualifier)) |
| | | 818 | | { |
| | 0 | 819 | | SetOption(name, value); |
| | | 820 | | } |
| | | 821 | | else |
| | | 822 | | { |
| | 1388 | 823 | | SetOption(qualifier + "-" + name, value); |
| | | 824 | | } |
| | 1388 | 825 | | } |
| | | 826 | | |
| | | 827 | | /// <summary> |
| | | 828 | | /// Sets a stream option. |
| | | 829 | | /// </summary> |
| | | 830 | | /// <param name="name">The name.</param> |
| | | 831 | | /// <param name="value">The value.</param> |
| | | 832 | | public void SetOption(string name, string value) |
| | | 833 | | { |
| | 1388 | 834 | | StreamOptions[name] = value; |
| | 1388 | 835 | | } |
| | | 836 | | |
| | | 837 | | /// <summary> |
| | | 838 | | /// Gets a stream option. |
| | | 839 | | /// </summary> |
| | | 840 | | /// <param name="qualifier">The qualifier.</param> |
| | | 841 | | /// <param name="name">The name.</param> |
| | | 842 | | /// <returns>The value.</returns> |
| | | 843 | | public string? GetOption(string? qualifier, string name) |
| | | 844 | | { |
| | 1170 | 845 | | var value = GetOption(qualifier + "-" + name); |
| | | 846 | | |
| | 1170 | 847 | | if (string.IsNullOrEmpty(value)) |
| | | 848 | | { |
| | 722 | 849 | | value = GetOption(name); |
| | | 850 | | } |
| | | 851 | | |
| | 1170 | 852 | | return value; |
| | | 853 | | } |
| | | 854 | | |
| | | 855 | | /// <summary> |
| | | 856 | | /// Gets a stream option. |
| | | 857 | | /// </summary> |
| | | 858 | | /// <param name="name">The name.</param> |
| | | 859 | | /// <returns>The value.</returns> |
| | | 860 | | public string? GetOption(string name) |
| | | 861 | | { |
| | 1892 | 862 | | if (StreamOptions.TryGetValue(name, out var value)) |
| | | 863 | | { |
| | 448 | 864 | | return value; |
| | | 865 | | } |
| | | 866 | | |
| | 1444 | 867 | | return null; |
| | | 868 | | } |
| | | 869 | | |
| | | 870 | | /// <summary> |
| | | 871 | | /// Returns this output stream URL for this class. |
| | | 872 | | /// </summary> |
| | | 873 | | /// <param name="baseUrl">The base Url.</param> |
| | | 874 | | /// <param name="accessToken">The access Token.</param> |
| | | 875 | | /// <param name="query">Optional extra query.</param> |
| | | 876 | | /// <returns>A querystring representation of this object.</returns> |
| | | 877 | | public string ToUrl(string? baseUrl, string? accessToken, string? query) |
| | | 878 | | { |
| | 100559 | 879 | | var sb = new StringBuilder(); |
| | 100559 | 880 | | if (!string.IsNullOrEmpty(baseUrl)) |
| | | 881 | | { |
| | 100559 | 882 | | sb.Append(baseUrl.TrimEnd('/')); |
| | | 883 | | } |
| | | 884 | | |
| | 100559 | 885 | | if (MediaType == DlnaProfileType.Audio) |
| | | 886 | | { |
| | 100001 | 887 | | sb.Append("/audio/"); |
| | | 888 | | } |
| | | 889 | | else |
| | | 890 | | { |
| | 558 | 891 | | sb.Append("/videos/"); |
| | | 892 | | } |
| | | 893 | | |
| | 100559 | 894 | | sb.Append(ItemId); |
| | | 895 | | |
| | 100559 | 896 | | if (SubProtocol == MediaStreamProtocol.hls) |
| | | 897 | | { |
| | 264 | 898 | | sb.Append("/master.m3u8?"); |
| | | 899 | | } |
| | | 900 | | else |
| | | 901 | | { |
| | 100295 | 902 | | sb.Append("/stream"); |
| | | 903 | | |
| | 100295 | 904 | | if (!string.IsNullOrEmpty(Container)) |
| | | 905 | | { |
| | 100276 | 906 | | sb.Append('.'); |
| | 100276 | 907 | | sb.Append(Container); |
| | | 908 | | } |
| | | 909 | | |
| | 100295 | 910 | | sb.Append('?'); |
| | | 911 | | } |
| | | 912 | | |
| | 100559 | 913 | | if (!string.IsNullOrEmpty(DeviceProfileId)) |
| | | 914 | | { |
| | 100000 | 915 | | sb.Append("&DeviceProfileId="); |
| | 100000 | 916 | | sb.Append(DeviceProfileId); |
| | | 917 | | } |
| | | 918 | | |
| | 100559 | 919 | | if (!string.IsNullOrEmpty(DeviceId)) |
| | | 920 | | { |
| | 100278 | 921 | | sb.Append("&DeviceId="); |
| | 100278 | 922 | | sb.Append(DeviceId); |
| | | 923 | | } |
| | | 924 | | |
| | 100559 | 925 | | if (!string.IsNullOrEmpty(MediaSourceId)) |
| | | 926 | | { |
| | 556 | 927 | | sb.Append("&MediaSourceId="); |
| | 556 | 928 | | sb.Append(MediaSourceId); |
| | | 929 | | } |
| | | 930 | | |
| | | 931 | | // default true so don't store. |
| | 100559 | 932 | | if (IsDirectStream) |
| | | 933 | | { |
| | 100248 | 934 | | sb.Append("&Static=true"); |
| | | 935 | | } |
| | | 936 | | |
| | 100559 | 937 | | if (VideoCodecs.Count != 0) |
| | | 938 | | { |
| | 540 | 939 | | sb.Append("&VideoCodec="); |
| | 540 | 940 | | sb.AppendJoin(',', VideoCodecs); |
| | | 941 | | } |
| | | 942 | | |
| | 100559 | 943 | | if (AudioCodecs.Count != 0) |
| | | 944 | | { |
| | 540 | 945 | | sb.Append("&AudioCodec="); |
| | 540 | 946 | | sb.AppendJoin(',', AudioCodecs); |
| | | 947 | | } |
| | | 948 | | |
| | 100559 | 949 | | if (AudioStreamIndex.HasValue) |
| | | 950 | | { |
| | 554 | 951 | | sb.Append("&AudioStreamIndex="); |
| | 554 | 952 | | sb.Append(AudioStreamIndex.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 953 | | } |
| | | 954 | | |
| | 100559 | 955 | | if (SubtitleStreamIndex.HasValue && (AlwaysBurnInSubtitleWhenTranscoding || SubtitleDeliveryMethod != SubtitleDe |
| | | 956 | | { |
| | 16 | 957 | | sb.Append("&SubtitleStreamIndex="); |
| | 16 | 958 | | sb.Append(SubtitleStreamIndex.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 959 | | } |
| | | 960 | | |
| | 100559 | 961 | | if (VideoBitrate.HasValue) |
| | | 962 | | { |
| | 292 | 963 | | sb.Append("&VideoBitrate="); |
| | 292 | 964 | | sb.Append(VideoBitrate.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 965 | | } |
| | | 966 | | |
| | 100559 | 967 | | if (AudioBitrate.HasValue) |
| | | 968 | | { |
| | 292 | 969 | | sb.Append("&AudioBitrate="); |
| | 292 | 970 | | sb.Append(AudioBitrate.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 971 | | } |
| | | 972 | | |
| | 100559 | 973 | | if (AudioSampleRate.HasValue) |
| | | 974 | | { |
| | 110 | 975 | | sb.Append("&AudioSampleRate="); |
| | 110 | 976 | | sb.Append(AudioSampleRate.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 977 | | } |
| | | 978 | | |
| | 100559 | 979 | | if (MaxFramerate.HasValue) |
| | | 980 | | { |
| | 290 | 981 | | sb.Append("&MaxFramerate="); |
| | 290 | 982 | | sb.Append(MaxFramerate.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 983 | | } |
| | | 984 | | |
| | 100559 | 985 | | if (MaxWidth.HasValue) |
| | | 986 | | { |
| | 8 | 987 | | sb.Append("&MaxWidth="); |
| | 8 | 988 | | sb.Append(MaxWidth.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 989 | | } |
| | | 990 | | |
| | 100559 | 991 | | if (MaxHeight.HasValue) |
| | | 992 | | { |
| | 0 | 993 | | sb.Append("&MaxHeight="); |
| | 0 | 994 | | sb.Append(MaxHeight.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 995 | | } |
| | | 996 | | |
| | 100559 | 997 | | if (SubProtocol == MediaStreamProtocol.hls) |
| | | 998 | | { |
| | 264 | 999 | | if (!string.IsNullOrEmpty(Container)) |
| | | 1000 | | { |
| | 264 | 1001 | | sb.Append("&SegmentContainer="); |
| | 264 | 1002 | | sb.Append(Container); |
| | | 1003 | | } |
| | | 1004 | | |
| | 264 | 1005 | | if (SegmentLength.HasValue) |
| | | 1006 | | { |
| | 0 | 1007 | | sb.Append("&SegmentLength="); |
| | 0 | 1008 | | sb.Append(SegmentLength.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 1009 | | } |
| | | 1010 | | |
| | 264 | 1011 | | if (MinSegments.HasValue) |
| | | 1012 | | { |
| | 204 | 1013 | | sb.Append("&MinSegments="); |
| | 204 | 1014 | | sb.Append(MinSegments.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 1015 | | } |
| | | 1016 | | } |
| | | 1017 | | else |
| | | 1018 | | { |
| | 100295 | 1019 | | if (StartPositionTicks != 0) |
| | | 1020 | | { |
| | 100000 | 1021 | | sb.Append("&StartTimeTicks="); |
| | 100000 | 1022 | | sb.Append(StartPositionTicks.ToString(CultureInfo.InvariantCulture)); |
| | | 1023 | | } |
| | | 1024 | | } |
| | | 1025 | | |
| | 100559 | 1026 | | if (!string.IsNullOrEmpty(PlaySessionId)) |
| | | 1027 | | { |
| | 100000 | 1028 | | sb.Append("&PlaySessionId="); |
| | 100000 | 1029 | | sb.Append(PlaySessionId); |
| | | 1030 | | } |
| | | 1031 | | |
| | 100559 | 1032 | | if (!string.IsNullOrEmpty(accessToken)) |
| | | 1033 | | { |
| | 100559 | 1034 | | sb.Append("&ApiKey="); |
| | 100559 | 1035 | | sb.Append(accessToken); |
| | | 1036 | | } |
| | | 1037 | | |
| | 100559 | 1038 | | var liveStreamId = MediaSource?.LiveStreamId; |
| | 100559 | 1039 | | if (!string.IsNullOrEmpty(liveStreamId)) |
| | | 1040 | | { |
| | 0 | 1041 | | sb.Append("&LiveStreamId="); |
| | 0 | 1042 | | sb.Append(liveStreamId); |
| | | 1043 | | } |
| | | 1044 | | |
| | 100559 | 1045 | | if (!IsDirectStream) |
| | | 1046 | | { |
| | 311 | 1047 | | if (RequireNonAnamorphic) |
| | | 1048 | | { |
| | 0 | 1049 | | sb.Append("&RequireNonAnamorphic="); |
| | 0 | 1050 | | sb.Append(RequireNonAnamorphic.ToString(CultureInfo.InvariantCulture)); |
| | | 1051 | | } |
| | | 1052 | | |
| | 311 | 1053 | | if (TranscodingMaxAudioChannels.HasValue) |
| | | 1054 | | { |
| | 266 | 1055 | | sb.Append("&TranscodingMaxAudioChannels="); |
| | 266 | 1056 | | sb.Append(TranscodingMaxAudioChannels.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 1057 | | } |
| | | 1058 | | |
| | 311 | 1059 | | if (EnableSubtitlesInManifest) |
| | | 1060 | | { |
| | 0 | 1061 | | sb.Append("&EnableSubtitlesInManifest="); |
| | 0 | 1062 | | sb.Append(EnableSubtitlesInManifest.ToString(CultureInfo.InvariantCulture)); |
| | | 1063 | | } |
| | | 1064 | | |
| | 311 | 1065 | | if (EnableMpegtsM2TsMode) |
| | | 1066 | | { |
| | 0 | 1067 | | sb.Append("&EnableMpegtsM2TsMode="); |
| | 0 | 1068 | | sb.Append(EnableMpegtsM2TsMode.ToString(CultureInfo.InvariantCulture)); |
| | | 1069 | | } |
| | | 1070 | | |
| | 311 | 1071 | | if (EstimateContentLength) |
| | | 1072 | | { |
| | 0 | 1073 | | sb.Append("&EstimateContentLength="); |
| | 0 | 1074 | | sb.Append(EstimateContentLength.ToString(CultureInfo.InvariantCulture)); |
| | | 1075 | | } |
| | | 1076 | | |
| | 311 | 1077 | | if (TranscodeSeekInfo != TranscodeSeekInfo.Auto) |
| | | 1078 | | { |
| | 0 | 1079 | | sb.Append("&TranscodeSeekInfo="); |
| | 0 | 1080 | | sb.Append(TranscodeSeekInfo.ToString()); |
| | | 1081 | | } |
| | | 1082 | | |
| | 311 | 1083 | | if (CopyTimestamps) |
| | | 1084 | | { |
| | 24 | 1085 | | sb.Append("&CopyTimestamps="); |
| | 24 | 1086 | | sb.Append(CopyTimestamps.ToString(CultureInfo.InvariantCulture)); |
| | | 1087 | | } |
| | | 1088 | | |
| | 311 | 1089 | | sb.Append("&RequireAvc="); |
| | 311 | 1090 | | sb.Append(RequireAvc.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()); |
| | | 1091 | | |
| | 311 | 1092 | | sb.Append("&EnableAudioVbrEncoding="); |
| | 311 | 1093 | | sb.Append(EnableAudioVbrEncoding.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()); |
| | | 1094 | | } |
| | | 1095 | | |
| | 100559 | 1096 | | var etag = MediaSource?.ETag; |
| | 100559 | 1097 | | if (!string.IsNullOrEmpty(etag)) |
| | | 1098 | | { |
| | 556 | 1099 | | sb.Append("&Tag="); |
| | 556 | 1100 | | sb.Append(etag); |
| | | 1101 | | } |
| | | 1102 | | |
| | 100559 | 1103 | | if (SubtitleStreamIndex.HasValue && SubtitleDeliveryMethod != SubtitleDeliveryMethod.External) |
| | | 1104 | | { |
| | 52 | 1105 | | sb.Append("&SubtitleMethod="); |
| | 52 | 1106 | | sb.Append(SubtitleDeliveryMethod); |
| | | 1107 | | } |
| | | 1108 | | |
| | 100559 | 1109 | | if (SubtitleStreamIndex.HasValue && SubtitleDeliveryMethod == SubtitleDeliveryMethod.Embed && SubtitleCodecs.Cou |
| | | 1110 | | { |
| | 0 | 1111 | | sb.Append("&SubtitleCodec="); |
| | 0 | 1112 | | sb.AppendJoin(',', SubtitleCodecs); |
| | | 1113 | | } |
| | | 1114 | | |
| | 206030 | 1115 | | foreach (var pair in StreamOptions) |
| | | 1116 | | { |
| | | 1117 | | // Strip spaces to avoid having to encode h264 profile names |
| | 2456 | 1118 | | sb.Append('&'); |
| | 2456 | 1119 | | sb.Append(pair.Key); |
| | 2456 | 1120 | | sb.Append('='); |
| | 2456 | 1121 | | sb.Append(pair.Value.Replace(" ", string.Empty, StringComparison.Ordinal)); |
| | | 1122 | | } |
| | | 1123 | | |
| | 100559 | 1124 | | var transcodeReasonsValues = TranscodeReasons.GetUniqueFlags().ToArray(); |
| | 100559 | 1125 | | if (!IsDirectStream && transcodeReasonsValues.Length > 0) |
| | | 1126 | | { |
| | 308 | 1127 | | sb.Append("&TranscodeReasons="); |
| | 308 | 1128 | | sb.AppendJoin(',', transcodeReasonsValues); |
| | | 1129 | | } |
| | | 1130 | | |
| | 100559 | 1131 | | if (!string.IsNullOrEmpty(query)) |
| | | 1132 | | { |
| | 0 | 1133 | | sb.Append(query); |
| | | 1134 | | } |
| | | 1135 | | |
| | 100559 | 1136 | | return sb.ToString(); |
| | | 1137 | | } |
| | | 1138 | | |
| | | 1139 | | /// <summary> |
| | | 1140 | | /// Gets the subtitle profiles. |
| | | 1141 | | /// </summary> |
| | | 1142 | | /// <param name="transcoderSupport">The transcoder support.</param> |
| | | 1143 | | /// <param name="includeSelectedTrackOnly">If only the selected track should be included.</param> |
| | | 1144 | | /// <param name="baseUrl">The base URL.</param> |
| | | 1145 | | /// <param name="accessToken">The access token.</param> |
| | | 1146 | | /// <returns>The <see cref="SubtitleStreamInfo"/> of the profiles.</returns> |
| | | 1147 | | public IEnumerable<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelecte |
| | | 1148 | | { |
| | 0 | 1149 | | return GetSubtitleProfiles(transcoderSupport, includeSelectedTrackOnly, false, baseUrl, accessToken); |
| | | 1150 | | } |
| | | 1151 | | |
| | | 1152 | | /// <summary> |
| | | 1153 | | /// Gets the subtitle profiles. |
| | | 1154 | | /// </summary> |
| | | 1155 | | /// <param name="transcoderSupport">The transcoder support.</param> |
| | | 1156 | | /// <param name="includeSelectedTrackOnly">If only the selected track should be included.</param> |
| | | 1157 | | /// <param name="enableAllProfiles">If all profiles are enabled.</param> |
| | | 1158 | | /// <param name="baseUrl">The base URL.</param> |
| | | 1159 | | /// <param name="accessToken">The access token.</param> |
| | | 1160 | | /// <returns>The <see cref="SubtitleStreamInfo"/> of the profiles.</returns> |
| | | 1161 | | public IEnumerable<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelecte |
| | | 1162 | | { |
| | 0 | 1163 | | if (MediaSource is null) |
| | | 1164 | | { |
| | 0 | 1165 | | return []; |
| | | 1166 | | } |
| | | 1167 | | |
| | 0 | 1168 | | List<SubtitleStreamInfo> list = []; |
| | | 1169 | | |
| | | 1170 | | // HLS will preserve timestamps so we can just grab the full subtitle stream |
| | 0 | 1171 | | long startPositionTicks = SubProtocol == MediaStreamProtocol.hls |
| | 0 | 1172 | | ? 0 |
| | 0 | 1173 | | : (PlayMethod == PlayMethod.Transcode && !CopyTimestamps ? StartPositionTicks : 0); |
| | | 1174 | | |
| | | 1175 | | // First add the selected track |
| | 0 | 1176 | | if (SubtitleStreamIndex.HasValue) |
| | | 1177 | | { |
| | 0 | 1178 | | foreach (var stream in MediaSource.MediaStreams) |
| | | 1179 | | { |
| | 0 | 1180 | | if (stream.Type == MediaStreamType.Subtitle && stream.Index == SubtitleStreamIndex.Value) |
| | | 1181 | | { |
| | 0 | 1182 | | AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startP |
| | | 1183 | | } |
| | | 1184 | | } |
| | | 1185 | | } |
| | | 1186 | | |
| | 0 | 1187 | | if (!includeSelectedTrackOnly) |
| | | 1188 | | { |
| | 0 | 1189 | | foreach (var stream in MediaSource.MediaStreams) |
| | | 1190 | | { |
| | 0 | 1191 | | if (stream.Type == MediaStreamType.Subtitle && (!SubtitleStreamIndex.HasValue || stream.Index != Subtitl |
| | | 1192 | | { |
| | 0 | 1193 | | AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startP |
| | | 1194 | | } |
| | | 1195 | | } |
| | | 1196 | | } |
| | | 1197 | | |
| | 0 | 1198 | | return list; |
| | | 1199 | | } |
| | | 1200 | | |
| | | 1201 | | private void AddSubtitleProfiles(List<SubtitleStreamInfo> list, MediaStream stream, ITranscoderSupport transcoderSup |
| | | 1202 | | { |
| | 0 | 1203 | | if (enableAllProfiles) |
| | | 1204 | | { |
| | 0 | 1205 | | foreach (var profile in DeviceProfile.SubtitleProfiles) |
| | | 1206 | | { |
| | 0 | 1207 | | var info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, new[] { profile }, tr |
| | 0 | 1208 | | if (info is not null) |
| | | 1209 | | { |
| | 0 | 1210 | | list.Add(info); |
| | | 1211 | | } |
| | | 1212 | | } |
| | | 1213 | | } |
| | | 1214 | | else |
| | | 1215 | | { |
| | 0 | 1216 | | var info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, DeviceProfile.SubtitlePro |
| | 0 | 1217 | | if (info is not null) |
| | | 1218 | | { |
| | 0 | 1219 | | list.Add(info); |
| | | 1220 | | } |
| | | 1221 | | } |
| | 0 | 1222 | | } |
| | | 1223 | | |
| | | 1224 | | private SubtitleStreamInfo? GetSubtitleStreamInfo(MediaStream stream, string baseUrl, string? accessToken, long star |
| | | 1225 | | { |
| | 0 | 1226 | | if (MediaSource is null) |
| | | 1227 | | { |
| | 0 | 1228 | | return null; |
| | | 1229 | | } |
| | | 1230 | | |
| | 0 | 1231 | | var subtitleProfile = StreamBuilder.GetSubtitleProfile(MediaSource, stream, subtitleProfiles, PlayMethod, transc |
| | 0 | 1232 | | var info = new SubtitleStreamInfo |
| | 0 | 1233 | | { |
| | 0 | 1234 | | IsForced = stream.IsForced, |
| | 0 | 1235 | | Language = stream.Language, |
| | 0 | 1236 | | Name = stream.Language ?? "Unknown", |
| | 0 | 1237 | | Format = subtitleProfile.Format, |
| | 0 | 1238 | | Index = stream.Index, |
| | 0 | 1239 | | DeliveryMethod = subtitleProfile.Method, |
| | 0 | 1240 | | DisplayTitle = stream.DisplayTitle |
| | 0 | 1241 | | }; |
| | | 1242 | | |
| | 0 | 1243 | | if (info.DeliveryMethod == SubtitleDeliveryMethod.External) |
| | | 1244 | | { |
| | | 1245 | | // Default to using the API URL |
| | 0 | 1246 | | info.Url = string.Format( |
| | 0 | 1247 | | CultureInfo.InvariantCulture, |
| | 0 | 1248 | | "{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}", |
| | 0 | 1249 | | baseUrl, |
| | 0 | 1250 | | ItemId, |
| | 0 | 1251 | | MediaSourceId, |
| | 0 | 1252 | | stream.Index.ToString(CultureInfo.InvariantCulture), |
| | 0 | 1253 | | startPositionTicks.ToString(CultureInfo.InvariantCulture), |
| | 0 | 1254 | | subtitleProfile.Format); |
| | 0 | 1255 | | info.IsExternalUrl = false; |
| | | 1256 | | |
| | | 1257 | | // Check conditions for potentially using the direct path |
| | 0 | 1258 | | if (stream.IsExternal // Must be external |
| | 0 | 1259 | | && stream.SupportsExternalStream |
| | 0 | 1260 | | && string.Equals(stream.Codec, subtitleProfile.Format, StringComparison.OrdinalIgnoreCase) // Format mus |
| | 0 | 1261 | | && !string.IsNullOrEmpty(stream.Path) // Path must exist |
| | 0 | 1262 | | && Uri.TryCreate(stream.Path, UriKind.Absolute, out Uri? uriResult) // Path must be an absolute URI |
| | 0 | 1263 | | && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)) // Scheme must be |
| | | 1264 | | { |
| | | 1265 | | // All conditions met, override with the direct path |
| | 0 | 1266 | | info.Url = stream.Path; |
| | 0 | 1267 | | info.IsExternalUrl = true; |
| | | 1268 | | } |
| | | 1269 | | |
| | | 1270 | | // Append ApiKey only if we are using the API URL |
| | 0 | 1271 | | if (!info.IsExternalUrl && !string.IsNullOrEmpty(accessToken)) |
| | | 1272 | | { |
| | | 1273 | | // Use "?ApiKey=" as seen in HEAD and other parts of the code |
| | 0 | 1274 | | info.Url += "?ApiKey=" + accessToken; |
| | | 1275 | | } |
| | | 1276 | | } |
| | | 1277 | | |
| | 0 | 1278 | | return info; |
| | | 1279 | | } |
| | | 1280 | | |
| | | 1281 | | /// <summary> |
| | | 1282 | | /// Gets the target video bit depth. |
| | | 1283 | | /// </summary> |
| | | 1284 | | /// <param name="codec">The codec.</param> |
| | | 1285 | | /// <returns>The target video bit depth.</returns> |
| | | 1286 | | public int? GetTargetVideoBitDepth(string? codec) |
| | | 1287 | | { |
| | 96 | 1288 | | var value = GetOption(codec, "videobitdepth"); |
| | | 1289 | | |
| | 96 | 1290 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var result)) |
| | | 1291 | | { |
| | 96 | 1292 | | return result; |
| | | 1293 | | } |
| | | 1294 | | |
| | 0 | 1295 | | return null; |
| | | 1296 | | } |
| | | 1297 | | |
| | | 1298 | | /// <summary> |
| | | 1299 | | /// Gets the target audio bit depth. |
| | | 1300 | | /// </summary> |
| | | 1301 | | /// <param name="codec">The codec.</param> |
| | | 1302 | | /// <returns>The target audio bit depth.</returns> |
| | | 1303 | | public int? GetTargetAudioBitDepth(string? codec) |
| | | 1304 | | { |
| | 0 | 1305 | | var value = GetOption(codec, "audiobitdepth"); |
| | | 1306 | | |
| | 0 | 1307 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var result)) |
| | | 1308 | | { |
| | 0 | 1309 | | return result; |
| | | 1310 | | } |
| | | 1311 | | |
| | 0 | 1312 | | return null; |
| | | 1313 | | } |
| | | 1314 | | |
| | | 1315 | | /// <summary> |
| | | 1316 | | /// Gets the target video level. |
| | | 1317 | | /// </summary> |
| | | 1318 | | /// <param name="codec">The codec.</param> |
| | | 1319 | | /// <returns>The target video level.</returns> |
| | | 1320 | | public double? GetTargetVideoLevel(string? codec) |
| | | 1321 | | { |
| | 308 | 1322 | | var value = GetOption(codec, "level"); |
| | | 1323 | | |
| | 308 | 1324 | | if (double.TryParse(value, CultureInfo.InvariantCulture, out var result)) |
| | | 1325 | | { |
| | 170 | 1326 | | return result; |
| | | 1327 | | } |
| | | 1328 | | |
| | 138 | 1329 | | return null; |
| | | 1330 | | } |
| | | 1331 | | |
| | | 1332 | | /// <summary> |
| | | 1333 | | /// Gets the target reference frames. |
| | | 1334 | | /// </summary> |
| | | 1335 | | /// <param name="codec">The codec.</param> |
| | | 1336 | | /// <returns>The target reference frames.</returns> |
| | | 1337 | | public int? GetTargetRefFrames(string? codec) |
| | | 1338 | | { |
| | 2 | 1339 | | var value = GetOption(codec, "maxrefframes"); |
| | | 1340 | | |
| | 2 | 1341 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var result)) |
| | | 1342 | | { |
| | 0 | 1343 | | return result; |
| | | 1344 | | } |
| | | 1345 | | |
| | 2 | 1346 | | return null; |
| | | 1347 | | } |
| | | 1348 | | |
| | | 1349 | | /// <summary> |
| | | 1350 | | /// Gets the target audio channels. |
| | | 1351 | | /// </summary> |
| | | 1352 | | /// <param name="codec">The codec.</param> |
| | | 1353 | | /// <returns>The target audio channels.</returns> |
| | | 1354 | | public int? GetTargetAudioChannels(string? codec) |
| | | 1355 | | { |
| | 174 | 1356 | | var defaultValue = GlobalMaxAudioChannels ?? TranscodingMaxAudioChannels; |
| | | 1357 | | |
| | 174 | 1358 | | var value = GetOption(codec, "audiochannels"); |
| | 174 | 1359 | | if (string.IsNullOrEmpty(value)) |
| | | 1360 | | { |
| | 174 | 1361 | | return defaultValue; |
| | | 1362 | | } |
| | | 1363 | | |
| | 0 | 1364 | | if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result)) |
| | | 1365 | | { |
| | 0 | 1366 | | return Math.Min(result, defaultValue ?? result); |
| | | 1367 | | } |
| | | 1368 | | |
| | 0 | 1369 | | return defaultValue; |
| | | 1370 | | } |
| | | 1371 | | |
| | | 1372 | | /// <summary> |
| | | 1373 | | /// Gets the media stream count. |
| | | 1374 | | /// </summary> |
| | | 1375 | | /// <param name="type">The type.</param> |
| | | 1376 | | /// <param name="limit">The limit.</param> |
| | | 1377 | | /// <returns>The media stream count.</returns> |
| | | 1378 | | private int? GetMediaStreamCount(MediaStreamType type, int limit) |
| | | 1379 | | { |
| | 0 | 1380 | | var count = MediaSource?.GetStreamCount(type); |
| | | 1381 | | |
| | 0 | 1382 | | if (count.HasValue) |
| | | 1383 | | { |
| | 0 | 1384 | | count = Math.Min(count.Value, limit); |
| | | 1385 | | } |
| | | 1386 | | |
| | 0 | 1387 | | return count; |
| | | 1388 | | } |
| | | 1389 | | } |