StreamBuilder.cs 44 KB

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