StreamBuilder.cs 50 KB

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