StreamBuilder.cs 57 KB

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