VideoHlsService.cs 7.0 KB

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