StreamBuilder.cs 37 KB

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