blob: db9ceb8784d9fa923ffff37ffd05f4a36ac47566 [file] [log] [blame]
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +00001/*
2 * Copyright (c) 2013 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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stddef.h>
12#include <cstdint>
13#include <vector>
14
15#include "api/rtp_headers.h"
16#include "api/video_codecs/video_codec.h"
17#include "api/video_codecs/video_decoder.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "modules/include/module_common_types.h"
Tommifbf3bce2018-02-21 15:56:05 +010020#include "modules/utility/include/process_thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "modules/video_coding/decoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/encoded_frame.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "modules/video_coding/generic_decoder.h"
24#include "modules/video_coding/include/video_coding.h"
25#include "modules/video_coding/include/video_coding_defines.h"
26#include "modules/video_coding/internal_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/video_coding/jitter_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "modules/video_coding/media_opt_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/video_coding/packet.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "modules/video_coding/receiver.h"
31#include "modules/video_coding/timing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/video_coding/video_coding_impl.h"
33#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/critical_section.h"
Tommifbf3bce2018-02-21 15:56:05 +010035#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/one_time_event.h"
Yves Gerey3e707812018-11-28 16:47:49 +010038#include "rtc_base/thread_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "rtc_base/trace_event.h"
40#include "system_wrappers/include/clock.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000041
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000042namespace webrtc {
43namespace vcm {
44
philipel83f831a2016-03-12 03:30:23 -080045VideoReceiver::VideoReceiver(Clock* clock,
philipel721d4022016-12-15 07:10:57 -080046 VCMTiming* timing,
philipel83f831a2016-03-12 03:30:23 -080047 NackSender* nack_sender,
48 KeyFrameRequestSender* keyframe_request_sender)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000049 : clock_(clock),
philipel721d4022016-12-15 07:10:57 -080050 _timing(timing),
51 _receiver(_timing,
philipel83f831a2016-03-12 03:30:23 -080052 clock_,
philipel83f831a2016-03-12 03:30:23 -080053 nack_sender,
54 keyframe_request_sender),
philipel721d4022016-12-15 07:10:57 -080055 _decodedFrameCallback(_timing, clock_),
sprang3911c262016-04-15 01:24:14 -070056 _frameTypeCallback(nullptr),
57 _receiveStatsCallback(nullptr),
sprang3911c262016-04-15 01:24:14 -070058 _packetRequestCallback(nullptr),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000059 _scheduleKeyRequest(false),
Peter Boströmed3277b2016-02-02 15:40:04 +010060 drop_frames_until_keyframe_(false),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000061 max_nack_list_size_(0),
Niels Möllerf9063782018-02-20 16:09:48 +010062 _codecDataBase(),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000063 _receiveStatsTimer(1000, clock_),
64 _retransmissionTimer(10, clock_),
Tommifbf3bce2018-02-21 15:56:05 +010065 _keyRequestTimer(500, clock_) {
66 decoder_thread_checker_.DetachFromThread();
67 module_thread_checker_.DetachFromThread();
68}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000069
Tommifbf3bce2018-02-21 15:56:05 +010070VideoReceiver::~VideoReceiver() {
71 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
72}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000073
pbosa26ac922016-02-25 04:50:01 -080074void VideoReceiver::Process() {
Tommifbf3bce2018-02-21 15:56:05 +010075 RTC_DCHECK_RUN_ON(&module_thread_checker_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000076 // Receive-side statistics
philipela45102f2017-02-22 05:30:39 -080077
78 // TODO(philipel): Remove this if block when we know what to do with
79 // ReceiveStatisticsProxy::QualitySample.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000080 if (_receiveStatsTimer.TimeUntilProcess() == 0) {
81 _receiveStatsTimer.Processed();
sprang3911c262016-04-15 01:24:14 -070082 if (_receiveStatsCallback != nullptr) {
philipela45102f2017-02-22 05:30:39 -080083 _receiveStatsCallback->OnReceiveRatesUpdated(0, 0);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +000084 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000085 }
86
87 // Key frame requests
88 if (_keyRequestTimer.TimeUntilProcess() == 0) {
89 _keyRequestTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010090 bool request_key_frame = _frameTypeCallback != nullptr;
91 if (request_key_frame) {
sprang3911c262016-04-15 01:24:14 -070092 rtc::CritScope cs(&process_crit_);
Tommifbf3bce2018-02-21 15:56:05 +010093 request_key_frame = _scheduleKeyRequest;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000094 }
pbosa26ac922016-02-25 04:50:01 -080095 if (request_key_frame)
96 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000097 }
98
99 // Packet retransmission requests
100 // TODO(holmer): Add API for changing Process interval and make sure it's
101 // disabled when NACK is off.
102 if (_retransmissionTimer.TimeUntilProcess() == 0) {
103 _retransmissionTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +0100104 bool callback_registered = _packetRequestCallback != nullptr;
105 uint16_t length = max_nack_list_size_;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000106 if (callback_registered && length > 0) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700107 // Collect sequence numbers from the default receiver.
108 bool request_key_frame = false;
109 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
110 int32_t ret = VCM_OK;
111 if (request_key_frame) {
112 ret = RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000113 }
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700114 if (ret == VCM_OK && !nackList.empty()) {
guidouc3372582017-04-04 07:16:21 -0700115 rtc::CritScope cs(&process_crit_);
sprang3911c262016-04-15 01:24:14 -0700116 if (_packetRequestCallback != nullptr) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700117 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000118 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000119 }
120 }
121 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000122}
123
Tommifbf3bce2018-02-21 15:56:05 +0100124void VideoReceiver::ProcessThreadAttached(ProcessThread* process_thread) {
125 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
126 if (process_thread) {
127 is_attached_to_process_thread_ = true;
128 RTC_DCHECK(!process_thread_ || process_thread_ == process_thread);
129 process_thread_ = process_thread;
130 } else {
131 is_attached_to_process_thread_ = false;
132 }
133}
134
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000135int64_t VideoReceiver::TimeUntilNextProcess() {
Tommifbf3bce2018-02-21 15:56:05 +0100136 RTC_DCHECK_RUN_ON(&module_thread_checker_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000137 int64_t timeUntilNextProcess = _receiveStatsTimer.TimeUntilProcess();
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000138 if (_receiver.NackMode() != kNoNack) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000139 // We need a Process call more often if we are relying on
140 // retransmissions
141 timeUntilNextProcess =
142 VCM_MIN(timeUntilNextProcess, _retransmissionTimer.TimeUntilProcess());
143 }
144 timeUntilNextProcess =
145 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
146
147 return timeUntilNextProcess;
148}
149
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000150int32_t VideoReceiver::SetReceiveChannelParameters(int64_t rtt) {
Tommifbf3bce2018-02-21 15:56:05 +0100151 RTC_DCHECK_RUN_ON(&module_thread_checker_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000152 _receiver.UpdateRtt(rtt);
153 return 0;
154}
155
156// Enable or disable a video protection method.
157// Note: This API should be deprecated, as it does not offer a distinction
tommia5c18d72017-03-20 10:43:23 -0700158// between the protection method and decoding with or without errors.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000159int32_t VideoReceiver::SetVideoProtection(VCMVideoProtection videoProtection,
160 bool enable) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000161 switch (videoProtection) {
pbosba8c15b2015-07-14 09:36:34 -0700162 case kProtectionNack: {
henrikg91d6ede2015-09-17 00:24:34 -0700163 RTC_DCHECK(enable);
pbosba8c15b2015-07-14 09:36:34 -0700164 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000165 break;
166 }
167
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000168 case kProtectionNackFEC: {
henrikg91d6ede2015-09-17 00:24:34 -0700169 RTC_DCHECK(enable);
Yves Gerey665174f2018-06-19 15:03:05 +0200170 _receiver.SetNackMode(kNack, media_optimization::kLowRttNackMs,
philipelae284082016-05-09 12:14:29 +0200171 media_optimization::kMaxRttDelayThreshold);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000172 break;
173 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000174 case kProtectionFEC:
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000175 case kProtectionNone:
pbosba8c15b2015-07-14 09:36:34 -0700176 // No receiver-side protection.
henrikg91d6ede2015-09-17 00:24:34 -0700177 RTC_DCHECK(enable);
pbosba8c15b2015-07-14 09:36:34 -0700178 _receiver.SetNackMode(kNoNack, -1, -1);
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000179 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000180 }
181 return VCM_OK;
182}
183
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000184// Register a receive callback. Will be called whenever there is a new frame
185// ready for rendering.
186int32_t VideoReceiver::RegisterReceiveCallback(
187 VCMReceiveCallback* receiveCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100188 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
189 RTC_DCHECK(!IsDecoderThreadRunning());
190 // This value is set before the decoder thread starts and unset after
191 // the decoder thread has been stopped.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000192 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
193 return VCM_OK;
194}
195
196int32_t VideoReceiver::RegisterReceiveStatisticsCallback(
197 VCMReceiveStatisticsCallback* receiveStats) {
Tommifbf3bce2018-02-21 15:56:05 +0100198 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
199 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
200 // |_receiver| is used on both the decoder and module threads.
201 // However, since we make sure that we never do anything on the module thread
202 // when the decoder thread is not running, we don't need a lock for the
203 // |_receiver| or |_receiveStatsCallback| here.
pbos@webrtc.org55707692014-12-19 15:45:03 +0000204 _receiver.RegisterStatsCallback(receiveStats);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000205 _receiveStatsCallback = receiveStats;
206 return VCM_OK;
207}
208
perkj796cfaf2015-12-10 09:27:38 -0800209// Register an externally defined decoder object.
Peter Boström795dbe42015-11-27 14:09:07 +0100210void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800211 uint8_t payloadType) {
Tommifbf3bce2018-02-21 15:56:05 +0100212 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
213 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700214 if (externalDecoder == nullptr) {
Peter Boström795dbe42015-11-27 14:09:07 +0100215 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
Peter Boström395c7c62015-11-27 15:23:12 +0100216 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000217 }
perkj796cfaf2015-12-10 09:27:38 -0800218 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000219}
220
221// Register a frame type request callback.
222int32_t VideoReceiver::RegisterFrameTypeCallback(
223 VCMFrameTypeCallback* frameTypeCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100224 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
225 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
226 // This callback is used on the module thread, but since we don't get
227 // callbacks on the module thread while the decoder thread isn't running
228 // (and this function must not be called when the decoder is running),
229 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000230 _frameTypeCallback = frameTypeCallback;
231 return VCM_OK;
232}
233
234int32_t VideoReceiver::RegisterPacketRequestCallback(
235 VCMPacketRequestCallback* callback) {
Tommifbf3bce2018-02-21 15:56:05 +0100236 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
237 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
238 // This callback is used on the module thread, but since we don't get
239 // callbacks on the module thread while the decoder thread isn't running
240 // (and this function must not be called when the decoder is running),
241 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000242 _packetRequestCallback = callback;
243 return VCM_OK;
244}
245
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000246void VideoReceiver::TriggerDecoderShutdown() {
Tommifbf3bce2018-02-21 15:56:05 +0100247 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
248 RTC_DCHECK(IsDecoderThreadRunning());
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000249 _receiver.TriggerDecoderShutdown();
250}
251
Tommifbf3bce2018-02-21 15:56:05 +0100252void VideoReceiver::DecoderThreadStarting() {
253 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
254 RTC_DCHECK(!IsDecoderThreadRunning());
255 if (process_thread_ && !is_attached_to_process_thread_) {
256 process_thread_->RegisterModule(this, RTC_FROM_HERE);
257 }
258#if RTC_DCHECK_IS_ON
259 decoder_thread_is_running_ = true;
260#endif
261}
262
263void VideoReceiver::DecoderThreadStopped() {
264 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
265 RTC_DCHECK(IsDecoderThreadRunning());
266 if (process_thread_ && is_attached_to_process_thread_) {
267 process_thread_->DeRegisterModule(this);
268 }
269#if RTC_DCHECK_IS_ON
270 decoder_thread_is_running_ = false;
271 decoder_thread_checker_.DetachFromThread();
272#endif
273}
274
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000275// Decode next frame, blocking.
276// Should be called as often as possible to get the most out of the decoder.
277int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
Tommifbf3bce2018-02-21 15:56:05 +0100278 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
279 VCMEncodedFrame* frame = _receiver.FrameForDecoding(
280 maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000281
Peter Boströmb7d9a972015-12-18 16:01:11 +0100282 if (!frame)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000283 return VCM_FRAME_NOT_READY;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000284
Tommifbf3bce2018-02-21 15:56:05 +0100285 bool drop_frame = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100286 {
sprang3911c262016-04-15 01:24:14 -0700287 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100288 if (drop_frames_until_keyframe_) {
289 // Still getting delta frames, schedule another keyframe request as if
290 // decode failed.
Niels Möller8f7ce222019-03-21 15:43:58 +0100291 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Tommifbf3bce2018-02-21 15:56:05 +0100292 drop_frame = true;
Peter Boströmed3277b2016-02-02 15:40:04 +0100293 _scheduleKeyRequest = true;
Tommifbf3bce2018-02-21 15:56:05 +0100294 // TODO(tommi): Consider if we could instead post a task to the module
295 // thread and call RequestKeyFrame directly. Here we call WakeUp so that
296 // TimeUntilNextProcess() gets called straight away.
297 process_thread_->WakeUp(this);
298 } else {
299 drop_frames_until_keyframe_ = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100300 }
Peter Boströmed3277b2016-02-02 15:40:04 +0100301 }
302 }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100303
Tommifbf3bce2018-02-21 15:56:05 +0100304 if (drop_frame) {
305 _receiver.ReleaseFrame(frame);
306 return VCM_FRAME_NOT_READY;
307 }
308
sprang3911c262016-04-15 01:24:14 -0700309 // If this frame was too late, we should adjust the delay accordingly
philipel721d4022016-12-15 07:10:57 -0800310 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
311 clock_->TimeInMilliseconds());
skvlad98bb6642016-04-07 15:36:45 -0700312
313 if (first_frame_received_()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100314 RTC_LOG(LS_INFO) << "Received first "
315 << (frame->Complete() ? "complete" : "incomplete")
316 << " decodable video frame";
skvlad98bb6642016-04-07 15:36:45 -0700317 }
318
Peter Boströmb7d9a972015-12-18 16:01:11 +0100319 const int32_t ret = Decode(*frame);
320 _receiver.ReleaseFrame(frame);
321 return ret;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000322}
323
philipela45102f2017-02-22 05:30:39 -0800324// Used for the new jitter buffer.
philipelfd5a20f2016-11-15 00:57:57 -0800325// TODO(philipel): Clean up among the Decode functions as we replace
326// VCMEncodedFrame with FrameObject.
327int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100328 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
philipelfd5a20f2016-11-15 00:57:57 -0800329 return Decode(*frame);
330}
331
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000332int32_t VideoReceiver::RequestKeyFrame() {
Tommifbf3bce2018-02-21 15:56:05 +0100333 RTC_DCHECK_RUN_ON(&module_thread_checker_);
334
335 // Since we deregister from the module thread when the decoder thread isn't
336 // running, we should get no calls here if decoding isn't being done.
337 RTC_DCHECK(IsDecoderThreadRunning());
338
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000339 TRACE_EVENT0("webrtc", "RequestKeyFrame");
sprang3911c262016-04-15 01:24:14 -0700340 if (_frameTypeCallback != nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000341 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
342 if (ret < 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000343 return ret;
344 }
Tommifbf3bce2018-02-21 15:56:05 +0100345 rtc::CritScope cs(&process_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000346 _scheduleKeyRequest = false;
347 } else {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000348 return VCM_MISSING_CALLBACK;
349 }
350 return VCM_OK;
351}
352
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000353// Must be called from inside the receive side critical section.
354int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100355 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
tommidb23ea62017-03-03 07:21:18 -0800356 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000357 // Change decoder if payload type has changed
tommid0a71ba2017-03-14 04:16:20 -0700358 VCMGenericDecoder* decoder =
359 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
360 if (decoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000361 return VCM_NO_CODEC_REGISTERED;
362 }
philipel3042c2d2017-08-18 04:55:02 -0700363 return decoder->Decode(frame, clock_->TimeInMilliseconds());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000364}
365
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000366// Register possible receive codecs, can be called multiple times
367int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
368 int32_t numberOfCores,
369 bool requireKeyFrame) {
Tommifbf3bce2018-02-21 15:56:05 +0100370 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
371 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700372 if (receiveCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000373 return VCM_PARAMETER_ERROR;
374 }
philipel9d3ab612015-12-21 04:12:39 -0800375 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
376 requireKeyFrame)) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000377 return -1;
378 }
379 return 0;
380}
381
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000382// Incoming packet from network parsed and ready for decode, non blocking.
383int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000384 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000385 const WebRtcRTPHeader& rtpInfo) {
Tommifbf3bce2018-02-21 15:56:05 +0100386 RTC_DCHECK_RUN_ON(&module_thread_checker_);
Niels Möller8f7ce222019-03-21 15:43:58 +0100387 if (rtpInfo.frameType == VideoFrameType::kVideoFrameKey) {
philipel9d3ab612015-12-21 04:12:39 -0800388 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000389 rtpInfo.header.sequenceNumber);
390 }
sprang3911c262016-04-15 01:24:14 -0700391 if (incomingPayload == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000392 // The jitter buffer doesn't handle non-zero payload lengths for packets
393 // without payload.
394 // TODO(holmer): We should fix this in the jitter buffer.
395 payloadLength = 0;
396 }
Niels Möller075e7fd2019-03-12 12:29:22 +0100397 const VCMPacket packet(incomingPayload, payloadLength, rtpInfo.header,
398 rtpInfo.video_header(), rtpInfo.frameType,
399 rtpInfo.ntp_time_ms);
Johan Ahlers95348f72016-06-28 11:11:28 +0200400 int32_t ret = _receiver.InsertPacket(packet);
sprang3911c262016-04-15 01:24:14 -0700401
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000402 // TODO(holmer): Investigate if this somehow should use the key frame
403 // request scheduling to throttle the requests.
404 if (ret == VCM_FLUSH_INDICATOR) {
Peter Boströmed3277b2016-02-02 15:40:04 +0100405 {
sprang3911c262016-04-15 01:24:14 -0700406 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100407 drop_frames_until_keyframe_ = true;
408 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000409 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000410 } else if (ret < 0) {
411 return ret;
412 }
413 return VCM_OK;
414}
415
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000416// The estimated delay caused by rendering, defaults to
417// kDefaultRenderDelayMs = 10 ms
418int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) {
Tommifbf3bce2018-02-21 15:56:05 +0100419 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
420 RTC_DCHECK(!IsDecoderThreadRunning());
philipel721d4022016-12-15 07:10:57 -0800421 _timing->set_render_delay(timeMS);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000422 return VCM_OK;
423}
424
425// Current video delay
philipel9d3ab612015-12-21 04:12:39 -0800426int32_t VideoReceiver::Delay() const {
Tommifbf3bce2018-02-21 15:56:05 +0100427 RTC_DCHECK_RUN_ON(&module_thread_checker_);
philipel721d4022016-12-15 07:10:57 -0800428 return _timing->TargetVideoDelay();
philipel9d3ab612015-12-21 04:12:39 -0800429}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000430
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000431int VideoReceiver::SetReceiverRobustnessMode(
Niels Möller375b3462019-01-10 15:35:56 +0100432 VideoCodingModule::ReceiverRobustness robustnessMode) {
Tommifbf3bce2018-02-21 15:56:05 +0100433 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
434 RTC_DCHECK(!IsDecoderThreadRunning());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000435 switch (robustnessMode) {
436 case VideoCodingModule::kNone:
437 _receiver.SetNackMode(kNoNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000438 break;
439 case VideoCodingModule::kHardNack:
440 // Always wait for retransmissions (except when decoding with errors).
441 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000442 break;
tommia5c18d72017-03-20 10:43:23 -0700443 default:
444 RTC_NOTREACHED();
445 return VCM_PARAMETER_ERROR;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000446 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000447 return VCM_OK;
448}
449
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000450void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
451 int max_packet_age_to_nack,
452 int max_incomplete_time_ms) {
Tommifbf3bce2018-02-21 15:56:05 +0100453 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
454 RTC_DCHECK(!IsDecoderThreadRunning());
guidouc3372582017-04-04 07:16:21 -0700455 if (max_nack_list_size != 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000456 max_nack_list_size_ = max_nack_list_size;
guidouc3372582017-04-04 07:16:21 -0700457 }
philipel9d3ab612015-12-21 04:12:39 -0800458 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
459 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000460}
461
Tommifbf3bce2018-02-21 15:56:05 +0100462bool VideoReceiver::IsDecoderThreadRunning() {
463#if RTC_DCHECK_IS_ON
464 return decoder_thread_is_running_;
465#else
466 return true;
467#endif
468}
469
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000470} // namespace vcm
471} // namespace webrtc