StreamBuilder.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. playlistItem.EnableSplittingOnNonKeyFrames = transcodingProfile.EnableSplittingOnNonKeyFrames;
  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. int? maxBitrateSetting = options.GetMaxBitrate(false);
  498. // Honor max rate
  499. if (maxBitrateSetting.HasValue)
  500. {
  501. int 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. int currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  508. playlistItem.VideoBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
  509. }
  510. }
  511. return playlistItem;
  512. }
  513. private int GetAudioBitrate(string subProtocol, int? maxTotalBitrate, int? targetAudioChannels, string targetAudioCodec, MediaStream audioStream)
  514. {
  515. int defaultBitrate = audioStream == null ? 192000 : audioStream.BitRate ?? 192000;
  516. // Reduce the bitrate if we're downmixing
  517. if (targetAudioChannels.HasValue && audioStream != null && audioStream.Channels.HasValue && targetAudioChannels.Value < audioStream.Channels.Value)
  518. {
  519. defaultBitrate = StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3") ? 192000 : 128000;
  520. }
  521. if (StringHelper.EqualsIgnoreCase(subProtocol, "hls"))
  522. {
  523. defaultBitrate = Math.Min(384000, defaultBitrate);
  524. }
  525. else
  526. {
  527. defaultBitrate = Math.Min(448000, defaultBitrate);
  528. }
  529. int encoderAudioBitrateLimit = int.MaxValue;
  530. if (audioStream != null)
  531. {
  532. // Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
  533. // Any attempts to transcode over 64k will fail
  534. if (audioStream.Channels.HasValue &&
  535. audioStream.Channels.Value == 1)
  536. {
  537. if ((audioStream.BitRate ?? 0) < 64000)
  538. {
  539. encoderAudioBitrateLimit = 64000;
  540. }
  541. }
  542. }
  543. if (maxTotalBitrate.HasValue)
  544. {
  545. if (maxTotalBitrate.Value < 640000)
  546. {
  547. defaultBitrate = Math.Min(128000, defaultBitrate);
  548. }
  549. }
  550. return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
  551. }
  552. private PlayMethod? GetVideoDirectPlayProfile(VideoOptions options,
  553. MediaSourceInfo mediaSource,
  554. MediaStream videoStream,
  555. MediaStream audioStream,
  556. bool isEligibleForDirectPlay,
  557. bool isEligibleForDirectStream)
  558. {
  559. DeviceProfile profile = options.Profile;
  560. if (options.ForceDirectPlay)
  561. {
  562. return PlayMethod.DirectPlay;
  563. }
  564. if (options.ForceDirectStream)
  565. {
  566. return PlayMethod.DirectStream;
  567. }
  568. if (videoStream == null)
  569. {
  570. _logger.Info("Profile: {0}, Cannot direct stream with no known video stream. Path: {1}",
  571. profile.Name ?? "Unknown Profile",
  572. mediaSource.Path ?? "Unknown path");
  573. return null;
  574. }
  575. // See if it can be direct played
  576. DirectPlayProfile directPlay = null;
  577. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  578. {
  579. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  580. {
  581. directPlay = i;
  582. break;
  583. }
  584. }
  585. if (directPlay == null)
  586. {
  587. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  588. profile.Name ?? "Unknown Profile",
  589. mediaSource.Path ?? "Unknown path");
  590. return null;
  591. }
  592. string container = mediaSource.Container;
  593. List<ProfileCondition> conditions = new List<ProfileCondition>();
  594. foreach (ContainerProfile i in profile.ContainerProfiles)
  595. {
  596. if (i.Type == DlnaProfileType.Video &&
  597. i.ContainsContainer(container))
  598. {
  599. foreach (ProfileCondition c in i.Conditions)
  600. {
  601. conditions.Add(c);
  602. }
  603. }
  604. }
  605. ConditionProcessor conditionProcessor = new ConditionProcessor();
  606. int? width = videoStream == null ? null : videoStream.Width;
  607. int? height = videoStream == null ? null : videoStream.Height;
  608. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  609. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  610. double? videoLevel = videoStream == null ? null : videoStream.Level;
  611. string videoProfile = videoStream == null ? null : videoStream.Profile;
  612. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  613. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  614. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  615. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  616. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  617. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  618. string audioProfile = audioStream == null ? null : audioStream.Profile;
  619. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  620. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  621. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  622. int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
  623. int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
  624. // Check container conditions
  625. foreach (ProfileCondition i in conditions)
  626. {
  627. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  628. {
  629. LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
  630. return null;
  631. }
  632. }
  633. string videoCodec = videoStream == null ? null : videoStream.Codec;
  634. if (string.IsNullOrEmpty(videoCodec))
  635. {
  636. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown video codec. Path: {1}",
  637. profile.Name ?? "Unknown Profile",
  638. mediaSource.Path ?? "Unknown path");
  639. return null;
  640. }
  641. conditions = new List<ProfileCondition>();
  642. foreach (CodecProfile i in profile.CodecProfiles)
  643. {
  644. if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec, container))
  645. {
  646. bool applyConditions = true;
  647. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  648. {
  649. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  650. {
  651. LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
  652. applyConditions = false;
  653. break;
  654. }
  655. }
  656. if (applyConditions)
  657. {
  658. foreach (ProfileCondition c in i.Conditions)
  659. {
  660. conditions.Add(c);
  661. }
  662. }
  663. }
  664. }
  665. foreach (ProfileCondition i in conditions)
  666. {
  667. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  668. {
  669. LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
  670. return null;
  671. }
  672. }
  673. if (audioStream != null)
  674. {
  675. string audioCodec = audioStream.Codec;
  676. if (string.IsNullOrEmpty(audioCodec))
  677. {
  678. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown audio codec. Path: {1}",
  679. profile.Name ?? "Unknown Profile",
  680. mediaSource.Path ?? "Unknown path");
  681. return null;
  682. }
  683. conditions = new List<ProfileCondition>();
  684. bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
  685. foreach (CodecProfile i in profile.CodecProfiles)
  686. {
  687. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec, container))
  688. {
  689. bool applyConditions = true;
  690. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  691. {
  692. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioProfile, isSecondaryAudio))
  693. {
  694. LogConditionFailure(profile, "VideoAudioCodecProfile", applyCondition, mediaSource);
  695. applyConditions = false;
  696. break;
  697. }
  698. }
  699. if (applyConditions)
  700. {
  701. foreach (ProfileCondition c in i.Conditions)
  702. {
  703. conditions.Add(c);
  704. }
  705. }
  706. }
  707. }
  708. foreach (ProfileCondition i in conditions)
  709. {
  710. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile, isSecondaryAudio))
  711. {
  712. LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
  713. return null;
  714. }
  715. }
  716. }
  717. if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
  718. {
  719. return PlayMethod.DirectStream;
  720. }
  721. return null;
  722. }
  723. private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
  724. {
  725. _logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
  726. type,
  727. profile.Name ?? "Unknown Profile",
  728. condition.Property,
  729. condition.Condition,
  730. condition.Value ?? string.Empty,
  731. condition.IsRequired,
  732. mediaSource.Path ?? "Unknown path");
  733. }
  734. private bool IsEligibleForDirectPlay(MediaSourceInfo item,
  735. int? maxBitrate,
  736. MediaStream subtitleStream,
  737. VideoOptions options,
  738. PlayMethod playMethod)
  739. {
  740. if (subtitleStream != null)
  741. {
  742. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, playMethod);
  743. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  744. {
  745. _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
  746. return false;
  747. }
  748. }
  749. return IsAudioEligibleForDirectPlay(item, maxBitrate);
  750. }
  751. public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod)
  752. {
  753. if (playMethod != PlayMethod.Transcode && !subtitleStream.IsExternal)
  754. {
  755. // Look for supported embedded subs
  756. foreach (SubtitleProfile profile in subtitleProfiles)
  757. {
  758. if (!profile.SupportsLanguage(subtitleStream.Language))
  759. {
  760. continue;
  761. }
  762. if (profile.Method != SubtitleDeliveryMethod.Embed)
  763. {
  764. continue;
  765. }
  766. if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
  767. {
  768. return profile;
  769. }
  770. }
  771. }
  772. // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
  773. return GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, false) ?? GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, true) ?? new SubtitleProfile
  774. {
  775. Method = SubtitleDeliveryMethod.Encode,
  776. Format = subtitleStream.Codec
  777. };
  778. }
  779. private static SubtitleProfile GetExternalSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, bool allowConversion)
  780. {
  781. foreach (SubtitleProfile profile in subtitleProfiles)
  782. {
  783. if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
  784. {
  785. continue;
  786. }
  787. if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
  788. {
  789. continue;
  790. }
  791. if (!profile.SupportsLanguage(subtitleStream.Language))
  792. {
  793. continue;
  794. }
  795. if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
  796. (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
  797. {
  798. bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
  799. if (!requiresConversion)
  800. {
  801. return profile;
  802. }
  803. if (!allowConversion)
  804. {
  805. continue;
  806. }
  807. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  808. {
  809. return profile;
  810. }
  811. }
  812. }
  813. return null;
  814. }
  815. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
  816. {
  817. if (!maxBitrate.HasValue)
  818. {
  819. _logger.Info("Cannot direct play due to unknown supported bitrate");
  820. return false;
  821. }
  822. if (!item.Bitrate.HasValue)
  823. {
  824. _logger.Info("Cannot direct play due to unknown content bitrate");
  825. return false;
  826. }
  827. if (item.Bitrate.Value > maxBitrate.Value)
  828. {
  829. _logger.Info("Bitrate exceeds DirectPlay limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), maxBitrate.Value.ToString(CultureInfo.InvariantCulture));
  830. return false;
  831. }
  832. return true;
  833. }
  834. private void ValidateInput(VideoOptions options)
  835. {
  836. ValidateAudioInput(options);
  837. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  838. {
  839. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  840. }
  841. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  842. {
  843. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  844. }
  845. }
  846. private void ValidateAudioInput(AudioOptions options)
  847. {
  848. if (string.IsNullOrEmpty(options.ItemId))
  849. {
  850. throw new ArgumentException("ItemId is required");
  851. }
  852. if (string.IsNullOrEmpty(options.DeviceId))
  853. {
  854. throw new ArgumentException("DeviceId is required");
  855. }
  856. if (options.Profile == null)
  857. {
  858. throw new ArgumentException("Profile is required");
  859. }
  860. if (options.MediaSources == null)
  861. {
  862. throw new ArgumentException("MediaSources is required");
  863. }
  864. }
  865. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  866. {
  867. foreach (ProfileCondition condition in conditions)
  868. {
  869. string value = condition.Value;
  870. if (string.IsNullOrEmpty(value))
  871. {
  872. continue;
  873. }
  874. // No way to express this
  875. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  876. {
  877. continue;
  878. }
  879. switch (condition.Property)
  880. {
  881. case ProfileConditionValue.AudioBitrate:
  882. {
  883. int num;
  884. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  885. {
  886. item.AudioBitrate = num;
  887. }
  888. break;
  889. }
  890. case ProfileConditionValue.AudioChannels:
  891. {
  892. int num;
  893. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  894. {
  895. item.MaxAudioChannels = num;
  896. }
  897. break;
  898. }
  899. case ProfileConditionValue.IsAvc:
  900. {
  901. bool isAvc;
  902. if (bool.TryParse(value, out isAvc))
  903. {
  904. if (isAvc && condition.Condition == ProfileConditionType.Equals)
  905. {
  906. item.RequireAvc = true;
  907. }
  908. else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
  909. {
  910. item.RequireAvc = true;
  911. }
  912. }
  913. break;
  914. }
  915. case ProfileConditionValue.IsAnamorphic:
  916. case ProfileConditionValue.AudioProfile:
  917. case ProfileConditionValue.Has64BitOffsets:
  918. case ProfileConditionValue.PacketLength:
  919. case ProfileConditionValue.NumAudioStreams:
  920. case ProfileConditionValue.NumVideoStreams:
  921. case ProfileConditionValue.IsSecondaryAudio:
  922. case ProfileConditionValue.VideoTimestamp:
  923. {
  924. // Not supported yet
  925. break;
  926. }
  927. case ProfileConditionValue.RefFrames:
  928. {
  929. int num;
  930. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  931. {
  932. item.MaxRefFrames = num;
  933. }
  934. break;
  935. }
  936. case ProfileConditionValue.VideoBitDepth:
  937. {
  938. int num;
  939. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  940. {
  941. item.MaxVideoBitDepth = num;
  942. }
  943. break;
  944. }
  945. case ProfileConditionValue.VideoProfile:
  946. {
  947. item.VideoProfile = (value ?? string.Empty).Split('|')[0];
  948. break;
  949. }
  950. case ProfileConditionValue.Height:
  951. {
  952. int num;
  953. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  954. {
  955. item.MaxHeight = num;
  956. }
  957. break;
  958. }
  959. case ProfileConditionValue.VideoBitrate:
  960. {
  961. int num;
  962. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  963. {
  964. item.VideoBitrate = num;
  965. }
  966. break;
  967. }
  968. case ProfileConditionValue.VideoFramerate:
  969. {
  970. float num;
  971. if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  972. {
  973. item.MaxFramerate = num;
  974. }
  975. break;
  976. }
  977. case ProfileConditionValue.VideoLevel:
  978. {
  979. int num;
  980. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  981. {
  982. item.VideoLevel = num;
  983. }
  984. break;
  985. }
  986. case ProfileConditionValue.Width:
  987. {
  988. int num;
  989. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  990. {
  991. item.MaxWidth = num;
  992. }
  993. break;
  994. }
  995. default:
  996. break;
  997. }
  998. }
  999. }
  1000. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  1001. {
  1002. if (profile.Container.Length > 0)
  1003. {
  1004. // Check container type
  1005. string mediaContainer = item.Container ?? string.Empty;
  1006. bool any = false;
  1007. foreach (string i in profile.GetContainers())
  1008. {
  1009. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  1010. {
  1011. any = true;
  1012. break;
  1013. }
  1014. }
  1015. if (!any)
  1016. {
  1017. return false;
  1018. }
  1019. }
  1020. // Check audio codec
  1021. List<string> audioCodecs = profile.GetAudioCodecs();
  1022. if (audioCodecs.Count > 0)
  1023. {
  1024. // Check audio codecs
  1025. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1026. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1027. {
  1028. return false;
  1029. }
  1030. }
  1031. return true;
  1032. }
  1033. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  1034. {
  1035. if (profile.Container.Length > 0)
  1036. {
  1037. // Check container type
  1038. string mediaContainer = item.Container ?? string.Empty;
  1039. bool any = false;
  1040. foreach (string i in profile.GetContainers())
  1041. {
  1042. if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
  1043. {
  1044. any = true;
  1045. break;
  1046. }
  1047. }
  1048. if (!any)
  1049. {
  1050. return false;
  1051. }
  1052. }
  1053. // Check video codec
  1054. List<string> videoCodecs = profile.GetVideoCodecs();
  1055. if (videoCodecs.Count > 0)
  1056. {
  1057. string videoCodec = videoStream == null ? null : videoStream.Codec;
  1058. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  1059. {
  1060. return false;
  1061. }
  1062. }
  1063. // Check audio codec
  1064. List<string> audioCodecs = profile.GetAudioCodecs();
  1065. if (audioCodecs.Count > 0)
  1066. {
  1067. // Check audio codecs
  1068. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1069. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1070. {
  1071. return false;
  1072. }
  1073. }
  1074. return true;
  1075. }
  1076. }
  1077. }