StreamBuilder.cs 57 KB

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