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