StreamBuilder.cs 52 KB

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