StreamBuilder.cs 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.Extensions;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.MediaInfo;
  6. using MediaBrowser.Model.Session;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. namespace MediaBrowser.Model.Dlna
  12. {
  13. public class StreamBuilder
  14. {
  15. private readonly ILogger _logger;
  16. private readonly ITranscoderSupport _transcoderSupport;
  17. public StreamBuilder(ITranscoderSupport transcoderSupport, ILogger logger)
  18. {
  19. _transcoderSupport = transcoderSupport;
  20. _logger = logger;
  21. }
  22. public StreamBuilder(ILogger logger)
  23. : this(new FullTranscoderSupport(), logger)
  24. {
  25. }
  26. public StreamInfo BuildAudioItem(AudioOptions options)
  27. {
  28. ValidateAudioInput(options);
  29. var mediaSources = new List<MediaSourceInfo>();
  30. foreach (MediaSourceInfo i in options.MediaSources)
  31. {
  32. if (string.IsNullOrEmpty(options.MediaSourceId) ||
  33. StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
  34. {
  35. mediaSources.Add(i);
  36. }
  37. }
  38. var streams = new List<StreamInfo>();
  39. foreach (MediaSourceInfo i in mediaSources)
  40. {
  41. StreamInfo streamInfo = BuildAudioItem(i, options);
  42. if (streamInfo != null)
  43. {
  44. streams.Add(streamInfo);
  45. }
  46. }
  47. foreach (StreamInfo stream in streams)
  48. {
  49. stream.DeviceId = options.DeviceId;
  50. stream.DeviceProfileId = options.Profile.Id;
  51. }
  52. return GetOptimalStream(streams, options.GetMaxBitrate(true));
  53. }
  54. public StreamInfo BuildVideoItem(VideoOptions options)
  55. {
  56. ValidateInput(options);
  57. var mediaSources = new List<MediaSourceInfo>();
  58. foreach (MediaSourceInfo i in options.MediaSources)
  59. {
  60. if (string.IsNullOrEmpty(options.MediaSourceId) ||
  61. StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
  62. {
  63. mediaSources.Add(i);
  64. }
  65. }
  66. var streams = new List<StreamInfo>();
  67. foreach (MediaSourceInfo i in mediaSources)
  68. {
  69. StreamInfo streamInfo = BuildVideoItem(i, options);
  70. if (streamInfo != null)
  71. {
  72. streams.Add(streamInfo);
  73. }
  74. }
  75. foreach (StreamInfo stream in streams)
  76. {
  77. stream.DeviceId = options.DeviceId;
  78. stream.DeviceProfileId = options.Profile.Id;
  79. }
  80. return GetOptimalStream(streams, options.GetMaxBitrate(false));
  81. }
  82. private StreamInfo GetOptimalStream(List<StreamInfo> streams, long? maxBitrate)
  83. {
  84. var sorted = StreamInfoSorter.SortMediaSources(streams, maxBitrate);
  85. foreach (StreamInfo stream in sorted)
  86. {
  87. return stream;
  88. }
  89. return null;
  90. }
  91. private TranscodeReason? GetTranscodeReasonForFailedCondition(ProfileCondition condition)
  92. {
  93. switch (condition.Property)
  94. {
  95. case ProfileConditionValue.AudioBitrate:
  96. if (condition.Condition == ProfileConditionType.LessThanEqual)
  97. {
  98. return TranscodeReason.AudioBitrateNotSupported;
  99. }
  100. return TranscodeReason.AudioBitrateNotSupported;
  101. case ProfileConditionValue.AudioChannels:
  102. if (condition.Condition == ProfileConditionType.LessThanEqual)
  103. {
  104. return TranscodeReason.AudioChannelsNotSupported;
  105. }
  106. return TranscodeReason.AudioChannelsNotSupported;
  107. case ProfileConditionValue.AudioProfile:
  108. return TranscodeReason.AudioProfileNotSupported;
  109. case ProfileConditionValue.AudioSampleRate:
  110. return TranscodeReason.AudioSampleRateNotSupported;
  111. case ProfileConditionValue.Has64BitOffsets:
  112. // TODO
  113. return null;
  114. case ProfileConditionValue.Height:
  115. return TranscodeReason.VideoResolutionNotSupported;
  116. case ProfileConditionValue.IsAnamorphic:
  117. return TranscodeReason.AnamorphicVideoNotSupported;
  118. case ProfileConditionValue.IsAvc:
  119. // TODO
  120. return null;
  121. case ProfileConditionValue.IsInterlaced:
  122. return TranscodeReason.InterlacedVideoNotSupported;
  123. case ProfileConditionValue.IsSecondaryAudio:
  124. return TranscodeReason.SecondaryAudioNotSupported;
  125. case ProfileConditionValue.NumAudioStreams:
  126. // TODO
  127. return null;
  128. case ProfileConditionValue.NumVideoStreams:
  129. // TODO
  130. return null;
  131. case ProfileConditionValue.PacketLength:
  132. // TODO
  133. return null;
  134. case ProfileConditionValue.RefFrames:
  135. return TranscodeReason.RefFramesNotSupported;
  136. case ProfileConditionValue.VideoBitDepth:
  137. return TranscodeReason.VideoBitDepthNotSupported;
  138. case ProfileConditionValue.AudioBitDepth:
  139. return TranscodeReason.AudioBitDepthNotSupported;
  140. case ProfileConditionValue.VideoBitrate:
  141. return TranscodeReason.VideoBitrateNotSupported;
  142. case ProfileConditionValue.VideoCodecTag:
  143. return TranscodeReason.VideoCodecNotSupported;
  144. case ProfileConditionValue.VideoFramerate:
  145. return TranscodeReason.VideoFramerateNotSupported;
  146. case ProfileConditionValue.VideoLevel:
  147. return TranscodeReason.VideoLevelNotSupported;
  148. case ProfileConditionValue.VideoProfile:
  149. return TranscodeReason.VideoProfileNotSupported;
  150. case ProfileConditionValue.VideoTimestamp:
  151. // TODO
  152. return null;
  153. case ProfileConditionValue.Width:
  154. return TranscodeReason.VideoResolutionNotSupported;
  155. default:
  156. return null;
  157. }
  158. }
  159. public static string NormalizeMediaSourceFormatIntoSingleContainer(string inputContainer, DeviceProfile profile, DlnaProfileType type)
  160. {
  161. if (string.IsNullOrWhiteSpace(inputContainer))
  162. {
  163. return null;
  164. }
  165. var formats = ContainerProfile.SplitValue(inputContainer);
  166. if (formats.Length == 1)
  167. {
  168. return formats[0];
  169. }
  170. if (profile != null)
  171. {
  172. foreach (var format in formats)
  173. {
  174. foreach (var directPlayProfile in profile.DirectPlayProfiles)
  175. {
  176. if (directPlayProfile.Type == type)
  177. {
  178. if (directPlayProfile.SupportsContainer(format))
  179. {
  180. return format;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return formats[0];
  187. }
  188. private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
  189. {
  190. var transcodeReasons = new List<TranscodeReason>();
  191. StreamInfo playlistItem = new StreamInfo
  192. {
  193. ItemId = options.ItemId,
  194. MediaType = DlnaProfileType.Audio,
  195. MediaSource = item,
  196. RunTimeTicks = item.RunTimeTicks,
  197. Context = options.Context,
  198. DeviceProfile = options.Profile
  199. };
  200. if (options.ForceDirectPlay)
  201. {
  202. playlistItem.PlayMethod = PlayMethod.DirectPlay;
  203. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
  204. return playlistItem;
  205. }
  206. if (options.ForceDirectStream)
  207. {
  208. playlistItem.PlayMethod = PlayMethod.DirectStream;
  209. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
  210. return playlistItem;
  211. }
  212. MediaStream audioStream = item.GetDefaultAudioStream(null);
  213. var directPlayInfo = GetAudioDirectPlayMethods(item, audioStream, options);
  214. var directPlayMethods = directPlayInfo.Item1;
  215. transcodeReasons.AddRange(directPlayInfo.Item2);
  216. ConditionProcessor conditionProcessor = new ConditionProcessor();
  217. int? inputAudioChannels = audioStream == null ? null : audioStream.Channels;
  218. int? inputAudioBitrate = audioStream == null ? null : audioStream.BitDepth;
  219. int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  220. int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  221. if (directPlayMethods.Count > 0)
  222. {
  223. string audioCodec = audioStream == null ? null : audioStream.Codec;
  224. // Make sure audio codec profiles are satisfied
  225. if (!string.IsNullOrEmpty(audioCodec))
  226. {
  227. var conditions = new List<ProfileCondition>();
  228. foreach (CodecProfile i in options.Profile.CodecProfiles)
  229. {
  230. if (i.Type == CodecType.Audio && i.ContainsAnyCodec(audioCodec, item.Container))
  231. {
  232. bool applyConditions = true;
  233. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  234. {
  235. if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
  236. {
  237. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  238. applyConditions = false;
  239. break;
  240. }
  241. }
  242. if (applyConditions)
  243. {
  244. foreach (ProfileCondition c in i.Conditions)
  245. {
  246. conditions.Add(c);
  247. }
  248. }
  249. }
  250. }
  251. bool all = true;
  252. foreach (ProfileCondition c in conditions)
  253. {
  254. if (!conditionProcessor.IsAudioConditionSatisfied(c, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
  255. {
  256. LogConditionFailure(options.Profile, "AudioCodecProfile", c, item);
  257. var transcodeReason = GetTranscodeReasonForFailedCondition(c);
  258. if (transcodeReason.HasValue)
  259. {
  260. transcodeReasons.Add(transcodeReason.Value);
  261. }
  262. all = false;
  263. break;
  264. }
  265. }
  266. if (all)
  267. {
  268. if (directPlayMethods.Contains(PlayMethod.DirectStream))
  269. {
  270. playlistItem.PlayMethod = PlayMethod.DirectStream;
  271. }
  272. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
  273. return playlistItem;
  274. }
  275. }
  276. }
  277. TranscodingProfile transcodingProfile = null;
  278. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  279. {
  280. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  281. {
  282. if (_transcoderSupport.CanEncodeToAudioCodec(i.AudioCodec ?? i.Container))
  283. {
  284. transcodingProfile = i;
  285. break;
  286. }
  287. }
  288. }
  289. if (transcodingProfile != null)
  290. {
  291. if (!item.SupportsTranscoding)
  292. {
  293. return null;
  294. }
  295. playlistItem.PlayMethod = PlayMethod.Transcode;
  296. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  297. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  298. playlistItem.Container = transcodingProfile.Container;
  299. if (string.IsNullOrEmpty(transcodingProfile.AudioCodec))
  300. {
  301. playlistItem.AudioCodecs = new string[] { };
  302. }
  303. else
  304. {
  305. playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
  306. }
  307. playlistItem.SubProtocol = transcodingProfile.Protocol;
  308. var audioCodecProfiles = new List<CodecProfile>();
  309. foreach (CodecProfile i in options.Profile.CodecProfiles)
  310. {
  311. if (i.Type == CodecType.Audio && i.ContainsAnyCodec(transcodingProfile.AudioCodec, transcodingProfile.Container))
  312. {
  313. audioCodecProfiles.Add(i);
  314. }
  315. if (audioCodecProfiles.Count >= 1) break;
  316. }
  317. var audioTranscodingConditions = new List<ProfileCondition>();
  318. foreach (CodecProfile i in audioCodecProfiles)
  319. {
  320. bool applyConditions = true;
  321. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  322. {
  323. if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
  324. {
  325. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  326. applyConditions = false;
  327. break;
  328. }
  329. }
  330. if (applyConditions)
  331. {
  332. foreach (ProfileCondition c in i.Conditions)
  333. {
  334. audioTranscodingConditions.Add(c);
  335. }
  336. }
  337. }
  338. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, false);
  339. // Honor requested max channels
  340. if (options.MaxAudioChannels.HasValue)
  341. {
  342. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  343. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  344. }
  345. long transcodingBitrate = options.AudioTranscodingBitrate ??
  346. options.Profile.MusicStreamingTranscodingBitrate ??
  347. 128000;
  348. var configuredBitrate = options.GetMaxBitrate(true);
  349. if (configuredBitrate.HasValue)
  350. {
  351. transcodingBitrate = Math.Min(configuredBitrate.Value, transcodingBitrate);
  352. }
  353. var longBitrate = Math.Min(transcodingBitrate, playlistItem.AudioBitrate ?? transcodingBitrate);
  354. playlistItem.AudioBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
  355. }
  356. playlistItem.TranscodeReasons = transcodeReasons;
  357. return playlistItem;
  358. }
  359. private long? GetBitrateForDirectPlayCheck(MediaSourceInfo item, AudioOptions options, bool isAudio)
  360. {
  361. if (item.Protocol == MediaProtocol.File)
  362. {
  363. return options.Profile.MaxStaticBitrate;
  364. }
  365. return options.GetMaxBitrate(isAudio);
  366. }
  367. private Tuple<List<PlayMethod>, List<TranscodeReason>> GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
  368. {
  369. var transcodeReasons = new List<TranscodeReason>();
  370. DirectPlayProfile directPlayProfile = null;
  371. foreach (DirectPlayProfile i in options.Profile.DirectPlayProfiles)
  372. {
  373. if (i.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(i, item, audioStream))
  374. {
  375. directPlayProfile = i;
  376. break;
  377. }
  378. }
  379. var playMethods = new List<PlayMethod>();
  380. if (directPlayProfile != null)
  381. {
  382. // While options takes the network and other factors into account. Only applies to direct stream
  383. if (item.SupportsDirectStream)
  384. {
  385. if (IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate(true), PlayMethod.DirectStream))
  386. {
  387. if (options.EnableDirectStream)
  388. {
  389. playMethods.Add(PlayMethod.DirectStream);
  390. }
  391. }
  392. else
  393. {
  394. transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
  395. }
  396. }
  397. // The profile describes what the device supports
  398. // If device requirements are satisfied then allow both direct stream and direct play
  399. if (item.SupportsDirectPlay)
  400. {
  401. if (IsAudioEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), PlayMethod.DirectPlay))
  402. {
  403. if (options.EnableDirectPlay)
  404. {
  405. playMethods.Add(PlayMethod.DirectPlay);
  406. }
  407. }
  408. else
  409. {
  410. transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
  411. }
  412. }
  413. }
  414. else
  415. {
  416. transcodeReasons.InsertRange(0, GetTranscodeReasonsFromDirectPlayProfile(item, null, audioStream, options.Profile.DirectPlayProfiles));
  417. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  418. options.Profile.Name ?? "Unknown Profile",
  419. item.Path ?? "Unknown path");
  420. }
  421. if (playMethods.Count > 0)
  422. {
  423. transcodeReasons.Clear();
  424. }
  425. else
  426. {
  427. transcodeReasons = transcodeReasons.Distinct().ToList();
  428. }
  429. return new Tuple<List<PlayMethod>, List<TranscodeReason>>(playMethods, transcodeReasons);
  430. }
  431. private List<TranscodeReason> GetTranscodeReasonsFromDirectPlayProfile(MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream, IEnumerable<DirectPlayProfile> directPlayProfiles)
  432. {
  433. var list = new List<TranscodeReason>();
  434. var containerSupported = false;
  435. var audioSupported = false;
  436. var videoSupported = false;
  437. foreach (var profile in directPlayProfiles)
  438. {
  439. // Check container type
  440. if (profile.SupportsContainer(item.Container))
  441. {
  442. containerSupported = true;
  443. if (videoStream != null)
  444. {
  445. // Check video codec
  446. var videoCodecs = profile.GetVideoCodecs();
  447. if (videoCodecs.Length > 0)
  448. {
  449. string videoCodec = videoStream.Codec;
  450. if (!string.IsNullOrEmpty(videoCodec) && ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  451. {
  452. videoSupported = true;
  453. }
  454. }
  455. else
  456. {
  457. videoSupported = true;
  458. }
  459. }
  460. if (audioStream != null)
  461. {
  462. // Check audio codec
  463. var audioCodecs = profile.GetAudioCodecs();
  464. if (audioCodecs.Length > 0)
  465. {
  466. string audioCodec = audioStream.Codec;
  467. if (!string.IsNullOrEmpty(audioCodec) && ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  468. {
  469. audioSupported = true;
  470. }
  471. }
  472. else
  473. {
  474. audioSupported = true;
  475. }
  476. }
  477. }
  478. }
  479. if (!containerSupported)
  480. {
  481. list.Add(TranscodeReason.ContainerNotSupported);
  482. }
  483. if (videoStream != null && !videoSupported)
  484. {
  485. list.Add(TranscodeReason.VideoCodecNotSupported);
  486. }
  487. if (audioStream != null && !audioSupported)
  488. {
  489. list.Add(TranscodeReason.AudioCodecNotSupported);
  490. }
  491. return list;
  492. }
  493. private int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles)
  494. {
  495. int highestScore = -1;
  496. foreach (MediaStream stream in item.MediaStreams)
  497. {
  498. if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue)
  499. {
  500. if (stream.Score.Value > highestScore)
  501. {
  502. highestScore = stream.Score.Value;
  503. }
  504. }
  505. }
  506. var topStreams = new List<MediaStream>();
  507. foreach (MediaStream stream in item.MediaStreams)
  508. {
  509. if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue && stream.Score.Value == highestScore)
  510. {
  511. topStreams.Add(stream);
  512. }
  513. }
  514. // If multiple streams have an equal score, try to pick the most efficient one
  515. if (topStreams.Count > 1)
  516. {
  517. foreach (MediaStream stream in topStreams)
  518. {
  519. foreach (SubtitleProfile profile in subtitleProfiles)
  520. {
  521. if (profile.Method == SubtitleDeliveryMethod.External && StringHelper.EqualsIgnoreCase(profile.Format, stream.Codec))
  522. {
  523. return stream.Index;
  524. }
  525. }
  526. }
  527. }
  528. // If no optimization panned out, just use the original default
  529. return item.DefaultSubtitleStreamIndex;
  530. }
  531. private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
  532. {
  533. if (item == null)
  534. {
  535. throw new ArgumentNullException("item");
  536. }
  537. var transcodeReasons = new List<TranscodeReason>();
  538. StreamInfo playlistItem = new StreamInfo
  539. {
  540. ItemId = options.ItemId,
  541. MediaType = DlnaProfileType.Video,
  542. MediaSource = item,
  543. RunTimeTicks = item.RunTimeTicks,
  544. Context = options.Context,
  545. DeviceProfile = options.Profile
  546. };
  547. playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? GetDefaultSubtitleStreamIndex(item, options.Profile.SubtitleProfiles);
  548. MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
  549. MediaStream audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
  550. int? audioStreamIndex = null;
  551. if (audioStream != null)
  552. {
  553. audioStreamIndex = audioStream.Index;
  554. }
  555. MediaStream videoStream = item.VideoStream;
  556. // TODO: This doesn't accout for situation of device being able to handle media bitrate, but wifi connection not fast enough
  557. var directPlayEligibilityResult = IsEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), subtitleStream, options, PlayMethod.DirectPlay);
  558. var directStreamEligibilityResult = IsEligibleForDirectPlay(item, options.GetMaxBitrate(false), subtitleStream, options, PlayMethod.DirectStream);
  559. bool isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || directPlayEligibilityResult.Item1);
  560. bool isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || directStreamEligibilityResult.Item1);
  561. _logger.Info("Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
  562. options.Profile.Name ?? "Unknown Profile",
  563. item.Path ?? "Unknown path",
  564. isEligibleForDirectPlay,
  565. isEligibleForDirectStream);
  566. if (isEligibleForDirectPlay || isEligibleForDirectStream)
  567. {
  568. // See if it can be direct played
  569. var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, isEligibleForDirectPlay, isEligibleForDirectStream);
  570. var directPlay = directPlayInfo.Item1;
  571. if (directPlay != null)
  572. {
  573. playlistItem.PlayMethod = directPlay.Value;
  574. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Video);
  575. if (subtitleStream != null)
  576. {
  577. SubtitleProfile subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, directPlay.Value, _transcoderSupport, null, null);
  578. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  579. playlistItem.SubtitleFormat = subtitleProfile.Format;
  580. }
  581. return playlistItem;
  582. }
  583. transcodeReasons.AddRange(directPlayInfo.Item2);
  584. }
  585. if (directPlayEligibilityResult.Item2.HasValue)
  586. {
  587. transcodeReasons.Add(directPlayEligibilityResult.Item2.Value);
  588. }
  589. if (directStreamEligibilityResult.Item2.HasValue)
  590. {
  591. transcodeReasons.Add(directStreamEligibilityResult.Item2.Value);
  592. }
  593. // Can't direct play, find the transcoding profile
  594. TranscodingProfile transcodingProfile = null;
  595. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  596. {
  597. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  598. {
  599. transcodingProfile = i;
  600. break;
  601. }
  602. }
  603. if (transcodingProfile != null)
  604. {
  605. if (!item.SupportsTranscoding)
  606. {
  607. return null;
  608. }
  609. if (subtitleStream != null)
  610. {
  611. SubtitleProfile subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.Transcode, _transcoderSupport, transcodingProfile.Protocol, transcodingProfile.Container);
  612. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  613. playlistItem.SubtitleFormat = subtitleProfile.Format;
  614. playlistItem.SubtitleCodecs = new[] { subtitleProfile.Format };
  615. }
  616. playlistItem.PlayMethod = PlayMethod.Transcode;
  617. playlistItem.Container = transcodingProfile.Container;
  618. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  619. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  620. playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
  621. playlistItem.VideoCodecs = transcodingProfile.VideoCodec.Split(',');
  622. playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
  623. playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
  624. playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames;
  625. if (transcodingProfile.MinSegments > 0)
  626. {
  627. playlistItem.MinSegments = transcodingProfile.MinSegments;
  628. }
  629. if (transcodingProfile.SegmentLength > 0)
  630. {
  631. playlistItem.SegmentLength = transcodingProfile.SegmentLength;
  632. }
  633. if (!string.IsNullOrEmpty(transcodingProfile.MaxAudioChannels))
  634. {
  635. int transcodingMaxAudioChannels;
  636. if (int.TryParse(transcodingProfile.MaxAudioChannels, NumberStyles.Any, CultureInfo.InvariantCulture, out transcodingMaxAudioChannels))
  637. {
  638. playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
  639. }
  640. }
  641. playlistItem.SubProtocol = transcodingProfile.Protocol;
  642. playlistItem.AudioStreamIndex = audioStreamIndex;
  643. ConditionProcessor conditionProcessor = new ConditionProcessor();
  644. var isFirstAppliedCodecProfile = true;
  645. foreach (CodecProfile i in options.Profile.CodecProfiles)
  646. {
  647. if (i.Type == CodecType.Video && i.ContainsAnyCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
  648. {
  649. bool applyConditions = true;
  650. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  651. {
  652. int? width = videoStream == null ? null : videoStream.Width;
  653. int? height = videoStream == null ? null : videoStream.Height;
  654. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  655. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  656. double? videoLevel = videoStream == null ? null : videoStream.Level;
  657. string videoProfile = videoStream == null ? null : videoStream.Profile;
  658. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  659. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  660. bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
  661. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  662. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  663. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
  664. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  665. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  666. int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
  667. int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
  668. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  669. {
  670. LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
  671. applyConditions = false;
  672. break;
  673. }
  674. }
  675. if (applyConditions)
  676. {
  677. var transcodingVideoCodecs = ContainerProfile.SplitValue(transcodingProfile.VideoCodec);
  678. foreach (var transcodingVideoCodec in transcodingVideoCodecs)
  679. {
  680. if (i.ContainsAnyCodec(transcodingVideoCodec, transcodingProfile.Container))
  681. {
  682. ApplyTranscodingConditions(playlistItem, i.Conditions, transcodingVideoCodec, !isFirstAppliedCodecProfile);
  683. isFirstAppliedCodecProfile = false;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. var audioTranscodingConditions = new List<ProfileCondition>();
  690. foreach (CodecProfile i in options.Profile.CodecProfiles)
  691. {
  692. if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(playlistItem.TargetAudioCodec, transcodingProfile.Container))
  693. {
  694. bool applyConditions = true;
  695. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  696. {
  697. bool? isSecondaryAudio = audioStream == null ? null : item.IsSecondaryAudio(audioStream);
  698. int? inputAudioBitrate = audioStream == null ? null : audioStream.BitRate;
  699. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  700. string audioProfile = audioStream == null ? null : audioStream.Profile;
  701. int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  702. int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  703. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, audioProfile, isSecondaryAudio))
  704. {
  705. LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
  706. applyConditions = false;
  707. break;
  708. }
  709. }
  710. if (applyConditions)
  711. {
  712. foreach (ProfileCondition c in i.Conditions)
  713. {
  714. audioTranscodingConditions.Add(c);
  715. }
  716. break;
  717. }
  718. }
  719. }
  720. // Honor requested max channels
  721. if (options.MaxAudioChannels.HasValue)
  722. {
  723. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  724. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  725. }
  726. int audioBitrate = GetAudioBitrate(playlistItem.SubProtocol, options.GetMaxBitrate(false), playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec, audioStream);
  727. playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
  728. var maxBitrateSetting = options.GetMaxBitrate(false);
  729. // Honor max rate
  730. if (maxBitrateSetting.HasValue)
  731. {
  732. var videoBitrate = maxBitrateSetting.Value;
  733. if (playlistItem.AudioBitrate.HasValue)
  734. {
  735. videoBitrate -= playlistItem.AudioBitrate.Value;
  736. }
  737. // Make sure the video bitrate is lower than bitrate settings but at least 64k
  738. long currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  739. var longBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
  740. playlistItem.VideoBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
  741. }
  742. // Do this after initial values are set to account for greater than/less than conditions
  743. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, false);
  744. }
  745. playlistItem.TranscodeReasons = transcodeReasons;
  746. return playlistItem;
  747. }
  748. private int GetDefaultAudioBitrateIfUnknown(MediaStream audioStream)
  749. {
  750. if ((audioStream.Channels ?? 0) >= 6)
  751. {
  752. return 384000;
  753. }
  754. return 192000;
  755. }
  756. private int GetAudioBitrate(string subProtocol, long? maxTotalBitrate, int? targetAudioChannels, string[] targetAudioCodecs, MediaStream audioStream)
  757. {
  758. var targetAudioCodec = targetAudioCodecs.Length == 0 ? null : targetAudioCodecs[0];
  759. int defaultBitrate = audioStream == null ? 192000 : audioStream.BitRate ?? GetDefaultAudioBitrateIfUnknown(audioStream);
  760. // Reduce the bitrate if we're downmixing
  761. if (targetAudioChannels.HasValue && audioStream != null && audioStream.Channels.HasValue && targetAudioChannels.Value < audioStream.Channels.Value)
  762. {
  763. defaultBitrate = StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3") ? 192000 : 128000;
  764. }
  765. if (StringHelper.EqualsIgnoreCase(subProtocol, "hls"))
  766. {
  767. defaultBitrate = Math.Min(384000, defaultBitrate);
  768. }
  769. else
  770. {
  771. defaultBitrate = Math.Min(448000, defaultBitrate);
  772. }
  773. int encoderAudioBitrateLimit = int.MaxValue;
  774. if (audioStream != null)
  775. {
  776. // Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
  777. // Any attempts to transcode over 64k will fail
  778. if (audioStream.Channels.HasValue &&
  779. audioStream.Channels.Value == 1)
  780. {
  781. if ((audioStream.BitRate ?? 0) < 64000)
  782. {
  783. encoderAudioBitrateLimit = 64000;
  784. }
  785. }
  786. }
  787. if (maxTotalBitrate.HasValue)
  788. {
  789. if (maxTotalBitrate.Value < 640000)
  790. {
  791. defaultBitrate = Math.Min(128000, defaultBitrate);
  792. }
  793. }
  794. return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
  795. }
  796. private Tuple<PlayMethod?, List<TranscodeReason>> GetVideoDirectPlayProfile(VideoOptions options,
  797. MediaSourceInfo mediaSource,
  798. MediaStream videoStream,
  799. MediaStream audioStream,
  800. bool isEligibleForDirectPlay,
  801. bool isEligibleForDirectStream)
  802. {
  803. DeviceProfile profile = options.Profile;
  804. if (options.ForceDirectPlay)
  805. {
  806. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectPlay, new List<TranscodeReason>());
  807. }
  808. if (options.ForceDirectStream)
  809. {
  810. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
  811. }
  812. if (videoStream == null)
  813. {
  814. _logger.Info("Profile: {0}, Cannot direct stream with no known video stream. Path: {1}",
  815. profile.Name ?? "Unknown Profile",
  816. mediaSource.Path ?? "Unknown path");
  817. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownVideoStreamInfo });
  818. }
  819. // See if it can be direct played
  820. DirectPlayProfile directPlay = null;
  821. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  822. {
  823. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  824. {
  825. directPlay = i;
  826. break;
  827. }
  828. }
  829. if (directPlay == null)
  830. {
  831. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  832. profile.Name ?? "Unknown Profile",
  833. mediaSource.Path ?? "Unknown path");
  834. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, GetTranscodeReasonsFromDirectPlayProfile(mediaSource, videoStream, audioStream, profile.DirectPlayProfiles));
  835. }
  836. string container = mediaSource.Container;
  837. var conditions = new List<ProfileCondition>();
  838. foreach (ContainerProfile i in profile.ContainerProfiles)
  839. {
  840. if (i.Type == DlnaProfileType.Video &&
  841. i.ContainsContainer(container))
  842. {
  843. foreach (ProfileCondition c in i.Conditions)
  844. {
  845. conditions.Add(c);
  846. }
  847. }
  848. }
  849. ConditionProcessor conditionProcessor = new ConditionProcessor();
  850. int? width = videoStream == null ? null : videoStream.Width;
  851. int? height = videoStream == null ? null : videoStream.Height;
  852. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  853. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  854. double? videoLevel = videoStream == null ? null : videoStream.Level;
  855. string videoProfile = videoStream == null ? null : videoStream.Profile;
  856. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  857. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  858. bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
  859. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  860. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  861. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  862. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  863. string audioProfile = audioStream == null ? null : audioStream.Profile;
  864. int? audioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  865. int? audioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  866. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  867. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  868. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  869. int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
  870. int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
  871. // Check container conditions
  872. foreach (ProfileCondition i in conditions)
  873. {
  874. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  875. {
  876. LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
  877. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  878. var transcodeReasons = transcodeReason.HasValue
  879. ? new List<TranscodeReason> { transcodeReason.Value }
  880. : new List<TranscodeReason> { };
  881. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  882. }
  883. }
  884. string videoCodec = videoStream == null ? null : videoStream.Codec;
  885. conditions = new List<ProfileCondition>();
  886. foreach (CodecProfile i in profile.CodecProfiles)
  887. {
  888. if (i.Type == CodecType.Video && i.ContainsAnyCodec(videoCodec, container))
  889. {
  890. bool applyConditions = true;
  891. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  892. {
  893. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  894. {
  895. LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
  896. applyConditions = false;
  897. break;
  898. }
  899. }
  900. if (applyConditions)
  901. {
  902. foreach (ProfileCondition c in i.Conditions)
  903. {
  904. conditions.Add(c);
  905. }
  906. }
  907. }
  908. }
  909. foreach (ProfileCondition i in conditions)
  910. {
  911. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  912. {
  913. LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
  914. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  915. var transcodeReasons = transcodeReason.HasValue
  916. ? new List<TranscodeReason> { transcodeReason.Value }
  917. : new List<TranscodeReason> { };
  918. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  919. }
  920. }
  921. if (audioStream != null)
  922. {
  923. string audioCodec = audioStream.Codec;
  924. if (string.IsNullOrEmpty(audioCodec))
  925. {
  926. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown audio codec. Path: {1}",
  927. profile.Name ?? "Unknown Profile",
  928. mediaSource.Path ?? "Unknown path");
  929. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownAudioStreamInfo });
  930. }
  931. conditions = new List<ProfileCondition>();
  932. bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
  933. foreach (CodecProfile i in profile.CodecProfiles)
  934. {
  935. if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(audioCodec, container))
  936. {
  937. bool applyConditions = true;
  938. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  939. {
  940. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  941. {
  942. LogConditionFailure(profile, "VideoAudioCodecProfile.ApplyConditions", applyCondition, mediaSource);
  943. applyConditions = false;
  944. break;
  945. }
  946. }
  947. if (applyConditions)
  948. {
  949. foreach (ProfileCondition c in i.Conditions)
  950. {
  951. conditions.Add(c);
  952. }
  953. }
  954. }
  955. }
  956. foreach (ProfileCondition i in conditions)
  957. {
  958. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  959. {
  960. LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
  961. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  962. var transcodeReasons = transcodeReason.HasValue
  963. ? new List<TranscodeReason> { transcodeReason.Value }
  964. : new List<TranscodeReason> { };
  965. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  966. }
  967. }
  968. }
  969. if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
  970. {
  971. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
  972. }
  973. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.ContainerBitrateExceedsLimit });
  974. }
  975. private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
  976. {
  977. _logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
  978. type,
  979. profile.Name ?? "Unknown Profile",
  980. condition.Property,
  981. condition.Condition,
  982. condition.Value ?? string.Empty,
  983. condition.IsRequired,
  984. mediaSource.Path ?? "Unknown path");
  985. }
  986. private Tuple<bool, TranscodeReason?> IsEligibleForDirectPlay(MediaSourceInfo item,
  987. long? maxBitrate,
  988. MediaStream subtitleStream,
  989. VideoOptions options,
  990. PlayMethod playMethod)
  991. {
  992. if (subtitleStream != null)
  993. {
  994. SubtitleProfile subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, playMethod, _transcoderSupport, null, null);
  995. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  996. {
  997. _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
  998. return new Tuple<bool, TranscodeReason?>(false, TranscodeReason.SubtitleCodecNotSupported);
  999. }
  1000. }
  1001. var result = IsAudioEligibleForDirectPlay(item, maxBitrate, playMethod);
  1002. if (result)
  1003. {
  1004. return new Tuple<bool, TranscodeReason?>(result, null);
  1005. }
  1006. return new Tuple<bool, TranscodeReason?>(result, TranscodeReason.ContainerBitrateExceedsLimit);
  1007. }
  1008. public static SubtitleProfile GetSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, string transcodingSubProtocol, string transcodingContainer)
  1009. {
  1010. if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
  1011. {
  1012. // Look for supported embedded subs of the same format
  1013. foreach (SubtitleProfile profile in subtitleProfiles)
  1014. {
  1015. if (!profile.SupportsLanguage(subtitleStream.Language))
  1016. {
  1017. continue;
  1018. }
  1019. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1020. {
  1021. continue;
  1022. }
  1023. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1024. {
  1025. continue;
  1026. }
  1027. if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
  1028. {
  1029. return profile;
  1030. }
  1031. }
  1032. // Look for supported embedded subs of a convertible format
  1033. foreach (SubtitleProfile profile in subtitleProfiles)
  1034. {
  1035. if (!profile.SupportsLanguage(subtitleStream.Language))
  1036. {
  1037. continue;
  1038. }
  1039. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1040. {
  1041. continue;
  1042. }
  1043. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1044. {
  1045. continue;
  1046. }
  1047. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1048. {
  1049. return profile;
  1050. }
  1051. }
  1052. }
  1053. // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
  1054. return GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, false) ??
  1055. GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, true) ??
  1056. new SubtitleProfile
  1057. {
  1058. Method = SubtitleDeliveryMethod.Encode,
  1059. Format = subtitleStream.Codec
  1060. };
  1061. }
  1062. private static bool IsSubtitleEmbedSupported(MediaStream subtitleStream, SubtitleProfile subtitleProfile, string transcodingSubProtocol, string transcodingContainer)
  1063. {
  1064. if (!string.IsNullOrWhiteSpace(transcodingContainer))
  1065. {
  1066. var normalizedContainers = ContainerProfile.SplitValue(transcodingContainer);
  1067. if (ContainerProfile.ContainsContainer(normalizedContainers, "ts"))
  1068. {
  1069. return false;
  1070. }
  1071. if (ContainerProfile.ContainsContainer(normalizedContainers, "mpegts"))
  1072. {
  1073. return false;
  1074. }
  1075. if (ContainerProfile.ContainsContainer(normalizedContainers, "mp4"))
  1076. {
  1077. return false;
  1078. }
  1079. if (ContainerProfile.ContainsContainer(normalizedContainers, "mkv") ||
  1080. ContainerProfile.ContainsContainer(normalizedContainers, "matroska"))
  1081. {
  1082. return true;
  1083. }
  1084. }
  1085. return false;
  1086. }
  1087. private static SubtitleProfile GetExternalSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, bool allowConversion)
  1088. {
  1089. foreach (SubtitleProfile profile in subtitleProfiles)
  1090. {
  1091. if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
  1092. {
  1093. continue;
  1094. }
  1095. if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
  1096. {
  1097. continue;
  1098. }
  1099. if (!profile.SupportsLanguage(subtitleStream.Language))
  1100. {
  1101. continue;
  1102. }
  1103. if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec))
  1104. {
  1105. continue;
  1106. }
  1107. if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
  1108. (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
  1109. {
  1110. bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
  1111. if (!requiresConversion)
  1112. {
  1113. return profile;
  1114. }
  1115. if (!allowConversion)
  1116. {
  1117. continue;
  1118. }
  1119. // TODO: Build this into subtitleStream.SupportsExternalStream
  1120. if (mediaSource.IsInfiniteStream)
  1121. {
  1122. continue;
  1123. }
  1124. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1125. {
  1126. return profile;
  1127. }
  1128. }
  1129. }
  1130. return null;
  1131. }
  1132. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long? maxBitrate, PlayMethod playMethod)
  1133. {
  1134. // Don't restrict by bitrate if coming from an external domain
  1135. if (item.IsRemote)
  1136. {
  1137. return true;
  1138. }
  1139. if (!maxBitrate.HasValue)
  1140. {
  1141. _logger.Info("Cannot " + playMethod + " due to unknown supported bitrate");
  1142. return false;
  1143. }
  1144. if (!item.Bitrate.HasValue)
  1145. {
  1146. _logger.Info("Cannot " + playMethod + " due to unknown content bitrate");
  1147. return false;
  1148. }
  1149. if (item.Bitrate.Value > maxBitrate.Value)
  1150. {
  1151. _logger.Info("Bitrate exceeds " + playMethod + " limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), maxBitrate.Value.ToString(CultureInfo.InvariantCulture));
  1152. return false;
  1153. }
  1154. return true;
  1155. }
  1156. private void ValidateInput(VideoOptions options)
  1157. {
  1158. ValidateAudioInput(options);
  1159. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1160. {
  1161. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  1162. }
  1163. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1164. {
  1165. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  1166. }
  1167. }
  1168. private void ValidateAudioInput(AudioOptions options)
  1169. {
  1170. if (string.IsNullOrEmpty(options.ItemId))
  1171. {
  1172. throw new ArgumentException("ItemId is required");
  1173. }
  1174. if (string.IsNullOrEmpty(options.DeviceId))
  1175. {
  1176. throw new ArgumentException("DeviceId is required");
  1177. }
  1178. if (options.Profile == null)
  1179. {
  1180. throw new ArgumentException("Profile is required");
  1181. }
  1182. if (options.MediaSources == null)
  1183. {
  1184. throw new ArgumentException("MediaSources is required");
  1185. }
  1186. }
  1187. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string qualifier, bool qualifiedOnly)
  1188. {
  1189. foreach (ProfileCondition condition in conditions)
  1190. {
  1191. string value = condition.Value;
  1192. if (string.IsNullOrEmpty(value))
  1193. {
  1194. continue;
  1195. }
  1196. // No way to express this
  1197. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1198. {
  1199. continue;
  1200. }
  1201. switch (condition.Property)
  1202. {
  1203. case ProfileConditionValue.AudioBitrate:
  1204. {
  1205. if (qualifiedOnly)
  1206. {
  1207. continue;
  1208. }
  1209. int num;
  1210. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1211. {
  1212. if (condition.Condition == ProfileConditionType.Equals)
  1213. {
  1214. item.AudioBitrate = num;
  1215. }
  1216. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1217. {
  1218. item.AudioBitrate = Math.Min(num, item.AudioBitrate ?? num);
  1219. }
  1220. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1221. {
  1222. item.AudioBitrate = Math.Max(num, item.AudioBitrate ?? num);
  1223. }
  1224. }
  1225. break;
  1226. }
  1227. case ProfileConditionValue.AudioChannels:
  1228. {
  1229. if (qualifiedOnly)
  1230. {
  1231. continue;
  1232. }
  1233. int num;
  1234. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1235. {
  1236. if (condition.Condition == ProfileConditionType.Equals)
  1237. {
  1238. item.MaxAudioChannels = num;
  1239. }
  1240. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1241. {
  1242. item.MaxAudioChannels = Math.Min(num, item.MaxAudioChannels ?? num);
  1243. }
  1244. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1245. {
  1246. item.MaxAudioChannels = Math.Max(num, item.MaxAudioChannels ?? num);
  1247. }
  1248. }
  1249. break;
  1250. }
  1251. case ProfileConditionValue.IsAvc:
  1252. {
  1253. if (qualifiedOnly)
  1254. {
  1255. continue;
  1256. }
  1257. bool isAvc;
  1258. if (bool.TryParse(value, out isAvc))
  1259. {
  1260. if (isAvc && condition.Condition == ProfileConditionType.Equals)
  1261. {
  1262. item.RequireAvc = true;
  1263. }
  1264. else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
  1265. {
  1266. item.RequireAvc = true;
  1267. }
  1268. }
  1269. break;
  1270. }
  1271. case ProfileConditionValue.IsAnamorphic:
  1272. {
  1273. if (qualifiedOnly)
  1274. {
  1275. continue;
  1276. }
  1277. bool isAnamorphic;
  1278. if (bool.TryParse(value, out isAnamorphic))
  1279. {
  1280. if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
  1281. {
  1282. item.RequireNonAnamorphic = true;
  1283. }
  1284. else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
  1285. {
  1286. item.RequireNonAnamorphic = true;
  1287. }
  1288. }
  1289. break;
  1290. }
  1291. case ProfileConditionValue.IsInterlaced:
  1292. {
  1293. if (string.IsNullOrWhiteSpace(qualifier))
  1294. {
  1295. continue;
  1296. }
  1297. bool isInterlaced;
  1298. if (bool.TryParse(value, out isInterlaced))
  1299. {
  1300. if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
  1301. {
  1302. item.SetOption(qualifier, "deinterlace", "true");
  1303. }
  1304. else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
  1305. {
  1306. item.SetOption(qualifier, "deinterlace", "true");
  1307. }
  1308. }
  1309. break;
  1310. }
  1311. case ProfileConditionValue.AudioProfile:
  1312. case ProfileConditionValue.Has64BitOffsets:
  1313. case ProfileConditionValue.PacketLength:
  1314. case ProfileConditionValue.NumAudioStreams:
  1315. case ProfileConditionValue.NumVideoStreams:
  1316. case ProfileConditionValue.IsSecondaryAudio:
  1317. case ProfileConditionValue.VideoTimestamp:
  1318. {
  1319. // Not supported yet
  1320. break;
  1321. }
  1322. case ProfileConditionValue.RefFrames:
  1323. {
  1324. if (string.IsNullOrWhiteSpace(qualifier))
  1325. {
  1326. continue;
  1327. }
  1328. int num;
  1329. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1330. {
  1331. if (condition.Condition == ProfileConditionType.Equals)
  1332. {
  1333. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(num));
  1334. }
  1335. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1336. {
  1337. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetRefFrames(qualifier) ?? num)));
  1338. }
  1339. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1340. {
  1341. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetRefFrames(qualifier) ?? num)));
  1342. }
  1343. }
  1344. break;
  1345. }
  1346. case ProfileConditionValue.VideoBitDepth:
  1347. {
  1348. if (qualifiedOnly)
  1349. {
  1350. continue;
  1351. }
  1352. int num;
  1353. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1354. {
  1355. if (condition.Condition == ProfileConditionType.Equals)
  1356. {
  1357. item.MaxVideoBitDepth = num;
  1358. }
  1359. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1360. {
  1361. item.MaxVideoBitDepth = Math.Min(num, item.MaxVideoBitDepth ?? num);
  1362. }
  1363. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1364. {
  1365. item.MaxVideoBitDepth = Math.Max(num, item.MaxVideoBitDepth ?? num);
  1366. }
  1367. }
  1368. break;
  1369. }
  1370. case ProfileConditionValue.VideoProfile:
  1371. {
  1372. if (string.IsNullOrWhiteSpace(qualifier))
  1373. {
  1374. continue;
  1375. }
  1376. if (!string.IsNullOrWhiteSpace(value))
  1377. {
  1378. // change from split by | to comma
  1379. // strip spaces to avoid having to encode
  1380. var values = value
  1381. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  1382. item.SetOption(qualifier, "profile", string.Join(",", values));
  1383. }
  1384. break;
  1385. }
  1386. case ProfileConditionValue.Height:
  1387. {
  1388. if (qualifiedOnly)
  1389. {
  1390. continue;
  1391. }
  1392. int num;
  1393. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1394. {
  1395. if (condition.Condition == ProfileConditionType.Equals)
  1396. {
  1397. item.MaxHeight = num;
  1398. }
  1399. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1400. {
  1401. item.MaxHeight = Math.Min(num, item.MaxHeight ?? num);
  1402. }
  1403. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1404. {
  1405. item.MaxHeight = Math.Max(num, item.MaxHeight ?? num);
  1406. }
  1407. }
  1408. break;
  1409. }
  1410. case ProfileConditionValue.VideoBitrate:
  1411. {
  1412. if (qualifiedOnly)
  1413. {
  1414. continue;
  1415. }
  1416. int num;
  1417. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1418. {
  1419. if (condition.Condition == ProfileConditionType.Equals)
  1420. {
  1421. item.VideoBitrate = num;
  1422. }
  1423. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1424. {
  1425. item.VideoBitrate = Math.Min(num, item.VideoBitrate ?? num);
  1426. }
  1427. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1428. {
  1429. item.VideoBitrate = Math.Max(num, item.VideoBitrate ?? num);
  1430. }
  1431. }
  1432. break;
  1433. }
  1434. case ProfileConditionValue.VideoFramerate:
  1435. {
  1436. if (qualifiedOnly)
  1437. {
  1438. continue;
  1439. }
  1440. float num;
  1441. if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1442. {
  1443. if (condition.Condition == ProfileConditionType.Equals)
  1444. {
  1445. item.MaxFramerate = num;
  1446. }
  1447. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1448. {
  1449. item.MaxFramerate = Math.Min(num, item.MaxFramerate ?? num);
  1450. }
  1451. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1452. {
  1453. item.MaxFramerate = Math.Max(num, item.MaxFramerate ?? num);
  1454. }
  1455. }
  1456. break;
  1457. }
  1458. case ProfileConditionValue.VideoLevel:
  1459. {
  1460. if (string.IsNullOrWhiteSpace(qualifier))
  1461. {
  1462. continue;
  1463. }
  1464. int num;
  1465. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1466. {
  1467. if (condition.Condition == ProfileConditionType.Equals)
  1468. {
  1469. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(num));
  1470. }
  1471. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1472. {
  1473. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetVideoLevel(qualifier) ?? num)));
  1474. }
  1475. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1476. {
  1477. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetVideoLevel(qualifier) ?? num)));
  1478. }
  1479. }
  1480. break;
  1481. }
  1482. case ProfileConditionValue.Width:
  1483. {
  1484. if (qualifiedOnly)
  1485. {
  1486. continue;
  1487. }
  1488. int num;
  1489. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1490. {
  1491. if (condition.Condition == ProfileConditionType.Equals)
  1492. {
  1493. item.MaxWidth = num;
  1494. }
  1495. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1496. {
  1497. item.MaxWidth = Math.Min(num, item.MaxWidth ?? num);
  1498. }
  1499. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1500. {
  1501. item.MaxWidth = Math.Max(num, item.MaxWidth ?? num);
  1502. }
  1503. }
  1504. break;
  1505. }
  1506. default:
  1507. break;
  1508. }
  1509. }
  1510. }
  1511. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  1512. {
  1513. // Check container type
  1514. if (!profile.SupportsContainer(item.Container))
  1515. {
  1516. return false;
  1517. }
  1518. // Check audio codec
  1519. var audioCodecs = profile.GetAudioCodecs();
  1520. if (audioCodecs.Length > 0)
  1521. {
  1522. // Check audio codecs
  1523. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1524. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1525. {
  1526. return false;
  1527. }
  1528. }
  1529. return true;
  1530. }
  1531. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  1532. {
  1533. // Check container type
  1534. if (!profile.SupportsContainer(item.Container))
  1535. {
  1536. return false;
  1537. }
  1538. // Check video codec
  1539. var videoCodecs = profile.GetVideoCodecs();
  1540. if (videoCodecs.Length > 0)
  1541. {
  1542. string videoCodec = videoStream == null ? null : videoStream.Codec;
  1543. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  1544. {
  1545. return false;
  1546. }
  1547. }
  1548. // Check audio codec
  1549. if (audioStream != null)
  1550. {
  1551. var audioCodecs = profile.GetAudioCodecs();
  1552. if (audioCodecs.Length > 0)
  1553. {
  1554. // Check audio codecs
  1555. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1556. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1557. {
  1558. return false;
  1559. }
  1560. }
  1561. }
  1562. return true;
  1563. }
  1564. }
  1565. }