StreamBuilder.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. Context = options.Context
  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. };
  207. int? audioStreamIndex = options.AudioStreamIndex ?? item.DefaultAudioStreamIndex;
  208. playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? item.DefaultSubtitleStreamIndex;
  209. MediaStream audioStream = audioStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Audio, audioStreamIndex.Value) : null;
  210. MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
  211. MediaStream videoStream = item.VideoStream;
  212. int? maxBitrateSetting = options.GetMaxBitrate();
  213. if (IsEligibleForDirectPlay(item, maxBitrateSetting, subtitleStream, options))
  214. {
  215. // See if it can be direct played
  216. var directPlay = GetVideoDirectPlayProfile(options.Profile, item, videoStream, audioStream);
  217. if (directPlay != null)
  218. {
  219. playlistItem.PlayMethod = directPlay.Value;
  220. playlistItem.Container = item.Container;
  221. if (subtitleStream != null)
  222. {
  223. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options);
  224. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  225. playlistItem.SubtitleFormat = subtitleProfile.Format;
  226. }
  227. return playlistItem;
  228. }
  229. }
  230. // Can't direct play, find the transcoding profile
  231. TranscodingProfile transcodingProfile = null;
  232. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  233. {
  234. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  235. {
  236. transcodingProfile = i;
  237. break;
  238. }
  239. }
  240. if (transcodingProfile != null)
  241. {
  242. if (subtitleStream != null)
  243. {
  244. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options);
  245. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  246. playlistItem.SubtitleFormat = subtitleProfile.Format;
  247. }
  248. playlistItem.PlayMethod = PlayMethod.Transcode;
  249. playlistItem.Container = transcodingProfile.Container;
  250. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  251. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  252. playlistItem.AudioCodec = transcodingProfile.AudioCodec.Split(',')[0];
  253. playlistItem.VideoCodec = transcodingProfile.VideoCodec;
  254. playlistItem.Protocol = transcodingProfile.Protocol;
  255. playlistItem.AudioStreamIndex = audioStreamIndex;
  256. List<ProfileCondition> videoTranscodingConditions = new List<ProfileCondition>();
  257. foreach (CodecProfile i in options.Profile.CodecProfiles)
  258. {
  259. if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec))
  260. {
  261. foreach (ProfileCondition c in i.Conditions)
  262. {
  263. videoTranscodingConditions.Add(c);
  264. }
  265. break;
  266. }
  267. }
  268. ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
  269. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  270. foreach (CodecProfile i in options.Profile.CodecProfiles)
  271. {
  272. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(transcodingProfile.AudioCodec))
  273. {
  274. foreach (ProfileCondition c in i.Conditions)
  275. {
  276. audioTranscodingConditions.Add(c);
  277. }
  278. break;
  279. }
  280. }
  281. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  282. // Honor requested max channels
  283. if (options.MaxAudioChannels.HasValue)
  284. {
  285. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  286. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  287. }
  288. if (!playlistItem.AudioBitrate.HasValue)
  289. {
  290. playlistItem.AudioBitrate = GetAudioBitrate(playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec);
  291. }
  292. // Honor max rate
  293. if (maxBitrateSetting.HasValue)
  294. {
  295. int videoBitrate = maxBitrateSetting.Value;
  296. if (playlistItem.AudioBitrate.HasValue)
  297. {
  298. videoBitrate -= playlistItem.AudioBitrate.Value;
  299. }
  300. int currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  301. playlistItem.VideoBitrate = Math.Min(videoBitrate, currentValue);
  302. }
  303. }
  304. return playlistItem;
  305. }
  306. private int GetAudioBitrate(int? channels, string codec)
  307. {
  308. if (channels.HasValue)
  309. {
  310. if (channels.Value >= 5)
  311. {
  312. return 320000;
  313. }
  314. }
  315. return 128000;
  316. }
  317. private PlayMethod? GetVideoDirectPlayProfile(DeviceProfile profile,
  318. MediaSourceInfo mediaSource,
  319. MediaStream videoStream,
  320. MediaStream audioStream)
  321. {
  322. // See if it can be direct played
  323. DirectPlayProfile directPlay = null;
  324. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  325. {
  326. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  327. {
  328. directPlay = i;
  329. break;
  330. }
  331. }
  332. if (directPlay == null)
  333. {
  334. return null;
  335. }
  336. string container = mediaSource.Container;
  337. List<ProfileCondition> conditions = new List<ProfileCondition>();
  338. foreach (ContainerProfile i in profile.ContainerProfiles)
  339. {
  340. if (i.Type == DlnaProfileType.Video &&
  341. ListHelper.ContainsIgnoreCase(i.GetContainers(), container))
  342. {
  343. foreach (ProfileCondition c in i.Conditions)
  344. {
  345. conditions.Add(c);
  346. }
  347. }
  348. }
  349. ConditionProcessor conditionProcessor = new ConditionProcessor();
  350. int? width = videoStream == null ? null : videoStream.Width;
  351. int? height = videoStream == null ? null : videoStream.Height;
  352. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  353. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  354. double? videoLevel = videoStream == null ? null : videoStream.Level;
  355. string videoProfile = videoStream == null ? null : videoStream.Profile;
  356. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  357. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  358. bool? isCabac = videoStream == null ? null : videoStream.IsCabac;
  359. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  360. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  361. string audioProfile = audioStream == null ? null : audioStream.Profile;
  362. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  363. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  364. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  365. // Check container conditions
  366. foreach (ProfileCondition i in conditions)
  367. {
  368. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isCabac, refFrames))
  369. {
  370. return null;
  371. }
  372. }
  373. string videoCodec = videoStream == null ? null : videoStream.Codec;
  374. if (string.IsNullOrEmpty(videoCodec))
  375. {
  376. return null;
  377. }
  378. conditions = new List<ProfileCondition>();
  379. foreach (CodecProfile i in profile.CodecProfiles)
  380. {
  381. if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec))
  382. {
  383. foreach (ProfileCondition c in i.Conditions)
  384. {
  385. conditions.Add(c);
  386. }
  387. }
  388. }
  389. foreach (ProfileCondition i in conditions)
  390. {
  391. if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isCabac, refFrames))
  392. {
  393. return null;
  394. }
  395. }
  396. if (audioStream != null)
  397. {
  398. string audioCodec = audioStream.Codec;
  399. if (string.IsNullOrEmpty(audioCodec))
  400. {
  401. return null;
  402. }
  403. conditions = new List<ProfileCondition>();
  404. foreach (CodecProfile i in profile.CodecProfiles)
  405. {
  406. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec))
  407. {
  408. foreach (ProfileCondition c in i.Conditions)
  409. {
  410. conditions.Add(c);
  411. }
  412. }
  413. }
  414. foreach (ProfileCondition i in conditions)
  415. {
  416. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile))
  417. {
  418. return null;
  419. }
  420. }
  421. }
  422. if (mediaSource.Protocol == MediaProtocol.Http)
  423. {
  424. if (!profile.SupportsDirectRemoteContent)
  425. {
  426. return null;
  427. }
  428. if (mediaSource.RequiredHttpHeaders.Count > 0 && !profile.SupportsCustomHttpHeaders)
  429. {
  430. return null;
  431. }
  432. return PlayMethod.DirectPlay;
  433. }
  434. return PlayMethod.DirectStream;
  435. }
  436. private bool IsEligibleForDirectPlay(MediaSourceInfo item,
  437. int? maxBitrate,
  438. MediaStream subtitleStream,
  439. VideoOptions options)
  440. {
  441. if (subtitleStream != null)
  442. {
  443. if (!subtitleStream.IsTextSubtitleStream)
  444. {
  445. return false;
  446. }
  447. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options);
  448. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  449. {
  450. return false;
  451. }
  452. }
  453. return IsAudioEligibleForDirectPlay(item, maxBitrate);
  454. }
  455. private SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, VideoOptions options)
  456. {
  457. if (subtitleStream.IsTextSubtitleStream)
  458. {
  459. SubtitleProfile externalProfile = GetSubtitleProfile(options.Profile.SubtitleProfiles, SubtitleDeliveryMethod.External, _serverTextSubtitleOutputs);
  460. if (externalProfile != null)
  461. {
  462. return externalProfile;
  463. }
  464. SubtitleProfile embedProfile = GetSubtitleProfile(options.Profile.SubtitleProfiles, SubtitleDeliveryMethod.Embed, _serverTextSubtitleOutputs);
  465. if (embedProfile != null)
  466. {
  467. return embedProfile;
  468. }
  469. }
  470. return new SubtitleProfile
  471. {
  472. Method = SubtitleDeliveryMethod.Encode,
  473. Format = subtitleStream.Codec
  474. };
  475. }
  476. private SubtitleProfile GetSubtitleProfile(SubtitleProfile[] profiles, SubtitleDeliveryMethod method, string[] formats)
  477. {
  478. foreach (SubtitleProfile profile in profiles)
  479. {
  480. if (method == profile.Method && ListHelper.ContainsIgnoreCase(formats, profile.Format))
  481. {
  482. return profile;
  483. }
  484. }
  485. return null;
  486. }
  487. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
  488. {
  489. // Honor the max bitrate setting
  490. return !maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value);
  491. }
  492. private void ValidateInput(VideoOptions options)
  493. {
  494. ValidateAudioInput(options);
  495. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  496. {
  497. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  498. }
  499. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  500. {
  501. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  502. }
  503. }
  504. private void ValidateAudioInput(AudioOptions options)
  505. {
  506. if (string.IsNullOrEmpty(options.ItemId))
  507. {
  508. throw new ArgumentException("ItemId is required");
  509. }
  510. if (string.IsNullOrEmpty(options.DeviceId))
  511. {
  512. throw new ArgumentException("DeviceId is required");
  513. }
  514. if (options.Profile == null)
  515. {
  516. throw new ArgumentException("Profile is required");
  517. }
  518. if (options.MediaSources == null)
  519. {
  520. throw new ArgumentException("MediaSources is required");
  521. }
  522. }
  523. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  524. {
  525. foreach (ProfileCondition condition in conditions)
  526. {
  527. string value = condition.Value;
  528. if (string.IsNullOrEmpty(value))
  529. {
  530. continue;
  531. }
  532. // No way to express this
  533. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  534. {
  535. continue;
  536. }
  537. switch (condition.Property)
  538. {
  539. case ProfileConditionValue.AudioBitrate:
  540. {
  541. int num;
  542. if (IntHelper.TryParseCultureInvariant(value, out num))
  543. {
  544. item.AudioBitrate = num;
  545. }
  546. break;
  547. }
  548. case ProfileConditionValue.AudioChannels:
  549. {
  550. int num;
  551. if (IntHelper.TryParseCultureInvariant(value, out num))
  552. {
  553. item.MaxAudioChannels = num;
  554. }
  555. break;
  556. }
  557. case ProfileConditionValue.IsCabac:
  558. {
  559. bool val;
  560. if (BoolHelper.TryParseCultureInvariant(value, out val))
  561. {
  562. if (condition.Condition == ProfileConditionType.Equals)
  563. {
  564. item.Cabac = val;
  565. }
  566. else if (condition.Condition == ProfileConditionType.NotEquals)
  567. {
  568. item.Cabac = !val;
  569. }
  570. }
  571. break;
  572. }
  573. case ProfileConditionValue.IsAnamorphic:
  574. case ProfileConditionValue.AudioProfile:
  575. case ProfileConditionValue.Has64BitOffsets:
  576. case ProfileConditionValue.PacketLength:
  577. case ProfileConditionValue.VideoTimestamp:
  578. {
  579. // Not supported yet
  580. break;
  581. }
  582. case ProfileConditionValue.RefFrames:
  583. {
  584. int num;
  585. if (IntHelper.TryParseCultureInvariant(value, out num))
  586. {
  587. item.MaxRefFrames = num;
  588. }
  589. break;
  590. }
  591. case ProfileConditionValue.VideoBitDepth:
  592. {
  593. int num;
  594. if (IntHelper.TryParseCultureInvariant(value, out num))
  595. {
  596. item.MaxVideoBitDepth = num;
  597. }
  598. break;
  599. }
  600. case ProfileConditionValue.VideoProfile:
  601. {
  602. item.VideoProfile = (value ?? string.Empty).Split('|')[0];
  603. break;
  604. }
  605. case ProfileConditionValue.Height:
  606. {
  607. int num;
  608. if (IntHelper.TryParseCultureInvariant(value, out num))
  609. {
  610. item.MaxHeight = num;
  611. }
  612. break;
  613. }
  614. case ProfileConditionValue.VideoBitrate:
  615. {
  616. int num;
  617. if (IntHelper.TryParseCultureInvariant(value, out num))
  618. {
  619. item.VideoBitrate = num;
  620. }
  621. break;
  622. }
  623. case ProfileConditionValue.VideoFramerate:
  624. {
  625. float num;
  626. if (FloatHelper.TryParseCultureInvariant(value, out num))
  627. {
  628. item.MaxFramerate = num;
  629. }
  630. break;
  631. }
  632. case ProfileConditionValue.VideoLevel:
  633. {
  634. int num;
  635. if (IntHelper.TryParseCultureInvariant(value, out num))
  636. {
  637. item.VideoLevel = num;
  638. }
  639. break;
  640. }
  641. case ProfileConditionValue.Width:
  642. {
  643. int num;
  644. if (IntHelper.TryParseCultureInvariant(value, out num))
  645. {
  646. item.MaxWidth = num;
  647. }
  648. break;
  649. }
  650. default:
  651. throw new ArgumentException("Unrecognized ProfileConditionValue");
  652. }
  653. }
  654. }
  655. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  656. {
  657. if (profile.Container.Length > 0)
  658. {
  659. // Check container type
  660. string mediaContainer = item.Container ?? string.Empty;
  661. bool any = false;
  662. foreach (string i in profile.GetContainers())
  663. {
  664. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  665. {
  666. any = true;
  667. break;
  668. }
  669. }
  670. if (!any)
  671. {
  672. return false;
  673. }
  674. }
  675. return true;
  676. }
  677. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  678. {
  679. if (profile.Container.Length > 0)
  680. {
  681. // Check container type
  682. string mediaContainer = item.Container ?? string.Empty;
  683. bool any = false;
  684. foreach (string i in profile.GetContainers())
  685. {
  686. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  687. {
  688. any = true;
  689. break;
  690. }
  691. }
  692. if (!any)
  693. {
  694. return false;
  695. }
  696. }
  697. // Check video codec
  698. List<string> videoCodecs = profile.GetVideoCodecs();
  699. if (videoCodecs.Count > 0)
  700. {
  701. string videoCodec = videoStream == null ? null : videoStream.Codec;
  702. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  703. {
  704. return false;
  705. }
  706. }
  707. List<string> audioCodecs = profile.GetAudioCodecs();
  708. if (audioCodecs.Count > 0)
  709. {
  710. // Check audio codecs
  711. string audioCodec = audioStream == null ? null : audioStream.Codec;
  712. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  713. {
  714. return false;
  715. }
  716. }
  717. return true;
  718. }
  719. }
  720. }