StreamBuilder.cs 56 KB

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