blob: f1503ba89e2aab7359fbb1fd225893ace3f65729 [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
pbosba8c15b2015-07-14 09:36:34 -070011#include "webrtc/base/checks.h"
tommie3aa88b2017-04-04 03:53:02 -070012#include "webrtc/base/location.h"
pbos854e84c2015-11-16 16:39:06 -080013#include "webrtc/base/logging.h"
tommie4f96502015-10-20 23:00:48 -070014#include "webrtc/base/trace_event.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000015#include "webrtc/common_types.h"
16#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
tommie3aa88b2017-04-04 03:53:02 -070017#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/encoded_frame.h"
tommie3aa88b2017-04-04 03:53:02 -070019#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010020#include "webrtc/modules/video_coding/jitter_buffer.h"
21#include "webrtc/modules/video_coding/packet.h"
22#include "webrtc/modules/video_coding/video_coding_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010023#include "webrtc/system_wrappers/include/clock.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000024
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000025namespace webrtc {
26namespace vcm {
27
philipel83f831a2016-03-12 03:30:23 -080028VideoReceiver::VideoReceiver(Clock* clock,
29 EventFactory* event_factory,
sprang3911c262016-04-15 01:24:14 -070030 EncodedImageCallback* pre_decode_image_callback,
philipel721d4022016-12-15 07:10:57 -080031 VCMTiming* timing,
philipel83f831a2016-03-12 03:30:23 -080032 NackSender* nack_sender,
33 KeyFrameRequestSender* keyframe_request_sender)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000034 : clock_(clock),
philipel721d4022016-12-15 07:10:57 -080035 _timing(timing),
36 _receiver(_timing,
philipel83f831a2016-03-12 03:30:23 -080037 clock_,
38 event_factory,
39 nack_sender,
40 keyframe_request_sender),
philipel721d4022016-12-15 07:10:57 -080041 _decodedFrameCallback(_timing, clock_),
sprang3911c262016-04-15 01:24:14 -070042 _frameTypeCallback(nullptr),
43 _receiveStatsCallback(nullptr),
sprang3911c262016-04-15 01:24:14 -070044 _packetRequestCallback(nullptr),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000045 _scheduleKeyRequest(false),
Peter Boströmed3277b2016-02-02 15:40:04 +010046 drop_frames_until_keyframe_(false),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000047 max_nack_list_size_(0),
perkjf5b2e512016-07-05 08:34:04 -070048 _codecDataBase(nullptr),
sprang3911c262016-04-15 01:24:14 -070049 pre_decode_image_callback_(pre_decode_image_callback),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000050 _receiveStatsTimer(1000, clock_),
51 _retransmissionTimer(10, clock_),
tommie3aa88b2017-04-04 03:53:02 -070052 _keyRequestTimer(500, clock_) {
53 decoder_thread_.DetachFromThread();
54 module_thread_.DetachFromThread();
55}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000056
tommie3aa88b2017-04-04 03:53:02 -070057VideoReceiver::~VideoReceiver() {
58 RTC_DCHECK_RUN_ON(&construction_thread_);
59}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000060
pbosa26ac922016-02-25 04:50:01 -080061void VideoReceiver::Process() {
tommie3aa88b2017-04-04 03:53:02 -070062 RTC_DCHECK_RUN_ON(&module_thread_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000063 // Receive-side statistics
philipela45102f2017-02-22 05:30:39 -080064
65 // TODO(philipel): Remove this if block when we know what to do with
66 // ReceiveStatisticsProxy::QualitySample.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000067 if (_receiveStatsTimer.TimeUntilProcess() == 0) {
68 _receiveStatsTimer.Processed();
sprang3911c262016-04-15 01:24:14 -070069 if (_receiveStatsCallback != nullptr) {
philipela45102f2017-02-22 05:30:39 -080070 _receiveStatsCallback->OnReceiveRatesUpdated(0, 0);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +000071 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000072 }
73
74 // Key frame requests
75 if (_keyRequestTimer.TimeUntilProcess() == 0) {
76 _keyRequestTimer.Processed();
tommie3aa88b2017-04-04 03:53:02 -070077 bool request_key_frame = _frameTypeCallback != nullptr;
78 if (request_key_frame) {
sprang3911c262016-04-15 01:24:14 -070079 rtc::CritScope cs(&process_crit_);
tommie3aa88b2017-04-04 03:53:02 -070080 request_key_frame = _scheduleKeyRequest;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000081 }
pbosa26ac922016-02-25 04:50:01 -080082 if (request_key_frame)
83 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000084 }
85
86 // Packet retransmission requests
87 // TODO(holmer): Add API for changing Process interval and make sure it's
88 // disabled when NACK is off.
89 if (_retransmissionTimer.TimeUntilProcess() == 0) {
90 _retransmissionTimer.Processed();
tommie3aa88b2017-04-04 03:53:02 -070091 bool callback_registered = _packetRequestCallback != nullptr;
92 uint16_t length = max_nack_list_size_;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000093 if (callback_registered && length > 0) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -070094 // Collect sequence numbers from the default receiver.
95 bool request_key_frame = false;
96 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
97 int32_t ret = VCM_OK;
98 if (request_key_frame) {
99 ret = RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100 }
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700101 if (ret == VCM_OK && !nackList.empty()) {
sprang3911c262016-04-15 01:24:14 -0700102 if (_packetRequestCallback != nullptr) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700103 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000104 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000105 }
106 }
107 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000108}
109
tommie3aa88b2017-04-04 03:53:02 -0700110void VideoReceiver::ProcessThreadAttached(ProcessThread* process_thread) {
111 RTC_DCHECK_RUN_ON(&construction_thread_);
112 if (process_thread) {
113 is_attached_to_process_thread_ = true;
114 process_thread_ = process_thread;
115 } else {
116 is_attached_to_process_thread_ = false;
117 }
118}
119
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000120int64_t VideoReceiver::TimeUntilNextProcess() {
tommie3aa88b2017-04-04 03:53:02 -0700121 RTC_DCHECK_RUN_ON(&module_thread_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000122 int64_t timeUntilNextProcess = _receiveStatsTimer.TimeUntilProcess();
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000123 if (_receiver.NackMode() != kNoNack) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000124 // We need a Process call more often if we are relying on
125 // retransmissions
126 timeUntilNextProcess =
127 VCM_MIN(timeUntilNextProcess, _retransmissionTimer.TimeUntilProcess());
128 }
129 timeUntilNextProcess =
130 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
131
132 return timeUntilNextProcess;
133}
134
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000135int32_t VideoReceiver::SetReceiveChannelParameters(int64_t rtt) {
tommie3aa88b2017-04-04 03:53:02 -0700136 RTC_DCHECK_RUN_ON(&module_thread_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000137 _receiver.UpdateRtt(rtt);
138 return 0;
139}
140
141// Enable or disable a video protection method.
142// Note: This API should be deprecated, as it does not offer a distinction
tommia5c18d72017-03-20 10:43:23 -0700143// between the protection method and decoding with or without errors.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144int32_t VideoReceiver::SetVideoProtection(VCMVideoProtection videoProtection,
145 bool enable) {
146 // By default, do not decode with errors.
147 _receiver.SetDecodeErrorMode(kNoErrors);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000148 switch (videoProtection) {
pbosba8c15b2015-07-14 09:36:34 -0700149 case kProtectionNack: {
henrikg91d6ede2015-09-17 00:24:34 -0700150 RTC_DCHECK(enable);
pbosba8c15b2015-07-14 09:36:34 -0700151 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000152 break;
153 }
154
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000155 case kProtectionNackFEC: {
henrikg91d6ede2015-09-17 00:24:34 -0700156 RTC_DCHECK(enable);
philipelae284082016-05-09 12:14:29 +0200157 _receiver.SetNackMode(kNack,
158 media_optimization::kLowRttNackMs,
159 media_optimization::kMaxRttDelayThreshold);
pbosba8c15b2015-07-14 09:36:34 -0700160 _receiver.SetDecodeErrorMode(kNoErrors);
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);
168 _receiver.SetDecodeErrorMode(kWithErrors);
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000169 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000170 }
171 return VCM_OK;
172}
173
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000174// Register a receive callback. Will be called whenever there is a new frame
175// ready for rendering.
176int32_t VideoReceiver::RegisterReceiveCallback(
177 VCMReceiveCallback* receiveCallback) {
tommie3aa88b2017-04-04 03:53:02 -0700178 RTC_DCHECK_RUN_ON(&construction_thread_);
179 RTC_DCHECK(!IsDecoderThreadRunning());
180 // This value is set before the decoder thread starts and unset after
181 // the decoder thread has been stopped.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000182 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
183 return VCM_OK;
184}
185
186int32_t VideoReceiver::RegisterReceiveStatisticsCallback(
187 VCMReceiveStatisticsCallback* receiveStats) {
tommie3aa88b2017-04-04 03:53:02 -0700188 RTC_DCHECK_RUN_ON(&construction_thread_);
189 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
190 // |_receiver| is used on both the decoder and module threads.
191 // However, since we make sure that we never do anything on the module thread
192 // when the decoder thread is not running, we don't need a lock for the
193 // |_receiver| or |_receiveStatsCallback| here.
pbos@webrtc.org55707692014-12-19 15:45:03 +0000194 _receiver.RegisterStatsCallback(receiveStats);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000195 _receiveStatsCallback = receiveStats;
196 return VCM_OK;
197}
198
perkj796cfaf2015-12-10 09:27:38 -0800199// Register an externally defined decoder object.
Peter Boström795dbe42015-11-27 14:09:07 +0100200void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800201 uint8_t payloadType) {
tommie3aa88b2017-04-04 03:53:02 -0700202 RTC_DCHECK_RUN_ON(&construction_thread_);
203 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700204 if (externalDecoder == nullptr) {
Peter Boström795dbe42015-11-27 14:09:07 +0100205 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
Peter Boström395c7c62015-11-27 15:23:12 +0100206 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000207 }
perkj796cfaf2015-12-10 09:27:38 -0800208 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000209}
210
211// Register a frame type request callback.
212int32_t VideoReceiver::RegisterFrameTypeCallback(
213 VCMFrameTypeCallback* frameTypeCallback) {
tommie3aa88b2017-04-04 03:53:02 -0700214 RTC_DCHECK_RUN_ON(&construction_thread_);
215 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
216 // This callback is used on the module thread, but since we don't get
217 // callbacks on the module thread while the decoder thread isn't running
218 // (and this function must not be called when the decoder is running),
219 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000220 _frameTypeCallback = frameTypeCallback;
221 return VCM_OK;
222}
223
224int32_t VideoReceiver::RegisterPacketRequestCallback(
225 VCMPacketRequestCallback* callback) {
tommie3aa88b2017-04-04 03:53:02 -0700226 RTC_DCHECK_RUN_ON(&construction_thread_);
227 RTC_DCHECK(!IsDecoderThreadRunning() && !is_attached_to_process_thread_);
228 // This callback is used on the module thread, but since we don't get
229 // callbacks on the module thread while the decoder thread isn't running
230 // (and this function must not be called when the decoder is running),
231 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000232 _packetRequestCallback = callback;
233 return VCM_OK;
234}
235
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000236void VideoReceiver::TriggerDecoderShutdown() {
tommie3aa88b2017-04-04 03:53:02 -0700237 RTC_DCHECK_RUN_ON(&construction_thread_);
238 RTC_DCHECK(IsDecoderThreadRunning());
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000239 _receiver.TriggerDecoderShutdown();
240}
241
tommie3aa88b2017-04-04 03:53:02 -0700242void VideoReceiver::DecoderThreadStarting() {
243 RTC_DCHECK_RUN_ON(&construction_thread_);
244 RTC_DCHECK(!IsDecoderThreadRunning());
245 if (process_thread_ && !is_attached_to_process_thread_) {
246 process_thread_->RegisterModule(this, RTC_FROM_HERE);
247 }
248#if RTC_DCHECK_IS_ON
249 decoder_thread_is_running_ = true;
250#endif
251}
252
253void VideoReceiver::DecoderThreadStopped() {
254 RTC_DCHECK_RUN_ON(&construction_thread_);
255 RTC_DCHECK(IsDecoderThreadRunning());
256 if (process_thread_ && is_attached_to_process_thread_) {
257 process_thread_->DeRegisterModule(this);
258 }
259#if RTC_DCHECK_IS_ON
260 decoder_thread_is_running_ = false;
261 decoder_thread_.DetachFromThread();
262#endif
263}
264
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000265// Decode next frame, blocking.
266// Should be called as often as possible to get the most out of the decoder.
267int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
tommie3aa88b2017-04-04 03:53:02 -0700268 RTC_DCHECK_RUN_ON(&decoder_thread_);
269 VCMEncodedFrame* frame = _receiver.FrameForDecoding(
270 maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000271
Peter Boströmb7d9a972015-12-18 16:01:11 +0100272 if (!frame)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000273 return VCM_FRAME_NOT_READY;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000274
tommie3aa88b2017-04-04 03:53:02 -0700275 bool drop_frame = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100276 {
sprang3911c262016-04-15 01:24:14 -0700277 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100278 if (drop_frames_until_keyframe_) {
279 // Still getting delta frames, schedule another keyframe request as if
280 // decode failed.
281 if (frame->FrameType() != kVideoFrameKey) {
tommie3aa88b2017-04-04 03:53:02 -0700282 drop_frame = true;
Peter Boströmed3277b2016-02-02 15:40:04 +0100283 _scheduleKeyRequest = true;
tommie3aa88b2017-04-04 03:53:02 -0700284 } else {
285 drop_frames_until_keyframe_ = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100286 }
Peter Boströmed3277b2016-02-02 15:40:04 +0100287 }
288 }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100289
tommie3aa88b2017-04-04 03:53:02 -0700290 if (drop_frame) {
291 _receiver.ReleaseFrame(frame);
292 return VCM_FRAME_NOT_READY;
293 }
294
Peter Boströmb7d9a972015-12-18 16:01:11 +0100295 if (pre_decode_image_callback_) {
296 EncodedImage encoded_image(frame->EncodedImage());
297 int qp = -1;
298 if (qp_parser_.GetQp(*frame, &qp)) {
299 encoded_image.qp_ = qp;
sprang@webrtc.org40709352013-11-26 11:41:59 +0000300 }
sergeyu2cb155a2016-11-04 11:39:29 -0700301 pre_decode_image_callback_->OnEncodedImage(encoded_image,
302 frame->CodecSpecific(), nullptr);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100303 }
sprang@webrtc.org40709352013-11-26 11:41:59 +0000304
sprang3911c262016-04-15 01:24:14 -0700305 // If this frame was too late, we should adjust the delay accordingly
philipel721d4022016-12-15 07:10:57 -0800306 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
307 clock_->TimeInMilliseconds());
skvlad98bb6642016-04-07 15:36:45 -0700308
309 if (first_frame_received_()) {
310 LOG(LS_INFO) << "Received first "
311 << (frame->Complete() ? "complete" : "incomplete")
312 << " decodable video frame";
313 }
314
Peter Boströmb7d9a972015-12-18 16:01:11 +0100315 const int32_t ret = Decode(*frame);
316 _receiver.ReleaseFrame(frame);
317 return ret;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000318}
319
philipela45102f2017-02-22 05:30:39 -0800320// Used for the new jitter buffer.
philipelfd5a20f2016-11-15 00:57:57 -0800321// TODO(philipel): Clean up among the Decode functions as we replace
322// VCMEncodedFrame with FrameObject.
323int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) {
tommie3aa88b2017-04-04 03:53:02 -0700324 RTC_DCHECK_RUN_ON(&decoder_thread_);
philipel266f0a42016-11-28 08:49:07 -0800325 if (pre_decode_image_callback_) {
326 EncodedImage encoded_image(frame->EncodedImage());
327 int qp = -1;
328 if (qp_parser_.GetQp(*frame, &qp)) {
329 encoded_image.qp_ = qp;
330 }
331 pre_decode_image_callback_->OnEncodedImage(encoded_image,
332 frame->CodecSpecific(), nullptr);
333 }
philipelfd5a20f2016-11-15 00:57:57 -0800334 return Decode(*frame);
335}
336
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000337int32_t VideoReceiver::RequestKeyFrame() {
tommie3aa88b2017-04-04 03:53:02 -0700338 RTC_DCHECK_RUN_ON(&module_thread_);
339
340 // Since we deregister from the module thread when the decoder thread isn't
341 // running, we should get no calls here if decoding isn't being done.
342 RTC_DCHECK(IsDecoderThreadRunning());
343
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000344 TRACE_EVENT0("webrtc", "RequestKeyFrame");
sprang3911c262016-04-15 01:24:14 -0700345 if (_frameTypeCallback != nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000346 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
347 if (ret < 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000348 return ret;
349 }
tommie3aa88b2017-04-04 03:53:02 -0700350 rtc::CritScope cs(&process_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000351 _scheduleKeyRequest = false;
352 } else {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000353 return VCM_MISSING_CALLBACK;
354 }
355 return VCM_OK;
356}
357
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000358// Must be called from inside the receive side critical section.
359int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
tommie3aa88b2017-04-04 03:53:02 -0700360 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommidb23ea62017-03-03 07:21:18 -0800361 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000362 // Change decoder if payload type has changed
tommid0a71ba2017-03-14 04:16:20 -0700363 VCMGenericDecoder* decoder =
364 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
365 if (decoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000366 return VCM_NO_CODEC_REGISTERED;
367 }
368 // Decode a frame
tommid0a71ba2017-03-14 04:16:20 -0700369 int32_t ret = decoder->Decode(frame, clock_->TimeInMilliseconds());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000370
371 // Check for failed decoding, run frame type request callback if needed.
tommie3aa88b2017-04-04 03:53:02 -0700372 bool request_key_frame = (ret < 0);
nissecd386eb2017-03-14 08:54:43 -0700373
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000374 if (!frame.Complete() || frame.MissingFrame()) {
pbos081af252015-07-27 08:02:22 -0700375 request_key_frame = true;
376 ret = VCM_OK;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000377 }
tommie3aa88b2017-04-04 03:53:02 -0700378
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000379 if (request_key_frame) {
sprang3911c262016-04-15 01:24:14 -0700380 rtc::CritScope cs(&process_crit_);
tommie3aa88b2017-04-04 03:53:02 -0700381 if (!_scheduleKeyRequest) {
382 _scheduleKeyRequest = true;
383 // TODO(tommi): Consider if we could instead post a task to the module
384 // thread and call RequestKeyFrame directly. Here we call WakeUp so that
385 // TimeUntilNextProcess() gets called straight away.
386 process_thread_->WakeUp(this);
387 }
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000388 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000389 return ret;
390}
391
tommie3aa88b2017-04-04 03:53:02 -0700392#if defined(WEBRTC_ANDROID)
393void VideoReceiver::PollDecodedFrames() {
394 RTC_DCHECK_RUN_ON(&decoder_thread_);
395 auto* current_decoder = _codecDataBase.GetCurrentDecoder();
396 if (current_decoder)
397 current_decoder->PollDecodedFrames();
398}
399#endif
400
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000401// Register possible receive codecs, can be called multiple times
402int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
403 int32_t numberOfCores,
404 bool requireKeyFrame) {
tommie3aa88b2017-04-04 03:53:02 -0700405 RTC_DCHECK_RUN_ON(&construction_thread_);
406 RTC_DCHECK(!IsDecoderThreadRunning());
sprang3911c262016-04-15 01:24:14 -0700407 if (receiveCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000408 return VCM_PARAMETER_ERROR;
409 }
philipel9d3ab612015-12-21 04:12:39 -0800410 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
411 requireKeyFrame)) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000412 return -1;
413 }
414 return 0;
415}
416
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000417// Incoming packet from network parsed and ready for decode, non blocking.
418int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000419 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000420 const WebRtcRTPHeader& rtpInfo) {
tommie3aa88b2017-04-04 03:53:02 -0700421 RTC_DCHECK_RUN_ON(&module_thread_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000422 if (rtpInfo.frameType == kVideoFrameKey) {
philipel9d3ab612015-12-21 04:12:39 -0800423 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000424 rtpInfo.header.sequenceNumber);
425 }
sprang3911c262016-04-15 01:24:14 -0700426 if (incomingPayload == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000427 // The jitter buffer doesn't handle non-zero payload lengths for packets
428 // without payload.
429 // TODO(holmer): We should fix this in the jitter buffer.
430 payloadLength = 0;
431 }
432 const VCMPacket packet(incomingPayload, payloadLength, rtpInfo);
Johan Ahlers95348f72016-06-28 11:11:28 +0200433 int32_t ret = _receiver.InsertPacket(packet);
sprang3911c262016-04-15 01:24:14 -0700434
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000435 // TODO(holmer): Investigate if this somehow should use the key frame
436 // request scheduling to throttle the requests.
437 if (ret == VCM_FLUSH_INDICATOR) {
Peter Boströmed3277b2016-02-02 15:40:04 +0100438 {
sprang3911c262016-04-15 01:24:14 -0700439 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100440 drop_frames_until_keyframe_ = true;
441 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000442 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000443 } else if (ret < 0) {
444 return ret;
445 }
446 return VCM_OK;
447}
448
449// Minimum playout delay (used for lip-sync). This is the minimum delay required
450// to sync with audio. Not included in VideoCodingModule::Delay()
451// Defaults to 0 ms.
452int32_t VideoReceiver::SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) {
tommie3aa88b2017-04-04 03:53:02 -0700453 RTC_DCHECK_RUN_ON(&module_thread_);
philipel721d4022016-12-15 07:10:57 -0800454 _timing->set_min_playout_delay(minPlayoutDelayMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000455 return VCM_OK;
456}
457
458// The estimated delay caused by rendering, defaults to
459// kDefaultRenderDelayMs = 10 ms
460int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) {
tommie3aa88b2017-04-04 03:53:02 -0700461 RTC_DCHECK_RUN_ON(&construction_thread_);
462 RTC_DCHECK(!IsDecoderThreadRunning());
philipel721d4022016-12-15 07:10:57 -0800463 _timing->set_render_delay(timeMS);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000464 return VCM_OK;
465}
466
467// Current video delay
philipel9d3ab612015-12-21 04:12:39 -0800468int32_t VideoReceiver::Delay() const {
tommie3aa88b2017-04-04 03:53:02 -0700469 RTC_DCHECK_RUN_ON(&module_thread_);
philipel721d4022016-12-15 07:10:57 -0800470 return _timing->TargetVideoDelay();
philipel9d3ab612015-12-21 04:12:39 -0800471}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000472
tommie3aa88b2017-04-04 03:53:02 -0700473// Only used by VCMRobustnessTest.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000474int VideoReceiver::SetReceiverRobustnessMode(
tommia5c18d72017-03-20 10:43:23 -0700475 VideoCodingModule::ReceiverRobustness robustnessMode,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000476 VCMDecodeErrorMode decode_error_mode) {
tommie3aa88b2017-04-04 03:53:02 -0700477 RTC_DCHECK_RUN_ON(&construction_thread_);
478 RTC_DCHECK(!IsDecoderThreadRunning());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000479 switch (robustnessMode) {
480 case VideoCodingModule::kNone:
481 _receiver.SetNackMode(kNoNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000482 break;
483 case VideoCodingModule::kHardNack:
484 // Always wait for retransmissions (except when decoding with errors).
485 _receiver.SetNackMode(kNack, -1, -1);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000486 break;
tommia5c18d72017-03-20 10:43:23 -0700487 default:
488 RTC_NOTREACHED();
489 return VCM_PARAMETER_ERROR;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000490 }
491 _receiver.SetDecodeErrorMode(decode_error_mode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000492 return VCM_OK;
493}
494
495void VideoReceiver::SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) {
tommie3aa88b2017-04-04 03:53:02 -0700496 RTC_DCHECK_RUN_ON(&construction_thread_);
497 RTC_DCHECK(!IsDecoderThreadRunning());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000498 _receiver.SetDecodeErrorMode(decode_error_mode);
499}
500
501void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
502 int max_packet_age_to_nack,
503 int max_incomplete_time_ms) {
tommie3aa88b2017-04-04 03:53:02 -0700504 RTC_DCHECK_RUN_ON(&construction_thread_);
505 RTC_DCHECK(!IsDecoderThreadRunning());
506
507 if (max_nack_list_size != 0)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000508 max_nack_list_size_ = max_nack_list_size;
philipel9d3ab612015-12-21 04:12:39 -0800509 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
510 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000511}
512
513int VideoReceiver::SetMinReceiverDelay(int desired_delay_ms) {
tommie3aa88b2017-04-04 03:53:02 -0700514 RTC_DCHECK_RUN_ON(&construction_thread_);
515 RTC_DCHECK(!IsDecoderThreadRunning());
516 // TODO(tommi): Is the method only used by tests? Maybe could be offered
517 // via a test only subclass?
518 // Info from Stefan: If it is indeed only used by tests I think it's just that
519 // it hasn't been cleaned up when the calling code was cleaned up.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000520 return _receiver.SetMinReceiverDelay(desired_delay_ms);
521}
522
tommie3aa88b2017-04-04 03:53:02 -0700523bool VideoReceiver::IsDecoderThreadRunning() {
524#if RTC_DCHECK_IS_ON
525 return decoder_thread_is_running_;
526#else
527 return true;
528#endif
529}
530
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000531} // namespace vcm
532} // namespace webrtc