StreamBuilder.cs 51 KB

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