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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 vie_encoder_->dropped_frame_count_ = 0; | 234 vie_encoder_->dropped_frame_count_ = 0; |
235 } | 235 } |
236 return true; | 236 return true; |
237 } | 237 } |
238 VideoFrame frame_; | 238 VideoFrame frame_; |
239 ViEEncoder* const vie_encoder_; | 239 ViEEncoder* const vie_encoder_; |
240 const int64_t time_when_posted_ms_; | 240 const int64_t time_when_posted_ms_; |
241 const bool log_stats_; | 241 const bool log_stats_; |
242 }; | 242 }; |
243 | 243 |
244 // VideoSourceProxy is responsible ensuring thread safety between calls to | |
245 // ViEEncoder::SetSource that will happen on libjingles worker thread when a | |
246 // video capturer is connected to the encoder and the encoder task queue | |
247 // (encoder_queue_) where the encoder reports its VideoSinkWants. | |
248 class ViEEncoder::VideoSourceProxy { | |
249 public: | |
250 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | |
251 : vie_encoder_(vie_encoder), source_(nullptr) {} | |
252 | |
253 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | |
254 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | |
255 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | |
256 { | |
257 rtc::CritScope lock(&crit_); | |
258 old_source = source_; | |
259 source_ = source; | |
260 } | |
261 | |
262 if (old_source != source && old_source != nullptr) { | |
263 old_source->RemoveSink(vie_encoder_); | |
264 } | |
265 | |
266 if (!source) { | |
267 return; | |
268 } | |
269 | |
270 // TODO(perkj): Let VideoSourceProxy implement LoadObserver and truly send | |
271 // CPU load as sink wants. | |
272 rtc::VideoSinkWants wants; | |
273 source->AddOrUpdateSink(vie_encoder_, wants); | |
274 } | |
275 | |
276 private: | |
277 rtc::CriticalSection crit_; | |
278 rtc::SequencedTaskChecker main_checker_; | |
279 ViEEncoder* vie_encoder_; | |
280 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | |
281 | |
282 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | |
283 }; | |
284 | |
285 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 244 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
286 SendStatisticsProxy* stats_proxy, | 245 SendStatisticsProxy* stats_proxy, |
287 const VideoSendStream::Config::EncoderSettings& settings, | 246 const VideoSendStream::Config::EncoderSettings& settings, |
288 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 247 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
289 LoadObserver* overuse_callback, | 248 LoadObserver* overuse_callback, |
290 EncodedFrameObserver* encoder_timing) | 249 EncodedFrameObserver* encoder_timing) |
291 : shutdown_event_(true /* manual_reset */, false), | 250 : shutdown_event_(true /* manual_reset */, false), |
292 number_of_cores_(number_of_cores), | 251 number_of_cores_(number_of_cores), |
293 source_proxy_(new VideoSourceProxy(this)), | |
294 settings_(settings), | 252 settings_(settings), |
295 vp_(VideoProcessing::Create()), | 253 vp_(VideoProcessing::Create()), |
296 video_sender_(Clock::GetRealTimeClock(), this, this), | 254 video_sender_(Clock::GetRealTimeClock(), this, this), |
297 overuse_detector_(Clock::GetRealTimeClock(), | 255 overuse_detector_(Clock::GetRealTimeClock(), |
298 GetCpuOveruseOptions(settings.full_overuse_time), | 256 GetCpuOveruseOptions(settings.full_overuse_time), |
299 this, | 257 this, |
300 encoder_timing, | 258 encoder_timing, |
301 stats_proxy), | 259 stats_proxy), |
302 load_observer_(overuse_callback), | 260 load_observer_(overuse_callback), |
303 stats_proxy_(stats_proxy), | 261 stats_proxy_(stats_proxy), |
(...skipping 19 matching lines...) Expand all Loading... |
323 | 281 |
324 encoder_queue_.PostTask([this, encoder_timing] { | 282 encoder_queue_.PostTask([this, encoder_timing] { |
325 RTC_DCHECK_RUN_ON(&encoder_queue_); | 283 RTC_DCHECK_RUN_ON(&encoder_queue_); |
326 video_sender_.RegisterExternalEncoder( | 284 video_sender_.RegisterExternalEncoder( |
327 settings_.encoder, settings_.payload_type, settings_.internal_source); | 285 settings_.encoder, settings_.payload_type, settings_.internal_source); |
328 overuse_detector_.StartCheckForOveruse(); | 286 overuse_detector_.StartCheckForOveruse(); |
329 }); | 287 }); |
330 } | 288 } |
331 | 289 |
332 ViEEncoder::~ViEEncoder() { | 290 ViEEncoder::~ViEEncoder() { |
333 RTC_DCHECK_RUN_ON(&thread_checker_); | |
334 RTC_DCHECK(shutdown_event_.Wait(0)) | 291 RTC_DCHECK(shutdown_event_.Wait(0)) |
335 << "Must call ::Stop() before destruction."; | 292 << "Must call ::Stop() before destruction."; |
336 } | 293 } |
337 | 294 |
338 void ViEEncoder::Stop() { | 295 void ViEEncoder::Stop() { |
339 RTC_DCHECK_RUN_ON(&thread_checker_); | 296 if (!encoder_queue_.IsCurrent()) { |
340 source_proxy_->SetSource(nullptr); | 297 encoder_queue_.PostTask([this] { Stop(); }); |
341 encoder_queue_.PostTask([this] { | 298 shutdown_event_.Wait(rtc::Event::kForever); |
342 RTC_DCHECK_RUN_ON(&encoder_queue_); | 299 return; |
343 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 300 } |
344 false); | 301 RTC_DCHECK_RUN_ON(&encoder_queue_); |
345 overuse_detector_.StopCheckForOveruse(); | 302 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, false); |
346 shutdown_event_.Set(); | 303 overuse_detector_.StopCheckForOveruse(); |
347 }); | 304 shutdown_event_.Set(); |
348 | |
349 shutdown_event_.Wait(rtc::Event::kForever); | |
350 } | 305 } |
351 | 306 |
352 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 307 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
353 RTC_DCHECK_RUN_ON(&thread_checker_); | |
354 RTC_DCHECK(!module_process_thread_); | 308 RTC_DCHECK(!module_process_thread_); |
355 module_process_thread_ = module_process_thread; | 309 module_process_thread_ = module_process_thread; |
356 module_process_thread_->RegisterModule(&video_sender_); | 310 module_process_thread_->RegisterModule(&video_sender_); |
357 module_process_thread_checker_.DetachFromThread(); | 311 module_process_thread_checker_.DetachFromThread(); |
358 } | 312 } |
359 | 313 |
360 void ViEEncoder::DeRegisterProcessThread() { | 314 void ViEEncoder::DeRegisterProcessThread() { |
361 RTC_DCHECK_RUN_ON(&thread_checker_); | |
362 module_process_thread_->DeRegisterModule(&video_sender_); | 315 module_process_thread_->DeRegisterModule(&video_sender_); |
363 } | 316 } |
364 | 317 |
365 void ViEEncoder::SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | |
366 RTC_DCHECK_RUN_ON(&thread_checker_); | |
367 source_proxy_->SetSource(source); | |
368 } | |
369 | |
370 void ViEEncoder::SetSink(EncodedImageCallback* sink) { | 318 void ViEEncoder::SetSink(EncodedImageCallback* sink) { |
371 encoder_queue_.PostTask([this, sink] { | 319 encoder_queue_.PostTask([this, sink] { |
372 RTC_DCHECK_RUN_ON(&encoder_queue_); | 320 RTC_DCHECK_RUN_ON(&encoder_queue_); |
373 sink_ = sink; | 321 sink_ = sink; |
374 }); | 322 }); |
375 } | 323 } |
376 | 324 |
377 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { | 325 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { |
378 encoder_queue_.PostTask([this, start_bitrate_bps] { | 326 encoder_queue_.PostTask([this, start_bitrate_bps] { |
379 RTC_DCHECK_RUN_ON(&encoder_queue_); | 327 RTC_DCHECK_RUN_ON(&encoder_queue_); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 content_type = VideoEncoderConfig::ContentType::kScreen; | 377 content_type = VideoEncoderConfig::ContentType::kScreen; |
430 break; | 378 break; |
431 default: | 379 default: |
432 RTC_NOTREACHED(); | 380 RTC_NOTREACHED(); |
433 break; | 381 break; |
434 } | 382 } |
435 stats_proxy_->SetContentType(content_type); | 383 stats_proxy_->SetContentType(content_type); |
436 } | 384 } |
437 } | 385 } |
438 | 386 |
439 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 387 void ViEEncoder::IncomingCapturedFrame(const VideoFrame& video_frame) { |
440 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 388 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
441 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | 389 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); |
442 | 390 |
443 VideoFrame incoming_frame = video_frame; | 391 VideoFrame incoming_frame = video_frame; |
444 | 392 |
445 // Local time in webrtc time base. | 393 // Local time in webrtc time base. |
446 int64_t current_time = clock_->TimeInMilliseconds(); | 394 int64_t current_time = clock_->TimeInMilliseconds(); |
447 incoming_frame.set_render_time_ms(current_time); | 395 incoming_frame.set_render_time_ms(current_time); |
448 | 396 |
449 // Capture time may come from clock with an offset and drift from clock_. | 397 // Capture time may come from clock with an offset and drift from clock_. |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); | 619 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); |
672 } | 620 } |
673 | 621 |
674 void ViEEncoder::NormalUsage() { | 622 void ViEEncoder::NormalUsage() { |
675 RTC_DCHECK_RUN_ON(&encoder_queue_); | 623 RTC_DCHECK_RUN_ON(&encoder_queue_); |
676 if (load_observer_) | 624 if (load_observer_) |
677 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); | 625 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); |
678 } | 626 } |
679 | 627 |
680 } // namespace webrtc | 628 } // namespace webrtc |
OLD | NEW |