From 8b4ff8cd1fccdf05885d6eafe598c59bdaf9dec9 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Mon, 10 Mar 2025 11:24:53 +0800 Subject: [PATCH] [fix] fix resolution adapter --- src/media/nvcodec/NvDecoder.cpp | 4 +-- .../resolution_adapter/resolution_adapter.cpp | 29 +++++++------------ .../resolution_adapter/resolution_adapter.h | 13 ++++----- .../resolution_bitrate_limits.h | 8 +++-- .../encode/nvcodec/nvidia_video_encoder.cpp | 1 - src/transport/ice_transport_controller.cpp | 8 ++--- 6 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/media/nvcodec/NvDecoder.cpp b/src/media/nvcodec/NvDecoder.cpp index 5c0109d..f4eb68a 100644 --- a/src/media/nvcodec/NvDecoder.cpp +++ b/src/media/nvcodec/NvDecoder.cpp @@ -681,8 +681,8 @@ int NvDecoder::HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo) { if (result == CUDA_SUCCESS && (DecodeStatus.decodeStatus == cuvidDecodeStatus_Error || DecodeStatus.decodeStatus == cuvidDecodeStatus_Error_Concealed)) { - printf("Decode Error occurred for picture %d\n", - m_nPicNumInDecodeOrder[pDispInfo->picture_index]); + // printf("Decode Error occurred for picture %d\n", + // m_nPicNumInDecodeOrder[pDispInfo->picture_index]); } uint8_t *pDecodedFrame = nullptr; diff --git a/src/media/resolution_adapter/resolution_adapter.cpp b/src/media/resolution_adapter/resolution_adapter.cpp index f2ae930..0d47ec4 100644 --- a/src/media/resolution_adapter/resolution_adapter.cpp +++ b/src/media/resolution_adapter/resolution_adapter.cpp @@ -6,31 +6,24 @@ int ResolutionAdapter::GetResolution(int target_bitrate, int current_width, int current_height, int* target_width, int* target_height) { + if (target_bitrate < GetBitrateLimits().front().min_start_bitrate_bps) { + *target_width = GetBitrateLimits().front().width; + *target_height = GetBitrateLimits().front().height; + return 0; + } + for (auto& resolution : GetBitrateLimits()) { if (target_bitrate >= resolution.min_start_bitrate_bps && - target_bitrate <= resolution.max_bitrate_bps) { - if (current_width * current_height <= resolution.frame_size_pixels) { - *target_width = current_width; - *target_height = current_height; - LOG_INFO("1 source resolution {}x{}, target resolution {}x{}", - current_width, current_height, *target_width, *target_height); - return 0; - } else { - *target_width = current_width * 3 / 5; - *target_height = current_height * 3 / 5; - LOG_INFO("2 source resolution {}x{}, target resolution {}x{}", - current_width, current_height, *target_width, *target_height); - return 0; - } + target_bitrate < resolution.max_bitrate_bps) { + *target_width = resolution.width; + *target_height = resolution.height; + return 0; } } *target_width = -1; *target_height = -1; - LOG_INFO("3 source resolution {}x{}, target resolution {}x{}", current_width, - current_height, *target_width, *target_height); - return -1; } @@ -41,8 +34,6 @@ int ResolutionAdapter::ResolutionDowngrade(const XVideoFrame* video_frame, return -1; } - target_width = video_frame->width * 3 / 5; - target_height = video_frame->height * 3 / 5; new_frame->width = target_width; new_frame->height = target_height; new_frame->data = new char[target_width * target_height * 3 / 2]; diff --git a/src/media/resolution_adapter/resolution_adapter.h b/src/media/resolution_adapter/resolution_adapter.h index bd9c336..1bff174 100644 --- a/src/media/resolution_adapter/resolution_adapter.h +++ b/src/media/resolution_adapter/resolution_adapter.h @@ -26,13 +26,12 @@ class ResolutionAdapter { public: std::vector GetBitrateLimits() { - return {{0 * 0, 0, 0, 0}, - {320 * 180, 0, 30000, 300000}, - {480 * 270, 300000, 30000, 500000}, - {640 * 360, 500000, 30000, 800000}, - {960 * 540, 800000, 30000, 1500000}, - {1280 * 720, 1500000, 30000, 2500000}, - {1920 * 1080, 2500000, 30000, 4000000}}; + return {{320, 180, 0, 30000, 300000}, + {480, 270, 300000, 30000, 500000}, + {640, 360, 500000, 30000, 800000}, + {960, 540, 800000, 30000, 1500000}, + {1280, 720, 1500000, 30000, 2500000}, + {1920, 1080, 2500000, 30000, 4000000}}; } int SetTargetBitrate(int bitrate); diff --git a/src/media/resolution_adapter/resolution_bitrate_limits.h b/src/media/resolution_adapter/resolution_bitrate_limits.h index f01c3e0..7fbe53e 100644 --- a/src/media/resolution_adapter/resolution_bitrate_limits.h +++ b/src/media/resolution_adapter/resolution_bitrate_limits.h @@ -8,13 +8,15 @@ #define _RESOLUTION_BITRATE_LIMITS_H_ struct ResolutionBitrateLimits { - ResolutionBitrateLimits(int frame_size_pixels, int min_start_bitrate_bps, + ResolutionBitrateLimits(int width, int height, int min_start_bitrate_bps, int min_bitrate_bps, int max_bitrate_bps) - : frame_size_pixels(frame_size_pixels), + : width(width), + height(height), min_start_bitrate_bps(min_start_bitrate_bps), min_bitrate_bps(min_bitrate_bps), max_bitrate_bps(max_bitrate_bps) {} - int frame_size_pixels = 0; + int width = 0; + int height = 0; int min_start_bitrate_bps = 0; int min_bitrate_bps = 0; int max_bitrate_bps = 0; diff --git a/src/media/video/encode/nvcodec/nvidia_video_encoder.cpp b/src/media/video/encode/nvcodec/nvidia_video_encoder.cpp index c7b4881..7b7dfd5 100644 --- a/src/media/video/encode/nvcodec/nvidia_video_encoder.cpp +++ b/src/media/video/encode/nvcodec/nvidia_video_encoder.cpp @@ -263,7 +263,6 @@ int NvidiaVideoEncoder::ResetEncodeResolution(unsigned int width, frame_width_ = width; frame_height_ = height; - LOG_WARN("Reset resolution to [{}x{}]", frame_width_, frame_height_); NV_ENC_RECONFIGURE_PARAMS reconfig_params = {NV_ENC_RECONFIGURE_PARAMS_VER}; NV_ENC_INITIALIZE_PARAMS init_params = {NV_ENC_INITIALIZE_PARAMS_VER}; diff --git a/src/transport/ice_transport_controller.cpp b/src/transport/ice_transport_controller.cpp index 1689768..b2be53a 100644 --- a/src/transport/ice_transport_controller.cpp +++ b/src/transport/ice_transport_controller.cpp @@ -13,8 +13,6 @@ IceTransportController::IceTransportController( : last_report_block_time_( webrtc::Timestamp::Millis(webrtc_clock_->TimeInMilliseconds())), b_force_i_frame_(true), - target_width_(1280), - target_height_(720), video_codec_inited_(false), audio_codec_inited_(false), load_nvcodec_dll_success_(false), @@ -472,13 +470,13 @@ void IceTransportController::PostUpdates(webrtc::NetworkControlUpdate update) { target_height_ = target_height; b_force_i_frame_ = true; - LOG_WARN("Set target resolution [{}x{}]", target_width_.value(), + LOG_INFO("Set target resolution [{}x{}]", target_width_.value(), target_height_.value()); } - } else { + } else if (target_width_.has_value() && target_height_.has_value()) { target_width_.reset(); target_height_.reset(); - LOG_WARN("Use original resolution [{}x{}]", source_width_, + LOG_INFO("Use original resolution [{}x{}]", source_width_, source_height_); } video_encoder_->SetTargetBitrate(target_bitrate_);