HlsVideoPlaylistHandler.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using MediaBrowser.Common.Net.Handlers;
  2. using MediaBrowser.Controller.Entities;
  3. using System;
  4. using System.ComponentModel.Composition;
  5. using System.Net;
  6. namespace MediaBrowser.Api.Streaming
  7. {
  8. /// <summary>
  9. /// Class HlsVideoPlaylistHandler
  10. /// </summary>
  11. [Export(typeof(IHttpServerHandler))]
  12. public class HlsVideoPlaylistHandler : BaseHlsPlaylistHandler<Video>
  13. {
  14. /// <summary>
  15. /// Handleses the request.
  16. /// </summary>
  17. /// <param name="request">The request.</param>
  18. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  19. public override bool HandlesRequest(HttpListenerRequest request)
  20. {
  21. return ApiService.IsApiUrlMatch("video.m3u8", request);
  22. }
  23. /// <summary>
  24. /// Gets the segment file extension.
  25. /// </summary>
  26. /// <value>The segment file extension.</value>
  27. protected override string SegmentFileExtension
  28. {
  29. get { return ".ts"; }
  30. }
  31. /// <summary>
  32. /// Gets the video arguments.
  33. /// </summary>
  34. /// <returns>System.String.</returns>
  35. protected override string GetVideoArguments()
  36. {
  37. var codec = GetVideoCodec();
  38. // Right now all we support is either h264 or copy
  39. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase) && !codec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  40. {
  41. codec = "libx264";
  42. }
  43. // See if we can save come cpu cycles by avoiding encoding
  44. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  45. {
  46. return IsH264(VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  47. }
  48. var args = "-codec:v:0 " + codec + " -preset superfast";
  49. if (VideoBitRate.HasValue)
  50. {
  51. args += string.Format(" -b:v {0}", VideoBitRate.Value);
  52. }
  53. // Add resolution params, if specified
  54. if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue)
  55. {
  56. args += GetOutputSizeParam(codec);
  57. }
  58. // Get the output framerate based on the FrameRate param
  59. double framerate = FrameRate ?? 0;
  60. // We have to supply a framerate for hls, so if it's null, account for that here
  61. if (framerate.Equals(0))
  62. {
  63. framerate = VideoStream.AverageFrameRate ?? 0;
  64. }
  65. if (framerate.Equals(0))
  66. {
  67. framerate = VideoStream.RealFrameRate ?? 0;
  68. }
  69. if (framerate.Equals(0))
  70. {
  71. framerate = 23.976;
  72. }
  73. args += string.Format(" -r {0}", framerate);
  74. // Needed to ensure segments stay under 10 seconds
  75. args += string.Format(" -g {0}", framerate);
  76. return args;
  77. }
  78. /// <summary>
  79. /// Gets the audio arguments to pass to ffmpeg
  80. /// </summary>
  81. /// <returns>System.String.</returns>
  82. protected override string GetAudioArguments()
  83. {
  84. if (!AudioCodec.HasValue)
  85. {
  86. return "-codec:a:0 copy";
  87. }
  88. var codec = GetAudioCodec();
  89. var args = "-codec:a:0 " + codec;
  90. if (AudioStream != null)
  91. {
  92. var channels = GetNumAudioChannelsParam();
  93. if (channels.HasValue)
  94. {
  95. args += " -ac " + channels.Value;
  96. }
  97. var sampleRate = GetSampleRateParam();
  98. if (sampleRate.HasValue)
  99. {
  100. args += " -ar " + sampleRate.Value;
  101. }
  102. if (AudioBitRate.HasValue)
  103. {
  104. args += " -ab " + AudioBitRate.Value;
  105. }
  106. return args;
  107. }
  108. return args;
  109. }
  110. }
  111. }