1 /*
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
11 #include <stddef.h>
12
13 #include <cstdint>
14 #include <vector>
15
16 #include "api/rtp_headers.h"
17 #include "api/sequence_checker.h"
18 #include "api/video_codecs/video_codec.h"
19 #include "api/video_codecs/video_decoder.h"
20 #include "modules/video_coding/decoder_database.h"
21 #include "modules/video_coding/encoded_frame.h"
22 #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"
26 #include "modules/video_coding/jitter_buffer.h"
27 #include "modules/video_coding/media_opt_util.h"
28 #include "modules/video_coding/packet.h"
29 #include "modules/video_coding/receiver.h"
30 #include "modules/video_coding/timing/timing.h"
31 #include "modules/video_coding/video_coding_impl.h"
32 #include "rtc_base/checks.h"
33 #include "rtc_base/logging.h"
34 #include "rtc_base/one_time_event.h"
35 #include "rtc_base/trace_event.h"
36 #include "system_wrappers/include/clock.h"
37
38 namespace webrtc {
39 namespace vcm {
40
VideoReceiver(Clock * clock,VCMTiming * timing,const FieldTrialsView & field_trials)41 VideoReceiver::VideoReceiver(Clock* clock,
42 VCMTiming* timing,
43 const FieldTrialsView& field_trials)
44 : clock_(clock),
45 _timing(timing),
46 _receiver(_timing, clock_, field_trials),
47 _decodedFrameCallback(_timing, clock_, field_trials),
48 _frameTypeCallback(nullptr),
49 _packetRequestCallback(nullptr),
50 _scheduleKeyRequest(false),
51 drop_frames_until_keyframe_(false),
52 max_nack_list_size_(0),
53 _codecDataBase(),
54 _retransmissionTimer(10, clock_),
55 _keyRequestTimer(500, clock_) {
56 decoder_thread_checker_.Detach();
57 module_thread_checker_.Detach();
58 }
59
~VideoReceiver()60 VideoReceiver::~VideoReceiver() {
61 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
62 }
63
Process()64 void VideoReceiver::Process() {
65 RTC_DCHECK_RUN_ON(&module_thread_checker_);
66
67 // Key frame requests
68 if (_keyRequestTimer.TimeUntilProcess() == 0) {
69 _keyRequestTimer.Processed();
70 bool request_key_frame = _frameTypeCallback != nullptr;
71 if (request_key_frame) {
72 MutexLock lock(&process_mutex_);
73 request_key_frame = _scheduleKeyRequest;
74 }
75 if (request_key_frame)
76 RequestKeyFrame();
77 }
78
79 // Packet retransmission requests
80 // TODO(holmer): Add API for changing Process interval and make sure it's
81 // disabled when NACK is off.
82 if (_retransmissionTimer.TimeUntilProcess() == 0) {
83 _retransmissionTimer.Processed();
84 bool callback_registered = _packetRequestCallback != nullptr;
85 uint16_t length = max_nack_list_size_;
86 if (callback_registered && length > 0) {
87 // Collect sequence numbers from the default receiver.
88 bool request_key_frame = false;
89 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
90 int32_t ret = VCM_OK;
91 if (request_key_frame) {
92 ret = RequestKeyFrame();
93 }
94 if (ret == VCM_OK && !nackList.empty()) {
95 MutexLock lock(&process_mutex_);
96 if (_packetRequestCallback != nullptr) {
97 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
98 }
99 }
100 }
101 }
102 }
103
104 // Register a receive callback. Will be called whenever there is a new frame
105 // ready for rendering.
RegisterReceiveCallback(VCMReceiveCallback * receiveCallback)106 int32_t VideoReceiver::RegisterReceiveCallback(
107 VCMReceiveCallback* receiveCallback) {
108 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
109 // This value is set before the decoder thread starts and unset after
110 // the decoder thread has been stopped.
111 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
112 return VCM_OK;
113 }
114
115 // Register an externally defined decoder object.
RegisterExternalDecoder(VideoDecoder * externalDecoder,uint8_t payloadType)116 void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
117 uint8_t payloadType) {
118 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
119 if (externalDecoder == nullptr) {
120 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
121 return;
122 }
123 _codecDataBase.RegisterExternalDecoder(payloadType, externalDecoder);
124 }
125
126 // Register a frame type request callback.
RegisterFrameTypeCallback(VCMFrameTypeCallback * frameTypeCallback)127 int32_t VideoReceiver::RegisterFrameTypeCallback(
128 VCMFrameTypeCallback* frameTypeCallback) {
129 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
130 // This callback is used on the module thread, but since we don't get
131 // callbacks on the module thread while the decoder thread isn't running
132 // (and this function must not be called when the decoder is running),
133 // we don't need a lock here.
134 _frameTypeCallback = frameTypeCallback;
135 return VCM_OK;
136 }
137
RegisterPacketRequestCallback(VCMPacketRequestCallback * callback)138 int32_t VideoReceiver::RegisterPacketRequestCallback(
139 VCMPacketRequestCallback* callback) {
140 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
141 // This callback is used on the module thread, but since we don't get
142 // callbacks on the module thread while the decoder thread isn't running
143 // (and this function must not be called when the decoder is running),
144 // we don't need a lock here.
145 _packetRequestCallback = callback;
146 return VCM_OK;
147 }
148
149 // Decode next frame, blocking.
150 // Should be called as often as possible to get the most out of the decoder.
Decode(uint16_t maxWaitTimeMs)151 int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
152 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
153 VCMEncodedFrame* frame = _receiver.FrameForDecoding(maxWaitTimeMs, true);
154
155 if (!frame)
156 return VCM_FRAME_NOT_READY;
157
158 bool drop_frame = false;
159 {
160 MutexLock lock(&process_mutex_);
161 if (drop_frames_until_keyframe_) {
162 // Still getting delta frames, schedule another keyframe request as if
163 // decode failed.
164 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
165 drop_frame = true;
166 _scheduleKeyRequest = true;
167 } else {
168 drop_frames_until_keyframe_ = false;
169 }
170 }
171 }
172
173 if (drop_frame) {
174 _receiver.ReleaseFrame(frame);
175 return VCM_FRAME_NOT_READY;
176 }
177
178 // If this frame was too late, we should adjust the delay accordingly
179 if (frame->RenderTimeMs() > 0)
180 _timing->UpdateCurrentDelay(Timestamp::Millis(frame->RenderTimeMs()),
181 clock_->CurrentTime());
182
183 if (first_frame_received_()) {
184 RTC_LOG(LS_INFO) << "Received first complete decodable video frame";
185 }
186
187 const int32_t ret = Decode(*frame);
188 _receiver.ReleaseFrame(frame);
189 return ret;
190 }
191
RequestKeyFrame()192 int32_t VideoReceiver::RequestKeyFrame() {
193 RTC_DCHECK_RUN_ON(&module_thread_checker_);
194
195 TRACE_EVENT0("webrtc", "RequestKeyFrame");
196 if (_frameTypeCallback != nullptr) {
197 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
198 if (ret < 0) {
199 return ret;
200 }
201 MutexLock lock(&process_mutex_);
202 _scheduleKeyRequest = false;
203 } else {
204 return VCM_MISSING_CALLBACK;
205 }
206 return VCM_OK;
207 }
208
209 // Must be called from inside the receive side critical section.
Decode(const VCMEncodedFrame & frame)210 int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
211 RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
212 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
213 // Change decoder if payload type has changed
214 VCMGenericDecoder* decoder =
215 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
216 if (decoder == nullptr) {
217 return VCM_NO_CODEC_REGISTERED;
218 }
219 return decoder->Decode(frame, clock_->CurrentTime());
220 }
221
222 // Register possible receive codecs, can be called multiple times
RegisterReceiveCodec(uint8_t payload_type,const VideoDecoder::Settings & settings)223 void VideoReceiver::RegisterReceiveCodec(
224 uint8_t payload_type,
225 const VideoDecoder::Settings& settings) {
226 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
227 _codecDataBase.RegisterReceiveCodec(payload_type, settings);
228 }
229
230 // Incoming packet from network parsed and ready for decode, non blocking.
IncomingPacket(const uint8_t * incomingPayload,size_t payloadLength,const RTPHeader & rtp_header,const RTPVideoHeader & video_header)231 int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
232 size_t payloadLength,
233 const RTPHeader& rtp_header,
234 const RTPVideoHeader& video_header) {
235 RTC_DCHECK_RUN_ON(&module_thread_checker_);
236 if (video_header.frame_type == VideoFrameType::kVideoFrameKey) {
237 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
238 rtp_header.sequenceNumber);
239 }
240 if (incomingPayload == nullptr) {
241 // The jitter buffer doesn't handle non-zero payload lengths for packets
242 // without payload.
243 // TODO(holmer): We should fix this in the jitter buffer.
244 payloadLength = 0;
245 }
246 // Callers don't provide any ntp time.
247 const VCMPacket packet(incomingPayload, payloadLength, rtp_header,
248 video_header, /*ntp_time_ms=*/0,
249 clock_->CurrentTime());
250 int32_t ret = _receiver.InsertPacket(packet);
251
252 // TODO(holmer): Investigate if this somehow should use the key frame
253 // request scheduling to throttle the requests.
254 if (ret == VCM_FLUSH_INDICATOR) {
255 {
256 MutexLock lock(&process_mutex_);
257 drop_frames_until_keyframe_ = true;
258 }
259 RequestKeyFrame();
260 } else if (ret < 0) {
261 return ret;
262 }
263 return VCM_OK;
264 }
265
SetNackSettings(size_t max_nack_list_size,int max_packet_age_to_nack,int max_incomplete_time_ms)266 void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
267 int max_packet_age_to_nack,
268 int max_incomplete_time_ms) {
269 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
270 if (max_nack_list_size != 0) {
271 max_nack_list_size_ = max_nack_list_size;
272 }
273 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
274 max_incomplete_time_ms);
275 }
276
277 } // namespace vcm
278 } // namespace webrtc
279