StreamBuilder.cs 31 KB

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