StreamBuilder.cs 34 KB

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