StreamBuilder.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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);
  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);
  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);
  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)
  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. return profile;
  461. }
  462. }
  463. foreach (SubtitleProfile profile in deviceProfile.SubtitleProfiles)
  464. {
  465. if (profile.Method == SubtitleDeliveryMethod.Embed && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format))
  466. {
  467. return profile;
  468. }
  469. }
  470. return new SubtitleProfile
  471. {
  472. Method = SubtitleDeliveryMethod.Encode,
  473. Format = subtitleStream.Codec
  474. };
  475. }
  476. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
  477. {
  478. // Honor the max bitrate setting
  479. return !maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value);
  480. }
  481. private void ValidateInput(VideoOptions options)
  482. {
  483. ValidateAudioInput(options);
  484. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  485. {
  486. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  487. }
  488. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  489. {
  490. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  491. }
  492. }
  493. private void ValidateAudioInput(AudioOptions options)
  494. {
  495. if (string.IsNullOrEmpty(options.ItemId))
  496. {
  497. throw new ArgumentException("ItemId is required");
  498. }
  499. if (string.IsNullOrEmpty(options.DeviceId))
  500. {
  501. throw new ArgumentException("DeviceId is required");
  502. }
  503. if (options.Profile == null)
  504. {
  505. throw new ArgumentException("Profile is required");
  506. }
  507. if (options.MediaSources == null)
  508. {
  509. throw new ArgumentException("MediaSources is required");
  510. }
  511. }
  512. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  513. {
  514. foreach (ProfileCondition condition in conditions)
  515. {
  516. string value = condition.Value;
  517. if (string.IsNullOrEmpty(value))
  518. {
  519. continue;
  520. }
  521. // No way to express this
  522. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  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. if (condition.Condition == ProfileConditionType.Equals)
  552. {
  553. item.Cabac = val;
  554. }
  555. else if (condition.Condition == ProfileConditionType.NotEquals)
  556. {
  557. item.Cabac = !val;
  558. }
  559. }
  560. break;
  561. }
  562. case ProfileConditionValue.IsAnamorphic:
  563. case ProfileConditionValue.AudioProfile:
  564. case ProfileConditionValue.Has64BitOffsets:
  565. case ProfileConditionValue.PacketLength:
  566. case ProfileConditionValue.VideoTimestamp:
  567. {
  568. // Not supported yet
  569. break;
  570. }
  571. case ProfileConditionValue.RefFrames:
  572. {
  573. int num;
  574. if (IntHelper.TryParseCultureInvariant(value, out num))
  575. {
  576. item.MaxRefFrames = num;
  577. }
  578. break;
  579. }
  580. case ProfileConditionValue.VideoBitDepth:
  581. {
  582. int num;
  583. if (IntHelper.TryParseCultureInvariant(value, out num))
  584. {
  585. item.MaxVideoBitDepth = num;
  586. }
  587. break;
  588. }
  589. case ProfileConditionValue.VideoProfile:
  590. {
  591. item.VideoProfile = (value ?? string.Empty).Split('|')[0];
  592. break;
  593. }
  594. case ProfileConditionValue.Height:
  595. {
  596. int num;
  597. if (IntHelper.TryParseCultureInvariant(value, out num))
  598. {
  599. item.MaxHeight = num;
  600. }
  601. break;
  602. }
  603. case ProfileConditionValue.VideoBitrate:
  604. {
  605. int num;
  606. if (IntHelper.TryParseCultureInvariant(value, out num))
  607. {
  608. item.VideoBitrate = num;
  609. }
  610. break;
  611. }
  612. case ProfileConditionValue.VideoFramerate:
  613. {
  614. float num;
  615. if (FloatHelper.TryParseCultureInvariant(value, out num))
  616. {
  617. item.MaxFramerate = num;
  618. }
  619. break;
  620. }
  621. case ProfileConditionValue.VideoLevel:
  622. {
  623. int num;
  624. if (IntHelper.TryParseCultureInvariant(value, out num))
  625. {
  626. item.VideoLevel = num;
  627. }
  628. break;
  629. }
  630. case ProfileConditionValue.Width:
  631. {
  632. int num;
  633. if (IntHelper.TryParseCultureInvariant(value, out num))
  634. {
  635. item.MaxWidth = num;
  636. }
  637. break;
  638. }
  639. default:
  640. throw new ArgumentException("Unrecognized ProfileConditionValue");
  641. }
  642. }
  643. }
  644. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  645. {
  646. if (profile.Container.Length > 0)
  647. {
  648. // Check container type
  649. string mediaContainer = item.Container ?? string.Empty;
  650. bool any = false;
  651. foreach (string i in profile.GetContainers())
  652. {
  653. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  654. {
  655. any = true;
  656. break;
  657. }
  658. }
  659. if (!any)
  660. {
  661. return false;
  662. }
  663. }
  664. return true;
  665. }
  666. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  667. {
  668. if (profile.Container.Length > 0)
  669. {
  670. // Check container type
  671. string mediaContainer = item.Container ?? string.Empty;
  672. bool any = false;
  673. foreach (string i in profile.GetContainers())
  674. {
  675. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  676. {
  677. any = true;
  678. break;
  679. }
  680. }
  681. if (!any)
  682. {
  683. return false;
  684. }
  685. }
  686. // Check video codec
  687. List<string> videoCodecs = profile.GetVideoCodecs();
  688. if (videoCodecs.Count > 0)
  689. {
  690. string videoCodec = videoStream == null ? null : videoStream.Codec;
  691. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  692. {
  693. return false;
  694. }
  695. }
  696. List<string> audioCodecs = profile.GetAudioCodecs();
  697. if (audioCodecs.Count > 0)
  698. {
  699. // Check audio codecs
  700. string audioCodec = audioStream == null ? null : audioStream.Codec;
  701. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  702. {
  703. return false;
  704. }
  705. }
  706. return true;
  707. }
  708. }
  709. }