BaseStreamingService.cs 40 KB

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