BaseStreamingService.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Jellyfin.Data.Enums;
  11. using MediaBrowser.Common.Configuration;
  12. using MediaBrowser.Common.Extensions;
  13. using MediaBrowser.Controller.Configuration;
  14. using MediaBrowser.Controller.Devices;
  15. using MediaBrowser.Controller.Dlna;
  16. using MediaBrowser.Controller.Library;
  17. using MediaBrowser.Controller.MediaEncoding;
  18. using MediaBrowser.Controller.Net;
  19. using MediaBrowser.Model.Configuration;
  20. using MediaBrowser.Model.Dlna;
  21. using MediaBrowser.Model.Dto;
  22. using MediaBrowser.Model.Entities;
  23. using MediaBrowser.Model.IO;
  24. using MediaBrowser.Model.MediaInfo;
  25. using MediaBrowser.Model.Serialization;
  26. using Microsoft.Extensions.Logging;
  27. namespace MediaBrowser.Api.Playback
  28. {
  29. /// <summary>
  30. /// Class BaseStreamingService
  31. /// </summary>
  32. public abstract class BaseStreamingService : BaseApiService
  33. {
  34. protected virtual bool EnableOutputInSubFolder => false;
  35. /// <summary>
  36. /// Gets or sets the user manager.
  37. /// </summary>
  38. /// <value>The user manager.</value>
  39. protected IUserManager UserManager { get; private set; }
  40. /// <summary>
  41. /// Gets or sets the library manager.
  42. /// </summary>
  43. /// <value>The library manager.</value>
  44. protected ILibraryManager LibraryManager { get; private set; }
  45. /// <summary>
  46. /// Gets or sets the iso manager.
  47. /// </summary>
  48. /// <value>The iso manager.</value>
  49. protected IIsoManager IsoManager { get; private set; }
  50. /// <summary>
  51. /// Gets or sets the media encoder.
  52. /// </summary>
  53. /// <value>The media encoder.</value>
  54. protected IMediaEncoder MediaEncoder { get; private set; }
  55. protected IFileSystem FileSystem { get; private set; }
  56. protected IDlnaManager DlnaManager { get; private set; }
  57. protected IDeviceManager DeviceManager { get; private set; }
  58. protected IMediaSourceManager MediaSourceManager { get; private set; }
  59. protected IJsonSerializer JsonSerializer { get; private set; }
  60. protected IAuthorizationContext AuthorizationContext { get; private set; }
  61. protected EncodingHelper EncodingHelper { get; set; }
  62. /// <summary>
  63. /// Gets the type of the transcoding job.
  64. /// </summary>
  65. /// <value>The type of the transcoding job.</value>
  66. protected abstract TranscodingJobType TranscodingJobType { get; }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  69. /// </summary>
  70. protected BaseStreamingService(
  71. ILogger<BaseStreamingService> logger,
  72. IServerConfigurationManager serverConfigurationManager,
  73. IHttpResultFactory httpResultFactory,
  74. IUserManager userManager,
  75. ILibraryManager libraryManager,
  76. IIsoManager isoManager,
  77. IMediaEncoder mediaEncoder,
  78. IFileSystem fileSystem,
  79. IDlnaManager dlnaManager,
  80. IDeviceManager deviceManager,
  81. IMediaSourceManager mediaSourceManager,
  82. IJsonSerializer jsonSerializer,
  83. IAuthorizationContext authorizationContext,
  84. EncodingHelper encodingHelper)
  85. : base(logger, serverConfigurationManager, httpResultFactory)
  86. {
  87. UserManager = userManager;
  88. LibraryManager = libraryManager;
  89. IsoManager = isoManager;
  90. MediaEncoder = mediaEncoder;
  91. FileSystem = fileSystem;
  92. DlnaManager = dlnaManager;
  93. DeviceManager = deviceManager;
  94. MediaSourceManager = mediaSourceManager;
  95. JsonSerializer = jsonSerializer;
  96. AuthorizationContext = authorizationContext;
  97. EncodingHelper = encodingHelper;
  98. }
  99. /// <summary>
  100. /// Gets the command line arguments.
  101. /// </summary>
  102. protected abstract string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding);
  103. /// <summary>
  104. /// Gets the output file extension.
  105. /// </summary>
  106. /// <param name="state">The state.</param>
  107. /// <returns>System.String.</returns>
  108. protected virtual string GetOutputFileExtension(StreamState state)
  109. {
  110. return Path.GetExtension(state.RequestedUrl);
  111. }
  112. /// <summary>
  113. /// Gets the output file path.
  114. /// </summary>
  115. private string GetOutputFilePath(StreamState state, EncodingOptions encodingOptions, string outputFileExtension)
  116. {
  117. var data = $"{state.MediaPath}-{state.UserAgent}-{state.Request.DeviceId}-{state.Request.PlaySessionId}";
  118. var filename = data.GetMD5().ToString("N", CultureInfo.InvariantCulture);
  119. var ext = outputFileExtension?.ToLowerInvariant();
  120. var folder = ServerConfigurationManager.GetTranscodePath();
  121. return EnableOutputInSubFolder
  122. ? Path.Combine(folder, filename, filename + ext)
  123. : Path.Combine(folder, filename + ext);
  124. }
  125. protected virtual string GetDefaultEncoderPreset()
  126. {
  127. return "superfast";
  128. }
  129. private async Task AcquireResources(StreamState state, CancellationTokenSource cancellationTokenSource)
  130. {
  131. if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  132. {
  133. state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationTokenSource.Token).ConfigureAwait(false);
  134. }
  135. if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.Request.LiveStreamId))
  136. {
  137. var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest
  138. {
  139. OpenToken = state.MediaSource.OpenToken
  140. }, cancellationTokenSource.Token).ConfigureAwait(false);
  141. EncodingHelper.AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.RequestedUrl);
  142. if (state.VideoRequest != null)
  143. {
  144. EncodingHelper.TryStreamCopy(state);
  145. }
  146. }
  147. if (state.MediaSource.BufferMs.HasValue)
  148. {
  149. await Task.Delay(state.MediaSource.BufferMs.Value, cancellationTokenSource.Token).ConfigureAwait(false);
  150. }
  151. }
  152. /// <summary>
  153. /// Starts the FFMPEG.
  154. /// </summary>
  155. /// <param name="state">The state.</param>
  156. /// <param name="outputPath">The output path.</param>
  157. /// <param name="cancellationTokenSource">The cancellation token source.</param>
  158. /// <param name="workingDirectory">The working directory.</param>
  159. /// <returns>Task.</returns>
  160. protected async Task<TranscodingJob> StartFfMpeg(
  161. StreamState state,
  162. string outputPath,
  163. CancellationTokenSource cancellationTokenSource,
  164. string workingDirectory = null)
  165. {
  166. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  167. await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false);
  168. if (state.VideoRequest != null && !EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
  169. {
  170. var auth = AuthorizationContext.GetAuthorizationInfo(Request);
  171. if (auth.User != null && !auth.User.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding))
  172. {
  173. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state);
  174. throw new ArgumentException("User does not have access to video transcoding");
  175. }
  176. }
  177. var encodingOptions = ServerConfigurationManager.GetEncodingOptions();
  178. var process = new Process()
  179. {
  180. StartInfo = new ProcessStartInfo()
  181. {
  182. WindowStyle = ProcessWindowStyle.Hidden,
  183. CreateNoWindow = true,
  184. UseShellExecute = false,
  185. // Must consume both stdout and stderr or deadlocks may occur
  186. //RedirectStandardOutput = true,
  187. RedirectStandardError = true,
  188. RedirectStandardInput = true,
  189. FileName = MediaEncoder.EncoderPath,
  190. Arguments = GetCommandLineArguments(outputPath, encodingOptions, state, true),
  191. WorkingDirectory = string.IsNullOrWhiteSpace(workingDirectory) ? null : workingDirectory,
  192. ErrorDialog = false
  193. },
  194. EnableRaisingEvents = true
  195. };
  196. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath,
  197. state.Request.PlaySessionId,
  198. state.MediaSource.LiveStreamId,
  199. Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
  200. TranscodingJobType,
  201. process,
  202. state.Request.DeviceId,
  203. state,
  204. cancellationTokenSource);
  205. var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;
  206. Logger.LogInformation(commandLineLogMessage);
  207. var logFilePrefix = "ffmpeg-transcode";
  208. if (state.VideoRequest != null
  209. && EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
  210. {
  211. logFilePrefix = EncodingHelper.IsCopyCodec(state.OutputAudioCodec)
  212. ? "ffmpeg-remux" : "ffmpeg-directstream";
  213. }
  214. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, logFilePrefix + "-" + Guid.NewGuid() + ".txt");
  215. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  216. Stream logStream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true);
  217. var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(Request.AbsoluteUri + Environment.NewLine + Environment.NewLine + JsonSerializer.SerializeToString(state.MediaSource) + Environment.NewLine + Environment.NewLine + commandLineLogMessage + Environment.NewLine + Environment.NewLine);
  218. await logStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationTokenSource.Token).ConfigureAwait(false);
  219. process.Exited += (sender, args) => OnFfMpegProcessExited(process, transcodingJob, state);
  220. try
  221. {
  222. process.Start();
  223. }
  224. catch (Exception ex)
  225. {
  226. Logger.LogError(ex, "Error starting ffmpeg");
  227. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state);
  228. throw;
  229. }
  230. Logger.LogDebug("Launched ffmpeg process");
  231. state.TranscodingJob = transcodingJob;
  232. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  233. _ = new JobLogger(Logger).StartStreamingLog(state, process.StandardError.BaseStream, logStream);
  234. // Wait for the file to exist before proceeeding
  235. var ffmpegTargetFile = state.WaitForPath ?? outputPath;
  236. Logger.LogDebug("Waiting for the creation of {0}", ffmpegTargetFile);
  237. while (!File.Exists(ffmpegTargetFile) && !transcodingJob.HasExited)
  238. {
  239. await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false);
  240. }
  241. Logger.LogDebug("File {0} created or transcoding has finished", ffmpegTargetFile);
  242. if (state.IsInputVideo && transcodingJob.Type == TranscodingJobType.Progressive && !transcodingJob.HasExited)
  243. {
  244. await Task.Delay(1000, cancellationTokenSource.Token).ConfigureAwait(false);
  245. if (state.ReadInputAtNativeFramerate && !transcodingJob.HasExited)
  246. {
  247. await Task.Delay(1500, cancellationTokenSource.Token).ConfigureAwait(false);
  248. }
  249. }
  250. if (!transcodingJob.HasExited)
  251. {
  252. StartThrottler(state, transcodingJob);
  253. }
  254. Logger.LogDebug("StartFfMpeg() finished successfully");
  255. return transcodingJob;
  256. }
  257. private void StartThrottler(StreamState state, TranscodingJob transcodingJob)
  258. {
  259. if (EnableThrottling(state))
  260. {
  261. transcodingJob.TranscodingThrottler = state.TranscodingThrottler = new TranscodingThrottler(transcodingJob, Logger, ServerConfigurationManager, FileSystem);
  262. state.TranscodingThrottler.Start();
  263. }
  264. }
  265. private bool EnableThrottling(StreamState state)
  266. {
  267. var encodingOptions = ServerConfigurationManager.GetEncodingOptions();
  268. // enable throttling when NOT using hardware acceleration
  269. if (string.IsNullOrEmpty(encodingOptions.HardwareAccelerationType))
  270. {
  271. return state.InputProtocol == MediaProtocol.File &&
  272. state.RunTimeTicks.HasValue &&
  273. state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks &&
  274. state.IsInputVideo &&
  275. state.VideoType == VideoType.VideoFile &&
  276. !EncodingHelper.IsCopyCodec(state.OutputVideoCodec);
  277. }
  278. return false;
  279. }
  280. /// <summary>
  281. /// Processes the exited.
  282. /// </summary>
  283. /// <param name="process">The process.</param>
  284. /// <param name="job">The job.</param>
  285. /// <param name="state">The state.</param>
  286. private void OnFfMpegProcessExited(Process process, TranscodingJob job, StreamState state)
  287. {
  288. if (job != null)
  289. {
  290. job.HasExited = true;
  291. }
  292. Logger.LogDebug("Disposing stream resources");
  293. state.Dispose();
  294. if (process.ExitCode == 0)
  295. {
  296. Logger.LogInformation("FFMpeg exited with code 0");
  297. }
  298. else
  299. {
  300. Logger.LogError("FFMpeg exited with code {0}", process.ExitCode);
  301. }
  302. process.Dispose();
  303. }
  304. /// <summary>
  305. /// Parses the parameters.
  306. /// </summary>
  307. /// <param name="request">The request.</param>
  308. private void ParseParams(StreamRequest request)
  309. {
  310. var vals = request.Params.Split(';');
  311. var videoRequest = request as VideoStreamRequest;
  312. for (var i = 0; i < vals.Length; i++)
  313. {
  314. var val = vals[i];
  315. if (string.IsNullOrWhiteSpace(val))
  316. {
  317. continue;
  318. }
  319. switch (i)
  320. {
  321. case 0:
  322. request.DeviceProfileId = val;
  323. break;
  324. case 1:
  325. request.DeviceId = val;
  326. break;
  327. case 2:
  328. request.MediaSourceId = val;
  329. break;
  330. case 3:
  331. request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  332. break;
  333. case 4:
  334. if (videoRequest != null)
  335. {
  336. videoRequest.VideoCodec = val;
  337. }
  338. break;
  339. case 5:
  340. request.AudioCodec = val;
  341. break;
  342. case 6:
  343. if (videoRequest != null)
  344. {
  345. videoRequest.AudioStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
  346. }
  347. break;
  348. case 7:
  349. if (videoRequest != null)
  350. {
  351. videoRequest.SubtitleStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
  352. }
  353. break;
  354. case 8:
  355. if (videoRequest != null)
  356. {
  357. videoRequest.VideoBitRate = int.Parse(val, CultureInfo.InvariantCulture);
  358. }
  359. break;
  360. case 9:
  361. request.AudioBitRate = int.Parse(val, CultureInfo.InvariantCulture);
  362. break;
  363. case 10:
  364. request.MaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
  365. break;
  366. case 11:
  367. if (videoRequest != null)
  368. {
  369. videoRequest.MaxFramerate = float.Parse(val, CultureInfo.InvariantCulture);
  370. }
  371. break;
  372. case 12:
  373. if (videoRequest != null)
  374. {
  375. videoRequest.MaxWidth = int.Parse(val, CultureInfo.InvariantCulture);
  376. }
  377. break;
  378. case 13:
  379. if (videoRequest != null)
  380. {
  381. videoRequest.MaxHeight = int.Parse(val, CultureInfo.InvariantCulture);
  382. }
  383. break;
  384. case 14:
  385. request.StartTimeTicks = long.Parse(val, CultureInfo.InvariantCulture);
  386. break;
  387. case 15:
  388. if (videoRequest != null)
  389. {
  390. videoRequest.Level = val;
  391. }
  392. break;
  393. case 16:
  394. if (videoRequest != null)
  395. {
  396. videoRequest.MaxRefFrames = int.Parse(val, CultureInfo.InvariantCulture);
  397. }
  398. break;
  399. case 17:
  400. if (videoRequest != null)
  401. {
  402. videoRequest.MaxVideoBitDepth = int.Parse(val, CultureInfo.InvariantCulture);
  403. }
  404. break;
  405. case 18:
  406. if (videoRequest != null)
  407. {
  408. videoRequest.Profile = val;
  409. }
  410. break;
  411. case 19:
  412. // cabac no longer used
  413. break;
  414. case 20:
  415. request.PlaySessionId = val;
  416. break;
  417. case 21:
  418. // api_key
  419. break;
  420. case 22:
  421. request.LiveStreamId = val;
  422. break;
  423. case 23:
  424. // Duplicating ItemId because of MediaMonkey
  425. break;
  426. case 24:
  427. if (videoRequest != null)
  428. {
  429. videoRequest.CopyTimestamps = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  430. }
  431. break;
  432. case 25:
  433. if (!string.IsNullOrWhiteSpace(val) && videoRequest != null)
  434. {
  435. if (Enum.TryParse(val, out SubtitleDeliveryMethod method))
  436. {
  437. videoRequest.SubtitleMethod = method;
  438. }
  439. }
  440. break;
  441. case 26:
  442. request.TranscodingMaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
  443. break;
  444. case 27:
  445. if (videoRequest != null)
  446. {
  447. videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  448. }
  449. break;
  450. case 28:
  451. request.Tag = val;
  452. break;
  453. case 29:
  454. if (videoRequest != null)
  455. {
  456. videoRequest.RequireAvc = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  457. }
  458. break;
  459. case 30:
  460. request.SubtitleCodec = val;
  461. break;
  462. case 31:
  463. if (videoRequest != null)
  464. {
  465. videoRequest.RequireNonAnamorphic = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  466. }
  467. break;
  468. case 32:
  469. if (videoRequest != null)
  470. {
  471. videoRequest.DeInterlace = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  472. }
  473. break;
  474. case 33:
  475. request.TranscodeReasons = val;
  476. break;
  477. }
  478. }
  479. }
  480. /// <summary>
  481. /// Parses query parameters as StreamOptions.
  482. /// </summary>
  483. /// <param name="request">The stream request.</param>
  484. private void ParseStreamOptions(StreamRequest request)
  485. {
  486. foreach (var param in Request.QueryString)
  487. {
  488. if (char.IsLower(param.Key[0]))
  489. {
  490. // This was probably not parsed initially and should be a StreamOptions
  491. // TODO: This should be incorporated either in the lower framework for parsing requests
  492. // or the generated URL should correctly serialize it
  493. request.StreamOptions[param.Key] = param.Value;
  494. }
  495. }
  496. }
  497. /// <summary>
  498. /// Parses the dlna headers.
  499. /// </summary>
  500. /// <param name="request">The request.</param>
  501. private void ParseDlnaHeaders(StreamRequest request)
  502. {
  503. if (!request.StartTimeTicks.HasValue)
  504. {
  505. var timeSeek = GetHeader("TimeSeekRange.dlna.org");
  506. request.StartTimeTicks = ParseTimeSeekHeader(timeSeek);
  507. }
  508. }
  509. /// <summary>
  510. /// Parses the time seek header.
  511. /// </summary>
  512. private long? ParseTimeSeekHeader(string value)
  513. {
  514. if (string.IsNullOrWhiteSpace(value))
  515. {
  516. return null;
  517. }
  518. const string Npt = "npt=";
  519. if (!value.StartsWith(Npt, StringComparison.OrdinalIgnoreCase))
  520. {
  521. throw new ArgumentException("Invalid timeseek header");
  522. }
  523. int index = value.IndexOf('-');
  524. value = index == -1
  525. ? value.Substring(Npt.Length)
  526. : value.Substring(Npt.Length, index - Npt.Length);
  527. if (value.IndexOf(':') == -1)
  528. {
  529. // Parses npt times in the format of '417.33'
  530. if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var seconds))
  531. {
  532. return TimeSpan.FromSeconds(seconds).Ticks;
  533. }
  534. throw new ArgumentException("Invalid timeseek header");
  535. }
  536. // Parses npt times in the format of '10:19:25.7'
  537. var tokens = value.Split(new[] { ':' }, 3);
  538. double secondsSum = 0;
  539. var timeFactor = 3600;
  540. foreach (var time in tokens)
  541. {
  542. if (double.TryParse(time, NumberStyles.Any, CultureInfo.InvariantCulture, out var digit))
  543. {
  544. secondsSum += digit * timeFactor;
  545. }
  546. else
  547. {
  548. throw new ArgumentException("Invalid timeseek header");
  549. }
  550. timeFactor /= 60;
  551. }
  552. return TimeSpan.FromSeconds(secondsSum).Ticks;
  553. }
  554. /// <summary>
  555. /// Gets the state.
  556. /// </summary>
  557. /// <param name="request">The request.</param>
  558. /// <param name="cancellationToken">The cancellation token.</param>
  559. /// <returns>StreamState.</returns>
  560. protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
  561. {
  562. ParseDlnaHeaders(request);
  563. if (!string.IsNullOrWhiteSpace(request.Params))
  564. {
  565. ParseParams(request);
  566. }
  567. ParseStreamOptions(request);
  568. var url = Request.PathInfo;
  569. if (string.IsNullOrEmpty(request.AudioCodec))
  570. {
  571. request.AudioCodec = EncodingHelper.InferAudioCodec(url);
  572. }
  573. var enableDlnaHeaders = !string.IsNullOrWhiteSpace(request.Params) ||
  574. string.Equals(GetHeader("GetContentFeatures.DLNA.ORG"), "1", StringComparison.OrdinalIgnoreCase);
  575. var state = new StreamState(MediaSourceManager, TranscodingJobType)
  576. {
  577. Request = request,
  578. RequestedUrl = url,
  579. UserAgent = Request.UserAgent,
  580. EnableDlnaHeaders = enableDlnaHeaders
  581. };
  582. var auth = AuthorizationContext.GetAuthorizationInfo(Request);
  583. if (!auth.UserId.Equals(Guid.Empty))
  584. {
  585. state.User = UserManager.GetUserById(auth.UserId);
  586. }
  587. //if ((Request.UserAgent ?? string.Empty).IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
  588. // (Request.UserAgent ?? string.Empty).IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
  589. // (Request.UserAgent ?? string.Empty).IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
  590. //{
  591. // state.SegmentLength = 6;
  592. //}
  593. if (state.VideoRequest != null && !string.IsNullOrWhiteSpace(state.VideoRequest.VideoCodec))
  594. {
  595. state.SupportedVideoCodecs = state.VideoRequest.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
  596. state.VideoRequest.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault();
  597. }
  598. if (!string.IsNullOrWhiteSpace(request.AudioCodec))
  599. {
  600. state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
  601. state.Request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToAudioCodec(i))
  602. ?? state.SupportedAudioCodecs.FirstOrDefault();
  603. }
  604. if (!string.IsNullOrWhiteSpace(request.SubtitleCodec))
  605. {
  606. state.SupportedSubtitleCodecs = request.SubtitleCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
  607. state.Request.SubtitleCodec = state.SupportedSubtitleCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToSubtitleCodec(i))
  608. ?? state.SupportedSubtitleCodecs.FirstOrDefault();
  609. }
  610. var item = LibraryManager.GetItemById(request.Id);
  611. state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  612. //var primaryImage = item.GetImageInfo(ImageType.Primary, 0) ??
  613. // item.Parents.Select(i => i.GetImageInfo(ImageType.Primary, 0)).FirstOrDefault(i => i != null);
  614. //if (primaryImage != null)
  615. //{
  616. // state.AlbumCoverPath = primaryImage.Path;
  617. //}
  618. MediaSourceInfo mediaSource = null;
  619. if (string.IsNullOrWhiteSpace(request.LiveStreamId))
  620. {
  621. var currentJob = !string.IsNullOrWhiteSpace(request.PlaySessionId) ?
  622. ApiEntryPoint.Instance.GetTranscodingJob(request.PlaySessionId)
  623. : null;
  624. if (currentJob != null)
  625. {
  626. mediaSource = currentJob.MediaSource;
  627. }
  628. if (mediaSource == null)
  629. {
  630. var mediaSources = await MediaSourceManager.GetPlaybackMediaSources(LibraryManager.GetItemById(request.Id), null, false, false, cancellationToken).ConfigureAwait(false);
  631. mediaSource = string.IsNullOrEmpty(request.MediaSourceId)
  632. ? mediaSources[0]
  633. : mediaSources.Find(i => string.Equals(i.Id, request.MediaSourceId));
  634. if (mediaSource == null && Guid.Parse(request.MediaSourceId) == request.Id)
  635. {
  636. mediaSource = mediaSources[0];
  637. }
  638. }
  639. }
  640. else
  641. {
  642. var liveStreamInfo = await MediaSourceManager.GetLiveStreamWithDirectStreamProvider(request.LiveStreamId, cancellationToken).ConfigureAwait(false);
  643. mediaSource = liveStreamInfo.Item1;
  644. state.DirectStreamProvider = liveStreamInfo.Item2;
  645. }
  646. var videoRequest = request as VideoStreamRequest;
  647. EncodingHelper.AttachMediaSourceInfo(state, mediaSource, url);
  648. var container = Path.GetExtension(state.RequestedUrl);
  649. if (string.IsNullOrEmpty(container))
  650. {
  651. container = request.Container;
  652. }
  653. if (string.IsNullOrEmpty(container))
  654. {
  655. container = request.Static ?
  656. StreamBuilder.NormalizeMediaSourceFormatIntoSingleContainer(state.InputContainer, state.MediaPath, null, DlnaProfileType.Audio) :
  657. GetOutputFileExtension(state);
  658. }
  659. state.OutputContainer = (container ?? string.Empty).TrimStart('.');
  660. state.OutputAudioBitrate = EncodingHelper.GetAudioBitrateParam(state.Request, state.AudioStream);
  661. state.OutputAudioCodec = state.Request.AudioCodec;
  662. state.OutputAudioChannels = EncodingHelper.GetNumAudioChannelsParam(state, state.AudioStream, state.OutputAudioCodec);
  663. if (videoRequest != null)
  664. {
  665. state.OutputVideoCodec = state.VideoRequest.VideoCodec;
  666. state.OutputVideoBitrate = EncodingHelper.GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream, state.OutputVideoCodec);
  667. if (videoRequest != null)
  668. {
  669. EncodingHelper.TryStreamCopy(state);
  670. }
  671. if (state.OutputVideoBitrate.HasValue && !EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
  672. {
  673. var resolution = ResolutionNormalizer.Normalize(
  674. state.VideoStream?.BitRate,
  675. state.VideoStream?.Width,
  676. state.VideoStream?.Height,
  677. state.OutputVideoBitrate.Value,
  678. state.VideoStream?.Codec,
  679. state.OutputVideoCodec,
  680. videoRequest.MaxWidth,
  681. videoRequest.MaxHeight);
  682. videoRequest.MaxWidth = resolution.MaxWidth;
  683. videoRequest.MaxHeight = resolution.MaxHeight;
  684. }
  685. }
  686. ApplyDeviceProfileSettings(state);
  687. var ext = string.IsNullOrWhiteSpace(state.OutputContainer)
  688. ? GetOutputFileExtension(state)
  689. : ('.' + state.OutputContainer);
  690. var encodingOptions = ServerConfigurationManager.GetEncodingOptions();
  691. state.OutputFilePath = GetOutputFilePath(state, encodingOptions, ext);
  692. return state;
  693. }
  694. private void ApplyDeviceProfileSettings(StreamState state)
  695. {
  696. var headers = Request.Headers;
  697. if (!string.IsNullOrWhiteSpace(state.Request.DeviceProfileId))
  698. {
  699. state.DeviceProfile = DlnaManager.GetProfile(state.Request.DeviceProfileId);
  700. }
  701. else if (!string.IsNullOrWhiteSpace(state.Request.DeviceId))
  702. {
  703. var caps = DeviceManager.GetCapabilities(state.Request.DeviceId);
  704. state.DeviceProfile = caps == null ? DlnaManager.GetProfile(headers) : caps.DeviceProfile;
  705. }
  706. var profile = state.DeviceProfile;
  707. if (profile == null)
  708. {
  709. // Don't use settings from the default profile.
  710. // Only use a specific profile if it was requested.
  711. return;
  712. }
  713. var audioCodec = state.ActualOutputAudioCodec;
  714. var videoCodec = state.ActualOutputVideoCodec;
  715. var mediaProfile = state.VideoRequest == null ?
  716. profile.GetAudioMediaProfile(state.OutputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate, state.OutputAudioSampleRate, state.OutputAudioBitDepth) :
  717. profile.GetVideoMediaProfile(state.OutputContainer,
  718. audioCodec,
  719. videoCodec,
  720. state.OutputWidth,
  721. state.OutputHeight,
  722. state.TargetVideoBitDepth,
  723. state.OutputVideoBitrate,
  724. state.TargetVideoProfile,
  725. state.TargetVideoLevel,
  726. state.TargetFramerate,
  727. state.TargetPacketLength,
  728. state.TargetTimestamp,
  729. state.IsTargetAnamorphic,
  730. state.IsTargetInterlaced,
  731. state.TargetRefFrames,
  732. state.TargetVideoStreamCount,
  733. state.TargetAudioStreamCount,
  734. state.TargetVideoCodecTag,
  735. state.IsTargetAVC);
  736. if (mediaProfile != null)
  737. {
  738. state.MimeType = mediaProfile.MimeType;
  739. }
  740. if (!state.Request.Static)
  741. {
  742. var transcodingProfile = state.VideoRequest == null ?
  743. profile.GetAudioTranscodingProfile(state.OutputContainer, audioCodec) :
  744. profile.GetVideoTranscodingProfile(state.OutputContainer, audioCodec, videoCodec);
  745. if (transcodingProfile != null)
  746. {
  747. state.EstimateContentLength = transcodingProfile.EstimateContentLength;
  748. //state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
  749. state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  750. if (state.VideoRequest != null)
  751. {
  752. state.VideoRequest.CopyTimestamps = transcodingProfile.CopyTimestamps;
  753. state.VideoRequest.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
  754. }
  755. }
  756. }
  757. }
  758. /// <summary>
  759. /// Adds the dlna headers.
  760. /// </summary>
  761. /// <param name="state">The state.</param>
  762. /// <param name="responseHeaders">The response headers.</param>
  763. /// <param name="isStaticallyStreamed">if set to <c>true</c> [is statically streamed].</param>
  764. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  765. protected void AddDlnaHeaders(StreamState state, IDictionary<string, string> responseHeaders, bool isStaticallyStreamed)
  766. {
  767. if (!state.EnableDlnaHeaders)
  768. {
  769. return;
  770. }
  771. var profile = state.DeviceProfile;
  772. var transferMode = GetHeader("transferMode.dlna.org");
  773. responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode;
  774. responseHeaders["realTimeInfo.dlna.org"] = "DLNA.ORG_TLAG=*";
  775. if (state.RunTimeTicks.HasValue)
  776. {
  777. if (string.Equals(GetHeader("getMediaInfo.sec"), "1", StringComparison.OrdinalIgnoreCase))
  778. {
  779. var ms = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalMilliseconds;
  780. responseHeaders["MediaInfo.sec"] = string.Format(
  781. CultureInfo.InvariantCulture,
  782. "SEC_Duration={0};",
  783. Convert.ToInt32(ms));
  784. }
  785. if (!isStaticallyStreamed && profile != null)
  786. {
  787. AddTimeSeekResponseHeaders(state, responseHeaders);
  788. }
  789. }
  790. if (profile == null)
  791. {
  792. profile = DlnaManager.GetDefaultProfile();
  793. }
  794. var audioCodec = state.ActualOutputAudioCodec;
  795. if (state.VideoRequest == null)
  796. {
  797. responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile).BuildAudioHeader(
  798. state.OutputContainer,
  799. audioCodec,
  800. state.OutputAudioBitrate,
  801. state.OutputAudioSampleRate,
  802. state.OutputAudioChannels,
  803. state.OutputAudioBitDepth,
  804. isStaticallyStreamed,
  805. state.RunTimeTicks,
  806. state.TranscodeSeekInfo);
  807. }
  808. else
  809. {
  810. var videoCodec = state.ActualOutputVideoCodec;
  811. responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile).BuildVideoHeader(
  812. state.OutputContainer,
  813. videoCodec,
  814. audioCodec,
  815. state.OutputWidth,
  816. state.OutputHeight,
  817. state.TargetVideoBitDepth,
  818. state.OutputVideoBitrate,
  819. state.TargetTimestamp,
  820. isStaticallyStreamed,
  821. state.RunTimeTicks,
  822. state.TargetVideoProfile,
  823. state.TargetVideoLevel,
  824. state.TargetFramerate,
  825. state.TargetPacketLength,
  826. state.TranscodeSeekInfo,
  827. state.IsTargetAnamorphic,
  828. state.IsTargetInterlaced,
  829. state.TargetRefFrames,
  830. state.TargetVideoStreamCount,
  831. state.TargetAudioStreamCount,
  832. state.TargetVideoCodecTag,
  833. state.IsTargetAVC).FirstOrDefault() ?? string.Empty;
  834. }
  835. }
  836. private void AddTimeSeekResponseHeaders(StreamState state, IDictionary<string, string> responseHeaders)
  837. {
  838. var runtimeSeconds = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds.ToString(CultureInfo.InvariantCulture);
  839. var startSeconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds.ToString(CultureInfo.InvariantCulture);
  840. responseHeaders["TimeSeekRange.dlna.org"] = string.Format(
  841. CultureInfo.InvariantCulture,
  842. "npt={0}-{1}/{1}",
  843. startSeconds,
  844. runtimeSeconds);
  845. responseHeaders["X-AvailableSeekRange"] = string.Format(
  846. CultureInfo.InvariantCulture,
  847. "1 npt={0}-{1}",
  848. startSeconds,
  849. runtimeSeconds);
  850. }
  851. }
  852. }