StreamBuilder.cs 52 KB

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