BaseStreamingService.cs 38 KB

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