BaseStreamingService.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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.Entities;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.Library;
  15. using MediaBrowser.Model.LiveTv;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. namespace MediaBrowser.Api.Playback
  25. {
  26. /// <summary>
  27. /// Class BaseStreamingService
  28. /// </summary>
  29. public abstract class BaseStreamingService : BaseApiService
  30. {
  31. /// <summary>
  32. /// Gets or sets the application paths.
  33. /// </summary>
  34. /// <value>The application paths.</value>
  35. protected IServerConfigurationManager ServerConfigurationManager { get; private set; }
  36. /// <summary>
  37. /// Gets or sets the user manager.
  38. /// </summary>
  39. /// <value>The user manager.</value>
  40. protected IUserManager UserManager { get; private set; }
  41. /// <summary>
  42. /// Gets or sets the library manager.
  43. /// </summary>
  44. /// <value>The library manager.</value>
  45. protected ILibraryManager LibraryManager { get; private set; }
  46. /// <summary>
  47. /// Gets or sets the iso manager.
  48. /// </summary>
  49. /// <value>The iso manager.</value>
  50. protected IIsoManager IsoManager { get; private set; }
  51. /// <summary>
  52. /// Gets or sets the media encoder.
  53. /// </summary>
  54. /// <value>The media encoder.</value>
  55. protected IMediaEncoder MediaEncoder { get; private set; }
  56. protected IEncodingManager EncodingManager { get; private set; }
  57. protected IDtoService DtoService { get; private set; }
  58. protected IFileSystem FileSystem { get; private set; }
  59. protected IItemRepository ItemRepository { get; private set; }
  60. protected ILiveTvManager LiveTvManager { get; private set; }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  63. /// </summary>
  64. /// <param name="serverConfig">The server configuration.</param>
  65. /// <param name="userManager">The user manager.</param>
  66. /// <param name="libraryManager">The library manager.</param>
  67. /// <param name="isoManager">The iso manager.</param>
  68. /// <param name="mediaEncoder">The media encoder.</param>
  69. /// <param name="dtoService">The dto service.</param>
  70. /// <param name="fileSystem">The file system.</param>
  71. /// <param name="itemRepository">The item repository.</param>
  72. protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IEncodingManager encodingManager)
  73. {
  74. EncodingManager = encodingManager;
  75. LiveTvManager = liveTvManager;
  76. ItemRepository = itemRepository;
  77. FileSystem = fileSystem;
  78. DtoService = dtoService;
  79. ServerConfigurationManager = serverConfig;
  80. UserManager = userManager;
  81. LibraryManager = libraryManager;
  82. IsoManager = isoManager;
  83. MediaEncoder = mediaEncoder;
  84. }
  85. /// <summary>
  86. /// Gets the command line arguments.
  87. /// </summary>
  88. /// <param name="outputPath">The output path.</param>
  89. /// <param name="state">The state.</param>
  90. /// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
  91. /// <returns>System.String.</returns>
  92. protected abstract string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions);
  93. /// <summary>
  94. /// Gets the type of the transcoding job.
  95. /// </summary>
  96. /// <value>The type of the transcoding job.</value>
  97. protected abstract TranscodingJobType TranscodingJobType { get; }
  98. /// <summary>
  99. /// Gets the output file extension.
  100. /// </summary>
  101. /// <param name="state">The state.</param>
  102. /// <returns>System.String.</returns>
  103. protected virtual string GetOutputFileExtension(StreamState state)
  104. {
  105. return Path.GetExtension(state.RequestedUrl);
  106. }
  107. /// <summary>
  108. /// Gets the output file path.
  109. /// </summary>
  110. /// <param name="state">The state.</param>
  111. /// <returns>System.String.</returns>
  112. protected virtual string GetOutputFilePath(StreamState state)
  113. {
  114. var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath;
  115. var outputFileExtension = GetOutputFileExtension(state);
  116. return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
  117. }
  118. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  119. /// <summary>
  120. /// The fast seek offset seconds
  121. /// </summary>
  122. private const int FastSeekOffsetSeconds = 1;
  123. /// <summary>
  124. /// Gets the fast seek command line parameter.
  125. /// </summary>
  126. /// <param name="request">The request.</param>
  127. /// <returns>System.String.</returns>
  128. /// <value>The fast seek command line parameter.</value>
  129. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  130. {
  131. var time = request.StartTimeTicks;
  132. if (time.HasValue)
  133. {
  134. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds;
  135. if (seconds > 0)
  136. {
  137. return string.Format("-ss {0}", seconds.ToString(UsCulture));
  138. }
  139. }
  140. return string.Empty;
  141. }
  142. /// <summary>
  143. /// Gets the slow seek command line parameter.
  144. /// </summary>
  145. /// <param name="request">The request.</param>
  146. /// <returns>System.String.</returns>
  147. /// <value>The slow seek command line parameter.</value>
  148. protected string GetSlowSeekCommandLineParameter(StreamRequest request)
  149. {
  150. var time = request.StartTimeTicks;
  151. if (time.HasValue)
  152. {
  153. if (TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds > 0)
  154. {
  155. return string.Format(" -ss {0}", FastSeekOffsetSeconds.ToString(UsCulture));
  156. }
  157. }
  158. return string.Empty;
  159. }
  160. /// <summary>
  161. /// Gets the map args.
  162. /// </summary>
  163. /// <param name="state">The state.</param>
  164. /// <returns>System.String.</returns>
  165. protected virtual string GetMapArgs(StreamState state)
  166. {
  167. var args = string.Empty;
  168. if (state.IsRemote || !state.HasMediaStreams)
  169. {
  170. return string.Empty;
  171. }
  172. if (state.VideoStream != null)
  173. {
  174. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  175. }
  176. else
  177. {
  178. args += "-map -0:v";
  179. }
  180. if (state.AudioStream != null)
  181. {
  182. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  183. }
  184. else
  185. {
  186. args += " -map -0:a";
  187. }
  188. if (state.SubtitleStream == null)
  189. {
  190. args += " -map -0:s";
  191. }
  192. return args;
  193. }
  194. /// <summary>
  195. /// Determines which stream will be used for playback
  196. /// </summary>
  197. /// <param name="allStream">All stream.</param>
  198. /// <param name="desiredIndex">Index of the desired.</param>
  199. /// <param name="type">The type.</param>
  200. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  201. /// <returns>MediaStream.</returns>
  202. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  203. {
  204. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  205. if (desiredIndex.HasValue)
  206. {
  207. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  208. if (stream != null)
  209. {
  210. return stream;
  211. }
  212. }
  213. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  214. {
  215. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  216. streams.FirstOrDefault();
  217. }
  218. // Just return the first one
  219. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  220. }
  221. protected EncodingQuality GetQualitySetting()
  222. {
  223. var quality = ServerConfigurationManager.Configuration.MediaEncodingQuality;
  224. if (quality == EncodingQuality.Auto)
  225. {
  226. var cpuCount = Environment.ProcessorCount;
  227. if (cpuCount >= 4)
  228. {
  229. //return EncodingQuality.HighQuality;
  230. }
  231. return EncodingQuality.HighSpeed;
  232. }
  233. return quality;
  234. }
  235. /// <summary>
  236. /// Gets the number of threads.
  237. /// </summary>
  238. /// <returns>System.Int32.</returns>
  239. /// <exception cref="System.Exception">Unrecognized MediaEncodingQuality value.</exception>
  240. protected int GetNumberOfThreads(StreamState state, bool isWebm)
  241. {
  242. // Use more when this is true. -re will keep cpu usage under control
  243. if (state.ReadInputAtNativeFramerate)
  244. {
  245. if (isWebm)
  246. {
  247. return Math.Max(Environment.ProcessorCount - 1, 1);
  248. }
  249. return 0;
  250. }
  251. // Webm: http://www.webmproject.org/docs/encoder-parameters/
  252. // 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
  253. // for the coefficient data if the encoder selected --token-parts > 0 at encode time.
  254. switch (GetQualitySetting())
  255. {
  256. case EncodingQuality.HighSpeed:
  257. return 2;
  258. case EncodingQuality.HighQuality:
  259. return 2;
  260. case EncodingQuality.MaxQuality:
  261. return isWebm ? 2 : 0;
  262. default:
  263. throw new Exception("Unrecognized MediaEncodingQuality value.");
  264. }
  265. }
  266. /// <summary>
  267. /// Gets the video bitrate to specify on the command line
  268. /// </summary>
  269. /// <param name="state">The state.</param>
  270. /// <param name="videoCodec">The video codec.</param>
  271. /// <returns>System.String.</returns>
  272. protected string GetVideoQualityParam(StreamState state, string videoCodec, bool isHls)
  273. {
  274. var param = string.Empty;
  275. var hasFixedResolution = state.VideoRequest.HasFixedResolution;
  276. var qualitySetting = GetQualitySetting();
  277. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  278. {
  279. switch (qualitySetting)
  280. {
  281. case EncodingQuality.HighSpeed:
  282. param = "-preset ultrafast";
  283. break;
  284. case EncodingQuality.HighQuality:
  285. param = "-preset superfast";
  286. break;
  287. case EncodingQuality.MaxQuality:
  288. param = "-preset superfast";
  289. break;
  290. }
  291. if (!isHls)
  292. {
  293. switch (qualitySetting)
  294. {
  295. case EncodingQuality.HighSpeed:
  296. param += " -crf 23";
  297. break;
  298. case EncodingQuality.HighQuality:
  299. param += " -crf 20";
  300. break;
  301. case EncodingQuality.MaxQuality:
  302. param += " -crf 18";
  303. break;
  304. }
  305. }
  306. }
  307. // webm
  308. else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  309. {
  310. // http://www.webmproject.org/docs/encoder-parameters/
  311. param = "-speed 16 -quality good -profile:v 0 -slices 8";
  312. if (!hasFixedResolution)
  313. {
  314. switch (qualitySetting)
  315. {
  316. case EncodingQuality.HighSpeed:
  317. param += " -crf 18";
  318. break;
  319. case EncodingQuality.HighQuality:
  320. param += " -crf 10";
  321. break;
  322. case EncodingQuality.MaxQuality:
  323. param += " -crf 4";
  324. break;
  325. }
  326. }
  327. }
  328. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  329. {
  330. param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  331. }
  332. // asf/wmv
  333. else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase))
  334. {
  335. param = "-qmin 2";
  336. }
  337. else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  338. {
  339. param = "-mbd 2";
  340. }
  341. param += GetVideoBitrateParam(state, videoCodec, isHls);
  342. var framerate = GetFramerateParam(state);
  343. if (framerate.HasValue)
  344. {
  345. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  346. }
  347. if (!string.IsNullOrEmpty(state.VideoSync))
  348. {
  349. param += " -vsync " + state.VideoSync;
  350. }
  351. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  352. {
  353. param += " -profile:v " + state.VideoRequest.Profile;
  354. }
  355. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  356. {
  357. param += " -level " + state.VideoRequest.Level;
  358. }
  359. return param;
  360. }
  361. protected string GetAudioFilterParam(StreamState state, bool isHls)
  362. {
  363. var volParam = string.Empty;
  364. var audioSampleRate = string.Empty;
  365. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  366. // Boost volume to 200% when downsampling from 6ch to 2ch
  367. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  368. {
  369. volParam = ",volume=2.000000";
  370. }
  371. if (state.Request.AudioSampleRate.HasValue)
  372. {
  373. audioSampleRate = state.Request.AudioSampleRate.Value + ":";
  374. }
  375. var adelay = isHls ? "adelay=1," : string.Empty;
  376. var pts = string.Empty;
  377. if (state.SubtitleStream != null)
  378. {
  379. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  380. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  381. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  382. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  383. {
  384. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  385. pts = string.Format(",asetpts=PTS-{0}/TB",
  386. Math.Round(seconds).ToString(UsCulture));
  387. }
  388. }
  389. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  390. adelay,
  391. audioSampleRate,
  392. volParam,
  393. pts,
  394. state.AudioSync);
  395. }
  396. /// <summary>
  397. /// If we're going to put a fixed size on the command line, this will calculate it
  398. /// </summary>
  399. /// <param name="state">The state.</param>
  400. /// <param name="outputVideoCodec">The output video codec.</param>
  401. /// <param name="performTextSubtitleConversion">if set to <c>true</c> [perform text subtitle conversion].</param>
  402. /// <returns>System.String.</returns>
  403. protected string GetOutputSizeParam(StreamState state, string outputVideoCodec, bool performTextSubtitleConversion)
  404. {
  405. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  406. var assSubtitleParam = string.Empty;
  407. var copyTsParam = string.Empty;
  408. var yadifParam = state.DeInterlace ? "yadif=0:-1:0," : string.Empty;
  409. var request = state.VideoRequest;
  410. if (state.SubtitleStream != null)
  411. {
  412. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  413. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  414. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  415. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  416. {
  417. assSubtitleParam = GetTextSubtitleParam(state, performTextSubtitleConversion);
  418. copyTsParam = " -copyts";
  419. }
  420. }
  421. // If fixed dimensions were supplied
  422. if (request.Width.HasValue && request.Height.HasValue)
  423. {
  424. var widthParam = request.Width.Value.ToString(UsCulture);
  425. var heightParam = request.Height.Value.ToString(UsCulture);
  426. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  427. }
  428. // If Max dimensions were supplied
  429. //this makes my brain hurt. 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.IsNullOrEmpty(codec))
  650. {
  651. if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase))
  652. {
  653. return "aac -strict experimental";
  654. }
  655. if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase))
  656. {
  657. return "libmp3lame";
  658. }
  659. if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase))
  660. {
  661. return "libvorbis";
  662. }
  663. if (string.Equals(codec, "wma", StringComparison.OrdinalIgnoreCase))
  664. {
  665. return "wmav2";
  666. }
  667. return codec.ToLower();
  668. }
  669. return "copy";
  670. }
  671. /// <summary>
  672. /// Gets the name of the output video codec
  673. /// </summary>
  674. /// <param name="request">The request.</param>
  675. /// <returns>System.String.</returns>
  676. protected string GetVideoCodec(VideoStreamRequest request)
  677. {
  678. var codec = request.VideoCodec;
  679. if (!string.IsNullOrEmpty(codec))
  680. {
  681. if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase))
  682. {
  683. return "libx264";
  684. }
  685. if (string.Equals(codec, "vpx", StringComparison.OrdinalIgnoreCase))
  686. {
  687. return "libvpx";
  688. }
  689. if (string.Equals(codec, "wmv", StringComparison.OrdinalIgnoreCase))
  690. {
  691. return "msmpeg4";
  692. }
  693. if (string.Equals(codec, "theora", StringComparison.OrdinalIgnoreCase))
  694. {
  695. return "libtheora";
  696. }
  697. return codec.ToLower();
  698. }
  699. return "copy";
  700. }
  701. /// <summary>
  702. /// Gets the input argument.
  703. /// </summary>
  704. /// <param name="state">The state.</param>
  705. /// <returns>System.String.</returns>
  706. protected string GetInputArgument(StreamState state)
  707. {
  708. if (state.SendInputOverStandardInput)
  709. {
  710. return "-";
  711. }
  712. var type = InputType.File;
  713. var inputPath = new[] { state.MediaPath };
  714. if (state.IsInputVideo)
  715. {
  716. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  717. {
  718. inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, state.IsoMount, state.PlayableStreamFileNames, out type);
  719. }
  720. }
  721. return MediaEncoder.GetInputArgument(inputPath, type);
  722. }
  723. /// <summary>
  724. /// Starts the FFMPEG.
  725. /// </summary>
  726. /// <param name="state">The state.</param>
  727. /// <param name="outputPath">The output path.</param>
  728. /// <returns>Task.</returns>
  729. protected async Task StartFfMpeg(StreamState state, string outputPath)
  730. {
  731. if (!File.Exists(MediaEncoder.EncoderPath))
  732. {
  733. throw new InvalidOperationException("ffmpeg was not found at " + MediaEncoder.EncoderPath);
  734. }
  735. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  736. if (state.IsInputVideo && state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  737. {
  738. state.IsoMount = await IsoManager.Mount(state.MediaPath, CancellationToken.None).ConfigureAwait(false);
  739. }
  740. var commandLineArgs = GetCommandLineArguments(outputPath, state, true);
  741. if (ServerConfigurationManager.Configuration.EnableDebugEncodingLogging)
  742. {
  743. commandLineArgs = "-loglevel debug " + commandLineArgs;
  744. }
  745. var process = new Process
  746. {
  747. StartInfo = new ProcessStartInfo
  748. {
  749. CreateNoWindow = true,
  750. UseShellExecute = false,
  751. // Must consume both stdout and stderr or deadlocks may occur
  752. RedirectStandardOutput = true,
  753. RedirectStandardError = true,
  754. FileName = MediaEncoder.EncoderPath,
  755. WorkingDirectory = Path.GetDirectoryName(MediaEncoder.EncoderPath),
  756. Arguments = commandLineArgs,
  757. WindowStyle = ProcessWindowStyle.Hidden,
  758. ErrorDialog = false,
  759. RedirectStandardInput = state.SendInputOverStandardInput
  760. },
  761. EnableRaisingEvents = true
  762. };
  763. ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, state.IsInputVideo, state.Request.StartTimeTicks, state.MediaPath, state.Request.DeviceId);
  764. Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
  765. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");
  766. Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
  767. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  768. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);
  769. process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);
  770. try
  771. {
  772. process.Start();
  773. }
  774. catch (Exception ex)
  775. {
  776. Logger.ErrorException("Error starting ffmpeg", ex);
  777. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
  778. state.LogFileStream.Dispose();
  779. throw;
  780. }
  781. if (state.SendInputOverStandardInput)
  782. {
  783. StreamToStandardInput(process, state);
  784. }
  785. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  786. process.BeginOutputReadLine();
  787. #pragma warning disable 4014
  788. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  789. process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);
  790. #pragma warning restore 4014
  791. // Wait for the file to exist before proceeeding
  792. while (!File.Exists(outputPath))
  793. {
  794. await Task.Delay(100).ConfigureAwait(false);
  795. }
  796. // Allow a small amount of time to buffer a little
  797. if (state.IsInputVideo)
  798. {
  799. await Task.Delay(500).ConfigureAwait(false);
  800. }
  801. // This is arbitrary, but add a little buffer time when internet streaming
  802. if (state.IsRemote)
  803. {
  804. await Task.Delay(3000).ConfigureAwait(false);
  805. }
  806. }
  807. private async void StreamToStandardInput(Process process, StreamState state)
  808. {
  809. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  810. try
  811. {
  812. await StreamToStandardInputInternal(process, state).ConfigureAwait(false);
  813. }
  814. catch (OperationCanceledException)
  815. {
  816. Logger.Debug("Stream to standard input closed normally.");
  817. }
  818. catch (Exception ex)
  819. {
  820. Logger.ErrorException("Error writing to standard input", ex);
  821. }
  822. }
  823. private async Task StreamToStandardInputInternal(Process process, StreamState state)
  824. {
  825. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  826. using (var fileStream = FileSystem.GetFileStream(state.MediaPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  827. {
  828. await new EndlessStreamCopy().CopyStream(fileStream, process.StandardInput.BaseStream, state.StandardInputCancellationTokenSource.Token).ConfigureAwait(false);
  829. }
  830. }
  831. protected int? GetVideoBitrateParamValue(StreamState state)
  832. {
  833. var bitrate = state.VideoRequest.VideoBitRate;
  834. if (state.VideoStream != null)
  835. {
  836. var isUpscaling = state.VideoRequest.Height.HasValue && state.VideoStream.Height.HasValue &&
  837. state.VideoRequest.Height.Value > state.VideoStream.Height.Value;
  838. if (state.VideoRequest.Width.HasValue && state.VideoStream.Width.HasValue &&
  839. state.VideoRequest.Width.Value > state.VideoStream.Width.Value)
  840. {
  841. isUpscaling = true;
  842. }
  843. // Don't allow bitrate increases unless upscaling
  844. if (!isUpscaling)
  845. {
  846. if (bitrate.HasValue && state.VideoStream.BitRate.HasValue)
  847. {
  848. bitrate = Math.Min(bitrate.Value, state.VideoStream.BitRate.Value);
  849. }
  850. }
  851. }
  852. return bitrate;
  853. }
  854. protected string GetVideoBitrateParam(StreamState state, string videoCodec, bool isHls)
  855. {
  856. var bitrate = GetVideoBitrateParamValue(state);
  857. if (bitrate.HasValue)
  858. {
  859. var hasFixedResolution = state.VideoRequest.HasFixedResolution;
  860. if (isHls)
  861. {
  862. return string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture));
  863. }
  864. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  865. {
  866. if (hasFixedResolution)
  867. {
  868. return string.Format(" -minrate:v ({0}*.90) -maxrate:v ({0}*1.10) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture));
  869. }
  870. // With vpx when crf is used, b:v becomes a max rate
  871. // https://trac.ffmpeg.org/wiki/vpxEncodingGuide
  872. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  873. //return string.Format(" -minrate:v ({0}*.95) -maxrate:v ({0}*1.05) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture));
  874. }
  875. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  876. {
  877. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  878. }
  879. // H264
  880. if (hasFixedResolution)
  881. {
  882. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  883. }
  884. return string.Format(" -maxrate {0} -bufsize {1}",
  885. bitrate.Value.ToString(UsCulture),
  886. (bitrate.Value * 2).ToString(UsCulture));
  887. }
  888. return string.Empty;
  889. }
  890. protected int? GetAudioBitrateParam(StreamState state)
  891. {
  892. if (state.Request.AudioBitRate.HasValue)
  893. {
  894. // Make sure we don't request a bitrate higher than the source
  895. var currentBitrate = state.AudioStream == null ? state.Request.AudioBitRate.Value : state.AudioStream.BitRate ?? state.Request.AudioBitRate.Value;
  896. return Math.Min(currentBitrate, state.Request.AudioBitRate.Value);
  897. }
  898. return null;
  899. }
  900. /// <summary>
  901. /// Gets the user agent param.
  902. /// </summary>
  903. /// <param name="path">The path.</param>
  904. /// <returns>System.String.</returns>
  905. private string GetUserAgentParam(string path)
  906. {
  907. var useragent = GetUserAgent(path);
  908. if (!string.IsNullOrEmpty(useragent))
  909. {
  910. return "-user-agent \"" + useragent + "\"";
  911. }
  912. return string.Empty;
  913. }
  914. /// <summary>
  915. /// Gets the user agent.
  916. /// </summary>
  917. /// <param name="path">The path.</param>
  918. /// <returns>System.String.</returns>
  919. protected string GetUserAgent(string path)
  920. {
  921. if (string.IsNullOrEmpty(path))
  922. {
  923. throw new ArgumentNullException("path");
  924. }
  925. if (path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1)
  926. {
  927. return "QuickTime/7.7.4";
  928. }
  929. return string.Empty;
  930. }
  931. /// <summary>
  932. /// Processes the exited.
  933. /// </summary>
  934. /// <param name="process">The process.</param>
  935. /// <param name="state">The state.</param>
  936. protected async void OnFfMpegProcessExited(Process process, StreamState state)
  937. {
  938. if (state.IsoMount != null)
  939. {
  940. state.IsoMount.Dispose();
  941. state.IsoMount = null;
  942. }
  943. if (state.StandardInputCancellationTokenSource != null)
  944. {
  945. state.StandardInputCancellationTokenSource.Cancel();
  946. }
  947. var outputFilePath = GetOutputFilePath(state);
  948. state.LogFileStream.Dispose();
  949. try
  950. {
  951. Logger.Info("FFMpeg exited with code {0} for {1}", process.ExitCode, outputFilePath);
  952. }
  953. catch
  954. {
  955. Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
  956. }
  957. if (!string.IsNullOrEmpty(state.LiveTvStreamId))
  958. {
  959. try
  960. {
  961. await LiveTvManager.CloseLiveStream(state.LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  962. }
  963. catch (Exception ex)
  964. {
  965. Logger.ErrorException("Error closing live tv stream", ex);
  966. }
  967. }
  968. }
  969. protected double? GetFramerateParam(StreamState state)
  970. {
  971. if (state.VideoRequest != null && state.VideoRequest.Framerate.HasValue)
  972. {
  973. return state.VideoRequest.Framerate.Value;
  974. }
  975. if (state.VideoStream != null)
  976. {
  977. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  978. if (contentRate.HasValue && contentRate.Value > 23.976)
  979. {
  980. return 23.976;
  981. }
  982. }
  983. return null;
  984. }
  985. /// <summary>
  986. /// Parses the parameters.
  987. /// </summary>
  988. /// <param name="request">The request.</param>
  989. private void ParseParams(StreamRequest request)
  990. {
  991. var vals = request.Params.Split(';');
  992. var videoRequest = request as VideoStreamRequest;
  993. for (var i = 0; i < vals.Length; i++)
  994. {
  995. var val = vals[i];
  996. if (string.IsNullOrWhiteSpace(val))
  997. {
  998. continue;
  999. }
  1000. if (i == 0)
  1001. {
  1002. // Device profile name
  1003. }
  1004. else if (i == 1)
  1005. {
  1006. request.DeviceId = val;
  1007. }
  1008. else if (i == 2)
  1009. {
  1010. request.MediaSourceId = val;
  1011. }
  1012. else if (i == 3)
  1013. {
  1014. request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  1015. }
  1016. else if (i == 4)
  1017. {
  1018. if (videoRequest != null)
  1019. {
  1020. videoRequest.VideoCodec = val;
  1021. }
  1022. }
  1023. else if (i == 5)
  1024. {
  1025. request.AudioCodec = val;
  1026. }
  1027. else if (i == 6)
  1028. {
  1029. if (videoRequest != null)
  1030. {
  1031. videoRequest.AudioStreamIndex = int.Parse(val, UsCulture);
  1032. }
  1033. }
  1034. else if (i == 7)
  1035. {
  1036. if (videoRequest != null)
  1037. {
  1038. videoRequest.SubtitleStreamIndex = int.Parse(val, UsCulture);
  1039. }
  1040. }
  1041. else if (i == 8)
  1042. {
  1043. if (videoRequest != null)
  1044. {
  1045. videoRequest.VideoBitRate = int.Parse(val, UsCulture);
  1046. }
  1047. }
  1048. else if (i == 9)
  1049. {
  1050. request.AudioBitRate = int.Parse(val, UsCulture);
  1051. }
  1052. else if (i == 10)
  1053. {
  1054. request.MaxAudioChannels = int.Parse(val, UsCulture);
  1055. }
  1056. else if (i == 11)
  1057. {
  1058. if (videoRequest != null)
  1059. {
  1060. request.StartTimeTicks = long.Parse(val, UsCulture);
  1061. }
  1062. }
  1063. else if (i == 12)
  1064. {
  1065. if (videoRequest != null)
  1066. {
  1067. videoRequest.Profile = val;
  1068. }
  1069. }
  1070. else if (i == 13)
  1071. {
  1072. if (videoRequest != null)
  1073. {
  1074. videoRequest.Level = val;
  1075. }
  1076. }
  1077. }
  1078. }
  1079. /// <summary>
  1080. /// Gets the state.
  1081. /// </summary>
  1082. /// <param name="request">The request.</param>
  1083. /// <param name="cancellationToken">The cancellation token.</param>
  1084. /// <returns>StreamState.</returns>
  1085. protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
  1086. {
  1087. if (!string.IsNullOrWhiteSpace(request.Params))
  1088. {
  1089. ParseParams(request);
  1090. }
  1091. if (request.ThrowDebugError)
  1092. {
  1093. throw new InvalidOperationException("You asked for a debug error, you got one.");
  1094. }
  1095. var user = AuthorizationRequestFilterAttribute.GetCurrentUser(Request, UserManager);
  1096. var url = Request.PathInfo;
  1097. if (string.IsNullOrEmpty(request.AudioCodec))
  1098. {
  1099. request.AudioCodec = InferAudioCodec(url);
  1100. }
  1101. var state = new StreamState
  1102. {
  1103. Request = request,
  1104. RequestedUrl = url
  1105. };
  1106. var item = string.IsNullOrEmpty(request.MediaSourceId) ?
  1107. DtoService.GetItemByDtoId(request.Id) :
  1108. DtoService.GetItemByDtoId(request.MediaSourceId);
  1109. if (user != null && item.GetPlayAccess(user) != PlayAccess.Full)
  1110. {
  1111. throw new ArgumentException(string.Format("{0} is not allowed to play media.", user.Name));
  1112. }
  1113. if (item is ILiveTvRecording)
  1114. {
  1115. var recording = await LiveTvManager.GetInternalRecording(request.Id, cancellationToken).ConfigureAwait(false);
  1116. state.VideoType = VideoType.VideoFile;
  1117. state.IsInputVideo = string.Equals(recording.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1118. state.PlayableStreamFileNames = new List<string>();
  1119. var path = recording.RecordingInfo.Path;
  1120. var mediaUrl = recording.RecordingInfo.Url;
  1121. if (string.IsNullOrWhiteSpace(path) && string.IsNullOrWhiteSpace(mediaUrl))
  1122. {
  1123. var streamInfo = await LiveTvManager.GetRecordingStream(request.Id, cancellationToken).ConfigureAwait(false);
  1124. state.LiveTvStreamId = streamInfo.Id;
  1125. path = streamInfo.Path;
  1126. mediaUrl = streamInfo.Url;
  1127. }
  1128. if (!string.IsNullOrEmpty(path) && File.Exists(path))
  1129. {
  1130. state.MediaPath = path;
  1131. state.IsRemote = false;
  1132. state.SendInputOverStandardInput = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  1133. }
  1134. else if (!string.IsNullOrEmpty(mediaUrl))
  1135. {
  1136. state.MediaPath = mediaUrl;
  1137. state.IsRemote = true;
  1138. }
  1139. //state.RunTimeTicks = recording.RunTimeTicks;
  1140. if (recording.RecordingInfo.Status == RecordingStatus.InProgress && !state.IsRemote)
  1141. {
  1142. await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
  1143. }
  1144. state.ReadInputAtNativeFramerate = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  1145. state.AudioSync = "1000";
  1146. state.DeInterlace = true;
  1147. state.InputVideoSync = "-1";
  1148. state.InputAudioSync = "1";
  1149. }
  1150. else if (item is LiveTvChannel)
  1151. {
  1152. var channel = LiveTvManager.GetInternalChannel(request.Id);
  1153. state.VideoType = VideoType.VideoFile;
  1154. state.IsInputVideo = string.Equals(channel.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1155. state.PlayableStreamFileNames = new List<string>();
  1156. var streamInfo = await LiveTvManager.GetChannelStream(request.Id, cancellationToken).ConfigureAwait(false);
  1157. state.LiveTvStreamId = streamInfo.Id;
  1158. if (!string.IsNullOrEmpty(streamInfo.Path) && File.Exists(streamInfo.Path))
  1159. {
  1160. state.MediaPath = streamInfo.Path;
  1161. state.IsRemote = false;
  1162. await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
  1163. }
  1164. else if (!string.IsNullOrEmpty(streamInfo.Url))
  1165. {
  1166. state.MediaPath = streamInfo.Url;
  1167. state.IsRemote = true;
  1168. }
  1169. state.ReadInputAtNativeFramerate = true;
  1170. state.AudioSync = "1000";
  1171. state.DeInterlace = true;
  1172. state.InputVideoSync = "-1";
  1173. state.InputAudioSync = "1";
  1174. }
  1175. else
  1176. {
  1177. state.MediaPath = item.Path;
  1178. state.IsRemote = item.LocationType == LocationType.Remote;
  1179. var video = item as Video;
  1180. if (video != null)
  1181. {
  1182. state.IsInputVideo = true;
  1183. state.VideoType = video.VideoType;
  1184. state.IsoType = video.IsoType;
  1185. state.PlayableStreamFileNames = video.PlayableStreamFileNames == null
  1186. ? new List<string>()
  1187. : video.PlayableStreamFileNames.ToList();
  1188. }
  1189. state.RunTimeTicks = item.RunTimeTicks;
  1190. }
  1191. var videoRequest = request as VideoStreamRequest;
  1192. var mediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery
  1193. {
  1194. ItemId = item.Id
  1195. }).ToList();
  1196. if (videoRequest != null)
  1197. {
  1198. if (string.IsNullOrEmpty(videoRequest.VideoCodec))
  1199. {
  1200. videoRequest.VideoCodec = InferVideoCodec(url);
  1201. }
  1202. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  1203. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  1204. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  1205. if (state.VideoStream != null && state.VideoStream.IsInterlaced)
  1206. {
  1207. state.DeInterlace = true;
  1208. }
  1209. EnforceResolutionLimit(state, videoRequest);
  1210. }
  1211. else
  1212. {
  1213. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  1214. }
  1215. state.HasMediaStreams = mediaStreams.Count > 0;
  1216. state.SegmentLength = state.ReadInputAtNativeFramerate ? 5 : 10;
  1217. state.HlsListSize = state.ReadInputAtNativeFramerate ? 100 : 1440;
  1218. return state;
  1219. }
  1220. /// <summary>
  1221. /// Enforces the resolution limit.
  1222. /// </summary>
  1223. /// <param name="state">The state.</param>
  1224. /// <param name="videoRequest">The video request.</param>
  1225. private void EnforceResolutionLimit(StreamState state, VideoStreamRequest videoRequest)
  1226. {
  1227. // If enabled, allow whatever the client asks for
  1228. if (ServerConfigurationManager.Configuration.AllowVideoUpscaling)
  1229. {
  1230. return;
  1231. }
  1232. // Switch the incoming params to be ceilings rather than fixed values
  1233. videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width;
  1234. videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height;
  1235. videoRequest.Width = null;
  1236. videoRequest.Height = null;
  1237. }
  1238. protected string GetInputModifier(StreamState state)
  1239. {
  1240. var inputModifier = string.Empty;
  1241. var probeSize = GetProbeSizeArgument(state.MediaPath, state.IsInputVideo, state.VideoType, state.IsoType);
  1242. inputModifier += " " + probeSize;
  1243. inputModifier = inputModifier.Trim();
  1244. inputModifier += " " + GetUserAgentParam(state.MediaPath);
  1245. inputModifier = inputModifier.Trim();
  1246. inputModifier += " " + GetFastSeekCommandLineParameter(state.Request);
  1247. inputModifier = inputModifier.Trim();
  1248. if (state.VideoRequest != null)
  1249. {
  1250. inputModifier += " -fflags genpts";
  1251. }
  1252. if (!string.IsNullOrEmpty(state.InputFormat))
  1253. {
  1254. inputModifier += " -f " + state.InputFormat;
  1255. }
  1256. if (!string.IsNullOrEmpty(state.InputVideoCodec))
  1257. {
  1258. inputModifier += " -vcodec " + state.InputVideoCodec;
  1259. }
  1260. if (!string.IsNullOrEmpty(state.InputAudioCodec))
  1261. {
  1262. inputModifier += " -acodec " + state.InputAudioCodec;
  1263. }
  1264. if (!string.IsNullOrEmpty(state.InputAudioSync))
  1265. {
  1266. inputModifier += " -async " + state.InputAudioSync;
  1267. }
  1268. if (!string.IsNullOrEmpty(state.InputVideoSync))
  1269. {
  1270. inputModifier += " -vsync " + state.InputVideoSync;
  1271. }
  1272. if (state.ReadInputAtNativeFramerate)
  1273. {
  1274. inputModifier += " -re";
  1275. }
  1276. return inputModifier;
  1277. }
  1278. /// <summary>
  1279. /// Infers the audio codec based on the url
  1280. /// </summary>
  1281. /// <param name="url">The URL.</param>
  1282. /// <returns>System.Nullable{AudioCodecs}.</returns>
  1283. private string InferAudioCodec(string url)
  1284. {
  1285. var ext = Path.GetExtension(url);
  1286. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  1287. {
  1288. return "mp3";
  1289. }
  1290. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  1291. {
  1292. return "aac";
  1293. }
  1294. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  1295. {
  1296. return "wma";
  1297. }
  1298. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  1299. {
  1300. return "vorbis";
  1301. }
  1302. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  1303. {
  1304. return "vorbis";
  1305. }
  1306. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1307. {
  1308. return "vorbis";
  1309. }
  1310. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1311. {
  1312. return "vorbis";
  1313. }
  1314. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  1315. {
  1316. return "vorbis";
  1317. }
  1318. return null;
  1319. }
  1320. /// <summary>
  1321. /// Infers the video codec.
  1322. /// </summary>
  1323. /// <param name="url">The URL.</param>
  1324. /// <returns>System.Nullable{VideoCodecs}.</returns>
  1325. private string InferVideoCodec(string url)
  1326. {
  1327. var ext = Path.GetExtension(url);
  1328. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  1329. {
  1330. return "wmv";
  1331. }
  1332. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1333. {
  1334. return "vpx";
  1335. }
  1336. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1337. {
  1338. return "theora";
  1339. }
  1340. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  1341. {
  1342. return "h264";
  1343. }
  1344. return "copy";
  1345. }
  1346. }
  1347. }