blob: a5491e9a40ec80c5f05a6638d5e3a68cd5a15d57 [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"
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 _retransmissionTimer(10, clock_),
Tommifbf3bce2018-02-21 15:56:05 +010056 _keyRequestTimer(500, clock_) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020057 decoder_thread_checker_.Detach();
58 module_thread_checker_.Detach();
Tommifbf3bce2018-02-21 15:56:05 +010059}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000060
Tommifbf3bce2018-02-21 15:56:05 +010061VideoReceiver::~VideoReceiver() {
62 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
63}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000064
pbosa26ac922016-02-25 04:50:01 -080065void VideoReceiver::Process() {
Tommifbf3bce2018-02-21 15:56:05 +010066 RTC_DCHECK_RUN_ON(&module_thread_checker_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000067
68 // Key frame requests
69 if (_keyRequestTimer.TimeUntilProcess() == 0) {
70 _keyRequestTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010071 bool request_key_frame = _frameTypeCallback != nullptr;
72 if (request_key_frame) {
Markus Handell6deec382020-07-07 12:17:12 +020073 MutexLock lock(&process_mutex_);
Tommifbf3bce2018-02-21 15:56:05 +010074 request_key_frame = _scheduleKeyRequest;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000075 }
pbosa26ac922016-02-25 04:50:01 -080076 if (request_key_frame)
77 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000078 }
79
80 // Packet retransmission requests
81 // TODO(holmer): Add API for changing Process interval and make sure it's
82 // disabled when NACK is off.
83 if (_retransmissionTimer.TimeUntilProcess() == 0) {
84 _retransmissionTimer.Processed();
Tommifbf3bce2018-02-21 15:56:05 +010085 bool callback_registered = _packetRequestCallback != nullptr;
86 uint16_t length = max_nack_list_size_;
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000087 if (callback_registered && length > 0) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -070088 // Collect sequence numbers from the default receiver.
89 bool request_key_frame = false;
90 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
91 int32_t ret = VCM_OK;
92 if (request_key_frame) {
93 ret = RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000094 }
Wan-Teh Changb1825a42015-06-03 15:03:35 -070095 if (ret == VCM_OK && !nackList.empty()) {
Markus Handell6deec382020-07-07 12:17:12 +020096 MutexLock lock(&process_mutex_);
sprang3911c262016-04-15 01:24:14 -070097 if (_packetRequestCallback != nullptr) {
Wan-Teh Changb1825a42015-06-03 15:03:35 -070098 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
stefan@webrtc.orgf7c6e742014-01-29 10:27:51 +000099 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100 }
101 }
102 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000103}
104
Tommifbf3bce2018-02-21 15:56:05 +0100105void VideoReceiver::ProcessThreadAttached(ProcessThread* process_thread) {
106 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
107 if (process_thread) {
108 is_attached_to_process_thread_ = true;
109 RTC_DCHECK(!process_thread_ || process_thread_ == process_thread);
110 process_thread_ = process_thread;
111 } else {
112 is_attached_to_process_thread_ = false;
113 }
114}
115
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000116int64_t VideoReceiver::TimeUntilNextProcess() {
Tommifbf3bce2018-02-21 15:56:05 +0100117 RTC_DCHECK_RUN_ON(&module_thread_checker_);
Tommieb03d282020-04-02 22:59:29 +0200118 int64_t timeUntilNextProcess = _retransmissionTimer.TimeUntilProcess();
Niels Möller691f62c2019-04-11 15:27:17 +0200119
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000120 timeUntilNextProcess =
121 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
122
123 return timeUntilNextProcess;
124}
125
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000126// Register a receive callback. Will be called whenever there is a new frame
127// ready for rendering.
128int32_t VideoReceiver::RegisterReceiveCallback(
129 VCMReceiveCallback* receiveCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100130 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
Tommifbf3bce2018-02-21 15:56:05 +0100131 // This value is set before the decoder thread starts and unset after
132 // the decoder thread has been stopped.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000133 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
134 return VCM_OK;
135}
136
perkj796cfaf2015-12-10 09:27:38 -0800137// Register an externally defined decoder object.
Peter Boström795dbe42015-11-27 14:09:07 +0100138void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800139 uint8_t payloadType) {
Tommifbf3bce2018-02-21 15:56:05 +0100140 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
sprang3911c262016-04-15 01:24:14 -0700141 if (externalDecoder == nullptr) {
Peter Boström795dbe42015-11-27 14:09:07 +0100142 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
Peter Boström395c7c62015-11-27 15:23:12 +0100143 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144 }
perkj796cfaf2015-12-10 09:27:38 -0800145 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000146}
147
148// Register a frame type request callback.
149int32_t VideoReceiver::RegisterFrameTypeCallback(
150 VCMFrameTypeCallback* frameTypeCallback) {
Tommifbf3bce2018-02-21 15:56:05 +0100151 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
Niels Möller45b01c72019-09-10 13:02:28 +0200152 RTC_DCHECK(!is_attached_to_process_thread_);
Tommifbf3bce2018-02-21 15:56:05 +0100153 // This callback is used on the module thread, but since we don't get
154 // callbacks on the module thread while the decoder thread isn't running
155 // (and this function must not be called when the decoder is running),
156 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000157 _frameTypeCallback = frameTypeCallback;
158 return VCM_OK;
159}
160
161int32_t VideoReceiver::RegisterPacketRequestCallback(
162 VCMPacketRequestCallback* callback) {
Tommifbf3bce2018-02-21 15:56:05 +0100163 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
Niels Möller45b01c72019-09-10 13:02:28 +0200164 RTC_DCHECK(!is_attached_to_process_thread_);
Tommifbf3bce2018-02-21 15:56:05 +0100165 // This callback is used on the module thread, but since we don't get
166 // callbacks on the module thread while the decoder thread isn't running
167 // (and this function must not be called when the decoder is running),
168 // we don't need a lock here.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000169 _packetRequestCallback = callback;
170 return VCM_OK;
171}
172
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000173// Decode next frame, blocking.
174// Should be called as often as possible to get the most out of the decoder.
175int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
Tommifbf3bce2018-02-21 15:56:05 +0100176 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
177 VCMEncodedFrame* frame = _receiver.FrameForDecoding(
178 maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000179
Peter Boströmb7d9a972015-12-18 16:01:11 +0100180 if (!frame)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000181 return VCM_FRAME_NOT_READY;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000182
Tommifbf3bce2018-02-21 15:56:05 +0100183 bool drop_frame = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100184 {
Markus Handell6deec382020-07-07 12:17:12 +0200185 MutexLock lock(&process_mutex_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100186 if (drop_frames_until_keyframe_) {
187 // Still getting delta frames, schedule another keyframe request as if
188 // decode failed.
Niels Möller8f7ce222019-03-21 15:43:58 +0100189 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Tommifbf3bce2018-02-21 15:56:05 +0100190 drop_frame = true;
Peter Boströmed3277b2016-02-02 15:40:04 +0100191 _scheduleKeyRequest = true;
Tommifbf3bce2018-02-21 15:56:05 +0100192 // TODO(tommi): Consider if we could instead post a task to the module
193 // thread and call RequestKeyFrame directly. Here we call WakeUp so that
194 // TimeUntilNextProcess() gets called straight away.
195 process_thread_->WakeUp(this);
196 } else {
197 drop_frames_until_keyframe_ = false;
Peter Boströmed3277b2016-02-02 15:40:04 +0100198 }
Peter Boströmed3277b2016-02-02 15:40:04 +0100199 }
200 }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100201
Tommifbf3bce2018-02-21 15:56:05 +0100202 if (drop_frame) {
203 _receiver.ReleaseFrame(frame);
204 return VCM_FRAME_NOT_READY;
205 }
206
sprang3911c262016-04-15 01:24:14 -0700207 // If this frame was too late, we should adjust the delay accordingly
philipel721d4022016-12-15 07:10:57 -0800208 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
209 clock_->TimeInMilliseconds());
skvlad98bb6642016-04-07 15:36:45 -0700210
211 if (first_frame_received_()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100212 RTC_LOG(LS_INFO) << "Received first "
213 << (frame->Complete() ? "complete" : "incomplete")
214 << " decodable video frame";
skvlad98bb6642016-04-07 15:36:45 -0700215 }
216
Peter Boströmb7d9a972015-12-18 16:01:11 +0100217 const int32_t ret = Decode(*frame);
218 _receiver.ReleaseFrame(frame);
219 return ret;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000220}
221
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000222int32_t VideoReceiver::RequestKeyFrame() {
Tommifbf3bce2018-02-21 15:56:05 +0100223 RTC_DCHECK_RUN_ON(&module_thread_checker_);
224
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000225 TRACE_EVENT0("webrtc", "RequestKeyFrame");
sprang3911c262016-04-15 01:24:14 -0700226 if (_frameTypeCallback != nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000227 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
228 if (ret < 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000229 return ret;
230 }
Markus Handell6deec382020-07-07 12:17:12 +0200231 MutexLock lock(&process_mutex_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000232 _scheduleKeyRequest = false;
233 } else {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000234 return VCM_MISSING_CALLBACK;
235 }
236 return VCM_OK;
237}
238
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000239// Must be called from inside the receive side critical section.
240int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
Tommifbf3bce2018-02-21 15:56:05 +0100241 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
tommidb23ea62017-03-03 07:21:18 -0800242 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000243 // Change decoder if payload type has changed
tommid0a71ba2017-03-14 04:16:20 -0700244 VCMGenericDecoder* decoder =
245 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
246 if (decoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000247 return VCM_NO_CODEC_REGISTERED;
248 }
Johannes Kron05f84872020-01-16 14:09:33 +0100249 return decoder->Decode(frame, clock_->CurrentTime());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000250}
251
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000252// Register possible receive codecs, can be called multiple times
253int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
Niels Möller18c83d32020-08-07 14:14:49 +0200254 int32_t numberOfCores) {
Tommifbf3bce2018-02-21 15:56:05 +0100255 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
sprang3911c262016-04-15 01:24:14 -0700256 if (receiveCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000257 return VCM_PARAMETER_ERROR;
258 }
Niels Möller18c83d32020-08-07 14:14:49 +0200259 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores)) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000260 return -1;
261 }
262 return 0;
263}
264
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000265// Incoming packet from network parsed and ready for decode, non blocking.
266int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000267 size_t payloadLength,
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200268 const RTPHeader& rtp_header,
269 const RTPVideoHeader& video_header) {
Tommifbf3bce2018-02-21 15:56:05 +0100270 RTC_DCHECK_RUN_ON(&module_thread_checker_);
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200271 if (video_header.frame_type == VideoFrameType::kVideoFrameKey) {
philipel9d3ab612015-12-21 04:12:39 -0800272 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200273 rtp_header.sequenceNumber);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000274 }
sprang3911c262016-04-15 01:24:14 -0700275 if (incomingPayload == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000276 // The jitter buffer doesn't handle non-zero payload lengths for packets
277 // without payload.
278 // TODO(holmer): We should fix this in the jitter buffer.
279 payloadLength = 0;
280 }
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200281 // Callers don't provide any ntp time.
282 const VCMPacket packet(incomingPayload, payloadLength, rtp_header,
Chen Xingf00bf422019-06-20 10:05:55 +0200283 video_header, /*ntp_time_ms=*/0,
284 clock_->TimeInMilliseconds());
Johan Ahlers95348f72016-06-28 11:11:28 +0200285 int32_t ret = _receiver.InsertPacket(packet);
sprang3911c262016-04-15 01:24:14 -0700286
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000287 // TODO(holmer): Investigate if this somehow should use the key frame
288 // request scheduling to throttle the requests.
289 if (ret == VCM_FLUSH_INDICATOR) {
Peter Boströmed3277b2016-02-02 15:40:04 +0100290 {
Markus Handell6deec382020-07-07 12:17:12 +0200291 MutexLock lock(&process_mutex_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100292 drop_frames_until_keyframe_ = true;
293 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000294 RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000295 } else if (ret < 0) {
296 return ret;
297 }
298 return VCM_OK;
299}
300
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000301void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
302 int max_packet_age_to_nack,
303 int max_incomplete_time_ms) {
Tommifbf3bce2018-02-21 15:56:05 +0100304 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
guidouc3372582017-04-04 07:16:21 -0700305 if (max_nack_list_size != 0) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000306 max_nack_list_size_ = max_nack_list_size;
guidouc3372582017-04-04 07:16:21 -0700307 }
philipel9d3ab612015-12-21 04:12:39 -0800308 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
309 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000310}
311
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000312} // namespace vcm
313} // namespace webrtc