StreamBuilder.cs 57 KB

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