BaseStreamingService.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Dlna;
  5. using MediaBrowser.Controller.Dto;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.LiveTv;
  9. using MediaBrowser.Controller.MediaEncoding;
  10. using MediaBrowser.Controller.Persistence;
  11. using MediaBrowser.Model.Configuration;
  12. using MediaBrowser.Model.Drawing;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Library;
  16. using MediaBrowser.Model.LiveTv;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Diagnostics;
  20. using System.Globalization;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. namespace MediaBrowser.Api.Playback
  26. {
  27. /// <summary>
  28. /// Class BaseStreamingService
  29. /// </summary>
  30. public abstract class BaseStreamingService : BaseApiService
  31. {
  32. /// <summary>
  33. /// Gets or sets the application paths.
  34. /// </summary>
  35. /// <value>The application paths.</value>
  36. protected IServerConfigurationManager ServerConfigurationManager { get; private set; }
  37. /// <summary>
  38. /// Gets or sets the user manager.
  39. /// </summary>
  40. /// <value>The user manager.</value>
  41. protected IUserManager UserManager { get; private set; }
  42. /// <summary>
  43. /// Gets or sets the library manager.
  44. /// </summary>
  45. /// <value>The library manager.</value>
  46. protected ILibraryManager LibraryManager { get; private set; }
  47. /// <summary>
  48. /// Gets or sets the iso manager.
  49. /// </summary>
  50. /// <value>The iso manager.</value>
  51. protected IIsoManager IsoManager { get; private set; }
  52. /// <summary>
  53. /// Gets or sets the media encoder.
  54. /// </summary>
  55. /// <value>The media encoder.</value>
  56. protected IMediaEncoder MediaEncoder { get; private set; }
  57. protected IEncodingManager EncodingManager { get; private set; }
  58. protected IDtoService DtoService { get; private set; }
  59. protected IFileSystem FileSystem { get; private set; }
  60. protected IItemRepository ItemRepository { get; private set; }
  61. protected ILiveTvManager LiveTvManager { get; private set; }
  62. protected IDlnaManager DlnaManager { get; private set; }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  65. /// </summary>
  66. /// <param name="serverConfig">The server configuration.</param>
  67. /// <param name="userManager">The user manager.</param>
  68. /// <param name="libraryManager">The library manager.</param>
  69. /// <param name="isoManager">The iso manager.</param>
  70. /// <param name="mediaEncoder">The media encoder.</param>
  71. /// <param name="dtoService">The dto service.</param>
  72. /// <param name="fileSystem">The file system.</param>
  73. /// <param name="itemRepository">The item repository.</param>
  74. protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IEncodingManager encodingManager, IDlnaManager dlnaManager)
  75. {
  76. DlnaManager = dlnaManager;
  77. EncodingManager = encodingManager;
  78. LiveTvManager = liveTvManager;
  79. ItemRepository = itemRepository;
  80. FileSystem = fileSystem;
  81. DtoService = dtoService;
  82. ServerConfigurationManager = serverConfig;
  83. UserManager = userManager;
  84. LibraryManager = libraryManager;
  85. IsoManager = isoManager;
  86. MediaEncoder = mediaEncoder;
  87. }
  88. /// <summary>
  89. /// Gets the command line arguments.
  90. /// </summary>
  91. /// <param name="outputPath">The output path.</param>
  92. /// <param name="state">The state.</param>
  93. /// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
  94. /// <returns>System.String.</returns>
  95. protected abstract string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions);
  96. /// <summary>
  97. /// Gets the type of the transcoding job.
  98. /// </summary>
  99. /// <value>The type of the transcoding job.</value>
  100. protected abstract TranscodingJobType TranscodingJobType { get; }
  101. /// <summary>
  102. /// Gets the output file extension.
  103. /// </summary>
  104. /// <param name="state">The state.</param>
  105. /// <returns>System.String.</returns>
  106. protected virtual string GetOutputFileExtension(StreamState state)
  107. {
  108. return Path.GetExtension(state.RequestedUrl);
  109. }
  110. /// <summary>
  111. /// Gets the output file path.
  112. /// </summary>
  113. /// <param name="state">The state.</param>
  114. /// <returns>System.String.</returns>
  115. protected virtual string GetOutputFilePath(StreamState state)
  116. {
  117. var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath;
  118. var outputFileExtension = GetOutputFileExtension(state);
  119. return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
  120. }
  121. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  122. /// <summary>
  123. /// The fast seek offset seconds
  124. /// </summary>
  125. private const int FastSeekOffsetSeconds = 1;
  126. /// <summary>
  127. /// Gets the fast seek command line parameter.
  128. /// </summary>
  129. /// <param name="request">The request.</param>
  130. /// <returns>System.String.</returns>
  131. /// <value>The fast seek command line parameter.</value>
  132. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  133. {
  134. var time = request.StartTimeTicks;
  135. if (time.HasValue)
  136. {
  137. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds;
  138. if (seconds > 0)
  139. {
  140. return string.Format("-ss {0}", seconds.ToString(UsCulture));
  141. }
  142. }
  143. return string.Empty;
  144. }
  145. /// <summary>
  146. /// Gets the slow seek command line parameter.
  147. /// </summary>
  148. /// <param name="request">The request.</param>
  149. /// <returns>System.String.</returns>
  150. /// <value>The slow seek command line parameter.</value>
  151. protected string GetSlowSeekCommandLineParameter(StreamRequest request)
  152. {
  153. var time = request.StartTimeTicks;
  154. if (time.HasValue)
  155. {
  156. if (TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds > 0)
  157. {
  158. return string.Format(" -ss {0}", FastSeekOffsetSeconds.ToString(UsCulture));
  159. }
  160. }
  161. return string.Empty;
  162. }
  163. /// <summary>
  164. /// Gets the map args.
  165. /// </summary>
  166. /// <param name="state">The state.</param>
  167. /// <returns>System.String.</returns>
  168. protected virtual string GetMapArgs(StreamState state)
  169. {
  170. var args = string.Empty;
  171. if (state.IsRemote || !state.HasMediaStreams)
  172. {
  173. return string.Empty;
  174. }
  175. if (state.VideoStream != null)
  176. {
  177. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  178. }
  179. else
  180. {
  181. args += "-map -0:v";
  182. }
  183. if (state.AudioStream != null)
  184. {
  185. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  186. }
  187. else
  188. {
  189. args += " -map -0:a";
  190. }
  191. if (state.SubtitleStream == null)
  192. {
  193. args += " -map -0:s";
  194. }
  195. return args;
  196. }
  197. /// <summary>
  198. /// Determines which stream will be used for playback
  199. /// </summary>
  200. /// <param name="allStream">All stream.</param>
  201. /// <param name="desiredIndex">Index of the desired.</param>
  202. /// <param name="type">The type.</param>
  203. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  204. /// <returns>MediaStream.</returns>
  205. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  206. {
  207. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  208. if (desiredIndex.HasValue)
  209. {
  210. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  211. if (stream != null)
  212. {
  213. return stream;
  214. }
  215. }
  216. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  217. {
  218. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  219. streams.FirstOrDefault();
  220. }
  221. // Just return the first one
  222. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  223. }
  224. protected EncodingQuality GetQualitySetting()
  225. {
  226. var quality = ServerConfigurationManager.Configuration.MediaEncodingQuality;
  227. if (quality == EncodingQuality.Auto)
  228. {
  229. var cpuCount = Environment.ProcessorCount;
  230. if (cpuCount >= 4)
  231. {
  232. //return EncodingQuality.HighQuality;
  233. }
  234. return EncodingQuality.HighSpeed;
  235. }
  236. return quality;
  237. }
  238. /// <summary>
  239. /// Gets the number of threads.
  240. /// </summary>
  241. /// <returns>System.Int32.</returns>
  242. /// <exception cref="System.Exception">Unrecognized MediaEncodingQuality value.</exception>
  243. protected int GetNumberOfThreads(StreamState state, bool isWebm)
  244. {
  245. // Use more when this is true. -re will keep cpu usage under control
  246. if (state.ReadInputAtNativeFramerate)
  247. {
  248. if (isWebm)
  249. {
  250. return Math.Max(Environment.ProcessorCount - 1, 1);
  251. }
  252. return 0;
  253. }
  254. // Webm: http://www.webmproject.org/docs/encoder-parameters/
  255. // The decoder will usually automatically use an appropriate number of threads according to how many cores are available but it can only use multiple threads
  256. // for the coefficient data if the encoder selected --token-parts > 0 at encode time.
  257. switch (GetQualitySetting())
  258. {
  259. case EncodingQuality.HighSpeed:
  260. return 2;
  261. case EncodingQuality.HighQuality:
  262. return 2;
  263. case EncodingQuality.MaxQuality:
  264. return isWebm ? 2 : 0;
  265. default:
  266. throw new Exception("Unrecognized MediaEncodingQuality value.");
  267. }
  268. }
  269. /// <summary>
  270. /// Gets the video bitrate to specify on the command line
  271. /// </summary>
  272. /// <param name="state">The state.</param>
  273. /// <param name="videoCodec">The video codec.</param>
  274. /// <param name="isHls">if set to <c>true</c> [is HLS].</param>
  275. /// <returns>System.String.</returns>
  276. protected string GetVideoQualityParam(StreamState state, string videoCodec, bool isHls)
  277. {
  278. var param = string.Empty;
  279. var hasFixedResolution = state.VideoRequest.HasFixedResolution;
  280. var qualitySetting = GetQualitySetting();
  281. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  282. {
  283. switch (qualitySetting)
  284. {
  285. case EncodingQuality.HighSpeed:
  286. param = "-preset ultrafast";
  287. break;
  288. case EncodingQuality.HighQuality:
  289. param = "-preset superfast";
  290. break;
  291. case EncodingQuality.MaxQuality:
  292. param = "-preset superfast";
  293. break;
  294. }
  295. switch (qualitySetting)
  296. {
  297. case EncodingQuality.HighSpeed:
  298. param += " -crf 23";
  299. break;
  300. case EncodingQuality.HighQuality:
  301. param += " -crf 20";
  302. break;
  303. case EncodingQuality.MaxQuality:
  304. param += " -crf 18";
  305. break;
  306. }
  307. }
  308. // webm
  309. else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  310. {
  311. // http://www.webmproject.org/docs/encoder-parameters/
  312. param = "-speed 16 -quality good -profile:v 0 -slices 8";
  313. if (!hasFixedResolution)
  314. {
  315. switch (qualitySetting)
  316. {
  317. case EncodingQuality.HighSpeed:
  318. param += " -crf 18";
  319. break;
  320. case EncodingQuality.HighQuality:
  321. param += " -crf 10";
  322. break;
  323. case EncodingQuality.MaxQuality:
  324. param += " -crf 4";
  325. break;
  326. }
  327. }
  328. }
  329. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  330. {
  331. param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  332. }
  333. // asf/wmv
  334. else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase))
  335. {
  336. param = "-qmin 2";
  337. }
  338. else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  339. {
  340. param = "-mbd 2";
  341. }
  342. param += GetVideoBitrateParam(state, videoCodec, isHls);
  343. var framerate = GetFramerateParam(state);
  344. if (framerate.HasValue)
  345. {
  346. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  347. }
  348. if (!string.IsNullOrEmpty(state.VideoSync))
  349. {
  350. param += " -vsync " + state.VideoSync;
  351. }
  352. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  353. {
  354. param += " -profile:v " + state.VideoRequest.Profile;
  355. }
  356. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  357. {
  358. param += " -level " + state.VideoRequest.Level;
  359. }
  360. return param;
  361. }
  362. protected string GetAudioFilterParam(StreamState state, bool isHls)
  363. {
  364. var volParam = string.Empty;
  365. var audioSampleRate = string.Empty;
  366. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  367. // Boost volume to 200% when downsampling from 6ch to 2ch
  368. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  369. {
  370. volParam = ",volume=2.000000";
  371. }
  372. if (state.Request.AudioSampleRate.HasValue)
  373. {
  374. audioSampleRate = state.Request.AudioSampleRate.Value + ":";
  375. }
  376. var adelay = isHls ? "adelay=1," : string.Empty;
  377. var pts = string.Empty;
  378. if (state.SubtitleStream != null)
  379. {
  380. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  381. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  382. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  383. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  384. {
  385. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  386. pts = string.Format(",asetpts=PTS-{0}/TB",
  387. Math.Round(seconds).ToString(UsCulture));
  388. }
  389. }
  390. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  391. adelay,
  392. audioSampleRate,
  393. volParam,
  394. pts,
  395. state.AudioSync);
  396. }
  397. /// <summary>
  398. /// If we're going to put a fixed size on the command line, this will calculate it
  399. /// </summary>
  400. /// <param name="state">The state.</param>
  401. /// <param name="outputVideoCodec">The output video codec.</param>
  402. /// <param name="performTextSubtitleConversion">if set to <c>true</c> [perform text subtitle conversion].</param>
  403. /// <returns>System.String.</returns>
  404. protected string GetOutputSizeParam(StreamState state, string outputVideoCodec, bool performTextSubtitleConversion)
  405. {
  406. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  407. var assSubtitleParam = string.Empty;
  408. var copyTsParam = string.Empty;
  409. var yadifParam = state.DeInterlace ? "yadif=0:-1:0," : string.Empty;
  410. var request = state.VideoRequest;
  411. if (state.SubtitleStream != null)
  412. {
  413. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  414. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  415. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  416. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  417. {
  418. assSubtitleParam = GetTextSubtitleParam(state, performTextSubtitleConversion);
  419. copyTsParam = " -copyts";
  420. }
  421. }
  422. // If fixed dimensions were supplied
  423. if (request.Width.HasValue && request.Height.HasValue)
  424. {
  425. var widthParam = request.Width.Value.ToString(UsCulture);
  426. var heightParam = request.Height.Value.ToString(UsCulture);
  427. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  428. }
  429. // If Max dimensions were supplied, for width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size
  430. if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
  431. {
  432. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  433. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  434. return string.Format("{4} -vf \"{0}scale=trunc(min(iw\\,{1})/2)*2:trunc(min((iw/dar)\\,{2})/2)*2{3}\"", yadifParam, maxWidthParam, maxHeightParam, assSubtitleParam, copyTsParam);
  435. }
  436. var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
  437. // If a fixed width was requested
  438. if (request.Width.HasValue)
  439. {
  440. var widthParam = request.Width.Value.ToString(UsCulture);
  441. return isH264Output ?
  442. string.Format("{3} -vf \"{0}scale={1}:trunc(ow/a/2)*2{2}\"", yadifParam, widthParam, assSubtitleParam, copyTsParam) :
  443. string.Format("{3} -vf \"{0}scale={1}:-1{2}\"", yadifParam, widthParam, assSubtitleParam, copyTsParam);
  444. }
  445. // If a fixed height was requested
  446. if (request.Height.HasValue)
  447. {
  448. var heightParam = request.Height.Value.ToString(UsCulture);
  449. return isH264Output ?
  450. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:{1}{2}\"", yadifParam, heightParam, assSubtitleParam, copyTsParam) :
  451. string.Format("{3} -vf \"{0}scale=-1:{1}{2}\"", yadifParam, heightParam, assSubtitleParam, copyTsParam);
  452. }
  453. // If a max width was requested
  454. if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null))
  455. {
  456. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  457. return isH264Output ?
  458. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):trunc(ow/a/2)*2{2}\"", yadifParam, maxWidthParam, assSubtitleParam, copyTsParam) :
  459. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):-1{2}\"", yadifParam, maxWidthParam, assSubtitleParam, copyTsParam);
  460. }
  461. // If a max height was requested
  462. if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null))
  463. {
  464. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  465. return isH264Output ?
  466. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:min(ih\\,{1}){2}\"", yadifParam, maxHeightParam, assSubtitleParam, copyTsParam) :
  467. string.Format("{3} -vf \"{0}scale=-1:min(ih\\,{1}){2}\"", yadifParam, maxHeightParam, assSubtitleParam, copyTsParam);
  468. }
  469. if (state.VideoStream == null)
  470. {
  471. // No way to figure this out
  472. return string.Empty;
  473. }
  474. // Need to perform calculations manually
  475. // Try to account for bad media info
  476. var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0;
  477. var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0;
  478. var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight);
  479. // If we're encoding with libx264, it can't handle odd numbered widths or heights, so we'll have to fix that
  480. if (isH264Output)
  481. {
  482. var widthParam = outputSize.Width.ToString(UsCulture);
  483. var heightParam = outputSize.Height.ToString(UsCulture);
  484. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  485. }
  486. // Otherwise use -vf scale since ffmpeg will ensure internally that the aspect ratio is preserved
  487. return string.Format("{3} -vf \"{0}scale={1}:-1{2}\"", yadifParam, Convert.ToInt32(outputSize.Width), assSubtitleParam, copyTsParam);
  488. }
  489. /// <summary>
  490. /// Gets the text subtitle param.
  491. /// </summary>
  492. /// <param name="state">The state.</param>
  493. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  494. /// <returns>System.String.</returns>
  495. protected string GetTextSubtitleParam(StreamState state, bool performConversion)
  496. {
  497. var path = state.SubtitleStream.IsExternal ? GetConvertedAssPath(state.MediaPath, state.SubtitleStream, performConversion) :
  498. GetExtractedAssPath(state, performConversion);
  499. if (string.IsNullOrEmpty(path))
  500. {
  501. return string.Empty;
  502. }
  503. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  504. return string.Format(",ass='{0}',setpts=PTS -{1}/TB",
  505. path.Replace('\\', '/').Replace(":/", "\\:/"),
  506. Math.Round(seconds).ToString(UsCulture));
  507. }
  508. /// <summary>
  509. /// Gets the extracted ass path.
  510. /// </summary>
  511. /// <param name="state">The state.</param>
  512. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  513. /// <returns>System.String.</returns>
  514. private string GetExtractedAssPath(StreamState state, bool performConversion)
  515. {
  516. var path = EncodingManager.GetSubtitleCachePath(state.MediaPath, state.SubtitleStream.Index, ".ass");
  517. if (performConversion)
  518. {
  519. InputType type;
  520. var inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, null, state.PlayableStreamFileNames, out type);
  521. try
  522. {
  523. var parentPath = Path.GetDirectoryName(path);
  524. Directory.CreateDirectory(parentPath);
  525. // Don't re-encode ass/ssa to ass because ffmpeg ass encoder fails if there's more than one ass rectangle. Affect Anime mostly.
  526. // See https://lists.ffmpeg.org/pipermail/ffmpeg-cvslog/2013-April/063616.html
  527. var isAssSubtitle = string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase);
  528. var task = MediaEncoder.ExtractTextSubtitle(inputPath, type, state.SubtitleStream.Index, isAssSubtitle, path, CancellationToken.None);
  529. Task.WaitAll(task);
  530. }
  531. catch
  532. {
  533. return null;
  534. }
  535. }
  536. return path;
  537. }
  538. /// <summary>
  539. /// Gets the converted ass path.
  540. /// </summary>
  541. /// <param name="mediaPath">The media path.</param>
  542. /// <param name="subtitleStream">The subtitle stream.</param>
  543. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  544. /// <returns>System.String.</returns>
  545. private string GetConvertedAssPath(string mediaPath, MediaStream subtitleStream, bool performConversion)
  546. {
  547. var path = EncodingManager.GetSubtitleCachePath(subtitleStream.Path, ".ass");
  548. if (performConversion)
  549. {
  550. try
  551. {
  552. var parentPath = Path.GetDirectoryName(path);
  553. Directory.CreateDirectory(parentPath);
  554. var task = MediaEncoder.ConvertTextSubtitleToAss(subtitleStream.Path, path, subtitleStream.Language, CancellationToken.None);
  555. Task.WaitAll(task);
  556. }
  557. catch
  558. {
  559. return null;
  560. }
  561. }
  562. return path;
  563. }
  564. /// <summary>
  565. /// Gets the internal graphical subtitle param.
  566. /// </summary>
  567. /// <param name="state">The state.</param>
  568. /// <param name="outputVideoCodec">The output video codec.</param>
  569. /// <returns>System.String.</returns>
  570. protected string GetInternalGraphicalSubtitleParam(StreamState state, string outputVideoCodec)
  571. {
  572. var outputSizeParam = string.Empty;
  573. var request = state.VideoRequest;
  574. // Add resolution params, if specified
  575. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  576. {
  577. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec, false).TrimEnd('"');
  578. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  579. }
  580. var videoSizeParam = string.Empty;
  581. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  582. {
  583. videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  584. }
  585. return string.Format(" -filter_complex \"[0:{0}]format=yuva444p{3},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{1}] [sub] overlay{2}\"",
  586. state.SubtitleStream.Index,
  587. state.VideoStream.Index,
  588. outputSizeParam,
  589. videoSizeParam);
  590. }
  591. /// <summary>
  592. /// Gets the probe size argument.
  593. /// </summary>
  594. /// <param name="mediaPath">The media path.</param>
  595. /// <param name="isVideo">if set to <c>true</c> [is video].</param>
  596. /// <param name="videoType">Type of the video.</param>
  597. /// <param name="isoType">Type of the iso.</param>
  598. /// <returns>System.String.</returns>
  599. private string GetProbeSizeArgument(string mediaPath, bool isVideo, VideoType? videoType, IsoType? isoType)
  600. {
  601. var type = !isVideo ? MediaEncoderHelpers.GetInputType(null, null) :
  602. MediaEncoderHelpers.GetInputType(videoType, isoType);
  603. return MediaEncoder.GetProbeSizeArgument(type);
  604. }
  605. /// <summary>
  606. /// Gets the number of audio channels to specify on the command line
  607. /// </summary>
  608. /// <param name="request">The request.</param>
  609. /// <param name="audioStream">The audio stream.</param>
  610. /// <returns>System.Nullable{System.Int32}.</returns>
  611. protected int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream)
  612. {
  613. if (audioStream != null)
  614. {
  615. if (audioStream.Channels > 2 && string.Equals(request.AudioCodec, "wma", StringComparison.OrdinalIgnoreCase))
  616. {
  617. // wmav2 currently only supports two channel output
  618. return 2;
  619. }
  620. }
  621. if (request.MaxAudioChannels.HasValue)
  622. {
  623. if (audioStream != null && audioStream.Channels.HasValue)
  624. {
  625. return Math.Min(request.MaxAudioChannels.Value, audioStream.Channels.Value);
  626. }
  627. return request.MaxAudioChannels.Value;
  628. }
  629. return request.AudioChannels;
  630. }
  631. /// <summary>
  632. /// Determines whether the specified stream is H264.
  633. /// </summary>
  634. /// <param name="stream">The stream.</param>
  635. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  636. protected bool IsH264(MediaStream stream)
  637. {
  638. return stream.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  639. stream.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  640. }
  641. /// <summary>
  642. /// Gets the name of the output audio codec
  643. /// </summary>
  644. /// <param name="request">The request.</param>
  645. /// <returns>System.String.</returns>
  646. protected string GetAudioCodec(StreamRequest request)
  647. {
  648. var codec = request.AudioCodec;
  649. if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase))
  650. {
  651. return "aac -strict experimental";
  652. }
  653. if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase))
  654. {
  655. return "libmp3lame";
  656. }
  657. if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase))
  658. {
  659. return "libvorbis";
  660. }
  661. if (string.Equals(codec, "wma", StringComparison.OrdinalIgnoreCase))
  662. {
  663. return "wmav2";
  664. }
  665. return codec.ToLower();
  666. }
  667. /// <summary>
  668. /// Gets the name of the output video codec
  669. /// </summary>
  670. /// <param name="request">The request.</param>
  671. /// <returns>System.String.</returns>
  672. protected string GetVideoCodec(VideoStreamRequest request)
  673. {
  674. var codec = request.VideoCodec;
  675. if (!string.IsNullOrEmpty(codec))
  676. {
  677. if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase))
  678. {
  679. return "libx264";
  680. }
  681. if (string.Equals(codec, "vpx", StringComparison.OrdinalIgnoreCase))
  682. {
  683. return "libvpx";
  684. }
  685. if (string.Equals(codec, "wmv", StringComparison.OrdinalIgnoreCase))
  686. {
  687. return "msmpeg4";
  688. }
  689. if (string.Equals(codec, "theora", StringComparison.OrdinalIgnoreCase))
  690. {
  691. return "libtheora";
  692. }
  693. return codec.ToLower();
  694. }
  695. return "copy";
  696. }
  697. /// <summary>
  698. /// Gets the input argument.
  699. /// </summary>
  700. /// <param name="state">The state.</param>
  701. /// <returns>System.String.</returns>
  702. protected string GetInputArgument(StreamState state)
  703. {
  704. if (state.SendInputOverStandardInput)
  705. {
  706. return "-";
  707. }
  708. var type = InputType.File;
  709. var inputPath = new[] { state.MediaPath };
  710. if (state.IsInputVideo)
  711. {
  712. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  713. {
  714. inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, state.IsoMount, state.PlayableStreamFileNames, out type);
  715. }
  716. }
  717. return MediaEncoder.GetInputArgument(inputPath, type);
  718. }
  719. /// <summary>
  720. /// Starts the FFMPEG.
  721. /// </summary>
  722. /// <param name="state">The state.</param>
  723. /// <param name="outputPath">The output path.</param>
  724. /// <returns>Task.</returns>
  725. protected async Task StartFfMpeg(StreamState state, string outputPath)
  726. {
  727. if (!File.Exists(MediaEncoder.EncoderPath))
  728. {
  729. throw new InvalidOperationException("ffmpeg was not found at " + MediaEncoder.EncoderPath);
  730. }
  731. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  732. if (state.IsInputVideo && state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  733. {
  734. state.IsoMount = await IsoManager.Mount(state.MediaPath, CancellationToken.None).ConfigureAwait(false);
  735. }
  736. var commandLineArgs = GetCommandLineArguments(outputPath, state, true);
  737. if (ServerConfigurationManager.Configuration.EnableDebugEncodingLogging)
  738. {
  739. commandLineArgs = "-loglevel debug " + commandLineArgs;
  740. }
  741. var process = new Process
  742. {
  743. StartInfo = new ProcessStartInfo
  744. {
  745. CreateNoWindow = true,
  746. UseShellExecute = false,
  747. // Must consume both stdout and stderr or deadlocks may occur
  748. RedirectStandardOutput = true,
  749. RedirectStandardError = true,
  750. FileName = MediaEncoder.EncoderPath,
  751. WorkingDirectory = Path.GetDirectoryName(MediaEncoder.EncoderPath),
  752. Arguments = commandLineArgs,
  753. WindowStyle = ProcessWindowStyle.Hidden,
  754. ErrorDialog = false,
  755. RedirectStandardInput = state.SendInputOverStandardInput
  756. },
  757. EnableRaisingEvents = true
  758. };
  759. ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, state.IsInputVideo, state.Request.StartTimeTicks, state.MediaPath, state.Request.DeviceId);
  760. Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
  761. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");
  762. Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
  763. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  764. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);
  765. process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);
  766. try
  767. {
  768. process.Start();
  769. }
  770. catch (Exception ex)
  771. {
  772. Logger.ErrorException("Error starting ffmpeg", ex);
  773. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
  774. state.LogFileStream.Dispose();
  775. throw;
  776. }
  777. if (state.SendInputOverStandardInput)
  778. {
  779. StreamToStandardInput(process, state);
  780. }
  781. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  782. process.BeginOutputReadLine();
  783. #pragma warning disable 4014
  784. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  785. process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);
  786. #pragma warning restore 4014
  787. // Wait for the file to exist before proceeeding
  788. while (!File.Exists(outputPath))
  789. {
  790. await Task.Delay(100).ConfigureAwait(false);
  791. }
  792. // Allow a small amount of time to buffer a little
  793. if (state.IsInputVideo)
  794. {
  795. await Task.Delay(500).ConfigureAwait(false);
  796. }
  797. // This is arbitrary, but add a little buffer time when internet streaming
  798. if (state.IsRemote)
  799. {
  800. await Task.Delay(3000).ConfigureAwait(false);
  801. }
  802. }
  803. private async void StreamToStandardInput(Process process, StreamState state)
  804. {
  805. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  806. try
  807. {
  808. await StreamToStandardInputInternal(process, state).ConfigureAwait(false);
  809. }
  810. catch (OperationCanceledException)
  811. {
  812. Logger.Debug("Stream to standard input closed normally.");
  813. }
  814. catch (Exception ex)
  815. {
  816. Logger.ErrorException("Error writing to standard input", ex);
  817. }
  818. }
  819. private async Task StreamToStandardInputInternal(Process process, StreamState state)
  820. {
  821. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  822. using (var fileStream = FileSystem.GetFileStream(state.MediaPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  823. {
  824. await new EndlessStreamCopy().CopyStream(fileStream, process.StandardInput.BaseStream, state.StandardInputCancellationTokenSource.Token).ConfigureAwait(false);
  825. }
  826. }
  827. protected int? GetVideoBitrateParamValue(StreamState state)
  828. {
  829. var bitrate = state.VideoRequest.VideoBitRate;
  830. if (state.VideoStream != null)
  831. {
  832. var isUpscaling = state.VideoRequest.Height.HasValue && state.VideoStream.Height.HasValue &&
  833. state.VideoRequest.Height.Value > state.VideoStream.Height.Value;
  834. if (state.VideoRequest.Width.HasValue && state.VideoStream.Width.HasValue &&
  835. state.VideoRequest.Width.Value > state.VideoStream.Width.Value)
  836. {
  837. isUpscaling = true;
  838. }
  839. // Don't allow bitrate increases unless upscaling
  840. if (!isUpscaling)
  841. {
  842. if (bitrate.HasValue && state.VideoStream.BitRate.HasValue)
  843. {
  844. bitrate = Math.Min(bitrate.Value, state.VideoStream.BitRate.Value);
  845. }
  846. }
  847. }
  848. return bitrate;
  849. }
  850. protected string GetVideoBitrateParam(StreamState state, string videoCodec, bool isHls)
  851. {
  852. var bitrate = GetVideoBitrateParamValue(state);
  853. if (bitrate.HasValue)
  854. {
  855. var hasFixedResolution = state.VideoRequest.HasFixedResolution;
  856. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  857. {
  858. if (hasFixedResolution)
  859. {
  860. return string.Format(" -minrate:v ({0}*.90) -maxrate:v ({0}*1.10) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture));
  861. }
  862. // With vpx when crf is used, b:v becomes a max rate
  863. // https://trac.ffmpeg.org/wiki/vpxEncodingGuide
  864. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  865. }
  866. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  867. {
  868. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  869. }
  870. // H264
  871. if (hasFixedResolution)
  872. {
  873. if (isHls)
  874. {
  875. return string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture));
  876. }
  877. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  878. }
  879. return string.Format(" -maxrate {0} -bufsize {1}",
  880. bitrate.Value.ToString(UsCulture),
  881. (bitrate.Value * 2).ToString(UsCulture));
  882. }
  883. return string.Empty;
  884. }
  885. protected int? GetAudioBitrateParam(StreamState state)
  886. {
  887. if (state.Request.AudioBitRate.HasValue)
  888. {
  889. // Make sure we don't request a bitrate higher than the source
  890. var currentBitrate = state.AudioStream == null ? state.Request.AudioBitRate.Value : state.AudioStream.BitRate ?? state.Request.AudioBitRate.Value;
  891. return Math.Min(currentBitrate, state.Request.AudioBitRate.Value);
  892. }
  893. return null;
  894. }
  895. /// <summary>
  896. /// Gets the user agent param.
  897. /// </summary>
  898. /// <param name="path">The path.</param>
  899. /// <returns>System.String.</returns>
  900. private string GetUserAgentParam(string path)
  901. {
  902. var useragent = GetUserAgent(path);
  903. if (!string.IsNullOrEmpty(useragent))
  904. {
  905. return "-user-agent \"" + useragent + "\"";
  906. }
  907. return string.Empty;
  908. }
  909. /// <summary>
  910. /// Gets the user agent.
  911. /// </summary>
  912. /// <param name="path">The path.</param>
  913. /// <returns>System.String.</returns>
  914. protected string GetUserAgent(string path)
  915. {
  916. if (string.IsNullOrEmpty(path))
  917. {
  918. throw new ArgumentNullException("path");
  919. }
  920. if (path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1)
  921. {
  922. return "QuickTime/7.7.4";
  923. }
  924. return string.Empty;
  925. }
  926. /// <summary>
  927. /// Processes the exited.
  928. /// </summary>
  929. /// <param name="process">The process.</param>
  930. /// <param name="state">The state.</param>
  931. protected async void OnFfMpegProcessExited(Process process, StreamState state)
  932. {
  933. if (state.IsoMount != null)
  934. {
  935. state.IsoMount.Dispose();
  936. state.IsoMount = null;
  937. }
  938. if (state.StandardInputCancellationTokenSource != null)
  939. {
  940. state.StandardInputCancellationTokenSource.Cancel();
  941. }
  942. var outputFilePath = GetOutputFilePath(state);
  943. state.LogFileStream.Dispose();
  944. try
  945. {
  946. Logger.Info("FFMpeg exited with code {0} for {1}", process.ExitCode, outputFilePath);
  947. }
  948. catch
  949. {
  950. Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
  951. }
  952. if (!string.IsNullOrEmpty(state.LiveTvStreamId))
  953. {
  954. try
  955. {
  956. await LiveTvManager.CloseLiveStream(state.LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  957. }
  958. catch (Exception ex)
  959. {
  960. Logger.ErrorException("Error closing live tv stream", ex);
  961. }
  962. }
  963. }
  964. protected double? GetFramerateParam(StreamState state)
  965. {
  966. if (state.VideoRequest != null)
  967. {
  968. if (state.VideoRequest.Framerate.HasValue)
  969. {
  970. return state.VideoRequest.Framerate.Value;
  971. }
  972. var maxrate = state.VideoRequest.MaxFramerate ?? 23.976;
  973. if (state.VideoStream != null)
  974. {
  975. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  976. if (contentRate.HasValue && contentRate.Value > maxrate)
  977. {
  978. return maxrate;
  979. }
  980. }
  981. }
  982. return null;
  983. }
  984. /// <summary>
  985. /// Parses the parameters.
  986. /// </summary>
  987. /// <param name="request">The request.</param>
  988. private void ParseParams(StreamRequest request)
  989. {
  990. var vals = request.Params.Split(';');
  991. var videoRequest = request as VideoStreamRequest;
  992. for (var i = 0; i < vals.Length; i++)
  993. {
  994. var val = vals[i];
  995. if (string.IsNullOrWhiteSpace(val))
  996. {
  997. continue;
  998. }
  999. if (i == 0)
  1000. {
  1001. request.DeviceProfileId = val;
  1002. }
  1003. else if (i == 1)
  1004. {
  1005. request.DeviceId = val;
  1006. }
  1007. else if (i == 2)
  1008. {
  1009. request.MediaSourceId = val;
  1010. }
  1011. else if (i == 3)
  1012. {
  1013. request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  1014. }
  1015. else if (i == 4)
  1016. {
  1017. if (videoRequest != null)
  1018. {
  1019. videoRequest.VideoCodec = val;
  1020. }
  1021. }
  1022. else if (i == 5)
  1023. {
  1024. request.AudioCodec = val;
  1025. }
  1026. else if (i == 6)
  1027. {
  1028. if (videoRequest != null)
  1029. {
  1030. videoRequest.AudioStreamIndex = int.Parse(val, UsCulture);
  1031. }
  1032. }
  1033. else if (i == 7)
  1034. {
  1035. if (videoRequest != null)
  1036. {
  1037. videoRequest.SubtitleStreamIndex = int.Parse(val, UsCulture);
  1038. }
  1039. }
  1040. else if (i == 8)
  1041. {
  1042. if (videoRequest != null)
  1043. {
  1044. videoRequest.VideoBitRate = int.Parse(val, UsCulture);
  1045. }
  1046. }
  1047. else if (i == 9)
  1048. {
  1049. request.AudioBitRate = int.Parse(val, UsCulture);
  1050. }
  1051. else if (i == 10)
  1052. {
  1053. request.MaxAudioChannels = int.Parse(val, UsCulture);
  1054. }
  1055. else if (i == 11)
  1056. {
  1057. if (videoRequest != null)
  1058. {
  1059. videoRequest.MaxWidth = int.Parse(val, UsCulture);
  1060. }
  1061. }
  1062. else if (i == 12)
  1063. {
  1064. if (videoRequest != null)
  1065. {
  1066. videoRequest.MaxHeight = int.Parse(val, UsCulture);
  1067. }
  1068. }
  1069. else if (i == 13)
  1070. {
  1071. if (videoRequest != null)
  1072. {
  1073. videoRequest.Framerate = int.Parse(val, UsCulture);
  1074. }
  1075. }
  1076. else if (i == 14)
  1077. {
  1078. if (videoRequest != null)
  1079. {
  1080. request.StartTimeTicks = long.Parse(val, UsCulture);
  1081. }
  1082. }
  1083. else if (i == 15)
  1084. {
  1085. if (videoRequest != null)
  1086. {
  1087. videoRequest.Level = val;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. /// <summary>
  1093. /// Gets the state.
  1094. /// </summary>
  1095. /// <param name="request">The request.</param>
  1096. /// <param name="cancellationToken">The cancellation token.</param>
  1097. /// <returns>StreamState.</returns>
  1098. protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
  1099. {
  1100. if (!string.IsNullOrWhiteSpace(request.Params))
  1101. {
  1102. ParseParams(request);
  1103. }
  1104. if (request.ThrowDebugError)
  1105. {
  1106. throw new InvalidOperationException("You asked for a debug error, you got one.");
  1107. }
  1108. var user = AuthorizationRequestFilterAttribute.GetCurrentUser(Request, UserManager);
  1109. var url = Request.PathInfo;
  1110. if (string.IsNullOrEmpty(request.AudioCodec))
  1111. {
  1112. request.AudioCodec = InferAudioCodec(url);
  1113. }
  1114. var state = new StreamState
  1115. {
  1116. Request = request,
  1117. RequestedUrl = url
  1118. };
  1119. var item = string.IsNullOrEmpty(request.MediaSourceId) ?
  1120. DtoService.GetItemByDtoId(request.Id) :
  1121. DtoService.GetItemByDtoId(request.MediaSourceId);
  1122. if (user != null && item.GetPlayAccess(user) != PlayAccess.Full)
  1123. {
  1124. throw new ArgumentException(string.Format("{0} is not allowed to play media.", user.Name));
  1125. }
  1126. if (item is ILiveTvRecording)
  1127. {
  1128. var recording = await LiveTvManager.GetInternalRecording(request.Id, cancellationToken).ConfigureAwait(false);
  1129. state.VideoType = VideoType.VideoFile;
  1130. state.IsInputVideo = string.Equals(recording.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1131. state.PlayableStreamFileNames = new List<string>();
  1132. var path = recording.RecordingInfo.Path;
  1133. var mediaUrl = recording.RecordingInfo.Url;
  1134. if (string.IsNullOrWhiteSpace(path) && string.IsNullOrWhiteSpace(mediaUrl))
  1135. {
  1136. var streamInfo = await LiveTvManager.GetRecordingStream(request.Id, cancellationToken).ConfigureAwait(false);
  1137. state.LiveTvStreamId = streamInfo.Id;
  1138. path = streamInfo.Path;
  1139. mediaUrl = streamInfo.Url;
  1140. }
  1141. if (!string.IsNullOrEmpty(path) && File.Exists(path))
  1142. {
  1143. state.MediaPath = path;
  1144. state.IsRemote = false;
  1145. state.SendInputOverStandardInput = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  1146. }
  1147. else if (!string.IsNullOrEmpty(mediaUrl))
  1148. {
  1149. state.MediaPath = mediaUrl;
  1150. state.IsRemote = true;
  1151. }
  1152. //state.RunTimeTicks = recording.RunTimeTicks;
  1153. if (recording.RecordingInfo.Status == RecordingStatus.InProgress && !state.IsRemote)
  1154. {
  1155. await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
  1156. }
  1157. state.ReadInputAtNativeFramerate = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  1158. state.AudioSync = "1000";
  1159. state.DeInterlace = true;
  1160. state.InputVideoSync = "-1";
  1161. state.InputAudioSync = "1";
  1162. }
  1163. else if (item is LiveTvChannel)
  1164. {
  1165. var channel = LiveTvManager.GetInternalChannel(request.Id);
  1166. state.VideoType = VideoType.VideoFile;
  1167. state.IsInputVideo = string.Equals(channel.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1168. state.PlayableStreamFileNames = new List<string>();
  1169. var streamInfo = await LiveTvManager.GetChannelStream(request.Id, cancellationToken).ConfigureAwait(false);
  1170. state.LiveTvStreamId = streamInfo.Id;
  1171. if (!string.IsNullOrEmpty(streamInfo.Path) && File.Exists(streamInfo.Path))
  1172. {
  1173. state.MediaPath = streamInfo.Path;
  1174. state.IsRemote = false;
  1175. await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
  1176. }
  1177. else if (!string.IsNullOrEmpty(streamInfo.Url))
  1178. {
  1179. state.MediaPath = streamInfo.Url;
  1180. state.IsRemote = true;
  1181. }
  1182. state.ReadInputAtNativeFramerate = true;
  1183. state.AudioSync = "1000";
  1184. state.DeInterlace = true;
  1185. state.InputVideoSync = "-1";
  1186. state.InputAudioSync = "1";
  1187. }
  1188. else
  1189. {
  1190. state.MediaPath = item.Path;
  1191. state.IsRemote = item.LocationType == LocationType.Remote;
  1192. var video = item as Video;
  1193. if (video != null)
  1194. {
  1195. state.IsInputVideo = true;
  1196. state.VideoType = video.VideoType;
  1197. state.IsoType = video.IsoType;
  1198. state.PlayableStreamFileNames = video.PlayableStreamFileNames == null
  1199. ? new List<string>()
  1200. : video.PlayableStreamFileNames.ToList();
  1201. }
  1202. state.RunTimeTicks = item.RunTimeTicks;
  1203. }
  1204. var videoRequest = request as VideoStreamRequest;
  1205. var mediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery
  1206. {
  1207. ItemId = item.Id
  1208. }).ToList();
  1209. if (videoRequest != null)
  1210. {
  1211. if (string.IsNullOrEmpty(videoRequest.VideoCodec))
  1212. {
  1213. videoRequest.VideoCodec = InferVideoCodec(url);
  1214. }
  1215. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  1216. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  1217. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  1218. if (state.VideoStream != null && state.VideoStream.IsInterlaced)
  1219. {
  1220. state.DeInterlace = true;
  1221. }
  1222. EnforceResolutionLimit(state, videoRequest);
  1223. }
  1224. else
  1225. {
  1226. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  1227. }
  1228. state.HasMediaStreams = mediaStreams.Count > 0;
  1229. state.SegmentLength = state.ReadInputAtNativeFramerate ? 5 : 10;
  1230. state.HlsListSize = state.ReadInputAtNativeFramerate ? 100 : 1440;
  1231. ApplyDeviceProfileSettings(state);
  1232. return state;
  1233. }
  1234. private void ApplyDeviceProfileSettings(StreamState state)
  1235. {
  1236. var headers = new Dictionary<string, string>();
  1237. foreach (var key in Request.Headers.AllKeys)
  1238. {
  1239. headers[key] = Request.Headers[key];
  1240. }
  1241. var profile = string.IsNullOrWhiteSpace(state.Request.DeviceProfileId) ?
  1242. DlnaManager.GetProfile(headers) :
  1243. DlnaManager.GetProfile(state.Request.DeviceProfileId);
  1244. if (profile == null)
  1245. {
  1246. // Don't use settings from the default profile.
  1247. // Only use a specific profile if it was requested.
  1248. return;
  1249. }
  1250. var container = Path.GetExtension(state.RequestedUrl);
  1251. if (string.IsNullOrEmpty(container))
  1252. {
  1253. container = Path.GetExtension(GetOutputFilePath(state));
  1254. }
  1255. var audioCodec = state.Request.AudioCodec;
  1256. if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.AudioStream != null)
  1257. {
  1258. audioCodec = state.AudioStream.Codec;
  1259. }
  1260. var videoCodec = state.VideoRequest == null ? null : state.VideoRequest.VideoCodec;
  1261. if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.VideoStream != null)
  1262. {
  1263. videoCodec = state.VideoStream.Codec;
  1264. }
  1265. var mediaProfile = state.VideoRequest == null ?
  1266. profile.GetAudioMediaProfile(container, audioCodec, state.AudioStream) :
  1267. profile.GetVideoMediaProfile(container, audioCodec, videoCodec, state.AudioStream, state.VideoStream);
  1268. if (mediaProfile != null)
  1269. {
  1270. state.MimeType = mediaProfile.MimeType;
  1271. state.OrgPn = mediaProfile.OrgPn;
  1272. }
  1273. var transcodingProfile = state.VideoRequest == null ?
  1274. profile.GetAudioTranscodingProfile(container, audioCodec) :
  1275. profile.GetVideoTranscodingProfile(container, audioCodec, videoCodec);
  1276. if (transcodingProfile != null)
  1277. {
  1278. state.EstimateContentLength = transcodingProfile.EstimateContentLength;
  1279. state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
  1280. state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  1281. foreach (var setting in transcodingProfile.Settings)
  1282. {
  1283. switch (setting.Name)
  1284. {
  1285. case TranscodingSettingType.VideoProfile:
  1286. {
  1287. if (state.VideoRequest != null && string.IsNullOrWhiteSpace(state.VideoRequest.Profile))
  1288. {
  1289. state.VideoRequest.Profile = setting.Value;
  1290. }
  1291. break;
  1292. }
  1293. default:
  1294. throw new ArgumentException("Unrecognized TranscodingSettingType");
  1295. }
  1296. }
  1297. }
  1298. }
  1299. /// <summary>
  1300. /// Adds the dlna headers.
  1301. /// </summary>
  1302. /// <param name="state">The state.</param>
  1303. /// <param name="responseHeaders">The response headers.</param>
  1304. /// <param name="isStaticallyStreamed">if set to <c>true</c> [is statically streamed].</param>
  1305. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  1306. protected void AddDlnaHeaders(StreamState state, IDictionary<string, string> responseHeaders, bool isStaticallyStreamed)
  1307. {
  1308. var timeSeek = GetHeader("TimeSeekRange.dlna.org");
  1309. if (!string.IsNullOrEmpty(timeSeek))
  1310. {
  1311. ResultFactory.ThrowError(406, "Time seek not supported during encoding.", responseHeaders);
  1312. return;
  1313. }
  1314. var transferMode = GetHeader("transferMode.dlna.org");
  1315. responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode;
  1316. responseHeaders["realTimeInfo.dlna.org"] = "DLNA.ORG_TLAG=*";
  1317. var contentFeatures = string.Empty;
  1318. var extension = GetOutputFileExtension(state);
  1319. // first bit means Time based seek supported, second byte range seek supported (not sure about the order now), so 01 = only byte seek, 10 = time based, 11 = both, 00 = none
  1320. var orgOp = isStaticallyStreamed || state.TranscodeSeekInfo == TranscodeSeekInfo.Bytes ? ";DLNA.ORG_OP=01" : ";DLNA.ORG_OP=00";
  1321. // 0 = native, 1 = transcoded
  1322. var orgCi = isStaticallyStreamed ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
  1323. const string dlnaflags = ";DLNA.ORG_FLAGS=01500000000000000000000000000000";
  1324. if (!string.IsNullOrWhiteSpace(state.OrgPn))
  1325. {
  1326. contentFeatures = "DLNA.ORG_PN=" + state.OrgPn;
  1327. }
  1328. else if (string.Equals(extension, ".mp3", StringComparison.OrdinalIgnoreCase))
  1329. {
  1330. contentFeatures = "DLNA.ORG_PN=MP3";
  1331. }
  1332. else if (string.Equals(extension, ".aac", StringComparison.OrdinalIgnoreCase))
  1333. {
  1334. contentFeatures = "DLNA.ORG_PN=AAC_ISO";
  1335. }
  1336. else if (string.Equals(extension, ".wma", StringComparison.OrdinalIgnoreCase))
  1337. {
  1338. contentFeatures = "DLNA.ORG_PN=WMABASE";
  1339. }
  1340. else if (string.Equals(extension, ".avi", StringComparison.OrdinalIgnoreCase))
  1341. {
  1342. contentFeatures = "DLNA.ORG_PN=AVI";
  1343. }
  1344. else if (string.Equals(extension, ".mkv", StringComparison.OrdinalIgnoreCase))
  1345. {
  1346. contentFeatures = "DLNA.ORG_PN=MATROSKA";
  1347. }
  1348. else if (string.Equals(extension, ".mp4", StringComparison.OrdinalIgnoreCase))
  1349. {
  1350. contentFeatures = "DLNA.ORG_PN=AVC_MP4_MP_HD_720p_AAC";
  1351. }
  1352. else if (string.Equals(extension, ".mpeg", StringComparison.OrdinalIgnoreCase))
  1353. {
  1354. contentFeatures = "DLNA.ORG_PN=MPEG_PS_PAL";
  1355. }
  1356. else if (string.Equals(extension, ".ts", StringComparison.OrdinalIgnoreCase))
  1357. {
  1358. contentFeatures = "DLNA.ORG_PN=MPEG_PS_PAL";
  1359. }
  1360. //else if (string.Equals(extension, ".wmv", StringComparison.OrdinalIgnoreCase))
  1361. //{
  1362. // contentFeatures = "DLNA.ORG_PN=WMVHIGH_BASE";
  1363. //}
  1364. //else if (string.Equals(extension, ".asf", StringComparison.OrdinalIgnoreCase))
  1365. //{
  1366. // // ??
  1367. // contentFeatures = "DLNA.ORG_PN=WMVHIGH_BASE";
  1368. //}
  1369. if (!string.IsNullOrEmpty(contentFeatures))
  1370. {
  1371. responseHeaders["contentFeatures.dlna.org"] = (contentFeatures + orgOp + orgCi + dlnaflags).Trim(';');
  1372. }
  1373. foreach (var item in responseHeaders)
  1374. {
  1375. Request.Response.AddHeader(item.Key, item.Value);
  1376. }
  1377. }
  1378. /// <summary>
  1379. /// Enforces the resolution limit.
  1380. /// </summary>
  1381. /// <param name="state">The state.</param>
  1382. /// <param name="videoRequest">The video request.</param>
  1383. private void EnforceResolutionLimit(StreamState state, VideoStreamRequest videoRequest)
  1384. {
  1385. // If enabled, allow whatever the client asks for
  1386. if (ServerConfigurationManager.Configuration.AllowVideoUpscaling)
  1387. {
  1388. return;
  1389. }
  1390. // Switch the incoming params to be ceilings rather than fixed values
  1391. videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width;
  1392. videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height;
  1393. videoRequest.Width = null;
  1394. videoRequest.Height = null;
  1395. }
  1396. protected string GetInputModifier(StreamState state)
  1397. {
  1398. var inputModifier = string.Empty;
  1399. var probeSize = GetProbeSizeArgument(state.MediaPath, state.IsInputVideo, state.VideoType, state.IsoType);
  1400. inputModifier += " " + probeSize;
  1401. inputModifier = inputModifier.Trim();
  1402. inputModifier += " " + GetUserAgentParam(state.MediaPath);
  1403. inputModifier = inputModifier.Trim();
  1404. inputModifier += " " + GetFastSeekCommandLineParameter(state.Request);
  1405. inputModifier = inputModifier.Trim();
  1406. if (state.VideoRequest != null)
  1407. {
  1408. inputModifier += " -fflags genpts";
  1409. }
  1410. if (!string.IsNullOrEmpty(state.InputFormat))
  1411. {
  1412. inputModifier += " -f " + state.InputFormat;
  1413. }
  1414. if (!string.IsNullOrEmpty(state.InputVideoCodec))
  1415. {
  1416. inputModifier += " -vcodec " + state.InputVideoCodec;
  1417. }
  1418. if (!string.IsNullOrEmpty(state.InputAudioCodec))
  1419. {
  1420. inputModifier += " -acodec " + state.InputAudioCodec;
  1421. }
  1422. if (!string.IsNullOrEmpty(state.InputAudioSync))
  1423. {
  1424. inputModifier += " -async " + state.InputAudioSync;
  1425. }
  1426. if (!string.IsNullOrEmpty(state.InputVideoSync))
  1427. {
  1428. inputModifier += " -vsync " + state.InputVideoSync;
  1429. }
  1430. if (state.ReadInputAtNativeFramerate)
  1431. {
  1432. inputModifier += " -re";
  1433. }
  1434. return inputModifier;
  1435. }
  1436. /// <summary>
  1437. /// Infers the audio codec based on the url
  1438. /// </summary>
  1439. /// <param name="url">The URL.</param>
  1440. /// <returns>System.Nullable{AudioCodecs}.</returns>
  1441. private string InferAudioCodec(string url)
  1442. {
  1443. var ext = Path.GetExtension(url);
  1444. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  1445. {
  1446. return "mp3";
  1447. }
  1448. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  1449. {
  1450. return "aac";
  1451. }
  1452. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  1453. {
  1454. return "wma";
  1455. }
  1456. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  1457. {
  1458. return "vorbis";
  1459. }
  1460. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  1461. {
  1462. return "vorbis";
  1463. }
  1464. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1465. {
  1466. return "vorbis";
  1467. }
  1468. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1469. {
  1470. return "vorbis";
  1471. }
  1472. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  1473. {
  1474. return "vorbis";
  1475. }
  1476. return "copy";
  1477. }
  1478. /// <summary>
  1479. /// Infers the video codec.
  1480. /// </summary>
  1481. /// <param name="url">The URL.</param>
  1482. /// <returns>System.Nullable{VideoCodecs}.</returns>
  1483. private string InferVideoCodec(string url)
  1484. {
  1485. var ext = Path.GetExtension(url);
  1486. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  1487. {
  1488. return "wmv";
  1489. }
  1490. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1491. {
  1492. return "vpx";
  1493. }
  1494. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1495. {
  1496. return "theora";
  1497. }
  1498. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  1499. {
  1500. return "h264";
  1501. }
  1502. return "copy";
  1503. }
  1504. }
  1505. }