BaseStreamingService.cs 45 KB

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