From 50fbc9cadfda52e60c75e45358d97d30291a90c6 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Fri, 11 Apr 2025 15:27:13 +0800 Subject: [PATCH] [fix] put resolution converter into task queue --- src/common/any_invocable.h | 2 + src/common/task_queue.h | 2 - src/transport/channel/rtp_video_sender.cpp | 2 +- src/transport/ice_transport_controller.cpp | 52 +++++++++++----------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/common/any_invocable.h b/src/common/any_invocable.h index b393ba6..454c00b 100644 --- a/src/common/any_invocable.h +++ b/src/common/any_invocable.h @@ -37,6 +37,8 @@ class AnyInvocable { // 移动赋值运算符 AnyInvocable& operator=(AnyInvocable&&) = default; + explicit operator bool() const { return static_cast(callable_); } + private: // 抽象基类,允许不同类型的可调用对象 struct CallableBase { diff --git a/src/common/task_queue.h b/src/common/task_queue.h index 65d85fb..0f4870c 100644 --- a/src/common/task_queue.h +++ b/src/common/task_queue.h @@ -104,8 +104,6 @@ class TaskQueue { [this]() { return stop_; }); } - if (stop_ && taskQueue_.empty()) return; - task = std::move( const_cast &>(taskQueue_.top().task)); taskQueue_.pop(); diff --git a/src/transport/channel/rtp_video_sender.cpp b/src/transport/channel/rtp_video_sender.cpp index 95dd2f4..243c535 100644 --- a/src/transport/channel/rtp_video_sender.cpp +++ b/src/transport/channel/rtp_video_sender.cpp @@ -53,7 +53,7 @@ void RtpVideoSender::Enqueue( to_send_rtp_packets.push_back(std::move(rtp_packet_to_send)); } - enqueue_packets_func_(std::move(to_send_rtp_packets)); + enqueue_packets_func_(to_send_rtp_packets); } void RtpVideoSender::SetSendDataFunc( diff --git a/src/transport/ice_transport_controller.cpp b/src/transport/ice_transport_controller.cpp index eb70cf5..f46a677 100644 --- a/src/transport/ice_transport_controller.cpp +++ b/src/transport/ice_transport_controller.cpp @@ -203,33 +203,33 @@ int IceTransportController::SendVideo(const XVideoFrame* video_frame) { b_force_i_frame_ = false; } - XVideoFrame new_frame; - new_frame.data = nullptr; - new_frame.width = video_frame->width; - new_frame.height = video_frame->height; - new_frame.size = video_frame->size; - new_frame.captured_timestamp = video_frame->captured_timestamp; - if (target_width_.has_value() && target_height_.has_value()) { - if (target_width_.value() < video_frame->width && - target_height_.value() < video_frame->height) { - resolution_adapter_->ResolutionDowngrade( - video_frame, target_width_.value(), target_height_.value(), - &new_frame); - } else { - new_frame.data = new char[video_frame->size]; - memcpy((void*)new_frame.data, (void*)video_frame->data, - video_frame->size); - } - } - - RawFrame raw_frame((const uint8_t*)new_frame.data, new_frame.size, - new_frame.width, new_frame.height); - raw_frame.SetCapturedTimestamp(video_frame->captured_timestamp); - - delete[] new_frame.data; - if (task_queue_encode_ && video_encoder_) { - task_queue_encode_->PostTask([this, raw_frame]() mutable { + auto video_frame_copy = std::make_shared(*video_frame); + task_queue_encode_->PostTask([this, video_frame_copy]() mutable { + XVideoFrame new_frame; + new_frame.data = nullptr; + new_frame.width = video_frame_copy->width; + new_frame.height = video_frame_copy->height; + new_frame.size = video_frame_copy->size; + new_frame.captured_timestamp = video_frame_copy->captured_timestamp; + if (target_width_.has_value() && target_height_.has_value()) { + if (target_width_.value() < video_frame_copy->width && + target_height_.value() < video_frame_copy->height) { + resolution_adapter_->ResolutionDowngrade( + video_frame_copy.get(), target_width_.value(), + target_height_.value(), &new_frame); + } else { + new_frame.data = new char[video_frame_copy->size]; + memcpy((void*)new_frame.data, (void*)video_frame_copy->data, + video_frame_copy->size); + } + } + + RawFrame raw_frame((const uint8_t*)new_frame.data, new_frame.size, + new_frame.width, new_frame.height); + raw_frame.SetCapturedTimestamp(video_frame_copy->captured_timestamp); + delete[] new_frame.data; + int ret = video_encoder_->Encode( std::move(raw_frame), [this](const EncodedFrame& encoded_frame) -> int {