StreamBuilder.cs 54 KB

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