StreamBuilder.cs 51 KB

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