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 = 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. List<PlayMethod> directPlayMethods = GetAudioDirectPlayMethods(item, options);
  121. if (directPlayMethods.Count > 0)
  122. {
  123. MediaStream audioStream = item.DefaultAudioStream;
  124. string audioCodec = audioStream == null ? null : audioStream.Codec;
  125. // Make sure audio codec profiles are satisfied
  126. if (!string.IsNullOrEmpty(audioCodec))
  127. {
  128. ConditionProcessor conditionProcessor = new ConditionProcessor();
  129. List<ProfileCondition> conditions = new List<ProfileCondition>();
  130. foreach (CodecProfile i in options.Profile.CodecProfiles)
  131. {
  132. if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec))
  133. {
  134. foreach (ProfileCondition c in i.Conditions)
  135. {
  136. conditions.Add(c);
  137. }
  138. }
  139. }
  140. int? audioChannels = audioStream.Channels;
  141. int? audioBitrate = audioStream.BitRate;
  142. bool all = true;
  143. foreach (ProfileCondition c in conditions)
  144. {
  145. if (!conditionProcessor.IsAudioConditionSatisfied(c, audioChannels, audioBitrate))
  146. {
  147. all = false;
  148. break;
  149. }
  150. }
  151. if (all)
  152. {
  153. if (item.Protocol == MediaProtocol.File &&
  154. directPlayMethods.Contains(PlayMethod.DirectPlay) &&
  155. _localPlayer.CanAccessFile(item.Path))
  156. {
  157. playlistItem.PlayMethod = PlayMethod.DirectPlay;
  158. }
  159. else if (item.Protocol == MediaProtocol.Http &&
  160. directPlayMethods.Contains(PlayMethod.DirectPlay) &&
  161. _localPlayer.CanAccessUrl(item.Path, item.RequiredHttpHeaders.Count > 0))
  162. {
  163. playlistItem.PlayMethod = PlayMethod.DirectPlay;
  164. }
  165. else if (directPlayMethods.Contains(PlayMethod.DirectStream))
  166. {
  167. playlistItem.PlayMethod = PlayMethod.DirectStream;
  168. }
  169. playlistItem.Container = item.Container;
  170. return playlistItem;
  171. }
  172. }
  173. }
  174. TranscodingProfile transcodingProfile = null;
  175. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  176. {
  177. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  178. {
  179. transcodingProfile = i;
  180. break;
  181. }
  182. }
  183. if (transcodingProfile != null)
  184. {
  185. if (!item.SupportsTranscoding)
  186. {
  187. return null;
  188. }
  189. playlistItem.PlayMethod = PlayMethod.Transcode;
  190. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  191. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  192. playlistItem.Container = transcodingProfile.Container;
  193. playlistItem.AudioCodec = transcodingProfile.AudioCodec;
  194. playlistItem.Protocol = transcodingProfile.Protocol;
  195. List<CodecProfile> audioCodecProfiles = new List<CodecProfile>();
  196. foreach (CodecProfile i in options.Profile.CodecProfiles)
  197. {
  198. if (i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec))
  199. {
  200. audioCodecProfiles.Add(i);
  201. }
  202. if (audioCodecProfiles.Count >= 1) break;
  203. }
  204. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  205. foreach (CodecProfile i in audioCodecProfiles)
  206. {
  207. foreach (ProfileCondition c in i.Conditions)
  208. {
  209. audioTranscodingConditions.Add(c);
  210. }
  211. }
  212. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  213. // Honor requested max channels
  214. if (options.MaxAudioChannels.HasValue)
  215. {
  216. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  217. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  218. }
  219. int configuredBitrate = options.AudioTranscodingBitrate ??
  220. (options.Context == EncodingContext.Static ? options.Profile.MusicSyncBitrate : options.Profile.MusicStreamingTranscodingBitrate) ??
  221. 128000;
  222. playlistItem.AudioBitrate = Math.Min(configuredBitrate, playlistItem.AudioBitrate ?? configuredBitrate);
  223. }
  224. return playlistItem;
  225. }
  226. private List<PlayMethod> GetAudioDirectPlayMethods(MediaSourceInfo item, AudioOptions options)
  227. {
  228. MediaStream audioStream = item.DefaultAudioStream;
  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 (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. int? audioStreamIndex = options.AudioStreamIndex ?? item.DefaultAudioStreamIndex;
  267. playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? item.DefaultSubtitleStreamIndex;
  268. MediaStream audioStream = audioStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Audio, audioStreamIndex.Value) : null;
  269. MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
  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, 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, 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. return PlayMethod.DirectStream;
  501. }
  502. private bool IsEligibleForDirectPlay(MediaSourceInfo item,
  503. int? maxBitrate,
  504. MediaStream subtitleStream,
  505. VideoOptions options)
  506. {
  507. if (subtitleStream != null)
  508. {
  509. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile, options.Context);
  510. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  511. {
  512. return false;
  513. }
  514. }
  515. return IsAudioEligibleForDirectPlay(item, maxBitrate);
  516. }
  517. public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, DeviceProfile deviceProfile, EncodingContext context)
  518. {
  519. // Look for an external profile that matches the stream type (text/graphical)
  520. foreach (SubtitleProfile profile in deviceProfile.SubtitleProfiles)
  521. {
  522. if (profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format))
  523. {
  524. if (subtitleStream.SupportsExternalStream)
  525. {
  526. return profile;
  527. }
  528. // For sync we can handle the longer extraction times
  529. if (context == EncodingContext.Static && subtitleStream.IsTextSubtitleStream)
  530. {
  531. return profile;
  532. }
  533. }
  534. }
  535. foreach (SubtitleProfile profile in deviceProfile.SubtitleProfiles)
  536. {
  537. if (profile.Method == SubtitleDeliveryMethod.Embed && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format))
  538. {
  539. return profile;
  540. }
  541. }
  542. return new SubtitleProfile
  543. {
  544. Method = SubtitleDeliveryMethod.Encode,
  545. Format = subtitleStream.Codec
  546. };
  547. }
  548. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
  549. {
  550. // Honor the max bitrate setting
  551. return !maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value);
  552. }
  553. private void ValidateInput(VideoOptions options)
  554. {
  555. ValidateAudioInput(options);
  556. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  557. {
  558. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  559. }
  560. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  561. {
  562. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  563. }
  564. }
  565. private void ValidateAudioInput(AudioOptions options)
  566. {
  567. if (string.IsNullOrEmpty(options.ItemId))
  568. {
  569. throw new ArgumentException("ItemId is required");
  570. }
  571. if (string.IsNullOrEmpty(options.DeviceId))
  572. {
  573. throw new ArgumentException("DeviceId is required");
  574. }
  575. if (options.Profile == null)
  576. {
  577. throw new ArgumentException("Profile is required");
  578. }
  579. if (options.MediaSources == null)
  580. {
  581. throw new ArgumentException("MediaSources is required");
  582. }
  583. }
  584. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  585. {
  586. foreach (ProfileCondition condition in conditions)
  587. {
  588. string value = condition.Value;
  589. if (string.IsNullOrEmpty(value))
  590. {
  591. continue;
  592. }
  593. // No way to express this
  594. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  595. {
  596. continue;
  597. }
  598. switch (condition.Property)
  599. {
  600. case ProfileConditionValue.AudioBitrate:
  601. {
  602. int num;
  603. if (IntHelper.TryParseCultureInvariant(value, out num))
  604. {
  605. item.AudioBitrate = num;
  606. }
  607. break;
  608. }
  609. case ProfileConditionValue.AudioChannels:
  610. {
  611. int num;
  612. if (IntHelper.TryParseCultureInvariant(value, out num))
  613. {
  614. item.MaxAudioChannels = num;
  615. }
  616. break;
  617. }
  618. case ProfileConditionValue.IsCabac:
  619. {
  620. bool val;
  621. if (BoolHelper.TryParseCultureInvariant(value, out val))
  622. {
  623. if (condition.Condition == ProfileConditionType.Equals)
  624. {
  625. item.Cabac = val;
  626. }
  627. else if (condition.Condition == ProfileConditionType.NotEquals)
  628. {
  629. item.Cabac = !val;
  630. }
  631. }
  632. break;
  633. }
  634. case ProfileConditionValue.IsAnamorphic:
  635. case ProfileConditionValue.AudioProfile:
  636. case ProfileConditionValue.Has64BitOffsets:
  637. case ProfileConditionValue.PacketLength:
  638. case ProfileConditionValue.VideoTimestamp:
  639. {
  640. // Not supported yet
  641. break;
  642. }
  643. case ProfileConditionValue.RefFrames:
  644. {
  645. int num;
  646. if (IntHelper.TryParseCultureInvariant(value, out num))
  647. {
  648. item.MaxRefFrames = num;
  649. }
  650. break;
  651. }
  652. case ProfileConditionValue.VideoBitDepth:
  653. {
  654. int num;
  655. if (IntHelper.TryParseCultureInvariant(value, out num))
  656. {
  657. item.MaxVideoBitDepth = num;
  658. }
  659. break;
  660. }
  661. case ProfileConditionValue.VideoProfile:
  662. {
  663. item.VideoProfile = (value ?? string.Empty).Split('|')[0];
  664. break;
  665. }
  666. case ProfileConditionValue.Height:
  667. {
  668. int num;
  669. if (IntHelper.TryParseCultureInvariant(value, out num))
  670. {
  671. item.MaxHeight = num;
  672. }
  673. break;
  674. }
  675. case ProfileConditionValue.VideoBitrate:
  676. {
  677. int num;
  678. if (IntHelper.TryParseCultureInvariant(value, out num))
  679. {
  680. item.VideoBitrate = num;
  681. }
  682. break;
  683. }
  684. case ProfileConditionValue.VideoFramerate:
  685. {
  686. float num;
  687. if (FloatHelper.TryParseCultureInvariant(value, out num))
  688. {
  689. item.MaxFramerate = num;
  690. }
  691. break;
  692. }
  693. case ProfileConditionValue.VideoLevel:
  694. {
  695. int num;
  696. if (IntHelper.TryParseCultureInvariant(value, out num))
  697. {
  698. item.VideoLevel = num;
  699. }
  700. break;
  701. }
  702. case ProfileConditionValue.Width:
  703. {
  704. int num;
  705. if (IntHelper.TryParseCultureInvariant(value, out num))
  706. {
  707. item.MaxWidth = num;
  708. }
  709. break;
  710. }
  711. default:
  712. throw new ArgumentException("Unrecognized ProfileConditionValue");
  713. }
  714. }
  715. }
  716. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  717. {
  718. if (profile.Container.Length > 0)
  719. {
  720. // Check container type
  721. string mediaContainer = item.Container ?? string.Empty;
  722. bool any = false;
  723. foreach (string i in profile.GetContainers())
  724. {
  725. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  726. {
  727. any = true;
  728. break;
  729. }
  730. }
  731. if (!any)
  732. {
  733. return false;
  734. }
  735. }
  736. return true;
  737. }
  738. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  739. {
  740. if (profile.Container.Length > 0)
  741. {
  742. // Check container type
  743. string mediaContainer = item.Container ?? string.Empty;
  744. bool any = false;
  745. foreach (string i in profile.GetContainers())
  746. {
  747. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  748. {
  749. any = true;
  750. break;
  751. }
  752. }
  753. if (!any)
  754. {
  755. return false;
  756. }
  757. }
  758. // Check video codec
  759. List<string> videoCodecs = profile.GetVideoCodecs();
  760. if (videoCodecs.Count > 0)
  761. {
  762. string videoCodec = videoStream == null ? null : videoStream.Codec;
  763. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  764. {
  765. return false;
  766. }
  767. }
  768. List<string> audioCodecs = profile.GetAudioCodecs();
  769. if (audioCodecs.Count > 0)
  770. {
  771. // Check audio codecs
  772. string audioCodec = audioStream == null ? null : audioStream.Codec;
  773. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  774. {
  775. return false;
  776. }
  777. }
  778. return true;
  779. }
  780. }
  781. }