1 /*
2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "test/pc/e2e/analyzer/video/quality_analyzing_video_decoder.h"
12
13 #include <cstdint>
14 #include <cstring>
15 #include <memory>
16 #include <utility>
17
18 #include "absl/strings/string_view.h"
19 #include "absl/types/optional.h"
20 #include "api/video/i420_buffer.h"
21 #include "modules/video_coding/include/video_error_codes.h"
22 #include "rtc_base/logging.h"
23 #include "test/pc/e2e/analyzer/video/simulcast_dummy_buffer_helper.h"
24
25 namespace webrtc {
26 namespace webrtc_pc_e2e {
27
QualityAnalyzingVideoDecoder(int id,absl::string_view peer_name,std::unique_ptr<VideoDecoder> delegate,EncodedImageDataExtractor * extractor,VideoQualityAnalyzerInterface * analyzer)28 QualityAnalyzingVideoDecoder::QualityAnalyzingVideoDecoder(
29 int id,
30 absl::string_view peer_name,
31 std::unique_ptr<VideoDecoder> delegate,
32 EncodedImageDataExtractor* extractor,
33 VideoQualityAnalyzerInterface* analyzer)
34 : id_(id),
35 peer_name_(peer_name),
36 implementation_name_("AnalyzingDecoder-" +
37 std::string(delegate->ImplementationName())),
38 delegate_(std::move(delegate)),
39 extractor_(extractor),
40 analyzer_(analyzer) {
41 analyzing_callback_ = std::make_unique<DecoderCallback>(this);
42 }
43 QualityAnalyzingVideoDecoder::~QualityAnalyzingVideoDecoder() = default;
44
InitDecode(const VideoCodec * codec_settings,int32_t number_of_cores)45 int32_t QualityAnalyzingVideoDecoder::InitDecode(
46 const VideoCodec* codec_settings,
47 int32_t number_of_cores) {
48 return delegate_->InitDecode(codec_settings, number_of_cores);
49 }
50
Decode(const EncodedImage & input_image,bool missing_frames,int64_t render_time_ms)51 int32_t QualityAnalyzingVideoDecoder::Decode(const EncodedImage& input_image,
52 bool missing_frames,
53 int64_t render_time_ms) {
54 // Image extractor extracts id from provided EncodedImage and also returns
55 // the image with the original buffer. Buffer can be modified in place, so
56 // owner of original buffer will be responsible for deleting it, or extractor
57 // can create a new buffer. In such case extractor will be responsible for
58 // deleting it.
59 EncodedImageExtractionResult out = extractor_->ExtractData(input_image, id_);
60
61 if (out.discard) {
62 // To partly emulate behavior of Selective Forwarding Unit (SFU) in the
63 // test, on receiver side we will "discard" frames from irrelevant streams.
64 // When all encoded images were marked to discarded, black frame have to be
65 // returned. Because simulcast streams will be received by receiver as 3
66 // different independent streams we don't want that irrelevant streams
67 // affect video quality metrics and also we don't want to use CPU time to
68 // decode them to prevent regressions on relevant streams. Also we can't
69 // just drop frame, because in such case, receiving part will be confused
70 // with all frames missing and will request a key frame, which will result
71 // into extra load on network and sender side. Because of it, discarded
72 // image will be always decoded as black frame and will be passed to
73 // callback directly without reaching decoder and video quality analyzer.
74 //
75 // For more details see QualityAnalyzingVideoEncoder.
76 return analyzing_callback_->IrrelevantSimulcastStreamDecoded(
77 out.id, input_image.Timestamp());
78 }
79
80 EncodedImage* origin_image;
81 {
82 MutexLock lock(&lock_);
83 // Store id to be able to retrieve it in analyzing callback.
84 timestamp_to_frame_id_.insert({input_image.Timestamp(), out.id});
85 // Store encoded image to prevent its destruction while it is used in
86 // decoder.
87 origin_image = &(
88 decoding_images_.insert({out.id, std::move(out.image)}).first->second);
89 }
90 // We can safely dereference |origin_image|, because it can be removed from
91 // the map only after |delegate_| Decode method will be invoked. Image will be
92 // removed inside DecodedImageCallback, which can be done on separate thread.
93 analyzer_->OnFramePreDecode(peer_name_, out.id, *origin_image);
94 int32_t result =
95 delegate_->Decode(*origin_image, missing_frames, render_time_ms);
96 if (result != WEBRTC_VIDEO_CODEC_OK) {
97 // If delegate decoder failed, then cleanup data for this image.
98 {
99 MutexLock lock(&lock_);
100 timestamp_to_frame_id_.erase(input_image.Timestamp());
101 decoding_images_.erase(out.id);
102 }
103 analyzer_->OnDecoderError(peer_name_, out.id, result);
104 }
105 return result;
106 }
107
RegisterDecodeCompleteCallback(DecodedImageCallback * callback)108 int32_t QualityAnalyzingVideoDecoder::RegisterDecodeCompleteCallback(
109 DecodedImageCallback* callback) {
110 analyzing_callback_->SetDelegateCallback(callback);
111 return delegate_->RegisterDecodeCompleteCallback(analyzing_callback_.get());
112 }
113
Release()114 int32_t QualityAnalyzingVideoDecoder::Release() {
115 // Release decoder first. During release process it can still decode some
116 // frames, so we don't take a lock to prevent deadlock.
117 int32_t result = delegate_->Release();
118
119 MutexLock lock(&lock_);
120 analyzing_callback_->SetDelegateCallback(nullptr);
121 timestamp_to_frame_id_.clear();
122 decoding_images_.clear();
123 return result;
124 }
125
PrefersLateDecoding() const126 bool QualityAnalyzingVideoDecoder::PrefersLateDecoding() const {
127 return delegate_->PrefersLateDecoding();
128 }
129
ImplementationName() const130 const char* QualityAnalyzingVideoDecoder::ImplementationName() const {
131 return implementation_name_.c_str();
132 }
133
DecoderCallback(QualityAnalyzingVideoDecoder * decoder)134 QualityAnalyzingVideoDecoder::DecoderCallback::DecoderCallback(
135 QualityAnalyzingVideoDecoder* decoder)
136 : decoder_(decoder), delegate_callback_(nullptr) {}
137 QualityAnalyzingVideoDecoder::DecoderCallback::~DecoderCallback() = default;
138
SetDelegateCallback(DecodedImageCallback * delegate)139 void QualityAnalyzingVideoDecoder::DecoderCallback::SetDelegateCallback(
140 DecodedImageCallback* delegate) {
141 MutexLock lock(&callback_lock_);
142 delegate_callback_ = delegate;
143 }
144
145 // We have to implement all next 3 methods because we don't know which one
146 // exactly is implemented in |delegate_callback_|, so we need to call the same
147 // method on |delegate_callback_|, as was called on |this| callback.
Decoded(VideoFrame & decodedImage)148 int32_t QualityAnalyzingVideoDecoder::DecoderCallback::Decoded(
149 VideoFrame& decodedImage) {
150 decoder_->OnFrameDecoded(&decodedImage, /*decode_time_ms=*/absl::nullopt,
151 /*qp=*/absl::nullopt);
152
153 MutexLock lock(&callback_lock_);
154 RTC_DCHECK(delegate_callback_);
155 return delegate_callback_->Decoded(decodedImage);
156 }
157
Decoded(VideoFrame & decodedImage,int64_t decode_time_ms)158 int32_t QualityAnalyzingVideoDecoder::DecoderCallback::Decoded(
159 VideoFrame& decodedImage,
160 int64_t decode_time_ms) {
161 decoder_->OnFrameDecoded(&decodedImage, decode_time_ms, /*qp=*/absl::nullopt);
162
163 MutexLock lock(&callback_lock_);
164 RTC_DCHECK(delegate_callback_);
165 return delegate_callback_->Decoded(decodedImage, decode_time_ms);
166 }
167
Decoded(VideoFrame & decodedImage,absl::optional<int32_t> decode_time_ms,absl::optional<uint8_t> qp)168 void QualityAnalyzingVideoDecoder::DecoderCallback::Decoded(
169 VideoFrame& decodedImage,
170 absl::optional<int32_t> decode_time_ms,
171 absl::optional<uint8_t> qp) {
172 decoder_->OnFrameDecoded(&decodedImage, decode_time_ms, qp);
173
174 MutexLock lock(&callback_lock_);
175 RTC_DCHECK(delegate_callback_);
176 delegate_callback_->Decoded(decodedImage, decode_time_ms, qp);
177 }
178
179 int32_t
IrrelevantSimulcastStreamDecoded(uint16_t frame_id,uint32_t timestamp_ms)180 QualityAnalyzingVideoDecoder::DecoderCallback::IrrelevantSimulcastStreamDecoded(
181 uint16_t frame_id,
182 uint32_t timestamp_ms) {
183 webrtc::VideoFrame dummy_frame =
184 webrtc::VideoFrame::Builder()
185 .set_video_frame_buffer(GetDummyFrameBuffer())
186 .set_timestamp_rtp(timestamp_ms)
187 .set_id(frame_id)
188 .build();
189 MutexLock lock(&callback_lock_);
190 RTC_DCHECK(delegate_callback_);
191 delegate_callback_->Decoded(dummy_frame, absl::nullopt, absl::nullopt);
192 return WEBRTC_VIDEO_CODEC_OK;
193 }
194
195 rtc::scoped_refptr<webrtc::VideoFrameBuffer>
GetDummyFrameBuffer()196 QualityAnalyzingVideoDecoder::DecoderCallback::GetDummyFrameBuffer() {
197 if (!dummy_frame_buffer_) {
198 dummy_frame_buffer_ = CreateDummyFrameBuffer();
199 }
200
201 return dummy_frame_buffer_;
202 }
203
OnFrameDecoded(VideoFrame * frame,absl::optional<int32_t> decode_time_ms,absl::optional<uint8_t> qp)204 void QualityAnalyzingVideoDecoder::OnFrameDecoded(
205 VideoFrame* frame,
206 absl::optional<int32_t> decode_time_ms,
207 absl::optional<uint8_t> qp) {
208 uint16_t frame_id;
209 {
210 MutexLock lock(&lock_);
211 auto it = timestamp_to_frame_id_.find(frame->timestamp());
212 if (it == timestamp_to_frame_id_.end()) {
213 // Ensure, that we have info about this frame. It can happen that for some
214 // reasons decoder response, that he failed to decode, when we were
215 // posting frame to it, but then call the callback for this frame.
216 RTC_LOG(LS_ERROR) << "QualityAnalyzingVideoDecoder::OnFrameDecoded: No "
217 "frame id for frame for frame->timestamp()="
218 << frame->timestamp();
219 return;
220 }
221 frame_id = it->second;
222 timestamp_to_frame_id_.erase(it);
223 decoding_images_.erase(frame_id);
224 }
225 // Set frame id to the value, that was extracted from corresponding encoded
226 // image.
227 frame->set_id(frame_id);
228 VideoQualityAnalyzerInterface::DecoderStats stats;
229 stats.decode_time_ms = decode_time_ms;
230 analyzer_->OnFrameDecoded(peer_name_, *frame, stats);
231 }
232
QualityAnalyzingVideoDecoderFactory(absl::string_view peer_name,std::unique_ptr<VideoDecoderFactory> delegate,IdGenerator<int> * id_generator,EncodedImageDataExtractor * extractor,VideoQualityAnalyzerInterface * analyzer)233 QualityAnalyzingVideoDecoderFactory::QualityAnalyzingVideoDecoderFactory(
234 absl::string_view peer_name,
235 std::unique_ptr<VideoDecoderFactory> delegate,
236 IdGenerator<int>* id_generator,
237 EncodedImageDataExtractor* extractor,
238 VideoQualityAnalyzerInterface* analyzer)
239 : peer_name_(peer_name),
240 delegate_(std::move(delegate)),
241 id_generator_(id_generator),
242 extractor_(extractor),
243 analyzer_(analyzer) {}
244 QualityAnalyzingVideoDecoderFactory::~QualityAnalyzingVideoDecoderFactory() =
245 default;
246
247 std::vector<SdpVideoFormat>
GetSupportedFormats() const248 QualityAnalyzingVideoDecoderFactory::GetSupportedFormats() const {
249 return delegate_->GetSupportedFormats();
250 }
251
252 std::unique_ptr<VideoDecoder>
CreateVideoDecoder(const SdpVideoFormat & format)253 QualityAnalyzingVideoDecoderFactory::CreateVideoDecoder(
254 const SdpVideoFormat& format) {
255 std::unique_ptr<VideoDecoder> decoder = delegate_->CreateVideoDecoder(format);
256 return std::make_unique<QualityAnalyzingVideoDecoder>(
257 id_generator_->GetNextId(), peer_name_, std::move(decoder), extractor_,
258 analyzer_);
259 }
260
261 std::unique_ptr<VideoDecoder>
LegacyCreateVideoDecoder(const SdpVideoFormat & format,const std::string & receive_stream_id)262 QualityAnalyzingVideoDecoderFactory::LegacyCreateVideoDecoder(
263 const SdpVideoFormat& format,
264 const std::string& receive_stream_id) {
265 std::unique_ptr<VideoDecoder> decoder =
266 delegate_->LegacyCreateVideoDecoder(format, receive_stream_id);
267 return std::make_unique<QualityAnalyzingVideoDecoder>(
268 id_generator_->GetNextId(), peer_name_, std::move(decoder), extractor_,
269 analyzer_);
270 }
271
272 } // namespace webrtc_pc_e2e
273 } // namespace webrtc
274