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