StreamBuilder.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. List<MediaSourceInfo> 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. List<StreamInfo> 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. List<MediaSourceInfo> 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. List<StreamInfo> 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. streams = StreamInfoSorter.SortMediaSources(streams, maxBitrate);
  85. foreach (StreamInfo stream in streams)
  86. {
  87. return stream;
  88. }
  89. return null;
  90. }
  91. private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
  92. {
  93. StreamInfo playlistItem = new StreamInfo
  94. {
  95. ItemId = options.ItemId,
  96. MediaType = DlnaProfileType.Audio,
  97. MediaSource = item,
  98. RunTimeTicks = item.RunTimeTicks,
  99. Context = options.Context,
  100. DeviceProfile = options.Profile
  101. };
  102. if (options.ForceDirectPlay)
  103. {
  104. playlistItem.PlayMethod = PlayMethod.DirectPlay;
  105. playlistItem.Container = item.Container;
  106. return playlistItem;
  107. }
  108. if (options.ForceDirectStream)
  109. {
  110. playlistItem.PlayMethod = PlayMethod.DirectStream;
  111. playlistItem.Container = item.Container;
  112. return playlistItem;
  113. }
  114. MediaStream audioStream = item.GetDefaultAudioStream(null);
  115. List<PlayMethod> directPlayMethods = GetAudioDirectPlayMethods(item, audioStream, options);
  116. ConditionProcessor conditionProcessor = new ConditionProcessor();
  117. int? inputAudioChannels = audioStream == null ? null : audioStream.Channels;
  118. int? inputAudioBitrate = audioStream == null ? null : audioStream.BitDepth;
  119. if (directPlayMethods.Count > 0)
  120. {
  121. string audioCodec = audioStream == null ? null : audioStream.Codec;
  122. // Make sure audio codec profiles are satisfied
  123. if (!string.IsNullOrEmpty(audioCodec))
  124. {
  125. List<ProfileCondition> conditions = new List<ProfileCondition>();
  126. foreach (CodecProfile i in options.Profile.CodecProfiles)
  127. {
  128. if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec, item.Container))
  129. {
  130. bool applyConditions = true;
  131. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  132. {
  133. if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate))
  134. {
  135. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  136. applyConditions = false;
  137. break;
  138. }
  139. }
  140. if (applyConditions)
  141. {
  142. foreach (ProfileCondition c in i.Conditions)
  143. {
  144. conditions.Add(c);
  145. }
  146. }
  147. }
  148. }
  149. bool all = true;
  150. foreach (ProfileCondition c in conditions)
  151. {
  152. if (!conditionProcessor.IsAudioConditionSatisfied(c, inputAudioChannels, inputAudioBitrate))
  153. {
  154. LogConditionFailure(options.Profile, "AudioCodecProfile", c, item);
  155. all = false;
  156. break;
  157. }
  158. }
  159. if (all)
  160. {
  161. if (directPlayMethods.Contains(PlayMethod.DirectStream))
  162. {
  163. playlistItem.PlayMethod = PlayMethod.DirectStream;
  164. }
  165. playlistItem.Container = item.Container;
  166. return playlistItem;
  167. }
  168. }
  169. }
  170. TranscodingProfile transcodingProfile = null;
  171. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  172. {
  173. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  174. {
  175. if (_transcoderSupport.CanEncodeToAudioCodec(i.AudioCodec ?? i.Container))
  176. {
  177. transcodingProfile = i;
  178. break;
  179. }
  180. }
  181. }
  182. if (transcodingProfile != null)
  183. {
  184. if (!item.SupportsTranscoding)
  185. {
  186. return null;
  187. }
  188. playlistItem.PlayMethod = PlayMethod.Transcode;
  189. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  190. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  191. playlistItem.Container = transcodingProfile.Container;
  192. if (string.IsNullOrEmpty(transcodingProfile.AudioCodec))
  193. {
  194. playlistItem.AudioCodecs = new string[] { };
  195. }
  196. else
  197. {
  198. playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
  199. }
  200. playlistItem.SubProtocol = transcodingProfile.Protocol;
  201. List<CodecProfile> audioCodecProfiles = new List<CodecProfile>();
  202. foreach (CodecProfile i in options.Profile.CodecProfiles)
  203. {
  204. if (i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec, transcodingProfile.Container))
  205. {
  206. audioCodecProfiles.Add(i);
  207. }
  208. if (audioCodecProfiles.Count >= 1) break;
  209. }
  210. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  211. foreach (CodecProfile i in audioCodecProfiles)
  212. {
  213. bool applyConditions = true;
  214. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  215. {
  216. if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate))
  217. {
  218. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  219. applyConditions = false;
  220. break;
  221. }
  222. }
  223. if (applyConditions)
  224. {
  225. foreach (ProfileCondition c in i.Conditions)
  226. {
  227. audioTranscodingConditions.Add(c);
  228. }
  229. }
  230. }
  231. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  232. // Honor requested max channels
  233. if (options.MaxAudioChannels.HasValue)
  234. {
  235. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  236. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  237. }
  238. long transcodingBitrate = options.AudioTranscodingBitrate ??
  239. options.Profile.MusicStreamingTranscodingBitrate ??
  240. 128000;
  241. var configuredBitrate = options.GetMaxBitrate(true);
  242. if (configuredBitrate.HasValue)
  243. {
  244. transcodingBitrate = Math.Min(configuredBitrate.Value, transcodingBitrate);
  245. }
  246. var longBitrate = Math.Min(transcodingBitrate, playlistItem.AudioBitrate ?? transcodingBitrate);
  247. playlistItem.AudioBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
  248. }
  249. return playlistItem;
  250. }
  251. private long? GetBitrateForDirectPlayCheck(MediaSourceInfo item, AudioOptions options, bool isAudio)
  252. {
  253. if (item.Protocol == MediaProtocol.File)
  254. {
  255. return options.Profile.MaxStaticBitrate;
  256. }
  257. return options.GetMaxBitrate(isAudio);
  258. }
  259. private List<PlayMethod> GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
  260. {
  261. DirectPlayProfile directPlayProfile = null;
  262. foreach (DirectPlayProfile i in options.Profile.DirectPlayProfiles)
  263. {
  264. if (i.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(i, item, audioStream))
  265. {
  266. directPlayProfile = i;
  267. break;
  268. }
  269. }
  270. List<PlayMethod> playMethods = new List<PlayMethod>();
  271. if (directPlayProfile != null)
  272. {
  273. // While options takes the network and other factors into account. Only applies to direct stream
  274. if (item.SupportsDirectStream && IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate(true)) && options.EnableDirectStream)
  275. {
  276. playMethods.Add(PlayMethod.DirectStream);
  277. }
  278. // The profile describes what the device supports
  279. // If device requirements are satisfied then allow both direct stream and direct play
  280. if (item.SupportsDirectPlay &&
  281. IsAudioEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true)) && options.EnableDirectPlay)
  282. {
  283. playMethods.Add(PlayMethod.DirectPlay);
  284. }
  285. }
  286. else
  287. {
  288. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  289. options.Profile.Name ?? "Unknown Profile",
  290. item.Path ?? "Unknown path");
  291. }
  292. return playMethods;
  293. }
  294. private int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles)
  295. {
  296. int highestScore = -1;
  297. foreach (MediaStream stream in item.MediaStreams)
  298. {
  299. if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue)
  300. {
  301. if (stream.Score.Value > highestScore)
  302. {
  303. highestScore = stream.Score.Value;
  304. }
  305. }
  306. }
  307. List<MediaStream> topStreams = new List<MediaStream>();
  308. foreach (MediaStream stream in item.MediaStreams)
  309. {
  310. if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue && stream.Score.Value == highestScore)
  311. {
  312. topStreams.Add(stream);
  313. }
  314. }
  315. // If multiple streams have an equal score, try to pick the most efficient one
  316. if (topStreams.Count > 1)
  317. {
  318. foreach (MediaStream stream in topStreams)
  319. {
  320. foreach (SubtitleProfile profile in subtitleProfiles)
  321. {
  322. if (profile.Method == SubtitleDeliveryMethod.External && StringHelper.EqualsIgnoreCase(profile.Format, stream.Codec))
  323. {
  324. return stream.Index;
  325. }
  326. }
  327. }
  328. }
  329. // If no optimization panned out, just use the original default
  330. return item.DefaultSubtitleStreamIndex;
  331. }
  332. private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
  333. {
  334. StreamInfo playlistItem = new StreamInfo
  335. {
  336. ItemId = options.ItemId,
  337. MediaType = DlnaProfileType.Video,
  338. MediaSource = item,
  339. RunTimeTicks = item.RunTimeTicks,
  340. Context = options.Context,
  341. DeviceProfile = options.Profile
  342. };
  343. playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? GetDefaultSubtitleStreamIndex(item, options.Profile.SubtitleProfiles);
  344. MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
  345. MediaStream audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
  346. int? audioStreamIndex = null;
  347. if (audioStream != null)
  348. {
  349. audioStreamIndex = audioStream.Index;
  350. }
  351. MediaStream videoStream = item.VideoStream;
  352. // TODO: This doesn't accout for situation of device being able to handle media bitrate, but wifi connection not fast enough
  353. bool isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || IsEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), subtitleStream, options, PlayMethod.DirectPlay));
  354. bool isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || IsEligibleForDirectPlay(item, options.GetMaxBitrate(false), subtitleStream, options, PlayMethod.DirectStream));
  355. _logger.Info("Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
  356. options.Profile.Name ?? "Unknown Profile",
  357. item.Path ?? "Unknown path",
  358. isEligibleForDirectPlay,
  359. isEligibleForDirectStream);
  360. if (isEligibleForDirectPlay || isEligibleForDirectStream)
  361. {
  362. // See if it can be direct played
  363. PlayMethod? directPlay = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, isEligibleForDirectPlay, isEligibleForDirectStream);
  364. if (directPlay != null)
  365. {
  366. playlistItem.PlayMethod = directPlay.Value;
  367. playlistItem.Container = item.Container;
  368. if (subtitleStream != null)
  369. {
  370. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, directPlay.Value);
  371. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  372. playlistItem.SubtitleFormat = subtitleProfile.Format;
  373. }
  374. return playlistItem;
  375. }
  376. }
  377. // Can't direct play, find the transcoding profile
  378. TranscodingProfile transcodingProfile = null;
  379. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  380. {
  381. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  382. {
  383. transcodingProfile = i;
  384. break;
  385. }
  386. }
  387. if (transcodingProfile != null)
  388. {
  389. if (!item.SupportsTranscoding)
  390. {
  391. return null;
  392. }
  393. if (subtitleStream != null)
  394. {
  395. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.Transcode);
  396. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  397. playlistItem.SubtitleFormat = subtitleProfile.Format;
  398. }
  399. playlistItem.PlayMethod = PlayMethod.Transcode;
  400. playlistItem.Container = transcodingProfile.Container;
  401. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  402. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  403. playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
  404. playlistItem.VideoCodec = transcodingProfile.VideoCodec;
  405. playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
  406. playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
  407. if (!string.IsNullOrEmpty(transcodingProfile.MaxAudioChannels))
  408. {
  409. int transcodingMaxAudioChannels;
  410. if (int.TryParse(transcodingProfile.MaxAudioChannels, NumberStyles.Any, CultureInfo.InvariantCulture, out transcodingMaxAudioChannels))
  411. {
  412. playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
  413. }
  414. }
  415. playlistItem.SubProtocol = transcodingProfile.Protocol;
  416. playlistItem.AudioStreamIndex = audioStreamIndex;
  417. ConditionProcessor conditionProcessor = new ConditionProcessor();
  418. List<ProfileCondition> videoTranscodingConditions = new List<ProfileCondition>();
  419. foreach (CodecProfile i in options.Profile.CodecProfiles)
  420. {
  421. if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
  422. {
  423. bool applyConditions = true;
  424. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  425. {
  426. bool? isSecondaryAudio = audioStream == null ? null : item.IsSecondaryAudio(audioStream);
  427. int? inputAudioBitrate = audioStream == null ? null : audioStream.BitRate;
  428. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  429. string audioProfile = audioStream == null ? null : audioStream.Profile;
  430. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, audioProfile, isSecondaryAudio))
  431. {
  432. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  433. applyConditions = false;
  434. break;
  435. }
  436. }
  437. if (applyConditions)
  438. {
  439. foreach (ProfileCondition c in i.Conditions)
  440. {
  441. videoTranscodingConditions.Add(c);
  442. }
  443. break;
  444. }
  445. }
  446. }
  447. ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
  448. List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
  449. foreach (CodecProfile i in options.Profile.CodecProfiles)
  450. {
  451. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(playlistItem.TargetAudioCodec, transcodingProfile.Container))
  452. {
  453. bool applyConditions = true;
  454. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  455. {
  456. int? width = videoStream == null ? null : videoStream.Width;
  457. int? height = videoStream == null ? null : videoStream.Height;
  458. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  459. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  460. double? videoLevel = videoStream == null ? null : videoStream.Level;
  461. string videoProfile = videoStream == null ? null : videoStream.Profile;
  462. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  463. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  464. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  465. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  466. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
  467. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  468. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  469. int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
  470. int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
  471. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  472. {
  473. LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
  474. applyConditions = false;
  475. break;
  476. }
  477. }
  478. if (applyConditions)
  479. {
  480. foreach (ProfileCondition c in i.Conditions)
  481. {
  482. audioTranscodingConditions.Add(c);
  483. }
  484. break;
  485. }
  486. }
  487. }
  488. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  489. // Honor requested max channels
  490. if (options.MaxAudioChannels.HasValue)
  491. {
  492. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  493. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  494. }
  495. int audioBitrate = GetAudioBitrate(playlistItem.SubProtocol, options.GetMaxBitrate(false), playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec, audioStream);
  496. playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
  497. var maxBitrateSetting = options.GetMaxBitrate(false);
  498. // Honor max rate
  499. if (maxBitrateSetting.HasValue)
  500. {
  501. var videoBitrate = maxBitrateSetting.Value;
  502. if (playlistItem.AudioBitrate.HasValue)
  503. {
  504. videoBitrate -= playlistItem.AudioBitrate.Value;
  505. }
  506. // Make sure the video bitrate is lower than bitrate settings but at least 64k
  507. long currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  508. var longBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
  509. playlistItem.VideoBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
  510. }
  511. }
  512. return playlistItem;
  513. }
  514. private int GetAudioBitrate(string subProtocol, long? maxTotalBitrate, int? targetAudioChannels, string targetAudioCodec, MediaStream audioStream)
  515. {
  516. int defaultBitrate = audioStream == null ? 192000 : audioStream.BitRate ?? 192000;
  517. // Reduce the bitrate if we're downmixing
  518. if (targetAudioChannels.HasValue && audioStream != null && audioStream.Channels.HasValue && targetAudioChannels.Value < audioStream.Channels.Value)
  519. {
  520. defaultBitrate = StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3") ? 192000 : 128000;
  521. }
  522. if (StringHelper.EqualsIgnoreCase(subProtocol, "hls"))
  523. {
  524. defaultBitrate = Math.Min(384000, defaultBitrate);
  525. }
  526. else
  527. {
  528. defaultBitrate = Math.Min(448000, defaultBitrate);
  529. }
  530. int encoderAudioBitrateLimit = int.MaxValue;
  531. if (audioStream != null)
  532. {
  533. // Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
  534. // Any attempts to transcode over 64k will fail
  535. if (audioStream.Channels.HasValue &&
  536. audioStream.Channels.Value == 1)
  537. {
  538. if ((audioStream.BitRate ?? 0) < 64000)
  539. {
  540. encoderAudioBitrateLimit = 64000;
  541. }
  542. }
  543. }
  544. if (maxTotalBitrate.HasValue)
  545. {
  546. if (maxTotalBitrate.Value < 640000)
  547. {
  548. defaultBitrate = Math.Min(128000, defaultBitrate);
  549. }
  550. }
  551. return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
  552. }
  553. private PlayMethod? GetVideoDirectPlayProfile(VideoOptions options,
  554. MediaSourceInfo mediaSource,
  555. MediaStream videoStream,
  556. MediaStream audioStream,
  557. bool isEligibleForDirectPlay,
  558. bool isEligibleForDirectStream)
  559. {
  560. DeviceProfile profile = options.Profile;
  561. if (options.ForceDirectPlay)
  562. {
  563. return PlayMethod.DirectPlay;
  564. }
  565. if (options.ForceDirectStream)
  566. {
  567. return PlayMethod.DirectStream;
  568. }
  569. if (videoStream == null)
  570. {
  571. _logger.Info("Profile: {0}, Cannot direct stream with no known video stream. Path: {1}",
  572. profile.Name ?? "Unknown Profile",
  573. mediaSource.Path ?? "Unknown path");
  574. return null;
  575. }
  576. // See if it can be direct played
  577. DirectPlayProfile directPlay = null;
  578. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  579. {
  580. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  581. {
  582. directPlay = i;
  583. break;
  584. }
  585. }
  586. if (directPlay == null)
  587. {
  588. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  589. profile.Name ?? "Unknown Profile",
  590. mediaSource.Path ?? "Unknown path");
  591. return null;
  592. }
  593. string container = mediaSource.Container;
  594. List<ProfileCondition> conditions = new List<ProfileCondition>();
  595. foreach (ContainerProfile i in profile.ContainerProfiles)
  596. {
  597. if (i.Type == DlnaProfileType.Video &&
  598. i.ContainsContainer(container))
  599. {
  600. foreach (ProfileCondition c in i.Conditions)
  601. {
  602. conditions.Add(c);
  603. }
  604. }
  605. }
  606. ConditionProcessor conditionProcessor = new ConditionProcessor();
  607. int? width = videoStream == null ? null : videoStream.Width;
  608. int? height = videoStream == null ? null : videoStream.Height;
  609. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  610. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  611. double? videoLevel = videoStream == null ? null : videoStream.Level;
  612. string videoProfile = videoStream == null ? null : videoStream.Profile;
  613. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  614. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  615. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  616. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  617. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  618. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  619. string audioProfile = audioStream == null ? null : audioStream.Profile;
  620. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  621. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  622. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  623. int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
  624. int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
  625. // Check container conditions
  626. foreach (ProfileCondition i in conditions)
  627. {
  628. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  629. {
  630. LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
  631. return null;
  632. }
  633. }
  634. string videoCodec = videoStream == null ? null : videoStream.Codec;
  635. if (string.IsNullOrEmpty(videoCodec))
  636. {
  637. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown video codec. Path: {1}",
  638. profile.Name ?? "Unknown Profile",
  639. mediaSource.Path ?? "Unknown path");
  640. return null;
  641. }
  642. conditions = new List<ProfileCondition>();
  643. foreach (CodecProfile i in profile.CodecProfiles)
  644. {
  645. if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec, container))
  646. {
  647. bool applyConditions = true;
  648. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  649. {
  650. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  651. {
  652. LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
  653. applyConditions = false;
  654. break;
  655. }
  656. }
  657. if (applyConditions)
  658. {
  659. foreach (ProfileCondition c in i.Conditions)
  660. {
  661. conditions.Add(c);
  662. }
  663. }
  664. }
  665. }
  666. foreach (ProfileCondition i in conditions)
  667. {
  668. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  669. {
  670. LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
  671. return null;
  672. }
  673. }
  674. if (audioStream != null)
  675. {
  676. string audioCodec = audioStream.Codec;
  677. if (string.IsNullOrEmpty(audioCodec))
  678. {
  679. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown audio codec. Path: {1}",
  680. profile.Name ?? "Unknown Profile",
  681. mediaSource.Path ?? "Unknown path");
  682. return null;
  683. }
  684. conditions = new List<ProfileCondition>();
  685. bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
  686. foreach (CodecProfile i in profile.CodecProfiles)
  687. {
  688. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec, container))
  689. {
  690. bool applyConditions = true;
  691. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  692. {
  693. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioProfile, isSecondaryAudio))
  694. {
  695. LogConditionFailure(profile, "VideoAudioCodecProfile", applyCondition, mediaSource);
  696. applyConditions = false;
  697. break;
  698. }
  699. }
  700. if (applyConditions)
  701. {
  702. foreach (ProfileCondition c in i.Conditions)
  703. {
  704. conditions.Add(c);
  705. }
  706. }
  707. }
  708. }
  709. foreach (ProfileCondition i in conditions)
  710. {
  711. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile, isSecondaryAudio))
  712. {
  713. LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
  714. return null;
  715. }
  716. }
  717. }
  718. if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
  719. {
  720. return PlayMethod.DirectStream;
  721. }
  722. return null;
  723. }
  724. private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
  725. {
  726. _logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
  727. type,
  728. profile.Name ?? "Unknown Profile",
  729. condition.Property,
  730. condition.Condition,
  731. condition.Value ?? string.Empty,
  732. condition.IsRequired,
  733. mediaSource.Path ?? "Unknown path");
  734. }
  735. private bool IsEligibleForDirectPlay(MediaSourceInfo item,
  736. long? maxBitrate,
  737. MediaStream subtitleStream,
  738. VideoOptions options,
  739. PlayMethod playMethod)
  740. {
  741. if (subtitleStream != null)
  742. {
  743. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, playMethod);
  744. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  745. {
  746. _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
  747. return false;
  748. }
  749. }
  750. return IsAudioEligibleForDirectPlay(item, maxBitrate);
  751. }
  752. public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod)
  753. {
  754. if (playMethod != PlayMethod.Transcode && !subtitleStream.IsExternal)
  755. {
  756. // Look for supported embedded subs
  757. foreach (SubtitleProfile profile in subtitleProfiles)
  758. {
  759. if (!profile.SupportsLanguage(subtitleStream.Language))
  760. {
  761. continue;
  762. }
  763. if (profile.Method != SubtitleDeliveryMethod.Embed)
  764. {
  765. continue;
  766. }
  767. if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
  768. {
  769. return profile;
  770. }
  771. }
  772. }
  773. // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
  774. return GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, false) ?? GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, true) ?? new SubtitleProfile
  775. {
  776. Method = SubtitleDeliveryMethod.Encode,
  777. Format = subtitleStream.Codec
  778. };
  779. }
  780. private static SubtitleProfile GetExternalSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, bool allowConversion)
  781. {
  782. foreach (SubtitleProfile profile in subtitleProfiles)
  783. {
  784. if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
  785. {
  786. continue;
  787. }
  788. if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
  789. {
  790. continue;
  791. }
  792. if (!profile.SupportsLanguage(subtitleStream.Language))
  793. {
  794. continue;
  795. }
  796. if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
  797. (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
  798. {
  799. bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
  800. if (!requiresConversion)
  801. {
  802. return profile;
  803. }
  804. if (!allowConversion)
  805. {
  806. continue;
  807. }
  808. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  809. {
  810. return profile;
  811. }
  812. }
  813. }
  814. return null;
  815. }
  816. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long? maxBitrate)
  817. {
  818. if (!maxBitrate.HasValue)
  819. {
  820. _logger.Info("Cannot direct play due to unknown supported bitrate");
  821. return false;
  822. }
  823. if (!item.Bitrate.HasValue)
  824. {
  825. _logger.Info("Cannot direct play due to unknown content bitrate");
  826. return false;
  827. }
  828. if (item.Bitrate.Value > maxBitrate.Value)
  829. {
  830. _logger.Info("Bitrate exceeds DirectPlay limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), maxBitrate.Value.ToString(CultureInfo.InvariantCulture));
  831. return false;
  832. }
  833. return true;
  834. }
  835. private void ValidateInput(VideoOptions options)
  836. {
  837. ValidateAudioInput(options);
  838. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  839. {
  840. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  841. }
  842. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  843. {
  844. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  845. }
  846. }
  847. private void ValidateAudioInput(AudioOptions options)
  848. {
  849. if (string.IsNullOrEmpty(options.ItemId))
  850. {
  851. throw new ArgumentException("ItemId is required");
  852. }
  853. if (string.IsNullOrEmpty(options.DeviceId))
  854. {
  855. throw new ArgumentException("DeviceId is required");
  856. }
  857. if (options.Profile == null)
  858. {
  859. throw new ArgumentException("Profile is required");
  860. }
  861. if (options.MediaSources == null)
  862. {
  863. throw new ArgumentException("MediaSources is required");
  864. }
  865. }
  866. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  867. {
  868. foreach (ProfileCondition condition in conditions)
  869. {
  870. string value = condition.Value;
  871. if (string.IsNullOrEmpty(value))
  872. {
  873. continue;
  874. }
  875. // No way to express this
  876. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  877. {
  878. continue;
  879. }
  880. switch (condition.Property)
  881. {
  882. case ProfileConditionValue.AudioBitrate:
  883. {
  884. int num;
  885. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  886. {
  887. item.AudioBitrate = num;
  888. }
  889. break;
  890. }
  891. case ProfileConditionValue.AudioChannels:
  892. {
  893. int num;
  894. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  895. {
  896. item.MaxAudioChannels = num;
  897. }
  898. break;
  899. }
  900. case ProfileConditionValue.IsAvc:
  901. {
  902. bool isAvc;
  903. if (bool.TryParse(value, out isAvc))
  904. {
  905. if (isAvc && condition.Condition == ProfileConditionType.Equals)
  906. {
  907. item.RequireAvc = true;
  908. }
  909. else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
  910. {
  911. item.RequireAvc = true;
  912. }
  913. }
  914. break;
  915. }
  916. case ProfileConditionValue.IsAnamorphic:
  917. case ProfileConditionValue.AudioProfile:
  918. case ProfileConditionValue.Has64BitOffsets:
  919. case ProfileConditionValue.PacketLength:
  920. case ProfileConditionValue.NumAudioStreams:
  921. case ProfileConditionValue.NumVideoStreams:
  922. case ProfileConditionValue.IsSecondaryAudio:
  923. case ProfileConditionValue.VideoTimestamp:
  924. {
  925. // Not supported yet
  926. break;
  927. }
  928. case ProfileConditionValue.RefFrames:
  929. {
  930. int num;
  931. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  932. {
  933. item.MaxRefFrames = num;
  934. }
  935. break;
  936. }
  937. case ProfileConditionValue.VideoBitDepth:
  938. {
  939. int num;
  940. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  941. {
  942. item.MaxVideoBitDepth = num;
  943. }
  944. break;
  945. }
  946. case ProfileConditionValue.VideoProfile:
  947. {
  948. item.VideoProfile = (value ?? string.Empty).Split('|')[0];
  949. break;
  950. }
  951. case ProfileConditionValue.Height:
  952. {
  953. int num;
  954. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  955. {
  956. item.MaxHeight = num;
  957. }
  958. break;
  959. }
  960. case ProfileConditionValue.VideoBitrate:
  961. {
  962. int num;
  963. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  964. {
  965. item.VideoBitrate = num;
  966. }
  967. break;
  968. }
  969. case ProfileConditionValue.VideoFramerate:
  970. {
  971. float num;
  972. if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  973. {
  974. item.MaxFramerate = num;
  975. }
  976. break;
  977. }
  978. case ProfileConditionValue.VideoLevel:
  979. {
  980. int num;
  981. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  982. {
  983. item.VideoLevel = num;
  984. }
  985. break;
  986. }
  987. case ProfileConditionValue.Width:
  988. {
  989. int num;
  990. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  991. {
  992. item.MaxWidth = num;
  993. }
  994. break;
  995. }
  996. default:
  997. break;
  998. }
  999. }
  1000. }
  1001. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  1002. {
  1003. if (profile.Container.Length > 0)
  1004. {
  1005. // Check container type
  1006. string mediaContainer = item.Container ?? string.Empty;
  1007. bool any = false;
  1008. foreach (string i in profile.GetContainers())
  1009. {
  1010. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  1011. {
  1012. any = true;
  1013. break;
  1014. }
  1015. }
  1016. if (!any)
  1017. {
  1018. return false;
  1019. }
  1020. }
  1021. // Check audio codec
  1022. List<string> audioCodecs = profile.GetAudioCodecs();
  1023. if (audioCodecs.Count > 0)
  1024. {
  1025. // Check audio codecs
  1026. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1027. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1028. {
  1029. return false;
  1030. }
  1031. }
  1032. return true;
  1033. }
  1034. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  1035. {
  1036. if (profile.Container.Length > 0)
  1037. {
  1038. // Check container type
  1039. string mediaContainer = item.Container ?? string.Empty;
  1040. bool any = false;
  1041. foreach (string i in profile.GetContainers())
  1042. {
  1043. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  1044. {
  1045. any = true;
  1046. break;
  1047. }
  1048. }
  1049. if (!any)
  1050. {
  1051. return false;
  1052. }
  1053. }
  1054. // Check video codec
  1055. List<string> videoCodecs = profile.GetVideoCodecs();
  1056. if (videoCodecs.Count > 0)
  1057. {
  1058. string videoCodec = videoStream == null ? null : videoStream.Codec;
  1059. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  1060. {
  1061. return false;
  1062. }
  1063. }
  1064. // Check audio codec
  1065. List<string> audioCodecs = profile.GetAudioCodecs();
  1066. if (audioCodecs.Count > 0)
  1067. {
  1068. // Check audio codecs
  1069. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1070. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1071. {
  1072. return false;
  1073. }
  1074. }
  1075. return true;
  1076. }
  1077. }
  1078. }