From e031c7bcaaa7e15be839f2329e7e2721966af302 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 2 Jun 2013 10:47:28 -0400 Subject: [PATCH] don't request a higher bitrate than source content --- MediaBrowser.Api/Playback/Hls/VideoHlsService.cs | 9 +++++++-- .../Playback/Progressive/VideoService.cs | 16 ++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs index 8273335989..19c7d6e05c 100644 --- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -158,9 +158,14 @@ namespace MediaBrowser.Api.Playback.Hls if (state.VideoRequest.VideoBitRate.HasValue) { - args += string.Format(" -b:v {0}", state.VideoRequest.VideoBitRate.Value); - } + // Make sure we don't request a bitrate higher than the source + var currentBitrate = state.VideoStream == null ? state.VideoRequest.VideoBitRate.Value : state.VideoStream.BitRate ?? state.VideoRequest.VideoBitRate.Value; + + var bitrate = Math.Min(currentBitrate, state.VideoRequest.VideoBitRate.Value); + args += string.Format(" -b:v {0}", bitrate); + } + // Add resolution params, if specified if (state.VideoRequest.Width.HasValue || state.VideoRequest.Height.HasValue || state.VideoRequest.MaxHeight.HasValue || state.VideoRequest.MaxWidth.HasValue) { diff --git a/MediaBrowser.Api/Playback/Progressive/VideoService.cs b/MediaBrowser.Api/Playback/Progressive/VideoService.cs index 3e92c96ec4..80ea77d8e6 100644 --- a/MediaBrowser.Api/Playback/Progressive/VideoService.cs +++ b/MediaBrowser.Api/Playback/Progressive/VideoService.cs @@ -160,8 +160,7 @@ namespace MediaBrowser.Api.Playback.Progressive args += string.Format(" -r {0}", request.Framerate.Value); } - // Add the audio bitrate - var qualityParam = GetVideoQualityParam(request, codec); + var qualityParam = GetVideoQualityParam(state, codec); if (!string.IsNullOrEmpty(qualityParam)) { @@ -256,10 +255,10 @@ namespace MediaBrowser.Api.Playback.Progressive /// /// Gets the video bitrate to specify on the command line /// - /// The request. + /// The state. /// The video codec. /// System.String. - private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec) + private string GetVideoQualityParam(StreamState state, string videoCodec) { var args = string.Empty; @@ -284,9 +283,14 @@ namespace MediaBrowser.Api.Playback.Progressive args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2"; } - if (request.VideoBitRate.HasValue) + if (state.VideoRequest.VideoBitRate.HasValue) { - args += " -b:v " + request.VideoBitRate; + // Make sure we don't request a bitrate higher than the source + var currentBitrate = state.VideoStream == null ? state.VideoRequest.VideoBitRate.Value : state.VideoStream.BitRate ?? state.VideoRequest.VideoBitRate.Value; + + var bitrate = Math.Min(currentBitrate, state.VideoRequest.VideoBitRate.Value); + + args += " -b:v " + bitrate; } return args.Trim();