StreamBuilder.cs 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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. using System.Linq;
  11. namespace MediaBrowser.Model.Dlna
  12. {
  13. public class StreamBuilder
  14. {
  15. private readonly ILogger _logger;
  16. private readonly ITranscoderSupport _transcoderSupport;
  17. public StreamBuilder(ITranscoderSupport transcoderSupport, ILogger logger)
  18. {
  19. _transcoderSupport = transcoderSupport;
  20. _logger = logger;
  21. }
  22. public StreamBuilder(ILogger logger)
  23. : this(new FullTranscoderSupport(), logger)
  24. {
  25. }
  26. public StreamInfo BuildAudioItem(AudioOptions options)
  27. {
  28. ValidateAudioInput(options);
  29. var mediaSources = new List<MediaSourceInfo>();
  30. foreach (MediaSourceInfo i in options.MediaSources)
  31. {
  32. if (string.IsNullOrEmpty(options.MediaSourceId) ||
  33. StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
  34. {
  35. mediaSources.Add(i);
  36. }
  37. }
  38. var streams = new List<StreamInfo>();
  39. foreach (MediaSourceInfo i in mediaSources)
  40. {
  41. StreamInfo streamInfo = BuildAudioItem(i, options);
  42. if (streamInfo != null)
  43. {
  44. streams.Add(streamInfo);
  45. }
  46. }
  47. foreach (StreamInfo stream in streams)
  48. {
  49. stream.DeviceId = options.DeviceId;
  50. stream.DeviceProfileId = options.Profile.Id;
  51. }
  52. return GetOptimalStream(streams, options.GetMaxBitrate(true));
  53. }
  54. public StreamInfo BuildVideoItem(VideoOptions options)
  55. {
  56. ValidateInput(options);
  57. var mediaSources = new List<MediaSourceInfo>();
  58. foreach (MediaSourceInfo i in options.MediaSources)
  59. {
  60. if (string.IsNullOrEmpty(options.MediaSourceId) ||
  61. StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
  62. {
  63. mediaSources.Add(i);
  64. }
  65. }
  66. var streams = new List<StreamInfo>();
  67. foreach (MediaSourceInfo i in mediaSources)
  68. {
  69. StreamInfo streamInfo = BuildVideoItem(i, options);
  70. if (streamInfo != null)
  71. {
  72. streams.Add(streamInfo);
  73. }
  74. }
  75. foreach (StreamInfo stream in streams)
  76. {
  77. stream.DeviceId = options.DeviceId;
  78. stream.DeviceProfileId = options.Profile.Id;
  79. }
  80. return GetOptimalStream(streams, options.GetMaxBitrate(false));
  81. }
  82. private StreamInfo GetOptimalStream(List<StreamInfo> streams, long? maxBitrate)
  83. {
  84. var sorted = StreamInfoSorter.SortMediaSources(streams, maxBitrate);
  85. foreach (StreamInfo stream in sorted)
  86. {
  87. return stream;
  88. }
  89. return null;
  90. }
  91. private TranscodeReason? GetTranscodeReasonForFailedCondition(ProfileCondition condition)
  92. {
  93. switch (condition.Property)
  94. {
  95. case ProfileConditionValue.AudioBitrate:
  96. if (condition.Condition == ProfileConditionType.LessThanEqual)
  97. {
  98. return TranscodeReason.AudioBitrateNotSupported;
  99. }
  100. return TranscodeReason.AudioBitrateNotSupported;
  101. case ProfileConditionValue.AudioChannels:
  102. if (condition.Condition == ProfileConditionType.LessThanEqual)
  103. {
  104. return TranscodeReason.AudioChannelsNotSupported;
  105. }
  106. return TranscodeReason.AudioChannelsNotSupported;
  107. case ProfileConditionValue.AudioProfile:
  108. return TranscodeReason.AudioProfileNotSupported;
  109. case ProfileConditionValue.AudioSampleRate:
  110. return TranscodeReason.AudioSampleRateNotSupported;
  111. case ProfileConditionValue.Has64BitOffsets:
  112. // TODO
  113. return null;
  114. case ProfileConditionValue.Height:
  115. return TranscodeReason.VideoResolutionNotSupported;
  116. case ProfileConditionValue.IsAnamorphic:
  117. return TranscodeReason.AnamorphicVideoNotSupported;
  118. case ProfileConditionValue.IsAvc:
  119. // TODO
  120. return null;
  121. case ProfileConditionValue.IsInterlaced:
  122. return TranscodeReason.InterlacedVideoNotSupported;
  123. case ProfileConditionValue.IsSecondaryAudio:
  124. return TranscodeReason.SecondaryAudioNotSupported;
  125. case ProfileConditionValue.NumAudioStreams:
  126. // TODO
  127. return null;
  128. case ProfileConditionValue.NumVideoStreams:
  129. // TODO
  130. return null;
  131. case ProfileConditionValue.PacketLength:
  132. // TODO
  133. return null;
  134. case ProfileConditionValue.RefFrames:
  135. return TranscodeReason.RefFramesNotSupported;
  136. case ProfileConditionValue.VideoBitDepth:
  137. return TranscodeReason.VideoBitDepthNotSupported;
  138. case ProfileConditionValue.AudioBitDepth:
  139. return TranscodeReason.AudioBitDepthNotSupported;
  140. case ProfileConditionValue.VideoBitrate:
  141. return TranscodeReason.VideoBitrateNotSupported;
  142. case ProfileConditionValue.VideoCodecTag:
  143. return TranscodeReason.VideoCodecNotSupported;
  144. case ProfileConditionValue.VideoFramerate:
  145. return TranscodeReason.VideoFramerateNotSupported;
  146. case ProfileConditionValue.VideoLevel:
  147. return TranscodeReason.VideoLevelNotSupported;
  148. case ProfileConditionValue.VideoProfile:
  149. return TranscodeReason.VideoProfileNotSupported;
  150. case ProfileConditionValue.VideoTimestamp:
  151. // TODO
  152. return null;
  153. case ProfileConditionValue.Width:
  154. return TranscodeReason.VideoResolutionNotSupported;
  155. default:
  156. return null;
  157. }
  158. }
  159. public static string NormalizeMediaSourceFormatIntoSingleContainer(string inputContainer, DeviceProfile profile, DlnaProfileType type)
  160. {
  161. if (string.IsNullOrWhiteSpace(inputContainer))
  162. {
  163. return null;
  164. }
  165. var formats = ContainerProfile.SplitValue(inputContainer);
  166. if (formats.Length == 1)
  167. {
  168. return formats[0];
  169. }
  170. if (profile != null)
  171. {
  172. foreach (var format in formats)
  173. {
  174. foreach (var directPlayProfile in profile.DirectPlayProfiles)
  175. {
  176. if (directPlayProfile.Type == type)
  177. {
  178. if (directPlayProfile.SupportsContainer(format))
  179. {
  180. return format;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return formats[0];
  187. }
  188. private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
  189. {
  190. var transcodeReasons = new List<TranscodeReason>();
  191. StreamInfo playlistItem = new StreamInfo
  192. {
  193. ItemId = options.ItemId,
  194. MediaType = DlnaProfileType.Audio,
  195. MediaSource = item,
  196. RunTimeTicks = item.RunTimeTicks,
  197. Context = options.Context,
  198. DeviceProfile = options.Profile
  199. };
  200. if (options.ForceDirectPlay)
  201. {
  202. playlistItem.PlayMethod = PlayMethod.DirectPlay;
  203. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
  204. return playlistItem;
  205. }
  206. if (options.ForceDirectStream)
  207. {
  208. playlistItem.PlayMethod = PlayMethod.DirectStream;
  209. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
  210. return playlistItem;
  211. }
  212. MediaStream audioStream = item.GetDefaultAudioStream(null);
  213. var directPlayInfo = GetAudioDirectPlayMethods(item, audioStream, options);
  214. var directPlayMethods = directPlayInfo.Item1;
  215. transcodeReasons.AddRange(directPlayInfo.Item2);
  216. ConditionProcessor conditionProcessor = new ConditionProcessor();
  217. int? inputAudioChannels = audioStream == null ? null : audioStream.Channels;
  218. int? inputAudioBitrate = audioStream == null ? null : audioStream.BitDepth;
  219. int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  220. int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  221. if (directPlayMethods.Count > 0)
  222. {
  223. string audioCodec = audioStream == null ? null : audioStream.Codec;
  224. // Make sure audio codec profiles are satisfied
  225. if (!string.IsNullOrEmpty(audioCodec))
  226. {
  227. var conditions = new List<ProfileCondition>();
  228. foreach (CodecProfile i in options.Profile.CodecProfiles)
  229. {
  230. if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec, item.Container))
  231. {
  232. bool applyConditions = true;
  233. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  234. {
  235. if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
  236. {
  237. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  238. applyConditions = false;
  239. break;
  240. }
  241. }
  242. if (applyConditions)
  243. {
  244. foreach (ProfileCondition c in i.Conditions)
  245. {
  246. conditions.Add(c);
  247. }
  248. }
  249. }
  250. }
  251. bool all = true;
  252. foreach (ProfileCondition c in conditions)
  253. {
  254. if (!conditionProcessor.IsAudioConditionSatisfied(c, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
  255. {
  256. LogConditionFailure(options.Profile, "AudioCodecProfile", c, item);
  257. var transcodeReason = GetTranscodeReasonForFailedCondition(c);
  258. if (transcodeReason.HasValue)
  259. {
  260. transcodeReasons.Add(transcodeReason.Value);
  261. }
  262. all = false;
  263. break;
  264. }
  265. }
  266. if (all)
  267. {
  268. if (directPlayMethods.Contains(PlayMethod.DirectStream))
  269. {
  270. playlistItem.PlayMethod = PlayMethod.DirectStream;
  271. }
  272. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
  273. return playlistItem;
  274. }
  275. }
  276. }
  277. TranscodingProfile transcodingProfile = null;
  278. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  279. {
  280. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  281. {
  282. if (_transcoderSupport.CanEncodeToAudioCodec(i.AudioCodec ?? i.Container))
  283. {
  284. transcodingProfile = i;
  285. break;
  286. }
  287. }
  288. }
  289. if (transcodingProfile != null)
  290. {
  291. if (!item.SupportsTranscoding)
  292. {
  293. return null;
  294. }
  295. playlistItem.PlayMethod = PlayMethod.Transcode;
  296. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  297. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  298. playlistItem.Container = transcodingProfile.Container;
  299. if (string.IsNullOrEmpty(transcodingProfile.AudioCodec))
  300. {
  301. playlistItem.AudioCodecs = new string[] { };
  302. }
  303. else
  304. {
  305. playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
  306. }
  307. playlistItem.SubProtocol = transcodingProfile.Protocol;
  308. var audioCodecProfiles = new List<CodecProfile>();
  309. foreach (CodecProfile i in options.Profile.CodecProfiles)
  310. {
  311. if (i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec, transcodingProfile.Container))
  312. {
  313. audioCodecProfiles.Add(i);
  314. }
  315. if (audioCodecProfiles.Count >= 1) break;
  316. }
  317. var audioTranscodingConditions = new List<ProfileCondition>();
  318. foreach (CodecProfile i in audioCodecProfiles)
  319. {
  320. bool applyConditions = true;
  321. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  322. {
  323. if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
  324. {
  325. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  326. applyConditions = false;
  327. break;
  328. }
  329. }
  330. if (applyConditions)
  331. {
  332. foreach (ProfileCondition c in i.Conditions)
  333. {
  334. audioTranscodingConditions.Add(c);
  335. }
  336. }
  337. }
  338. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  339. // Honor requested max channels
  340. if (options.MaxAudioChannels.HasValue)
  341. {
  342. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  343. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  344. }
  345. long transcodingBitrate = options.AudioTranscodingBitrate ??
  346. options.Profile.MusicStreamingTranscodingBitrate ??
  347. 128000;
  348. var configuredBitrate = options.GetMaxBitrate(true);
  349. if (configuredBitrate.HasValue)
  350. {
  351. transcodingBitrate = Math.Min(configuredBitrate.Value, transcodingBitrate);
  352. }
  353. var longBitrate = Math.Min(transcodingBitrate, playlistItem.AudioBitrate ?? transcodingBitrate);
  354. playlistItem.AudioBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
  355. }
  356. playlistItem.TranscodeReasons = transcodeReasons;
  357. return playlistItem;
  358. }
  359. private long? GetBitrateForDirectPlayCheck(MediaSourceInfo item, AudioOptions options, bool isAudio)
  360. {
  361. if (item.Protocol == MediaProtocol.File)
  362. {
  363. return options.Profile.MaxStaticBitrate;
  364. }
  365. return options.GetMaxBitrate(isAudio);
  366. }
  367. private Tuple<List<PlayMethod>, List<TranscodeReason>> GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
  368. {
  369. var transcodeReasons = new List<TranscodeReason>();
  370. DirectPlayProfile directPlayProfile = null;
  371. foreach (DirectPlayProfile i in options.Profile.DirectPlayProfiles)
  372. {
  373. if (i.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(i, item, audioStream))
  374. {
  375. directPlayProfile = i;
  376. break;
  377. }
  378. }
  379. var playMethods = new List<PlayMethod>();
  380. if (directPlayProfile != null)
  381. {
  382. // While options takes the network and other factors into account. Only applies to direct stream
  383. if (item.SupportsDirectStream)
  384. {
  385. if (IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate(true), PlayMethod.DirectStream))
  386. {
  387. if (options.EnableDirectStream)
  388. {
  389. playMethods.Add(PlayMethod.DirectStream);
  390. }
  391. }
  392. else
  393. {
  394. transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
  395. }
  396. }
  397. // The profile describes what the device supports
  398. // If device requirements are satisfied then allow both direct stream and direct play
  399. if (item.SupportsDirectPlay)
  400. {
  401. if (IsAudioEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), PlayMethod.DirectPlay))
  402. {
  403. if (options.EnableDirectPlay)
  404. {
  405. playMethods.Add(PlayMethod.DirectPlay);
  406. }
  407. }
  408. else
  409. {
  410. transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
  411. }
  412. }
  413. }
  414. else
  415. {
  416. transcodeReasons.InsertRange(0, GetTranscodeReasonsFromDirectPlayProfile(item, null, audioStream, options.Profile.DirectPlayProfiles));
  417. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  418. options.Profile.Name ?? "Unknown Profile",
  419. item.Path ?? "Unknown path");
  420. }
  421. if (playMethods.Count > 0)
  422. {
  423. transcodeReasons.Clear();
  424. }
  425. else
  426. {
  427. transcodeReasons = transcodeReasons.Distinct().ToList();
  428. }
  429. return new Tuple<List<PlayMethod>, List<TranscodeReason>>(playMethods, transcodeReasons);
  430. }
  431. private List<TranscodeReason> GetTranscodeReasonsFromDirectPlayProfile(MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream, IEnumerable<DirectPlayProfile> directPlayProfiles)
  432. {
  433. var list = new List<TranscodeReason>();
  434. var containerSupported = false;
  435. var audioSupported = false;
  436. var videoSupported = false;
  437. foreach (var profile in directPlayProfiles)
  438. {
  439. // Check container type
  440. if (profile.SupportsContainer(item.Container))
  441. {
  442. containerSupported = true;
  443. if (videoStream != null)
  444. {
  445. // Check video codec
  446. var videoCodecs = profile.GetVideoCodecs();
  447. if (videoCodecs.Length > 0)
  448. {
  449. string videoCodec = videoStream.Codec;
  450. if (!string.IsNullOrEmpty(videoCodec) && ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  451. {
  452. videoSupported = true;
  453. }
  454. }
  455. else
  456. {
  457. videoSupported = true;
  458. }
  459. }
  460. if (audioStream != null)
  461. {
  462. // Check audio codec
  463. var audioCodecs = profile.GetAudioCodecs();
  464. if (audioCodecs.Length > 0)
  465. {
  466. string audioCodec = audioStream.Codec;
  467. if (!string.IsNullOrEmpty(audioCodec) && ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  468. {
  469. audioSupported = true;
  470. }
  471. }
  472. else
  473. {
  474. audioSupported = true;
  475. }
  476. }
  477. }
  478. }
  479. if (!containerSupported)
  480. {
  481. list.Add(TranscodeReason.ContainerNotSupported);
  482. }
  483. if (videoStream != null && !videoSupported)
  484. {
  485. list.Add(TranscodeReason.VideoCodecNotSupported);
  486. }
  487. if (audioStream != null && !audioSupported)
  488. {
  489. list.Add(TranscodeReason.AudioCodecNotSupported);
  490. }
  491. return list;
  492. }
  493. private int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles)
  494. {
  495. int highestScore = -1;
  496. foreach (MediaStream stream in item.MediaStreams)
  497. {
  498. if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue)
  499. {
  500. if (stream.Score.Value > highestScore)
  501. {
  502. highestScore = stream.Score.Value;
  503. }
  504. }
  505. }
  506. var topStreams = new List<MediaStream>();
  507. foreach (MediaStream stream in item.MediaStreams)
  508. {
  509. if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue && stream.Score.Value == highestScore)
  510. {
  511. topStreams.Add(stream);
  512. }
  513. }
  514. // If multiple streams have an equal score, try to pick the most efficient one
  515. if (topStreams.Count > 1)
  516. {
  517. foreach (MediaStream stream in topStreams)
  518. {
  519. foreach (SubtitleProfile profile in subtitleProfiles)
  520. {
  521. if (profile.Method == SubtitleDeliveryMethod.External && StringHelper.EqualsIgnoreCase(profile.Format, stream.Codec))
  522. {
  523. return stream.Index;
  524. }
  525. }
  526. }
  527. }
  528. // If no optimization panned out, just use the original default
  529. return item.DefaultSubtitleStreamIndex;
  530. }
  531. private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
  532. {
  533. if (item == null)
  534. {
  535. throw new ArgumentNullException("item");
  536. }
  537. var transcodeReasons = new List<TranscodeReason>();
  538. StreamInfo playlistItem = new StreamInfo
  539. {
  540. ItemId = options.ItemId,
  541. MediaType = DlnaProfileType.Video,
  542. MediaSource = item,
  543. RunTimeTicks = item.RunTimeTicks,
  544. Context = options.Context,
  545. DeviceProfile = options.Profile
  546. };
  547. playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? GetDefaultSubtitleStreamIndex(item, options.Profile.SubtitleProfiles);
  548. MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
  549. MediaStream audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
  550. int? audioStreamIndex = null;
  551. if (audioStream != null)
  552. {
  553. audioStreamIndex = audioStream.Index;
  554. }
  555. MediaStream videoStream = item.VideoStream;
  556. // TODO: This doesn't accout for situation of device being able to handle media bitrate, but wifi connection not fast enough
  557. var directPlayEligibilityResult = IsEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), subtitleStream, options, PlayMethod.DirectPlay);
  558. var directStreamEligibilityResult = IsEligibleForDirectPlay(item, options.GetMaxBitrate(false), subtitleStream, options, PlayMethod.DirectStream);
  559. bool isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || directPlayEligibilityResult.Item1);
  560. bool isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || directStreamEligibilityResult.Item1);
  561. _logger.Info("Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
  562. options.Profile.Name ?? "Unknown Profile",
  563. item.Path ?? "Unknown path",
  564. isEligibleForDirectPlay,
  565. isEligibleForDirectStream);
  566. if (isEligibleForDirectPlay || isEligibleForDirectStream)
  567. {
  568. // See if it can be direct played
  569. var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, isEligibleForDirectPlay, isEligibleForDirectStream);
  570. var directPlay = directPlayInfo.Item1;
  571. if (directPlay != null)
  572. {
  573. playlistItem.PlayMethod = directPlay.Value;
  574. playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Video);
  575. if (subtitleStream != null)
  576. {
  577. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, directPlay.Value, _transcoderSupport, null, null);
  578. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  579. playlistItem.SubtitleFormat = subtitleProfile.Format;
  580. }
  581. return playlistItem;
  582. }
  583. transcodeReasons.AddRange(directPlayInfo.Item2);
  584. }
  585. if (directPlayEligibilityResult.Item2.HasValue)
  586. {
  587. transcodeReasons.Add(directPlayEligibilityResult.Item2.Value);
  588. }
  589. if (directStreamEligibilityResult.Item2.HasValue)
  590. {
  591. transcodeReasons.Add(directStreamEligibilityResult.Item2.Value);
  592. }
  593. // Can't direct play, find the transcoding profile
  594. TranscodingProfile transcodingProfile = null;
  595. foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
  596. {
  597. if (i.Type == playlistItem.MediaType && i.Context == options.Context)
  598. {
  599. transcodingProfile = i;
  600. break;
  601. }
  602. }
  603. if (transcodingProfile != null)
  604. {
  605. if (!item.SupportsTranscoding)
  606. {
  607. return null;
  608. }
  609. if (subtitleStream != null)
  610. {
  611. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.Transcode, _transcoderSupport, transcodingProfile.Protocol, transcodingProfile.Container);
  612. playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
  613. playlistItem.SubtitleFormat = subtitleProfile.Format;
  614. playlistItem.SubtitleCodecs = new[] { subtitleProfile.Format };
  615. }
  616. playlistItem.PlayMethod = PlayMethod.Transcode;
  617. playlistItem.Container = transcodingProfile.Container;
  618. playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
  619. playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  620. playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
  621. playlistItem.VideoCodecs = transcodingProfile.VideoCodec.Split(',');
  622. playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
  623. playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
  624. playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames;
  625. if (transcodingProfile.MinSegments > 0)
  626. {
  627. playlistItem.MinSegments = transcodingProfile.MinSegments;
  628. }
  629. if (transcodingProfile.SegmentLength > 0)
  630. {
  631. playlistItem.SegmentLength = transcodingProfile.SegmentLength;
  632. }
  633. if (!string.IsNullOrEmpty(transcodingProfile.MaxAudioChannels))
  634. {
  635. int transcodingMaxAudioChannels;
  636. if (int.TryParse(transcodingProfile.MaxAudioChannels, NumberStyles.Any, CultureInfo.InvariantCulture, out transcodingMaxAudioChannels))
  637. {
  638. playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
  639. }
  640. }
  641. playlistItem.SubProtocol = transcodingProfile.Protocol;
  642. playlistItem.AudioStreamIndex = audioStreamIndex;
  643. ConditionProcessor conditionProcessor = new ConditionProcessor();
  644. var videoTranscodingConditions = new List<ProfileCondition>();
  645. foreach (CodecProfile i in options.Profile.CodecProfiles)
  646. {
  647. if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
  648. {
  649. bool applyConditions = true;
  650. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  651. {
  652. bool? isSecondaryAudio = audioStream == null ? null : item.IsSecondaryAudio(audioStream);
  653. int? inputAudioBitrate = audioStream == null ? null : audioStream.BitRate;
  654. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  655. string audioProfile = audioStream == null ? null : audioStream.Profile;
  656. int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  657. int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  658. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, audioProfile, isSecondaryAudio))
  659. {
  660. LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
  661. applyConditions = false;
  662. break;
  663. }
  664. }
  665. if (applyConditions)
  666. {
  667. foreach (ProfileCondition c in i.Conditions)
  668. {
  669. videoTranscodingConditions.Add(c);
  670. }
  671. break;
  672. }
  673. }
  674. }
  675. ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
  676. var audioTranscodingConditions = new List<ProfileCondition>();
  677. foreach (CodecProfile i in options.Profile.CodecProfiles)
  678. {
  679. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(playlistItem.TargetAudioCodec, transcodingProfile.Container))
  680. {
  681. bool applyConditions = true;
  682. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  683. {
  684. int? width = videoStream == null ? null : videoStream.Width;
  685. int? height = videoStream == null ? null : videoStream.Height;
  686. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  687. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  688. double? videoLevel = videoStream == null ? null : videoStream.Level;
  689. string videoProfile = videoStream == null ? null : videoStream.Profile;
  690. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  691. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  692. bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
  693. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  694. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  695. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
  696. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  697. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  698. int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
  699. int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
  700. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  701. {
  702. LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
  703. applyConditions = false;
  704. break;
  705. }
  706. }
  707. if (applyConditions)
  708. {
  709. foreach (ProfileCondition c in i.Conditions)
  710. {
  711. audioTranscodingConditions.Add(c);
  712. }
  713. break;
  714. }
  715. }
  716. }
  717. // Honor requested max channels
  718. if (options.MaxAudioChannels.HasValue)
  719. {
  720. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  721. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  722. }
  723. int audioBitrate = GetAudioBitrate(playlistItem.SubProtocol, options.GetMaxBitrate(false), playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec, audioStream);
  724. playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
  725. var maxBitrateSetting = options.GetMaxBitrate(false);
  726. // Honor max rate
  727. if (maxBitrateSetting.HasValue)
  728. {
  729. var videoBitrate = maxBitrateSetting.Value;
  730. if (playlistItem.AudioBitrate.HasValue)
  731. {
  732. videoBitrate -= playlistItem.AudioBitrate.Value;
  733. }
  734. // Make sure the video bitrate is lower than bitrate settings but at least 64k
  735. long currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  736. var longBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
  737. playlistItem.VideoBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
  738. }
  739. // Do this after initial values are set to account for greater than/less than conditions
  740. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
  741. }
  742. playlistItem.TranscodeReasons = transcodeReasons;
  743. return playlistItem;
  744. }
  745. private int GetDefaultAudioBitrateIfUnknown(MediaStream audioStream)
  746. {
  747. if ((audioStream.Channels ?? 0) >= 6)
  748. {
  749. return 384000;
  750. }
  751. return 192000;
  752. }
  753. private int GetAudioBitrate(string subProtocol, long? maxTotalBitrate, int? targetAudioChannels, string targetAudioCodec, MediaStream audioStream)
  754. {
  755. int defaultBitrate = audioStream == null ? 192000 : audioStream.BitRate ?? GetDefaultAudioBitrateIfUnknown(audioStream);
  756. // Reduce the bitrate if we're downmixing
  757. if (targetAudioChannels.HasValue && audioStream != null && audioStream.Channels.HasValue && targetAudioChannels.Value < audioStream.Channels.Value)
  758. {
  759. defaultBitrate = StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3") ? 192000 : 128000;
  760. }
  761. if (StringHelper.EqualsIgnoreCase(subProtocol, "hls"))
  762. {
  763. defaultBitrate = Math.Min(384000, defaultBitrate);
  764. }
  765. else
  766. {
  767. defaultBitrate = Math.Min(448000, defaultBitrate);
  768. }
  769. int encoderAudioBitrateLimit = int.MaxValue;
  770. if (audioStream != null)
  771. {
  772. // Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
  773. // Any attempts to transcode over 64k will fail
  774. if (audioStream.Channels.HasValue &&
  775. audioStream.Channels.Value == 1)
  776. {
  777. if ((audioStream.BitRate ?? 0) < 64000)
  778. {
  779. encoderAudioBitrateLimit = 64000;
  780. }
  781. }
  782. }
  783. if (maxTotalBitrate.HasValue)
  784. {
  785. if (maxTotalBitrate.Value < 640000)
  786. {
  787. defaultBitrate = Math.Min(128000, defaultBitrate);
  788. }
  789. }
  790. return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
  791. }
  792. private Tuple<PlayMethod?, List<TranscodeReason>> GetVideoDirectPlayProfile(VideoOptions options,
  793. MediaSourceInfo mediaSource,
  794. MediaStream videoStream,
  795. MediaStream audioStream,
  796. bool isEligibleForDirectPlay,
  797. bool isEligibleForDirectStream)
  798. {
  799. DeviceProfile profile = options.Profile;
  800. if (options.ForceDirectPlay)
  801. {
  802. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectPlay, new List<TranscodeReason>());
  803. }
  804. if (options.ForceDirectStream)
  805. {
  806. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
  807. }
  808. if (videoStream == null)
  809. {
  810. _logger.Info("Profile: {0}, Cannot direct stream with no known video stream. Path: {1}",
  811. profile.Name ?? "Unknown Profile",
  812. mediaSource.Path ?? "Unknown path");
  813. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownVideoStreamInfo });
  814. }
  815. // See if it can be direct played
  816. DirectPlayProfile directPlay = null;
  817. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  818. {
  819. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  820. {
  821. directPlay = i;
  822. break;
  823. }
  824. }
  825. if (directPlay == null)
  826. {
  827. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  828. profile.Name ?? "Unknown Profile",
  829. mediaSource.Path ?? "Unknown path");
  830. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, GetTranscodeReasonsFromDirectPlayProfile(mediaSource, videoStream, audioStream, profile.DirectPlayProfiles));
  831. }
  832. string container = mediaSource.Container;
  833. var conditions = new List<ProfileCondition>();
  834. foreach (ContainerProfile i in profile.ContainerProfiles)
  835. {
  836. if (i.Type == DlnaProfileType.Video &&
  837. i.ContainsContainer(container))
  838. {
  839. foreach (ProfileCondition c in i.Conditions)
  840. {
  841. conditions.Add(c);
  842. }
  843. }
  844. }
  845. ConditionProcessor conditionProcessor = new ConditionProcessor();
  846. int? width = videoStream == null ? null : videoStream.Width;
  847. int? height = videoStream == null ? null : videoStream.Height;
  848. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  849. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  850. double? videoLevel = videoStream == null ? null : videoStream.Level;
  851. string videoProfile = videoStream == null ? null : videoStream.Profile;
  852. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  853. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  854. bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
  855. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  856. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  857. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  858. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  859. string audioProfile = audioStream == null ? null : audioStream.Profile;
  860. int? audioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  861. int? audioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  862. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  863. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  864. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  865. int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
  866. int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
  867. // Check container conditions
  868. foreach (ProfileCondition i in conditions)
  869. {
  870. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  871. {
  872. LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
  873. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  874. var transcodeReasons = transcodeReason.HasValue
  875. ? new List<TranscodeReason> { transcodeReason.Value }
  876. : new List<TranscodeReason> { };
  877. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  878. }
  879. }
  880. string videoCodec = videoStream == null ? null : videoStream.Codec;
  881. if (string.IsNullOrEmpty(videoCodec))
  882. {
  883. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown video codec. Path: {1}",
  884. profile.Name ?? "Unknown Profile",
  885. mediaSource.Path ?? "Unknown path");
  886. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownVideoStreamInfo });
  887. }
  888. conditions = new List<ProfileCondition>();
  889. foreach (CodecProfile i in profile.CodecProfiles)
  890. {
  891. if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec, container))
  892. {
  893. bool applyConditions = true;
  894. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  895. {
  896. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  897. {
  898. LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
  899. applyConditions = false;
  900. break;
  901. }
  902. }
  903. if (applyConditions)
  904. {
  905. foreach (ProfileCondition c in i.Conditions)
  906. {
  907. conditions.Add(c);
  908. }
  909. }
  910. }
  911. }
  912. foreach (ProfileCondition i in conditions)
  913. {
  914. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  915. {
  916. LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
  917. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  918. var transcodeReasons = transcodeReason.HasValue
  919. ? new List<TranscodeReason> { transcodeReason.Value }
  920. : new List<TranscodeReason> { };
  921. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  922. }
  923. }
  924. if (audioStream != null)
  925. {
  926. string audioCodec = audioStream.Codec;
  927. if (string.IsNullOrEmpty(audioCodec))
  928. {
  929. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown audio codec. Path: {1}",
  930. profile.Name ?? "Unknown Profile",
  931. mediaSource.Path ?? "Unknown path");
  932. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownAudioStreamInfo });
  933. }
  934. conditions = new List<ProfileCondition>();
  935. bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
  936. foreach (CodecProfile i in profile.CodecProfiles)
  937. {
  938. if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec, container))
  939. {
  940. bool applyConditions = true;
  941. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  942. {
  943. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  944. {
  945. LogConditionFailure(profile, "VideoAudioCodecProfile.ApplyConditions", applyCondition, mediaSource);
  946. applyConditions = false;
  947. break;
  948. }
  949. }
  950. if (applyConditions)
  951. {
  952. foreach (ProfileCondition c in i.Conditions)
  953. {
  954. conditions.Add(c);
  955. }
  956. }
  957. }
  958. }
  959. foreach (ProfileCondition i in conditions)
  960. {
  961. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  962. {
  963. LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
  964. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  965. var transcodeReasons = transcodeReason.HasValue
  966. ? new List<TranscodeReason> { transcodeReason.Value }
  967. : new List<TranscodeReason> { };
  968. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  969. }
  970. }
  971. }
  972. if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
  973. {
  974. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
  975. }
  976. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.ContainerBitrateExceedsLimit });
  977. }
  978. private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
  979. {
  980. _logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
  981. type,
  982. profile.Name ?? "Unknown Profile",
  983. condition.Property,
  984. condition.Condition,
  985. condition.Value ?? string.Empty,
  986. condition.IsRequired,
  987. mediaSource.Path ?? "Unknown path");
  988. }
  989. private Tuple<bool, TranscodeReason?> IsEligibleForDirectPlay(MediaSourceInfo item,
  990. long? maxBitrate,
  991. MediaStream subtitleStream,
  992. VideoOptions options,
  993. PlayMethod playMethod)
  994. {
  995. if (subtitleStream != null)
  996. {
  997. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, playMethod, _transcoderSupport, null, null);
  998. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  999. {
  1000. _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
  1001. return new Tuple<bool, TranscodeReason?>(false, TranscodeReason.SubtitleCodecNotSupported);
  1002. }
  1003. }
  1004. var result = IsAudioEligibleForDirectPlay(item, maxBitrate, playMethod);
  1005. if (result)
  1006. {
  1007. return new Tuple<bool, TranscodeReason?>(result, null);
  1008. }
  1009. return new Tuple<bool, TranscodeReason?>(result, TranscodeReason.ContainerBitrateExceedsLimit);
  1010. }
  1011. public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, string transcodingSubProtocol, string transcodingContainer)
  1012. {
  1013. if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
  1014. {
  1015. // Look for supported embedded subs of the same format
  1016. foreach (SubtitleProfile profile in subtitleProfiles)
  1017. {
  1018. if (!profile.SupportsLanguage(subtitleStream.Language))
  1019. {
  1020. continue;
  1021. }
  1022. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1023. {
  1024. continue;
  1025. }
  1026. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1027. {
  1028. continue;
  1029. }
  1030. if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
  1031. {
  1032. return profile;
  1033. }
  1034. }
  1035. // Look for supported embedded subs of a convertible format
  1036. foreach (SubtitleProfile profile in subtitleProfiles)
  1037. {
  1038. if (!profile.SupportsLanguage(subtitleStream.Language))
  1039. {
  1040. continue;
  1041. }
  1042. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1043. {
  1044. continue;
  1045. }
  1046. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1047. {
  1048. continue;
  1049. }
  1050. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1051. {
  1052. return profile;
  1053. }
  1054. }
  1055. }
  1056. // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
  1057. return GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, transcoderSupport, false) ??
  1058. GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, transcoderSupport, true) ??
  1059. new SubtitleProfile
  1060. {
  1061. Method = SubtitleDeliveryMethod.Encode,
  1062. Format = subtitleStream.Codec
  1063. };
  1064. }
  1065. private static bool IsSubtitleEmbedSupported(MediaStream subtitleStream, SubtitleProfile subtitleProfile, string transcodingSubProtocol, string transcodingContainer)
  1066. {
  1067. if (!string.IsNullOrWhiteSpace(transcodingContainer))
  1068. {
  1069. var normalizedContainers = ContainerProfile.SplitValue(transcodingContainer);
  1070. if (ContainerProfile.ContainsContainer(normalizedContainers, "ts"))
  1071. {
  1072. return false;
  1073. }
  1074. if (ContainerProfile.ContainsContainer(normalizedContainers, "mpegts"))
  1075. {
  1076. return false;
  1077. }
  1078. if (ContainerProfile.ContainsContainer(normalizedContainers, "mp4"))
  1079. {
  1080. return false;
  1081. }
  1082. if (ContainerProfile.ContainsContainer(normalizedContainers, "mkv") ||
  1083. ContainerProfile.ContainsContainer(normalizedContainers, "matroska"))
  1084. {
  1085. return true;
  1086. }
  1087. }
  1088. return false;
  1089. }
  1090. private static SubtitleProfile GetExternalSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, bool allowConversion)
  1091. {
  1092. foreach (SubtitleProfile profile in subtitleProfiles)
  1093. {
  1094. if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
  1095. {
  1096. continue;
  1097. }
  1098. if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
  1099. {
  1100. continue;
  1101. }
  1102. if (!profile.SupportsLanguage(subtitleStream.Language))
  1103. {
  1104. continue;
  1105. }
  1106. if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec))
  1107. {
  1108. continue;
  1109. }
  1110. if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
  1111. (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
  1112. {
  1113. bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
  1114. if (!requiresConversion)
  1115. {
  1116. return profile;
  1117. }
  1118. if (!allowConversion)
  1119. {
  1120. continue;
  1121. }
  1122. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1123. {
  1124. return profile;
  1125. }
  1126. }
  1127. }
  1128. return null;
  1129. }
  1130. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long? maxBitrate, PlayMethod playMethod)
  1131. {
  1132. // Don't restrict by bitrate if coming from an external domain
  1133. if (item.IsRemote)
  1134. {
  1135. return true;
  1136. }
  1137. if (!maxBitrate.HasValue)
  1138. {
  1139. _logger.Info("Cannot " + playMethod + " due to unknown supported bitrate");
  1140. return false;
  1141. }
  1142. if (!item.Bitrate.HasValue)
  1143. {
  1144. _logger.Info("Cannot " + playMethod + " due to unknown content bitrate");
  1145. return false;
  1146. }
  1147. if (item.Bitrate.Value > maxBitrate.Value)
  1148. {
  1149. _logger.Info("Bitrate exceeds " + playMethod + " limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), maxBitrate.Value.ToString(CultureInfo.InvariantCulture));
  1150. return false;
  1151. }
  1152. return true;
  1153. }
  1154. private void ValidateInput(VideoOptions options)
  1155. {
  1156. ValidateAudioInput(options);
  1157. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1158. {
  1159. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  1160. }
  1161. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1162. {
  1163. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  1164. }
  1165. }
  1166. private void ValidateAudioInput(AudioOptions options)
  1167. {
  1168. if (string.IsNullOrEmpty(options.ItemId))
  1169. {
  1170. throw new ArgumentException("ItemId is required");
  1171. }
  1172. if (string.IsNullOrEmpty(options.DeviceId))
  1173. {
  1174. throw new ArgumentException("DeviceId is required");
  1175. }
  1176. if (options.Profile == null)
  1177. {
  1178. throw new ArgumentException("Profile is required");
  1179. }
  1180. if (options.MediaSources == null)
  1181. {
  1182. throw new ArgumentException("MediaSources is required");
  1183. }
  1184. }
  1185. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions)
  1186. {
  1187. foreach (ProfileCondition condition in conditions)
  1188. {
  1189. string value = condition.Value;
  1190. if (string.IsNullOrEmpty(value))
  1191. {
  1192. continue;
  1193. }
  1194. // No way to express this
  1195. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1196. {
  1197. continue;
  1198. }
  1199. switch (condition.Property)
  1200. {
  1201. case ProfileConditionValue.AudioBitrate:
  1202. {
  1203. int num;
  1204. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1205. {
  1206. if (condition.Condition == ProfileConditionType.Equals)
  1207. {
  1208. item.AudioBitrate = num;
  1209. }
  1210. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1211. {
  1212. item.AudioBitrate = Math.Min(num, item.AudioBitrate ?? num);
  1213. }
  1214. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1215. {
  1216. item.AudioBitrate = Math.Max(num, item.AudioBitrate ?? num);
  1217. }
  1218. }
  1219. break;
  1220. }
  1221. case ProfileConditionValue.AudioChannels:
  1222. {
  1223. int num;
  1224. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1225. {
  1226. if (condition.Condition == ProfileConditionType.Equals)
  1227. {
  1228. item.MaxAudioChannels = num;
  1229. }
  1230. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1231. {
  1232. item.MaxAudioChannels = Math.Min(num, item.MaxAudioChannels ?? num);
  1233. }
  1234. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1235. {
  1236. item.MaxAudioChannels = Math.Max(num, item.MaxAudioChannels ?? num);
  1237. }
  1238. }
  1239. break;
  1240. }
  1241. case ProfileConditionValue.IsAvc:
  1242. {
  1243. bool isAvc;
  1244. if (bool.TryParse(value, out isAvc))
  1245. {
  1246. if (isAvc && condition.Condition == ProfileConditionType.Equals)
  1247. {
  1248. item.RequireAvc = true;
  1249. }
  1250. else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
  1251. {
  1252. item.RequireAvc = true;
  1253. }
  1254. }
  1255. break;
  1256. }
  1257. case ProfileConditionValue.IsAnamorphic:
  1258. {
  1259. bool isAnamorphic;
  1260. if (bool.TryParse(value, out isAnamorphic))
  1261. {
  1262. if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
  1263. {
  1264. item.RequireNonAnamorphic = true;
  1265. }
  1266. else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
  1267. {
  1268. item.RequireNonAnamorphic = true;
  1269. }
  1270. }
  1271. break;
  1272. }
  1273. case ProfileConditionValue.IsInterlaced:
  1274. {
  1275. bool isInterlaced;
  1276. if (bool.TryParse(value, out isInterlaced))
  1277. {
  1278. if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
  1279. {
  1280. item.DeInterlace = true;
  1281. }
  1282. else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
  1283. {
  1284. item.DeInterlace = true;
  1285. }
  1286. }
  1287. break;
  1288. }
  1289. case ProfileConditionValue.AudioProfile:
  1290. case ProfileConditionValue.Has64BitOffsets:
  1291. case ProfileConditionValue.PacketLength:
  1292. case ProfileConditionValue.NumAudioStreams:
  1293. case ProfileConditionValue.NumVideoStreams:
  1294. case ProfileConditionValue.IsSecondaryAudio:
  1295. case ProfileConditionValue.VideoTimestamp:
  1296. {
  1297. // Not supported yet
  1298. break;
  1299. }
  1300. case ProfileConditionValue.RefFrames:
  1301. {
  1302. int num;
  1303. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1304. {
  1305. if (condition.Condition == ProfileConditionType.Equals)
  1306. {
  1307. item.MaxRefFrames = num;
  1308. }
  1309. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1310. {
  1311. item.MaxRefFrames = Math.Min(num, item.MaxRefFrames ?? num);
  1312. }
  1313. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1314. {
  1315. item.MaxRefFrames = Math.Max(num, item.MaxRefFrames ?? num);
  1316. }
  1317. }
  1318. break;
  1319. }
  1320. case ProfileConditionValue.VideoBitDepth:
  1321. {
  1322. int num;
  1323. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1324. {
  1325. if (condition.Condition == ProfileConditionType.Equals)
  1326. {
  1327. item.MaxVideoBitDepth = num;
  1328. }
  1329. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1330. {
  1331. item.MaxVideoBitDepth = Math.Min(num, item.MaxVideoBitDepth ?? num);
  1332. }
  1333. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1334. {
  1335. item.MaxVideoBitDepth = Math.Max(num, item.MaxVideoBitDepth ?? num);
  1336. }
  1337. }
  1338. break;
  1339. }
  1340. case ProfileConditionValue.VideoProfile:
  1341. {
  1342. item.VideoProfile = (value ?? string.Empty).Split('|')[0];
  1343. break;
  1344. }
  1345. case ProfileConditionValue.Height:
  1346. {
  1347. int num;
  1348. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1349. {
  1350. if (condition.Condition == ProfileConditionType.Equals)
  1351. {
  1352. item.MaxHeight = num;
  1353. }
  1354. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1355. {
  1356. item.MaxHeight = Math.Min(num, item.MaxHeight ?? num);
  1357. }
  1358. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1359. {
  1360. item.MaxHeight = Math.Max(num, item.MaxHeight ?? num);
  1361. }
  1362. }
  1363. break;
  1364. }
  1365. case ProfileConditionValue.VideoBitrate:
  1366. {
  1367. int num;
  1368. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1369. {
  1370. if (condition.Condition == ProfileConditionType.Equals)
  1371. {
  1372. item.VideoBitrate = num;
  1373. }
  1374. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1375. {
  1376. item.VideoBitrate = Math.Min(num, item.VideoBitrate ?? num);
  1377. }
  1378. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1379. {
  1380. item.VideoBitrate = Math.Max(num, item.VideoBitrate ?? num);
  1381. }
  1382. }
  1383. break;
  1384. }
  1385. case ProfileConditionValue.VideoFramerate:
  1386. {
  1387. float num;
  1388. if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1389. {
  1390. if (condition.Condition == ProfileConditionType.Equals)
  1391. {
  1392. item.MaxFramerate = num;
  1393. }
  1394. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1395. {
  1396. item.MaxFramerate = Math.Min(num, item.MaxFramerate ?? num);
  1397. }
  1398. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1399. {
  1400. item.MaxFramerate = Math.Max(num, item.MaxFramerate ?? num);
  1401. }
  1402. }
  1403. break;
  1404. }
  1405. case ProfileConditionValue.VideoLevel:
  1406. {
  1407. int num;
  1408. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1409. {
  1410. if (condition.Condition == ProfileConditionType.Equals)
  1411. {
  1412. item.VideoLevel = num;
  1413. }
  1414. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1415. {
  1416. item.VideoLevel = Math.Min(num, item.VideoLevel ?? num);
  1417. }
  1418. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1419. {
  1420. item.VideoLevel = Math.Max(num, item.VideoLevel ?? num);
  1421. }
  1422. }
  1423. break;
  1424. }
  1425. case ProfileConditionValue.Width:
  1426. {
  1427. int num;
  1428. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1429. {
  1430. if (condition.Condition == ProfileConditionType.Equals)
  1431. {
  1432. item.MaxWidth = num;
  1433. }
  1434. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1435. {
  1436. item.MaxWidth = Math.Min(num, item.MaxWidth ?? num);
  1437. }
  1438. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1439. {
  1440. item.MaxWidth = Math.Max(num, item.MaxWidth ?? num);
  1441. }
  1442. }
  1443. break;
  1444. }
  1445. default:
  1446. break;
  1447. }
  1448. }
  1449. }
  1450. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  1451. {
  1452. // Check container type
  1453. if (!profile.SupportsContainer(item.Container))
  1454. {
  1455. return false;
  1456. }
  1457. // Check audio codec
  1458. var audioCodecs = profile.GetAudioCodecs();
  1459. if (audioCodecs.Length > 0)
  1460. {
  1461. // Check audio codecs
  1462. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1463. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1464. {
  1465. return false;
  1466. }
  1467. }
  1468. return true;
  1469. }
  1470. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  1471. {
  1472. // Check container type
  1473. if (!profile.SupportsContainer(item.Container))
  1474. {
  1475. return false;
  1476. }
  1477. // Check video codec
  1478. var videoCodecs = profile.GetVideoCodecs();
  1479. if (videoCodecs.Length > 0)
  1480. {
  1481. string videoCodec = videoStream == null ? null : videoStream.Codec;
  1482. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  1483. {
  1484. return false;
  1485. }
  1486. }
  1487. // Check audio codec
  1488. if (audioStream != null)
  1489. {
  1490. var audioCodecs = profile.GetAudioCodecs();
  1491. if (audioCodecs.Length > 0)
  1492. {
  1493. // Check audio codecs
  1494. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1495. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1496. {
  1497. return false;
  1498. }
  1499. }
  1500. }
  1501. return true;
  1502. }
  1503. }
  1504. }