StreamBuilder.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. List<MediaSourceInfo> 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 (ProfileCondition 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 (ProfileCondition 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. int 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 (ProfileCondition 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 (ProfileCondition 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 (ProfileCondition 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. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  352. // Check container conditions
  353. foreach (ProfileCondition i in conditions)
  354. {
  355. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames))
  356. {
  357. return null;
  358. }
  359. }
  360. string videoCodec = videoStream == null ? null : videoStream.Codec;
  361. if (string.IsNullOrEmpty(videoCodec))
  362. {
  363. return null;
  364. }
  365. conditions = new List<ProfileCondition>();
  366. foreach (CodecProfile i in profile.CodecProfiles)
  367. {
  368. if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec))
  369. {
  370. foreach (ProfileCondition c in i.Conditions)
  371. {
  372. conditions.Add(c);
  373. }
  374. }
  375. }
  376. foreach (ProfileCondition i in conditions)
  377. {
  378. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames))
  379. {
  380. return null;
  381. }
  382. }
  383. if (audioStream != null)
  384. {
  385. string audioCodec = audioStream.Codec;
  386. if (string.IsNullOrEmpty(audioCodec))
  387. {
  388. return null;
  389. }
  390. conditions = new List<ProfileCondition>();
  391. foreach (CodecProfile i in profile.CodecProfiles)
  392. {
  393. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec))
  394. {
  395. foreach (ProfileCondition c in i.Conditions)
  396. {
  397. conditions.Add(c);
  398. }
  399. }
  400. }
  401. foreach (ProfileCondition i in conditions)
  402. {
  403. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile))
  404. {
  405. return null;
  406. }
  407. }
  408. }
  409. return directPlay;
  410. }
  411. private bool IsEligibleForDirectPlay(MediaSourceInfo item,
  412. int? maxBitrate,
  413. MediaStream subtitleStream,
  414. VideoOptions options)
  415. {
  416. if (subtitleStream != null)
  417. {
  418. if (!subtitleStream.IsTextSubtitleStream)
  419. {
  420. return false;
  421. }
  422. SubtitleDeliveryMethod subtitleMethod = GetSubtitleDeliveryMethod(subtitleStream, options);
  423. if (subtitleMethod != SubtitleDeliveryMethod.External && subtitleMethod != SubtitleDeliveryMethod.Embed)
  424. {
  425. return false;
  426. }
  427. }
  428. return IsAudioEligibleForDirectPlay(item, maxBitrate);
  429. }
  430. private SubtitleDeliveryMethod GetSubtitleDeliveryMethod(MediaStream subtitleStream,
  431. VideoOptions options)
  432. {
  433. if (subtitleStream.IsTextSubtitleStream)
  434. {
  435. // See if the device can retrieve the subtitles externally
  436. bool supportsSubsExternally = options.Context == EncodingContext.Streaming &&
  437. ContainsSubtitleFormat(options.Profile.SubtitleProfiles, SubtitleDeliveryMethod.External, _serverTextSubtitleOutputs);
  438. if (supportsSubsExternally)
  439. {
  440. return SubtitleDeliveryMethod.External;
  441. }
  442. // See if the device can retrieve the subtitles externally
  443. bool supportsEmbedded = ContainsSubtitleFormat(options.Profile.SubtitleProfiles, SubtitleDeliveryMethod.Embed, _serverTextSubtitleOutputs);
  444. if (supportsEmbedded)
  445. {
  446. return SubtitleDeliveryMethod.Embed;
  447. }
  448. }
  449. return SubtitleDeliveryMethod.Encode;
  450. }
  451. private bool ContainsSubtitleFormat(SubtitleProfile[] profiles, SubtitleDeliveryMethod method, string[] formats)
  452. {
  453. foreach (SubtitleProfile profile in profiles)
  454. {
  455. if (method == profile.Method && ListHelper.ContainsIgnoreCase(formats, profile.Format))
  456. {
  457. return true;
  458. }
  459. }
  460. return false;
  461. }
  462. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
  463. {
  464. // Honor the max bitrate setting
  465. return !maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value);
  466. }
  467. private void ValidateInput(VideoOptions options)
  468. {
  469. ValidateAudioInput(options);
  470. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  471. {
  472. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  473. }
  474. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  475. {
  476. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  477. }
  478. }
  479. private void ValidateAudioInput(AudioOptions options)
  480. {
  481. if (string.IsNullOrEmpty(options.ItemId))
  482. {
  483. throw new ArgumentException("ItemId is required");
  484. }
  485. if (string.IsNullOrEmpty(options.DeviceId))
  486. {
  487. throw new ArgumentException("DeviceId is required");
  488. }
  489. if (options.Profile == null)
  490. {
  491. throw new ArgumentException("Profile is required");
  492. }
  493. if (options.MediaSources == null)
  494. {
  495. throw new ArgumentException("MediaSources is required");
  496. }
  497. }
  498. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  499. {
  500. foreach (ProfileCondition condition in conditions)
  501. {
  502. string value = condition.Value;
  503. if (string.IsNullOrEmpty(value))
  504. {
  505. continue;
  506. }
  507. switch (condition.Property)
  508. {
  509. case ProfileConditionValue.AudioBitrate:
  510. {
  511. int num;
  512. if (IntHelper.TryParseCultureInvariant(value, out num))
  513. {
  514. item.AudioBitrate = num;
  515. }
  516. break;
  517. }
  518. case ProfileConditionValue.AudioChannels:
  519. {
  520. int num;
  521. if (IntHelper.TryParseCultureInvariant(value, out num))
  522. {
  523. item.MaxAudioChannels = num;
  524. }
  525. break;
  526. }
  527. case ProfileConditionValue.AudioProfile:
  528. case ProfileConditionValue.IsAnamorphic:
  529. case ProfileConditionValue.Has64BitOffsets:
  530. case ProfileConditionValue.PacketLength:
  531. case ProfileConditionValue.VideoTimestamp:
  532. case ProfileConditionValue.VideoBitDepth:
  533. {
  534. // Not supported yet
  535. break;
  536. }
  537. case ProfileConditionValue.VideoProfile:
  538. {
  539. item.VideoProfile = value;
  540. break;
  541. }
  542. case ProfileConditionValue.Height:
  543. {
  544. int num;
  545. if (IntHelper.TryParseCultureInvariant(value, out num))
  546. {
  547. item.MaxHeight = num;
  548. }
  549. break;
  550. }
  551. case ProfileConditionValue.VideoBitrate:
  552. {
  553. int num;
  554. if (IntHelper.TryParseCultureInvariant(value, out num))
  555. {
  556. item.VideoBitrate = num;
  557. }
  558. break;
  559. }
  560. case ProfileConditionValue.VideoFramerate:
  561. {
  562. float num;
  563. if (FloatHelper.TryParseCultureInvariant(value, out num))
  564. {
  565. item.MaxFramerate = num;
  566. }
  567. break;
  568. }
  569. case ProfileConditionValue.VideoLevel:
  570. {
  571. int num;
  572. if (IntHelper.TryParseCultureInvariant(value, out num))
  573. {
  574. item.VideoLevel = num;
  575. }
  576. break;
  577. }
  578. case ProfileConditionValue.Width:
  579. {
  580. int num;
  581. if (IntHelper.TryParseCultureInvariant(value, out num))
  582. {
  583. item.MaxWidth = num;
  584. }
  585. break;
  586. }
  587. default:
  588. throw new ArgumentException("Unrecognized ProfileConditionValue");
  589. }
  590. }
  591. }
  592. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  593. {
  594. if (profile.Container.Length > 0)
  595. {
  596. // Check container type
  597. string mediaContainer = item.Container ?? string.Empty;
  598. bool any = false;
  599. foreach (string i in profile.GetContainers())
  600. {
  601. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  602. {
  603. any = true;
  604. break;
  605. }
  606. }
  607. if (!any)
  608. {
  609. return false;
  610. }
  611. }
  612. return true;
  613. }
  614. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  615. {
  616. // Only plain video files can be direct played
  617. if (item.VideoType != VideoType.VideoFile)
  618. {
  619. return false;
  620. }
  621. if (profile.Container.Length > 0)
  622. {
  623. // Check container type
  624. string mediaContainer = item.Container ?? string.Empty;
  625. bool any = false;
  626. foreach (string i in profile.GetContainers())
  627. {
  628. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  629. {
  630. any = true;
  631. break;
  632. }
  633. }
  634. if (!any)
  635. {
  636. return false;
  637. }
  638. }
  639. // Check video codec
  640. List<string> videoCodecs = profile.GetVideoCodecs();
  641. if (videoCodecs.Count > 0)
  642. {
  643. string videoCodec = videoStream == null ? null : videoStream.Codec;
  644. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  645. {
  646. return false;
  647. }
  648. }
  649. List<string> audioCodecs = profile.GetAudioCodecs();
  650. if (audioCodecs.Count > 0)
  651. {
  652. // Check audio codecs
  653. string audioCodec = audioStream == null ? null : audioStream.Codec;
  654. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  655. {
  656. return false;
  657. }
  658. }
  659. return true;
  660. }
  661. }
  662. }