| 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 |
| 244 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 285 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
| 245 SendStatisticsProxy* stats_proxy, | 286 SendStatisticsProxy* stats_proxy, |
| 246 const VideoSendStream::Config::EncoderSettings& settings, | 287 const VideoSendStream::Config::EncoderSettings& settings, |
| 247 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 288 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
| 248 LoadObserver* overuse_callback, | 289 LoadObserver* overuse_callback, |
| 249 EncodedFrameObserver* encoder_timing) | 290 EncodedFrameObserver* encoder_timing) |
| 250 : shutdown_event_(true /* manual_reset */, false), | 291 : shutdown_event_(true /* manual_reset */, false), |
| 251 number_of_cores_(number_of_cores), | 292 number_of_cores_(number_of_cores), |
| 293 source_proxy_(new VideoSourceProxy(this)), |
| 252 settings_(settings), | 294 settings_(settings), |
| 253 vp_(VideoProcessing::Create()), | 295 vp_(VideoProcessing::Create()), |
| 254 video_sender_(Clock::GetRealTimeClock(), this, this), | 296 video_sender_(Clock::GetRealTimeClock(), this, this), |
| 255 overuse_detector_(Clock::GetRealTimeClock(), | 297 overuse_detector_(Clock::GetRealTimeClock(), |
| 256 GetCpuOveruseOptions(settings.full_overuse_time), | 298 GetCpuOveruseOptions(settings.full_overuse_time), |
| 257 this, | 299 this, |
| 258 encoder_timing, | 300 encoder_timing, |
| 259 stats_proxy), | 301 stats_proxy), |
| 260 load_observer_(overuse_callback), | 302 load_observer_(overuse_callback), |
| 261 stats_proxy_(stats_proxy), | 303 stats_proxy_(stats_proxy), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 281 | 323 |
| 282 encoder_queue_.PostTask([this, encoder_timing] { | 324 encoder_queue_.PostTask([this, encoder_timing] { |
| 283 RTC_DCHECK_RUN_ON(&encoder_queue_); | 325 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 284 video_sender_.RegisterExternalEncoder( | 326 video_sender_.RegisterExternalEncoder( |
| 285 settings_.encoder, settings_.payload_type, settings_.internal_source); | 327 settings_.encoder, settings_.payload_type, settings_.internal_source); |
| 286 overuse_detector_.StartCheckForOveruse(); | 328 overuse_detector_.StartCheckForOveruse(); |
| 287 }); | 329 }); |
| 288 } | 330 } |
| 289 | 331 |
| 290 ViEEncoder::~ViEEncoder() { | 332 ViEEncoder::~ViEEncoder() { |
| 333 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 291 RTC_DCHECK(shutdown_event_.Wait(0)) | 334 RTC_DCHECK(shutdown_event_.Wait(0)) |
| 292 << "Must call ::Stop() before destruction."; | 335 << "Must call ::Stop() before destruction."; |
| 293 } | 336 } |
| 294 | 337 |
| 295 void ViEEncoder::Stop() { | 338 void ViEEncoder::Stop() { |
| 296 if (!encoder_queue_.IsCurrent()) { | 339 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 297 encoder_queue_.PostTask([this] { Stop(); }); | 340 source_proxy_->SetSource(nullptr); |
| 298 shutdown_event_.Wait(rtc::Event::kForever); | 341 encoder_queue_.PostTask([this] { |
| 299 return; | 342 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 300 } | 343 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
| 301 RTC_DCHECK_RUN_ON(&encoder_queue_); | 344 false); |
| 302 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, false); | 345 overuse_detector_.StopCheckForOveruse(); |
| 303 overuse_detector_.StopCheckForOveruse(); | 346 shutdown_event_.Set(); |
| 304 shutdown_event_.Set(); | 347 }); |
| 348 |
| 349 shutdown_event_.Wait(rtc::Event::kForever); |
| 305 } | 350 } |
| 306 | 351 |
| 307 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 352 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
| 353 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 308 RTC_DCHECK(!module_process_thread_); | 354 RTC_DCHECK(!module_process_thread_); |
| 309 module_process_thread_ = module_process_thread; | 355 module_process_thread_ = module_process_thread; |
| 310 module_process_thread_->RegisterModule(&video_sender_); | 356 module_process_thread_->RegisterModule(&video_sender_); |
| 311 module_process_thread_checker_.DetachFromThread(); | 357 module_process_thread_checker_.DetachFromThread(); |
| 312 } | 358 } |
| 313 | 359 |
| 314 void ViEEncoder::DeRegisterProcessThread() { | 360 void ViEEncoder::DeRegisterProcessThread() { |
| 361 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 315 module_process_thread_->DeRegisterModule(&video_sender_); | 362 module_process_thread_->DeRegisterModule(&video_sender_); |
| 316 } | 363 } |
| 317 | 364 |
| 365 void ViEEncoder::SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { |
| 366 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 367 source_proxy_->SetSource(source); |
| 368 } |
| 369 |
| 318 void ViEEncoder::SetSink(EncodedImageCallback* sink) { | 370 void ViEEncoder::SetSink(EncodedImageCallback* sink) { |
| 319 encoder_queue_.PostTask([this, sink] { | 371 encoder_queue_.PostTask([this, sink] { |
| 320 RTC_DCHECK_RUN_ON(&encoder_queue_); | 372 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 321 sink_ = sink; | 373 sink_ = sink; |
| 322 }); | 374 }); |
| 323 } | 375 } |
| 324 | 376 |
| 325 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { | 377 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { |
| 326 encoder_queue_.PostTask([this, start_bitrate_bps] { | 378 encoder_queue_.PostTask([this, start_bitrate_bps] { |
| 327 RTC_DCHECK_RUN_ON(&encoder_queue_); | 379 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 content_type = VideoEncoderConfig::ContentType::kScreen; | 429 content_type = VideoEncoderConfig::ContentType::kScreen; |
| 378 break; | 430 break; |
| 379 default: | 431 default: |
| 380 RTC_NOTREACHED(); | 432 RTC_NOTREACHED(); |
| 381 break; | 433 break; |
| 382 } | 434 } |
| 383 stats_proxy_->SetContentType(content_type); | 435 stats_proxy_->SetContentType(content_type); |
| 384 } | 436 } |
| 385 } | 437 } |
| 386 | 438 |
| 387 void ViEEncoder::IncomingCapturedFrame(const VideoFrame& video_frame) { | 439 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
| 388 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 440 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
| 389 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | 441 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); |
| 390 | 442 |
| 391 VideoFrame incoming_frame = video_frame; | 443 VideoFrame incoming_frame = video_frame; |
| 392 | 444 |
| 393 // Local time in webrtc time base. | 445 // Local time in webrtc time base. |
| 394 int64_t current_time = clock_->TimeInMilliseconds(); | 446 int64_t current_time = clock_->TimeInMilliseconds(); |
| 395 incoming_frame.set_render_time_ms(current_time); | 447 incoming_frame.set_render_time_ms(current_time); |
| 396 | 448 |
| 397 // Capture time may come from clock with an offset and drift from clock_. | 449 // 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... |
| 619 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); | 671 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); |
| 620 } | 672 } |
| 621 | 673 |
| 622 void ViEEncoder::NormalUsage() { | 674 void ViEEncoder::NormalUsage() { |
| 623 RTC_DCHECK_RUN_ON(&encoder_queue_); | 675 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 624 if (load_observer_) | 676 if (load_observer_) |
| 625 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); | 677 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); |
| 626 } | 678 } |
| 627 | 679 |
| 628 } // namespace webrtc | 680 } // namespace webrtc |
| OLD | NEW |