BaseStreamingService.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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.MediaInfo;
  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.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 IDtoService DtoService { get; private set; }
  57. protected IFileSystem FileSystem { get; private set; }
  58. protected IItemRepository ItemRepository { get; private set; }
  59. protected ILiveTvManager LiveTvManager { get; private set; }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  62. /// </summary>
  63. /// <param name="serverConfig">The server configuration.</param>
  64. /// <param name="userManager">The user manager.</param>
  65. /// <param name="libraryManager">The library manager.</param>
  66. /// <param name="isoManager">The iso manager.</param>
  67. /// <param name="mediaEncoder">The media encoder.</param>
  68. /// <param name="dtoService">The dto service.</param>
  69. /// <param name="fileSystem">The file system.</param>
  70. /// <param name="itemRepository">The item repository.</param>
  71. protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager)
  72. {
  73. LiveTvManager = liveTvManager;
  74. ItemRepository = itemRepository;
  75. FileSystem = fileSystem;
  76. DtoService = dtoService;
  77. ServerConfigurationManager = serverConfig;
  78. UserManager = userManager;
  79. LibraryManager = libraryManager;
  80. IsoManager = isoManager;
  81. MediaEncoder = mediaEncoder;
  82. }
  83. /// <summary>
  84. /// Gets the command line arguments.
  85. /// </summary>
  86. /// <param name="outputPath">The output path.</param>
  87. /// <param name="state">The state.</param>
  88. /// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
  89. /// <returns>System.String.</returns>
  90. protected abstract string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions);
  91. /// <summary>
  92. /// Gets the type of the transcoding job.
  93. /// </summary>
  94. /// <value>The type of the transcoding job.</value>
  95. protected abstract TranscodingJobType TranscodingJobType { get; }
  96. /// <summary>
  97. /// Gets the output file extension.
  98. /// </summary>
  99. /// <param name="state">The state.</param>
  100. /// <returns>System.String.</returns>
  101. protected virtual string GetOutputFileExtension(StreamState state)
  102. {
  103. return Path.GetExtension(state.RequestedUrl);
  104. }
  105. /// <summary>
  106. /// Gets the output file path.
  107. /// </summary>
  108. /// <param name="state">The state.</param>
  109. /// <returns>System.String.</returns>
  110. protected virtual string GetOutputFilePath(StreamState state)
  111. {
  112. var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath;
  113. var outputFileExtension = GetOutputFileExtension(state);
  114. return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
  115. }
  116. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  117. /// <summary>
  118. /// The fast seek offset seconds
  119. /// </summary>
  120. private const int FastSeekOffsetSeconds = 1;
  121. /// <summary>
  122. /// Gets the fast seek command line parameter.
  123. /// </summary>
  124. /// <param name="request">The request.</param>
  125. /// <returns>System.String.</returns>
  126. /// <value>The fast seek command line parameter.</value>
  127. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  128. {
  129. var time = request.StartTimeTicks;
  130. if (time.HasValue)
  131. {
  132. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds;
  133. if (seconds > 0)
  134. {
  135. return string.Format("-ss {0}", seconds.ToString(UsCulture));
  136. }
  137. }
  138. return string.Empty;
  139. }
  140. /// <summary>
  141. /// Gets the slow seek command line parameter.
  142. /// </summary>
  143. /// <param name="request">The request.</param>
  144. /// <returns>System.String.</returns>
  145. /// <value>The slow seek command line parameter.</value>
  146. protected string GetSlowSeekCommandLineParameter(StreamRequest request)
  147. {
  148. var time = request.StartTimeTicks;
  149. if (time.HasValue)
  150. {
  151. if (TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds > 0)
  152. {
  153. return string.Format(" -ss {0}", FastSeekOffsetSeconds.ToString(UsCulture));
  154. }
  155. }
  156. return string.Empty;
  157. }
  158. /// <summary>
  159. /// Gets the map args.
  160. /// </summary>
  161. /// <param name="state">The state.</param>
  162. /// <returns>System.String.</returns>
  163. protected virtual string GetMapArgs(StreamState state)
  164. {
  165. var args = string.Empty;
  166. if (state.IsRemote || !state.HasMediaStreams)
  167. {
  168. return string.Empty;
  169. }
  170. if (state.VideoStream != null)
  171. {
  172. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  173. }
  174. else
  175. {
  176. args += "-map -0:v";
  177. }
  178. if (state.AudioStream != null)
  179. {
  180. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  181. }
  182. else
  183. {
  184. args += " -map -0:a";
  185. }
  186. if (state.SubtitleStream == null)
  187. {
  188. args += " -map -0:s";
  189. }
  190. return args;
  191. }
  192. /// <summary>
  193. /// Determines which stream will be used for playback
  194. /// </summary>
  195. /// <param name="allStream">All stream.</param>
  196. /// <param name="desiredIndex">Index of the desired.</param>
  197. /// <param name="type">The type.</param>
  198. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  199. /// <returns>MediaStream.</returns>
  200. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  201. {
  202. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  203. if (desiredIndex.HasValue)
  204. {
  205. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  206. if (stream != null)
  207. {
  208. return stream;
  209. }
  210. }
  211. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  212. {
  213. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  214. streams.FirstOrDefault();
  215. }
  216. // Just return the first one
  217. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  218. }
  219. protected EncodingQuality GetQualitySetting()
  220. {
  221. var quality = ServerConfigurationManager.Configuration.MediaEncodingQuality;
  222. if (quality == EncodingQuality.Auto)
  223. {
  224. var cpuCount = Environment.ProcessorCount;
  225. if (cpuCount >= 4)
  226. {
  227. return EncodingQuality.HighQuality;
  228. }
  229. return EncodingQuality.HighSpeed;
  230. }
  231. return quality;
  232. }
  233. /// <summary>
  234. /// Gets the number of threads.
  235. /// </summary>
  236. /// <returns>System.Int32.</returns>
  237. /// <exception cref="System.Exception">Unrecognized MediaEncodingQuality value.</exception>
  238. protected int GetNumberOfThreads(bool isWebm)
  239. {
  240. // Webm: http://www.webmproject.org/docs/encoder-parameters/
  241. // 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
  242. // for the coefficient data if the encoder selected --token-parts > 0 at encode time.
  243. switch (GetQualitySetting())
  244. {
  245. case EncodingQuality.HighSpeed:
  246. return 2;
  247. case EncodingQuality.HighQuality:
  248. return 2;
  249. case EncodingQuality.MaxQuality:
  250. return isWebm ? 2 : 0;
  251. default:
  252. throw new Exception("Unrecognized MediaEncodingQuality value.");
  253. }
  254. }
  255. /// <summary>
  256. /// Gets the video bitrate to specify on the command line
  257. /// </summary>
  258. /// <param name="state">The state.</param>
  259. /// <param name="videoCodec">The video codec.</param>
  260. /// <returns>System.String.</returns>
  261. protected string GetVideoQualityParam(StreamState state, string videoCodec)
  262. {
  263. // webm
  264. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  265. {
  266. // http://www.webmproject.org/docs/encoder-parameters/
  267. return "-speed 16 -quality good -profile:v 0 -slices 8";
  268. }
  269. // asf/wmv
  270. if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  271. {
  272. return "-g 100 -qmax 15";
  273. }
  274. if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  275. {
  276. return "-preset superfast";
  277. }
  278. if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase))
  279. {
  280. return "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  281. }
  282. return string.Empty;
  283. }
  284. protected string GetAudioFilterParam(StreamState state, bool isHls)
  285. {
  286. var volParam = string.Empty;
  287. var audioSampleRate = string.Empty;
  288. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  289. // Boost volume to 200% when downsampling from 6ch to 2ch
  290. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  291. {
  292. volParam = ",volume=2.000000";
  293. }
  294. if (state.Request.AudioSampleRate.HasValue)
  295. {
  296. audioSampleRate = state.Request.AudioSampleRate.Value + ":";
  297. }
  298. var adelay = isHls ? "adelay=1," : string.Empty;
  299. var pts = string.Empty;
  300. if (state.SubtitleStream != null)
  301. {
  302. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  303. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  304. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  305. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  306. {
  307. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  308. pts = string.Format(",asetpts=PTS-{0}/TB",
  309. Math.Round(seconds).ToString(UsCulture));
  310. }
  311. }
  312. return string.Format("-af \"{0}aresample={1}async=1{2}{3}\"",
  313. adelay,
  314. audioSampleRate,
  315. volParam,
  316. pts);
  317. }
  318. /// <summary>
  319. /// If we're going to put a fixed size on the command line, this will calculate it
  320. /// </summary>
  321. /// <param name="state">The state.</param>
  322. /// <param name="outputVideoCodec">The output video codec.</param>
  323. /// <param name="performTextSubtitleConversion">if set to <c>true</c> [perform text subtitle conversion].</param>
  324. /// <returns>System.String.</returns>
  325. protected string GetOutputSizeParam(StreamState state, string outputVideoCodec, bool performTextSubtitleConversion)
  326. {
  327. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  328. var assSubtitleParam = string.Empty;
  329. var copyTsParam = string.Empty;
  330. var yadifParam = "yadif=0:-1:0,";
  331. var request = state.VideoRequest;
  332. if (state.SubtitleStream != null)
  333. {
  334. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  335. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  336. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  337. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  338. {
  339. assSubtitleParam = GetTextSubtitleParam(state, performTextSubtitleConversion);
  340. copyTsParam = " -copyts";
  341. }
  342. }
  343. // If fixed dimensions were supplied
  344. if (request.Width.HasValue && request.Height.HasValue)
  345. {
  346. var widthParam = request.Width.Value.ToString(UsCulture);
  347. var heightParam = request.Height.Value.ToString(UsCulture);
  348. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"",yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  349. }
  350. var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
  351. // If a fixed width was requested
  352. if (request.Width.HasValue)
  353. {
  354. var widthParam = request.Width.Value.ToString(UsCulture);
  355. return isH264Output ?
  356. string.Format("{3} -vf \"{0}scale={1}:trunc(ow/a/2)*2{2}\"",yadifParam, widthParam, assSubtitleParam, copyTsParam) :
  357. string.Format("{3} -vf \"{0}scale={1}:-1{2}\"",yadifParam, widthParam, assSubtitleParam, copyTsParam);
  358. }
  359. // If a fixed height was requested
  360. if (request.Height.HasValue)
  361. {
  362. var heightParam = request.Height.Value.ToString(UsCulture);
  363. return isH264Output ?
  364. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:{1}{2}\"",yadifParam, heightParam, assSubtitleParam, copyTsParam) :
  365. string.Format("{3} -vf \"{0}scale=-1:{1}{2}\"",yadifParam, heightParam, assSubtitleParam, copyTsParam);
  366. }
  367. // If a max width was requested
  368. if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null))
  369. {
  370. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  371. return isH264Output ?
  372. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):trunc(ow/a/2)*2{2}\"",yadifParam, maxWidthParam, assSubtitleParam, copyTsParam) :
  373. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):-1{2}\"",yadifParam, maxWidthParam, assSubtitleParam, copyTsParam);
  374. }
  375. // If a max height was requested
  376. if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null))
  377. {
  378. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  379. return isH264Output ?
  380. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:min(ih\\,{1}){2}\"",yadifParam, maxHeightParam, assSubtitleParam, copyTsParam) :
  381. string.Format("{3} -vf \"{0}scale=-1:min(ih\\,{1}){2}\"",yadifParam, maxHeightParam, assSubtitleParam, copyTsParam);
  382. }
  383. if (state.VideoStream == null)
  384. {
  385. // No way to figure this out
  386. return string.Empty;
  387. }
  388. // Need to perform calculations manually
  389. // Try to account for bad media info
  390. var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0;
  391. var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0;
  392. var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight);
  393. // If we're encoding with libx264, it can't handle odd numbered widths or heights, so we'll have to fix that
  394. if (isH264Output)
  395. {
  396. var widthParam = outputSize.Width.ToString(UsCulture);
  397. var heightParam = outputSize.Height.ToString(UsCulture);
  398. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"",yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  399. }
  400. // Otherwise use -vf scale since ffmpeg will ensure internally that the aspect ratio is preserved
  401. return string.Format("{3} -vf \"{0}scale={1}:-1{2}\"",yadifParam, Convert.ToInt32(outputSize.Width), assSubtitleParam, copyTsParam);
  402. }
  403. /// <summary>
  404. /// Gets the text subtitle param.
  405. /// </summary>
  406. /// <param name="state">The state.</param>
  407. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  408. /// <returns>System.String.</returns>
  409. protected string GetTextSubtitleParam(StreamState state, bool performConversion)
  410. {
  411. var path = state.SubtitleStream.IsExternal ? GetConvertedAssPath(state.MediaPath, state.SubtitleStream, performConversion) :
  412. GetExtractedAssPath(state, performConversion);
  413. if (string.IsNullOrEmpty(path))
  414. {
  415. return string.Empty;
  416. }
  417. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  418. return string.Format(",ass='{0}',setpts=PTS -{1}/TB",
  419. path.Replace('\\', '/').Replace(":/", "\\:/"),
  420. Math.Round(seconds).ToString(UsCulture));
  421. }
  422. /// <summary>
  423. /// Gets the extracted ass path.
  424. /// </summary>
  425. /// <param name="state">The state.</param>
  426. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  427. /// <returns>System.String.</returns>
  428. private string GetExtractedAssPath(StreamState state, bool performConversion)
  429. {
  430. var path = FFMpegManager.Instance.GetSubtitleCachePath(state.MediaPath, state.SubtitleStream, ".ass");
  431. if (performConversion)
  432. {
  433. InputType type;
  434. var inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, null, state.PlayableStreamFileNames, out type);
  435. try
  436. {
  437. var parentPath = Path.GetDirectoryName(path);
  438. Directory.CreateDirectory(parentPath);
  439. var task = MediaEncoder.ExtractTextSubtitle(inputPath, type, state.SubtitleStream.Index, path, CancellationToken.None);
  440. Task.WaitAll(task);
  441. }
  442. catch
  443. {
  444. return null;
  445. }
  446. }
  447. return path;
  448. }
  449. /// <summary>
  450. /// Gets the converted ass path.
  451. /// </summary>
  452. /// <param name="mediaPath">The media path.</param>
  453. /// <param name="subtitleStream">The subtitle stream.</param>
  454. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  455. /// <returns>System.String.</returns>
  456. private string GetConvertedAssPath(string mediaPath, MediaStream subtitleStream, bool performConversion)
  457. {
  458. var path = FFMpegManager.Instance.GetSubtitleCachePath(mediaPath, subtitleStream, ".ass");
  459. if (performConversion)
  460. {
  461. try
  462. {
  463. var parentPath = Path.GetDirectoryName(path);
  464. Directory.CreateDirectory(parentPath);
  465. var task = MediaEncoder.ConvertTextSubtitleToAss(subtitleStream.Path, path, subtitleStream.Language, CancellationToken.None);
  466. Task.WaitAll(task);
  467. }
  468. catch
  469. {
  470. return null;
  471. }
  472. }
  473. return path;
  474. }
  475. /// <summary>
  476. /// Gets the internal graphical subtitle param.
  477. /// </summary>
  478. /// <param name="state">The state.</param>
  479. /// <param name="outputVideoCodec">The output video codec.</param>
  480. /// <returns>System.String.</returns>
  481. protected string GetInternalGraphicalSubtitleParam(StreamState state, string outputVideoCodec)
  482. {
  483. var outputSizeParam = string.Empty;
  484. var request = state.VideoRequest;
  485. // Add resolution params, if specified
  486. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  487. {
  488. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec, false).TrimEnd('"');
  489. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  490. }
  491. var videoSizeParam = string.Empty;
  492. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  493. {
  494. videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  495. }
  496. return string.Format(" -filter_complex \"[0:{0}]format=yuva444p{3},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{1}] [sub] overlay{2}\"",
  497. state.SubtitleStream.Index,
  498. state.VideoStream.Index,
  499. outputSizeParam,
  500. videoSizeParam);
  501. }
  502. /// <summary>
  503. /// Gets the probe size argument.
  504. /// </summary>
  505. /// <param name="mediaPath">The media path.</param>
  506. /// <param name="isVideo">if set to <c>true</c> [is video].</param>
  507. /// <param name="videoType">Type of the video.</param>
  508. /// <param name="isoType">Type of the iso.</param>
  509. /// <returns>System.String.</returns>
  510. protected string GetProbeSizeArgument(string mediaPath, bool isVideo, VideoType? videoType, IsoType? isoType)
  511. {
  512. var type = !isVideo ? MediaEncoderHelpers.GetInputType(null, null) :
  513. MediaEncoderHelpers.GetInputType(videoType, isoType);
  514. return MediaEncoder.GetProbeSizeArgument(type);
  515. }
  516. /// <summary>
  517. /// Gets the number of audio channels to specify on the command line
  518. /// </summary>
  519. /// <param name="request">The request.</param>
  520. /// <param name="audioStream">The audio stream.</param>
  521. /// <returns>System.Nullable{System.Int32}.</returns>
  522. protected int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream)
  523. {
  524. if (audioStream != null)
  525. {
  526. if (audioStream.Channels > 2 && request.AudioCodec.HasValue)
  527. {
  528. if (request.AudioCodec.Value == AudioCodecs.Wma)
  529. {
  530. // wmav2 currently only supports two channel output
  531. return 2;
  532. }
  533. }
  534. }
  535. return request.AudioChannels;
  536. }
  537. /// <summary>
  538. /// Determines whether the specified stream is H264.
  539. /// </summary>
  540. /// <param name="stream">The stream.</param>
  541. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  542. protected bool IsH264(MediaStream stream)
  543. {
  544. return stream.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  545. stream.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  546. }
  547. /// <summary>
  548. /// Gets the name of the output audio codec
  549. /// </summary>
  550. /// <param name="request">The request.</param>
  551. /// <returns>System.String.</returns>
  552. protected string GetAudioCodec(StreamRequest request)
  553. {
  554. var codec = request.AudioCodec;
  555. if (codec.HasValue)
  556. {
  557. if (codec == AudioCodecs.Aac)
  558. {
  559. return "aac -strict experimental";
  560. }
  561. if (codec == AudioCodecs.Mp3)
  562. {
  563. return "libmp3lame";
  564. }
  565. if (codec == AudioCodecs.Vorbis)
  566. {
  567. return "libvorbis";
  568. }
  569. if (codec == AudioCodecs.Wma)
  570. {
  571. return "wmav2";
  572. }
  573. return codec.ToString().ToLower();
  574. }
  575. return "copy";
  576. }
  577. /// <summary>
  578. /// Gets the name of the output video codec
  579. /// </summary>
  580. /// <param name="request">The request.</param>
  581. /// <returns>System.String.</returns>
  582. protected string GetVideoCodec(VideoStreamRequest request)
  583. {
  584. var codec = request.VideoCodec;
  585. if (codec.HasValue)
  586. {
  587. if (codec == VideoCodecs.H264)
  588. {
  589. return "libx264";
  590. }
  591. if (codec == VideoCodecs.Vpx)
  592. {
  593. return "libvpx";
  594. }
  595. if (codec == VideoCodecs.Wmv)
  596. {
  597. return "wmv2";
  598. }
  599. if (codec == VideoCodecs.Theora)
  600. {
  601. return "libtheora";
  602. }
  603. return codec.ToString().ToLower();
  604. }
  605. return "copy";
  606. }
  607. /// <summary>
  608. /// Gets the input argument.
  609. /// </summary>
  610. /// <param name="state">The state.</param>
  611. /// <returns>System.String.</returns>
  612. protected string GetInputArgument(StreamState state)
  613. {
  614. if (state.SendInputOverStandardInput)
  615. {
  616. return "-";
  617. }
  618. var type = InputType.File;
  619. var inputPath = new[] { state.MediaPath };
  620. if (state.IsInputVideo)
  621. {
  622. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  623. {
  624. inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, state.IsoMount, state.PlayableStreamFileNames, out type);
  625. }
  626. }
  627. return MediaEncoder.GetInputArgument(inputPath, type);
  628. }
  629. /// <summary>
  630. /// Starts the FFMPEG.
  631. /// </summary>
  632. /// <param name="state">The state.</param>
  633. /// <param name="outputPath">The output path.</param>
  634. /// <returns>Task.</returns>
  635. protected async Task StartFfMpeg(StreamState state, string outputPath)
  636. {
  637. if (!File.Exists(MediaEncoder.EncoderPath))
  638. {
  639. throw new InvalidOperationException("ffmpeg was not found at " + MediaEncoder.EncoderPath);
  640. }
  641. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  642. if (state.IsInputVideo && state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  643. {
  644. state.IsoMount = await IsoManager.Mount(state.MediaPath, CancellationToken.None).ConfigureAwait(false);
  645. }
  646. var commandLineArgs = GetCommandLineArguments(outputPath, state, true);
  647. if (ServerConfigurationManager.Configuration.EnableDebugEncodingLogging)
  648. {
  649. commandLineArgs = "-loglevel debug " + commandLineArgs;
  650. }
  651. var process = new Process
  652. {
  653. StartInfo = new ProcessStartInfo
  654. {
  655. CreateNoWindow = true,
  656. UseShellExecute = false,
  657. // Must consume both stdout and stderr or deadlocks may occur
  658. RedirectStandardOutput = true,
  659. RedirectStandardError = true,
  660. FileName = MediaEncoder.EncoderPath,
  661. WorkingDirectory = Path.GetDirectoryName(MediaEncoder.EncoderPath),
  662. Arguments = commandLineArgs,
  663. WindowStyle = ProcessWindowStyle.Hidden,
  664. ErrorDialog = false,
  665. RedirectStandardInput = state.SendInputOverStandardInput
  666. },
  667. EnableRaisingEvents = true
  668. };
  669. ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, state.IsInputVideo, state.Request.StartTimeTicks, state.MediaPath, state.Request.DeviceId);
  670. Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
  671. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");
  672. Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
  673. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  674. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);
  675. process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);
  676. try
  677. {
  678. process.Start();
  679. }
  680. catch (Exception ex)
  681. {
  682. Logger.ErrorException("Error starting ffmpeg", ex);
  683. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
  684. state.LogFileStream.Dispose();
  685. throw;
  686. }
  687. if (state.SendInputOverStandardInput)
  688. {
  689. StreamToStandardInput(process, state);
  690. }
  691. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  692. process.BeginOutputReadLine();
  693. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  694. process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);
  695. // Wait for the file to exist before proceeeding
  696. while (!File.Exists(outputPath))
  697. {
  698. await Task.Delay(100).ConfigureAwait(false);
  699. }
  700. // Allow a small amount of time to buffer a little
  701. if (state.IsInputVideo)
  702. {
  703. await Task.Delay(500).ConfigureAwait(false);
  704. }
  705. // This is arbitrary, but add a little buffer time when internet streaming
  706. if (state.IsRemote)
  707. {
  708. await Task.Delay(3000).ConfigureAwait(false);
  709. }
  710. }
  711. private async void StreamToStandardInput(Process process, StreamState state)
  712. {
  713. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  714. try
  715. {
  716. await StreamToStandardInputInternal(process, state).ConfigureAwait(false);
  717. }
  718. catch (OperationCanceledException)
  719. {
  720. Logger.Debug("Stream to standard input closed normally.");
  721. }
  722. catch (Exception ex)
  723. {
  724. Logger.ErrorException("Error writing to standard input", ex);
  725. }
  726. }
  727. private async Task StreamToStandardInputInternal(Process process, StreamState state)
  728. {
  729. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  730. using (var fileStream = FileSystem.GetFileStream(state.MediaPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  731. {
  732. await new EndlessStreamCopy().CopyStream(fileStream, process.StandardInput.BaseStream, state.StandardInputCancellationTokenSource.Token).ConfigureAwait(false);
  733. }
  734. }
  735. protected int? GetVideoBitrateParam(StreamState state)
  736. {
  737. return state.VideoRequest.VideoBitRate;
  738. }
  739. protected int? GetAudioBitrateParam(StreamState state)
  740. {
  741. if (state.Request.AudioBitRate.HasValue)
  742. {
  743. // Make sure we don't request a bitrate higher than the source
  744. var currentBitrate = state.AudioStream == null ? state.Request.AudioBitRate.Value : state.AudioStream.BitRate ?? state.Request.AudioBitRate.Value;
  745. return Math.Min(currentBitrate, state.Request.AudioBitRate.Value);
  746. }
  747. return null;
  748. }
  749. /// <summary>
  750. /// Gets the user agent param.
  751. /// </summary>
  752. /// <param name="path">The path.</param>
  753. /// <returns>System.String.</returns>
  754. protected string GetUserAgentParam(string path)
  755. {
  756. var useragent = GetUserAgent(path);
  757. if (!string.IsNullOrEmpty(useragent))
  758. {
  759. return "-user-agent \"" + useragent + "\"";
  760. }
  761. return string.Empty;
  762. }
  763. /// <summary>
  764. /// Gets the user agent.
  765. /// </summary>
  766. /// <param name="path">The path.</param>
  767. /// <returns>System.String.</returns>
  768. protected string GetUserAgent(string path)
  769. {
  770. if (string.IsNullOrEmpty(path))
  771. {
  772. throw new ArgumentNullException("path");
  773. }
  774. if (path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1)
  775. {
  776. return "QuickTime/7.7.4";
  777. }
  778. return string.Empty;
  779. }
  780. /// <summary>
  781. /// Processes the exited.
  782. /// </summary>
  783. /// <param name="process">The process.</param>
  784. /// <param name="state">The state.</param>
  785. protected async void OnFfMpegProcessExited(Process process, StreamState state)
  786. {
  787. if (state.IsoMount != null)
  788. {
  789. state.IsoMount.Dispose();
  790. state.IsoMount = null;
  791. }
  792. if (state.StandardInputCancellationTokenSource != null)
  793. {
  794. state.StandardInputCancellationTokenSource.Cancel();
  795. }
  796. var outputFilePath = GetOutputFilePath(state);
  797. state.LogFileStream.Dispose();
  798. try
  799. {
  800. Logger.Info("FFMpeg exited with code {0} for {1}", process.ExitCode, outputFilePath);
  801. }
  802. catch
  803. {
  804. Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
  805. }
  806. if (!string.IsNullOrEmpty(state.LiveTvStreamId))
  807. {
  808. try
  809. {
  810. await LiveTvManager.CloseLiveStream(state.LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  811. }
  812. catch (Exception ex)
  813. {
  814. Logger.ErrorException("Error closing live tv stream", ex);
  815. }
  816. }
  817. }
  818. /// <summary>
  819. /// Gets the state.
  820. /// </summary>
  821. /// <param name="request">The request.</param>
  822. /// <param name="cancellationToken">The cancellation token.</param>
  823. /// <returns>StreamState.</returns>
  824. protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
  825. {
  826. var url = Request.PathInfo;
  827. if (!request.AudioCodec.HasValue)
  828. {
  829. request.AudioCodec = InferAudioCodec(url);
  830. }
  831. var state = new StreamState
  832. {
  833. Request = request,
  834. RequestedUrl = url
  835. };
  836. var item = DtoService.GetItemByDtoId(request.Id);
  837. if (item is ILiveTvRecording)
  838. {
  839. var recording = await LiveTvManager.GetInternalRecording(request.Id, cancellationToken).ConfigureAwait(false);
  840. state.VideoType = VideoType.VideoFile;
  841. state.IsInputVideo = string.Equals(recording.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  842. state.PlayableStreamFileNames = new List<string>();
  843. if (!string.IsNullOrEmpty(recording.RecordingInfo.Path) && File.Exists(recording.RecordingInfo.Path))
  844. {
  845. state.MediaPath = recording.RecordingInfo.Path;
  846. state.IsRemote = false;
  847. }
  848. else if (!string.IsNullOrEmpty(recording.RecordingInfo.Url))
  849. {
  850. state.MediaPath = recording.RecordingInfo.Url;
  851. state.IsRemote = true;
  852. }
  853. else
  854. {
  855. var streamInfo = await LiveTvManager.GetRecordingStream(request.Id, cancellationToken).ConfigureAwait(false);
  856. state.LiveTvStreamId = streamInfo.Id;
  857. if (!string.IsNullOrEmpty(streamInfo.Path) && File.Exists(streamInfo.Path))
  858. {
  859. state.MediaPath = streamInfo.Path;
  860. state.IsRemote = false;
  861. }
  862. else if (!string.IsNullOrEmpty(streamInfo.Url))
  863. {
  864. state.MediaPath = streamInfo.Url;
  865. state.IsRemote = true;
  866. }
  867. }
  868. //state.RunTimeTicks = recording.RunTimeTicks;
  869. state.SendInputOverStandardInput = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  870. }
  871. else if (item is LiveTvChannel)
  872. {
  873. var channel = LiveTvManager.GetInternalChannel(request.Id);
  874. state.VideoType = VideoType.VideoFile;
  875. state.IsInputVideo = string.Equals(channel.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  876. state.PlayableStreamFileNames = new List<string>();
  877. var streamInfo = await LiveTvManager.GetChannelStream(request.Id, cancellationToken).ConfigureAwait(false);
  878. state.LiveTvStreamId = streamInfo.Id;
  879. if (!string.IsNullOrEmpty(streamInfo.Path) && File.Exists(streamInfo.Path))
  880. {
  881. state.MediaPath = streamInfo.Path;
  882. state.IsRemote = false;
  883. }
  884. else if (!string.IsNullOrEmpty(streamInfo.Url))
  885. {
  886. state.MediaPath = streamInfo.Url;
  887. state.IsRemote = true;
  888. }
  889. state.SendInputOverStandardInput = true;
  890. }
  891. else
  892. {
  893. state.MediaPath = item.Path;
  894. state.IsRemote = item.LocationType == LocationType.Remote;
  895. var video = item as Video;
  896. if (video != null)
  897. {
  898. state.IsInputVideo = true;
  899. state.VideoType = video.VideoType;
  900. state.IsoType = video.IsoType;
  901. state.PlayableStreamFileNames = video.PlayableStreamFileNames == null
  902. ? new List<string>()
  903. : video.PlayableStreamFileNames.ToList();
  904. }
  905. state.RunTimeTicks = item.RunTimeTicks;
  906. }
  907. var videoRequest = request as VideoStreamRequest;
  908. var mediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery
  909. {
  910. ItemId = item.Id
  911. }).ToList();
  912. if (videoRequest != null)
  913. {
  914. if (!videoRequest.VideoCodec.HasValue)
  915. {
  916. videoRequest.VideoCodec = InferVideoCodec(url);
  917. }
  918. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  919. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  920. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  921. }
  922. else
  923. {
  924. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  925. }
  926. state.HasMediaStreams = mediaStreams.Count > 0;
  927. return state;
  928. }
  929. /// <summary>
  930. /// Infers the audio codec based on the url
  931. /// </summary>
  932. /// <param name="url">The URL.</param>
  933. /// <returns>System.Nullable{AudioCodecs}.</returns>
  934. private AudioCodecs? InferAudioCodec(string url)
  935. {
  936. var ext = Path.GetExtension(url);
  937. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  938. {
  939. return AudioCodecs.Mp3;
  940. }
  941. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  942. {
  943. return AudioCodecs.Aac;
  944. }
  945. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  946. {
  947. return AudioCodecs.Wma;
  948. }
  949. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  950. {
  951. return AudioCodecs.Vorbis;
  952. }
  953. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  954. {
  955. return AudioCodecs.Vorbis;
  956. }
  957. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  958. {
  959. return AudioCodecs.Vorbis;
  960. }
  961. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  962. {
  963. return AudioCodecs.Vorbis;
  964. }
  965. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  966. {
  967. return AudioCodecs.Vorbis;
  968. }
  969. return null;
  970. }
  971. /// <summary>
  972. /// Infers the video codec.
  973. /// </summary>
  974. /// <param name="url">The URL.</param>
  975. /// <returns>System.Nullable{VideoCodecs}.</returns>
  976. private VideoCodecs? InferVideoCodec(string url)
  977. {
  978. var ext = Path.GetExtension(url);
  979. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  980. {
  981. return VideoCodecs.Wmv;
  982. }
  983. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  984. {
  985. return VideoCodecs.Vpx;
  986. }
  987. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  988. {
  989. return VideoCodecs.Theora;
  990. }
  991. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  992. {
  993. return VideoCodecs.H264;
  994. }
  995. return VideoCodecs.Copy;
  996. }
  997. }
  998. }