StreamBuilder.cs 54 KB

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