StreamBuilder.cs 51 KB

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