blob: 621fd73972a4369973086175ef4678424758cdac [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mikhal@webrtc.orga2031d52012-07-31 15:53:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/generic_decoder.h"
tommi5b7fc8c2017-07-05 16:45:57 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
ilnik2edc6842017-07-06 03:06:50 -070015#include <algorithm>
Johannes Kron111e9812020-10-26 13:54:40 +010016#include <cmath>
ilnik2edc6842017-07-06 03:06:50 -070017
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "api/video/video_timing.h"
19#include "modules/video_coding/include/video_error_codes.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
Johannes Kron7ddea572019-09-11 12:00:22 +020022#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/trace_event.h"
25#include "system_wrappers/include/clock.h"
Johannes Kron7ddea572019-09-11 12:00:22 +020026#include "system_wrappers/include/field_trial.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28namespace webrtc {
29
philipel9d3ab612015-12-21 04:12:39 -080030VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000031 Clock* clock)
Johannes Kron7ddea572019-09-11 12:00:22 +020032 : _clock(clock),
33 _timing(timing),
34 _timestampMap(kDecoderFrameMemoryLength),
Johannes Kron111e9812020-10-26 13:54:40 +010035 _extra_decode_time("t", absl::nullopt),
36 low_latency_renderer_enabled_("enabled", true),
37 low_latency_renderer_include_predecode_buffer_("include_predecode_buffer",
38 true) {
ilnik04f4d122017-06-19 07:18:55 -070039 ntp_offset_ =
40 _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
Johannes Kron7ddea572019-09-11 12:00:22 +020041
42 ParseFieldTrial({&_extra_decode_time},
43 field_trial::FindFullName("WebRTC-SlowDownDecoder"));
Johannes Kron111e9812020-10-26 13:54:40 +010044 ParseFieldTrial({&low_latency_renderer_enabled_,
45 &low_latency_renderer_include_predecode_buffer_},
46 field_trial::FindFullName("WebRTC-LowLatencyRenderer"));
ilnik04f4d122017-06-19 07:18:55 -070047}
niklase@google.com470e71d2011-07-07 08:21:25 +000048
Yves Gerey665174f2018-06-19 15:03:05 +020049VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000050
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000051void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080052 VCMReceiveCallback* receiveCallback) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020053 RTC_DCHECK(construction_thread_.IsCurrent());
tommid0a71ba2017-03-14 04:16:20 -070054 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
55 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080056 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
philipel9d3ab612015-12-21 04:12:39 -080059VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
tommid0a71ba2017-03-14 04:16:20 -070060 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
61 // The callback must always have been set before this happens.
62 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080063 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000064}
65
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070066int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Tommi553c8692020-05-05 15:35:45 +020067 // This function may be called on the decode TaskQueue, but may also be called
68 // on an OS provided queue such as on iOS (see e.g. b/153465112).
Per327d8ba2015-11-10 14:00:27 +010069 return Decoded(decodedImage, -1);
70}
71
72int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
73 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080074 Decoded(decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020075 decode_time_ms >= 0 ? absl::optional<int32_t>(decode_time_ms)
76 : absl::nullopt,
77 absl::nullopt);
sakalcc452e12017-02-09 04:53:45 -080078 return WEBRTC_VIDEO_CODEC_OK;
79}
80
81void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020082 absl::optional<int32_t> decode_time_ms,
83 absl::optional<uint8_t> qp) {
Johannes Kron7ddea572019-09-11 12:00:22 +020084 // Wait some extra time to simulate a slow decoder.
85 if (_extra_decode_time) {
86 rtc::Thread::SleepMs(_extra_decode_time->ms());
87 }
88
tommid0a71ba2017-03-14 04:16:20 -070089 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080090 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
91 "timestamp", decodedImage.timestamp());
92 // TODO(holmer): We should improve this so that we can handle multiple
93 // callbacks from one call to Decode().
Johannes Kronb6b782d2021-03-03 14:39:44 +010094 absl::optional<VCMFrameInformation> frameInfo;
Johannes Kron111e9812020-10-26 13:54:40 +010095 int timestamp_map_size = 0;
Johannes Kronfc5d2762021-04-09 16:03:22 +020096 int dropped_frames = 0;
Lu Liu352314a2018-02-21 19:38:59 +000097 {
Markus Handell6deec382020-07-07 12:17:12 +020098 MutexLock lock(&lock_);
Johannes Kronfc5d2762021-04-09 16:03:22 +020099 int initial_timestamp_map_size = _timestampMap.Size();
Lu Liu352314a2018-02-21 19:38:59 +0000100 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
Johannes Kron111e9812020-10-26 13:54:40 +0100101 timestamp_map_size = _timestampMap.Size();
Johannes Kronfc5d2762021-04-09 16:03:22 +0200102 // _timestampMap.Pop() erases all frame upto the specified timestamp and
103 // return the frame info for this timestamp if it exists. Thus, the
104 // difference in the _timestampMap size before and after Pop() will show
105 // internally dropped frames.
106 dropped_frames =
107 initial_timestamp_map_size - timestamp_map_size - (frameInfo ? 1 : 0);
108 }
109
110 if (dropped_frames > 0) {
111 _receiveCallback->OnDroppedFrames(dropped_frames);
Lu Liu352314a2018-02-21 19:38:59 +0000112 }
philipel9d3ab612015-12-21 04:12:39 -0800113
Johannes Kronb6b782d2021-03-03 14:39:44 +0100114 if (!frameInfo) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
116 "this one.";
sakalcc452e12017-02-09 04:53:45 -0800117 return;
philipel9d3ab612015-12-21 04:12:39 -0800118 }
119
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200120 decodedImage.set_ntp_time_ms(frameInfo->ntp_time_ms);
Chen Xingf00bf422019-06-20 10:05:55 +0200121 decodedImage.set_packet_infos(frameInfo->packet_infos);
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200122 decodedImage.set_rotation(frameInfo->rotation);
123
Johannes Kron0093a382021-02-23 22:59:40 +0100124 if (low_latency_renderer_enabled_) {
Johannes Kron111e9812020-10-26 13:54:40 +0100125 absl::optional<int> max_composition_delay_in_frames =
126 _timing->MaxCompositionDelayInFrames();
127 if (max_composition_delay_in_frames) {
128 // Subtract frames that are in flight.
129 if (low_latency_renderer_include_predecode_buffer_) {
130 *max_composition_delay_in_frames -= timestamp_map_size;
131 *max_composition_delay_in_frames =
132 std::max(0, *max_composition_delay_in_frames);
133 }
134 decodedImage.set_max_composition_delay_in_frames(
135 max_composition_delay_in_frames);
136 }
137 }
138
Johannes Kron05f84872020-01-16 14:09:33 +0100139 RTC_DCHECK(frameInfo->decodeStart);
philipel85ddb232020-10-02 14:46:14 +0200140 const Timestamp now = _clock->CurrentTime();
141 const TimeDelta decode_time = decode_time_ms
142 ? TimeDelta::Millis(*decode_time_ms)
143 : now - *frameInfo->decodeStart;
144 _timing->StopDecodeTimer(decode_time.ms(), now.ms());
145 decodedImage.set_processing_time(
146 {*frameInfo->decodeStart, *frameInfo->decodeStart + decode_time});
philipel9d3ab612015-12-21 04:12:39 -0800147
ilnik04f4d122017-06-19 07:18:55 -0700148 // Report timing information.
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800149 TimingFrameInfo timing_frame_info;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200150 if (frameInfo->timing.flags != VideoSendTiming::kInvalid) {
ilnik2edc6842017-07-06 03:06:50 -0700151 int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
ilnik04f4d122017-06-19 07:18:55 -0700152 // Convert remote timestamps to local time from ntp timestamps.
153 frameInfo->timing.encode_start_ms -= ntp_offset_;
154 frameInfo->timing.encode_finish_ms -= ntp_offset_;
155 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
156 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
157 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
158 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
ilnik2edc6842017-07-06 03:06:50 -0700159
160 int64_t sender_delta_ms = 0;
161 if (decodedImage.ntp_time_ms() < 0) {
162 // Sender clock is not estimated yet. Make sure that sender times are all
163 // negative to indicate that. Yet they still should be relatively correct.
164 sender_delta_ms =
165 std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
166 frameInfo->timing.encode_finish_ms,
167 frameInfo->timing.packetization_finish_ms,
168 frameInfo->timing.pacer_exit_ms,
169 frameInfo->timing.network_timestamp_ms,
170 frameInfo->timing.network2_timestamp_ms}) +
171 1;
172 }
173
ilnik2edc6842017-07-06 03:06:50 -0700174 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
175 timing_frame_info.encode_start_ms =
176 frameInfo->timing.encode_start_ms - sender_delta_ms;
177 timing_frame_info.encode_finish_ms =
178 frameInfo->timing.encode_finish_ms - sender_delta_ms;
179 timing_frame_info.packetization_finish_ms =
180 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
181 timing_frame_info.pacer_exit_ms =
182 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
183 timing_frame_info.network_timestamp_ms =
184 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
185 timing_frame_info.network2_timestamp_ms =
186 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700187 }
188
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800189 timing_frame_info.flags = frameInfo->timing.flags;
Johannes Kron05f84872020-01-16 14:09:33 +0100190 timing_frame_info.decode_start_ms = frameInfo->decodeStart->ms();
191 timing_frame_info.decode_finish_ms = now.ms();
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800192 timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
193 timing_frame_info.rtp_timestamp = decodedImage.timestamp();
194 timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
195 timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
196 _timing->SetTimingFrameInfo(timing_frame_info);
197
Yves Gerey665174f2018-06-19 15:03:05 +0200198 decodedImage.set_timestamp_us(frameInfo->renderTimeMs *
199 rtc::kNumMicrosecsPerMillisec);
philipel85ddb232020-10-02 14:46:14 +0200200 _receiveCallback->FrameToRender(decodedImage, qp, decode_time.ms(),
Johannes Kronbfd343b2019-07-01 10:07:50 +0200201 frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
Peter Boströmb7d9a972015-12-18 16:01:11 +0100204void VCMDecodedFrameCallback::OnDecoderImplementationName(
205 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700206 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100207}
208
pbos1968d3f2015-09-28 08:52:18 -0700209void VCMDecodedFrameCallback::Map(uint32_t timestamp,
Johannes Kronb6b782d2021-03-03 14:39:44 +0100210 const VCMFrameInformation& frameInfo) {
Johannes Kronfc5d2762021-04-09 16:03:22 +0200211 int dropped_frames = 0;
212 {
213 MutexLock lock(&lock_);
214 int initial_size = _timestampMap.Size();
215 _timestampMap.Add(timestamp, frameInfo);
216 // If no frame is dropped, the new size should be |initial_size| + 1
217 dropped_frames = (initial_size + 1) - _timestampMap.Size();
218 }
219 if (dropped_frames > 0) {
220 _receiveCallback->OnDroppedFrames(dropped_frames);
221 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
Johannes Kronfc5d2762021-04-09 16:03:22 +0200224void VCMDecodedFrameCallback::ClearTimestampMap() {
225 int dropped_frames = 0;
226 {
227 MutexLock lock(&lock_);
228 dropped_frames = _timestampMap.Size();
229 _timestampMap.Clear();
Lu Liu352314a2018-02-21 19:38:59 +0000230 }
Johannes Kronfc5d2762021-04-09 16:03:22 +0200231 if (dropped_frames > 0) {
232 _receiveCallback->OnDroppedFrames(dropped_frames);
233 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
Magnus Jedvert46a27652017-11-13 14:10:02 +0100236VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
237 : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
238
Peter Boström187db632015-12-01 17:20:01 +0100239VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
240 : _callback(NULL),
tommi5b7fc8c2017-07-05 16:45:57 -0700241 decoder_(decoder),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +0200242 _codecType(kVideoCodecGeneric),
guidouc3372582017-04-04 07:16:21 -0700243 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700244 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
245 RTC_DCHECK(decoder_);
246}
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
tommi5b7fc8c2017-07-05 16:45:57 -0700248VCMGenericDecoder::~VCMGenericDecoder() {
249 decoder_->Release();
250 if (_isExternal)
251 decoder_.release();
tommi058aa712017-07-15 11:33:35 -0700252 RTC_DCHECK(_isExternal || decoder_);
tommi5b7fc8c2017-07-05 16:45:57 -0700253}
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000255int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800256 int32_t numberOfCores) {
257 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
258 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200260 int err = decoder_->InitDecode(settings, numberOfCores);
Erik Språngc12f6252021-01-13 21:49:59 +0100261 decoder_info_ = decoder_->GetDecoderInfo();
262 RTC_LOG(LS_INFO) << "Decoder implementation: " << decoder_info_.ToString();
263 if (_callback) {
264 _callback->OnDecoderImplementationName(
265 decoder_info_.implementation_name.c_str());
266 }
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200267 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000268}
269
Johannes Kron05f84872020-01-16 14:09:33 +0100270int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, Timestamp now) {
Yves Gerey665174f2018-06-19 15:03:05 +0200271 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
Niels Möller23775882018-08-16 10:24:12 +0200272 frame.Timestamp());
Johannes Kronb6b782d2021-03-03 14:39:44 +0100273 VCMFrameInformation frame_info;
274 frame_info.decodeStart = now;
275 frame_info.renderTimeMs = frame.RenderTimeMs();
276 frame_info.rotation = frame.rotation();
277 frame_info.timing = frame.video_timing();
278 frame_info.ntp_time_ms = frame.EncodedImage().ntp_time_ms_;
279 frame_info.packet_infos = frame.PacketInfos();
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200280
Yves Gerey665174f2018-06-19 15:03:05 +0200281 // Set correctly only for key frames. Thus, use latest key frame
282 // content type. If the corresponding key frame was lost, decode will fail
283 // and content type will be ignored.
Niels Möller8f7ce222019-03-21 15:43:58 +0100284 if (frame.FrameType() == VideoFrameType::kVideoFrameKey) {
Johannes Kronb6b782d2021-03-03 14:39:44 +0100285 frame_info.content_type = frame.contentType();
Yves Gerey665174f2018-06-19 15:03:05 +0200286 _last_keyframe_content_type = frame.contentType();
287 } else {
Johannes Kronb6b782d2021-03-03 14:39:44 +0100288 frame_info.content_type = _last_keyframe_content_type;
Yves Gerey665174f2018-06-19 15:03:05 +0200289 }
Johannes Kronb6b782d2021-03-03 14:39:44 +0100290 _callback->Map(frame.Timestamp(), frame_info);
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
Yves Gerey665174f2018-06-19 15:03:05 +0200292 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
Niels Möller7aacdd92019-03-25 09:11:40 +0100293 frame.RenderTimeMs());
Erik Språngc12f6252021-01-13 21:49:59 +0100294 VideoDecoder::DecoderInfo decoder_info = decoder_->GetDecoderInfo();
295 if (decoder_info != decoder_info_) {
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200296 RTC_LOG(LS_INFO) << "Changed decoder implementation to: "
Erik Språngc12f6252021-01-13 21:49:59 +0100297 << decoder_info.ToString();
Erik Språngc80f9552021-03-12 16:36:49 +0100298 decoder_info_ = decoder_info;
Erik Språngc12f6252021-01-13 21:49:59 +0100299 _callback->OnDecoderImplementationName(
300 decoder_info.implementation_name.empty()
301 ? "unknown"
302 : decoder_info.implementation_name.c_str());
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200303 }
Yves Gerey665174f2018-06-19 15:03:05 +0200304 if (ret < WEBRTC_VIDEO_CODEC_OK) {
305 RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
Niels Möller23775882018-08-16 10:24:12 +0200306 << frame.Timestamp() << ", error code: " << ret;
Johannes Kronfc5d2762021-04-09 16:03:22 +0200307 _callback->ClearTimestampMap();
Niels Möller9cccf852018-12-04 11:01:26 +0100308 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT) {
Johannes Kronfc5d2762021-04-09 16:03:22 +0200309 // No output.
310 _callback->ClearTimestampMap();
Yves Gerey665174f2018-06-19 15:03:05 +0200311 }
312 return ret;
guidouc3372582017-04-04 07:16:21 -0700313}
niklase@google.com470e71d2011-07-07 08:21:25 +0000314
Peter Boström187db632015-12-01 17:20:01 +0100315int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
316 VCMDecodedFrameCallback* callback) {
317 _callback = callback;
Erik Språngc12f6252021-01-13 21:49:59 +0100318 int32_t ret = decoder_->RegisterDecodeCompleteCallback(callback);
319 if (callback && !decoder_info_.implementation_name.empty()) {
320 callback->OnDecoderImplementationName(
321 decoder_info_.implementation_name.c_str());
322 }
323 return ret;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324}
325
philipel9d3ab612015-12-21 04:12:39 -0800326} // namespace webrtc