StreamInfo.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using MediaBrowser.Model.Drawing;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.MediaInfo;
  11. using MediaBrowser.Model.Session;
  12. namespace MediaBrowser.Model.Dlna
  13. {
  14. /// <summary>
  15. /// Class StreamInfo.
  16. /// </summary>
  17. public class StreamInfo
  18. {
  19. public StreamInfo()
  20. {
  21. AudioCodecs = Array.Empty<string>();
  22. VideoCodecs = Array.Empty<string>();
  23. SubtitleCodecs = Array.Empty<string>();
  24. TranscodeReasons = Array.Empty<TranscodeReason>();
  25. StreamOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  26. }
  27. public Guid ItemId { get; set; }
  28. public PlayMethod PlayMethod { get; set; }
  29. public EncodingContext Context { get; set; }
  30. public DlnaProfileType MediaType { get; set; }
  31. public string Container { get; set; }
  32. public string SubProtocol { get; set; }
  33. public long StartPositionTicks { get; set; }
  34. public int? SegmentLength { get; set; }
  35. public int? MinSegments { get; set; }
  36. public bool BreakOnNonKeyFrames { get; set; }
  37. public bool RequireAvc { get; set; }
  38. public bool RequireNonAnamorphic { get; set; }
  39. public bool CopyTimestamps { get; set; }
  40. public bool EnableMpegtsM2TsMode { get; set; }
  41. public bool EnableSubtitlesInManifest { get; set; }
  42. public string[] AudioCodecs { get; set; }
  43. public string[] VideoCodecs { get; set; }
  44. public int? AudioStreamIndex { get; set; }
  45. public int? SubtitleStreamIndex { get; set; }
  46. public int? TranscodingMaxAudioChannels { get; set; }
  47. public int? GlobalMaxAudioChannels { get; set; }
  48. public int? AudioBitrate { get; set; }
  49. public int? AudioSampleRate { get; set; }
  50. public int? VideoBitrate { get; set; }
  51. public int? MaxWidth { get; set; }
  52. public int? MaxHeight { get; set; }
  53. public float? MaxFramerate { get; set; }
  54. public DeviceProfile DeviceProfile { get; set; }
  55. public string DeviceProfileId { get; set; }
  56. public string DeviceId { get; set; }
  57. public long? RunTimeTicks { get; set; }
  58. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  59. public bool EstimateContentLength { get; set; }
  60. public MediaSourceInfo MediaSource { get; set; }
  61. public string[] SubtitleCodecs { get; set; }
  62. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  63. public string SubtitleFormat { get; set; }
  64. public string PlaySessionId { get; set; }
  65. public TranscodeReason[] TranscodeReasons { get; set; }
  66. public Dictionary<string, string> StreamOptions { get; private set; }
  67. public string MediaSourceId => MediaSource?.Id;
  68. public bool IsDirectStream =>
  69. PlayMethod == PlayMethod.DirectStream ||
  70. PlayMethod == PlayMethod.DirectPlay;
  71. /// <summary>
  72. /// Gets the audio stream that will be used.
  73. /// </summary>
  74. public MediaStream TargetAudioStream => MediaSource?.GetDefaultAudioStream(AudioStreamIndex);
  75. /// <summary>
  76. /// Gets the video stream that will be used.
  77. /// </summary>
  78. public MediaStream TargetVideoStream => MediaSource?.VideoStream;
  79. /// <summary>
  80. /// Gets the audio sample rate that will be in the output stream.
  81. /// </summary>
  82. public int? TargetAudioSampleRate
  83. {
  84. get
  85. {
  86. var stream = TargetAudioStream;
  87. return AudioSampleRate.HasValue && !IsDirectStream
  88. ? AudioSampleRate
  89. : stream == null ? null : stream.SampleRate;
  90. }
  91. }
  92. /// <summary>
  93. /// Gets the audio sample rate that will be in the output stream.
  94. /// </summary>
  95. public int? TargetAudioBitDepth
  96. {
  97. get
  98. {
  99. if (IsDirectStream)
  100. {
  101. return TargetAudioStream == null ? (int?)null : TargetAudioStream.BitDepth;
  102. }
  103. var targetAudioCodecs = TargetAudioCodec;
  104. var audioCodec = targetAudioCodecs.Length == 0 ? null : targetAudioCodecs[0];
  105. if (!string.IsNullOrEmpty(audioCodec))
  106. {
  107. return GetTargetAudioBitDepth(audioCodec);
  108. }
  109. return TargetAudioStream == null ? (int?)null : TargetAudioStream.BitDepth;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets the audio sample rate that will be in the output stream.
  114. /// </summary>
  115. public int? TargetVideoBitDepth
  116. {
  117. get
  118. {
  119. if (IsDirectStream)
  120. {
  121. return TargetVideoStream == null ? (int?)null : TargetVideoStream.BitDepth;
  122. }
  123. var targetVideoCodecs = TargetVideoCodec;
  124. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  125. if (!string.IsNullOrEmpty(videoCodec))
  126. {
  127. return GetTargetVideoBitDepth(videoCodec);
  128. }
  129. return TargetVideoStream == null ? (int?)null : TargetVideoStream.BitDepth;
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the target reference frames.
  134. /// </summary>
  135. /// <value>The target reference frames.</value>
  136. public int? TargetRefFrames
  137. {
  138. get
  139. {
  140. if (IsDirectStream)
  141. {
  142. return TargetVideoStream == null ? (int?)null : TargetVideoStream.RefFrames;
  143. }
  144. var targetVideoCodecs = TargetVideoCodec;
  145. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  146. if (!string.IsNullOrEmpty(videoCodec))
  147. {
  148. return GetTargetRefFrames(videoCodec);
  149. }
  150. return TargetVideoStream == null ? (int?)null : TargetVideoStream.RefFrames;
  151. }
  152. }
  153. /// <summary>
  154. /// Gets the audio sample rate that will be in the output stream.
  155. /// </summary>
  156. public float? TargetFramerate
  157. {
  158. get
  159. {
  160. var stream = TargetVideoStream;
  161. return MaxFramerate.HasValue && !IsDirectStream
  162. ? MaxFramerate
  163. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  164. }
  165. }
  166. /// <summary>
  167. /// Gets the audio sample rate that will be in the output stream.
  168. /// </summary>
  169. public double? TargetVideoLevel
  170. {
  171. get
  172. {
  173. if (IsDirectStream)
  174. {
  175. return TargetVideoStream == null ? (double?)null : TargetVideoStream.Level;
  176. }
  177. var targetVideoCodecs = TargetVideoCodec;
  178. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  179. if (!string.IsNullOrEmpty(videoCodec))
  180. {
  181. return GetTargetVideoLevel(videoCodec);
  182. }
  183. return TargetVideoStream == null ? (double?)null : TargetVideoStream.Level;
  184. }
  185. }
  186. /// <summary>
  187. /// Gets the audio sample rate that will be in the output stream.
  188. /// </summary>
  189. public int? TargetPacketLength
  190. {
  191. get
  192. {
  193. var stream = TargetVideoStream;
  194. return !IsDirectStream
  195. ? null
  196. : stream == null ? null : stream.PacketLength;
  197. }
  198. }
  199. /// <summary>
  200. /// Gets the audio sample rate that will be in the output stream.
  201. /// </summary>
  202. public string TargetVideoProfile
  203. {
  204. get
  205. {
  206. if (IsDirectStream)
  207. {
  208. return TargetVideoStream == null ? null : TargetVideoStream.Profile;
  209. }
  210. var targetVideoCodecs = TargetVideoCodec;
  211. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  212. if (!string.IsNullOrEmpty(videoCodec))
  213. {
  214. return GetOption(videoCodec, "profile");
  215. }
  216. return TargetVideoStream == null ? null : TargetVideoStream.Profile;
  217. }
  218. }
  219. /// <summary>
  220. /// Gets the target video codec tag.
  221. /// </summary>
  222. /// <value>The target video codec tag.</value>
  223. public string TargetVideoCodecTag
  224. {
  225. get
  226. {
  227. var stream = TargetVideoStream;
  228. return !IsDirectStream
  229. ? null
  230. : stream == null ? null : stream.CodecTag;
  231. }
  232. }
  233. /// <summary>
  234. /// Gets the audio bitrate that will be in the output stream.
  235. /// </summary>
  236. public int? TargetAudioBitrate
  237. {
  238. get
  239. {
  240. var stream = TargetAudioStream;
  241. return AudioBitrate.HasValue && !IsDirectStream
  242. ? AudioBitrate
  243. : stream == null ? null : stream.BitRate;
  244. }
  245. }
  246. /// <summary>
  247. /// Gets the audio channels that will be in the output stream.
  248. /// </summary>
  249. public int? TargetAudioChannels
  250. {
  251. get
  252. {
  253. if (IsDirectStream)
  254. {
  255. return TargetAudioStream == null ? (int?)null : TargetAudioStream.Channels;
  256. }
  257. var targetAudioCodecs = TargetAudioCodec;
  258. var codec = targetAudioCodecs.Length == 0 ? null : targetAudioCodecs[0];
  259. if (!string.IsNullOrEmpty(codec))
  260. {
  261. return GetTargetRefFrames(codec);
  262. }
  263. return TargetAudioStream == null ? (int?)null : TargetAudioStream.Channels;
  264. }
  265. }
  266. /// <summary>
  267. /// Gets the audio codec that will be in the output stream.
  268. /// </summary>
  269. public string[] TargetAudioCodec
  270. {
  271. get
  272. {
  273. var stream = TargetAudioStream;
  274. string inputCodec = stream?.Codec;
  275. if (IsDirectStream)
  276. {
  277. return string.IsNullOrEmpty(inputCodec) ? Array.Empty<string>() : new[] { inputCodec };
  278. }
  279. foreach (string codec in AudioCodecs)
  280. {
  281. if (string.Equals(codec, inputCodec, StringComparison.OrdinalIgnoreCase))
  282. {
  283. return string.IsNullOrEmpty(codec) ? Array.Empty<string>() : new[] { codec };
  284. }
  285. }
  286. return AudioCodecs;
  287. }
  288. }
  289. public string[] TargetVideoCodec
  290. {
  291. get
  292. {
  293. var stream = TargetVideoStream;
  294. string inputCodec = stream?.Codec;
  295. if (IsDirectStream)
  296. {
  297. return string.IsNullOrEmpty(inputCodec) ? Array.Empty<string>() : new[] { inputCodec };
  298. }
  299. foreach (string codec in VideoCodecs)
  300. {
  301. if (string.Equals(codec, inputCodec, StringComparison.OrdinalIgnoreCase))
  302. {
  303. return string.IsNullOrEmpty(codec) ? Array.Empty<string>() : new[] { codec };
  304. }
  305. }
  306. return VideoCodecs;
  307. }
  308. }
  309. /// <summary>
  310. /// Gets the audio channels that will be in the output stream.
  311. /// </summary>
  312. public long? TargetSize
  313. {
  314. get
  315. {
  316. if (IsDirectStream)
  317. {
  318. return MediaSource.Size;
  319. }
  320. if (RunTimeTicks.HasValue)
  321. {
  322. int? totalBitrate = TargetTotalBitrate;
  323. double totalSeconds = RunTimeTicks.Value;
  324. // Convert to ms
  325. totalSeconds /= 10000;
  326. // Convert to seconds
  327. totalSeconds /= 1000;
  328. return totalBitrate.HasValue ?
  329. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  330. (long?)null;
  331. }
  332. return null;
  333. }
  334. }
  335. public int? TargetVideoBitrate
  336. {
  337. get
  338. {
  339. var stream = TargetVideoStream;
  340. return VideoBitrate.HasValue && !IsDirectStream
  341. ? VideoBitrate
  342. : stream == null ? null : stream.BitRate;
  343. }
  344. }
  345. public TransportStreamTimestamp TargetTimestamp
  346. {
  347. get
  348. {
  349. var defaultValue = string.Equals(Container, "m2ts", StringComparison.OrdinalIgnoreCase)
  350. ? TransportStreamTimestamp.Valid
  351. : TransportStreamTimestamp.None;
  352. return !IsDirectStream
  353. ? defaultValue
  354. : MediaSource == null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  355. }
  356. }
  357. public int? TargetTotalBitrate => (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  358. public bool? IsTargetAnamorphic
  359. {
  360. get
  361. {
  362. if (IsDirectStream)
  363. {
  364. return TargetVideoStream == null ? null : TargetVideoStream.IsAnamorphic;
  365. }
  366. return false;
  367. }
  368. }
  369. public bool? IsTargetInterlaced
  370. {
  371. get
  372. {
  373. if (IsDirectStream)
  374. {
  375. return TargetVideoStream == null ? (bool?)null : TargetVideoStream.IsInterlaced;
  376. }
  377. var targetVideoCodecs = TargetVideoCodec;
  378. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  379. if (!string.IsNullOrEmpty(videoCodec))
  380. {
  381. if (string.Equals(GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase))
  382. {
  383. return false;
  384. }
  385. }
  386. return TargetVideoStream == null ? (bool?)null : TargetVideoStream.IsInterlaced;
  387. }
  388. }
  389. public bool? IsTargetAVC
  390. {
  391. get
  392. {
  393. if (IsDirectStream)
  394. {
  395. return TargetVideoStream == null ? null : TargetVideoStream.IsAVC;
  396. }
  397. return true;
  398. }
  399. }
  400. public int? TargetWidth
  401. {
  402. get
  403. {
  404. var videoStream = TargetVideoStream;
  405. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  406. {
  407. ImageDimensions size = new ImageDimensions(videoStream.Width.Value, videoStream.Height.Value);
  408. size = DrawingUtils.Resize(size, 0, 0, MaxWidth ?? 0, MaxHeight ?? 0);
  409. return size.Width;
  410. }
  411. return MaxWidth;
  412. }
  413. }
  414. public int? TargetHeight
  415. {
  416. get
  417. {
  418. var videoStream = TargetVideoStream;
  419. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  420. {
  421. ImageDimensions size = new ImageDimensions(videoStream.Width.Value, videoStream.Height.Value);
  422. size = DrawingUtils.Resize(size, 0, 0, MaxWidth ?? 0, MaxHeight ?? 0);
  423. return size.Height;
  424. }
  425. return MaxHeight;
  426. }
  427. }
  428. public int? TargetVideoStreamCount
  429. {
  430. get
  431. {
  432. if (IsDirectStream)
  433. {
  434. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  435. }
  436. return GetMediaStreamCount(MediaStreamType.Video, 1);
  437. }
  438. }
  439. public int? TargetAudioStreamCount
  440. {
  441. get
  442. {
  443. if (IsDirectStream)
  444. {
  445. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  446. }
  447. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  448. }
  449. }
  450. public void SetOption(string qualifier, string name, string value)
  451. {
  452. if (string.IsNullOrEmpty(qualifier))
  453. {
  454. SetOption(name, value);
  455. }
  456. else
  457. {
  458. SetOption(qualifier + "-" + name, value);
  459. }
  460. }
  461. public void SetOption(string name, string value)
  462. {
  463. StreamOptions[name] = value;
  464. }
  465. public string GetOption(string qualifier, string name)
  466. {
  467. var value = GetOption(qualifier + "-" + name);
  468. if (string.IsNullOrEmpty(value))
  469. {
  470. value = GetOption(name);
  471. }
  472. return value;
  473. }
  474. public string GetOption(string name)
  475. {
  476. if (StreamOptions.TryGetValue(name, out var value))
  477. {
  478. return value;
  479. }
  480. return null;
  481. }
  482. public string ToUrl(string baseUrl, string accessToken)
  483. {
  484. if (PlayMethod == PlayMethod.DirectPlay)
  485. {
  486. return MediaSource.Path;
  487. }
  488. if (string.IsNullOrEmpty(baseUrl))
  489. {
  490. throw new ArgumentNullException(nameof(baseUrl));
  491. }
  492. var list = new List<string>();
  493. foreach (NameValuePair pair in BuildParams(this, accessToken))
  494. {
  495. if (string.IsNullOrEmpty(pair.Value))
  496. {
  497. continue;
  498. }
  499. // Try to keep the url clean by omitting defaults
  500. if (string.Equals(pair.Name, "StartTimeTicks", StringComparison.OrdinalIgnoreCase) &&
  501. string.Equals(pair.Value, "0", StringComparison.OrdinalIgnoreCase))
  502. {
  503. continue;
  504. }
  505. if (string.Equals(pair.Name, "SubtitleStreamIndex", StringComparison.OrdinalIgnoreCase) &&
  506. string.Equals(pair.Value, "-1", StringComparison.OrdinalIgnoreCase))
  507. {
  508. continue;
  509. }
  510. if (string.Equals(pair.Name, "Static", StringComparison.OrdinalIgnoreCase) &&
  511. string.Equals(pair.Value, "false", StringComparison.OrdinalIgnoreCase))
  512. {
  513. continue;
  514. }
  515. var encodedValue = pair.Value.Replace(" ", "%20");
  516. list.Add(string.Format(CultureInfo.InvariantCulture, "{0}={1}", pair.Name, encodedValue));
  517. }
  518. string queryString = string.Join("&", list.ToArray());
  519. return GetUrl(baseUrl, queryString);
  520. }
  521. private string GetUrl(string baseUrl, string queryString)
  522. {
  523. if (string.IsNullOrEmpty(baseUrl))
  524. {
  525. throw new ArgumentNullException(nameof(baseUrl));
  526. }
  527. string extension = string.IsNullOrEmpty(Container) ? string.Empty : "." + Container;
  528. baseUrl = baseUrl.TrimEnd('/');
  529. if (MediaType == DlnaProfileType.Audio)
  530. {
  531. if (string.Equals(SubProtocol, "hls", StringComparison.OrdinalIgnoreCase))
  532. {
  533. return string.Format(CultureInfo.InvariantCulture, "{0}/audio/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  534. }
  535. return string.Format(CultureInfo.InvariantCulture, "{0}/audio/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  536. }
  537. if (string.Equals(SubProtocol, "hls", StringComparison.OrdinalIgnoreCase))
  538. {
  539. return string.Format(CultureInfo.InvariantCulture, "{0}/videos/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  540. }
  541. return string.Format(CultureInfo.InvariantCulture, "{0}/videos/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  542. }
  543. private static List<NameValuePair> BuildParams(StreamInfo item, string accessToken)
  544. {
  545. var list = new List<NameValuePair>();
  546. string audioCodecs = item.AudioCodecs.Length == 0 ?
  547. string.Empty :
  548. string.Join(",", item.AudioCodecs);
  549. string videoCodecs = item.VideoCodecs.Length == 0 ?
  550. string.Empty :
  551. string.Join(",", item.VideoCodecs);
  552. list.Add(new NameValuePair("DeviceProfileId", item.DeviceProfileId ?? string.Empty));
  553. list.Add(new NameValuePair("DeviceId", item.DeviceId ?? string.Empty));
  554. list.Add(new NameValuePair("MediaSourceId", item.MediaSourceId ?? string.Empty));
  555. list.Add(new NameValuePair("Static", item.IsDirectStream.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
  556. list.Add(new NameValuePair("VideoCodec", videoCodecs));
  557. list.Add(new NameValuePair("AudioCodec", audioCodecs));
  558. list.Add(new NameValuePair("AudioStreamIndex", item.AudioStreamIndex.HasValue ? item.AudioStreamIndex.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  559. list.Add(new NameValuePair("SubtitleStreamIndex", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? item.SubtitleStreamIndex.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  560. list.Add(new NameValuePair("VideoBitrate", item.VideoBitrate.HasValue ? item.VideoBitrate.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  561. list.Add(new NameValuePair("AudioBitrate", item.AudioBitrate.HasValue ? item.AudioBitrate.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  562. list.Add(new NameValuePair("AudioSampleRate", item.AudioSampleRate.HasValue ? item.AudioSampleRate.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  563. list.Add(new NameValuePair("MaxFramerate", item.MaxFramerate.HasValue ? item.MaxFramerate.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  564. list.Add(new NameValuePair("MaxWidth", item.MaxWidth.HasValue ? item.MaxWidth.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  565. list.Add(new NameValuePair("MaxHeight", item.MaxHeight.HasValue ? item.MaxHeight.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  566. long startPositionTicks = item.StartPositionTicks;
  567. var isHls = string.Equals(item.SubProtocol, "hls", StringComparison.OrdinalIgnoreCase);
  568. if (isHls)
  569. {
  570. list.Add(new NameValuePair("StartTimeTicks", string.Empty));
  571. }
  572. else
  573. {
  574. list.Add(new NameValuePair("StartTimeTicks", startPositionTicks.ToString(CultureInfo.InvariantCulture)));
  575. }
  576. list.Add(new NameValuePair("PlaySessionId", item.PlaySessionId ?? string.Empty));
  577. list.Add(new NameValuePair("api_key", accessToken ?? string.Empty));
  578. string liveStreamId = item.MediaSource?.LiveStreamId;
  579. list.Add(new NameValuePair("LiveStreamId", liveStreamId ?? string.Empty));
  580. list.Add(new NameValuePair("SubtitleMethod", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? item.SubtitleDeliveryMethod.ToString() : string.Empty));
  581. if (!item.IsDirectStream)
  582. {
  583. if (item.RequireNonAnamorphic)
  584. {
  585. list.Add(new NameValuePair("RequireNonAnamorphic", item.RequireNonAnamorphic.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
  586. }
  587. list.Add(new NameValuePair("TranscodingMaxAudioChannels", item.TranscodingMaxAudioChannels.HasValue ? item.TranscodingMaxAudioChannels.Value.ToString(CultureInfo.InvariantCulture) : string.Empty));
  588. if (item.EnableSubtitlesInManifest)
  589. {
  590. list.Add(new NameValuePair("EnableSubtitlesInManifest", item.EnableSubtitlesInManifest.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
  591. }
  592. if (item.EnableMpegtsM2TsMode)
  593. {
  594. list.Add(new NameValuePair("EnableMpegtsM2TsMode", item.EnableMpegtsM2TsMode.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
  595. }
  596. if (item.EstimateContentLength)
  597. {
  598. list.Add(new NameValuePair("EstimateContentLength", item.EstimateContentLength.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
  599. }
  600. if (item.TranscodeSeekInfo != TranscodeSeekInfo.Auto)
  601. {
  602. list.Add(new NameValuePair("TranscodeSeekInfo", item.TranscodeSeekInfo.ToString().ToLowerInvariant()));
  603. }
  604. if (item.CopyTimestamps)
  605. {
  606. list.Add(new NameValuePair("CopyTimestamps", item.CopyTimestamps.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
  607. }
  608. list.Add(new NameValuePair("RequireAvc", item.RequireAvc.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
  609. }
  610. list.Add(new NameValuePair("Tag", item.MediaSource.ETag ?? string.Empty));
  611. string subtitleCodecs = item.SubtitleCodecs.Length == 0 ?
  612. string.Empty :
  613. string.Join(",", item.SubtitleCodecs);
  614. list.Add(new NameValuePair("SubtitleCodec", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Embed ? subtitleCodecs : string.Empty));
  615. if (isHls)
  616. {
  617. list.Add(new NameValuePair("SegmentContainer", item.Container ?? string.Empty));
  618. if (item.SegmentLength.HasValue)
  619. {
  620. list.Add(new NameValuePair("SegmentLength", item.SegmentLength.Value.ToString(CultureInfo.InvariantCulture)));
  621. }
  622. if (item.MinSegments.HasValue)
  623. {
  624. list.Add(new NameValuePair("MinSegments", item.MinSegments.Value.ToString(CultureInfo.InvariantCulture)));
  625. }
  626. list.Add(new NameValuePair("BreakOnNonKeyFrames", item.BreakOnNonKeyFrames.ToString(CultureInfo.InvariantCulture)));
  627. }
  628. foreach (var pair in item.StreamOptions)
  629. {
  630. if (string.IsNullOrEmpty(pair.Value))
  631. {
  632. continue;
  633. }
  634. // strip spaces to avoid having to encode h264 profile names
  635. list.Add(new NameValuePair(pair.Key, pair.Value.Replace(" ", string.Empty)));
  636. }
  637. if (!item.IsDirectStream)
  638. {
  639. list.Add(new NameValuePair("TranscodeReasons", string.Join(',', item.TranscodeReasons.Distinct())));
  640. }
  641. return list;
  642. }
  643. public List<SubtitleStreamInfo> GetExternalSubtitles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  644. {
  645. return GetExternalSubtitles(transcoderSupport, includeSelectedTrackOnly, false, baseUrl, accessToken);
  646. }
  647. public List<SubtitleStreamInfo> GetExternalSubtitles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  648. {
  649. var list = GetSubtitleProfiles(transcoderSupport, includeSelectedTrackOnly, enableAllProfiles, baseUrl, accessToken);
  650. var newList = new List<SubtitleStreamInfo>();
  651. // First add the selected track
  652. foreach (SubtitleStreamInfo stream in list)
  653. {
  654. if (stream.DeliveryMethod == SubtitleDeliveryMethod.External)
  655. {
  656. newList.Add(stream);
  657. }
  658. }
  659. return newList;
  660. }
  661. public List<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  662. {
  663. return GetSubtitleProfiles(transcoderSupport, includeSelectedTrackOnly, false, baseUrl, accessToken);
  664. }
  665. public List<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  666. {
  667. var list = new List<SubtitleStreamInfo>();
  668. // HLS will preserve timestamps so we can just grab the full subtitle stream
  669. long startPositionTicks = string.Equals(SubProtocol, "hls", StringComparison.OrdinalIgnoreCase)
  670. ? 0
  671. : (PlayMethod == PlayMethod.Transcode && !CopyTimestamps ? StartPositionTicks : 0);
  672. // First add the selected track
  673. if (SubtitleStreamIndex.HasValue)
  674. {
  675. foreach (var stream in MediaSource.MediaStreams)
  676. {
  677. if (stream.Type == MediaStreamType.Subtitle && stream.Index == SubtitleStreamIndex.Value)
  678. {
  679. AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  680. }
  681. }
  682. }
  683. if (!includeSelectedTrackOnly)
  684. {
  685. foreach (var stream in MediaSource.MediaStreams)
  686. {
  687. if (stream.Type == MediaStreamType.Subtitle && (!SubtitleStreamIndex.HasValue || stream.Index != SubtitleStreamIndex.Value))
  688. {
  689. AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  690. }
  691. }
  692. }
  693. return list;
  694. }
  695. private void AddSubtitleProfiles(List<SubtitleStreamInfo> list, MediaStream stream, ITranscoderSupport transcoderSupport, bool enableAllProfiles, string baseUrl, string accessToken, long startPositionTicks)
  696. {
  697. if (enableAllProfiles)
  698. {
  699. foreach (var profile in DeviceProfile.SubtitleProfiles)
  700. {
  701. var info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, new[] { profile }, transcoderSupport);
  702. list.Add(info);
  703. }
  704. }
  705. else
  706. {
  707. var info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, DeviceProfile.SubtitleProfiles, transcoderSupport);
  708. list.Add(info);
  709. }
  710. }
  711. private SubtitleStreamInfo GetSubtitleStreamInfo(MediaStream stream, string baseUrl, string accessToken, long startPositionTicks, SubtitleProfile[] subtitleProfiles, ITranscoderSupport transcoderSupport)
  712. {
  713. var subtitleProfile = StreamBuilder.GetSubtitleProfile(MediaSource, stream, subtitleProfiles, PlayMethod, transcoderSupport, Container, SubProtocol);
  714. var info = new SubtitleStreamInfo
  715. {
  716. IsForced = stream.IsForced,
  717. Language = stream.Language,
  718. Name = stream.Language ?? "Unknown",
  719. Format = subtitleProfile.Format,
  720. Index = stream.Index,
  721. DeliveryMethod = subtitleProfile.Method,
  722. DisplayTitle = stream.DisplayTitle
  723. };
  724. if (info.DeliveryMethod == SubtitleDeliveryMethod.External)
  725. {
  726. if (MediaSource.Protocol == MediaProtocol.File || !string.Equals(stream.Codec, subtitleProfile.Format, StringComparison.OrdinalIgnoreCase) || !stream.IsExternal)
  727. {
  728. info.Url = string.Format(
  729. CultureInfo.InvariantCulture,
  730. "{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  731. baseUrl,
  732. ItemId,
  733. MediaSourceId,
  734. stream.Index.ToString(CultureInfo.InvariantCulture),
  735. startPositionTicks.ToString(CultureInfo.InvariantCulture),
  736. subtitleProfile.Format);
  737. if (!string.IsNullOrEmpty(accessToken))
  738. {
  739. info.Url += "?api_key=" + accessToken;
  740. }
  741. info.IsExternalUrl = false;
  742. }
  743. else
  744. {
  745. info.Url = stream.Path;
  746. info.IsExternalUrl = true;
  747. }
  748. }
  749. return info;
  750. }
  751. public int? GetTargetVideoBitDepth(string codec)
  752. {
  753. var value = GetOption(codec, "videobitdepth");
  754. if (string.IsNullOrEmpty(value))
  755. {
  756. return null;
  757. }
  758. if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
  759. {
  760. return result;
  761. }
  762. return null;
  763. }
  764. public int? GetTargetAudioBitDepth(string codec)
  765. {
  766. var value = GetOption(codec, "audiobitdepth");
  767. if (string.IsNullOrEmpty(value))
  768. {
  769. return null;
  770. }
  771. if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
  772. {
  773. return result;
  774. }
  775. return null;
  776. }
  777. public double? GetTargetVideoLevel(string codec)
  778. {
  779. var value = GetOption(codec, "level");
  780. if (string.IsNullOrEmpty(value))
  781. {
  782. return null;
  783. }
  784. if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  785. {
  786. return result;
  787. }
  788. return null;
  789. }
  790. public int? GetTargetRefFrames(string codec)
  791. {
  792. var value = GetOption(codec, "maxrefframes");
  793. if (string.IsNullOrEmpty(value))
  794. {
  795. return null;
  796. }
  797. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  798. {
  799. return result;
  800. }
  801. return null;
  802. }
  803. public int? GetTargetAudioChannels(string codec)
  804. {
  805. var defaultValue = GlobalMaxAudioChannels ?? TranscodingMaxAudioChannels;
  806. var value = GetOption(codec, "audiochannels");
  807. if (string.IsNullOrEmpty(value))
  808. {
  809. return defaultValue;
  810. }
  811. if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
  812. {
  813. return Math.Min(result, defaultValue ?? result);
  814. }
  815. return defaultValue;
  816. }
  817. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  818. {
  819. var count = MediaSource.GetStreamCount(type);
  820. if (count.HasValue)
  821. {
  822. count = Math.Min(count.Value, limit);
  823. }
  824. return count;
  825. }
  826. public List<MediaStream> GetSelectableAudioStreams()
  827. {
  828. return GetSelectableStreams(MediaStreamType.Audio);
  829. }
  830. public List<MediaStream> GetSelectableSubtitleStreams()
  831. {
  832. return GetSelectableStreams(MediaStreamType.Subtitle);
  833. }
  834. public List<MediaStream> GetSelectableStreams(MediaStreamType type)
  835. {
  836. var list = new List<MediaStream>();
  837. foreach (var stream in MediaSource.MediaStreams)
  838. {
  839. if (type == stream.Type)
  840. {
  841. list.Add(stream);
  842. }
  843. }
  844. return list;
  845. }
  846. }
  847. }