StreamBuilder.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.Extensions;
  4. using MediaBrowser.Model.MediaInfo;
  5. using MediaBrowser.Model.Session;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace MediaBrowser.Model.Dlna
  9. {
  10. public class StreamBuilder
  11. {
  12. private readonly string[] _serverTextSubtitleOutputs = { "srt", "vtt", "ttml" };
  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 (StringHelper.EqualsIgnoreCase(i.Id, mediaSourceId))
  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 source, filter now
  44. if (!string.IsNullOrEmpty(options.MediaSourceId))
  45. {
  46. var newMediaSources = new List<MediaSourceInfo>();
  47. foreach (MediaSourceInfo i in mediaSources)
  48. {
  49. if (StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
  50. newMediaSources.Add(i);
  51. }
  52. mediaSources = newMediaSources;
  53. }
  54. List<StreamInfo> streams = new List<StreamInfo>();
  55. foreach (MediaSourceInfo i in mediaSources)
  56. streams.Add(BuildVideoItem(i, options));
  57. foreach (StreamInfo stream in streams)
  58. {
  59. stream.DeviceId = options.DeviceId;
  60. stream.DeviceProfileId = options.Profile.Id;
  61. }
  62. return GetOptimalStream(streams);
  63. }
  64. private StreamInfo GetOptimalStream(List<StreamInfo> streams)
  65. {
  66. // Grab the first one that can be direct streamed
  67. // If that doesn't produce anything, just take the first
  68. foreach (StreamInfo i in streams)
  69. {
  70. if (i.IsDirectStream)
  71. {
  72. return i;
  73. }
  74. }
  75. foreach (StreamInfo stream in streams)
  76. {
  77. return stream;
  78. }
  79. return null;
  80. }
  81. private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
  82. {
  83. StreamInfo playlistItem = new StreamInfo
  84. {
  85. ItemId = options.ItemId,
  86. MediaType = DlnaProfileType.Audio,
  87. MediaSource = item,
  88. RunTimeTicks = item.RunTimeTicks
  89. };
  90. int? maxBitrateSetting = options.GetMaxBitrate();
  91. MediaStream audioStream = item.DefaultAudioStream;
  92. // Honor the max bitrate setting
  93. if (IsAudioEligibleForDirectPlay(item, maxBitrateSetting))
  94. {
  95. DirectPlayProfile directPlay = null;
  96. foreach (DirectPlayProfile i in options.Profile.DirectPlayProfiles)
  97. {
  98. if (i.Type == playlistItem.MediaType && IsAudioDirectPlaySupported(i, item, audioStream))
  99. {
  100. directPlay = i;
  101. break;
  102. }
  103. }
  104. if (directPlay != null)
  105. {
  106. string audioCodec = audioStream == null ? null : audioStream.Codec;
  107. // Make sure audio codec profiles are satisfied
  108. if (!string.IsNullOrEmpty(audioCodec))
  109. {
  110. ConditionProcessor conditionProcessor = new ConditionProcessor();
  111. List<ProfileCondition> conditions = new List<ProfileCondition>();
  112. foreach (CodecProfile i in options.Profile.CodecProfiles)
  113. {
  114. if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec))
  115. {
  116. foreach (var c in i.Conditions)
  117. {
  118. conditions.Add(c);
  119. }
  120. }
  121. }
  122. int? audioChannels = audioStream.Channels;
  123. int? audioBitrate = audioStream.BitRate;
  124. bool all = true;
  125. foreach (ProfileCondition c in conditions)
  126. {
  127. if (!conditionProcessor.IsAudioConditionSatisfied(c, audioChannels, audioBitrate))
  128. {
  129. all = false;
  130. break;
  131. }
  132. }
  133. if (all)
  134. {
  135. playlistItem.PlayMethod = PlayMethod.DirectStream;
  136. playlistItem.Container = item.Container;
  137. return playlistItem;
  138. }
  139. }
  140. }
  141. }
  142. TranscodingProfile transcodingProfile = null;
  143. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  144. {
  145. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  146. {
  147. transcodingProfile = i;
  148. break;
  149. }
  150. }
  151. if (transcodingProfile != null)
  152. {
  153. playlistItem.PlayMethod = PlayMethod.Transcode;
  154. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  155. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  156. playlistItem.Container = transcodingProfile.Container;
  157. playlistItem.AudioCodec = transcodingProfile.AudioCodec;
  158. playlistItem.Protocol = transcodingProfile.Protocol;
  159. List<CodecProfile> audioCodecProfiles = new List<CodecProfile>();
  160. foreach (CodecProfile i in options.Profile.CodecProfiles)
  161. {
  162. if (i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec))
  163. {
  164. audioCodecProfiles.Add(i);
  165. }
  166. if (audioCodecProfiles.Count >= 1) break;
  167. }
  168. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  169. foreach (CodecProfile i in audioCodecProfiles)
  170. {
  171. foreach (var c in i.Conditions)
  172. {
  173. audioTranscodingConditions.Add(c);
  174. }
  175. }
  176. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  177. // Honor requested max channels
  178. if (options.MaxAudioChannels.HasValue)
  179. {
  180. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  181. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  182. }
  183. var configuredBitrate = options.AudioTranscodingBitrate ??
  184. (options.Context == EncodingContext.Static ? options.Profile.MusicSyncBitrate : options.Profile.MusicStreamingTranscodingBitrate) ??
  185. 128000;
  186. playlistItem.AudioBitrate = Math.Min(configuredBitrate, playlistItem.AudioBitrate ?? configuredBitrate);
  187. }
  188. return playlistItem;
  189. }
  190. private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
  191. {
  192. StreamInfo playlistItem = new StreamInfo
  193. {
  194. ItemId = options.ItemId,
  195. MediaType = DlnaProfileType.Video,
  196. MediaSource = item,
  197. RunTimeTicks = item.RunTimeTicks
  198. };
  199. int? audioStreamIndex = options.AudioStreamIndex ?? item.DefaultAudioStreamIndex;
  200. playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? item.DefaultSubtitleStreamIndex;
  201. MediaStream audioStream = audioStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Audio, audioStreamIndex.Value) : null;
  202. MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
  203. MediaStream videoStream = item.VideoStream;
  204. int? maxBitrateSetting = options.GetMaxBitrate();
  205. if (IsEligibleForDirectPlay(item, maxBitrateSetting, subtitleStream, options))
  206. {
  207. // See if it can be direct played
  208. DirectPlayProfile directPlay = GetVideoDirectPlayProfile(options.Profile, item, videoStream, audioStream);
  209. if (directPlay != null)
  210. {
  211. playlistItem.PlayMethod = PlayMethod.DirectStream;
  212. playlistItem.Container = item.Container;
  213. if (subtitleStream != null)
  214. {
  215. playlistItem.SubtitleDeliveryMethod = GetSubtitleDeliveryMethod(subtitleStream, options);
  216. }
  217. return playlistItem;
  218. }
  219. }
  220. // Can't direct play, find the transcoding profile
  221. TranscodingProfile transcodingProfile = null;
  222. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  223. {
  224. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  225. {
  226. transcodingProfile = i;
  227. break;
  228. }
  229. }
  230. if (transcodingProfile != null)
  231. {
  232. if (subtitleStream != null)
  233. {
  234. playlistItem.SubtitleDeliveryMethod = GetSubtitleDeliveryMethod(subtitleStream, options);
  235. }
  236. playlistItem.PlayMethod = PlayMethod.Transcode;
  237. playlistItem.Container = transcodingProfile.Container;
  238. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  239. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  240. playlistItem.AudioCodec = transcodingProfile.AudioCodec.Split(',')[0];
  241. playlistItem.VideoCodec = transcodingProfile.VideoCodec;
  242. playlistItem.Protocol = transcodingProfile.Protocol;
  243. playlistItem.AudioStreamIndex = audioStreamIndex;
  244. List<ProfileCondition> videoTranscodingConditions = new List<ProfileCondition>();
  245. foreach (CodecProfile i in options.Profile.CodecProfiles)
  246. {
  247. if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec))
  248. {
  249. foreach (var c in i.Conditions)
  250. {
  251. videoTranscodingConditions.Add(c);
  252. }
  253. break;
  254. }
  255. }
  256. ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
  257. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  258. foreach (CodecProfile i in options.Profile.CodecProfiles)
  259. {
  260. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(transcodingProfile.AudioCodec))
  261. {
  262. foreach (var c in i.Conditions)
  263. {
  264. audioTranscodingConditions.Add(c);
  265. }
  266. break;
  267. }
  268. }
  269. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  270. // Honor requested max channels
  271. if (options.MaxAudioChannels.HasValue)
  272. {
  273. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  274. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  275. }
  276. if (!playlistItem.AudioBitrate.HasValue)
  277. {
  278. playlistItem.AudioBitrate = GetAudioBitrate(playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec);
  279. }
  280. // Honor max rate
  281. if (maxBitrateSetting.HasValue)
  282. {
  283. int videoBitrate = maxBitrateSetting.Value;
  284. if (playlistItem.AudioBitrate.HasValue)
  285. {
  286. videoBitrate -= playlistItem.AudioBitrate.Value;
  287. }
  288. int currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  289. playlistItem.VideoBitrate = Math.Min(videoBitrate, currentValue);
  290. }
  291. }
  292. return playlistItem;
  293. }
  294. private int GetAudioBitrate(int? channels, string codec)
  295. {
  296. if (channels.HasValue)
  297. {
  298. if (channels.Value >= 5)
  299. {
  300. return 320000;
  301. }
  302. }
  303. return 128000;
  304. }
  305. private DirectPlayProfile GetVideoDirectPlayProfile(DeviceProfile profile,
  306. MediaSourceInfo mediaSource,
  307. MediaStream videoStream,
  308. MediaStream audioStream)
  309. {
  310. // See if it can be direct played
  311. DirectPlayProfile directPlay = null;
  312. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  313. {
  314. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  315. {
  316. directPlay = i;
  317. break;
  318. }
  319. }
  320. if (directPlay == null)
  321. {
  322. return null;
  323. }
  324. string container = mediaSource.Container;
  325. List<ProfileCondition> conditions = new List<ProfileCondition>();
  326. foreach (ContainerProfile i in profile.ContainerProfiles)
  327. {
  328. if (i.Type == DlnaProfileType.Video &&
  329. ListHelper.ContainsIgnoreCase(i.GetContainers(), container))
  330. {
  331. foreach (var c in i.Conditions)
  332. {
  333. conditions.Add(c);
  334. }
  335. }
  336. }
  337. ConditionProcessor conditionProcessor = new ConditionProcessor();
  338. int? width = videoStream == null ? null : videoStream.Width;
  339. int? height = videoStream == null ? null : videoStream.Height;
  340. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  341. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  342. double? videoLevel = videoStream == null ? null : videoStream.Level;
  343. string videoProfile = videoStream == null ? null : videoStream.Profile;
  344. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  345. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  346. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  347. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  348. string audioProfile = audioStream == null ? null : audioStream.Profile;
  349. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  350. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  351. // Check container conditions
  352. foreach (ProfileCondition i in conditions)
  353. {
  354. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic))
  355. {
  356. return null;
  357. }
  358. }
  359. string videoCodec = videoStream == null ? null : videoStream.Codec;
  360. if (string.IsNullOrEmpty(videoCodec))
  361. {
  362. return null;
  363. }
  364. conditions = new List<ProfileCondition>();
  365. foreach (CodecProfile i in profile.CodecProfiles)
  366. {
  367. if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec))
  368. {
  369. foreach (var c in i.Conditions)
  370. {
  371. conditions.Add(c);
  372. }
  373. }
  374. }
  375. foreach (ProfileCondition i in conditions)
  376. {
  377. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic))
  378. {
  379. return null;
  380. }
  381. }
  382. if (audioStream != null)
  383. {
  384. string audioCodec = audioStream.Codec;
  385. if (string.IsNullOrEmpty(audioCodec))
  386. {
  387. return null;
  388. }
  389. conditions = new List<ProfileCondition>();
  390. foreach (CodecProfile i in profile.CodecProfiles)
  391. {
  392. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec))
  393. {
  394. foreach (var c in i.Conditions)
  395. {
  396. conditions.Add(c);
  397. }
  398. }
  399. }
  400. foreach (ProfileCondition i in conditions)
  401. {
  402. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile))
  403. {
  404. return null;
  405. }
  406. }
  407. }
  408. return directPlay;
  409. }
  410. private bool IsEligibleForDirectPlay(MediaSourceInfo item,
  411. int? maxBitrate,
  412. MediaStream subtitleStream,
  413. VideoOptions options)
  414. {
  415. if (subtitleStream != null)
  416. {
  417. if (!subtitleStream.IsTextSubtitleStream)
  418. {
  419. return false;
  420. }
  421. SubtitleDeliveryMethod subtitleMethod = GetSubtitleDeliveryMethod(subtitleStream, options);
  422. if (subtitleMethod != SubtitleDeliveryMethod.External && subtitleMethod != SubtitleDeliveryMethod.Embed)
  423. {
  424. return false;
  425. }
  426. }
  427. return IsAudioEligibleForDirectPlay(item, maxBitrate);
  428. }
  429. private SubtitleDeliveryMethod GetSubtitleDeliveryMethod(MediaStream subtitleStream,
  430. VideoOptions options)
  431. {
  432. if (subtitleStream.IsTextSubtitleStream)
  433. {
  434. // See if the device can retrieve the subtitles externally
  435. bool supportsSubsExternally = options.Context == EncodingContext.Streaming &&
  436. ContainsSubtitleFormat(options.Profile.SubtitleProfiles, SubtitleDeliveryMethod.External, _serverTextSubtitleOutputs);
  437. if (supportsSubsExternally)
  438. {
  439. return SubtitleDeliveryMethod.External;
  440. }
  441. // See if the device can retrieve the subtitles externally
  442. bool supportsEmbedded = ContainsSubtitleFormat(options.Profile.SubtitleProfiles, SubtitleDeliveryMethod.Embed, _serverTextSubtitleOutputs);
  443. if (supportsEmbedded)
  444. {
  445. return SubtitleDeliveryMethod.Embed;
  446. }
  447. }
  448. return SubtitleDeliveryMethod.Encode;
  449. }
  450. private bool ContainsSubtitleFormat(SubtitleProfile[] profiles, SubtitleDeliveryMethod method, string[] formats)
  451. {
  452. foreach (SubtitleProfile profile in profiles)
  453. {
  454. if (method == profile.Method && ListHelper.ContainsIgnoreCase(formats, profile.Format))
  455. {
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
  462. {
  463. // Honor the max bitrate setting
  464. return !maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value);
  465. }
  466. private void ValidateInput(VideoOptions options)
  467. {
  468. ValidateAudioInput(options);
  469. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  470. {
  471. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  472. }
  473. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  474. {
  475. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  476. }
  477. }
  478. private void ValidateAudioInput(AudioOptions options)
  479. {
  480. if (string.IsNullOrEmpty(options.ItemId))
  481. {
  482. throw new ArgumentException("ItemId is required");
  483. }
  484. if (string.IsNullOrEmpty(options.DeviceId))
  485. {
  486. throw new ArgumentException("DeviceId is required");
  487. }
  488. if (options.Profile == null)
  489. {
  490. throw new ArgumentException("Profile is required");
  491. }
  492. if (options.MediaSources == null)
  493. {
  494. throw new ArgumentException("MediaSources is required");
  495. }
  496. }
  497. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  498. {
  499. foreach (ProfileCondition condition in conditions)
  500. {
  501. string value = condition.Value;
  502. if (string.IsNullOrEmpty(value))
  503. {
  504. continue;
  505. }
  506. switch (condition.Property)
  507. {
  508. case ProfileConditionValue.AudioBitrate:
  509. {
  510. int num;
  511. if (IntHelper.TryParseCultureInvariant(value, out num))
  512. {
  513. item.AudioBitrate = num;
  514. }
  515. break;
  516. }
  517. case ProfileConditionValue.AudioChannels:
  518. {
  519. int num;
  520. if (IntHelper.TryParseCultureInvariant(value, out num))
  521. {
  522. item.MaxAudioChannels = num;
  523. }
  524. break;
  525. }
  526. case ProfileConditionValue.AudioProfile:
  527. case ProfileConditionValue.IsAnamorphic:
  528. case ProfileConditionValue.Has64BitOffsets:
  529. case ProfileConditionValue.PacketLength:
  530. case ProfileConditionValue.VideoTimestamp:
  531. case ProfileConditionValue.VideoBitDepth:
  532. {
  533. // Not supported yet
  534. break;
  535. }
  536. case ProfileConditionValue.VideoProfile:
  537. {
  538. item.VideoProfile = value;
  539. break;
  540. }
  541. case ProfileConditionValue.Height:
  542. {
  543. int num;
  544. if (IntHelper.TryParseCultureInvariant(value, out num))
  545. {
  546. item.MaxHeight = num;
  547. }
  548. break;
  549. }
  550. case ProfileConditionValue.VideoBitrate:
  551. {
  552. int num;
  553. if (IntHelper.TryParseCultureInvariant(value, out num))
  554. {
  555. item.VideoBitrate = num;
  556. }
  557. break;
  558. }
  559. case ProfileConditionValue.VideoFramerate:
  560. {
  561. float num;
  562. if (FloatHelper.TryParseCultureInvariant(value, out num))
  563. {
  564. item.MaxFramerate = num;
  565. }
  566. break;
  567. }
  568. case ProfileConditionValue.VideoLevel:
  569. {
  570. int num;
  571. if (IntHelper.TryParseCultureInvariant(value, out num))
  572. {
  573. item.VideoLevel = num;
  574. }
  575. break;
  576. }
  577. case ProfileConditionValue.Width:
  578. {
  579. int num;
  580. if (IntHelper.TryParseCultureInvariant(value, out num))
  581. {
  582. item.MaxWidth = num;
  583. }
  584. break;
  585. }
  586. default:
  587. throw new ArgumentException("Unrecognized ProfileConditionValue");
  588. }
  589. }
  590. }
  591. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  592. {
  593. if (profile.Container.Length > 0)
  594. {
  595. // Check container type
  596. string mediaContainer = item.Container ?? string.Empty;
  597. bool any = false;
  598. foreach (string i in profile.GetContainers())
  599. {
  600. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  601. {
  602. any = true;
  603. break;
  604. }
  605. }
  606. if (!any)
  607. {
  608. return false;
  609. }
  610. }
  611. return true;
  612. }
  613. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  614. {
  615. // Only plain video files can be direct played
  616. if (item.VideoType != VideoType.VideoFile)
  617. {
  618. return false;
  619. }
  620. if (profile.Container.Length > 0)
  621. {
  622. // Check container type
  623. string mediaContainer = item.Container ?? string.Empty;
  624. bool any = false;
  625. foreach (string i in profile.GetContainers())
  626. {
  627. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  628. {
  629. any = true;
  630. break;
  631. }
  632. }
  633. if (!any)
  634. {
  635. return false;
  636. }
  637. }
  638. // Check video codec
  639. List<string> videoCodecs = profile.GetVideoCodecs();
  640. if (videoCodecs.Count > 0)
  641. {
  642. string videoCodec = videoStream == null ? null : videoStream.Codec;
  643. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  644. {
  645. return false;
  646. }
  647. }
  648. List<string> audioCodecs = profile.GetAudioCodecs();
  649. if (audioCodecs.Count > 0)
  650. {
  651. // Check audio codecs
  652. string audioCodec = audioStream == null ? null : audioStream.Codec;
  653. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  654. {
  655. return false;
  656. }
  657. }
  658. return true;
  659. }
  660. }
  661. }