BaseStreamingService.cs 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Dto;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using MediaBrowser.Controller.Persistence;
  10. using MediaBrowser.Model.Configuration;
  11. using MediaBrowser.Model.Drawing;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.LiveTv;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. namespace MediaBrowser.Api.Playback
  25. {
  26. /// <summary>
  27. /// Class BaseStreamingService
  28. /// </summary>
  29. public abstract class BaseStreamingService : BaseApiService
  30. {
  31. /// <summary>
  32. /// Gets or sets the application paths.
  33. /// </summary>
  34. /// <value>The application paths.</value>
  35. protected IServerConfigurationManager ServerConfigurationManager { get; private set; }
  36. /// <summary>
  37. /// Gets or sets the user manager.
  38. /// </summary>
  39. /// <value>The user manager.</value>
  40. protected IUserManager UserManager { get; private set; }
  41. /// <summary>
  42. /// Gets or sets the library manager.
  43. /// </summary>
  44. /// <value>The library manager.</value>
  45. protected ILibraryManager LibraryManager { get; private set; }
  46. /// <summary>
  47. /// Gets or sets the iso manager.
  48. /// </summary>
  49. /// <value>The iso manager.</value>
  50. protected IIsoManager IsoManager { get; private set; }
  51. /// <summary>
  52. /// Gets or sets the media encoder.
  53. /// </summary>
  54. /// <value>The media encoder.</value>
  55. protected IMediaEncoder MediaEncoder { get; private set; }
  56. protected IEncodingManager EncodingManager { 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, IEncodingManager encodingManager)
  73. {
  74. EncodingManager = encodingManager;
  75. LiveTvManager = liveTvManager;
  76. ItemRepository = itemRepository;
  77. FileSystem = fileSystem;
  78. DtoService = dtoService;
  79. ServerConfigurationManager = serverConfig;
  80. UserManager = userManager;
  81. LibraryManager = libraryManager;
  82. IsoManager = isoManager;
  83. MediaEncoder = mediaEncoder;
  84. }
  85. /// <summary>
  86. /// Gets the command line arguments.
  87. /// </summary>
  88. /// <param name="outputPath">The output path.</param>
  89. /// <param name="state">The state.</param>
  90. /// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
  91. /// <returns>System.String.</returns>
  92. protected abstract string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions);
  93. /// <summary>
  94. /// Gets the type of the transcoding job.
  95. /// </summary>
  96. /// <value>The type of the transcoding job.</value>
  97. protected abstract TranscodingJobType TranscodingJobType { get; }
  98. /// <summary>
  99. /// Gets the output file extension.
  100. /// </summary>
  101. /// <param name="state">The state.</param>
  102. /// <returns>System.String.</returns>
  103. protected virtual string GetOutputFileExtension(StreamState state)
  104. {
  105. return Path.GetExtension(state.RequestedUrl);
  106. }
  107. /// <summary>
  108. /// Gets the output file path.
  109. /// </summary>
  110. /// <param name="state">The state.</param>
  111. /// <returns>System.String.</returns>
  112. protected virtual string GetOutputFilePath(StreamState state)
  113. {
  114. var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath;
  115. var outputFileExtension = GetOutputFileExtension(state);
  116. return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
  117. }
  118. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  119. /// <summary>
  120. /// The fast seek offset seconds
  121. /// </summary>
  122. private const int FastSeekOffsetSeconds = 1;
  123. /// <summary>
  124. /// Gets the fast seek command line parameter.
  125. /// </summary>
  126. /// <param name="request">The request.</param>
  127. /// <returns>System.String.</returns>
  128. /// <value>The fast seek command line parameter.</value>
  129. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  130. {
  131. var time = request.StartTimeTicks;
  132. if (time.HasValue)
  133. {
  134. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds;
  135. if (seconds > 0)
  136. {
  137. return string.Format("-ss {0}", seconds.ToString(UsCulture));
  138. }
  139. }
  140. return string.Empty;
  141. }
  142. /// <summary>
  143. /// Gets the slow seek command line parameter.
  144. /// </summary>
  145. /// <param name="request">The request.</param>
  146. /// <returns>System.String.</returns>
  147. /// <value>The slow seek command line parameter.</value>
  148. protected string GetSlowSeekCommandLineParameter(StreamRequest request)
  149. {
  150. var time = request.StartTimeTicks;
  151. if (time.HasValue)
  152. {
  153. if (TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds > 0)
  154. {
  155. return string.Format(" -ss {0}", FastSeekOffsetSeconds.ToString(UsCulture));
  156. }
  157. }
  158. return string.Empty;
  159. }
  160. /// <summary>
  161. /// Gets the map args.
  162. /// </summary>
  163. /// <param name="state">The state.</param>
  164. /// <returns>System.String.</returns>
  165. protected virtual string GetMapArgs(StreamState state)
  166. {
  167. var args = string.Empty;
  168. if (state.IsRemote || !state.HasMediaStreams)
  169. {
  170. return string.Empty;
  171. }
  172. if (state.VideoStream != null)
  173. {
  174. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  175. }
  176. else
  177. {
  178. args += "-map -0:v";
  179. }
  180. if (state.AudioStream != null)
  181. {
  182. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  183. }
  184. else
  185. {
  186. args += " -map -0:a";
  187. }
  188. if (state.SubtitleStream == null)
  189. {
  190. args += " -map -0:s";
  191. }
  192. return args;
  193. }
  194. /// <summary>
  195. /// Determines which stream will be used for playback
  196. /// </summary>
  197. /// <param name="allStream">All stream.</param>
  198. /// <param name="desiredIndex">Index of the desired.</param>
  199. /// <param name="type">The type.</param>
  200. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  201. /// <returns>MediaStream.</returns>
  202. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  203. {
  204. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  205. if (desiredIndex.HasValue)
  206. {
  207. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  208. if (stream != null)
  209. {
  210. return stream;
  211. }
  212. }
  213. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  214. {
  215. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  216. streams.FirstOrDefault();
  217. }
  218. // Just return the first one
  219. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  220. }
  221. protected EncodingQuality GetQualitySetting()
  222. {
  223. var quality = ServerConfigurationManager.Configuration.MediaEncodingQuality;
  224. if (quality == EncodingQuality.Auto)
  225. {
  226. var cpuCount = Environment.ProcessorCount;
  227. if (cpuCount >= 4)
  228. {
  229. return EncodingQuality.HighQuality;
  230. }
  231. return EncodingQuality.HighSpeed;
  232. }
  233. return quality;
  234. }
  235. /// <summary>
  236. /// Gets the number of threads.
  237. /// </summary>
  238. /// <returns>System.Int32.</returns>
  239. /// <exception cref="System.Exception">Unrecognized MediaEncodingQuality value.</exception>
  240. protected int GetNumberOfThreads(bool isWebm)
  241. {
  242. // Webm: http://www.webmproject.org/docs/encoder-parameters/
  243. // 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
  244. // for the coefficient data if the encoder selected --token-parts > 0 at encode time.
  245. switch (GetQualitySetting())
  246. {
  247. case EncodingQuality.HighSpeed:
  248. return 2;
  249. case EncodingQuality.HighQuality:
  250. return 2;
  251. case EncodingQuality.MaxQuality:
  252. return isWebm ? 2 : 0;
  253. default:
  254. throw new Exception("Unrecognized MediaEncodingQuality value.");
  255. }
  256. }
  257. /// <summary>
  258. /// Gets the video bitrate to specify on the command line
  259. /// </summary>
  260. /// <param name="state">The state.</param>
  261. /// <param name="videoCodec">The video codec.</param>
  262. /// <returns>System.String.</returns>
  263. protected string GetVideoQualityParam(StreamState state, string videoCodec, bool isHls)
  264. {
  265. var param = string.Empty;
  266. var hasFixedResolution = state.VideoRequest.HasFixedResolution;
  267. var qualitySetting = GetQualitySetting();
  268. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  269. {
  270. switch (qualitySetting)
  271. {
  272. case EncodingQuality.HighSpeed:
  273. param = "-preset ultrafast";
  274. break;
  275. case EncodingQuality.HighQuality:
  276. param = "-preset superfast";
  277. break;
  278. case EncodingQuality.MaxQuality:
  279. param = "-preset superfast";
  280. break;
  281. }
  282. if (!isHls)
  283. {
  284. switch (qualitySetting)
  285. {
  286. case EncodingQuality.HighSpeed:
  287. param += " -crf 23";
  288. break;
  289. case EncodingQuality.HighQuality:
  290. param += " -crf 20";
  291. break;
  292. case EncodingQuality.MaxQuality:
  293. param += " -crf 18";
  294. break;
  295. }
  296. }
  297. }
  298. // webm
  299. else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  300. {
  301. // http://www.webmproject.org/docs/encoder-parameters/
  302. param = "-speed 16 -quality good -profile:v 0 -slices 8";
  303. if (!hasFixedResolution)
  304. {
  305. switch (qualitySetting)
  306. {
  307. case EncodingQuality.HighSpeed:
  308. param += " -crf 18";
  309. break;
  310. case EncodingQuality.HighQuality:
  311. param += " -crf 10";
  312. break;
  313. case EncodingQuality.MaxQuality:
  314. param += " -crf 4";
  315. break;
  316. }
  317. }
  318. }
  319. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  320. {
  321. param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  322. }
  323. // asf/wmv
  324. else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase))
  325. {
  326. param = "-qmin 2";
  327. }
  328. else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  329. {
  330. param = "-mbd 2";
  331. }
  332. param += GetVideoBitrateParam(state, videoCodec, isHls);
  333. var framerate = GetFramerateParam(state);
  334. if (framerate.HasValue)
  335. {
  336. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  337. }
  338. if (!string.IsNullOrEmpty(state.VideoSync))
  339. {
  340. param += " -vsync " + state.VideoSync;
  341. }
  342. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  343. {
  344. param += " -profile:v " + state.VideoRequest.Profile;
  345. }
  346. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  347. {
  348. param += " -level " + state.VideoRequest.Level;
  349. }
  350. return param;
  351. }
  352. protected string GetAudioFilterParam(StreamState state, bool isHls)
  353. {
  354. var volParam = string.Empty;
  355. var audioSampleRate = string.Empty;
  356. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  357. // Boost volume to 200% when downsampling from 6ch to 2ch
  358. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  359. {
  360. volParam = ",volume=2.000000";
  361. }
  362. if (state.Request.AudioSampleRate.HasValue)
  363. {
  364. audioSampleRate = state.Request.AudioSampleRate.Value + ":";
  365. }
  366. var adelay = isHls ? "adelay=1," : string.Empty;
  367. var pts = string.Empty;
  368. if (state.SubtitleStream != null)
  369. {
  370. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  371. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  372. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  373. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  374. {
  375. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  376. pts = string.Format(",asetpts=PTS-{0}/TB",
  377. Math.Round(seconds).ToString(UsCulture));
  378. }
  379. }
  380. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  381. adelay,
  382. audioSampleRate,
  383. volParam,
  384. pts,
  385. state.AudioSync);
  386. }
  387. /// <summary>
  388. /// If we're going to put a fixed size on the command line, this will calculate it
  389. /// </summary>
  390. /// <param name="state">The state.</param>
  391. /// <param name="outputVideoCodec">The output video codec.</param>
  392. /// <param name="performTextSubtitleConversion">if set to <c>true</c> [perform text subtitle conversion].</param>
  393. /// <returns>System.String.</returns>
  394. protected string GetOutputSizeParam(StreamState state, string outputVideoCodec, bool performTextSubtitleConversion)
  395. {
  396. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  397. var assSubtitleParam = string.Empty;
  398. var copyTsParam = string.Empty;
  399. var yadifParam = state.DeInterlace ? "yadif=0:-1:0," : string.Empty;
  400. var request = state.VideoRequest;
  401. if (state.SubtitleStream != null)
  402. {
  403. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  404. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  405. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  406. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  407. {
  408. assSubtitleParam = GetTextSubtitleParam(state, performTextSubtitleConversion);
  409. copyTsParam = " -copyts";
  410. }
  411. }
  412. // If fixed dimensions were supplied
  413. if (request.Width.HasValue && request.Height.HasValue)
  414. {
  415. var widthParam = request.Width.Value.ToString(UsCulture);
  416. var heightParam = request.Height.Value.ToString(UsCulture);
  417. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  418. }
  419. var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
  420. // If a fixed width was requested
  421. if (request.Width.HasValue)
  422. {
  423. var widthParam = request.Width.Value.ToString(UsCulture);
  424. return isH264Output ?
  425. string.Format("{3} -vf \"{0}scale={1}:trunc(ow/a/2)*2{2}\"", yadifParam, widthParam, assSubtitleParam, copyTsParam) :
  426. string.Format("{3} -vf \"{0}scale={1}:-1{2}\"", yadifParam, widthParam, assSubtitleParam, copyTsParam);
  427. }
  428. // If a fixed height was requested
  429. if (request.Height.HasValue)
  430. {
  431. var heightParam = request.Height.Value.ToString(UsCulture);
  432. return isH264Output ?
  433. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:{1}{2}\"", yadifParam, heightParam, assSubtitleParam, copyTsParam) :
  434. string.Format("{3} -vf \"{0}scale=-1:{1}{2}\"", yadifParam, heightParam, assSubtitleParam, copyTsParam);
  435. }
  436. // If a max width was requested
  437. if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null))
  438. {
  439. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  440. return isH264Output ?
  441. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):trunc(ow/a/2)*2{2}\"", yadifParam, maxWidthParam, assSubtitleParam, copyTsParam) :
  442. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):-1{2}\"", yadifParam, maxWidthParam, assSubtitleParam, copyTsParam);
  443. }
  444. // If a max height was requested
  445. if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null))
  446. {
  447. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  448. return isH264Output ?
  449. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:min(ih\\,{1}){2}\"", yadifParam, maxHeightParam, assSubtitleParam, copyTsParam) :
  450. string.Format("{3} -vf \"{0}scale=-1:min(ih\\,{1}){2}\"", yadifParam, maxHeightParam, assSubtitleParam, copyTsParam);
  451. }
  452. if (state.VideoStream == null)
  453. {
  454. // No way to figure this out
  455. return string.Empty;
  456. }
  457. // Need to perform calculations manually
  458. // Try to account for bad media info
  459. var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0;
  460. var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0;
  461. var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight);
  462. // If we're encoding with libx264, it can't handle odd numbered widths or heights, so we'll have to fix that
  463. if (isH264Output)
  464. {
  465. var widthParam = outputSize.Width.ToString(UsCulture);
  466. var heightParam = outputSize.Height.ToString(UsCulture);
  467. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  468. }
  469. // Otherwise use -vf scale since ffmpeg will ensure internally that the aspect ratio is preserved
  470. return string.Format("{3} -vf \"{0}scale={1}:-1{2}\"", yadifParam, Convert.ToInt32(outputSize.Width), assSubtitleParam, copyTsParam);
  471. }
  472. /// <summary>
  473. /// Gets the text subtitle param.
  474. /// </summary>
  475. /// <param name="state">The state.</param>
  476. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  477. /// <returns>System.String.</returns>
  478. protected string GetTextSubtitleParam(StreamState state, bool performConversion)
  479. {
  480. var path = state.SubtitleStream.IsExternal ? GetConvertedAssPath(state.MediaPath, state.SubtitleStream, performConversion) :
  481. GetExtractedAssPath(state, performConversion);
  482. if (string.IsNullOrEmpty(path))
  483. {
  484. return string.Empty;
  485. }
  486. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  487. return string.Format(",ass='{0}',setpts=PTS -{1}/TB",
  488. path.Replace('\\', '/').Replace(":/", "\\:/"),
  489. Math.Round(seconds).ToString(UsCulture));
  490. }
  491. /// <summary>
  492. /// Gets the extracted ass path.
  493. /// </summary>
  494. /// <param name="state">The state.</param>
  495. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  496. /// <returns>System.String.</returns>
  497. private string GetExtractedAssPath(StreamState state, bool performConversion)
  498. {
  499. var path = EncodingManager.GetSubtitleCachePath(state.MediaPath, state.SubtitleStream.Index, ".ass");
  500. if (performConversion)
  501. {
  502. InputType type;
  503. var inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, null, state.PlayableStreamFileNames, out type);
  504. try
  505. {
  506. var parentPath = Path.GetDirectoryName(path);
  507. Directory.CreateDirectory(parentPath);
  508. // Don't re-encode ass/ssa to ass because ffmpeg ass encoder fails if there's more than one ass rectangle. Affect Anime mostly.
  509. // See https://lists.ffmpeg.org/pipermail/ffmpeg-cvslog/2013-April/063616.html
  510. bool isAssSubtitle = string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase);
  511. var task = MediaEncoder.ExtractTextSubtitle(inputPath, type, state.SubtitleStream.Index, isAssSubtitle, path, CancellationToken.None);
  512. Task.WaitAll(task);
  513. }
  514. catch
  515. {
  516. return null;
  517. }
  518. }
  519. return path;
  520. }
  521. /// <summary>
  522. /// Gets the converted ass path.
  523. /// </summary>
  524. /// <param name="mediaPath">The media path.</param>
  525. /// <param name="subtitleStream">The subtitle stream.</param>
  526. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  527. /// <returns>System.String.</returns>
  528. private string GetConvertedAssPath(string mediaPath, MediaStream subtitleStream, bool performConversion)
  529. {
  530. var path = EncodingManager.GetSubtitleCachePath(subtitleStream.Path, ".ass");
  531. if (performConversion)
  532. {
  533. try
  534. {
  535. var parentPath = Path.GetDirectoryName(path);
  536. Directory.CreateDirectory(parentPath);
  537. var task = MediaEncoder.ConvertTextSubtitleToAss(subtitleStream.Path, path, subtitleStream.Language, CancellationToken.None);
  538. Task.WaitAll(task);
  539. }
  540. catch
  541. {
  542. return null;
  543. }
  544. }
  545. return path;
  546. }
  547. /// <summary>
  548. /// Gets the internal graphical subtitle param.
  549. /// </summary>
  550. /// <param name="state">The state.</param>
  551. /// <param name="outputVideoCodec">The output video codec.</param>
  552. /// <returns>System.String.</returns>
  553. protected string GetInternalGraphicalSubtitleParam(StreamState state, string outputVideoCodec)
  554. {
  555. var outputSizeParam = string.Empty;
  556. var request = state.VideoRequest;
  557. // Add resolution params, if specified
  558. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  559. {
  560. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec, false).TrimEnd('"');
  561. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  562. }
  563. var videoSizeParam = string.Empty;
  564. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  565. {
  566. videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  567. }
  568. return string.Format(" -filter_complex \"[0:{0}]format=yuva444p{3},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{1}] [sub] overlay{2}\"",
  569. state.SubtitleStream.Index,
  570. state.VideoStream.Index,
  571. outputSizeParam,
  572. videoSizeParam);
  573. }
  574. /// <summary>
  575. /// Gets the probe size argument.
  576. /// </summary>
  577. /// <param name="mediaPath">The media path.</param>
  578. /// <param name="isVideo">if set to <c>true</c> [is video].</param>
  579. /// <param name="videoType">Type of the video.</param>
  580. /// <param name="isoType">Type of the iso.</param>
  581. /// <returns>System.String.</returns>
  582. private string GetProbeSizeArgument(string mediaPath, bool isVideo, VideoType? videoType, IsoType? isoType)
  583. {
  584. var type = !isVideo ? MediaEncoderHelpers.GetInputType(null, null) :
  585. MediaEncoderHelpers.GetInputType(videoType, isoType);
  586. return MediaEncoder.GetProbeSizeArgument(type);
  587. }
  588. /// <summary>
  589. /// Gets the number of audio channels to specify on the command line
  590. /// </summary>
  591. /// <param name="request">The request.</param>
  592. /// <param name="audioStream">The audio stream.</param>
  593. /// <returns>System.Nullable{System.Int32}.</returns>
  594. protected int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream)
  595. {
  596. if (audioStream != null)
  597. {
  598. if (audioStream.Channels > 2 && request.AudioCodec.HasValue)
  599. {
  600. if (request.AudioCodec.Value == AudioCodecs.Wma)
  601. {
  602. // wmav2 currently only supports two channel output
  603. return 2;
  604. }
  605. }
  606. }
  607. return request.AudioChannels;
  608. }
  609. /// <summary>
  610. /// Determines whether the specified stream is H264.
  611. /// </summary>
  612. /// <param name="stream">The stream.</param>
  613. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  614. protected bool IsH264(MediaStream stream)
  615. {
  616. return stream.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  617. stream.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  618. }
  619. /// <summary>
  620. /// Gets the name of the output audio codec
  621. /// </summary>
  622. /// <param name="request">The request.</param>
  623. /// <returns>System.String.</returns>
  624. protected string GetAudioCodec(StreamRequest request)
  625. {
  626. var codec = request.AudioCodec;
  627. if (codec.HasValue)
  628. {
  629. if (codec == AudioCodecs.Aac)
  630. {
  631. return "aac -strict experimental";
  632. }
  633. if (codec == AudioCodecs.Mp3)
  634. {
  635. return "libmp3lame";
  636. }
  637. if (codec == AudioCodecs.Vorbis)
  638. {
  639. return "libvorbis";
  640. }
  641. if (codec == AudioCodecs.Wma)
  642. {
  643. return "wmav2";
  644. }
  645. return codec.ToString().ToLower();
  646. }
  647. return "copy";
  648. }
  649. /// <summary>
  650. /// Gets the name of the output video codec
  651. /// </summary>
  652. /// <param name="request">The request.</param>
  653. /// <returns>System.String.</returns>
  654. protected string GetVideoCodec(VideoStreamRequest request)
  655. {
  656. var codec = request.VideoCodec;
  657. if (codec.HasValue)
  658. {
  659. if (codec == VideoCodecs.H264)
  660. {
  661. return "libx264";
  662. }
  663. if (codec == VideoCodecs.Vpx)
  664. {
  665. return "libvpx";
  666. }
  667. if (codec == VideoCodecs.Wmv)
  668. {
  669. return "msmpeg4";
  670. }
  671. if (codec == VideoCodecs.Theora)
  672. {
  673. return "libtheora";
  674. }
  675. return codec.ToString().ToLower();
  676. }
  677. return "copy";
  678. }
  679. /// <summary>
  680. /// Gets the input argument.
  681. /// </summary>
  682. /// <param name="state">The state.</param>
  683. /// <returns>System.String.</returns>
  684. protected string GetInputArgument(StreamState state)
  685. {
  686. if (state.SendInputOverStandardInput)
  687. {
  688. return "-";
  689. }
  690. var type = InputType.File;
  691. var inputPath = new[] { state.MediaPath };
  692. if (state.IsInputVideo)
  693. {
  694. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  695. {
  696. inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, state.IsoMount, state.PlayableStreamFileNames, out type);
  697. }
  698. }
  699. return MediaEncoder.GetInputArgument(inputPath, type);
  700. }
  701. /// <summary>
  702. /// Starts the FFMPEG.
  703. /// </summary>
  704. /// <param name="state">The state.</param>
  705. /// <param name="outputPath">The output path.</param>
  706. /// <returns>Task.</returns>
  707. protected async Task StartFfMpeg(StreamState state, string outputPath)
  708. {
  709. if (!File.Exists(MediaEncoder.EncoderPath))
  710. {
  711. throw new InvalidOperationException("ffmpeg was not found at " + MediaEncoder.EncoderPath);
  712. }
  713. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  714. if (state.IsInputVideo && state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  715. {
  716. state.IsoMount = await IsoManager.Mount(state.MediaPath, CancellationToken.None).ConfigureAwait(false);
  717. }
  718. var commandLineArgs = GetCommandLineArguments(outputPath, state, true);
  719. if (ServerConfigurationManager.Configuration.EnableDebugEncodingLogging)
  720. {
  721. commandLineArgs = "-loglevel debug " + commandLineArgs;
  722. }
  723. var process = new Process
  724. {
  725. StartInfo = new ProcessStartInfo
  726. {
  727. CreateNoWindow = true,
  728. UseShellExecute = false,
  729. // Must consume both stdout and stderr or deadlocks may occur
  730. RedirectStandardOutput = true,
  731. RedirectStandardError = true,
  732. FileName = MediaEncoder.EncoderPath,
  733. WorkingDirectory = Path.GetDirectoryName(MediaEncoder.EncoderPath),
  734. Arguments = commandLineArgs,
  735. WindowStyle = ProcessWindowStyle.Hidden,
  736. ErrorDialog = false,
  737. RedirectStandardInput = state.SendInputOverStandardInput
  738. },
  739. EnableRaisingEvents = true
  740. };
  741. ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, state.IsInputVideo, state.Request.StartTimeTicks, state.MediaPath, state.Request.DeviceId);
  742. Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
  743. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");
  744. Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
  745. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  746. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);
  747. process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);
  748. try
  749. {
  750. process.Start();
  751. }
  752. catch (Exception ex)
  753. {
  754. Logger.ErrorException("Error starting ffmpeg", ex);
  755. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
  756. state.LogFileStream.Dispose();
  757. throw;
  758. }
  759. if (state.SendInputOverStandardInput)
  760. {
  761. StreamToStandardInput(process, state);
  762. }
  763. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  764. process.BeginOutputReadLine();
  765. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  766. process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);
  767. // Wait for the file to exist before proceeeding
  768. while (!File.Exists(outputPath))
  769. {
  770. await Task.Delay(100).ConfigureAwait(false);
  771. }
  772. // Allow a small amount of time to buffer a little
  773. if (state.IsInputVideo)
  774. {
  775. await Task.Delay(500).ConfigureAwait(false);
  776. }
  777. // This is arbitrary, but add a little buffer time when internet streaming
  778. if (state.IsRemote)
  779. {
  780. await Task.Delay(3000).ConfigureAwait(false);
  781. }
  782. }
  783. private async void StreamToStandardInput(Process process, StreamState state)
  784. {
  785. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  786. try
  787. {
  788. await StreamToStandardInputInternal(process, state).ConfigureAwait(false);
  789. }
  790. catch (OperationCanceledException)
  791. {
  792. Logger.Debug("Stream to standard input closed normally.");
  793. }
  794. catch (Exception ex)
  795. {
  796. Logger.ErrorException("Error writing to standard input", ex);
  797. }
  798. }
  799. private async Task StreamToStandardInputInternal(Process process, StreamState state)
  800. {
  801. state.StandardInputCancellationTokenSource = new CancellationTokenSource();
  802. using (var fileStream = FileSystem.GetFileStream(state.MediaPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  803. {
  804. await new EndlessStreamCopy().CopyStream(fileStream, process.StandardInput.BaseStream, state.StandardInputCancellationTokenSource.Token).ConfigureAwait(false);
  805. }
  806. }
  807. protected int? GetVideoBitrateParamValue(StreamState state)
  808. {
  809. var bitrate = state.VideoRequest.VideoBitRate;
  810. if (state.VideoStream != null)
  811. {
  812. var isUpscaling = false;
  813. if (state.VideoRequest.Height.HasValue && state.VideoStream.Height.HasValue &&
  814. state.VideoRequest.Height.Value > state.VideoStream.Height.Value)
  815. {
  816. isUpscaling = true;
  817. }
  818. if (state.VideoRequest.Width.HasValue && state.VideoStream.Width.HasValue &&
  819. state.VideoRequest.Width.Value > state.VideoStream.Width.Value)
  820. {
  821. isUpscaling = true;
  822. }
  823. // Don't allow bitrate increases unless upscaling
  824. if (!isUpscaling)
  825. {
  826. if (bitrate.HasValue && state.VideoStream.BitRate.HasValue)
  827. {
  828. bitrate = Math.Min(bitrate.Value, state.VideoStream.BitRate.Value);
  829. }
  830. }
  831. }
  832. return bitrate;
  833. }
  834. protected string GetVideoBitrateParam(StreamState state, string videoCodec, bool isHls)
  835. {
  836. var bitrate = GetVideoBitrateParamValue(state);
  837. if (bitrate.HasValue)
  838. {
  839. var hasFixedResolution = state.VideoRequest.HasFixedResolution;
  840. if (isHls)
  841. {
  842. return string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture));
  843. }
  844. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  845. {
  846. if (hasFixedResolution)
  847. {
  848. return string.Format(" -minrate:v ({0}*.90) -maxrate:v ({0}*1.10) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture));
  849. }
  850. // With vpx when crf is used, b:v becomes a max rate
  851. // https://trac.ffmpeg.org/wiki/vpxEncodingGuide
  852. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  853. //return string.Format(" -minrate:v ({0}*.95) -maxrate:v ({0}*1.05) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture));
  854. }
  855. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  856. {
  857. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  858. }
  859. // H264
  860. if (hasFixedResolution)
  861. {
  862. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  863. }
  864. return string.Format(" -maxrate {0} -bufsize {1}",
  865. bitrate.Value.ToString(UsCulture),
  866. (bitrate.Value * 2).ToString(UsCulture));
  867. }
  868. return string.Empty;
  869. }
  870. protected int? GetAudioBitrateParam(StreamState state)
  871. {
  872. if (state.Request.AudioBitRate.HasValue)
  873. {
  874. // Make sure we don't request a bitrate higher than the source
  875. var currentBitrate = state.AudioStream == null ? state.Request.AudioBitRate.Value : state.AudioStream.BitRate ?? state.Request.AudioBitRate.Value;
  876. return Math.Min(currentBitrate, state.Request.AudioBitRate.Value);
  877. }
  878. return null;
  879. }
  880. /// <summary>
  881. /// Gets the user agent param.
  882. /// </summary>
  883. /// <param name="path">The path.</param>
  884. /// <returns>System.String.</returns>
  885. private string GetUserAgentParam(string path)
  886. {
  887. var useragent = GetUserAgent(path);
  888. if (!string.IsNullOrEmpty(useragent))
  889. {
  890. return "-user-agent \"" + useragent + "\"";
  891. }
  892. return string.Empty;
  893. }
  894. /// <summary>
  895. /// Gets the user agent.
  896. /// </summary>
  897. /// <param name="path">The path.</param>
  898. /// <returns>System.String.</returns>
  899. protected string GetUserAgent(string path)
  900. {
  901. if (string.IsNullOrEmpty(path))
  902. {
  903. throw new ArgumentNullException("path");
  904. }
  905. if (path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1)
  906. {
  907. return "QuickTime/7.7.4";
  908. }
  909. return string.Empty;
  910. }
  911. /// <summary>
  912. /// Processes the exited.
  913. /// </summary>
  914. /// <param name="process">The process.</param>
  915. /// <param name="state">The state.</param>
  916. protected async void OnFfMpegProcessExited(Process process, StreamState state)
  917. {
  918. if (state.IsoMount != null)
  919. {
  920. state.IsoMount.Dispose();
  921. state.IsoMount = null;
  922. }
  923. if (state.StandardInputCancellationTokenSource != null)
  924. {
  925. state.StandardInputCancellationTokenSource.Cancel();
  926. }
  927. var outputFilePath = GetOutputFilePath(state);
  928. state.LogFileStream.Dispose();
  929. try
  930. {
  931. Logger.Info("FFMpeg exited with code {0} for {1}", process.ExitCode, outputFilePath);
  932. }
  933. catch
  934. {
  935. Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
  936. }
  937. if (!string.IsNullOrEmpty(state.LiveTvStreamId))
  938. {
  939. try
  940. {
  941. await LiveTvManager.CloseLiveStream(state.LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  942. }
  943. catch (Exception ex)
  944. {
  945. Logger.ErrorException("Error closing live tv stream", ex);
  946. }
  947. }
  948. }
  949. protected double? GetFramerateParam(StreamState state)
  950. {
  951. if (state.VideoRequest != null && state.VideoRequest.Framerate.HasValue)
  952. {
  953. return state.VideoRequest.Framerate.Value;
  954. }
  955. if (state.VideoStream != null)
  956. {
  957. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  958. if (contentRate.HasValue && contentRate.Value > 23.976)
  959. {
  960. return 23.976;
  961. }
  962. }
  963. return null;
  964. }
  965. /// <summary>
  966. /// Parses the parameters.
  967. /// </summary>
  968. /// <param name="request">The request.</param>
  969. private void ParseParams(StreamRequest request)
  970. {
  971. var vals = request.Params.Split(';');
  972. var videoRequest = request as VideoStreamRequest;
  973. for (var i = 0; i < vals.Length; i++)
  974. {
  975. var val = vals[i];
  976. if (string.IsNullOrWhiteSpace(val))
  977. {
  978. continue;
  979. }
  980. if (i == 0)
  981. {
  982. request.DeviceId = val;
  983. }
  984. else if (i == 1)
  985. {
  986. if (videoRequest != null)
  987. {
  988. videoRequest.VideoCodec = (VideoCodecs)Enum.Parse(typeof(VideoCodecs), val, true);
  989. }
  990. }
  991. else if (i == 2)
  992. {
  993. request.AudioCodec = (AudioCodecs)Enum.Parse(typeof(AudioCodecs), val, true);
  994. }
  995. else if (i == 3)
  996. {
  997. if (videoRequest != null)
  998. {
  999. videoRequest.AudioStreamIndex = int.Parse(val, UsCulture);
  1000. }
  1001. }
  1002. else if (i == 4)
  1003. {
  1004. if (videoRequest != null)
  1005. {
  1006. videoRequest.SubtitleStreamIndex = int.Parse(val, UsCulture);
  1007. }
  1008. }
  1009. else if (i == 5)
  1010. {
  1011. if (videoRequest != null)
  1012. {
  1013. videoRequest.VideoBitRate = int.Parse(val, UsCulture);
  1014. }
  1015. }
  1016. else if (i == 6)
  1017. {
  1018. request.AudioBitRate = int.Parse(val, UsCulture);
  1019. }
  1020. else if (i == 7)
  1021. {
  1022. request.AudioChannels = int.Parse(val, UsCulture);
  1023. }
  1024. else if (i == 8)
  1025. {
  1026. if (videoRequest != null)
  1027. {
  1028. request.StartTimeTicks = long.Parse(val, UsCulture);
  1029. }
  1030. }
  1031. else if (i == 9)
  1032. {
  1033. if (videoRequest != null)
  1034. {
  1035. videoRequest.Profile = val;
  1036. }
  1037. }
  1038. else if (i == 10)
  1039. {
  1040. if (videoRequest != null)
  1041. {
  1042. videoRequest.Level = val;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. /// <summary>
  1048. /// Gets the state.
  1049. /// </summary>
  1050. /// <param name="request">The request.</param>
  1051. /// <param name="cancellationToken">The cancellation token.</param>
  1052. /// <returns>StreamState.</returns>
  1053. protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
  1054. {
  1055. if (!string.IsNullOrWhiteSpace(request.Params))
  1056. {
  1057. ParseParams(request);
  1058. }
  1059. if (request.ThrowDebugError)
  1060. {
  1061. throw new InvalidOperationException("You asked for a debug error, you got one.");
  1062. }
  1063. var user = AuthorizationRequestFilterAttribute.GetCurrentUser(Request, UserManager);
  1064. if (user != null && !user.Configuration.EnableMediaPlayback)
  1065. {
  1066. throw new ArgumentException(string.Format("{0} is not allowed to play media.", user.Name));
  1067. }
  1068. var url = Request.PathInfo;
  1069. if (!request.AudioCodec.HasValue)
  1070. {
  1071. request.AudioCodec = InferAudioCodec(url);
  1072. }
  1073. var state = new StreamState
  1074. {
  1075. Request = request,
  1076. RequestedUrl = url
  1077. };
  1078. var item = DtoService.GetItemByDtoId(request.Id);
  1079. if (item is ILiveTvRecording)
  1080. {
  1081. var recording = await LiveTvManager.GetInternalRecording(request.Id, cancellationToken).ConfigureAwait(false);
  1082. state.VideoType = VideoType.VideoFile;
  1083. state.IsInputVideo = string.Equals(recording.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1084. state.PlayableStreamFileNames = new List<string>();
  1085. if (!string.IsNullOrEmpty(recording.RecordingInfo.Path) && File.Exists(recording.RecordingInfo.Path))
  1086. {
  1087. state.MediaPath = recording.RecordingInfo.Path;
  1088. state.IsRemote = false;
  1089. }
  1090. else if (!string.IsNullOrEmpty(recording.RecordingInfo.Url))
  1091. {
  1092. state.MediaPath = recording.RecordingInfo.Url;
  1093. state.IsRemote = true;
  1094. }
  1095. else
  1096. {
  1097. var streamInfo = await LiveTvManager.GetRecordingStream(request.Id, cancellationToken).ConfigureAwait(false);
  1098. state.LiveTvStreamId = streamInfo.Id;
  1099. if (!string.IsNullOrEmpty(streamInfo.Path) && File.Exists(streamInfo.Path))
  1100. {
  1101. state.MediaPath = streamInfo.Path;
  1102. state.IsRemote = false;
  1103. }
  1104. else if (!string.IsNullOrEmpty(streamInfo.Url))
  1105. {
  1106. state.MediaPath = streamInfo.Url;
  1107. state.IsRemote = true;
  1108. }
  1109. }
  1110. //state.RunTimeTicks = recording.RunTimeTicks;
  1111. state.ReadInputAtNativeFramerate = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  1112. state.SendInputOverStandardInput = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  1113. state.AudioSync = "1000";
  1114. state.DeInterlace = true;
  1115. }
  1116. else if (item is LiveTvChannel)
  1117. {
  1118. var channel = LiveTvManager.GetInternalChannel(request.Id);
  1119. state.VideoType = VideoType.VideoFile;
  1120. state.IsInputVideo = string.Equals(channel.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1121. state.PlayableStreamFileNames = new List<string>();
  1122. var streamInfo = await LiveTvManager.GetChannelStream(request.Id, cancellationToken).ConfigureAwait(false);
  1123. state.LiveTvStreamId = streamInfo.Id;
  1124. if (!string.IsNullOrEmpty(streamInfo.Path) && File.Exists(streamInfo.Path))
  1125. {
  1126. state.MediaPath = streamInfo.Path;
  1127. state.IsRemote = false;
  1128. }
  1129. else if (!string.IsNullOrEmpty(streamInfo.Url))
  1130. {
  1131. state.MediaPath = streamInfo.Url;
  1132. state.IsRemote = true;
  1133. }
  1134. state.SendInputOverStandardInput = true;
  1135. state.ReadInputAtNativeFramerate = true;
  1136. state.AudioSync = "1000";
  1137. state.DeInterlace = true;
  1138. }
  1139. else
  1140. {
  1141. state.MediaPath = item.Path;
  1142. state.IsRemote = item.LocationType == LocationType.Remote;
  1143. var video = item as Video;
  1144. if (video != null)
  1145. {
  1146. state.IsInputVideo = true;
  1147. state.VideoType = video.VideoType;
  1148. state.IsoType = video.IsoType;
  1149. state.PlayableStreamFileNames = video.PlayableStreamFileNames == null
  1150. ? new List<string>()
  1151. : video.PlayableStreamFileNames.ToList();
  1152. }
  1153. state.RunTimeTicks = item.RunTimeTicks;
  1154. }
  1155. var videoRequest = request as VideoStreamRequest;
  1156. var mediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery
  1157. {
  1158. ItemId = item.Id
  1159. }).ToList();
  1160. if (videoRequest != null)
  1161. {
  1162. if (!videoRequest.VideoCodec.HasValue)
  1163. {
  1164. videoRequest.VideoCodec = InferVideoCodec(url);
  1165. }
  1166. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  1167. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  1168. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  1169. EnforceResolutionLimit(state, videoRequest);
  1170. }
  1171. else
  1172. {
  1173. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  1174. }
  1175. state.HasMediaStreams = mediaStreams.Count > 0;
  1176. return state;
  1177. }
  1178. /// <summary>
  1179. /// Enforces the resolution limit.
  1180. /// </summary>
  1181. /// <param name="state">The state.</param>
  1182. /// <param name="videoRequest">The video request.</param>
  1183. private void EnforceResolutionLimit(StreamState state, VideoStreamRequest videoRequest)
  1184. {
  1185. // If enabled, allow whatever the client asks for
  1186. if (ServerConfigurationManager.Configuration.AllowVideoUpscaling)
  1187. {
  1188. return;
  1189. }
  1190. // Switch the incoming params to be ceilings rather than fixed values
  1191. videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width;
  1192. videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height;
  1193. videoRequest.Width = null;
  1194. videoRequest.Height = null;
  1195. }
  1196. protected string GetInputModifier(StreamState state)
  1197. {
  1198. var inputModifier = string.Empty;
  1199. var probeSize = GetProbeSizeArgument(state.MediaPath, state.IsInputVideo, state.VideoType, state.IsoType);
  1200. inputModifier += " " + probeSize;
  1201. inputModifier = inputModifier.Trim();
  1202. inputModifier += " " + GetUserAgentParam(state.MediaPath);
  1203. inputModifier = inputModifier.Trim();
  1204. inputModifier += " " + GetFastSeekCommandLineParameter(state.Request);
  1205. inputModifier = inputModifier.Trim();
  1206. if (state.VideoRequest != null)
  1207. {
  1208. inputModifier += " -fflags genpts";
  1209. }
  1210. if (!string.IsNullOrEmpty(state.InputFormat))
  1211. {
  1212. inputModifier += " -f " + state.InputFormat;
  1213. }
  1214. if (!string.IsNullOrEmpty(state.InputVideoCodec))
  1215. {
  1216. inputModifier += " -vcodec " + state.InputVideoCodec;
  1217. }
  1218. if (!string.IsNullOrEmpty(state.InputAudioCodec))
  1219. {
  1220. inputModifier += " -acodec " + state.InputAudioCodec;
  1221. }
  1222. if (state.ReadInputAtNativeFramerate)
  1223. {
  1224. inputModifier += " -re";
  1225. }
  1226. return inputModifier;
  1227. }
  1228. /// <summary>
  1229. /// Infers the audio codec based on the url
  1230. /// </summary>
  1231. /// <param name="url">The URL.</param>
  1232. /// <returns>System.Nullable{AudioCodecs}.</returns>
  1233. private AudioCodecs? InferAudioCodec(string url)
  1234. {
  1235. var ext = Path.GetExtension(url);
  1236. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  1237. {
  1238. return AudioCodecs.Mp3;
  1239. }
  1240. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  1241. {
  1242. return AudioCodecs.Aac;
  1243. }
  1244. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  1245. {
  1246. return AudioCodecs.Wma;
  1247. }
  1248. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  1249. {
  1250. return AudioCodecs.Vorbis;
  1251. }
  1252. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  1253. {
  1254. return AudioCodecs.Vorbis;
  1255. }
  1256. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1257. {
  1258. return AudioCodecs.Vorbis;
  1259. }
  1260. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1261. {
  1262. return AudioCodecs.Vorbis;
  1263. }
  1264. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  1265. {
  1266. return AudioCodecs.Vorbis;
  1267. }
  1268. return null;
  1269. }
  1270. /// <summary>
  1271. /// Infers the video codec.
  1272. /// </summary>
  1273. /// <param name="url">The URL.</param>
  1274. /// <returns>System.Nullable{VideoCodecs}.</returns>
  1275. private VideoCodecs? InferVideoCodec(string url)
  1276. {
  1277. var ext = Path.GetExtension(url);
  1278. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  1279. {
  1280. return VideoCodecs.Wmv;
  1281. }
  1282. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1283. {
  1284. return VideoCodecs.Vpx;
  1285. }
  1286. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1287. {
  1288. return VideoCodecs.Theora;
  1289. }
  1290. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  1291. {
  1292. return VideoCodecs.H264;
  1293. }
  1294. return VideoCodecs.Copy;
  1295. }
  1296. }
  1297. }