BaseStreamingService.cs 35 KB

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