OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 LOG(LS_VERBOSE) | 218 LOG(LS_VERBOSE) |
219 << "Incoming frame dropped due to that the encoder is blocked."; | 219 << "Incoming frame dropped due to that the encoder is blocked."; |
220 } | 220 } |
221 return true; | 221 return true; |
222 } | 222 } |
223 VideoFrame frame_; | 223 VideoFrame frame_; |
224 ViEEncoder* const vie_encoder_; | 224 ViEEncoder* const vie_encoder_; |
225 const int64_t time_when_posted_ms_; | 225 const int64_t time_when_posted_ms_; |
226 }; | 226 }; |
227 | 227 |
228 // VideoSourceProxy is responsible ensuring thread safety between calls to | |
229 // ViEEncoder::SetSource that will happen on libjingles worker thread when a | |
230 // video capturer is connected to the encoder and the encoder task queue | |
231 // (encoder_queue_) where encoders report its VideoSinkWants. | |
stefan-webrtc
2016/09/13 09:26:43
"report their"
perkj_webrtc
2016/09/14 14:20:22
encoder reports its Vide,,
| |
232 class ViEEncoder::VideoSourceProxy { | |
233 public: | |
234 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | |
235 : vie_encoder_(vie_encoder), source_(nullptr) {} | |
236 | |
237 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | |
238 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | |
239 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | |
240 { | |
241 rtc::CritScope lock(&crit_); | |
242 old_source = source_; | |
243 source_ = source; | |
244 } | |
245 | |
246 if (old_source != source && old_source != nullptr) { | |
247 old_source->RemoveSink(vie_encoder_); | |
248 } | |
249 | |
250 if (!source) { | |
251 return; | |
252 } | |
253 | |
254 // TODO(perkj): Let VideoSourceProxy implement LoadObserver and truly send | |
255 // CPU load as sink wants. | |
256 rtc::VideoSinkWants wants; | |
257 source->AddOrUpdateSink(vie_encoder_, wants); | |
258 } | |
259 | |
260 private: | |
261 rtc::CriticalSection crit_; | |
262 rtc::SequencedTaskChecker main_checker_; | |
263 ViEEncoder* vie_encoder_; | |
264 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | |
265 | |
266 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | |
267 }; | |
268 | |
228 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 269 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
229 SendStatisticsProxy* stats_proxy, | 270 SendStatisticsProxy* stats_proxy, |
230 const VideoSendStream::Config::EncoderSettings& settings, | 271 const VideoSendStream::Config::EncoderSettings& settings, |
231 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 272 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
232 LoadObserver* overuse_callback, | 273 LoadObserver* overuse_callback, |
233 EncodedFrameObserver* encoder_timing) | 274 EncodedFrameObserver* encoder_timing) |
234 : shutdown_event_(true /* manual_reset */, false), | 275 : shutdown_event_(true /* manual_reset */, false), |
235 number_of_cores_(number_of_cores), | 276 number_of_cores_(number_of_cores), |
277 source_proxy_(new VideoSourceProxy(this)), | |
236 settings_(settings), | 278 settings_(settings), |
237 vp_(VideoProcessing::Create()), | 279 vp_(VideoProcessing::Create()), |
238 video_sender_(Clock::GetRealTimeClock(), this, this), | 280 video_sender_(Clock::GetRealTimeClock(), this, this), |
239 overuse_detector_(Clock::GetRealTimeClock(), | 281 overuse_detector_(Clock::GetRealTimeClock(), |
240 GetCpuOveruseOptions(settings.full_overuse_time), | 282 GetCpuOveruseOptions(settings.full_overuse_time), |
241 this, | 283 this, |
242 encoder_timing, | 284 encoder_timing, |
243 stats_proxy), | 285 stats_proxy), |
244 load_observer_(overuse_callback), | 286 load_observer_(overuse_callback), |
245 stats_proxy_(stats_proxy), | 287 stats_proxy_(stats_proxy), |
(...skipping 16 matching lines...) Expand all Loading... | |
262 | 304 |
263 encoder_queue_.PostTask([this, encoder_timing] { | 305 encoder_queue_.PostTask([this, encoder_timing] { |
264 RTC_DCHECK_RUN_ON(&encoder_queue_); | 306 RTC_DCHECK_RUN_ON(&encoder_queue_); |
265 video_sender_.RegisterExternalEncoder( | 307 video_sender_.RegisterExternalEncoder( |
266 settings_.encoder, settings_.payload_type, settings_.internal_source); | 308 settings_.encoder, settings_.payload_type, settings_.internal_source); |
267 overuse_detector_.StartCheckForOveruse(); | 309 overuse_detector_.StartCheckForOveruse(); |
268 }); | 310 }); |
269 } | 311 } |
270 | 312 |
271 ViEEncoder::~ViEEncoder() { | 313 ViEEncoder::~ViEEncoder() { |
314 RTC_DCHECK_RUN_ON(&main_thread_checker_); | |
272 RTC_DCHECK(shutdown_event_.Wait(0)) | 315 RTC_DCHECK(shutdown_event_.Wait(0)) |
273 << "Must call ::Stop() before destruction."; | 316 << "Must call ::Stop() before destruction."; |
274 } | 317 } |
275 | 318 |
276 void ViEEncoder::Stop() { | 319 void ViEEncoder::Stop() { |
277 if (!encoder_queue_.IsCurrent()) { | 320 RTC_DCHECK_RUN_ON(&main_thread_checker_); |
278 encoder_queue_.PostTask([this] { Stop(); }); | 321 source_proxy_->SetSource(nullptr); |
279 shutdown_event_.Wait(rtc::Event::kForever); | 322 encoder_queue_.PostTask([this] { |
280 return; | 323 RTC_DCHECK_RUN_ON(&encoder_queue_); |
281 } | 324 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
282 RTC_DCHECK_RUN_ON(&encoder_queue_); | 325 false); |
283 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, false); | 326 overuse_detector_.StopCheckForOveruse(); |
284 overuse_detector_.StopCheckForOveruse(); | 327 shutdown_event_.Set(); |
285 shutdown_event_.Set(); | 328 }); |
329 | |
330 shutdown_event_.Wait(rtc::Event::kForever); | |
286 } | 331 } |
287 | 332 |
288 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 333 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
334 RTC_DCHECK_RUN_ON(&main_thread_checker_); | |
289 RTC_DCHECK(!module_process_thread_); | 335 RTC_DCHECK(!module_process_thread_); |
290 module_process_thread_ = module_process_thread; | 336 module_process_thread_ = module_process_thread; |
291 module_process_thread_->RegisterModule(&video_sender_); | 337 module_process_thread_->RegisterModule(&video_sender_); |
292 module_process_thread_checker_.DetachFromThread(); | 338 module_process_thread_checker_.DetachFromThread(); |
293 } | 339 } |
294 | 340 |
295 void ViEEncoder::DeRegisterProcessThread() { | 341 void ViEEncoder::DeRegisterProcessThread() { |
342 RTC_DCHECK_RUN_ON(&main_thread_checker_); | |
296 module_process_thread_->DeRegisterModule(&video_sender_); | 343 module_process_thread_->DeRegisterModule(&video_sender_); |
297 } | 344 } |
298 | 345 |
346 void ViEEncoder::SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | |
347 RTC_DCHECK_RUN_ON(&main_thread_checker_); | |
348 source_proxy_->SetSource(source); | |
349 } | |
350 | |
299 void ViEEncoder::SetSink(EncodedImageCallback* sink) { | 351 void ViEEncoder::SetSink(EncodedImageCallback* sink) { |
300 encoder_queue_.PostTask([this, sink] { | 352 encoder_queue_.PostTask([this, sink] { |
301 RTC_DCHECK_RUN_ON(&encoder_queue_); | 353 RTC_DCHECK_RUN_ON(&encoder_queue_); |
302 sink_ = sink; | 354 sink_ = sink; |
303 }); | 355 }); |
304 } | 356 } |
305 | 357 |
306 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { | 358 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { |
307 encoder_queue_.PostTask([this, start_bitrate_bps] { | 359 encoder_queue_.PostTask([this, start_bitrate_bps] { |
308 RTC_DCHECK_RUN_ON(&encoder_queue_); | 360 RTC_DCHECK_RUN_ON(&encoder_queue_); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 content_type = VideoEncoderConfig::ContentType::kScreen; | 410 content_type = VideoEncoderConfig::ContentType::kScreen; |
359 break; | 411 break; |
360 default: | 412 default: |
361 RTC_NOTREACHED(); | 413 RTC_NOTREACHED(); |
362 break; | 414 break; |
363 } | 415 } |
364 stats_proxy_->SetContentType(content_type); | 416 stats_proxy_->SetContentType(content_type); |
365 } | 417 } |
366 } | 418 } |
367 | 419 |
368 void ViEEncoder::IncomingCapturedFrame(const VideoFrame& video_frame) { | 420 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
369 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 421 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
370 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | 422 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); |
371 | 423 |
372 VideoFrame incoming_frame = video_frame; | 424 VideoFrame incoming_frame = video_frame; |
373 | 425 |
374 // Local time in webrtc time base. | 426 // Local time in webrtc time base. |
375 int64_t current_time = clock_->TimeInMilliseconds(); | 427 int64_t current_time = clock_->TimeInMilliseconds(); |
376 incoming_frame.set_render_time_ms(current_time); | 428 incoming_frame.set_render_time_ms(current_time); |
377 | 429 |
378 // Capture time may come from clock with an offset and drift from clock_. | 430 // Capture time may come from clock with an offset and drift from clock_. |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
594 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); | 646 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); |
595 } | 647 } |
596 | 648 |
597 void ViEEncoder::NormalUsage() { | 649 void ViEEncoder::NormalUsage() { |
598 RTC_DCHECK_RUN_ON(&encoder_queue_); | 650 RTC_DCHECK_RUN_ON(&encoder_queue_); |
599 if (load_observer_) | 651 if (load_observer_) |
600 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); | 652 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); |
601 } | 653 } |
602 | 654 |
603 } // namespace webrtc | 655 } // namespace webrtc |
OLD | NEW |