Bladeren bron

better bitrate syncing

Luke Pulverenti 11 jaren geleden
bovenliggende
commit
a5fa31cf94

+ 26 - 0
MediaBrowser.Api/Playback/BaseStreamingService.cs

@@ -659,6 +659,32 @@ namespace MediaBrowser.Api.Playback
             }
         }
 
+        protected int? GetVideoBitrateParam(StreamState state)
+        {
+            if (state.VideoRequest.VideoBitRate.HasValue)
+            {
+                // 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;
+
+                return Math.Min(currentBitrate, state.VideoRequest.VideoBitRate.Value);
+            }
+
+            return null;
+        }
+
+        protected int? GetAudioBitrateParam(StreamState state)
+        {
+            if (state.Request.AudioBitRate.HasValue)
+            {
+                // Make sure we don't request a bitrate higher than the source
+                var currentBitrate = state.AudioStream == null ? state.Request.AudioBitRate.Value : state.AudioStream.BitRate ?? state.Request.AudioBitRate.Value;
+
+                return Math.Min(currentBitrate, state.Request.AudioBitRate.Value);
+            }
+
+            return null;
+        }
+
         /// <summary>
         /// Gets the user agent param.
         /// </summary>

+ 4 - 1
MediaBrowser.Api/Playback/Hls/BaseHlsService.cs

@@ -119,7 +119,10 @@ namespace MediaBrowser.Api.Playback.Hls
                 await WaitForMinimumSegmentCount(playlist, 3).ConfigureAwait(false);
             }
 
-            var playlistText = GetMasterPlaylistFileText(playlist, state.VideoRequest.VideoBitRate.Value + state.Request.AudioBitRate.Value);
+            var audioBitrate = GetAudioBitrateParam(state) ?? 0;
+            var videoBitrate = GetVideoBitrateParam(state) ?? 0;
+
+            var playlistText = GetMasterPlaylistFileText(playlist, videoBitrate + audioBitrate);
 
             try
             {

+ 8 - 9
MediaBrowser.Api/Playback/Hls/VideoHlsService.cs

@@ -146,9 +146,11 @@ namespace MediaBrowser.Api.Playback.Hls
                     args += " -ar " + state.Request.AudioSampleRate.Value;
                 }
 
-                if (state.Request.AudioBitRate.HasValue)
+                var bitrate = GetAudioBitrateParam(state);
+
+                if (bitrate.HasValue)
                 {
-                    args += " -ab " + state.Request.AudioBitRate.Value;
+                    args += " -ab " + bitrate.Value.ToString(UsCulture);
                 }
 
                 var volParam = string.Empty;
@@ -187,14 +189,11 @@ namespace MediaBrowser.Api.Playback.Hls
 
             var args = "-codec:v:0 " + codec + " -preset superfast" + keyFrameArg;
 
-            if (state.VideoRequest.VideoBitRate.HasValue)
-            {
-                // 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);
+            var bitrate = GetVideoBitrateParam(state);
 
-                args += string.Format(" -b:v {0}", bitrate);
+            if (bitrate.HasValue)
+            {
+                args += string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
             }
             
             // Add resolution params, if specified

+ 4 - 2
MediaBrowser.Api/Playback/Progressive/AudioService.cs

@@ -93,9 +93,11 @@ namespace MediaBrowser.Api.Playback.Progressive
                 audioTranscodeParams.Add("-strict experimental");
             }
 
-            if (request.AudioBitRate.HasValue)
+            var bitrate = GetAudioBitrateParam(state);
+
+            if (bitrate.HasValue)
             {
-                audioTranscodeParams.Add("-ab " + request.AudioBitRate.Value);
+                audioTranscodeParams.Add("-ab " + bitrate.Value.ToString(UsCulture));
             }
 
             var channels = GetNumAudioChannelsParam(request, state.AudioStream);

+ 9 - 10
MediaBrowser.Api/Playback/Progressive/VideoService.cs

@@ -236,9 +236,11 @@ namespace MediaBrowser.Api.Playback.Progressive
                 args += " -ar " + request.AudioSampleRate.Value;
             }
 
-            if (request.AudioBitRate.HasValue)
+            var bitrate = GetAudioBitrateParam(state);
+
+            if (bitrate.HasValue)
             {
-                args += " -ab " + request.AudioBitRate.Value;
+                args += " -ab " + bitrate.Value.ToString(UsCulture);
             }
 
             var volParam = string.Empty;
@@ -283,16 +285,13 @@ namespace MediaBrowser.Api.Playback.Progressive
             else if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase))
             {
                 args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
-            } 
-            
-            if (state.VideoRequest.VideoBitRate.HasValue)
-            {
-                // 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);
+            var bitrate = GetVideoBitrateParam(state);
 
-                args += " -b:v " + bitrate;
+            if (bitrate.HasValue)
+            {
+                args += " -b:v " + bitrate.Value.ToString(UsCulture);
             }
 
             return args.Trim();