StreamBuilder.cs 33 KB

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