blob: 100686d33682356b59bca1355b20438e2bad26f7 [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) {
Per327d8ba2015-11-10 14:00:27 +010060 return Decoded(decodedImage, -1);
61}
62
63int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
64 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080065 Decoded(decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020066 decode_time_ms >= 0 ? absl::optional<int32_t>(decode_time_ms)
67 : absl::nullopt,
68 absl::nullopt);
sakalcc452e12017-02-09 04:53:45 -080069 return WEBRTC_VIDEO_CODEC_OK;
70}
71
72void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020073 absl::optional<int32_t> decode_time_ms,
74 absl::optional<uint8_t> qp) {
Johannes Kron7ddea572019-09-11 12:00:22 +020075 // Wait some extra time to simulate a slow decoder.
76 if (_extra_decode_time) {
77 rtc::Thread::SleepMs(_extra_decode_time->ms());
78 }
79
tommid0a71ba2017-03-14 04:16:20 -070080 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080081 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
82 "timestamp", decodedImage.timestamp());
83 // TODO(holmer): We should improve this so that we can handle multiple
84 // callbacks from one call to Decode().
Lu Liu352314a2018-02-21 19:38:59 +000085 VCMFrameInformation* frameInfo;
86 {
87 rtc::CritScope cs(&lock_);
88 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
89 }
philipel9d3ab612015-12-21 04:12:39 -080090
91 if (frameInfo == NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010092 RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
93 "this one.";
Johannes Kron0c141c52019-08-26 15:04:43 +020094 _receiveCallback->OnDroppedFrames(1);
sakalcc452e12017-02-09 04:53:45 -080095 return;
philipel9d3ab612015-12-21 04:12:39 -080096 }
97
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020098 decodedImage.set_ntp_time_ms(frameInfo->ntp_time_ms);
Chen Xingf00bf422019-06-20 10:05:55 +020099 decodedImage.set_packet_infos(frameInfo->packet_infos);
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200100 decodedImage.set_rotation(frameInfo->rotation);
101
Johannes Kron05f84872020-01-16 14:09:33 +0100102 const Timestamp now = _clock->CurrentTime();
103 RTC_DCHECK(frameInfo->decodeStart);
sakalcc452e12017-02-09 04:53:45 -0800104 if (!decode_time_ms) {
Johannes Kron05f84872020-01-16 14:09:33 +0100105 decode_time_ms = (now - *frameInfo->decodeStart).ms();
philipel9d3ab612015-12-21 04:12:39 -0800106 }
Johannes Kron05f84872020-01-16 14:09:33 +0100107 _timing->StopDecodeTimer(*decode_time_ms, now.ms());
108 decodedImage.set_processing_time({*frameInfo->decodeStart, now});
philipel9d3ab612015-12-21 04:12:39 -0800109
ilnik04f4d122017-06-19 07:18:55 -0700110 // Report timing information.
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800111 TimingFrameInfo timing_frame_info;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200112 if (frameInfo->timing.flags != VideoSendTiming::kInvalid) {
ilnik2edc6842017-07-06 03:06:50 -0700113 int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
ilnik04f4d122017-06-19 07:18:55 -0700114 // Convert remote timestamps to local time from ntp timestamps.
115 frameInfo->timing.encode_start_ms -= ntp_offset_;
116 frameInfo->timing.encode_finish_ms -= ntp_offset_;
117 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
118 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
119 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
120 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
ilnik2edc6842017-07-06 03:06:50 -0700121
122 int64_t sender_delta_ms = 0;
123 if (decodedImage.ntp_time_ms() < 0) {
124 // Sender clock is not estimated yet. Make sure that sender times are all
125 // negative to indicate that. Yet they still should be relatively correct.
126 sender_delta_ms =
127 std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
128 frameInfo->timing.encode_finish_ms,
129 frameInfo->timing.packetization_finish_ms,
130 frameInfo->timing.pacer_exit_ms,
131 frameInfo->timing.network_timestamp_ms,
132 frameInfo->timing.network2_timestamp_ms}) +
133 1;
134 }
135
ilnik2edc6842017-07-06 03:06:50 -0700136 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
137 timing_frame_info.encode_start_ms =
138 frameInfo->timing.encode_start_ms - sender_delta_ms;
139 timing_frame_info.encode_finish_ms =
140 frameInfo->timing.encode_finish_ms - sender_delta_ms;
141 timing_frame_info.packetization_finish_ms =
142 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
143 timing_frame_info.pacer_exit_ms =
144 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
145 timing_frame_info.network_timestamp_ms =
146 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
147 timing_frame_info.network2_timestamp_ms =
148 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700149 }
150
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800151 timing_frame_info.flags = frameInfo->timing.flags;
Johannes Kron05f84872020-01-16 14:09:33 +0100152 timing_frame_info.decode_start_ms = frameInfo->decodeStart->ms();
153 timing_frame_info.decode_finish_ms = now.ms();
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800154 timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
155 timing_frame_info.rtp_timestamp = decodedImage.timestamp();
156 timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
157 timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
158 _timing->SetTimingFrameInfo(timing_frame_info);
159
Yves Gerey665174f2018-06-19 15:03:05 +0200160 decodedImage.set_timestamp_us(frameInfo->renderTimeMs *
161 rtc::kNumMicrosecsPerMillisec);
Johannes Kronbfd343b2019-07-01 10:07:50 +0200162 _receiveCallback->FrameToRender(decodedImage, qp, *decode_time_ms,
163 frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164}
165
Peter Boströmb7d9a972015-12-18 16:01:11 +0100166void VCMDecodedFrameCallback::OnDecoderImplementationName(
167 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700168 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100169}
170
pbos1968d3f2015-09-28 08:52:18 -0700171void VCMDecodedFrameCallback::Map(uint32_t timestamp,
172 VCMFrameInformation* frameInfo) {
Lu Liu352314a2018-02-21 19:38:59 +0000173 rtc::CritScope cs(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700174 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
philipel9d3ab612015-12-21 04:12:39 -0800177int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
Lu Liu352314a2018-02-21 19:38:59 +0000178 rtc::CritScope cs(&lock_);
179 if (_timestampMap.Pop(timestamp) == NULL) {
180 return VCM_GENERAL_ERROR;
181 }
Johannes Kron0c141c52019-08-26 15:04:43 +0200182 _receiveCallback->OnDroppedFrames(1);
Lu Liu352314a2018-02-21 19:38:59 +0000183 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
Magnus Jedvert46a27652017-11-13 14:10:02 +0100186VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
187 : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
188
Peter Boström187db632015-12-01 17:20:01 +0100189VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
190 : _callback(NULL),
191 _frameInfos(),
192 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700193 decoder_(decoder),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +0200194 _codecType(kVideoCodecGeneric),
guidouc3372582017-04-04 07:16:21 -0700195 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700196 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
197 RTC_DCHECK(decoder_);
198}
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
tommi5b7fc8c2017-07-05 16:45:57 -0700200VCMGenericDecoder::~VCMGenericDecoder() {
201 decoder_->Release();
202 if (_isExternal)
203 decoder_.release();
tommi058aa712017-07-15 11:33:35 -0700204 RTC_DCHECK(_isExternal || decoder_);
tommi5b7fc8c2017-07-05 16:45:57 -0700205}
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000207int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800208 int32_t numberOfCores) {
209 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
210 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211
tommi5b7fc8c2017-07-05 16:45:57 -0700212 return decoder_->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
Johannes Kron05f84872020-01-16 14:09:33 +0100215int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, Timestamp now) {
Yves Gerey665174f2018-06-19 15:03:05 +0200216 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
Niels Möller23775882018-08-16 10:24:12 +0200217 frame.Timestamp());
Johannes Kron05f84872020-01-16 14:09:33 +0100218 _frameInfos[_nextFrameInfoIdx].decodeStart = now;
Yves Gerey665174f2018-06-19 15:03:05 +0200219 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
220 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
221 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200222 _frameInfos[_nextFrameInfoIdx].ntp_time_ms =
223 frame.EncodedImage().ntp_time_ms_;
Chen Xingf00bf422019-06-20 10:05:55 +0200224 _frameInfos[_nextFrameInfoIdx].packet_infos = frame.PacketInfos();
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200225
Yves Gerey665174f2018-06-19 15:03:05 +0200226 // Set correctly only for key frames. Thus, use latest key frame
227 // content type. If the corresponding key frame was lost, decode will fail
228 // and content type will be ignored.
Niels Möller8f7ce222019-03-21 15:43:58 +0100229 if (frame.FrameType() == VideoFrameType::kVideoFrameKey) {
Yves Gerey665174f2018-06-19 15:03:05 +0200230 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
231 _last_keyframe_content_type = frame.contentType();
232 } else {
233 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
234 }
Niels Möller23775882018-08-16 10:24:12 +0200235 _callback->Map(frame.Timestamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
Yves Gerey665174f2018-06-19 15:03:05 +0200237 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
238 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
Niels Möller7aacdd92019-03-25 09:11:40 +0100239 frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
Yves Gerey665174f2018-06-19 15:03:05 +0200241 _callback->OnDecoderImplementationName(decoder_->ImplementationName());
242 if (ret < WEBRTC_VIDEO_CODEC_OK) {
243 RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
Niels Möller23775882018-08-16 10:24:12 +0200244 << frame.Timestamp() << ", error code: " << ret;
245 _callback->Pop(frame.Timestamp());
Lu Liu352314a2018-02-21 19:38:59 +0000246 return ret;
Niels Möller9cccf852018-12-04 11:01:26 +0100247 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT) {
Yves Gerey665174f2018-06-19 15:03:05 +0200248 // No output
Niels Möller23775882018-08-16 10:24:12 +0200249 _callback->Pop(frame.Timestamp());
Yves Gerey665174f2018-06-19 15:03:05 +0200250 }
251 return ret;
guidouc3372582017-04-04 07:16:21 -0700252}
niklase@google.com470e71d2011-07-07 08:21:25 +0000253
Peter Boström187db632015-12-01 17:20:01 +0100254int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
255 VCMDecodedFrameCallback* callback) {
256 _callback = callback;
tommi5b7fc8c2017-07-05 16:45:57 -0700257 return decoder_->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
perkj796cfaf2015-12-10 09:27:38 -0800260bool VCMGenericDecoder::PrefersLateDecoding() const {
tommi5b7fc8c2017-07-05 16:45:57 -0700261 return decoder_->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800262}
263
philipel9d3ab612015-12-21 04:12:39 -0800264} // namespace webrtc