BaseStreamingService.cs 42 KB

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