From 38c88e27e07be9eb80dda0ac09745a77531e566a Mon Sep 17 00:00:00 2001 From: dijunkun Date: Wed, 16 Apr 2025 17:22:04 +0800 Subject: [PATCH] [feat] use original resolution ratio when upgrade/downgrade source frame --- .../resolution_adapter/resolution_adapter.cpp | 18 ++++++++++++++++-- src/media/video/encode/aom/aom_av1_encoder.h | 4 ++++ .../encode/nvcodec/nvidia_video_encoder.h | 4 ++++ .../video/encode/openh264/openh264_encoder.h | 4 ++++ src/transport/ice_transport_controller.cpp | 6 +++++- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/media/resolution_adapter/resolution_adapter.cpp b/src/media/resolution_adapter/resolution_adapter.cpp index c9d0996..9748952 100644 --- a/src/media/resolution_adapter/resolution_adapter.cpp +++ b/src/media/resolution_adapter/resolution_adapter.cpp @@ -15,8 +15,22 @@ int ResolutionAdapter::GetResolution(int target_bitrate, int current_width, for (auto& resolution : GetBitrateLimits()) { if (target_bitrate >= resolution.min_start_bitrate_bps && target_bitrate < resolution.max_bitrate_bps) { - *target_width = resolution.width; - *target_height = resolution.height; + // Adjust the resolution to maintain the same aspect ratio as the current + // resolution + float aspect_ratio = static_cast(current_width) / current_height; + if (static_cast(resolution.width) / resolution.height != + aspect_ratio) { + if (aspect_ratio > 1.0f) { + *target_width = resolution.width; + *target_height = static_cast(resolution.width / aspect_ratio); + } else { + *target_height = resolution.height; + *target_width = static_cast(resolution.height * aspect_ratio); + } + } else { + *target_width = resolution.width; + *target_height = resolution.height; + } return 0; } } diff --git a/src/media/video/encode/aom/aom_av1_encoder.h b/src/media/video/encode/aom/aom_av1_encoder.h index 5920a5d..1e6fd47 100644 --- a/src/media/video/encode/aom/aom_av1_encoder.h +++ b/src/media/video/encode/aom/aom_av1_encoder.h @@ -46,6 +46,10 @@ class AomAv1Encoder : public VideoEncoder { int SetTargetBitrate(int bitrate); int GetResolution(int* width, int* height) { + if (seq_ == 0) { + return -1; + } + *width = frame_width_; *height = frame_height_; return 0; diff --git a/src/media/video/encode/nvcodec/nvidia_video_encoder.h b/src/media/video/encode/nvcodec/nvidia_video_encoder.h index 6432d0c..4970646 100644 --- a/src/media/video/encode/nvcodec/nvidia_video_encoder.h +++ b/src/media/video/encode/nvcodec/nvidia_video_encoder.h @@ -23,6 +23,10 @@ class NvidiaVideoEncoder : public VideoEncoder { int SetTargetBitrate(int bitrate); int GetResolution(int* width, int* height) { + if (seq_ == 0) { + return -1; + } + *width = frame_width_; *height = frame_height_; return 0; diff --git a/src/media/video/encode/openh264/openh264_encoder.h b/src/media/video/encode/openh264/openh264_encoder.h index ae85a6e..48b48b3 100644 --- a/src/media/video/encode/openh264/openh264_encoder.h +++ b/src/media/video/encode/openh264/openh264_encoder.h @@ -33,6 +33,10 @@ class OpenH264Encoder : public VideoEncoder { int SetTargetBitrate(int bitrate); int GetResolution(int* width, int* height) { + if (seq_ == 0) { + return -1; + } + *width = frame_width_; *height = frame_height_; return 0; diff --git a/src/transport/ice_transport_controller.cpp b/src/transport/ice_transport_controller.cpp index 78a4333..207572b 100644 --- a/src/transport/ice_transport_controller.cpp +++ b/src/transport/ice_transport_controller.cpp @@ -600,7 +600,11 @@ void IceTransportController::PostUpdates(webrtc::NetworkControlUpdate update) { if (target_bitrate != target_bitrate_ && video_encoder_) { target_bitrate_ = target_bitrate; int width, height, target_width, target_height; - video_encoder_->GetResolution(&width, &height); + int valid = video_encoder_->GetResolution(&width, &height); + if (valid != 0) { + width = source_width_; + height = source_height_; + } if (0 == resolution_adapter_->GetResolution(target_bitrate_, width, height, &target_width, &target_height)) {