VideoHlsService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.MediaInfo;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Dto;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Persistence;
  7. using MediaBrowser.Model.IO;
  8. using ServiceStack;
  9. using System;
  10. namespace MediaBrowser.Api.Playback.Hls
  11. {
  12. /// <summary>
  13. /// Class GetHlsVideoStream
  14. /// </summary>
  15. [Route("/Videos/{Id}/stream.m3u8", "GET")]
  16. [Api(Description = "Gets a video stream using HTTP live streaming.")]
  17. public class GetHlsVideoStream : VideoStreamRequest
  18. {
  19. [ApiMember(Name = "BaselineStreamAudioBitRate", Description = "Optional. Specify the audio bitrate for the baseline stream.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  20. public int? BaselineStreamAudioBitRate { get; set; }
  21. [ApiMember(Name = "AppendBaselineStream", Description = "Optional. Whether or not to include a baseline audio-only stream in the master playlist.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  22. public bool AppendBaselineStream { get; set; }
  23. [ApiMember(Name = "TimeStampOffsetMs", Description = "Optional. Alter the timestamps in the playlist by a given amount, in ms. Default is 1000.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  24. public int TimeStampOffsetMs { get; set; }
  25. }
  26. /// <summary>
  27. /// Class VideoHlsService
  28. /// </summary>
  29. public class VideoHlsService : BaseHlsService
  30. {
  31. public VideoHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository)
  32. : base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository)
  33. {
  34. }
  35. /// <summary>
  36. /// Gets the specified request.
  37. /// </summary>
  38. /// <param name="request">The request.</param>
  39. /// <returns>System.Object.</returns>
  40. public object Get(GetHlsVideoStream request)
  41. {
  42. return ProcessRequest(request);
  43. }
  44. /// <summary>
  45. /// Gets the audio arguments.
  46. /// </summary>
  47. /// <param name="state">The state.</param>
  48. /// <returns>System.String.</returns>
  49. protected override string GetAudioArguments(StreamState state)
  50. {
  51. var codec = GetAudioCodec(state.Request);
  52. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  53. {
  54. return "-codec:a:0 copy";
  55. }
  56. var args = "-codec:a:0 " + codec;
  57. if (state.AudioStream != null)
  58. {
  59. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  60. if (channels.HasValue)
  61. {
  62. args += " -ac " + channels.Value;
  63. }
  64. var bitrate = GetAudioBitrateParam(state);
  65. if (bitrate.HasValue)
  66. {
  67. args += " -ab " + bitrate.Value.ToString(UsCulture);
  68. }
  69. var volParam = string.Empty;
  70. var audioSampleRate = string.Empty;
  71. // Boost volume to 200% when downsampling from 6ch to 2ch
  72. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  73. {
  74. volParam = ",volume=2.000000";
  75. }
  76. if (state.Request.AudioSampleRate.HasValue)
  77. {
  78. audioSampleRate = state.Request.AudioSampleRate.Value + ":";
  79. }
  80. args += string.Format(" -af \"adelay=1,aresample={0}async=1000{1}\"", audioSampleRate, volParam);
  81. return args;
  82. }
  83. return args;
  84. }
  85. /// <summary>
  86. /// Gets the video arguments.
  87. /// </summary>
  88. /// <param name="state">The state.</param>
  89. /// <param name="performSubtitleConversion">if set to <c>true</c> [perform subtitle conversion].</param>
  90. /// <returns>System.String.</returns>
  91. protected override string GetVideoArguments(StreamState state, bool performSubtitleConversion)
  92. {
  93. var codec = GetVideoCodec(state.VideoRequest);
  94. // See if we can save come cpu cycles by avoiding encoding
  95. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  96. {
  97. return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  98. }
  99. const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,.1),gte(t,prev_forced_t+5))";
  100. var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsExternal &&
  101. (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 ||
  102. state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1);
  103. var args = "-codec:v:0 " + codec + " -preset superfast" + keyFrameArg;
  104. var bitrate = GetVideoBitrateParam(state);
  105. if (bitrate.HasValue)
  106. {
  107. args += string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture));
  108. }
  109. // Add resolution params, if specified
  110. if (!hasGraphicalSubs)
  111. {
  112. if (state.VideoRequest.Width.HasValue || state.VideoRequest.Height.HasValue || state.VideoRequest.MaxHeight.HasValue || state.VideoRequest.MaxWidth.HasValue)
  113. {
  114. args += GetOutputSizeParam(state, codec, performSubtitleConversion);
  115. }
  116. }
  117. if (state.VideoRequest.Framerate.HasValue)
  118. {
  119. args += string.Format(" -r {0}", state.VideoRequest.Framerate.Value);
  120. }
  121. args += " -vsync vfr";
  122. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  123. {
  124. args += " -profile:v " + state.VideoRequest.Profile;
  125. }
  126. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  127. {
  128. args += " -level " + state.VideoRequest.Level;
  129. }
  130. // This is for internal graphical subs
  131. if (hasGraphicalSubs)
  132. {
  133. args += GetInternalGraphicalSubtitleParam(state, codec);
  134. }
  135. return args;
  136. }
  137. /// <summary>
  138. /// Gets the segment file extension.
  139. /// </summary>
  140. /// <param name="state">The state.</param>
  141. /// <returns>System.String.</returns>
  142. protected override string GetSegmentFileExtension(StreamState state)
  143. {
  144. return ".ts";
  145. }
  146. }
  147. }