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

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

Issue 2811963004: Deliver video frames on Android, on the decode thread.
Patch Set: Rebase Created 3 years, 4 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/common_types.h" 11 #include "webrtc/common_types.h"
12 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" 12 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
13 #include "webrtc/modules/utility/include/process_thread.h"
13 #include "webrtc/modules/video_coding/encoded_frame.h" 14 #include "webrtc/modules/video_coding/encoded_frame.h"
14 #include "webrtc/modules/video_coding/include/video_codec_interface.h" 15 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
15 #include "webrtc/modules/video_coding/jitter_buffer.h" 16 #include "webrtc/modules/video_coding/jitter_buffer.h"
16 #include "webrtc/modules/video_coding/packet.h" 17 #include "webrtc/modules/video_coding/packet.h"
17 #include "webrtc/modules/video_coding/video_coding_impl.h" 18 #include "webrtc/modules/video_coding/video_coding_impl.h"
18 #include "webrtc/rtc_base/checks.h" 19 #include "webrtc/rtc_base/checks.h"
20 #include "webrtc/rtc_base/location.h"
19 #include "webrtc/rtc_base/logging.h" 21 #include "webrtc/rtc_base/logging.h"
20 #include "webrtc/rtc_base/trace_event.h" 22 #include "webrtc/rtc_base/trace_event.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 // TODO(tommi): Consider if we could instead post a task to the module
245 return VCM_FRAME_NOT_READY; 285 // thread and call RequestKeyFrame directly. Here we call WakeUp so that
286 // TimeUntilNextProcess() gets called straight away.
287 process_thread_->WakeUp(this);
288 } else {
289 drop_frames_until_keyframe_ = false;
246 } 290 }
247 drop_frames_until_keyframe_ = false;
248 } 291 }
249 } 292 }
250 293
294 if (drop_frame) {
295 _receiver.ReleaseFrame(frame);
296 return VCM_FRAME_NOT_READY;
297 }
298
251 if (pre_decode_image_callback_) { 299 if (pre_decode_image_callback_) {
252 EncodedImage encoded_image(frame->EncodedImage()); 300 EncodedImage encoded_image(frame->EncodedImage());
253 int qp = -1; 301 int qp = -1;
254 if (qp_parser_.GetQp(*frame, &qp)) { 302 if (qp_parser_.GetQp(*frame, &qp)) {
255 encoded_image.qp_ = qp; 303 encoded_image.qp_ = qp;
256 } 304 }
257 pre_decode_image_callback_->OnEncodedImage(encoded_image, 305 pre_decode_image_callback_->OnEncodedImage(encoded_image,
258 frame->CodecSpecific(), nullptr); 306 frame->CodecSpecific(), nullptr);
259 } 307 }
260 308
261 rtc::CritScope cs(&receive_crit_);
262 // If this frame was too late, we should adjust the delay accordingly 309 // If this frame was too late, we should adjust the delay accordingly
263 _timing->UpdateCurrentDelay(frame->RenderTimeMs(), 310 _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
264 clock_->TimeInMilliseconds()); 311 clock_->TimeInMilliseconds());
265 312
266 if (first_frame_received_()) { 313 if (first_frame_received_()) {
267 LOG(LS_INFO) << "Received first " 314 LOG(LS_INFO) << "Received first "
268 << (frame->Complete() ? "complete" : "incomplete") 315 << (frame->Complete() ? "complete" : "incomplete")
269 << " decodable video frame"; 316 << " decodable video frame";
270 } 317 }
271 318
272 const int32_t ret = Decode(*frame); 319 const int32_t ret = Decode(*frame);
273 _receiver.ReleaseFrame(frame); 320 _receiver.ReleaseFrame(frame);
274 return ret; 321 return ret;
275 } 322 }
276 323
277 // Used for the new jitter buffer. 324 // Used for the new jitter buffer.
278 // TODO(philipel): Clean up among the Decode functions as we replace 325 // TODO(philipel): Clean up among the Decode functions as we replace
279 // VCMEncodedFrame with FrameObject. 326 // VCMEncodedFrame with FrameObject.
280 int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) { 327 int32_t VideoReceiver::Decode(const webrtc::VCMEncodedFrame* frame) {
281 rtc::CritScope lock(&receive_crit_); 328 RTC_DCHECK_RUN_ON(&decoder_thread_);
282 if (pre_decode_image_callback_) { 329 if (pre_decode_image_callback_) {
283 EncodedImage encoded_image(frame->EncodedImage()); 330 EncodedImage encoded_image(frame->EncodedImage());
284 int qp = -1; 331 int qp = -1;
285 if (qp_parser_.GetQp(*frame, &qp)) { 332 if (qp_parser_.GetQp(*frame, &qp)) {
286 encoded_image.qp_ = qp; 333 encoded_image.qp_ = qp;
287 } 334 }
288 pre_decode_image_callback_->OnEncodedImage(encoded_image, 335 pre_decode_image_callback_->OnEncodedImage(encoded_image,
289 frame->CodecSpecific(), nullptr); 336 frame->CodecSpecific(), nullptr);
290 } 337 }
291 return Decode(*frame); 338 return Decode(*frame);
292 } 339 }
293 340
294 void VideoReceiver::DecodingStopped() { 341 int32_t VideoReceiver::RequestKeyFrame() {
295 // No further calls to Decode() will be made after this point. 342 RTC_DCHECK_RUN_ON(&module_thread_);
296 // TODO(tommi): Make use of this to clarify and check threading model.
297 }
298 343
299 int32_t VideoReceiver::RequestKeyFrame() { 344 // Since we deregister from the module thread when the decoder thread isn't
345 // running, we should get no calls here if decoding isn't being done.
346 RTC_DCHECK(IsDecoderThreadRunning());
347
300 TRACE_EVENT0("webrtc", "RequestKeyFrame"); 348 TRACE_EVENT0("webrtc", "RequestKeyFrame");
301 rtc::CritScope cs(&process_crit_);
302 if (_frameTypeCallback != nullptr) { 349 if (_frameTypeCallback != nullptr) {
303 const int32_t ret = _frameTypeCallback->RequestKeyFrame(); 350 const int32_t ret = _frameTypeCallback->RequestKeyFrame();
304 if (ret < 0) { 351 if (ret < 0) {
305 return ret; 352 return ret;
306 } 353 }
354 rtc::CritScope cs(&process_crit_);
307 _scheduleKeyRequest = false; 355 _scheduleKeyRequest = false;
308 } else { 356 } else {
309 return VCM_MISSING_CALLBACK; 357 return VCM_MISSING_CALLBACK;
310 } 358 }
311 return VCM_OK; 359 return VCM_OK;
312 } 360 }
313 361
314 // Must be called from inside the receive side critical section. 362 // Must be called from inside the receive side critical section.
315 int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) { 363 int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
364 RTC_DCHECK_RUN_ON(&decoder_thread_);
316 TRACE_EVENT0("webrtc", "VideoReceiver::Decode"); 365 TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
317 // Change decoder if payload type has changed 366 // Change decoder if payload type has changed
318 VCMGenericDecoder* decoder = 367 VCMGenericDecoder* decoder =
319 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback); 368 _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
320 if (decoder == nullptr) { 369 if (decoder == nullptr) {
321 return VCM_NO_CODEC_REGISTERED; 370 return VCM_NO_CODEC_REGISTERED;
322 } 371 }
323 return decoder->Decode(frame, clock_->TimeInMilliseconds()); 372 return decoder->Decode(frame, clock_->TimeInMilliseconds());
324 } 373 }
325 374
375 #if defined(WEBRTC_ANDROID)
376 void VideoReceiver::PollDecodedFrames() {
377 RTC_DCHECK_RUN_ON(&decoder_thread_);
378 auto* current_decoder = _codecDataBase.GetCurrentDecoder();
379 if (current_decoder)
380 current_decoder->PollDecodedFrames();
381 }
382 #endif
383
326 // Register possible receive codecs, can be called multiple times 384 // Register possible receive codecs, can be called multiple times
327 int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec, 385 int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
328 int32_t numberOfCores, 386 int32_t numberOfCores,
329 bool requireKeyFrame) { 387 bool requireKeyFrame) {
330 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 388 RTC_DCHECK_RUN_ON(&construction_thread_);
331 // TODO(tommi): This method must only be called when the decoder thread 389 RTC_DCHECK(!IsDecoderThreadRunning());
332 // is not running. Do we need a lock? If not, it looks like we might not need
333 // a lock at all for |_codecDataBase|.
334 rtc::CritScope cs(&receive_crit_);
335 if (receiveCodec == nullptr) { 390 if (receiveCodec == nullptr) {
336 return VCM_PARAMETER_ERROR; 391 return VCM_PARAMETER_ERROR;
337 } 392 }
338 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores, 393 if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
339 requireKeyFrame)) { 394 requireKeyFrame)) {
340 return -1; 395 return -1;
341 } 396 }
342 return 0; 397 return 0;
343 } 398 }
344 399
345 // Incoming packet from network parsed and ready for decode, non blocking. 400 // Incoming packet from network parsed and ready for decode, non blocking.
346 int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload, 401 int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
347 size_t payloadLength, 402 size_t payloadLength,
348 const WebRtcRTPHeader& rtpInfo) { 403 const WebRtcRTPHeader& rtpInfo) {
404 RTC_DCHECK_RUN_ON(&module_thread_);
349 if (rtpInfo.frameType == kVideoFrameKey) { 405 if (rtpInfo.frameType == kVideoFrameKey) {
350 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum", 406 TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
351 rtpInfo.header.sequenceNumber); 407 rtpInfo.header.sequenceNumber);
352 } 408 }
353 if (incomingPayload == nullptr) { 409 if (incomingPayload == nullptr) {
354 // The jitter buffer doesn't handle non-zero payload lengths for packets 410 // The jitter buffer doesn't handle non-zero payload lengths for packets
355 // without payload. 411 // without payload.
356 // TODO(holmer): We should fix this in the jitter buffer. 412 // TODO(holmer): We should fix this in the jitter buffer.
357 payloadLength = 0; 413 payloadLength = 0;
358 } 414 }
(...skipping 11 matching lines...) Expand all
370 } else if (ret < 0) { 426 } else if (ret < 0) {
371 return ret; 427 return ret;
372 } 428 }
373 return VCM_OK; 429 return VCM_OK;
374 } 430 }
375 431
376 // Minimum playout delay (used for lip-sync). This is the minimum delay required 432 // Minimum playout delay (used for lip-sync). This is the minimum delay required
377 // to sync with audio. Not included in VideoCodingModule::Delay() 433 // to sync with audio. Not included in VideoCodingModule::Delay()
378 // Defaults to 0 ms. 434 // Defaults to 0 ms.
379 int32_t VideoReceiver::SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) { 435 int32_t VideoReceiver::SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) {
436 RTC_DCHECK_RUN_ON(&module_thread_);
380 _timing->set_min_playout_delay(minPlayoutDelayMs); 437 _timing->set_min_playout_delay(minPlayoutDelayMs);
381 return VCM_OK; 438 return VCM_OK;
382 } 439 }
383 440
384 // The estimated delay caused by rendering, defaults to 441 // The estimated delay caused by rendering, defaults to
385 // kDefaultRenderDelayMs = 10 ms 442 // kDefaultRenderDelayMs = 10 ms
386 int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) { 443 int32_t VideoReceiver::SetRenderDelay(uint32_t timeMS) {
444 RTC_DCHECK_RUN_ON(&construction_thread_);
445 RTC_DCHECK(!IsDecoderThreadRunning());
387 _timing->set_render_delay(timeMS); 446 _timing->set_render_delay(timeMS);
388 return VCM_OK; 447 return VCM_OK;
389 } 448 }
390 449
391 // Current video delay 450 // Current video delay
392 int32_t VideoReceiver::Delay() const { 451 int32_t VideoReceiver::Delay() const {
452 RTC_DCHECK_RUN_ON(&module_thread_);
393 return _timing->TargetVideoDelay(); 453 return _timing->TargetVideoDelay();
394 } 454 }
395 455
456 // Only used by VCMRobustnessTest.
396 int VideoReceiver::SetReceiverRobustnessMode( 457 int VideoReceiver::SetReceiverRobustnessMode(
397 VideoCodingModule::ReceiverRobustness robustnessMode, 458 VideoCodingModule::ReceiverRobustness robustnessMode,
398 VCMDecodeErrorMode decode_error_mode) { 459 VCMDecodeErrorMode decode_error_mode) {
399 RTC_DCHECK(construction_thread_.CalledOnValidThread()); 460 RTC_DCHECK_RUN_ON(&construction_thread_);
400 // TODO(tommi): This method must only be called when the decoder thread 461 RTC_DCHECK(!IsDecoderThreadRunning());
401 // is not running and we don't need to hold this lock.
402 rtc::CritScope cs(&receive_crit_);
403 switch (robustnessMode) { 462 switch (robustnessMode) {
404 case VideoCodingModule::kNone: 463 case VideoCodingModule::kNone:
405 _receiver.SetNackMode(kNoNack, -1, -1); 464 _receiver.SetNackMode(kNoNack, -1, -1);
406 break; 465 break;
407 case VideoCodingModule::kHardNack: 466 case VideoCodingModule::kHardNack:
408 // Always wait for retransmissions (except when decoding with errors). 467 // Always wait for retransmissions (except when decoding with errors).
409 _receiver.SetNackMode(kNack, -1, -1); 468 _receiver.SetNackMode(kNack, -1, -1);
410 break; 469 break;
411 default: 470 default:
412 RTC_NOTREACHED(); 471 RTC_NOTREACHED();
413 return VCM_PARAMETER_ERROR; 472 return VCM_PARAMETER_ERROR;
414 } 473 }
415 _receiver.SetDecodeErrorMode(decode_error_mode); 474 _receiver.SetDecodeErrorMode(decode_error_mode);
416 return VCM_OK; 475 return VCM_OK;
417 } 476 }
418 477
419 void VideoReceiver::SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) { 478 void VideoReceiver::SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) {
420 rtc::CritScope cs(&receive_crit_); 479 RTC_DCHECK_RUN_ON(&construction_thread_);
480 RTC_DCHECK(!IsDecoderThreadRunning());
421 _receiver.SetDecodeErrorMode(decode_error_mode); 481 _receiver.SetDecodeErrorMode(decode_error_mode);
422 } 482 }
423 483
424 void VideoReceiver::SetNackSettings(size_t max_nack_list_size, 484 void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
425 int max_packet_age_to_nack, 485 int max_packet_age_to_nack,
426 int max_incomplete_time_ms) { 486 int max_incomplete_time_ms) {
427 if (max_nack_list_size != 0) { 487 RTC_DCHECK_RUN_ON(&construction_thread_);
428 rtc::CritScope cs(&process_crit_); 488 RTC_DCHECK(!IsDecoderThreadRunning());
489
490 if (max_nack_list_size != 0)
429 max_nack_list_size_ = max_nack_list_size; 491 max_nack_list_size_ = max_nack_list_size;
430 }
431 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack, 492 _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
432 max_incomplete_time_ms); 493 max_incomplete_time_ms);
433 } 494 }
434 495
435 int VideoReceiver::SetMinReceiverDelay(int desired_delay_ms) { 496 int VideoReceiver::SetMinReceiverDelay(int desired_delay_ms) {
497 RTC_DCHECK_RUN_ON(&construction_thread_);
498 RTC_DCHECK(!IsDecoderThreadRunning());
499 // TODO(tommi): Is the method only used by tests? Maybe could be offered
500 // via a test only subclass?
501 // Info from Stefan: If it is indeed only used by tests I think it's just that
502 // it hasn't been cleaned up when the calling code was cleaned up.
436 return _receiver.SetMinReceiverDelay(desired_delay_ms); 503 return _receiver.SetMinReceiverDelay(desired_delay_ms);
437 } 504 }
438 505
506 bool VideoReceiver::IsDecoderThreadRunning() {
507 #if RTC_DCHECK_IS_ON
508 return decoder_thread_is_running_;
509 #else
510 return true;
511 #endif
512 }
513
439 } // namespace vcm 514 } // namespace vcm
440 } // namespace webrtc 515 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/video_coding_impl.h ('k') | webrtc/sdk/android/api/org/webrtc/MediaCodecVideoDecoder.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698