Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: webrtc/modules/video_coding/video_receiver.cc

Issue 2764573002: Deliver video frames on Android, on the decode thread. (Closed)
Patch Set: Update comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/checks.h" 11 #include "webrtc/base/checks.h"
12 #include "webrtc/base/location.h"
12 #include "webrtc/base/logging.h" 13 #include "webrtc/base/logging.h"
13 #include "webrtc/base/trace_event.h" 14 #include "webrtc/base/trace_event.h"
14 #include "webrtc/common_types.h" 15 #include "webrtc/common_types.h"
15 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" 16 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
17 #include "webrtc/modules/utility/include/process_thread.h"
18 #include "webrtc/modules/video_coding/encoded_frame.h"
16 #include "webrtc/modules/video_coding/include/video_codec_interface.h" 19 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
17 #include "webrtc/modules/video_coding/encoded_frame.h"
18 #include "webrtc/modules/video_coding/jitter_buffer.h" 20 #include "webrtc/modules/video_coding/jitter_buffer.h"
19 #include "webrtc/modules/video_coding/packet.h" 21 #include "webrtc/modules/video_coding/packet.h"
20 #include "webrtc/modules/video_coding/video_coding_impl.h" 22 #include "webrtc/modules/video_coding/video_coding_impl.h"
21 #include "webrtc/system_wrappers/include/clock.h" 23 #include "webrtc/system_wrappers/include/clock.h"
22 24
23 namespace webrtc { 25 namespace webrtc {
24 namespace vcm { 26 namespace vcm {
25 27
26 VideoReceiver::VideoReceiver(Clock* clock, 28 VideoReceiver::VideoReceiver(Clock* clock,
27 EventFactory* event_factory, 29 EventFactory* event_factory,
28 EncodedImageCallback* pre_decode_image_callback, 30 EncodedImageCallback* pre_decode_image_callback,
29 VCMTiming* timing, 31 VCMTiming* timing,
30 NackSender* nack_sender, 32 NackSender* nack_sender,
31 KeyFrameRequestSender* keyframe_request_sender) 33 KeyFrameRequestSender* keyframe_request_sender)
32 : clock_(clock), 34 : clock_(clock),
33 _timing(timing), 35 _timing(timing),
34 _receiver(_timing, 36 _receiver(_timing,
35 clock_, 37 clock_,
36 event_factory, 38 event_factory,
37 nack_sender, 39 nack_sender,
38 keyframe_request_sender), 40 keyframe_request_sender),
39 _decodedFrameCallback(_timing, clock_), 41 _decodedFrameCallback(_timing, clock_),
40 _frameTypeCallback(nullptr), 42 _frameTypeCallback(nullptr),
41 _receiveStatsCallback(nullptr), 43 _receiveStatsCallback(nullptr),
42 _packetRequestCallback(nullptr), 44 _packetRequestCallback(nullptr),
43 _frameFromFile(),
44 _scheduleKeyRequest(false), 45 _scheduleKeyRequest(false),
45 drop_frames_until_keyframe_(false), 46 drop_frames_until_keyframe_(false),
46 max_nack_list_size_(0), 47 max_nack_list_size_(0),
47 _codecDataBase(nullptr), 48 _codecDataBase(nullptr),
48 pre_decode_image_callback_(pre_decode_image_callback), 49 pre_decode_image_callback_(pre_decode_image_callback),
49 _receiveStatsTimer(1000, clock_), 50 _receiveStatsTimer(1000, clock_),
50 _retransmissionTimer(10, clock_), 51 _retransmissionTimer(10, clock_),
51 _keyRequestTimer(500, clock_) {} 52 _keyRequestTimer(500, clock_) {
53 decoder_thread_.DetachFromThread();
54 module_thread_.DetachFromThread();
55 }
52 56
53 VideoReceiver::~VideoReceiver() {} 57 VideoReceiver::~VideoReceiver() {
58 RTC_DCHECK_RUN_ON(&construction_thread_);
59 }
54 60
55 void VideoReceiver::Process() { 61 void VideoReceiver::Process() {
62 RTC_DCHECK_RUN_ON(&module_thread_);
56 // Receive-side statistics 63 // Receive-side statistics
57 64
58 // TODO(philipel): Remove this if block when we know what to do with 65 // TODO(philipel): Remove this if block when we know what to do with
59 // ReceiveStatisticsProxy::QualitySample. 66 // ReceiveStatisticsProxy::QualitySample.
60 if (_receiveStatsTimer.TimeUntilProcess() == 0) { 67 if (_receiveStatsTimer.TimeUntilProcess() == 0) {
61 _receiveStatsTimer.Processed(); 68 _receiveStatsTimer.Processed();
62 rtc::CritScope cs(&process_crit_);
63 if (_receiveStatsCallback != nullptr) { 69 if (_receiveStatsCallback != nullptr) {
64 _receiveStatsCallback->OnReceiveRatesUpdated(0, 0); 70 _receiveStatsCallback->OnReceiveRatesUpdated(0, 0);
65 } 71 }
66 } 72 }
67 73
68 // Key frame requests 74 // Key frame requests
69 if (_keyRequestTimer.TimeUntilProcess() == 0) { 75 if (_keyRequestTimer.TimeUntilProcess() == 0) {
70 _keyRequestTimer.Processed(); 76 _keyRequestTimer.Processed();
71 bool request_key_frame = false; 77 bool request_key_frame = _frameTypeCallback != nullptr;
72 { 78 if (request_key_frame) {
73 rtc::CritScope cs(&process_crit_); 79 rtc::CritScope cs(&process_crit_);
74 request_key_frame = _scheduleKeyRequest && _frameTypeCallback != nullptr; 80 request_key_frame = _scheduleKeyRequest;
75 } 81 }
76 if (request_key_frame) 82 if (request_key_frame)
77 RequestKeyFrame(); 83 RequestKeyFrame();
78 } 84 }
79 85
80 // Packet retransmission requests 86 // Packet retransmission requests
81 // TODO(holmer): Add API for changing Process interval and make sure it's 87 // TODO(holmer): Add API for changing Process interval and make sure it's
82 // disabled when NACK is off. 88 // disabled when NACK is off.
83 if (_retransmissionTimer.TimeUntilProcess() == 0) { 89 if (_retransmissionTimer.TimeUntilProcess() == 0) {
84 _retransmissionTimer.Processed(); 90 _retransmissionTimer.Processed();
85 bool callback_registered = false; 91 bool callback_registered = _packetRequestCallback != nullptr;
86 uint16_t length; 92 uint16_t length = max_nack_list_size_;
87 {
88 rtc::CritScope cs(&process_crit_);
89 length = max_nack_list_size_;
90 callback_registered = _packetRequestCallback != nullptr;
91 }
92 if (callback_registered && length > 0) { 93 if (callback_registered && length > 0) {
93 // Collect sequence numbers from the default receiver. 94 // Collect sequence numbers from the default receiver.
94 bool request_key_frame = false; 95 bool request_key_frame = false;
95 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame); 96 std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
96 int32_t ret = VCM_OK; 97 int32_t ret = VCM_OK;
97 if (request_key_frame) { 98 if (request_key_frame) {
98 ret = RequestKeyFrame(); 99 ret = RequestKeyFrame();
99 } 100 }
100 if (ret == VCM_OK && !nackList.empty()) { 101 if (ret == VCM_OK && !nackList.empty()) {
101 rtc::CritScope cs(&process_crit_);
102 if (_packetRequestCallback != nullptr) { 102 if (_packetRequestCallback != nullptr) {
103 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size()); 103 _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
104 } 104 }
105 } 105 }
106 } 106 }
107 } 107 }
108 } 108 }
109 109
110 void 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
110 int64_t VideoReceiver::TimeUntilNextProcess() { 120 int64_t VideoReceiver::TimeUntilNextProcess() {
121 RTC_DCHECK_RUN_ON(&module_thread_);
111 int64_t timeUntilNextProcess = _receiveStatsTimer.TimeUntilProcess(); 122 int64_t timeUntilNextProcess = _receiveStatsTimer.TimeUntilProcess();
112 if (_receiver.NackMode() != kNoNack) { 123 if (_receiver.NackMode() != kNoNack) {
113 // We need a Process call more often if we are relying on 124 // We need a Process call more often if we are relying on
114 // retransmissions 125 // retransmissions
115 timeUntilNextProcess = 126 timeUntilNextProcess =
116 VCM_MIN(timeUntilNextProcess, _retransmissionTimer.TimeUntilProcess()); 127 VCM_MIN(timeUntilNextProcess, _retransmissionTimer.TimeUntilProcess());
117 } 128 }
118 timeUntilNextProcess = 129 timeUntilNextProcess =
119 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess()); 130 VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
120 131
121 return timeUntilNextProcess; 132 return timeUntilNextProcess;
122 } 133 }
123 134
124 int32_t VideoReceiver::SetReceiveChannelParameters(int64_t rtt) { 135 int32_t VideoReceiver::SetReceiveChannelParameters(int64_t rtt) {
125 rtc::CritScope cs(&receive_crit_); 136 RTC_DCHECK_RUN_ON(&module_thread_);
126 _receiver.UpdateRtt(rtt); 137 _receiver.UpdateRtt(rtt);
127 return 0; 138 return 0;
128 } 139 }
129 140
130 // Enable or disable a video protection method. 141 // Enable or disable a video protection method.
131 // Note: This API should be deprecated, as it does not offer a distinction 142 // Note: This API should be deprecated, as it does not offer a distinction
132 // between the protection method and decoding with or without errors. 143 // between the protection method and decoding with or without errors.
133 int32_t VideoReceiver::SetVideoProtection(VCMVideoProtection videoProtection, 144 int32_t VideoReceiver::SetVideoProtection(VCMVideoProtection videoProtection,
134 bool enable) { 145 bool enable) {
135 // By default, do not decode with errors. 146 // By default, do not decode with errors.
136 _receiver.SetDecodeErrorMode(kNoErrors); 147 _receiver.SetDecodeErrorMode(kNoErrors);
137 switch (videoProtection) { 148 switch (videoProtection) {
138 case kProtectionNack: { 149 case kProtectionNack: {
139 RTC_DCHECK(enable); 150 RTC_DCHECK(enable);
140 _receiver.SetNackMode(kNack, -1, -1); 151 _receiver.SetNackMode(kNack, -1, -1);
141 break; 152 break;
142 } 153 }
143 154
144 case kProtectionNackFEC: { 155 case kProtectionNackFEC: {
145 rtc::CritScope cs(&receive_crit_);
146 RTC_DCHECK(enable); 156 RTC_DCHECK(enable);
147 _receiver.SetNackMode(kNack, 157 _receiver.SetNackMode(kNack,
148 media_optimization::kLowRttNackMs, 158 media_optimization::kLowRttNackMs,
149 media_optimization::kMaxRttDelayThreshold); 159 media_optimization::kMaxRttDelayThreshold);
150 _receiver.SetDecodeErrorMode(kNoErrors); 160 _receiver.SetDecodeErrorMode(kNoErrors);
151 break; 161 break;
152 } 162 }
153 case kProtectionFEC: 163 case kProtectionFEC:
154 case kProtectionNone: 164 case kProtectionNone:
155 // No receiver-side protection. 165 // No receiver-side protection.
156 RTC_DCHECK(enable); 166 RTC_DCHECK(enable);
157 _receiver.SetNackMode(kNoNack, -1, -1); 167 _receiver.SetNackMode(kNoNack, -1, -1);
158 _receiver.SetDecodeErrorMode(kWithErrors); 168 _receiver.SetDecodeErrorMode(kWithErrors);
159 break; 169 break;
160 } 170 }
161 return VCM_OK; 171 return VCM_OK;
162 } 172 }
163 173
164 // Register a receive callback. Will be called whenever there is a new frame 174 // Register a receive callback. Will be called whenever there is a new frame
165 // ready for rendering. 175 // ready for rendering.
166 int32_t VideoReceiver::RegisterReceiveCallback( 176 int32_t VideoReceiver::RegisterReceiveCallback(
167 VCMReceiveCallback* receiveCallback) { 177 VCMReceiveCallback* receiveCallback) {
168 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 178 RTC_DCHECK_RUN_ON(&construction_thread_);
169 // TODO(tommi): Callback may be null, but only after the decoder thread has 179 RTC_DCHECK(!IsDecoderThreadRunning());
170 // been stopped. Use the signal we now get that tells us when the decoder 180 // This value is set before the decoder thread starts and unset after
171 // thread isn't running, to DCHECK that the method is never called while it 181 // the decoder thread has been stopped.
172 // is. Once we're confident, we can remove the lock.
173 rtc::CritScope cs(&receive_crit_);
174 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback); 182 _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
175 return VCM_OK; 183 return VCM_OK;
176 } 184 }
177 185
178 int32_t VideoReceiver::RegisterReceiveStatisticsCallback( 186 int32_t VideoReceiver::RegisterReceiveStatisticsCallback(
179 VCMReceiveStatisticsCallback* receiveStats) { 187 VCMReceiveStatisticsCallback* receiveStats) {
180 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 188 RTC_DCHECK_RUN_ON(&construction_thread_);
181 rtc::CritScope cs(&process_crit_); 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.
182 _receiver.RegisterStatsCallback(receiveStats); 194 _receiver.RegisterStatsCallback(receiveStats);
183 _receiveStatsCallback = receiveStats; 195 _receiveStatsCallback = receiveStats;
184 return VCM_OK; 196 return VCM_OK;
185 } 197 }
186 198
187 // Register an externally defined decoder object. 199 // Register an externally defined decoder object.
188 void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder, 200 void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
189 uint8_t payloadType) { 201 uint8_t payloadType) {
190 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 202 RTC_DCHECK_RUN_ON(&construction_thread_);
191 // TODO(tommi): This method must be called when the decoder thread is not 203 RTC_DCHECK(!IsDecoderThreadRunning());
192 // running. Do we need a lock in that case?
193 rtc::CritScope cs(&receive_crit_);
194 if (externalDecoder == nullptr) { 204 if (externalDecoder == nullptr) {
195 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType)); 205 RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
196 return; 206 return;
197 } 207 }
198 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType); 208 _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
199 } 209 }
200 210
201 // Register a frame type request callback. 211 // Register a frame type request callback.
202 int32_t VideoReceiver::RegisterFrameTypeCallback( 212 int32_t VideoReceiver::RegisterFrameTypeCallback(
203 VCMFrameTypeCallback* frameTypeCallback) { 213 VCMFrameTypeCallback* frameTypeCallback) {
204 rtc::CritScope cs(&process_crit_); 214 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.
205 _frameTypeCallback = frameTypeCallback; 220 _frameTypeCallback = frameTypeCallback;
206 return VCM_OK; 221 return VCM_OK;
207 } 222 }
208 223
209 int32_t VideoReceiver::RegisterPacketRequestCallback( 224 int32_t VideoReceiver::RegisterPacketRequestCallback(
210 VCMPacketRequestCallback* callback) { 225 VCMPacketRequestCallback* callback) {
211 rtc::CritScope cs(&process_crit_); 226 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.
212 _packetRequestCallback = callback; 232 _packetRequestCallback = callback;
213 return VCM_OK; 233 return VCM_OK;
214 } 234 }
215 235
216 void VideoReceiver::TriggerDecoderShutdown() { 236 void VideoReceiver::TriggerDecoderShutdown() {
217 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 237 RTC_DCHECK_RUN_ON(&construction_thread_);
238 RTC_DCHECK(IsDecoderThreadRunning());
218 _receiver.TriggerDecoderShutdown(); 239 _receiver.TriggerDecoderShutdown();
219 } 240 }
220 241
242 void 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
253 void 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
221 // Decode next frame, blocking. 265 // Decode next frame, blocking.
222 // Should be called as often as possible to get the most out of the decoder. 266 // Should be called as often as possible to get the most out of the decoder.
223 int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) { 267 int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
224 bool prefer_late_decoding = false; 268 RTC_DCHECK_RUN_ON(&decoder_thread_);
225 { 269 VCMEncodedFrame* frame = _receiver.FrameForDecoding(
226 // TODO(tommi): Chances are that this lock isn't required. 270 maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
227 rtc::CritScope cs(&receive_crit_);
228 prefer_late_decoding = _codecDataBase.PrefersLateDecoding();
229 }
230
231 VCMEncodedFrame* frame =
232 _receiver.FrameForDecoding(maxWaitTimeMs, prefer_late_decoding);
233 271
234 if (!frame) 272 if (!frame)
235 return VCM_FRAME_NOT_READY; 273 return VCM_FRAME_NOT_READY;
236 274
275 bool drop_frame = false;
237 { 276 {
238 rtc::CritScope cs(&process_crit_); 277 rtc::CritScope cs(&process_crit_);
239 if (drop_frames_until_keyframe_) { 278 if (drop_frames_until_keyframe_) {
240 // Still getting delta frames, schedule another keyframe request as if 279 // Still getting delta frames, schedule another keyframe request as if
241 // decode failed. 280 // decode failed.
242 if (frame->FrameType() != kVideoFrameKey) { 281 if (frame->FrameType() != kVideoFrameKey) {
282 drop_frame = true;
243 _scheduleKeyRequest = true; 283 _scheduleKeyRequest = true;
244 _receiver.ReleaseFrame(frame); 284 } else {
245 return VCM_FRAME_NOT_READY; 285 drop_frames_until_keyframe_ = false;
246 } 286 }
247 drop_frames_until_keyframe_ = false;
248 } 287 }
249 } 288 }
250 289
290 if (drop_frame) {
291 _receiver.ReleaseFrame(frame);
292 return VCM_FRAME_NOT_READY;
293 }
294
251 if (pre_decode_image_callback_) { 295 if (pre_decode_image_callback_) {
252 EncodedImage encoded_image(frame->EncodedImage()); 296 EncodedImage encoded_image(frame->EncodedImage());
253 int qp = -1; 297 int qp = -1;
254 if (qp_parser_.GetQp(*frame, &qp)) { 298 if (qp_parser_.GetQp(*frame, &qp)) {
255 encoded_image.qp_ = qp; 299 encoded_image.qp_ = qp;
256 } 300 }
257 pre_decode_image_callback_->OnEncodedImage(encoded_image, 301 pre_decode_image_callback_->OnEncodedImage(encoded_image,
258 frame->CodecSpecific(), nullptr); 302 frame->CodecSpecific(), nullptr);
259 } 303 }
260 304
261 rtc::CritScope cs(&receive_crit_);
262 // If this frame was too late, we should adjust the delay accordingly 305 // If this frame was too late, we should adjust the delay accordingly
263 _timing->UpdateCurrentDelay(frame->RenderTimeMs(), 306 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
264 clock_->TimeInMilliseconds()); 307 clock_->TimeInMilliseconds());
265 308
266 if (first_frame_received_()) { 309 if (first_frame_received_()) {
267 LOG(LS_INFO) << "Received first " 310 LOG(LS_INFO) << "Received first "
268 << (frame->Complete() ? "complete" : "incomplete") 311 << (frame->Complete() ? "complete" : "incomplete")
269 << " decodable video frame"; 312 << " decodable video frame";
270 } 313 }
271 314
272 const int32_t ret = Decode(*frame); 315 const int32_t ret = Decode(*frame);
273 _receiver.ReleaseFrame(frame); 316 _receiver.ReleaseFrame(frame);
274 return ret; 317 return ret;
275 } 318 }
276 319
277 // Used for the new jitter buffer. 320 // Used for the new jitter buffer.
278 // TODO(philipel): Clean up among the Decode functions as we replace 321 // TODO(philipel): Clean up among the Decode functions as we replace
279 // VCMEncodedFrame with FrameObject. 322 // VCMEncodedFrame with FrameObject.
280 int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) { 323 int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) {
281 rtc::CritScope lock(&receive_crit_); 324 RTC_DCHECK_RUN_ON(&decoder_thread_);
282 if (pre_decode_image_callback_) { 325 if (pre_decode_image_callback_) {
283 EncodedImage encoded_image(frame->EncodedImage()); 326 EncodedImage encoded_image(frame->EncodedImage());
284 int qp = -1; 327 int qp = -1;
285 if (qp_parser_.GetQp(*frame, &qp)) { 328 if (qp_parser_.GetQp(*frame, &qp)) {
286 encoded_image.qp_ = qp; 329 encoded_image.qp_ = qp;
287 } 330 }
288 pre_decode_image_callback_->OnEncodedImage(encoded_image, 331 pre_decode_image_callback_->OnEncodedImage(encoded_image,
289 frame->CodecSpecific(), nullptr); 332 frame->CodecSpecific(), nullptr);
290 } 333 }
291 return Decode(*frame); 334 return Decode(*frame);
292 } 335 }
293 336
294 void VideoReceiver::DecodingStopped() { 337 int32_t VideoReceiver::RequestKeyFrame() {
295 // No further calls to Decode() will be made after this point. 338 RTC_DCHECK_RUN_ON(&module_thread_);
296 // TODO(tommi): Make use of this to clarify and check threading model.
297 }
298 339
299 int32_t VideoReceiver::RequestKeyFrame() { 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
300 TRACE_EVENT0("webrtc", "RequestKeyFrame"); 344 TRACE_EVENT0("webrtc", "RequestKeyFrame");
301 rtc::CritScope cs(&process_crit_);
302 if (_frameTypeCallback != nullptr) { 345 if (_frameTypeCallback != nullptr) {
303 const int32_t ret = _frameTypeCallback->RequestKeyFrame(); 346 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
304 if (ret < 0) { 347 if (ret < 0) {
305 return ret; 348 return ret;
306 } 349 }
350 rtc::CritScope cs(&process_crit_);
307 _scheduleKeyRequest = false; 351 _scheduleKeyRequest = false;
308 } else { 352 } else {
309 return VCM_MISSING_CALLBACK; 353 return VCM_MISSING_CALLBACK;
310 } 354 }
311 return VCM_OK; 355 return VCM_OK;
312 } 356 }
313 357
314 // Must be called from inside the receive side critical section. 358 // Must be called from inside the receive side critical section.
315 int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) { 359 int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
360 RTC_DCHECK_RUN_ON(&decoder_thread_);
316 TRACE_EVENT0("webrtc", "VideoReceiver::Decode"); 361 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
317 // Change decoder if payload type has changed 362 // Change decoder if payload type has changed
318 VCMGenericDecoder* decoder = 363 VCMGenericDecoder* decoder =
319 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback); 364 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
320 if (decoder == nullptr) { 365 if (decoder == nullptr) {
321 return VCM_NO_CODEC_REGISTERED; 366 return VCM_NO_CODEC_REGISTERED;
322 } 367 }
323 // Decode a frame 368 // Decode a frame
324 int32_t ret = decoder->Decode(frame, clock_->TimeInMilliseconds()); 369 int32_t ret = decoder->Decode(frame, clock_->TimeInMilliseconds());
325 370
326 // Check for failed decoding, run frame type request callback if needed. 371 // Check for failed decoding, run frame type request callback if needed.
327 bool request_key_frame = false; 372 bool request_key_frame = (ret < 0);
328 if (ret < 0) {
329 request_key_frame = true;
330 }
331 373
332 if (!frame.Complete() || frame.MissingFrame()) { 374 if (!frame.Complete() || frame.MissingFrame()) {
333 request_key_frame = true; 375 request_key_frame = true;
334 ret = VCM_OK; 376 ret = VCM_OK;
335 } 377 }
378
336 if (request_key_frame) { 379 if (request_key_frame) {
337 rtc::CritScope cs(&process_crit_); 380 rtc::CritScope cs(&process_crit_);
338 _scheduleKeyRequest = true; 381 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 }
339 } 388 }
340 return ret; 389 return ret;
341 } 390 }
342 391
392 #if defined(WEBRTC_ANDROID)
393 void 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
343 // Register possible receive codecs, can be called multiple times 401 // Register possible receive codecs, can be called multiple times
344 int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec, 402 int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
345 int32_t numberOfCores, 403 int32_t numberOfCores,
346 bool requireKeyFrame) { 404 bool requireKeyFrame) {
347 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 405 RTC_DCHECK_RUN_ON(&construction_thread_);
348 // TODO(tommi): This method must only be called when the decoder thread 406 RTC_DCHECK(!IsDecoderThreadRunning());
349 // is not running. Do we need a lock? If not, it looks like we might not need
350 // a lock at all for |_codecDataBase|.
351 rtc::CritScope cs(&receive_crit_);
352 if (receiveCodec == nullptr) { 407 if (receiveCodec == nullptr) {
353 return VCM_PARAMETER_ERROR; 408 return VCM_PARAMETER_ERROR;
354 } 409 }
355 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores, 410 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
356 requireKeyFrame)) { 411 requireKeyFrame)) {
357 return -1; 412 return -1;
358 } 413 }
359 return 0; 414 return 0;
360 } 415 }
361 416
362 // Incoming packet from network parsed and ready for decode, non blocking. 417 // Incoming packet from network parsed and ready for decode, non blocking.
363 int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload, 418 int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
364 size_t payloadLength, 419 size_t payloadLength,
365 const WebRtcRTPHeader& rtpInfo) { 420 const WebRtcRTPHeader& rtpInfo) {
421 RTC_DCHECK_RUN_ON(&module_thread_);
366 if (rtpInfo.frameType == kVideoFrameKey) { 422 if (rtpInfo.frameType == kVideoFrameKey) {
367 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum", 423 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
368 rtpInfo.header.sequenceNumber); 424 rtpInfo.header.sequenceNumber);
369 } 425 }
370 if (incomingPayload == nullptr) { 426 if (incomingPayload == nullptr) {
371 // The jitter buffer doesn't handle non-zero payload lengths for packets 427 // The jitter buffer doesn't handle non-zero payload lengths for packets
372 // without payload. 428 // without payload.
373 // TODO(holmer): We should fix this in the jitter buffer. 429 // TODO(holmer): We should fix this in the jitter buffer.
374 payloadLength = 0; 430 payloadLength = 0;
375 } 431 }
(...skipping 11 matching lines...) Expand all
387 } else if (ret < 0) { 443 } else if (ret < 0) {
388 return ret; 444 return ret;
389 } 445 }
390 return VCM_OK; 446 return VCM_OK;
391 } 447 }
392 448
393 // Minimum playout delay (used for lip-sync). This is the minimum delay required 449 // Minimum playout delay (used for lip-sync). This is the minimum delay required
394 // to sync with audio. Not included in VideoCodingModule::Delay() 450 // to sync with audio. Not included in VideoCodingModule::Delay()
395 // Defaults to 0 ms. 451 // Defaults to 0 ms.
396 int32_t VideoReceiver::SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) { 452 int32_t VideoReceiver::SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) {
453 RTC_DCHECK_RUN_ON(&module_thread_);
397 _timing->set_min_playout_delay(minPlayoutDelayMs); 454 _timing->set_min_playout_delay(minPlayoutDelayMs);
398 return VCM_OK; 455 return VCM_OK;
399 } 456 }
400 457
401 // The estimated delay caused by rendering, defaults to 458 // The estimated delay caused by rendering, defaults to
402 // kDefaultRenderDelayMs = 10 ms 459 // kDefaultRenderDelayMs = 10 ms
403 int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) { 460 int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) {
461 RTC_DCHECK_RUN_ON(&construction_thread_);
462 RTC_DCHECK(!IsDecoderThreadRunning());
404 _timing->set_render_delay(timeMS); 463 _timing->set_render_delay(timeMS);
405 return VCM_OK; 464 return VCM_OK;
406 } 465 }
407 466
408 // Current video delay 467 // Current video delay
409 int32_t VideoReceiver::Delay() const { 468 int32_t VideoReceiver::Delay() const {
469 RTC_DCHECK_RUN_ON(&module_thread_);
410 return _timing->TargetVideoDelay(); 470 return _timing->TargetVideoDelay();
411 } 471 }
412 472
473 // Only used by VCMRobustnessTest.
413 int VideoReceiver::SetReceiverRobustnessMode( 474 int VideoReceiver::SetReceiverRobustnessMode(
414 VideoCodingModule::ReceiverRobustness robustnessMode, 475 VideoCodingModule::ReceiverRobustness robustnessMode,
415 VCMDecodeErrorMode decode_error_mode) { 476 VCMDecodeErrorMode decode_error_mode) {
416 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 477 RTC_DCHECK_RUN_ON(&construction_thread_);
417 // TODO(tommi): This method must only be called when the decoder thread 478 RTC_DCHECK(!IsDecoderThreadRunning());
418 // is not running and we don't need to hold this lock.
419 rtc::CritScope cs(&receive_crit_);
420 switch (robustnessMode) { 479 switch (robustnessMode) {
421 case VideoCodingModule::kNone: 480 case VideoCodingModule::kNone:
422 _receiver.SetNackMode(kNoNack, -1, -1); 481 _receiver.SetNackMode(kNoNack, -1, -1);
423 break; 482 break;
424 case VideoCodingModule::kHardNack: 483 case VideoCodingModule::kHardNack:
425 // Always wait for retransmissions (except when decoding with errors). 484 // Always wait for retransmissions (except when decoding with errors).
426 _receiver.SetNackMode(kNack, -1, -1); 485 _receiver.SetNackMode(kNack, -1, -1);
427 break; 486 break;
428 default: 487 default:
429 RTC_NOTREACHED(); 488 RTC_NOTREACHED();
430 return VCM_PARAMETER_ERROR; 489 return VCM_PARAMETER_ERROR;
431 } 490 }
432 _receiver.SetDecodeErrorMode(decode_error_mode); 491 _receiver.SetDecodeErrorMode(decode_error_mode);
433 return VCM_OK; 492 return VCM_OK;
434 } 493 }
435 494
436 void VideoReceiver::SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) { 495 void VideoReceiver::SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) {
437 rtc::CritScope cs(&receive_crit_); 496 RTC_DCHECK_RUN_ON(&construction_thread_);
497 RTC_DCHECK(!IsDecoderThreadRunning());
438 _receiver.SetDecodeErrorMode(decode_error_mode); 498 _receiver.SetDecodeErrorMode(decode_error_mode);
439 } 499 }
440 500
441 void VideoReceiver::SetNackSettings(size_t max_nack_list_size, 501 void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
442 int max_packet_age_to_nack, 502 int max_packet_age_to_nack,
443 int max_incomplete_time_ms) { 503 int max_incomplete_time_ms) {
444 if (max_nack_list_size != 0) { 504 RTC_DCHECK_RUN_ON(&construction_thread_);
445 rtc::CritScope cs(&process_crit_); 505 RTC_DCHECK(!IsDecoderThreadRunning());
506
507 if (max_nack_list_size != 0)
446 max_nack_list_size_ = max_nack_list_size; 508 max_nack_list_size_ = max_nack_list_size;
447 }
448 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack, 509 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
449 max_incomplete_time_ms); 510 max_incomplete_time_ms);
450 } 511 }
451 512
452 int VideoReceiver::SetMinReceiverDelay(int desired_delay_ms) { 513 int VideoReceiver::SetMinReceiverDelay(int desired_delay_ms) {
514 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.
453 return _receiver.SetMinReceiverDelay(desired_delay_ms); 520 return _receiver.SetMinReceiverDelay(desired_delay_ms);
454 } 521 }
455 522
523 bool VideoReceiver::IsDecoderThreadRunning() {
524 #if RTC_DCHECK_IS_ON
525 return decoder_thread_is_running_;
526 #else
527 return true;
528 #endif
529 }
530
456 } // namespace vcm 531 } // namespace vcm
457 } // namespace webrtc 532 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/video_coding_impl.h ('k') | webrtc/sdk/android/src/jni/androidmediadecoder_jni.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698