StreamBuilder.cs 34 KB

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