StreamBuilder.cs 35 KB

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