blob: ef80995a61b930228ad68d51081e81bfc6492a46 [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
philipel83f831a2016-03-12 03:30:23 -080044VideoReceiver::VideoReceiver(Clock* clock,
philipel721d4022016-12-15 07:10:57 -080045 VCMTiming* timing,
philipel83f831a2016-03-12 03:30:23 -080046 NackSender* nack_sender,
47 KeyFrameRequestSender* keyframe_request_sender)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000048 : clock_(clock),
philipel721d4022016-12-15 07:10:57 -080049 _timing(timing),
50 _receiver(_timing,
philipel83f831a2016-03-12 03:30:23 -080051 clock_,
philipel83f831a2016-03-12 03:30:23 -080052 nack_sender,
53 keyframe_request_sender),
philipel721d4022016-12-15 07:10:57 -080054 _decodedFrameCallback(_timing, clock_),
sprang3911c262016-04-15 01:24:14 -070055 _frameTypeCallback(nullptr),
sprang3911c262016-04-15 01:24:14 -070056 _packetRequestCallback(nullptr),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000057 _scheduleKeyRequest(false),
Peter Boströmed3277b2016-02-02 15:40:04 +010058 drop_frames_until_keyframe_(false),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000059 max_nack_list_size_(0),
Niels Möllerf9063782018-02-20 16:09:48 +010060 _codecDataBase(),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000061 _receiveStatsTimer(1000, clock_),
62 _retransmissionTimer(10, clock_),
Tommifbf3bce2018-02-21 15:56:05 +010063 _keyRequestTimer(500, clock_) {
64 decoder_thread_checker_.DetachFromThread();
65 module_thread_checker_.DetachFromThread();
66}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000067
Tommifbf3bce2018-02-21 15:56:05 +010068VideoReceiver::~VideoReceiver() {
69 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
70}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000071
pbosa26ac922016-02-25 04:50:01 -080072void VideoReceiver::Process() {
Tommifbf3bce2018-02-21 15:56:05 +010073 RTC_DCHECK_RUN_ON(&module_thread_checker_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000074 // Receive-side statistics
philipela45102f2017-02-22 05:30:39 -080075
76 // TODO(philipel): Remove this if block when we know what to do with
77 // ReceiveStatisticsProxy::QualitySample.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000078 if (_receiveStatsTimer.TimeUntilProcess() == 0) {
79 _receiveStatsTimer.Processed();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000080 }
81
82 // Key frame requests
83 if (_keyRequestTimer.TimeUntilProcess() == 0) {
84 _keyRequestTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010085 bool request_key_frame = _frameTypeCallback != nullptr;
86 if (request_key_frame) {
sprang3911c262016-04-15 01:24:14 -070087 rtc::CritScope cs(&process_crit_);
Tommifbf3bce2018-02-21 15:56:05 +010088 request_key_frame = _scheduleKeyRequest;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000089 }
pbosa26ac922016-02-25 04:50:01 -080090 if (request_key_frame)
91 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000092 }
93
94 // Packet retransmission requests
95 // TODO(holmer): Add API for changing Process interval and make sure it's
96 // disabled when NACK is off.
97 if (_retransmissionTimer.TimeUntilProcess() == 0) {
98 _retransmissionTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010099 bool callback_registered = _packetRequestCallback != nullptr;
100 uint16_t length = max_nack_list_size_;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000101 if (callback_registered && length > 0) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700102 // Collect sequence numbers from the default receiver.
103 bool request_key_frame = false;
104 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
105 int32_t ret = VCM_OK;
106 if (request_key_frame) {
107 ret = RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000108 }
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700109 if (ret == VCM_OK && !nackList.empty()) {
guidouc3372582017-04-04 07:16:21 -0700110 rtc::CritScope cs(&process_crit_);
sprang3911c262016-04-15 01:24:14 -0700111 if (_packetRequestCallback != nullptr) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700112 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000113 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000114 }
115 }
116 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000117}
118
Tommifbf3bce2018-02-21 15:56:05 +0100119void VideoReceiver::ProcessThreadAttached(ProcessThread* process_thread) {
120 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
121 if (process_thread) {
122 is_attached_to_process_thread_ = true;
123 RTC_DCHECK(!process_thread_ || process_thread_ == process_thread);
124 process_thread_ = process_thread;
125 } else {
126 is_attached_to_process_thread_ = false;
127 }
128}
129
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000130int64_t VideoReceiver::TimeUntilNextProcess() {
Tommifbf3bce2018-02-21 15:56:05 +0100131 RTC_DCHECK_RUN_ON(&module_thread_checker_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000132 int64_t timeUntilNextProcess = _receiveStatsTimer.TimeUntilProcess();
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000133 if (_receiver.NackMode() != kNoNack) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000134 // We need a Process call more often if we are relying on
135 // retransmissions
136 timeUntilNextProcess =
137 VCM_MIN(timeUntilNextProcess, _retransmissionTimer.TimeUntilProcess());
138 }
139 timeUntilNextProcess =
140 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
141
142 return timeUntilNextProcess;
143}
144
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000145int32_t VideoReceiver::SetReceiveChannelParameters(int64_t rtt) {
Tommifbf3bce2018-02-21 15:56:05 +0100146 RTC_DCHECK_RUN_ON(&module_thread_checker_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000147 _receiver.UpdateRtt(rtt);
148 return 0;
149}
150
151// Enable or disable a video protection method.
152// Note: This API should be deprecated, as it does not offer a distinction
tommia5c18d72017-03-20 10:43:23 -0700153// between the protection method and decoding with or without errors.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000154int32_t VideoReceiver::SetVideoProtection(VCMVideoProtection videoProtection,
155 bool enable) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000156 switch (videoProtection) {
pbosba8c15b2015-07-14 09:36:34 -0700157 case kProtectionNack: {
henrikg91d6ede2015-09-17 00:24:34 -0700158 RTC_DCHECK(enable);
pbosba8c15b2015-07-14 09:36:34 -0700159 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000160 break;
161 }
162
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000163 case kProtectionNackFEC: {
henrikg91d6ede2015-09-17 00:24:34 -0700164 RTC_DCHECK(enable);
Yves Gerey665174f2018-06-19 15:03:05 +0200165 _receiver.SetNackMode(kNack, media_optimization::kLowRttNackMs,
philipelae284082016-05-09 12:14:29 +0200166 media_optimization::kMaxRttDelayThreshold);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000167 break;
168 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000169 case kProtectionFEC:
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000170 case kProtectionNone:
pbosba8c15b2015-07-14 09:36:34 -0700171 // No receiver-side protection.
henrikg91d6ede2015-09-17 00:24:34 -0700172 RTC_DCHECK(enable);
pbosba8c15b2015-07-14 09:36:34 -0700173 _receiver.SetNackMode(kNoNack, -1, -1);
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000174 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000175 }
176 return VCM_OK;
177}
178
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000179// Register a receive callback. Will be called whenever there is a new frame
180// ready for rendering.
181int32_t VideoReceiver::RegisterReceiveCallback(
182 VCMReceiveCallback* receiveCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100183 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
184 RTC_DCHECK(!IsDecoderThreadRunning());
185 // This value is set before the decoder thread starts and unset after
186 // the decoder thread has been stopped.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000187 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
188 return VCM_OK;
189}
190
perkj796cfaf2015-12-10 09:27:38 -0800191// Register an externally defined decoder object.
Peter Boström795dbe42015-11-27 14:09:07 +0100192void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800193 uint8_t payloadType) {
Tommifbf3bce2018-02-21 15:56:05 +0100194 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
195 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700196 if (externalDecoder == nullptr) {
Peter Boström795dbe42015-11-27 14:09:07 +0100197 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
Peter Boström395c7c62015-11-27 15:23:12 +0100198 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000199 }
perkj796cfaf2015-12-10 09:27:38 -0800200 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000201}
202
203// Register a frame type request callback.
204int32_t VideoReceiver::RegisterFrameTypeCallback(
205 VCMFrameTypeCallback* frameTypeCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100206 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
207 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
208 // This callback is used on the module thread, but since we don't get
209 // callbacks on the module thread while the decoder thread isn't running
210 // (and this function must not be called when the decoder is running),
211 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000212 _frameTypeCallback = frameTypeCallback;
213 return VCM_OK;
214}
215
216int32_t VideoReceiver::RegisterPacketRequestCallback(
217 VCMPacketRequestCallback* callback) {
Tommifbf3bce2018-02-21 15:56:05 +0100218 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
219 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
220 // This callback is used on the module thread, but since we don't get
221 // callbacks on the module thread while the decoder thread isn't running
222 // (and this function must not be called when the decoder is running),
223 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000224 _packetRequestCallback = callback;
225 return VCM_OK;
226}
227
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000228void VideoReceiver::TriggerDecoderShutdown() {
Tommifbf3bce2018-02-21 15:56:05 +0100229 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
230 RTC_DCHECK(IsDecoderThreadRunning());
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000231 _receiver.TriggerDecoderShutdown();
232}
233
Tommifbf3bce2018-02-21 15:56:05 +0100234void VideoReceiver::DecoderThreadStarting() {
235 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
236 RTC_DCHECK(!IsDecoderThreadRunning());
237 if (process_thread_ && !is_attached_to_process_thread_) {
238 process_thread_->RegisterModule(this, RTC_FROM_HERE);
239 }
240#if RTC_DCHECK_IS_ON
241 decoder_thread_is_running_ = true;
242#endif
243}
244
245void VideoReceiver::DecoderThreadStopped() {
246 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
247 RTC_DCHECK(IsDecoderThreadRunning());
248 if (process_thread_ && is_attached_to_process_thread_) {
249 process_thread_->DeRegisterModule(this);
250 }
251#if RTC_DCHECK_IS_ON
252 decoder_thread_is_running_ = false;
253 decoder_thread_checker_.DetachFromThread();
254#endif
255}
256
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000257// Decode next frame, blocking.
258// Should be called as often as possible to get the most out of the decoder.
259int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
Tommifbf3bce2018-02-21 15:56:05 +0100260 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
261 VCMEncodedFrame* frame = _receiver.FrameForDecoding(
262 maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000263
Peter Boströmb7d9a972015-12-18 16:01:11 +0100264 if (!frame)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000265 return VCM_FRAME_NOT_READY;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000266
Tommifbf3bce2018-02-21 15:56:05 +0100267 bool drop_frame = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100268 {
sprang3911c262016-04-15 01:24:14 -0700269 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100270 if (drop_frames_until_keyframe_) {
271 // Still getting delta frames, schedule another keyframe request as if
272 // decode failed.
Niels Möller8f7ce222019-03-21 15:43:58 +0100273 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Tommifbf3bce2018-02-21 15:56:05 +0100274 drop_frame = true;
Peter Boströmed3277b2016-02-02 15:40:04 +0100275 _scheduleKeyRequest = true;
Tommifbf3bce2018-02-21 15:56:05 +0100276 // TODO(tommi): Consider if we could instead post a task to the module
277 // thread and call RequestKeyFrame directly. Here we call WakeUp so that
278 // TimeUntilNextProcess() gets called straight away.
279 process_thread_->WakeUp(this);
280 } else {
281 drop_frames_until_keyframe_ = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100282 }
Peter Boströmed3277b2016-02-02 15:40:04 +0100283 }
284 }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100285
Tommifbf3bce2018-02-21 15:56:05 +0100286 if (drop_frame) {
287 _receiver.ReleaseFrame(frame);
288 return VCM_FRAME_NOT_READY;
289 }
290
sprang3911c262016-04-15 01:24:14 -0700291 // If this frame was too late, we should adjust the delay accordingly
philipel721d4022016-12-15 07:10:57 -0800292 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
293 clock_->TimeInMilliseconds());
skvlad98bb6642016-04-07 15:36:45 -0700294
295 if (first_frame_received_()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100296 RTC_LOG(LS_INFO) << "Received first "
297 << (frame->Complete() ? "complete" : "incomplete")
298 << " decodable video frame";
skvlad98bb6642016-04-07 15:36:45 -0700299 }
300
Peter Boströmb7d9a972015-12-18 16:01:11 +0100301 const int32_t ret = Decode(*frame);
302 _receiver.ReleaseFrame(frame);
303 return ret;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000304}
305
philipela45102f2017-02-22 05:30:39 -0800306// Used for the new jitter buffer.
philipelfd5a20f2016-11-15 00:57:57 -0800307// TODO(philipel): Clean up among the Decode functions as we replace
308// VCMEncodedFrame with FrameObject.
309int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100310 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
philipelfd5a20f2016-11-15 00:57:57 -0800311 return Decode(*frame);
312}
313
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000314int32_t VideoReceiver::RequestKeyFrame() {
Tommifbf3bce2018-02-21 15:56:05 +0100315 RTC_DCHECK_RUN_ON(&module_thread_checker_);
316
317 // Since we deregister from the module thread when the decoder thread isn't
318 // running, we should get no calls here if decoding isn't being done.
319 RTC_DCHECK(IsDecoderThreadRunning());
320
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000321 TRACE_EVENT0("webrtc", "RequestKeyFrame");
sprang3911c262016-04-15 01:24:14 -0700322 if (_frameTypeCallback != nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000323 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
324 if (ret < 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000325 return ret;
326 }
Tommifbf3bce2018-02-21 15:56:05 +0100327 rtc::CritScope cs(&process_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000328 _scheduleKeyRequest = false;
329 } else {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000330 return VCM_MISSING_CALLBACK;
331 }
332 return VCM_OK;
333}
334
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000335// Must be called from inside the receive side critical section.
336int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100337 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
tommidb23ea62017-03-03 07:21:18 -0800338 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000339 // Change decoder if payload type has changed
tommid0a71ba2017-03-14 04:16:20 -0700340 VCMGenericDecoder* decoder =
341 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
342 if (decoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000343 return VCM_NO_CODEC_REGISTERED;
344 }
philipel3042c2d2017-08-18 04:55:02 -0700345 return decoder->Decode(frame, clock_->TimeInMilliseconds());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000346}
347
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000348// Register possible receive codecs, can be called multiple times
349int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
350 int32_t numberOfCores,
351 bool requireKeyFrame) {
Tommifbf3bce2018-02-21 15:56:05 +0100352 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
353 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700354 if (receiveCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000355 return VCM_PARAMETER_ERROR;
356 }
philipel9d3ab612015-12-21 04:12:39 -0800357 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
358 requireKeyFrame)) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000359 return -1;
360 }
361 return 0;
362}
363
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000364// Incoming packet from network parsed and ready for decode, non blocking.
365int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000366 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000367 const WebRtcRTPHeader& rtpInfo) {
Tommifbf3bce2018-02-21 15:56:05 +0100368 RTC_DCHECK_RUN_ON(&module_thread_checker_);
Niels Möller8f7ce222019-03-21 15:43:58 +0100369 if (rtpInfo.frameType == VideoFrameType::kVideoFrameKey) {
philipel9d3ab612015-12-21 04:12:39 -0800370 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000371 rtpInfo.header.sequenceNumber);
372 }
sprang3911c262016-04-15 01:24:14 -0700373 if (incomingPayload == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000374 // The jitter buffer doesn't handle non-zero payload lengths for packets
375 // without payload.
376 // TODO(holmer): We should fix this in the jitter buffer.
377 payloadLength = 0;
378 }
Niels Möller075e7fd2019-03-12 12:29:22 +0100379 const VCMPacket packet(incomingPayload, payloadLength, rtpInfo.header,
380 rtpInfo.video_header(), rtpInfo.frameType,
381 rtpInfo.ntp_time_ms);
Johan Ahlers95348f72016-06-28 11:11:28 +0200382 int32_t ret = _receiver.InsertPacket(packet);
sprang3911c262016-04-15 01:24:14 -0700383
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000384 // TODO(holmer): Investigate if this somehow should use the key frame
385 // request scheduling to throttle the requests.
386 if (ret == VCM_FLUSH_INDICATOR) {
Peter Boströmed3277b2016-02-02 15:40:04 +0100387 {
sprang3911c262016-04-15 01:24:14 -0700388 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100389 drop_frames_until_keyframe_ = true;
390 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000391 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000392 } else if (ret < 0) {
393 return ret;
394 }
395 return VCM_OK;
396}
397
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000398// The estimated delay caused by rendering, defaults to
399// kDefaultRenderDelayMs = 10 ms
400int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) {
Tommifbf3bce2018-02-21 15:56:05 +0100401 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
402 RTC_DCHECK(!IsDecoderThreadRunning());
philipel721d4022016-12-15 07:10:57 -0800403 _timing->set_render_delay(timeMS);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000404 return VCM_OK;
405}
406
407// Current video delay
philipel9d3ab612015-12-21 04:12:39 -0800408int32_t VideoReceiver::Delay() const {
Tommifbf3bce2018-02-21 15:56:05 +0100409 RTC_DCHECK_RUN_ON(&module_thread_checker_);
philipel721d4022016-12-15 07:10:57 -0800410 return _timing->TargetVideoDelay();
philipel9d3ab612015-12-21 04:12:39 -0800411}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000412
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000413int VideoReceiver::SetReceiverRobustnessMode(
Niels Möller375b3462019-01-10 15:35:56 +0100414 VideoCodingModule::ReceiverRobustness robustnessMode) {
Tommifbf3bce2018-02-21 15:56:05 +0100415 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
416 RTC_DCHECK(!IsDecoderThreadRunning());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000417 switch (robustnessMode) {
418 case VideoCodingModule::kNone:
419 _receiver.SetNackMode(kNoNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000420 break;
421 case VideoCodingModule::kHardNack:
422 // Always wait for retransmissions (except when decoding with errors).
423 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000424 break;
tommia5c18d72017-03-20 10:43:23 -0700425 default:
426 RTC_NOTREACHED();
427 return VCM_PARAMETER_ERROR;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000428 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000429 return VCM_OK;
430}
431
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000432void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
433 int max_packet_age_to_nack,
434 int max_incomplete_time_ms) {
Tommifbf3bce2018-02-21 15:56:05 +0100435 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
436 RTC_DCHECK(!IsDecoderThreadRunning());
guidouc3372582017-04-04 07:16:21 -0700437 if (max_nack_list_size != 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000438 max_nack_list_size_ = max_nack_list_size;
guidouc3372582017-04-04 07:16:21 -0700439 }
philipel9d3ab612015-12-21 04:12:39 -0800440 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
441 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000442}
443
Tommifbf3bce2018-02-21 15:56:05 +0100444bool VideoReceiver::IsDecoderThreadRunning() {
445#if RTC_DCHECK_IS_ON
446 return decoder_thread_is_running_;
447#else
448 return true;
449#endif
450}
451
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000452} // namespace vcm
453} // namespace webrtc