StreamBuilder.cs 32 KB

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