BaseStreamingService.cs 58 KB

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