blob: a817293f2f24b3534bf52368bd7f53e752068c23 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstdint>
14#include <vector>
15
16#include "api/rtp_headers.h"
17#include "api/video_codecs/video_codec.h"
18#include "api/video_codecs/video_decoder.h"
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
Niels Möllerdb64d992019-03-29 14:30:53 +010045VideoReceiver::VideoReceiver(Clock* clock, VCMTiming* timing)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000046 : clock_(clock),
philipel721d4022016-12-15 07:10:57 -080047 _timing(timing),
Niels Möllerdb64d992019-03-29 14:30:53 +010048 _receiver(_timing, clock_),
philipel721d4022016-12-15 07:10:57 -080049 _decodedFrameCallback(_timing, clock_),
sprang3911c262016-04-15 01:24:14 -070050 _frameTypeCallback(nullptr),
sprang3911c262016-04-15 01:24:14 -070051 _packetRequestCallback(nullptr),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000052 _scheduleKeyRequest(false),
Peter Boströmed3277b2016-02-02 15:40:04 +010053 drop_frames_until_keyframe_(false),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000054 max_nack_list_size_(0),
Niels Möllerf9063782018-02-20 16:09:48 +010055 _codecDataBase(),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000056 _retransmissionTimer(10, clock_),
Tommifbf3bce2018-02-21 15:56:05 +010057 _keyRequestTimer(500, clock_) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020058 decoder_thread_checker_.Detach();
59 module_thread_checker_.Detach();
Tommifbf3bce2018-02-21 15:56:05 +010060}
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
69 // Key frame requests
70 if (_keyRequestTimer.TimeUntilProcess() == 0) {
71 _keyRequestTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010072 bool request_key_frame = _frameTypeCallback != nullptr;
73 if (request_key_frame) {
sprang3911c262016-04-15 01:24:14 -070074 rtc::CritScope cs(&process_crit_);
Tommifbf3bce2018-02-21 15:56:05 +010075 request_key_frame = _scheduleKeyRequest;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000076 }
pbosa26ac922016-02-25 04:50:01 -080077 if (request_key_frame)
78 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079 }
80
81 // Packet retransmission requests
82 // TODO(holmer): Add API for changing Process interval and make sure it's
83 // disabled when NACK is off.
84 if (_retransmissionTimer.TimeUntilProcess() == 0) {
85 _retransmissionTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010086 bool callback_registered = _packetRequestCallback != nullptr;
87 uint16_t length = max_nack_list_size_;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000088 if (callback_registered && length > 0) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -070089 // Collect sequence numbers from the default receiver.
90 bool request_key_frame = false;
91 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
92 int32_t ret = VCM_OK;
93 if (request_key_frame) {
94 ret = RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000095 }
Wan-Teh Changb1825a42015-06-03 15:03:35 -070096 if (ret == VCM_OK && !nackList.empty()) {
guidouc3372582017-04-04 07:16:21 -070097 rtc::CritScope cs(&process_crit_);
sprang3911c262016-04-15 01:24:14 -070098 if (_packetRequestCallback != nullptr) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -070099 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +0000100 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000101 }
102 }
103 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000104}
105
Tommifbf3bce2018-02-21 15:56:05 +0100106void VideoReceiver::ProcessThreadAttached(ProcessThread* process_thread) {
107 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
108 if (process_thread) {
109 is_attached_to_process_thread_ = true;
110 RTC_DCHECK(!process_thread_ || process_thread_ == process_thread);
111 process_thread_ = process_thread;
112 } else {
113 is_attached_to_process_thread_ = false;
114 }
115}
116
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000117int64_t VideoReceiver::TimeUntilNextProcess() {
Tommifbf3bce2018-02-21 15:56:05 +0100118 RTC_DCHECK_RUN_ON(&module_thread_checker_);
Tommieb03d282020-04-02 22:59:29 +0200119 int64_t timeUntilNextProcess = _retransmissionTimer.TimeUntilProcess();
Niels Möller691f62c2019-04-11 15:27:17 +0200120
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000121 timeUntilNextProcess =
122 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
123
124 return timeUntilNextProcess;
125}
126
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000127// Register a receive callback. Will be called whenever there is a new frame
128// ready for rendering.
129int32_t VideoReceiver::RegisterReceiveCallback(
130 VCMReceiveCallback* receiveCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100131 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
Tommifbf3bce2018-02-21 15:56:05 +0100132 // This value is set before the decoder thread starts and unset after
133 // the decoder thread has been stopped.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000134 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
135 return VCM_OK;
136}
137
perkj796cfaf2015-12-10 09:27:38 -0800138// Register an externally defined decoder object.
Peter Boström795dbe42015-11-27 14:09:07 +0100139void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800140 uint8_t payloadType) {
Tommifbf3bce2018-02-21 15:56:05 +0100141 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
sprang3911c262016-04-15 01:24:14 -0700142 if (externalDecoder == nullptr) {
Peter Boström795dbe42015-11-27 14:09:07 +0100143 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
Peter Boström395c7c62015-11-27 15:23:12 +0100144 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000145 }
perkj796cfaf2015-12-10 09:27:38 -0800146 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000147}
148
149// Register a frame type request callback.
150int32_t VideoReceiver::RegisterFrameTypeCallback(
151 VCMFrameTypeCallback* frameTypeCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100152 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
Niels Möller45b01c72019-09-10 13:02:28 +0200153 RTC_DCHECK(!is_attached_to_process_thread_);
Tommifbf3bce2018-02-21 15:56:05 +0100154 // This callback is used on the module thread, but since we don't get
155 // callbacks on the module thread while the decoder thread isn't running
156 // (and this function must not be called when the decoder is running),
157 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000158 _frameTypeCallback = frameTypeCallback;
159 return VCM_OK;
160}
161
162int32_t VideoReceiver::RegisterPacketRequestCallback(
163 VCMPacketRequestCallback* callback) {
Tommifbf3bce2018-02-21 15:56:05 +0100164 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
Niels Möller45b01c72019-09-10 13:02:28 +0200165 RTC_DCHECK(!is_attached_to_process_thread_);
Tommifbf3bce2018-02-21 15:56:05 +0100166 // This callback is used on the module thread, but since we don't get
167 // callbacks on the module thread while the decoder thread isn't running
168 // (and this function must not be called when the decoder is running),
169 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000170 _packetRequestCallback = callback;
171 return VCM_OK;
172}
173
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000174// Decode next frame, blocking.
175// Should be called as often as possible to get the most out of the decoder.
176int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
Tommifbf3bce2018-02-21 15:56:05 +0100177 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
178 VCMEncodedFrame* frame = _receiver.FrameForDecoding(
179 maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000180
Peter Boströmb7d9a972015-12-18 16:01:11 +0100181 if (!frame)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000182 return VCM_FRAME_NOT_READY;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000183
Tommifbf3bce2018-02-21 15:56:05 +0100184 bool drop_frame = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100185 {
sprang3911c262016-04-15 01:24:14 -0700186 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100187 if (drop_frames_until_keyframe_) {
188 // Still getting delta frames, schedule another keyframe request as if
189 // decode failed.
Niels Möller8f7ce222019-03-21 15:43:58 +0100190 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Tommifbf3bce2018-02-21 15:56:05 +0100191 drop_frame = true;
Peter Boströmed3277b2016-02-02 15:40:04 +0100192 _scheduleKeyRequest = true;
Tommifbf3bce2018-02-21 15:56:05 +0100193 // TODO(tommi): Consider if we could instead post a task to the module
194 // thread and call RequestKeyFrame directly. Here we call WakeUp so that
195 // TimeUntilNextProcess() gets called straight away.
196 process_thread_->WakeUp(this);
197 } else {
198 drop_frames_until_keyframe_ = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100199 }
Peter Boströmed3277b2016-02-02 15:40:04 +0100200 }
201 }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100202
Tommifbf3bce2018-02-21 15:56:05 +0100203 if (drop_frame) {
204 _receiver.ReleaseFrame(frame);
205 return VCM_FRAME_NOT_READY;
206 }
207
sprang3911c262016-04-15 01:24:14 -0700208 // If this frame was too late, we should adjust the delay accordingly
philipel721d4022016-12-15 07:10:57 -0800209 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
210 clock_->TimeInMilliseconds());
skvlad98bb6642016-04-07 15:36:45 -0700211
212 if (first_frame_received_()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100213 RTC_LOG(LS_INFO) << "Received first "
214 << (frame->Complete() ? "complete" : "incomplete")
215 << " decodable video frame";
skvlad98bb6642016-04-07 15:36:45 -0700216 }
217
Peter Boströmb7d9a972015-12-18 16:01:11 +0100218 const int32_t ret = Decode(*frame);
219 _receiver.ReleaseFrame(frame);
220 return ret;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000221}
222
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000223int32_t VideoReceiver::RequestKeyFrame() {
Tommifbf3bce2018-02-21 15:56:05 +0100224 RTC_DCHECK_RUN_ON(&module_thread_checker_);
225
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000226 TRACE_EVENT0("webrtc", "RequestKeyFrame");
sprang3911c262016-04-15 01:24:14 -0700227 if (_frameTypeCallback != nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000228 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
229 if (ret < 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000230 return ret;
231 }
Tommifbf3bce2018-02-21 15:56:05 +0100232 rtc::CritScope cs(&process_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000233 _scheduleKeyRequest = false;
234 } else {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000235 return VCM_MISSING_CALLBACK;
236 }
237 return VCM_OK;
238}
239
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000240// Must be called from inside the receive side critical section.
241int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100242 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
tommidb23ea62017-03-03 07:21:18 -0800243 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000244 // Change decoder if payload type has changed
tommid0a71ba2017-03-14 04:16:20 -0700245 VCMGenericDecoder* decoder =
246 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
247 if (decoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000248 return VCM_NO_CODEC_REGISTERED;
249 }
Johannes Kron05f84872020-01-16 14:09:33 +0100250 return decoder->Decode(frame, clock_->CurrentTime());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000251}
252
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000253// Register possible receive codecs, can be called multiple times
254int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
255 int32_t numberOfCores,
256 bool requireKeyFrame) {
Tommifbf3bce2018-02-21 15:56:05 +0100257 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
sprang3911c262016-04-15 01:24:14 -0700258 if (receiveCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000259 return VCM_PARAMETER_ERROR;
260 }
philipel9d3ab612015-12-21 04:12:39 -0800261 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
262 requireKeyFrame)) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000263 return -1;
264 }
265 return 0;
266}
267
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000268// Incoming packet from network parsed and ready for decode, non blocking.
269int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000270 size_t payloadLength,
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200271 const RTPHeader& rtp_header,
272 const RTPVideoHeader& video_header) {
Tommifbf3bce2018-02-21 15:56:05 +0100273 RTC_DCHECK_RUN_ON(&module_thread_checker_);
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200274 if (video_header.frame_type == VideoFrameType::kVideoFrameKey) {
philipel9d3ab612015-12-21 04:12:39 -0800275 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200276 rtp_header.sequenceNumber);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000277 }
sprang3911c262016-04-15 01:24:14 -0700278 if (incomingPayload == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000279 // The jitter buffer doesn't handle non-zero payload lengths for packets
280 // without payload.
281 // TODO(holmer): We should fix this in the jitter buffer.
282 payloadLength = 0;
283 }
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200284 // Callers don't provide any ntp time.
285 const VCMPacket packet(incomingPayload, payloadLength, rtp_header,
Chen Xingf00bf422019-06-20 10:05:55 +0200286 video_header, /*ntp_time_ms=*/0,
287 clock_->TimeInMilliseconds());
Johan Ahlers95348f72016-06-28 11:11:28 +0200288 int32_t ret = _receiver.InsertPacket(packet);
sprang3911c262016-04-15 01:24:14 -0700289
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000290 // TODO(holmer): Investigate if this somehow should use the key frame
291 // request scheduling to throttle the requests.
292 if (ret == VCM_FLUSH_INDICATOR) {
Peter Boströmed3277b2016-02-02 15:40:04 +0100293 {
sprang3911c262016-04-15 01:24:14 -0700294 rtc::CritScope cs(&process_crit_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100295 drop_frames_until_keyframe_ = true;
296 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000297 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000298 } else if (ret < 0) {
299 return ret;
300 }
301 return VCM_OK;
302}
303
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000304void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
305 int max_packet_age_to_nack,
306 int max_incomplete_time_ms) {
Tommifbf3bce2018-02-21 15:56:05 +0100307 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
guidouc3372582017-04-04 07:16:21 -0700308 if (max_nack_list_size != 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000309 max_nack_list_size_ = max_nack_list_size;
guidouc3372582017-04-04 07:16:21 -0700310 }
philipel9d3ab612015-12-21 04:12:39 -0800311 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
312 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000313}
314
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000315} // namespace vcm
316} // namespace webrtc