StreamBuilder.cs 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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.ContainsAnyCodec(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.ContainsAnyCodec(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, null, false);
  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 isFirstAppliedCodecProfile = true;
  645. foreach (CodecProfile i in options.Profile.CodecProfiles)
  646. {
  647. if (i.Type == CodecType.Video && i.ContainsAnyCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
  648. {
  649. bool applyConditions = true;
  650. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  651. {
  652. int? width = videoStream == null ? null : videoStream.Width;
  653. int? height = videoStream == null ? null : videoStream.Height;
  654. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  655. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  656. double? videoLevel = videoStream == null ? null : videoStream.Level;
  657. string videoProfile = videoStream == null ? null : videoStream.Profile;
  658. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  659. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  660. bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
  661. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  662. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  663. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
  664. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  665. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  666. int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
  667. int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
  668. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  669. {
  670. LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
  671. applyConditions = false;
  672. break;
  673. }
  674. }
  675. if (applyConditions)
  676. {
  677. var transcodingVideoCodecs = ContainerProfile.SplitValue(transcodingProfile.VideoCodec);
  678. foreach (var transcodingVideoCodec in transcodingVideoCodecs)
  679. {
  680. if (i.ContainsAnyCodec(transcodingVideoCodec, transcodingProfile.Container))
  681. {
  682. ApplyTranscodingConditions(playlistItem, i.Conditions, transcodingVideoCodec, !isFirstAppliedCodecProfile);
  683. isFirstAppliedCodecProfile = false;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. var audioTranscodingConditions = new List<ProfileCondition>();
  690. foreach (CodecProfile i in options.Profile.CodecProfiles)
  691. {
  692. if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(playlistItem.TargetAudioCodec, transcodingProfile.Container))
  693. {
  694. bool applyConditions = true;
  695. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  696. {
  697. bool? isSecondaryAudio = audioStream == null ? null : item.IsSecondaryAudio(audioStream);
  698. int? inputAudioBitrate = audioStream == null ? null : audioStream.BitRate;
  699. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  700. string audioProfile = audioStream == null ? null : audioStream.Profile;
  701. int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  702. int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  703. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, audioProfile, isSecondaryAudio))
  704. {
  705. LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
  706. applyConditions = false;
  707. break;
  708. }
  709. }
  710. if (applyConditions)
  711. {
  712. foreach (ProfileCondition c in i.Conditions)
  713. {
  714. audioTranscodingConditions.Add(c);
  715. }
  716. break;
  717. }
  718. }
  719. }
  720. // Honor requested max channels
  721. if (options.MaxAudioChannels.HasValue)
  722. {
  723. int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
  724. playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
  725. }
  726. int audioBitrate = GetAudioBitrate(playlistItem.SubProtocol, options.GetMaxBitrate(false), playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec, audioStream);
  727. playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
  728. var maxBitrateSetting = options.GetMaxBitrate(false);
  729. // Honor max rate
  730. if (maxBitrateSetting.HasValue)
  731. {
  732. var videoBitrate = maxBitrateSetting.Value;
  733. if (playlistItem.AudioBitrate.HasValue)
  734. {
  735. videoBitrate -= playlistItem.AudioBitrate.Value;
  736. }
  737. // Make sure the video bitrate is lower than bitrate settings but at least 64k
  738. long currentValue = playlistItem.VideoBitrate ?? videoBitrate;
  739. var longBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
  740. playlistItem.VideoBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
  741. }
  742. // Do this after initial values are set to account for greater than/less than conditions
  743. ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, false);
  744. }
  745. playlistItem.TranscodeReasons = transcodeReasons;
  746. return playlistItem;
  747. }
  748. private int GetDefaultAudioBitrateIfUnknown(MediaStream audioStream)
  749. {
  750. if ((audioStream.Channels ?? 0) >= 6)
  751. {
  752. return 384000;
  753. }
  754. return 192000;
  755. }
  756. private int GetAudioBitrate(string subProtocol, long? maxTotalBitrate, int? targetAudioChannels, string[] targetAudioCodecs, MediaStream audioStream)
  757. {
  758. var targetAudioCodec = targetAudioCodecs.Length == 0 ? null : targetAudioCodecs[0];
  759. int defaultBitrate = audioStream == null ? 192000 : audioStream.BitRate ?? GetDefaultAudioBitrateIfUnknown(audioStream);
  760. // Reduce the bitrate if we're downmixing
  761. if (targetAudioChannels.HasValue && audioStream != null && audioStream.Channels.HasValue && targetAudioChannels.Value < audioStream.Channels.Value)
  762. {
  763. defaultBitrate = StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3") ? 192000 : 128000;
  764. }
  765. if (StringHelper.EqualsIgnoreCase(subProtocol, "hls"))
  766. {
  767. defaultBitrate = Math.Min(384000, defaultBitrate);
  768. }
  769. else
  770. {
  771. defaultBitrate = Math.Min(448000, defaultBitrate);
  772. }
  773. int encoderAudioBitrateLimit = int.MaxValue;
  774. if (audioStream != null)
  775. {
  776. // Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
  777. // Any attempts to transcode over 64k will fail
  778. if (audioStream.Channels.HasValue &&
  779. audioStream.Channels.Value == 1)
  780. {
  781. if ((audioStream.BitRate ?? 0) < 64000)
  782. {
  783. encoderAudioBitrateLimit = 64000;
  784. }
  785. }
  786. }
  787. if (maxTotalBitrate.HasValue)
  788. {
  789. if (maxTotalBitrate.Value < 640000)
  790. {
  791. defaultBitrate = Math.Min(128000, defaultBitrate);
  792. }
  793. }
  794. return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
  795. }
  796. private Tuple<PlayMethod?, List<TranscodeReason>> GetVideoDirectPlayProfile(VideoOptions options,
  797. MediaSourceInfo mediaSource,
  798. MediaStream videoStream,
  799. MediaStream audioStream,
  800. bool isEligibleForDirectPlay,
  801. bool isEligibleForDirectStream)
  802. {
  803. DeviceProfile profile = options.Profile;
  804. if (options.ForceDirectPlay)
  805. {
  806. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectPlay, new List<TranscodeReason>());
  807. }
  808. if (options.ForceDirectStream)
  809. {
  810. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
  811. }
  812. if (videoStream == null)
  813. {
  814. _logger.Info("Profile: {0}, Cannot direct stream with no known video stream. Path: {1}",
  815. profile.Name ?? "Unknown Profile",
  816. mediaSource.Path ?? "Unknown path");
  817. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownVideoStreamInfo });
  818. }
  819. // See if it can be direct played
  820. DirectPlayProfile directPlay = null;
  821. foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
  822. {
  823. if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
  824. {
  825. directPlay = i;
  826. break;
  827. }
  828. }
  829. if (directPlay == null)
  830. {
  831. _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
  832. profile.Name ?? "Unknown Profile",
  833. mediaSource.Path ?? "Unknown path");
  834. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, GetTranscodeReasonsFromDirectPlayProfile(mediaSource, videoStream, audioStream, profile.DirectPlayProfiles));
  835. }
  836. string container = mediaSource.Container;
  837. var conditions = new List<ProfileCondition>();
  838. foreach (ContainerProfile i in profile.ContainerProfiles)
  839. {
  840. if (i.Type == DlnaProfileType.Video &&
  841. i.ContainsContainer(container))
  842. {
  843. foreach (ProfileCondition c in i.Conditions)
  844. {
  845. conditions.Add(c);
  846. }
  847. }
  848. }
  849. ConditionProcessor conditionProcessor = new ConditionProcessor();
  850. int? width = videoStream == null ? null : videoStream.Width;
  851. int? height = videoStream == null ? null : videoStream.Height;
  852. int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
  853. int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
  854. double? videoLevel = videoStream == null ? null : videoStream.Level;
  855. string videoProfile = videoStream == null ? null : videoStream.Profile;
  856. float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
  857. bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
  858. bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
  859. string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
  860. bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
  861. int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
  862. int? audioChannels = audioStream == null ? null : audioStream.Channels;
  863. string audioProfile = audioStream == null ? null : audioStream.Profile;
  864. int? audioSampleRate = audioStream == null ? null : audioStream.SampleRate;
  865. int? audioBitDepth = audioStream == null ? null : audioStream.BitDepth;
  866. TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
  867. int? packetLength = videoStream == null ? null : videoStream.PacketLength;
  868. int? refFrames = videoStream == null ? null : videoStream.RefFrames;
  869. int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
  870. int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
  871. // Check container conditions
  872. foreach (ProfileCondition i in conditions)
  873. {
  874. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  875. {
  876. LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
  877. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  878. var transcodeReasons = transcodeReason.HasValue
  879. ? new List<TranscodeReason> { transcodeReason.Value }
  880. : new List<TranscodeReason> { };
  881. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  882. }
  883. }
  884. string videoCodec = videoStream == null ? null : videoStream.Codec;
  885. if (string.IsNullOrEmpty(videoCodec))
  886. {
  887. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown video codec. Path: {1}",
  888. profile.Name ?? "Unknown Profile",
  889. mediaSource.Path ?? "Unknown path");
  890. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownVideoStreamInfo });
  891. }
  892. conditions = new List<ProfileCondition>();
  893. foreach (CodecProfile i in profile.CodecProfiles)
  894. {
  895. if (i.Type == CodecType.Video && i.ContainsAnyCodec(videoCodec, container))
  896. {
  897. bool applyConditions = true;
  898. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  899. {
  900. if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  901. {
  902. LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
  903. applyConditions = false;
  904. break;
  905. }
  906. }
  907. if (applyConditions)
  908. {
  909. foreach (ProfileCondition c in i.Conditions)
  910. {
  911. conditions.Add(c);
  912. }
  913. }
  914. }
  915. }
  916. foreach (ProfileCondition i in conditions)
  917. {
  918. if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
  919. {
  920. LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
  921. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  922. var transcodeReasons = transcodeReason.HasValue
  923. ? new List<TranscodeReason> { transcodeReason.Value }
  924. : new List<TranscodeReason> { };
  925. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  926. }
  927. }
  928. if (audioStream != null)
  929. {
  930. string audioCodec = audioStream.Codec;
  931. if (string.IsNullOrEmpty(audioCodec))
  932. {
  933. _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown audio codec. Path: {1}",
  934. profile.Name ?? "Unknown Profile",
  935. mediaSource.Path ?? "Unknown path");
  936. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownAudioStreamInfo });
  937. }
  938. conditions = new List<ProfileCondition>();
  939. bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
  940. foreach (CodecProfile i in profile.CodecProfiles)
  941. {
  942. if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(audioCodec, container))
  943. {
  944. bool applyConditions = true;
  945. foreach (ProfileCondition applyCondition in i.ApplyConditions)
  946. {
  947. if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  948. {
  949. LogConditionFailure(profile, "VideoAudioCodecProfile.ApplyConditions", applyCondition, mediaSource);
  950. applyConditions = false;
  951. break;
  952. }
  953. }
  954. if (applyConditions)
  955. {
  956. foreach (ProfileCondition c in i.Conditions)
  957. {
  958. conditions.Add(c);
  959. }
  960. }
  961. }
  962. }
  963. foreach (ProfileCondition i in conditions)
  964. {
  965. if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
  966. {
  967. LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
  968. var transcodeReason = GetTranscodeReasonForFailedCondition(i);
  969. var transcodeReasons = transcodeReason.HasValue
  970. ? new List<TranscodeReason> { transcodeReason.Value }
  971. : new List<TranscodeReason> { };
  972. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
  973. }
  974. }
  975. }
  976. if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
  977. {
  978. return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
  979. }
  980. return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.ContainerBitrateExceedsLimit });
  981. }
  982. private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
  983. {
  984. _logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
  985. type,
  986. profile.Name ?? "Unknown Profile",
  987. condition.Property,
  988. condition.Condition,
  989. condition.Value ?? string.Empty,
  990. condition.IsRequired,
  991. mediaSource.Path ?? "Unknown path");
  992. }
  993. private Tuple<bool, TranscodeReason?> IsEligibleForDirectPlay(MediaSourceInfo item,
  994. long? maxBitrate,
  995. MediaStream subtitleStream,
  996. VideoOptions options,
  997. PlayMethod playMethod)
  998. {
  999. if (subtitleStream != null)
  1000. {
  1001. SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, playMethod, _transcoderSupport, null, null);
  1002. if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
  1003. {
  1004. _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
  1005. return new Tuple<bool, TranscodeReason?>(false, TranscodeReason.SubtitleCodecNotSupported);
  1006. }
  1007. }
  1008. var result = IsAudioEligibleForDirectPlay(item, maxBitrate, playMethod);
  1009. if (result)
  1010. {
  1011. return new Tuple<bool, TranscodeReason?>(result, null);
  1012. }
  1013. return new Tuple<bool, TranscodeReason?>(result, TranscodeReason.ContainerBitrateExceedsLimit);
  1014. }
  1015. public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, string transcodingSubProtocol, string transcodingContainer)
  1016. {
  1017. if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
  1018. {
  1019. // Look for supported embedded subs of the same format
  1020. foreach (SubtitleProfile profile in subtitleProfiles)
  1021. {
  1022. if (!profile.SupportsLanguage(subtitleStream.Language))
  1023. {
  1024. continue;
  1025. }
  1026. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1027. {
  1028. continue;
  1029. }
  1030. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1031. {
  1032. continue;
  1033. }
  1034. if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
  1035. {
  1036. return profile;
  1037. }
  1038. }
  1039. // Look for supported embedded subs of a convertible format
  1040. foreach (SubtitleProfile profile in subtitleProfiles)
  1041. {
  1042. if (!profile.SupportsLanguage(subtitleStream.Language))
  1043. {
  1044. continue;
  1045. }
  1046. if (profile.Method != SubtitleDeliveryMethod.Embed)
  1047. {
  1048. continue;
  1049. }
  1050. if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
  1051. {
  1052. continue;
  1053. }
  1054. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1055. {
  1056. return profile;
  1057. }
  1058. }
  1059. }
  1060. // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
  1061. return GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, transcoderSupport, false) ??
  1062. GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, transcoderSupport, true) ??
  1063. new SubtitleProfile
  1064. {
  1065. Method = SubtitleDeliveryMethod.Encode,
  1066. Format = subtitleStream.Codec
  1067. };
  1068. }
  1069. private static bool IsSubtitleEmbedSupported(MediaStream subtitleStream, SubtitleProfile subtitleProfile, string transcodingSubProtocol, string transcodingContainer)
  1070. {
  1071. if (!string.IsNullOrWhiteSpace(transcodingContainer))
  1072. {
  1073. var normalizedContainers = ContainerProfile.SplitValue(transcodingContainer);
  1074. if (ContainerProfile.ContainsContainer(normalizedContainers, "ts"))
  1075. {
  1076. return false;
  1077. }
  1078. if (ContainerProfile.ContainsContainer(normalizedContainers, "mpegts"))
  1079. {
  1080. return false;
  1081. }
  1082. if (ContainerProfile.ContainsContainer(normalizedContainers, "mp4"))
  1083. {
  1084. return false;
  1085. }
  1086. if (ContainerProfile.ContainsContainer(normalizedContainers, "mkv") ||
  1087. ContainerProfile.ContainsContainer(normalizedContainers, "matroska"))
  1088. {
  1089. return true;
  1090. }
  1091. }
  1092. return false;
  1093. }
  1094. private static SubtitleProfile GetExternalSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, bool allowConversion)
  1095. {
  1096. foreach (SubtitleProfile profile in subtitleProfiles)
  1097. {
  1098. if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
  1099. {
  1100. continue;
  1101. }
  1102. if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
  1103. {
  1104. continue;
  1105. }
  1106. if (!profile.SupportsLanguage(subtitleStream.Language))
  1107. {
  1108. continue;
  1109. }
  1110. if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec))
  1111. {
  1112. continue;
  1113. }
  1114. if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
  1115. (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
  1116. {
  1117. bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
  1118. if (!requiresConversion)
  1119. {
  1120. return profile;
  1121. }
  1122. if (!allowConversion)
  1123. {
  1124. continue;
  1125. }
  1126. if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
  1127. {
  1128. return profile;
  1129. }
  1130. }
  1131. }
  1132. return null;
  1133. }
  1134. private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long? maxBitrate, PlayMethod playMethod)
  1135. {
  1136. // Don't restrict by bitrate if coming from an external domain
  1137. if (item.IsRemote)
  1138. {
  1139. return true;
  1140. }
  1141. if (!maxBitrate.HasValue)
  1142. {
  1143. _logger.Info("Cannot " + playMethod + " due to unknown supported bitrate");
  1144. return false;
  1145. }
  1146. if (!item.Bitrate.HasValue)
  1147. {
  1148. _logger.Info("Cannot " + playMethod + " due to unknown content bitrate");
  1149. return false;
  1150. }
  1151. if (item.Bitrate.Value > maxBitrate.Value)
  1152. {
  1153. _logger.Info("Bitrate exceeds " + playMethod + " limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), maxBitrate.Value.ToString(CultureInfo.InvariantCulture));
  1154. return false;
  1155. }
  1156. return true;
  1157. }
  1158. private void ValidateInput(VideoOptions options)
  1159. {
  1160. ValidateAudioInput(options);
  1161. if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1162. {
  1163. throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
  1164. }
  1165. if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
  1166. {
  1167. throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
  1168. }
  1169. }
  1170. private void ValidateAudioInput(AudioOptions options)
  1171. {
  1172. if (string.IsNullOrEmpty(options.ItemId))
  1173. {
  1174. throw new ArgumentException("ItemId is required");
  1175. }
  1176. if (string.IsNullOrEmpty(options.DeviceId))
  1177. {
  1178. throw new ArgumentException("DeviceId is required");
  1179. }
  1180. if (options.Profile == null)
  1181. {
  1182. throw new ArgumentException("Profile is required");
  1183. }
  1184. if (options.MediaSources == null)
  1185. {
  1186. throw new ArgumentException("MediaSources is required");
  1187. }
  1188. }
  1189. private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string qualifier, bool qualifiedOnly)
  1190. {
  1191. foreach (ProfileCondition condition in conditions)
  1192. {
  1193. string value = condition.Value;
  1194. if (string.IsNullOrEmpty(value))
  1195. {
  1196. continue;
  1197. }
  1198. // No way to express this
  1199. if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1200. {
  1201. continue;
  1202. }
  1203. switch (condition.Property)
  1204. {
  1205. case ProfileConditionValue.AudioBitrate:
  1206. {
  1207. if (qualifiedOnly)
  1208. {
  1209. continue;
  1210. }
  1211. int num;
  1212. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1213. {
  1214. if (condition.Condition == ProfileConditionType.Equals)
  1215. {
  1216. item.AudioBitrate = num;
  1217. }
  1218. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1219. {
  1220. item.AudioBitrate = Math.Min(num, item.AudioBitrate ?? num);
  1221. }
  1222. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1223. {
  1224. item.AudioBitrate = Math.Max(num, item.AudioBitrate ?? num);
  1225. }
  1226. }
  1227. break;
  1228. }
  1229. case ProfileConditionValue.AudioChannels:
  1230. {
  1231. if (qualifiedOnly)
  1232. {
  1233. continue;
  1234. }
  1235. int num;
  1236. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1237. {
  1238. if (condition.Condition == ProfileConditionType.Equals)
  1239. {
  1240. item.MaxAudioChannels = num;
  1241. }
  1242. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1243. {
  1244. item.MaxAudioChannels = Math.Min(num, item.MaxAudioChannels ?? num);
  1245. }
  1246. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1247. {
  1248. item.MaxAudioChannels = Math.Max(num, item.MaxAudioChannels ?? num);
  1249. }
  1250. }
  1251. break;
  1252. }
  1253. case ProfileConditionValue.IsAvc:
  1254. {
  1255. if (qualifiedOnly)
  1256. {
  1257. continue;
  1258. }
  1259. bool isAvc;
  1260. if (bool.TryParse(value, out isAvc))
  1261. {
  1262. if (isAvc && condition.Condition == ProfileConditionType.Equals)
  1263. {
  1264. item.RequireAvc = true;
  1265. }
  1266. else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
  1267. {
  1268. item.RequireAvc = true;
  1269. }
  1270. }
  1271. break;
  1272. }
  1273. case ProfileConditionValue.IsAnamorphic:
  1274. {
  1275. if (qualifiedOnly)
  1276. {
  1277. continue;
  1278. }
  1279. bool isAnamorphic;
  1280. if (bool.TryParse(value, out isAnamorphic))
  1281. {
  1282. if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
  1283. {
  1284. item.RequireNonAnamorphic = true;
  1285. }
  1286. else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
  1287. {
  1288. item.RequireNonAnamorphic = true;
  1289. }
  1290. }
  1291. break;
  1292. }
  1293. case ProfileConditionValue.IsInterlaced:
  1294. {
  1295. if (string.IsNullOrWhiteSpace(qualifier))
  1296. {
  1297. continue;
  1298. }
  1299. bool isInterlaced;
  1300. if (bool.TryParse(value, out isInterlaced))
  1301. {
  1302. if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
  1303. {
  1304. item.SetOption(qualifier, "deinterlace", "true");
  1305. }
  1306. else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
  1307. {
  1308. item.SetOption(qualifier, "deinterlace", "true");
  1309. }
  1310. }
  1311. break;
  1312. }
  1313. case ProfileConditionValue.AudioProfile:
  1314. case ProfileConditionValue.Has64BitOffsets:
  1315. case ProfileConditionValue.PacketLength:
  1316. case ProfileConditionValue.NumAudioStreams:
  1317. case ProfileConditionValue.NumVideoStreams:
  1318. case ProfileConditionValue.IsSecondaryAudio:
  1319. case ProfileConditionValue.VideoTimestamp:
  1320. {
  1321. // Not supported yet
  1322. break;
  1323. }
  1324. case ProfileConditionValue.RefFrames:
  1325. {
  1326. if (string.IsNullOrWhiteSpace(qualifier))
  1327. {
  1328. continue;
  1329. }
  1330. int num;
  1331. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1332. {
  1333. if (condition.Condition == ProfileConditionType.Equals)
  1334. {
  1335. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(num));
  1336. }
  1337. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1338. {
  1339. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetRefFrames(qualifier) ?? num)));
  1340. }
  1341. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1342. {
  1343. item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetRefFrames(qualifier) ?? num)));
  1344. }
  1345. }
  1346. break;
  1347. }
  1348. case ProfileConditionValue.VideoBitDepth:
  1349. {
  1350. if (qualifiedOnly)
  1351. {
  1352. continue;
  1353. }
  1354. int num;
  1355. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1356. {
  1357. if (condition.Condition == ProfileConditionType.Equals)
  1358. {
  1359. item.MaxVideoBitDepth = num;
  1360. }
  1361. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1362. {
  1363. item.MaxVideoBitDepth = Math.Min(num, item.MaxVideoBitDepth ?? num);
  1364. }
  1365. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1366. {
  1367. item.MaxVideoBitDepth = Math.Max(num, item.MaxVideoBitDepth ?? num);
  1368. }
  1369. }
  1370. break;
  1371. }
  1372. case ProfileConditionValue.VideoProfile:
  1373. {
  1374. if (string.IsNullOrWhiteSpace(qualifier))
  1375. {
  1376. continue;
  1377. }
  1378. if (!string.IsNullOrWhiteSpace(value))
  1379. {
  1380. // change from split by | to comma
  1381. item.SetOption(qualifier, "profile", string.Join(",", value.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)));
  1382. }
  1383. break;
  1384. }
  1385. case ProfileConditionValue.Height:
  1386. {
  1387. if (qualifiedOnly)
  1388. {
  1389. continue;
  1390. }
  1391. int num;
  1392. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1393. {
  1394. if (condition.Condition == ProfileConditionType.Equals)
  1395. {
  1396. item.MaxHeight = num;
  1397. }
  1398. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1399. {
  1400. item.MaxHeight = Math.Min(num, item.MaxHeight ?? num);
  1401. }
  1402. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1403. {
  1404. item.MaxHeight = Math.Max(num, item.MaxHeight ?? num);
  1405. }
  1406. }
  1407. break;
  1408. }
  1409. case ProfileConditionValue.VideoBitrate:
  1410. {
  1411. if (qualifiedOnly)
  1412. {
  1413. continue;
  1414. }
  1415. int num;
  1416. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1417. {
  1418. if (condition.Condition == ProfileConditionType.Equals)
  1419. {
  1420. item.VideoBitrate = num;
  1421. }
  1422. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1423. {
  1424. item.VideoBitrate = Math.Min(num, item.VideoBitrate ?? num);
  1425. }
  1426. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1427. {
  1428. item.VideoBitrate = Math.Max(num, item.VideoBitrate ?? num);
  1429. }
  1430. }
  1431. break;
  1432. }
  1433. case ProfileConditionValue.VideoFramerate:
  1434. {
  1435. if (qualifiedOnly)
  1436. {
  1437. continue;
  1438. }
  1439. float num;
  1440. if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1441. {
  1442. if (condition.Condition == ProfileConditionType.Equals)
  1443. {
  1444. item.MaxFramerate = num;
  1445. }
  1446. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1447. {
  1448. item.MaxFramerate = Math.Min(num, item.MaxFramerate ?? num);
  1449. }
  1450. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1451. {
  1452. item.MaxFramerate = Math.Max(num, item.MaxFramerate ?? num);
  1453. }
  1454. }
  1455. break;
  1456. }
  1457. case ProfileConditionValue.VideoLevel:
  1458. {
  1459. if (string.IsNullOrWhiteSpace(qualifier))
  1460. {
  1461. continue;
  1462. }
  1463. int num;
  1464. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1465. {
  1466. if (condition.Condition == ProfileConditionType.Equals)
  1467. {
  1468. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(num));
  1469. }
  1470. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1471. {
  1472. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetVideoLevel(qualifier) ?? num)));
  1473. }
  1474. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1475. {
  1476. item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetVideoLevel(qualifier) ?? num)));
  1477. }
  1478. }
  1479. break;
  1480. }
  1481. case ProfileConditionValue.Width:
  1482. {
  1483. if (qualifiedOnly)
  1484. {
  1485. continue;
  1486. }
  1487. int num;
  1488. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
  1489. {
  1490. if (condition.Condition == ProfileConditionType.Equals)
  1491. {
  1492. item.MaxWidth = num;
  1493. }
  1494. else if (condition.Condition == ProfileConditionType.LessThanEqual)
  1495. {
  1496. item.MaxWidth = Math.Min(num, item.MaxWidth ?? num);
  1497. }
  1498. else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
  1499. {
  1500. item.MaxWidth = Math.Max(num, item.MaxWidth ?? num);
  1501. }
  1502. }
  1503. break;
  1504. }
  1505. default:
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
  1511. {
  1512. // Check container type
  1513. if (!profile.SupportsContainer(item.Container))
  1514. {
  1515. return false;
  1516. }
  1517. // Check audio codec
  1518. var audioCodecs = profile.GetAudioCodecs();
  1519. if (audioCodecs.Length > 0)
  1520. {
  1521. // Check audio codecs
  1522. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1523. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1524. {
  1525. return false;
  1526. }
  1527. }
  1528. return true;
  1529. }
  1530. private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
  1531. {
  1532. // Check container type
  1533. if (!profile.SupportsContainer(item.Container))
  1534. {
  1535. return false;
  1536. }
  1537. // Check video codec
  1538. var videoCodecs = profile.GetVideoCodecs();
  1539. if (videoCodecs.Length > 0)
  1540. {
  1541. string videoCodec = videoStream == null ? null : videoStream.Codec;
  1542. if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
  1543. {
  1544. return false;
  1545. }
  1546. }
  1547. // Check audio codec
  1548. if (audioStream != null)
  1549. {
  1550. var audioCodecs = profile.GetAudioCodecs();
  1551. if (audioCodecs.Length > 0)
  1552. {
  1553. // Check audio codecs
  1554. string audioCodec = audioStream == null ? null : audioStream.Codec;
  1555. if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
  1556. {
  1557. return false;
  1558. }
  1559. }
  1560. }
  1561. return true;
  1562. }
  1563. }
  1564. }