From 1b649a889950362a1e59e08264fdd2a9e3e88552 Mon Sep 17 00:00:00 2001 From: Marina Ciocea Date: Fri, 29 May 2020 12:37:01 +0200 Subject: [PATCH] [M84][InsertableStreams] Send transformed frames on worker queue. When video frame encoding is done on an external thread (for example in the case of hardware encoders), the WebRTC TaskQueueBase::Current() is null; in this case use the worker queue instead to send transformed frames. (cherry picked from commit 2e69660b3e628bb86839cee75c54d9f7cb98cde3) Bug: chromium:1086373 Change-Id: I903ddc52ad6832557fc5b5f76396fe26cf5a88f3 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/176303 Reviewed-by: Magnus Flodman Reviewed-by: Danil Chapovalov Commit-Queue: Marina Ciocea Cr-Original-Commit-Position: refs/heads/master@{#31388} TBR: marinaciocea@webrtc.org Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/176844 Commit-Queue: Guido Urdaneta Reviewed-by: Guido Urdaneta Cr-Commit-Position: refs/branch-heads/4147@{#6} Cr-Branched-From: 2b7d96959916306cb267899a219cbd20ec24b841-refs/heads/master@{#31262} --- call/rtp_video_sender.cc | 2 ++ modules/rtp_rtcp/source/rtp_sender_video.cc | 3 ++- modules/rtp_rtcp/source/rtp_sender_video.h | 2 ++ ...tp_sender_video_frame_transformer_delegate.cc | 16 ++++++++++++---- ...rtp_sender_video_frame_transformer_delegate.h | 4 +++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/call/rtp_video_sender.cc b/call/rtp_video_sender.cc index b6cb054488..ca8baee2b0 100644 --- a/call/rtp_video_sender.cc +++ b/call/rtp_video_sender.cc @@ -30,6 +30,7 @@ #include "rtc_base/checks.h" #include "rtc_base/location.h" #include "rtc_base/logging.h" +#include "rtc_base/task_queue.h" namespace webrtc { @@ -281,6 +282,7 @@ std::vector CreateRtpStreamSenders( video_config.fec_overhead_bytes = fec_generator->MaxPacketOverhead(); } video_config.frame_transformer = frame_transformer; + video_config.worker_queue = transport->GetWorkerQueue()->Get(); auto sender_video = std::make_unique(video_config); rtp_streams.emplace_back(std::move(rtp_rtcp), std::move(sender_video), std::move(fec_generator)); diff --git a/modules/rtp_rtcp/source/rtp_sender_video.cc b/modules/rtp_rtcp/source/rtp_sender_video.cc index b903b9f001..02c4eb8d28 100644 --- a/modules/rtp_rtcp/source/rtp_sender_video.cc +++ b/modules/rtp_rtcp/source/rtp_sender_video.cc @@ -144,7 +144,8 @@ RTPSenderVideo::RTPSenderVideo(const Config& config) RTPSenderVideoFrameTransformerDelegate>( this, config.frame_transformer, - rtp_sender_->SSRC()) + rtp_sender_->SSRC(), + config.worker_queue) : nullptr) { if (frame_transformer_delegate_) frame_transformer_delegate_->Init(); diff --git a/modules/rtp_rtcp/source/rtp_sender_video.h b/modules/rtp_rtcp/source/rtp_sender_video.h index bf5f181823..216f16faf6 100644 --- a/modules/rtp_rtcp/source/rtp_sender_video.h +++ b/modules/rtp_rtcp/source/rtp_sender_video.h @@ -20,6 +20,7 @@ #include "api/array_view.h" #include "api/frame_transformer_interface.h" #include "api/scoped_refptr.h" +#include "api/task_queue/task_queue_base.h" #include "api/transport/rtp/dependency_descriptor.h" #include "api/video/video_codec_type.h" #include "api/video/video_frame_type.h" @@ -81,6 +82,7 @@ class RTPSenderVideo { absl::optional red_payload_type; const WebRtcKeyValueConfig* field_trials = nullptr; rtc::scoped_refptr frame_transformer; + TaskQueueBase* worker_queue = nullptr; }; explicit RTPSenderVideo(const Config& config); diff --git a/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc b/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc index 25ebd1b64c..51fbdb0165 100644 --- a/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc +++ b/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc @@ -105,10 +105,12 @@ class TransformableVideoSenderFrame : public TransformableVideoFrameInterface { RTPSenderVideoFrameTransformerDelegate::RTPSenderVideoFrameTransformerDelegate( RTPSenderVideo* sender, rtc::scoped_refptr frame_transformer, - uint32_t ssrc) + uint32_t ssrc, + TaskQueueBase* worker_queue) : sender_(sender), frame_transformer_(std::move(frame_transformer)), - ssrc_(ssrc) {} + ssrc_(ssrc), + worker_queue_(worker_queue) {} void RTPSenderVideoFrameTransformerDelegate::Init() { frame_transformer_->RegisterTransformedFrameSinkCallback( @@ -123,8 +125,14 @@ bool RTPSenderVideoFrameTransformerDelegate::TransformFrame( const RTPFragmentationHeader* fragmentation, RTPVideoHeader video_header, absl::optional expected_retransmission_time_ms) { - if (!encoder_queue_) - encoder_queue_ = TaskQueueBase::Current(); + if (!encoder_queue_) { + // Save the current task queue to post the transformed frame for sending + // once it is transformed. When there is no current task queue, i.e. + // encoding is done on an external thread (for example in the case of + // hardware encoders), use the worker queue instead. + TaskQueueBase* current = TaskQueueBase::Current(); + encoder_queue_ = current ? current : worker_queue_; + } frame_transformer_->Transform(std::make_unique( encoded_image, video_header, payload_type, codec_type, rtp_timestamp, fragmentation, expected_retransmission_time_ms, ssrc_)); diff --git a/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.h b/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.h index 29ac9e4e1c..bea5ba7b65 100644 --- a/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.h +++ b/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.h @@ -30,7 +30,8 @@ class RTPSenderVideoFrameTransformerDelegate : public TransformedFrameCallback { RTPSenderVideoFrameTransformerDelegate( RTPSenderVideo* sender, rtc::scoped_refptr frame_transformer, - uint32_t ssrc); + uint32_t ssrc, + TaskQueueBase* worker_queue); void Init(); @@ -69,6 +70,7 @@ class RTPSenderVideoFrameTransformerDelegate : public TransformedFrameCallback { rtc::scoped_refptr frame_transformer_; const uint32_t ssrc_; TaskQueueBase* encoder_queue_ = nullptr; + TaskQueueBase* worker_queue_; }; } // namespace webrtc