VideoHlsService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Library;
  4. using System;
  5. namespace MediaBrowser.Api.Playback.Hls
  6. {
  7. public class VideoHlsService : BaseHlsService
  8. {
  9. public VideoHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager)
  10. : base(appPaths, userManager, libraryManager, isoManager)
  11. {
  12. }
  13. /// <summary>
  14. /// Gets the audio arguments.
  15. /// </summary>
  16. /// <param name="state">The state.</param>
  17. /// <returns>System.String.</returns>
  18. protected override string GetAudioArguments(StreamState state)
  19. {
  20. if (!state.Request.AudioCodec.HasValue)
  21. {
  22. return "-codec:a:0 copy";
  23. }
  24. var codec = GetAudioCodec(state.Request);
  25. var args = "-codec:a:0 " + codec;
  26. if (state.AudioStream != null)
  27. {
  28. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  29. if (channels.HasValue)
  30. {
  31. args += " -ac " + channels.Value;
  32. }
  33. if (state.Request.AudioSampleRate.HasValue)
  34. {
  35. args += " -ar " + state.Request.AudioSampleRate.Value;
  36. }
  37. if (state.Request.AudioBitRate.HasValue)
  38. {
  39. args += " -ab " + state.Request.AudioBitRate.Value;
  40. }
  41. return args;
  42. }
  43. return args;
  44. }
  45. /// <summary>
  46. /// Gets the video arguments.
  47. /// </summary>
  48. /// <param name="state">The state.</param>
  49. /// <returns>System.String.</returns>
  50. protected override string GetVideoArguments(StreamState state)
  51. {
  52. var codec = GetVideoCodec(state.Request);
  53. // Right now all we support is either h264 or copy
  54. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase) && !codec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  55. {
  56. codec = "libx264";
  57. }
  58. // See if we can save come cpu cycles by avoiding encoding
  59. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  60. {
  61. return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  62. }
  63. var args = "-codec:v:0 " + codec + " -preset superfast";
  64. if (state.Request.VideoBitRate.HasValue)
  65. {
  66. args += string.Format(" -b:v {0}", state.Request.VideoBitRate.Value);
  67. }
  68. // Add resolution params, if specified
  69. if (state.Request.Width.HasValue || state.Request.Height.HasValue || state.Request.MaxHeight.HasValue || state.Request.MaxWidth.HasValue)
  70. {
  71. args += GetOutputSizeParam(state, codec);
  72. }
  73. // Get the output framerate based on the FrameRate param
  74. double framerate = state.Request.Framerate ?? 0;
  75. // We have to supply a framerate for hls, so if it's null, account for that here
  76. if (framerate.Equals(0))
  77. {
  78. framerate = state.VideoStream.AverageFrameRate ?? 0;
  79. }
  80. if (framerate.Equals(0))
  81. {
  82. framerate = state.VideoStream.RealFrameRate ?? 0;
  83. }
  84. if (framerate.Equals(0))
  85. {
  86. framerate = 23.976;
  87. }
  88. args += string.Format(" -r {0}", framerate);
  89. // Needed to ensure segments stay under 10 seconds
  90. args += string.Format(" -g {0}", framerate);
  91. return args;
  92. }
  93. /// <summary>
  94. /// Gets the segment file extension.
  95. /// </summary>
  96. /// <param name="state">The state.</param>
  97. /// <returns>System.String.</returns>
  98. protected override string GetSegmentFileExtension(StreamState state)
  99. {
  100. return ".ts";
  101. }
  102. }
  103. }