StreamBuilder.cs 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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. // See if it can be direct played
  813. DirectPlayProfile directPlay = null;
  814. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  815. {
  816. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  817. {
  818. directPlay = i;
  819. break;
  820. }
  821. }
  822. if (directPlay == null)
  823. {
  824. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  825. profile.Name ?? "Unknown Profile",
  826. mediaSource.Path ?? "Unknown path");
  827. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, GetTranscodeReasonsFromDirectPlayProfile(mediaSource, videoStream, audioStream, profile.DirectPlayProfiles));
  828. }
  829. string container = mediaSource.Container;
  830. var conditions = new List<ProfileCondition>();
  831. foreach (ContainerProfile i in profile.ContainerProfiles)
  832. {
  833. if (i.Type == DlnaProfileType.Video &&
  834. i.ContainsContainer(container))
  835. {
  836. foreach (ProfileCondition c in i.Conditions)
  837. {
  838. conditions.Add(c);
  839. }
  840. }
  841. }
  842. ConditionProcessor conditionProcessor = new ConditionProcessor();
  843. int? width = videoStream == null ? null : videoStream.Width;
  844. int? height = videoStream == null ? null : videoStream.Height;
  845. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  846. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  847. double? videoLevel = videoStream == null ? null : videoStream.Level;
  848. string videoProfile = videoStream == null ? null : videoStream.Profile;
  849. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  850. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  851. bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
  852. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  853. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  854. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  855. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  856. string audioProfile = audioStream == null ? null : audioStream.Profile;
  857. int? audioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  858. int? audioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  859. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  860. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  861. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  862. int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
  863. int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
  864. // Check container conditions
  865. foreach (ProfileCondition i in conditions)
  866. {
  867. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  868. {
  869. LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
  870. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  871. var transcodeReasons = transcodeReason.HasValue
  872. ? new List<TranscodeReason> { transcodeReason.Value }
  873. : new List<TranscodeReason> { };
  874. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  875. }
  876. }
  877. string videoCodec = videoStream == null ? null : videoStream.Codec;
  878. conditions = new List<ProfileCondition>();
  879. foreach (CodecProfile i in profile.CodecProfiles)
  880. {
  881. if (i.Type == CodecType.Video && i.ContainsAnyCodec(videoCodec, container))
  882. {
  883. bool applyConditions = true;
  884. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  885. {
  886. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  887. {
  888. LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
  889. applyConditions = false;
  890. break;
  891. }
  892. }
  893. if (applyConditions)
  894. {
  895. foreach (ProfileCondition c in i.Conditions)
  896. {
  897. conditions.Add(c);
  898. }
  899. }
  900. }
  901. }
  902. foreach (ProfileCondition i in conditions)
  903. {
  904. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  905. {
  906. LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
  907. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  908. var transcodeReasons = transcodeReason.HasValue
  909. ? new List<TranscodeReason> { transcodeReason.Value }
  910. : new List<TranscodeReason> { };
  911. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  912. }
  913. }
  914. if (audioStream != null)
  915. {
  916. string audioCodec = audioStream.Codec;
  917. conditions = new List<ProfileCondition>();
  918. bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
  919. foreach (CodecProfile i in profile.CodecProfiles)
  920. {
  921. if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(audioCodec, container))
  922. {
  923. bool applyConditions = true;
  924. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  925. {
  926. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  927. {
  928. LogConditionFailure(profile, "VideoAudioCodecProfile.ApplyConditions", applyCondition, mediaSource);
  929. applyConditions = false;
  930. break;
  931. }
  932. }
  933. if (applyConditions)
  934. {
  935. foreach (ProfileCondition c in i.Conditions)
  936. {
  937. conditions.Add(c);
  938. }
  939. }
  940. }
  941. }
  942. foreach (ProfileCondition i in conditions)
  943. {
  944. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  945. {
  946. LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
  947. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  948. var transcodeReasons = transcodeReason.HasValue
  949. ? new List<TranscodeReason> { transcodeReason.Value }
  950. : new List<TranscodeReason> { };
  951. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  952. }
  953. }
  954. }
  955. if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
  956. {
  957. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
  958. }
  959. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.ContainerBitrateExceedsLimit });
  960. }
  961. private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
  962. {
  963. _logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
  964. type,
  965. profile.Name ?? "Unknown Profile",
  966. condition.Property,
  967. condition.Condition,
  968. condition.Value ?? string.Empty,
  969. condition.IsRequired,
  970. mediaSource.Path ?? "Unknown path");
  971. }
  972. private Tuple<bool, TranscodeReason?> IsEligibleForDirectPlay(MediaSourceInfo item,
  973. long? maxBitrate,
  974. MediaStream subtitleStream,
  975. VideoOptions options,
  976. PlayMethod playMethod)
  977. {
  978. if (subtitleStream != null)
  979. {
  980. SubtitleProfile subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, playMethod, _transcoderSupport, null, null);
  981. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  982. {
  983. _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
  984. return new Tuple<bool, TranscodeReason?>(false, TranscodeReason.SubtitleCodecNotSupported);
  985. }
  986. }
  987. var result = IsAudioEligibleForDirectPlay(item, maxBitrate, playMethod);
  988. if (result)
  989. {
  990. return new Tuple<bool, TranscodeReason?>(result, null);
  991. }
  992. return new Tuple<bool, TranscodeReason?>(result, TranscodeReason.ContainerBitrateExceedsLimit);
  993. }
  994. public static SubtitleProfile GetSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, string transcodingSubProtocol, string transcodingContainer)
  995. {
  996. if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
  997. {
  998. // Look for supported embedded subs of the same format
  999. foreach (SubtitleProfile profile in subtitleProfiles)
  1000. {
  1001. if (!profile.SupportsLanguage(subtitleStream.Language))
  1002. {
  1003. continue;
  1004. }
  1005. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1006. {
  1007. continue;
  1008. }
  1009. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1010. {
  1011. continue;
  1012. }
  1013. if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
  1014. {
  1015. return profile;
  1016. }
  1017. }
  1018. // Look for supported embedded subs of a convertible format
  1019. foreach (SubtitleProfile profile in subtitleProfiles)
  1020. {
  1021. if (!profile.SupportsLanguage(subtitleStream.Language))
  1022. {
  1023. continue;
  1024. }
  1025. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1026. {
  1027. continue;
  1028. }
  1029. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1030. {
  1031. continue;
  1032. }
  1033. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1034. {
  1035. return profile;
  1036. }
  1037. }
  1038. }
  1039. // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
  1040. return GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, false) ??
  1041. GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, true) ??
  1042. new SubtitleProfile
  1043. {
  1044. Method = SubtitleDeliveryMethod.Encode,
  1045. Format = subtitleStream.Codec
  1046. };
  1047. }
  1048. private static bool IsSubtitleEmbedSupported(MediaStream subtitleStream, SubtitleProfile subtitleProfile, string transcodingSubProtocol, string transcodingContainer)
  1049. {
  1050. if (!string.IsNullOrWhiteSpace(transcodingContainer))
  1051. {
  1052. var normalizedContainers = ContainerProfile.SplitValue(transcodingContainer);
  1053. if (ContainerProfile.ContainsContainer(normalizedContainers, "ts"))
  1054. {
  1055. return false;
  1056. }
  1057. if (ContainerProfile.ContainsContainer(normalizedContainers, "mpegts"))
  1058. {
  1059. return false;
  1060. }
  1061. if (ContainerProfile.ContainsContainer(normalizedContainers, "mp4"))
  1062. {
  1063. return false;
  1064. }
  1065. if (ContainerProfile.ContainsContainer(normalizedContainers, "mkv") ||
  1066. ContainerProfile.ContainsContainer(normalizedContainers, "matroska"))
  1067. {
  1068. return true;
  1069. }
  1070. }
  1071. return false;
  1072. }
  1073. private static SubtitleProfile GetExternalSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, bool allowConversion)
  1074. {
  1075. foreach (SubtitleProfile profile in subtitleProfiles)
  1076. {
  1077. if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
  1078. {
  1079. continue;
  1080. }
  1081. if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
  1082. {
  1083. continue;
  1084. }
  1085. if (!profile.SupportsLanguage(subtitleStream.Language))
  1086. {
  1087. continue;
  1088. }
  1089. if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec))
  1090. {
  1091. continue;
  1092. }
  1093. if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
  1094. (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
  1095. {
  1096. bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
  1097. if (!requiresConversion)
  1098. {
  1099. return profile;
  1100. }
  1101. if (!allowConversion)
  1102. {
  1103. continue;
  1104. }
  1105. // TODO: Build this into subtitleStream.SupportsExternalStream
  1106. if (mediaSource.IsInfiniteStream)
  1107. {
  1108. continue;
  1109. }
  1110. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1111. {
  1112. return profile;
  1113. }
  1114. }
  1115. }
  1116. return null;
  1117. }
  1118. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long? maxBitrate, PlayMethod playMethod)
  1119. {
  1120. // Don't restrict by bitrate if coming from an external domain
  1121. if (item.IsRemote)
  1122. {
  1123. return true;
  1124. }
  1125. if (!maxBitrate.HasValue)
  1126. {
  1127. _logger.Info("Cannot " + playMethod + " due to unknown supported bitrate");
  1128. return false;
  1129. }
  1130. if (!item.Bitrate.HasValue)
  1131. {
  1132. _logger.Info("Cannot " + playMethod + " due to unknown content bitrate");
  1133. return false;
  1134. }
  1135. if (item.Bitrate.Value > maxBitrate.Value)
  1136. {
  1137. _logger.Info("Bitrate exceeds " + playMethod + " limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), maxBitrate.Value.ToString(CultureInfo.InvariantCulture));
  1138. return false;
  1139. }
  1140. return true;
  1141. }
  1142. private void ValidateInput(VideoOptions options)
  1143. {
  1144. ValidateAudioInput(options);
  1145. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1146. {
  1147. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  1148. }
  1149. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1150. {
  1151. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  1152. }
  1153. }
  1154. private void ValidateAudioInput(AudioOptions options)
  1155. {
  1156. if (string.IsNullOrEmpty(options.ItemId))
  1157. {
  1158. throw new ArgumentException("ItemId is required");
  1159. }
  1160. if (string.IsNullOrEmpty(options.DeviceId))
  1161. {
  1162. throw new ArgumentException("DeviceId is required");
  1163. }
  1164. if (options.Profile == null)
  1165. {
  1166. throw new ArgumentException("Profile is required");
  1167. }
  1168. if (options.MediaSources == null)
  1169. {
  1170. throw new ArgumentException("MediaSources is required");
  1171. }
  1172. }
  1173. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string qualifier, bool qualifiedOnly)
  1174. {
  1175. foreach (ProfileCondition condition in conditions)
  1176. {
  1177. string value = condition.Value;
  1178. if (string.IsNullOrEmpty(value))
  1179. {
  1180. continue;
  1181. }
  1182. // No way to express this
  1183. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1184. {
  1185. continue;
  1186. }
  1187. switch (condition.Property)
  1188. {
  1189. case ProfileConditionValue.AudioBitrate:
  1190. {
  1191. if (qualifiedOnly)
  1192. {
  1193. continue;
  1194. }
  1195. int num;
  1196. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1197. {
  1198. if (condition.Condition == ProfileConditionType.Equals)
  1199. {
  1200. item.AudioBitrate = num;
  1201. }
  1202. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1203. {
  1204. item.AudioBitrate = Math.Min(num, item.AudioBitrate ?? num);
  1205. }
  1206. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1207. {
  1208. item.AudioBitrate = Math.Max(num, item.AudioBitrate ?? num);
  1209. }
  1210. }
  1211. break;
  1212. }
  1213. case ProfileConditionValue.AudioChannels:
  1214. {
  1215. if (qualifiedOnly)
  1216. {
  1217. continue;
  1218. }
  1219. int num;
  1220. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1221. {
  1222. if (condition.Condition == ProfileConditionType.Equals)
  1223. {
  1224. item.MaxAudioChannels = num;
  1225. }
  1226. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1227. {
  1228. item.MaxAudioChannels = Math.Min(num, item.MaxAudioChannels ?? num);
  1229. }
  1230. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1231. {
  1232. item.MaxAudioChannels = Math.Max(num, item.MaxAudioChannels ?? num);
  1233. }
  1234. }
  1235. break;
  1236. }
  1237. case ProfileConditionValue.IsAvc:
  1238. {
  1239. if (qualifiedOnly)
  1240. {
  1241. continue;
  1242. }
  1243. bool isAvc;
  1244. if (bool.TryParse(value, out isAvc))
  1245. {
  1246. if (isAvc && condition.Condition == ProfileConditionType.Equals)
  1247. {
  1248. item.RequireAvc = true;
  1249. }
  1250. else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
  1251. {
  1252. item.RequireAvc = true;
  1253. }
  1254. }
  1255. break;
  1256. }
  1257. case ProfileConditionValue.IsAnamorphic:
  1258. {
  1259. if (qualifiedOnly)
  1260. {
  1261. continue;
  1262. }
  1263. bool isAnamorphic;
  1264. if (bool.TryParse(value, out isAnamorphic))
  1265. {
  1266. if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
  1267. {
  1268. item.RequireNonAnamorphic = true;
  1269. }
  1270. else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
  1271. {
  1272. item.RequireNonAnamorphic = true;
  1273. }
  1274. }
  1275. break;
  1276. }
  1277. case ProfileConditionValue.IsInterlaced:
  1278. {
  1279. if (string.IsNullOrWhiteSpace(qualifier))
  1280. {
  1281. continue;
  1282. }
  1283. bool isInterlaced;
  1284. if (bool.TryParse(value, out isInterlaced))
  1285. {
  1286. if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
  1287. {
  1288. item.SetOption(qualifier, "deinterlace", "true");
  1289. }
  1290. else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
  1291. {
  1292. item.SetOption(qualifier, "deinterlace", "true");
  1293. }
  1294. }
  1295. break;
  1296. }
  1297. case ProfileConditionValue.AudioProfile:
  1298. case ProfileConditionValue.Has64BitOffsets:
  1299. case ProfileConditionValue.PacketLength:
  1300. case ProfileConditionValue.NumAudioStreams:
  1301. case ProfileConditionValue.NumVideoStreams:
  1302. case ProfileConditionValue.IsSecondaryAudio:
  1303. case ProfileConditionValue.VideoTimestamp:
  1304. {
  1305. // Not supported yet
  1306. break;
  1307. }
  1308. case ProfileConditionValue.RefFrames:
  1309. {
  1310. if (string.IsNullOrWhiteSpace(qualifier))
  1311. {
  1312. continue;
  1313. }
  1314. int num;
  1315. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1316. {
  1317. if (condition.Condition == ProfileConditionType.Equals)
  1318. {
  1319. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(num));
  1320. }
  1321. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1322. {
  1323. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetRefFrames(qualifier) ?? num)));
  1324. }
  1325. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1326. {
  1327. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetRefFrames(qualifier) ?? num)));
  1328. }
  1329. }
  1330. break;
  1331. }
  1332. case ProfileConditionValue.VideoBitDepth:
  1333. {
  1334. if (qualifiedOnly)
  1335. {
  1336. continue;
  1337. }
  1338. int num;
  1339. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1340. {
  1341. if (condition.Condition == ProfileConditionType.Equals)
  1342. {
  1343. item.MaxVideoBitDepth = num;
  1344. }
  1345. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1346. {
  1347. item.MaxVideoBitDepth = Math.Min(num, item.MaxVideoBitDepth ?? num);
  1348. }
  1349. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1350. {
  1351. item.MaxVideoBitDepth = Math.Max(num, item.MaxVideoBitDepth ?? num);
  1352. }
  1353. }
  1354. break;
  1355. }
  1356. case ProfileConditionValue.VideoProfile:
  1357. {
  1358. if (string.IsNullOrWhiteSpace(qualifier))
  1359. {
  1360. continue;
  1361. }
  1362. if (!string.IsNullOrWhiteSpace(value))
  1363. {
  1364. // change from split by | to comma
  1365. // strip spaces to avoid having to encode
  1366. var values = value
  1367. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  1368. item.SetOption(qualifier, "profile", string.Join(",", values));
  1369. }
  1370. break;
  1371. }
  1372. case ProfileConditionValue.Height:
  1373. {
  1374. if (qualifiedOnly)
  1375. {
  1376. continue;
  1377. }
  1378. int num;
  1379. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1380. {
  1381. if (condition.Condition == ProfileConditionType.Equals)
  1382. {
  1383. item.MaxHeight = num;
  1384. }
  1385. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1386. {
  1387. item.MaxHeight = Math.Min(num, item.MaxHeight ?? num);
  1388. }
  1389. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1390. {
  1391. item.MaxHeight = Math.Max(num, item.MaxHeight ?? num);
  1392. }
  1393. }
  1394. break;
  1395. }
  1396. case ProfileConditionValue.VideoBitrate:
  1397. {
  1398. if (qualifiedOnly)
  1399. {
  1400. continue;
  1401. }
  1402. int num;
  1403. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1404. {
  1405. if (condition.Condition == ProfileConditionType.Equals)
  1406. {
  1407. item.VideoBitrate = num;
  1408. }
  1409. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1410. {
  1411. item.VideoBitrate = Math.Min(num, item.VideoBitrate ?? num);
  1412. }
  1413. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1414. {
  1415. item.VideoBitrate = Math.Max(num, item.VideoBitrate ?? num);
  1416. }
  1417. }
  1418. break;
  1419. }
  1420. case ProfileConditionValue.VideoFramerate:
  1421. {
  1422. if (qualifiedOnly)
  1423. {
  1424. continue;
  1425. }
  1426. float num;
  1427. if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1428. {
  1429. if (condition.Condition == ProfileConditionType.Equals)
  1430. {
  1431. item.MaxFramerate = num;
  1432. }
  1433. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1434. {
  1435. item.MaxFramerate = Math.Min(num, item.MaxFramerate ?? num);
  1436. }
  1437. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1438. {
  1439. item.MaxFramerate = Math.Max(num, item.MaxFramerate ?? num);
  1440. }
  1441. }
  1442. break;
  1443. }
  1444. case ProfileConditionValue.VideoLevel:
  1445. {
  1446. if (string.IsNullOrWhiteSpace(qualifier))
  1447. {
  1448. continue;
  1449. }
  1450. int num;
  1451. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1452. {
  1453. if (condition.Condition == ProfileConditionType.Equals)
  1454. {
  1455. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(num));
  1456. }
  1457. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1458. {
  1459. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetVideoLevel(qualifier) ?? num)));
  1460. }
  1461. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1462. {
  1463. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetVideoLevel(qualifier) ?? num)));
  1464. }
  1465. }
  1466. break;
  1467. }
  1468. case ProfileConditionValue.Width:
  1469. {
  1470. if (qualifiedOnly)
  1471. {
  1472. continue;
  1473. }
  1474. int num;
  1475. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1476. {
  1477. if (condition.Condition == ProfileConditionType.Equals)
  1478. {
  1479. item.MaxWidth = num;
  1480. }
  1481. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1482. {
  1483. item.MaxWidth = Math.Min(num, item.MaxWidth ?? num);
  1484. }
  1485. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1486. {
  1487. item.MaxWidth = Math.Max(num, item.MaxWidth ?? num);
  1488. }
  1489. }
  1490. break;
  1491. }
  1492. default:
  1493. break;
  1494. }
  1495. }
  1496. }
  1497. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  1498. {
  1499. // Check container type
  1500. if (!profile.SupportsContainer(item.Container))
  1501. {
  1502. return false;
  1503. }
  1504. // Check audio codec
  1505. var audioCodecs = profile.GetAudioCodecs();
  1506. if (audioCodecs.Length > 0)
  1507. {
  1508. // Check audio codecs
  1509. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1510. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1511. {
  1512. return false;
  1513. }
  1514. }
  1515. return true;
  1516. }
  1517. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  1518. {
  1519. // Check container type
  1520. if (!profile.SupportsContainer(item.Container))
  1521. {
  1522. return false;
  1523. }
  1524. // Check video codec
  1525. var videoCodecs = profile.GetVideoCodecs();
  1526. if (videoCodecs.Length > 0)
  1527. {
  1528. string videoCodec = videoStream == null ? null : videoStream.Codec;
  1529. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  1530. {
  1531. return false;
  1532. }
  1533. }
  1534. // Check audio codec
  1535. if (audioStream != null)
  1536. {
  1537. var audioCodecs = profile.GetAudioCodecs();
  1538. if (audioCodecs.Length > 0)
  1539. {
  1540. // Check audio codecs
  1541. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1542. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1543. {
  1544. return false;
  1545. }
  1546. }
  1547. }
  1548. return true;
  1549. }
  1550. }
  1551. }