StreamBuilder.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.MediaInfo;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. namespace MediaBrowser.Model.Dlna
  9. {
  10. public class StreamBuilder
  11. {
  12. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  13. public StreamInfo BuildAudioItem(AudioOptions options)
  14. {
  15. ValidateAudioInput(options);
  16. List<MediaSourceInfo> mediaSources = options.MediaSources;
  17. // If the client wants a specific media soure, filter now
  18. if (!string.IsNullOrEmpty(options.MediaSourceId))
  19. {
  20. // Avoid implicitly captured closure
  21. string mediaSourceId = options.MediaSourceId;
  22. mediaSources = new List<MediaSourceInfo>();
  23. foreach (MediaSourceInfo i in mediaSources)
  24. {
  25. if (string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase))
  26. mediaSources.Add(i);
  27. }
  28. }
  29. List<StreamInfo> streams = new List<StreamInfo>();
  30. foreach (MediaSourceInfo i in mediaSources)
  31. streams.Add(BuildAudioItem(i, options));
  32. foreach (StreamInfo stream in streams)
  33. {
  34. stream.DeviceId = options.DeviceId;
  35. stream.DeviceProfileId = options.Profile.Id;
  36. }
  37. return GetOptimalStream(streams);
  38. }
  39. public StreamInfo BuildVideoItem(VideoOptions options)
  40. {
  41. ValidateInput(options);
  42. List<MediaSourceInfo> mediaSources = options.MediaSources;
  43. // If the client wants a specific media soure, filter now
  44. if (!string.IsNullOrEmpty(options.MediaSourceId))
  45. {
  46. // Avoid implicitly captured closure
  47. string mediaSourceId = options.MediaSourceId;
  48. mediaSources = new List<MediaSourceInfo>();
  49. foreach (MediaSourceInfo i in mediaSources)
  50. {
  51. if (string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase))
  52. mediaSources.Add(i);
  53. }
  54. }
  55. List<StreamInfo> streams = new List<StreamInfo>();
  56. foreach (MediaSourceInfo i in mediaSources)
  57. streams.Add(BuildVideoItem(i, options));
  58. foreach (StreamInfo stream in streams)
  59. {
  60. stream.DeviceId = options.DeviceId;
  61. stream.DeviceProfileId = options.Profile.Id;
  62. }
  63. return GetOptimalStream(streams);
  64. }
  65. private StreamInfo GetOptimalStream(List<StreamInfo> streams)
  66. {
  67. // Grab the first one that can be direct streamed
  68. // If that doesn't produce anything, just take the first
  69. foreach (StreamInfo i in streams)
  70. {
  71. if (i.IsDirectStream)
  72. {
  73. return i;
  74. }
  75. }
  76. foreach (StreamInfo stream in streams)
  77. {
  78. return stream;
  79. }
  80. return null;
  81. }
  82. private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
  83. {
  84. StreamInfo playlistItem = new StreamInfo
  85. {
  86. ItemId = options.ItemId,
  87. MediaType = DlnaProfileType.Audio,
  88. MediaSource = item,
  89. RunTimeTicks = item.RunTimeTicks
  90. };
  91. int? maxBitrateSetting = options.MaxBitrate ?? options.Profile.MaxBitrate;
  92. MediaStream audioStream = item.DefaultAudioStream;
  93. // Honor the max bitrate setting
  94. if (IsAudioEligibleForDirectPlay(item, maxBitrateSetting))
  95. {
  96. DirectPlayProfile directPlay = null;
  97. foreach (DirectPlayProfile i in options.Profile.DirectPlayProfiles)
  98. {
  99. if (i.Type == playlistItem.MediaType && IsAudioDirectPlaySupported(i, item, audioStream))
  100. {
  101. directPlay = i;
  102. break;
  103. }
  104. }
  105. if (directPlay != null)
  106. {
  107. string audioCodec = audioStream == null ? null : audioStream.Codec;
  108. // Make sure audio codec profiles are satisfied
  109. if (!string.IsNullOrEmpty(audioCodec))
  110. {
  111. ConditionProcessor conditionProcessor = new ConditionProcessor();
  112. List<ProfileCondition> conditions = new List<ProfileCondition>();
  113. foreach (CodecProfile i in options.Profile.CodecProfiles)
  114. {
  115. if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec))
  116. conditions.AddRange(i.Conditions);
  117. }
  118. int? audioChannels = audioStream.Channels;
  119. int? audioBitrate = audioStream.BitRate;
  120. bool all = true;
  121. foreach (ProfileCondition c in conditions)
  122. {
  123. if (!conditionProcessor.IsAudioConditionSatisfied(c, audioChannels, audioBitrate))
  124. {
  125. all = false;
  126. break;
  127. }
  128. }
  129. if (all)
  130. {
  131. playlistItem.IsDirectStream = true;
  132. playlistItem.Container = item.Container;
  133. return playlistItem;
  134. }
  135. }
  136. }
  137. }
  138. TranscodingProfile transcodingProfile = null;
  139. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  140. {
  141. if (i.Type == playlistItem.MediaType)
  142. {
  143. transcodingProfile = i;
  144. break;
  145. }
  146. }
  147. if (transcodingProfile != null)
  148. {
  149. playlistItem.IsDirectStream = false;
  150. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  151. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  152. playlistItem.Container = transcodingProfile.Container;
  153. playlistItem.AudioCodec = transcodingProfile.AudioCodec;
  154. playlistItem.Protocol = transcodingProfile.Protocol;
  155. List<CodecProfile> audioCodecProfiles = new List<CodecProfile>();
  156. foreach (CodecProfile i in options.Profile.CodecProfiles)
  157. {
  158. if (i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec))
  159. {
  160. audioCodecProfiles.Add(i);
  161. }
  162. if (audioCodecProfiles.Count >= 1) break;
  163. }
  164. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  165. foreach (CodecProfile i in audioCodecProfiles)
  166. audioTranscodingConditions.AddRange(i.Conditions);
  167. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  168. // Honor requested max channels
  169. if (options.MaxAudioChannels.HasValue)
  170. {
  171. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  172. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  173. }
  174. // Honor requested max bitrate
  175. if (maxBitrateSetting.HasValue)
  176. {
  177. int currentValue = playlistItem.AudioBitrate ?? maxBitrateSetting.Value;
  178. playlistItem.AudioBitrate = Math.Min(maxBitrateSetting.Value, currentValue);
  179. }
  180. }
  181. return playlistItem;
  182. }
  183. private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
  184. {
  185. StreamInfo playlistItem = new StreamInfo
  186. {
  187. ItemId = options.ItemId,
  188. MediaType = DlnaProfileType.Video,
  189. MediaSource = item,
  190. RunTimeTicks = item.RunTimeTicks
  191. };
  192. MediaStream audioStream = item.DefaultAudioStream;
  193. MediaStream videoStream = item.VideoStream;
  194. int? maxBitrateSetting = options.MaxBitrate ?? options.Profile.MaxBitrate;
  195. if (IsEligibleForDirectPlay(item, options, maxBitrateSetting))
  196. {
  197. // See if it can be direct played
  198. DirectPlayProfile directPlay = GetVideoDirectPlayProfile(options.Profile, item, videoStream, audioStream);
  199. if (directPlay != null)
  200. {
  201. playlistItem.IsDirectStream = true;
  202. playlistItem.Container = item.Container;
  203. return playlistItem;
  204. }
  205. }
  206. // Can't direct play, find the transcoding profile
  207. TranscodingProfile transcodingProfile = null;
  208. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  209. {
  210. if (i.Type == playlistItem.MediaType)
  211. {
  212. transcodingProfile = i;
  213. break;
  214. }
  215. }
  216. if (transcodingProfile != null)
  217. {
  218. playlistItem.IsDirectStream = false;
  219. playlistItem.Container = transcodingProfile.Container;
  220. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  221. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  222. playlistItem.AudioCodec = transcodingProfile.AudioCodec.Split(',')[0];
  223. playlistItem.VideoCodec = transcodingProfile.VideoCodec;
  224. playlistItem.Protocol = transcodingProfile.Protocol;
  225. playlistItem.AudioStreamIndex = options.AudioStreamIndex ?? item.DefaultAudioStreamIndex;
  226. playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? item.DefaultSubtitleStreamIndex;
  227. List<ProfileCondition> videoTranscodingConditions = new List<ProfileCondition>();
  228. foreach (CodecProfile i in options.Profile.CodecProfiles)
  229. {
  230. if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec))
  231. {
  232. videoTranscodingConditions.AddRange(i.Conditions);
  233. break;
  234. }
  235. }
  236. ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
  237. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  238. foreach (CodecProfile i in options.Profile.CodecProfiles)
  239. {
  240. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(transcodingProfile.AudioCodec))
  241. {
  242. audioTranscodingConditions.AddRange(i.Conditions);
  243. break;
  244. }
  245. }
  246. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  247. // Honor requested max channels
  248. if (options.MaxAudioChannels.HasValue)
  249. {
  250. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  251. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  252. }
  253. // Honor requested max bitrate
  254. if (options.MaxAudioTranscodingBitrate.HasValue)
  255. {
  256. int currentValue = playlistItem.AudioBitrate ?? options.MaxAudioTranscodingBitrate.Value;
  257. playlistItem.AudioBitrate = Math.Min(options.MaxAudioTranscodingBitrate.Value, currentValue);
  258. }
  259. // Honor max rate
  260. if (maxBitrateSetting.HasValue)
  261. {
  262. int videoBitrate = maxBitrateSetting.Value;
  263. if (playlistItem.AudioBitrate.HasValue)
  264. {
  265. videoBitrate -= playlistItem.AudioBitrate.Value;
  266. }
  267. int currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  268. playlistItem.VideoBitrate = Math.Min(videoBitrate, currentValue);
  269. }
  270. }
  271. return playlistItem;
  272. }
  273. private DirectPlayProfile GetVideoDirectPlayProfile(DeviceProfile profile,
  274. MediaSourceInfo mediaSource,
  275. MediaStream videoStream,
  276. MediaStream audioStream)
  277. {
  278. // See if it can be direct played
  279. DirectPlayProfile directPlay = null;
  280. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  281. {
  282. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  283. {
  284. directPlay = i;
  285. break;
  286. }
  287. }
  288. if (directPlay == null)
  289. {
  290. return null;
  291. }
  292. string container = mediaSource.Container;
  293. List<ProfileCondition> conditions = new List<ProfileCondition>();
  294. foreach (ContainerProfile i in profile.ContainerProfiles)
  295. {
  296. if (i.Type == DlnaProfileType.Video &&
  297. i.GetContainers().Contains(container, StringComparer.OrdinalIgnoreCase))
  298. {
  299. conditions.AddRange(i.Conditions);
  300. }
  301. }
  302. ConditionProcessor conditionProcessor = new ConditionProcessor();
  303. int? width = videoStream == null ? null : videoStream.Width;
  304. int? height = videoStream == null ? null : videoStream.Height;
  305. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  306. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  307. double? videoLevel = videoStream == null ? null : videoStream.Level;
  308. string videoProfile = videoStream == null ? null : videoStream.Profile;
  309. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  310. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  311. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  312. string audioProfile = audioStream == null ? null : audioStream.Profile;
  313. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  314. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  315. // Check container conditions
  316. foreach (ProfileCondition i in conditions)
  317. {
  318. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp))
  319. {
  320. return null;
  321. }
  322. }
  323. string videoCodec = videoStream == null ? null : videoStream.Codec;
  324. if (string.IsNullOrEmpty(videoCodec))
  325. {
  326. return null;
  327. }
  328. conditions = new List<ProfileCondition>();
  329. foreach (CodecProfile i in profile.CodecProfiles)
  330. {
  331. if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec))
  332. conditions.AddRange(i.Conditions);
  333. }
  334. foreach (ProfileCondition i in conditions)
  335. {
  336. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp))
  337. {
  338. return null;
  339. }
  340. }
  341. if (audioStream != null)
  342. {
  343. string audioCodec = audioStream.Codec;
  344. if (string.IsNullOrEmpty(audioCodec))
  345. {
  346. return null;
  347. }
  348. conditions = new List<ProfileCondition>();
  349. foreach (CodecProfile i in profile.CodecProfiles)
  350. {
  351. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec))
  352. conditions.AddRange(i.Conditions);
  353. }
  354. foreach (ProfileCondition i in conditions)
  355. {
  356. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile))
  357. {
  358. return null;
  359. }
  360. }
  361. }
  362. return directPlay;
  363. }
  364. private bool IsEligibleForDirectPlay(MediaSourceInfo item, VideoOptions options, int? maxBitrate)
  365. {
  366. if (options.SubtitleStreamIndex.HasValue)
  367. {
  368. return false;
  369. }
  370. return IsAudioEligibleForDirectPlay(item, maxBitrate);
  371. }
  372. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
  373. {
  374. // Honor the max bitrate setting
  375. return !maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value);
  376. }
  377. private void ValidateInput(VideoOptions options)
  378. {
  379. ValidateAudioInput(options);
  380. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  381. {
  382. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  383. }
  384. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  385. {
  386. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  387. }
  388. }
  389. private void ValidateAudioInput(AudioOptions options)
  390. {
  391. if (string.IsNullOrEmpty(options.ItemId))
  392. {
  393. throw new ArgumentException("ItemId is required");
  394. }
  395. if (string.IsNullOrEmpty(options.DeviceId))
  396. {
  397. throw new ArgumentException("DeviceId is required");
  398. }
  399. if (options.Profile == null)
  400. {
  401. throw new ArgumentException("Profile is required");
  402. }
  403. if (options.MediaSources == null)
  404. {
  405. throw new ArgumentException("MediaSources is required");
  406. }
  407. }
  408. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  409. {
  410. foreach (ProfileCondition condition in conditions)
  411. {
  412. string value = condition.Value;
  413. if (string.IsNullOrEmpty(value))
  414. {
  415. continue;
  416. }
  417. switch (condition.Property)
  418. {
  419. case ProfileConditionValue.AudioBitrate:
  420. {
  421. int num;
  422. if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
  423. {
  424. item.AudioBitrate = num;
  425. }
  426. break;
  427. }
  428. case ProfileConditionValue.AudioChannels:
  429. {
  430. int num;
  431. if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
  432. {
  433. item.MaxAudioChannels = num;
  434. }
  435. break;
  436. }
  437. case ProfileConditionValue.AudioProfile:
  438. case ProfileConditionValue.Has64BitOffsets:
  439. case ProfileConditionValue.PacketLength:
  440. case ProfileConditionValue.VideoTimestamp:
  441. case ProfileConditionValue.VideoBitDepth:
  442. {
  443. // Not supported yet
  444. break;
  445. }
  446. case ProfileConditionValue.VideoProfile:
  447. {
  448. item.VideoProfile = value;
  449. break;
  450. }
  451. case ProfileConditionValue.Height:
  452. {
  453. int num;
  454. if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
  455. {
  456. item.MaxHeight = num;
  457. }
  458. break;
  459. }
  460. case ProfileConditionValue.VideoBitrate:
  461. {
  462. int num;
  463. if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
  464. {
  465. item.VideoBitrate = num;
  466. }
  467. break;
  468. }
  469. case ProfileConditionValue.VideoFramerate:
  470. {
  471. int num;
  472. if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
  473. {
  474. item.MaxFramerate = num;
  475. }
  476. break;
  477. }
  478. case ProfileConditionValue.VideoLevel:
  479. {
  480. int num;
  481. if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
  482. {
  483. item.VideoLevel = num;
  484. }
  485. break;
  486. }
  487. case ProfileConditionValue.Width:
  488. {
  489. int num;
  490. if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
  491. {
  492. item.MaxWidth = num;
  493. }
  494. break;
  495. }
  496. default:
  497. throw new ArgumentException("Unrecognized ProfileConditionValue");
  498. }
  499. }
  500. }
  501. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  502. {
  503. if (profile.Container.Length > 0)
  504. {
  505. // Check container type
  506. string mediaContainer = item.Container ?? string.Empty;
  507. bool any = false;
  508. foreach (string i in profile.GetContainers())
  509. {
  510. if (string.Equals(i, mediaContainer, StringComparison.OrdinalIgnoreCase))
  511. {
  512. any = true;
  513. break;
  514. }
  515. }
  516. if (!any)
  517. {
  518. return false;
  519. }
  520. }
  521. return true;
  522. }
  523. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  524. {
  525. // Only plain video files can be direct played
  526. if (item.VideoType != VideoType.VideoFile)
  527. {
  528. return false;
  529. }
  530. if (profile.Container.Length > 0)
  531. {
  532. // Check container type
  533. string mediaContainer = item.Container ?? string.Empty;
  534. bool any = false;
  535. foreach (string i in profile.GetContainers())
  536. {
  537. if (string.Equals(i, mediaContainer, StringComparison.OrdinalIgnoreCase))
  538. {
  539. any = true;
  540. break;
  541. }
  542. }
  543. if (!any)
  544. {
  545. return false;
  546. }
  547. }
  548. // Check video codec
  549. List<string> videoCodecs = profile.GetVideoCodecs();
  550. if (videoCodecs.Count > 0)
  551. {
  552. string videoCodec = videoStream == null ? null : videoStream.Codec;
  553. if (string.IsNullOrEmpty(videoCodec) || !videoCodecs.Contains(videoCodec, StringComparer.OrdinalIgnoreCase))
  554. {
  555. return false;
  556. }
  557. }
  558. List<string> audioCodecs = profile.GetAudioCodecs();
  559. if (audioCodecs.Count > 0)
  560. {
  561. // Check audio codecs
  562. string audioCodec = audioStream == null ? null : audioStream.Codec;
  563. if (string.IsNullOrEmpty(audioCodec) || !audioCodecs.Contains(audioCodec, StringComparer.OrdinalIgnoreCase))
  564. {
  565. return false;
  566. }
  567. }
  568. return true;
  569. }
  570. }
  571. }