blob: 50ecd8da8d3f82c999df6a9f32de56c7795b95d2 [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>
16
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "api/video/video_timing.h"
18#include "modules/video_coding/include/video_error_codes.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
Johannes Kron7ddea572019-09-11 12:00:22 +020021#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/trace_event.h"
24#include "system_wrappers/include/clock.h"
Johannes Kron7ddea572019-09-11 12:00:22 +020025#include "system_wrappers/include/field_trial.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27namespace webrtc {
28
philipel9d3ab612015-12-21 04:12:39 -080029VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000030 Clock* clock)
Johannes Kron7ddea572019-09-11 12:00:22 +020031 : _clock(clock),
32 _timing(timing),
33 _timestampMap(kDecoderFrameMemoryLength),
34 _extra_decode_time("t", absl::nullopt) {
ilnik04f4d122017-06-19 07:18:55 -070035 ntp_offset_ =
36 _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
Johannes Kron7ddea572019-09-11 12:00:22 +020037
38 ParseFieldTrial({&_extra_decode_time},
39 field_trial::FindFullName("WebRTC-SlowDownDecoder"));
ilnik04f4d122017-06-19 07:18:55 -070040}
niklase@google.com470e71d2011-07-07 08:21:25 +000041
Yves Gerey665174f2018-06-19 15:03:05 +020042VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000043
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000044void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080045 VCMReceiveCallback* receiveCallback) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020046 RTC_DCHECK(construction_thread_.IsCurrent());
tommid0a71ba2017-03-14 04:16:20 -070047 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
48 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080049 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
philipel9d3ab612015-12-21 04:12:39 -080052VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
tommid0a71ba2017-03-14 04:16:20 -070053 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
54 // The callback must always have been set before this happens.
55 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080056 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000057}
58
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070059int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Tommi553c8692020-05-05 15:35:45 +020060 // This function may be called on the decode TaskQueue, but may also be called
61 // on an OS provided queue such as on iOS (see e.g. b/153465112).
Per327d8ba2015-11-10 14:00:27 +010062 return Decoded(decodedImage, -1);
63}
64
65int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
66 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080067 Decoded(decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020068 decode_time_ms >= 0 ? absl::optional<int32_t>(decode_time_ms)
69 : absl::nullopt,
70 absl::nullopt);
sakalcc452e12017-02-09 04:53:45 -080071 return WEBRTC_VIDEO_CODEC_OK;
72}
73
74void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020075 absl::optional<int32_t> decode_time_ms,
76 absl::optional<uint8_t> qp) {
Johannes Kron7ddea572019-09-11 12:00:22 +020077 // Wait some extra time to simulate a slow decoder.
78 if (_extra_decode_time) {
79 rtc::Thread::SleepMs(_extra_decode_time->ms());
80 }
81
tommid0a71ba2017-03-14 04:16:20 -070082 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080083 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
84 "timestamp", decodedImage.timestamp());
85 // TODO(holmer): We should improve this so that we can handle multiple
86 // callbacks from one call to Decode().
Lu Liu352314a2018-02-21 19:38:59 +000087 VCMFrameInformation* frameInfo;
88 {
Markus Handell6deec382020-07-07 12:17:12 +020089 MutexLock lock(&lock_);
Lu Liu352314a2018-02-21 19:38:59 +000090 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
91 }
philipel9d3ab612015-12-21 04:12:39 -080092
93 if (frameInfo == NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
95 "this one.";
Johannes Kron0c141c52019-08-26 15:04:43 +020096 _receiveCallback->OnDroppedFrames(1);
sakalcc452e12017-02-09 04:53:45 -080097 return;
philipel9d3ab612015-12-21 04:12:39 -080098 }
99
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200100 decodedImage.set_ntp_time_ms(frameInfo->ntp_time_ms);
Chen Xingf00bf422019-06-20 10:05:55 +0200101 decodedImage.set_packet_infos(frameInfo->packet_infos);
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200102 decodedImage.set_rotation(frameInfo->rotation);
103
Johannes Kron05f84872020-01-16 14:09:33 +0100104 const Timestamp now = _clock->CurrentTime();
105 RTC_DCHECK(frameInfo->decodeStart);
sakalcc452e12017-02-09 04:53:45 -0800106 if (!decode_time_ms) {
Johannes Kron05f84872020-01-16 14:09:33 +0100107 decode_time_ms = (now - *frameInfo->decodeStart).ms();
philipel9d3ab612015-12-21 04:12:39 -0800108 }
Johannes Kron05f84872020-01-16 14:09:33 +0100109 _timing->StopDecodeTimer(*decode_time_ms, now.ms());
110 decodedImage.set_processing_time({*frameInfo->decodeStart, now});
philipel9d3ab612015-12-21 04:12:39 -0800111
ilnik04f4d122017-06-19 07:18:55 -0700112 // Report timing information.
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800113 TimingFrameInfo timing_frame_info;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200114 if (frameInfo->timing.flags != VideoSendTiming::kInvalid) {
ilnik2edc6842017-07-06 03:06:50 -0700115 int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
ilnik04f4d122017-06-19 07:18:55 -0700116 // Convert remote timestamps to local time from ntp timestamps.
117 frameInfo->timing.encode_start_ms -= ntp_offset_;
118 frameInfo->timing.encode_finish_ms -= ntp_offset_;
119 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
120 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
121 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
122 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
ilnik2edc6842017-07-06 03:06:50 -0700123
124 int64_t sender_delta_ms = 0;
125 if (decodedImage.ntp_time_ms() < 0) {
126 // Sender clock is not estimated yet. Make sure that sender times are all
127 // negative to indicate that. Yet they still should be relatively correct.
128 sender_delta_ms =
129 std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
130 frameInfo->timing.encode_finish_ms,
131 frameInfo->timing.packetization_finish_ms,
132 frameInfo->timing.pacer_exit_ms,
133 frameInfo->timing.network_timestamp_ms,
134 frameInfo->timing.network2_timestamp_ms}) +
135 1;
136 }
137
ilnik2edc6842017-07-06 03:06:50 -0700138 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
139 timing_frame_info.encode_start_ms =
140 frameInfo->timing.encode_start_ms - sender_delta_ms;
141 timing_frame_info.encode_finish_ms =
142 frameInfo->timing.encode_finish_ms - sender_delta_ms;
143 timing_frame_info.packetization_finish_ms =
144 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
145 timing_frame_info.pacer_exit_ms =
146 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
147 timing_frame_info.network_timestamp_ms =
148 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
149 timing_frame_info.network2_timestamp_ms =
150 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700151 }
152
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800153 timing_frame_info.flags = frameInfo->timing.flags;
Johannes Kron05f84872020-01-16 14:09:33 +0100154 timing_frame_info.decode_start_ms = frameInfo->decodeStart->ms();
155 timing_frame_info.decode_finish_ms = now.ms();
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800156 timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
157 timing_frame_info.rtp_timestamp = decodedImage.timestamp();
158 timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
159 timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
160 _timing->SetTimingFrameInfo(timing_frame_info);
161
Yves Gerey665174f2018-06-19 15:03:05 +0200162 decodedImage.set_timestamp_us(frameInfo->renderTimeMs *
163 rtc::kNumMicrosecsPerMillisec);
Johannes Kronbfd343b2019-07-01 10:07:50 +0200164 _receiveCallback->FrameToRender(decodedImage, qp, *decode_time_ms,
165 frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
Peter Boströmb7d9a972015-12-18 16:01:11 +0100168void VCMDecodedFrameCallback::OnDecoderImplementationName(
169 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700170 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100171}
172
pbos1968d3f2015-09-28 08:52:18 -0700173void VCMDecodedFrameCallback::Map(uint32_t timestamp,
174 VCMFrameInformation* frameInfo) {
Markus Handell6deec382020-07-07 12:17:12 +0200175 MutexLock lock(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700176 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177}
178
philipel9d3ab612015-12-21 04:12:39 -0800179int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +0200180 MutexLock lock(&lock_);
Lu Liu352314a2018-02-21 19:38:59 +0000181 if (_timestampMap.Pop(timestamp) == NULL) {
182 return VCM_GENERAL_ERROR;
183 }
Johannes Kron0c141c52019-08-26 15:04:43 +0200184 _receiveCallback->OnDroppedFrames(1);
Lu Liu352314a2018-02-21 19:38:59 +0000185 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
Magnus Jedvert46a27652017-11-13 14:10:02 +0100188VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
189 : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
190
Peter Boström187db632015-12-01 17:20:01 +0100191VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
192 : _callback(NULL),
193 _frameInfos(),
194 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700195 decoder_(decoder),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +0200196 _codecType(kVideoCodecGeneric),
guidouc3372582017-04-04 07:16:21 -0700197 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700198 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
199 RTC_DCHECK(decoder_);
200}
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
tommi5b7fc8c2017-07-05 16:45:57 -0700202VCMGenericDecoder::~VCMGenericDecoder() {
203 decoder_->Release();
204 if (_isExternal)
205 decoder_.release();
tommi058aa712017-07-15 11:33:35 -0700206 RTC_DCHECK(_isExternal || decoder_);
tommi5b7fc8c2017-07-05 16:45:57 -0700207}
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000209int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800210 int32_t numberOfCores) {
211 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
212 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200214 int err = decoder_->InitDecode(settings, numberOfCores);
215 implementation_name_ = decoder_->ImplementationName();
216 RTC_LOG(LS_INFO) << "Decoder implementation: " << implementation_name_;
217 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218}
219
Johannes Kron05f84872020-01-16 14:09:33 +0100220int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, Timestamp now) {
Yves Gerey665174f2018-06-19 15:03:05 +0200221 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
Niels Möller23775882018-08-16 10:24:12 +0200222 frame.Timestamp());
Johannes Kron05f84872020-01-16 14:09:33 +0100223 _frameInfos[_nextFrameInfoIdx].decodeStart = now;
Yves Gerey665174f2018-06-19 15:03:05 +0200224 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
225 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
226 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200227 _frameInfos[_nextFrameInfoIdx].ntp_time_ms =
228 frame.EncodedImage().ntp_time_ms_;
Chen Xingf00bf422019-06-20 10:05:55 +0200229 _frameInfos[_nextFrameInfoIdx].packet_infos = frame.PacketInfos();
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200230
Yves Gerey665174f2018-06-19 15:03:05 +0200231 // Set correctly only for key frames. Thus, use latest key frame
232 // content type. If the corresponding key frame was lost, decode will fail
233 // and content type will be ignored.
Niels Möller8f7ce222019-03-21 15:43:58 +0100234 if (frame.FrameType() == VideoFrameType::kVideoFrameKey) {
Yves Gerey665174f2018-06-19 15:03:05 +0200235 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
236 _last_keyframe_content_type = frame.contentType();
237 } else {
238 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
239 }
Niels Möller23775882018-08-16 10:24:12 +0200240 _callback->Map(frame.Timestamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
Yves Gerey665174f2018-06-19 15:03:05 +0200242 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
243 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
Niels Möller7aacdd92019-03-25 09:11:40 +0100244 frame.RenderTimeMs());
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200245 const char* new_implementation_name = decoder_->ImplementationName();
246 if (new_implementation_name != implementation_name_) {
247 implementation_name_ = new_implementation_name;
248 RTC_LOG(LS_INFO) << "Changed decoder implementation to: "
249 << new_implementation_name;
250 }
251 _callback->OnDecoderImplementationName(implementation_name_.c_str());
Yves Gerey665174f2018-06-19 15:03:05 +0200252 if (ret < WEBRTC_VIDEO_CODEC_OK) {
253 RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
Niels Möller23775882018-08-16 10:24:12 +0200254 << frame.Timestamp() << ", error code: " << ret;
255 _callback->Pop(frame.Timestamp());
Lu Liu352314a2018-02-21 19:38:59 +0000256 return ret;
Niels Möller9cccf852018-12-04 11:01:26 +0100257 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT) {
Yves Gerey665174f2018-06-19 15:03:05 +0200258 // No output
Niels Möller23775882018-08-16 10:24:12 +0200259 _callback->Pop(frame.Timestamp());
Yves Gerey665174f2018-06-19 15:03:05 +0200260 }
261 return ret;
guidouc3372582017-04-04 07:16:21 -0700262}
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
Peter Boström187db632015-12-01 17:20:01 +0100264int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
265 VCMDecodedFrameCallback* callback) {
266 _callback = callback;
tommi5b7fc8c2017-07-05 16:45:57 -0700267 return decoder_->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000268}
269
perkj796cfaf2015-12-10 09:27:38 -0800270bool VCMGenericDecoder::PrefersLateDecoding() const {
tommi5b7fc8c2017-07-05 16:45:57 -0700271 return decoder_->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800272}
273
philipel9d3ab612015-12-21 04:12:39 -0800274} // namespace webrtc