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