blob: b6e8203c0abf6f2874e5e3855a709a05ad767f60 [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 RTC_DCHECK(frameInfo->decodeStart);
philipel85ddb232020-10-02 14:46:14 +0200105 const Timestamp now = _clock->CurrentTime();
106 const TimeDelta decode_time = decode_time_ms
107 ? TimeDelta::Millis(*decode_time_ms)
108 : now - *frameInfo->decodeStart;
109 _timing->StopDecodeTimer(decode_time.ms(), now.ms());
110 decodedImage.set_processing_time(
111 {*frameInfo->decodeStart, *frameInfo->decodeStart + decode_time});
philipel9d3ab612015-12-21 04:12:39 -0800112
ilnik04f4d122017-06-19 07:18:55 -0700113 // Report timing information.
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800114 TimingFrameInfo timing_frame_info;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200115 if (frameInfo->timing.flags != VideoSendTiming::kInvalid) {
ilnik2edc6842017-07-06 03:06:50 -0700116 int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
ilnik04f4d122017-06-19 07:18:55 -0700117 // Convert remote timestamps to local time from ntp timestamps.
118 frameInfo->timing.encode_start_ms -= ntp_offset_;
119 frameInfo->timing.encode_finish_ms -= ntp_offset_;
120 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
121 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
122 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
123 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
ilnik2edc6842017-07-06 03:06:50 -0700124
125 int64_t sender_delta_ms = 0;
126 if (decodedImage.ntp_time_ms() < 0) {
127 // Sender clock is not estimated yet. Make sure that sender times are all
128 // negative to indicate that. Yet they still should be relatively correct.
129 sender_delta_ms =
130 std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
131 frameInfo->timing.encode_finish_ms,
132 frameInfo->timing.packetization_finish_ms,
133 frameInfo->timing.pacer_exit_ms,
134 frameInfo->timing.network_timestamp_ms,
135 frameInfo->timing.network2_timestamp_ms}) +
136 1;
137 }
138
ilnik2edc6842017-07-06 03:06:50 -0700139 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
140 timing_frame_info.encode_start_ms =
141 frameInfo->timing.encode_start_ms - sender_delta_ms;
142 timing_frame_info.encode_finish_ms =
143 frameInfo->timing.encode_finish_ms - sender_delta_ms;
144 timing_frame_info.packetization_finish_ms =
145 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
146 timing_frame_info.pacer_exit_ms =
147 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
148 timing_frame_info.network_timestamp_ms =
149 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
150 timing_frame_info.network2_timestamp_ms =
151 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700152 }
153
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800154 timing_frame_info.flags = frameInfo->timing.flags;
Johannes Kron05f84872020-01-16 14:09:33 +0100155 timing_frame_info.decode_start_ms = frameInfo->decodeStart->ms();
156 timing_frame_info.decode_finish_ms = now.ms();
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800157 timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
158 timing_frame_info.rtp_timestamp = decodedImage.timestamp();
159 timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
160 timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
161 _timing->SetTimingFrameInfo(timing_frame_info);
162
Yves Gerey665174f2018-06-19 15:03:05 +0200163 decodedImage.set_timestamp_us(frameInfo->renderTimeMs *
164 rtc::kNumMicrosecsPerMillisec);
philipel85ddb232020-10-02 14:46:14 +0200165 _receiveCallback->FrameToRender(decodedImage, qp, decode_time.ms(),
Johannes Kronbfd343b2019-07-01 10:07:50 +0200166 frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000167}
168
Peter Boströmb7d9a972015-12-18 16:01:11 +0100169void VCMDecodedFrameCallback::OnDecoderImplementationName(
170 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700171 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100172}
173
pbos1968d3f2015-09-28 08:52:18 -0700174void VCMDecodedFrameCallback::Map(uint32_t timestamp,
175 VCMFrameInformation* frameInfo) {
Markus Handell6deec382020-07-07 12:17:12 +0200176 MutexLock lock(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700177 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178}
179
philipel9d3ab612015-12-21 04:12:39 -0800180int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +0200181 MutexLock lock(&lock_);
Lu Liu352314a2018-02-21 19:38:59 +0000182 if (_timestampMap.Pop(timestamp) == NULL) {
183 return VCM_GENERAL_ERROR;
184 }
Johannes Kron0c141c52019-08-26 15:04:43 +0200185 _receiveCallback->OnDroppedFrames(1);
Lu Liu352314a2018-02-21 19:38:59 +0000186 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
Magnus Jedvert46a27652017-11-13 14:10:02 +0100189VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
190 : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
191
Peter Boström187db632015-12-01 17:20:01 +0100192VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
193 : _callback(NULL),
194 _frameInfos(),
195 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700196 decoder_(decoder),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +0200197 _codecType(kVideoCodecGeneric),
guidouc3372582017-04-04 07:16:21 -0700198 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700199 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
200 RTC_DCHECK(decoder_);
201}
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
tommi5b7fc8c2017-07-05 16:45:57 -0700203VCMGenericDecoder::~VCMGenericDecoder() {
204 decoder_->Release();
205 if (_isExternal)
206 decoder_.release();
tommi058aa712017-07-15 11:33:35 -0700207 RTC_DCHECK(_isExternal || decoder_);
tommi5b7fc8c2017-07-05 16:45:57 -0700208}
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000210int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800211 int32_t numberOfCores) {
212 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
213 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200215 int err = decoder_->InitDecode(settings, numberOfCores);
216 implementation_name_ = decoder_->ImplementationName();
217 RTC_LOG(LS_INFO) << "Decoder implementation: " << implementation_name_;
218 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219}
220
Johannes Kron05f84872020-01-16 14:09:33 +0100221int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, Timestamp now) {
Yves Gerey665174f2018-06-19 15:03:05 +0200222 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
Niels Möller23775882018-08-16 10:24:12 +0200223 frame.Timestamp());
Johannes Kron05f84872020-01-16 14:09:33 +0100224 _frameInfos[_nextFrameInfoIdx].decodeStart = now;
Yves Gerey665174f2018-06-19 15:03:05 +0200225 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
226 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
227 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200228 _frameInfos[_nextFrameInfoIdx].ntp_time_ms =
229 frame.EncodedImage().ntp_time_ms_;
Chen Xingf00bf422019-06-20 10:05:55 +0200230 _frameInfos[_nextFrameInfoIdx].packet_infos = frame.PacketInfos();
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200231
Yves Gerey665174f2018-06-19 15:03:05 +0200232 // Set correctly only for key frames. Thus, use latest key frame
233 // content type. If the corresponding key frame was lost, decode will fail
234 // and content type will be ignored.
Niels Möller8f7ce222019-03-21 15:43:58 +0100235 if (frame.FrameType() == VideoFrameType::kVideoFrameKey) {
Yves Gerey665174f2018-06-19 15:03:05 +0200236 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
237 _last_keyframe_content_type = frame.contentType();
238 } else {
239 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
240 }
Niels Möller23775882018-08-16 10:24:12 +0200241 _callback->Map(frame.Timestamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
Yves Gerey665174f2018-06-19 15:03:05 +0200243 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
244 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
Niels Möller7aacdd92019-03-25 09:11:40 +0100245 frame.RenderTimeMs());
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +0200246 const char* new_implementation_name = decoder_->ImplementationName();
247 if (new_implementation_name != implementation_name_) {
248 implementation_name_ = new_implementation_name;
249 RTC_LOG(LS_INFO) << "Changed decoder implementation to: "
250 << new_implementation_name;
251 }
252 _callback->OnDecoderImplementationName(implementation_name_.c_str());
Yves Gerey665174f2018-06-19 15:03:05 +0200253 if (ret < WEBRTC_VIDEO_CODEC_OK) {
254 RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
Niels Möller23775882018-08-16 10:24:12 +0200255 << frame.Timestamp() << ", error code: " << ret;
256 _callback->Pop(frame.Timestamp());
Lu Liu352314a2018-02-21 19:38:59 +0000257 return ret;
Niels Möller9cccf852018-12-04 11:01:26 +0100258 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT) {
Yves Gerey665174f2018-06-19 15:03:05 +0200259 // No output
Niels Möller23775882018-08-16 10:24:12 +0200260 _callback->Pop(frame.Timestamp());
Yves Gerey665174f2018-06-19 15:03:05 +0200261 }
262 return ret;
guidouc3372582017-04-04 07:16:21 -0700263}
niklase@google.com470e71d2011-07-07 08:21:25 +0000264
Peter Boström187db632015-12-01 17:20:01 +0100265int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
266 VCMDecodedFrameCallback* callback) {
267 _callback = callback;
tommi5b7fc8c2017-07-05 16:45:57 -0700268 return decoder_->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000269}
270
perkj796cfaf2015-12-10 09:27:38 -0800271bool VCMGenericDecoder::PrefersLateDecoding() const {
tommi5b7fc8c2017-07-05 16:45:57 -0700272 return decoder_->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800273}
274
philipel9d3ab612015-12-21 04:12:39 -0800275} // namespace webrtc