blob: d2bc02965aadaa4d1a101610bde456724c66fd13 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "modules/include/module_common_types.h"
Tommifbf3bce2018-02-21 15:56:05 +010019#include "modules/utility/include/process_thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "modules/video_coding/decoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/video_coding/encoded_frame.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "modules/video_coding/generic_decoder.h"
23#include "modules/video_coding/include/video_coding.h"
24#include "modules/video_coding/include/video_coding_defines.h"
25#include "modules/video_coding/internal_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/video_coding/jitter_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "modules/video_coding/media_opt_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/video_coding/packet.h"
Yves Gerey3e707812018-11-28 16:47:49 +010029#include "modules/video_coding/receiver.h"
30#include "modules/video_coding/timing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/video_coding/video_coding_impl.h"
32#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/critical_section.h"
Tommifbf3bce2018-02-21 15:56:05 +010034#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/one_time_event.h"
Yves Gerey3e707812018-11-28 16:47:49 +010037#include "rtc_base/thread_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/trace_event.h"
39#include "system_wrappers/include/clock.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000040
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000041namespace webrtc {
42namespace vcm {
43
Niels Möllerdb64d992019-03-29 14:30:53 +010044VideoReceiver::VideoReceiver(Clock* clock, VCMTiming* timing)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000045 : clock_(clock),
philipel721d4022016-12-15 07:10:57 -080046 _timing(timing),
Niels Möllerdb64d992019-03-29 14:30:53 +010047 _receiver(_timing, clock_),
philipel721d4022016-12-15 07:10:57 -080048 _decodedFrameCallback(_timing, clock_),
sprang3911c262016-04-15 01:24:14 -070049 _frameTypeCallback(nullptr),
sprang3911c262016-04-15 01:24:14 -070050 _packetRequestCallback(nullptr),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000051 _scheduleKeyRequest(false),
Peter Boströmed3277b2016-02-02 15:40:04 +010052 drop_frames_until_keyframe_(false),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000053 max_nack_list_size_(0),
Niels Möllerf9063782018-02-20 16:09:48 +010054 _codecDataBase(),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000055 _receiveStatsTimer(1000, clock_),
56 _retransmissionTimer(10, clock_),
Tommifbf3bce2018-02-21 15:56:05 +010057 _keyRequestTimer(500, clock_) {
58 decoder_thread_checker_.DetachFromThread();
59 module_thread_checker_.DetachFromThread();
60}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000061
Tommifbf3bce2018-02-21 15:56:05 +010062VideoReceiver::~VideoReceiver() {
63 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
64}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000065
pbosa26ac922016-02-25 04:50:01 -080066void VideoReceiver::Process() {
Tommifbf3bce2018-02-21 15:56:05 +010067 RTC_DCHECK_RUN_ON(&module_thread_checker_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000068 // Receive-side statistics
philipela45102f2017-02-22 05:30:39 -080069
70 // TODO(philipel): Remove this if block when we know what to do with
71 // ReceiveStatisticsProxy::QualitySample.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000072 if (_receiveStatsTimer.TimeUntilProcess() == 0) {
73 _receiveStatsTimer.Processed();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000074 }
75
76 // Key frame requests
77 if (_keyRequestTimer.TimeUntilProcess() == 0) {
78 _keyRequestTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010079 bool request_key_frame = _frameTypeCallback != nullptr;
80 if (request_key_frame) {
sprang3911c262016-04-15 01:24:14 -070081 rtc::CritScope cs(&process_crit_);
Tommifbf3bce2018-02-21 15:56:05 +010082 request_key_frame = _scheduleKeyRequest;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000083 }
pbosa26ac922016-02-25 04:50:01 -080084 if (request_key_frame)
85 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000086 }
87
88 // Packet retransmission requests
89 // TODO(holmer): Add API for changing Process interval and make sure it's
90 // disabled when NACK is off.
91 if (_retransmissionTimer.TimeUntilProcess() == 0) {
92 _retransmissionTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010093 bool callback_registered = _packetRequestCallback != nullptr;
94 uint16_t length = max_nack_list_size_;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000095 if (callback_registered && length > 0) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -070096 // Collect sequence numbers from the default receiver.
97 bool request_key_frame = false;
98 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
99 int32_t ret = VCM_OK;
100 if (request_key_frame) {
101 ret = RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000102 }
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700103 if (ret == VCM_OK && !nackList.empty()) {
guidouc3372582017-04-04 07:16:21 -0700104 rtc::CritScope cs(&process_crit_);
sprang3911c262016-04-15 01:24:14 -0700105 if (_packetRequestCallback != nullptr) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700106 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000107 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000108 }
109 }
110 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000111}
112
Tommifbf3bce2018-02-21 15:56:05 +0100113void VideoReceiver::ProcessThreadAttached(ProcessThread* process_thread) {
114 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
115 if (process_thread) {
116 is_attached_to_process_thread_ = true;
117 RTC_DCHECK(!process_thread_ || process_thread_ == process_thread);
118 process_thread_ = process_thread;
119 } else {
120 is_attached_to_process_thread_ = false;
121 }
122}
123
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000124int64_t VideoReceiver::TimeUntilNextProcess() {
Tommifbf3bce2018-02-21 15:56:05 +0100125 RTC_DCHECK_RUN_ON(&module_thread_checker_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000126 int64_t timeUntilNextProcess = _receiveStatsTimer.TimeUntilProcess();
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000127 if (_receiver.NackMode() != kNoNack) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000128 // We need a Process call more often if we are relying on
129 // retransmissions
130 timeUntilNextProcess =
131 VCM_MIN(timeUntilNextProcess, _retransmissionTimer.TimeUntilProcess());
132 }
133 timeUntilNextProcess =
134 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
135
136 return timeUntilNextProcess;
137}
138
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000139int32_t VideoReceiver::SetReceiveChannelParameters(int64_t rtt) {
Tommifbf3bce2018-02-21 15:56:05 +0100140 RTC_DCHECK_RUN_ON(&module_thread_checker_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000141 _receiver.UpdateRtt(rtt);
142 return 0;
143}
144
145// Enable or disable a video protection method.
146// Note: This API should be deprecated, as it does not offer a distinction
tommia5c18d72017-03-20 10:43:23 -0700147// between the protection method and decoding with or without errors.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000148int32_t VideoReceiver::SetVideoProtection(VCMVideoProtection videoProtection,
149 bool enable) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000150 switch (videoProtection) {
pbosba8c15b2015-07-14 09:36:34 -0700151 case kProtectionNack: {
henrikg91d6ede2015-09-17 00:24:34 -0700152 RTC_DCHECK(enable);
pbosba8c15b2015-07-14 09:36:34 -0700153 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000154 break;
155 }
156
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000157 case kProtectionNackFEC: {
henrikg91d6ede2015-09-17 00:24:34 -0700158 RTC_DCHECK(enable);
Yves Gerey665174f2018-06-19 15:03:05 +0200159 _receiver.SetNackMode(kNack, media_optimization::kLowRttNackMs,
philipelae284082016-05-09 12:14:29 +0200160 media_optimization::kMaxRttDelayThreshold);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000161 break;
162 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000163 case kProtectionFEC:
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000164 case kProtectionNone:
pbosba8c15b2015-07-14 09:36:34 -0700165 // No receiver-side protection.
henrikg91d6ede2015-09-17 00:24:34 -0700166 RTC_DCHECK(enable);
pbosba8c15b2015-07-14 09:36:34 -0700167 _receiver.SetNackMode(kNoNack, -1, -1);
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000168 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000169 }
170 return VCM_OK;
171}
172
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000173// Register a receive callback. Will be called whenever there is a new frame
174// ready for rendering.
175int32_t VideoReceiver::RegisterReceiveCallback(
176 VCMReceiveCallback* receiveCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100177 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
178 RTC_DCHECK(!IsDecoderThreadRunning());
179 // This value is set before the decoder thread starts and unset after
180 // the decoder thread has been stopped.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000181 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
182 return VCM_OK;
183}
184
perkj796cfaf2015-12-10 09:27:38 -0800185// Register an externally defined decoder object.
Peter Boström795dbe42015-11-27 14:09:07 +0100186void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800187 uint8_t payloadType) {
Tommifbf3bce2018-02-21 15:56:05 +0100188 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
189 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700190 if (externalDecoder == nullptr) {
Peter Boström795dbe42015-11-27 14:09:07 +0100191 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
Peter Boström395c7c62015-11-27 15:23:12 +0100192 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000193 }
perkj796cfaf2015-12-10 09:27:38 -0800194 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000195}
196
197// Register a frame type request callback.
198int32_t VideoReceiver::RegisterFrameTypeCallback(
199 VCMFrameTypeCallback* frameTypeCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100200 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
201 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
202 // This callback is used on the module thread, but since we don't get
203 // callbacks on the module thread while the decoder thread isn't running
204 // (and this function must not be called when the decoder is running),
205 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000206 _frameTypeCallback = frameTypeCallback;
207 return VCM_OK;
208}
209
210int32_t VideoReceiver::RegisterPacketRequestCallback(
211 VCMPacketRequestCallback* callback) {
Tommifbf3bce2018-02-21 15:56:05 +0100212 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
213 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
214 // This callback is used on the module thread, but since we don't get
215 // callbacks on the module thread while the decoder thread isn't running
216 // (and this function must not be called when the decoder is running),
217 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000218 _packetRequestCallback = callback;
219 return VCM_OK;
220}
221
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000222void VideoReceiver::TriggerDecoderShutdown() {
Tommifbf3bce2018-02-21 15:56:05 +0100223 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
224 RTC_DCHECK(IsDecoderThreadRunning());
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000225 _receiver.TriggerDecoderShutdown();
226}
227
Tommifbf3bce2018-02-21 15:56:05 +0100228void VideoReceiver::DecoderThreadStarting() {
229 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
230 RTC_DCHECK(!IsDecoderThreadRunning());
231 if (process_thread_ && !is_attached_to_process_thread_) {
232 process_thread_->RegisterModule(this, RTC_FROM_HERE);
233 }
234#if RTC_DCHECK_IS_ON
235 decoder_thread_is_running_ = true;
236#endif
237}
238
239void VideoReceiver::DecoderThreadStopped() {
240 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
241 RTC_DCHECK(IsDecoderThreadRunning());
242 if (process_thread_ && is_attached_to_process_thread_) {
243 process_thread_->DeRegisterModule(this);
244 }
245#if RTC_DCHECK_IS_ON
246 decoder_thread_is_running_ = false;
247 decoder_thread_checker_.DetachFromThread();
248#endif
249}
250
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000251// Decode next frame, blocking.
252// Should be called as often as possible to get the most out of the decoder.
253int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
Tommifbf3bce2018-02-21 15:56:05 +0100254 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
255 VCMEncodedFrame* frame = _receiver.FrameForDecoding(
256 maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000257
Peter Boströmb7d9a972015-12-18 16:01:11 +0100258 if (!frame)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000259 return VCM_FRAME_NOT_READY;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000260
Tommifbf3bce2018-02-21 15:56:05 +0100261 bool drop_frame = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100262 {
sprang3911c262016-04-15 01:24:14 -0700263 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100264 if (drop_frames_until_keyframe_) {
265 // Still getting delta frames, schedule another keyframe request as if
266 // decode failed.
Niels Möller8f7ce222019-03-21 15:43:58 +0100267 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Tommifbf3bce2018-02-21 15:56:05 +0100268 drop_frame = true;
Peter Boströmed3277b2016-02-02 15:40:04 +0100269 _scheduleKeyRequest = true;
Tommifbf3bce2018-02-21 15:56:05 +0100270 // TODO(tommi): Consider if we could instead post a task to the module
271 // thread and call RequestKeyFrame directly. Here we call WakeUp so that
272 // TimeUntilNextProcess() gets called straight away.
273 process_thread_->WakeUp(this);
274 } else {
275 drop_frames_until_keyframe_ = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100276 }
Peter Boströmed3277b2016-02-02 15:40:04 +0100277 }
278 }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100279
Tommifbf3bce2018-02-21 15:56:05 +0100280 if (drop_frame) {
281 _receiver.ReleaseFrame(frame);
282 return VCM_FRAME_NOT_READY;
283 }
284
sprang3911c262016-04-15 01:24:14 -0700285 // If this frame was too late, we should adjust the delay accordingly
philipel721d4022016-12-15 07:10:57 -0800286 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
287 clock_->TimeInMilliseconds());
skvlad98bb6642016-04-07 15:36:45 -0700288
289 if (first_frame_received_()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100290 RTC_LOG(LS_INFO) << "Received first "
291 << (frame->Complete() ? "complete" : "incomplete")
292 << " decodable video frame";
skvlad98bb6642016-04-07 15:36:45 -0700293 }
294
Peter Boströmb7d9a972015-12-18 16:01:11 +0100295 const int32_t ret = Decode(*frame);
296 _receiver.ReleaseFrame(frame);
297 return ret;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000298}
299
philipela45102f2017-02-22 05:30:39 -0800300// Used for the new jitter buffer.
philipelfd5a20f2016-11-15 00:57:57 -0800301// TODO(philipel): Clean up among the Decode functions as we replace
302// VCMEncodedFrame with FrameObject.
303int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100304 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
philipelfd5a20f2016-11-15 00:57:57 -0800305 return Decode(*frame);
306}
307
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000308int32_t VideoReceiver::RequestKeyFrame() {
Tommifbf3bce2018-02-21 15:56:05 +0100309 RTC_DCHECK_RUN_ON(&module_thread_checker_);
310
311 // Since we deregister from the module thread when the decoder thread isn't
312 // running, we should get no calls here if decoding isn't being done.
313 RTC_DCHECK(IsDecoderThreadRunning());
314
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000315 TRACE_EVENT0("webrtc", "RequestKeyFrame");
sprang3911c262016-04-15 01:24:14 -0700316 if (_frameTypeCallback != nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000317 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
318 if (ret < 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000319 return ret;
320 }
Tommifbf3bce2018-02-21 15:56:05 +0100321 rtc::CritScope cs(&process_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000322 _scheduleKeyRequest = false;
323 } else {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000324 return VCM_MISSING_CALLBACK;
325 }
326 return VCM_OK;
327}
328
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000329// Must be called from inside the receive side critical section.
330int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100331 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
tommidb23ea62017-03-03 07:21:18 -0800332 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000333 // Change decoder if payload type has changed
tommid0a71ba2017-03-14 04:16:20 -0700334 VCMGenericDecoder* decoder =
335 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
336 if (decoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000337 return VCM_NO_CODEC_REGISTERED;
338 }
philipel3042c2d2017-08-18 04:55:02 -0700339 return decoder->Decode(frame, clock_->TimeInMilliseconds());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000340}
341
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000342// Register possible receive codecs, can be called multiple times
343int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
344 int32_t numberOfCores,
345 bool requireKeyFrame) {
Tommifbf3bce2018-02-21 15:56:05 +0100346 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
347 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700348 if (receiveCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000349 return VCM_PARAMETER_ERROR;
350 }
philipel9d3ab612015-12-21 04:12:39 -0800351 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
352 requireKeyFrame)) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000353 return -1;
354 }
355 return 0;
356}
357
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000358// Incoming packet from network parsed and ready for decode, non blocking.
359int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000360 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000361 const WebRtcRTPHeader& rtpInfo) {
Tommifbf3bce2018-02-21 15:56:05 +0100362 RTC_DCHECK_RUN_ON(&module_thread_checker_);
Niels Möller8f7ce222019-03-21 15:43:58 +0100363 if (rtpInfo.frameType == VideoFrameType::kVideoFrameKey) {
philipel9d3ab612015-12-21 04:12:39 -0800364 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000365 rtpInfo.header.sequenceNumber);
366 }
sprang3911c262016-04-15 01:24:14 -0700367 if (incomingPayload == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000368 // The jitter buffer doesn't handle non-zero payload lengths for packets
369 // without payload.
370 // TODO(holmer): We should fix this in the jitter buffer.
371 payloadLength = 0;
372 }
Niels Möller075e7fd2019-03-12 12:29:22 +0100373 const VCMPacket packet(incomingPayload, payloadLength, rtpInfo.header,
374 rtpInfo.video_header(), rtpInfo.frameType,
375 rtpInfo.ntp_time_ms);
Johan Ahlers95348f72016-06-28 11:11:28 +0200376 int32_t ret = _receiver.InsertPacket(packet);
sprang3911c262016-04-15 01:24:14 -0700377
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000378 // TODO(holmer): Investigate if this somehow should use the key frame
379 // request scheduling to throttle the requests.
380 if (ret == VCM_FLUSH_INDICATOR) {
Peter Boströmed3277b2016-02-02 15:40:04 +0100381 {
sprang3911c262016-04-15 01:24:14 -0700382 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100383 drop_frames_until_keyframe_ = true;
384 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000385 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000386 } else if (ret < 0) {
387 return ret;
388 }
389 return VCM_OK;
390}
391
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000392// The estimated delay caused by rendering, defaults to
393// kDefaultRenderDelayMs = 10 ms
394int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) {
Tommifbf3bce2018-02-21 15:56:05 +0100395 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
396 RTC_DCHECK(!IsDecoderThreadRunning());
philipel721d4022016-12-15 07:10:57 -0800397 _timing->set_render_delay(timeMS);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000398 return VCM_OK;
399}
400
401// Current video delay
philipel9d3ab612015-12-21 04:12:39 -0800402int32_t VideoReceiver::Delay() const {
Tommifbf3bce2018-02-21 15:56:05 +0100403 RTC_DCHECK_RUN_ON(&module_thread_checker_);
philipel721d4022016-12-15 07:10:57 -0800404 return _timing->TargetVideoDelay();
philipel9d3ab612015-12-21 04:12:39 -0800405}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000406
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000407int VideoReceiver::SetReceiverRobustnessMode(
Niels Möller375b3462019-01-10 15:35:56 +0100408 VideoCodingModule::ReceiverRobustness robustnessMode) {
Tommifbf3bce2018-02-21 15:56:05 +0100409 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
410 RTC_DCHECK(!IsDecoderThreadRunning());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000411 switch (robustnessMode) {
412 case VideoCodingModule::kNone:
413 _receiver.SetNackMode(kNoNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000414 break;
415 case VideoCodingModule::kHardNack:
416 // Always wait for retransmissions (except when decoding with errors).
417 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000418 break;
tommia5c18d72017-03-20 10:43:23 -0700419 default:
420 RTC_NOTREACHED();
421 return VCM_PARAMETER_ERROR;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000422 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000423 return VCM_OK;
424}
425
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000426void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
427 int max_packet_age_to_nack,
428 int max_incomplete_time_ms) {
Tommifbf3bce2018-02-21 15:56:05 +0100429 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
430 RTC_DCHECK(!IsDecoderThreadRunning());
guidouc3372582017-04-04 07:16:21 -0700431 if (max_nack_list_size != 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000432 max_nack_list_size_ = max_nack_list_size;
guidouc3372582017-04-04 07:16:21 -0700433 }
philipel9d3ab612015-12-21 04:12:39 -0800434 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
435 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000436}
437
Tommifbf3bce2018-02-21 15:56:05 +0100438bool VideoReceiver::IsDecoderThreadRunning() {
439#if RTC_DCHECK_IS_ON
440 return decoder_thread_is_running_;
441#else
442 return true;
443#endif
444}
445
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000446} // namespace vcm
447} // namespace webrtc