VideoHlsService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Devices;
  4. using MediaBrowser.Controller.Dlna;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.MediaEncoding;
  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. // TODO: Deprecate with new iOS app
  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. [Route("/Videos/{Id}/live.m3u8", "GET")]
  28. [Api(Description = "Gets a video stream using HTTP live streaming.")]
  29. public class GetLiveHlsStream : VideoStreamRequest
  30. {
  31. }
  32. /// <summary>
  33. /// Class VideoHlsService
  34. /// </summary>
  35. public class VideoHlsService : BaseHlsService
  36. {
  37. public VideoHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient)
  38. {
  39. }
  40. /// <summary>
  41. /// Gets the specified request.
  42. /// </summary>
  43. /// <param name="request">The request.</param>
  44. /// <returns>System.Object.</returns>
  45. public object Get(GetHlsVideoStream request)
  46. {
  47. return ProcessRequest(request, false);
  48. }
  49. public object Get(GetLiveHlsStream request)
  50. {
  51. return ProcessRequest(request, true);
  52. }
  53. /// <summary>
  54. /// Gets the audio arguments.
  55. /// </summary>
  56. /// <param name="state">The state.</param>
  57. /// <returns>System.String.</returns>
  58. protected override string GetAudioArguments(StreamState state)
  59. {
  60. var codec = state.OutputAudioCodec;
  61. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  62. {
  63. return "-codec:a:0 copy";
  64. }
  65. var args = "-codec:a:0 " + codec;
  66. var channels = state.OutputAudioChannels;
  67. if (channels.HasValue)
  68. {
  69. args += " -ac " + channels.Value;
  70. }
  71. var bitrate = state.OutputAudioBitrate;
  72. if (bitrate.HasValue)
  73. {
  74. args += " -ab " + bitrate.Value.ToString(UsCulture);
  75. }
  76. args += " " + GetAudioFilterParam(state, true);
  77. return args;
  78. }
  79. /// <summary>
  80. /// Gets the video arguments.
  81. /// </summary>
  82. /// <param name="state">The state.</param>
  83. /// <returns>System.String.</returns>
  84. protected override string GetVideoArguments(StreamState state)
  85. {
  86. var codec = state.OutputVideoCodec;
  87. var args = "-codec:v:0 " + codec;
  88. if (state.EnableMpegtsM2TsMode)
  89. {
  90. args += " -mpegts_m2ts_mode 1";
  91. }
  92. // See if we can save come cpu cycles by avoiding encoding
  93. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  94. {
  95. return state.VideoStream != null && IsH264(state.VideoStream) ?
  96. args + " -bsf:v h264_mp4toannexb" :
  97. args;
  98. }
  99. var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
  100. state.SegmentLength.ToString(UsCulture));
  101. var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
  102. args += " " + GetVideoQualityParam(state, H264Encoder, true) + keyFrameArg;
  103. // Add resolution params, if specified
  104. if (!hasGraphicalSubs)
  105. {
  106. args += GetOutputSizeParam(state, codec);
  107. }
  108. // This is for internal graphical subs
  109. if (hasGraphicalSubs)
  110. {
  111. args += GetGraphicalSubtitleParam(state, codec);
  112. }
  113. return args;
  114. }
  115. /// <summary>
  116. /// Gets the segment file extension.
  117. /// </summary>
  118. /// <param name="state">The state.</param>
  119. /// <returns>System.String.</returns>
  120. protected override string GetSegmentFileExtension(StreamState state)
  121. {
  122. return ".ts";
  123. }
  124. }
  125. }