From c7a46c49a05df117aa73644ab1c9c325ea6938cb Mon Sep 17 00:00:00 2001 From: philipel Date: Mon, 25 Nov 2019 15:01:09 +0100 Subject: [PATCH] Fix VideoStreamEncoder to not reference encoded data from the RunPostEncode task. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:9378 Change-Id: I1ada7018507d0c78fee51523f8cd4fab76c35432 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/160306 Commit-Queue: Philip Eliasson Reviewed-by: Niels Moller Reviewed-by: Erik Språng Cr-Commit-Position: refs/heads/master@{#29903} --- api/video/encoded_image.h | 7 +++++++ video/video_stream_encoder.cc | 21 +++++++++++---------- video/video_stream_encoder.h | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/api/video/encoded_image.h b/api/video/encoded_image.h index ffb1adf830..1fa2b0455b 100644 --- a/api/video/encoded_image.h +++ b/api/video/encoded_image.h @@ -146,6 +146,13 @@ class RTC_EXPORT EncodedImage { buffer_ = nullptr; } + void ClearEncodedData() { + encoded_data_ = nullptr; + size_ = 0; + buffer_ = nullptr; + capacity_ = 0; + } + // TODO(nisse): Delete, provide only read-only access to the buffer. uint8_t* data() { return buffer_ ? buffer_ diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc index 05615f6652..dd557d58ee 100644 --- a/video/video_stream_encoder.cc +++ b/video/video_stream_encoder.cc @@ -1737,7 +1737,8 @@ EncodedImageCallback::Result VideoStreamEncoder::OnEncodedImage( // We are only interested in propagating the meta-data about the image, not // encoded data itself, to the post encode function. Since we cannot be sure // the pointer will still be valid when run on the task queue, set it to null. - image_copy.set_buffer(nullptr, 0); + DataSize frame_size = DataSize::bytes(image_copy.size()); + image_copy.ClearEncodedData(); int temporal_index = 0; if (codec_specific_info) { @@ -1751,7 +1752,7 @@ EncodedImageCallback::Result VideoStreamEncoder::OnEncodedImage( temporal_index = 0; } - RunPostEncode(image_copy, rtc::TimeMicros(), temporal_index); + RunPostEncode(image_copy, rtc::TimeMicros(), temporal_index, frame_size); if (result.error == Result::OK) { // In case of an internal encoder running on a separate thread, the @@ -2207,12 +2208,13 @@ VideoStreamEncoder::GetConstAdaptCounter() { void VideoStreamEncoder::RunPostEncode(EncodedImage encoded_image, int64_t time_sent_us, - int temporal_index) { + int temporal_index, + DataSize frame_size) { if (!encoder_queue_.IsCurrent()) { - encoder_queue_.PostTask( - [this, encoded_image, time_sent_us, temporal_index] { - RunPostEncode(encoded_image, time_sent_us, temporal_index); - }); + encoder_queue_.PostTask([this, encoded_image, time_sent_us, temporal_index, + frame_size] { + RunPostEncode(encoded_image, time_sent_us, temporal_index, frame_size); + }); return; } @@ -2229,12 +2231,11 @@ void VideoStreamEncoder::RunPostEncode(EncodedImage encoded_image, // Run post encode tasks, such as overuse detection and frame rate/drop // stats for internal encoders. - const size_t frame_size = encoded_image.size(); const bool keyframe = encoded_image._frameType == VideoFrameType::kVideoFrameKey; - if (frame_size > 0) { - frame_dropper_.Fill(frame_size, !keyframe); + if (!frame_size.IsZero()) { + frame_dropper_.Fill(frame_size.bytes(), !keyframe); } if (HasInternalSource()) { diff --git a/video/video_stream_encoder.h b/video/video_stream_encoder.h index 149fcd647b..12cc689b34 100644 --- a/video/video_stream_encoder.h +++ b/video/video_stream_encoder.h @@ -229,7 +229,8 @@ class VideoStreamEncoder : public VideoStreamEncoderInterface, RTC_RUN_ON(&encoder_queue_); void RunPostEncode(EncodedImage encoded_image, int64_t time_sent_us, - int temporal_index); + int temporal_index, + DataSize frame_size); bool HasInternalSource() const RTC_RUN_ON(&encoder_queue_); void ReleaseEncoder() RTC_RUN_ON(&encoder_queue_);