OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cast/sender/h264_vt_encoder.h" | 5 #include "media/cast/sender/h264_vt_encoder.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/big_endian.h" | 10 #include "base/big_endian.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/power_monitor/power_monitor.h" |
16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
17 #include "media/base/mac/corevideo_glue.h" | 18 #include "media/base/mac/corevideo_glue.h" |
18 #include "media/base/mac/video_frame_mac.h" | 19 #include "media/base/mac/video_frame_mac.h" |
19 #include "media/cast/sender/video_frame_factory.h" | 20 #include "media/cast/sender/video_frame_factory.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 namespace cast { | 23 namespace cast { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 } | 294 } |
294 | 295 |
295 private: | 296 private: |
296 ~Proxy() override {} | 297 ~Proxy() override {} |
297 | 298 |
298 const scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; | 299 const scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; |
299 | 300 |
300 DISALLOW_COPY_AND_ASSIGN(Proxy); | 301 DISALLOW_COPY_AND_ASSIGN(Proxy); |
301 }; | 302 }; |
302 | 303 |
| 304 H264VideoToolboxEncoder::PowerObserver::PowerObserver( |
| 305 H264VideoToolboxEncoder* encoder) |
| 306 : encoder_(encoder) { |
| 307 DCHECK(encoder_); |
| 308 auto power_monitor = base::PowerMonitor::Get(); |
| 309 if (!power_monitor) { |
| 310 DLOG(WARNING) << "No power monitor. Encoder may fail if power state change " |
| 311 "goes undetected."; |
| 312 return; |
| 313 } |
| 314 power_monitor->AddObserver(this); |
| 315 VLOG(1) << "Registered for power state changes."; |
| 316 } |
| 317 |
| 318 H264VideoToolboxEncoder::PowerObserver::~PowerObserver() { |
| 319 auto power_monitor = base::PowerMonitor::Get(); |
| 320 if (power_monitor) |
| 321 power_monitor->RemoveObserver(this); |
| 322 } |
| 323 |
| 324 void H264VideoToolboxEncoder::PowerObserver::OnSuspend() { |
| 325 VLOG(1) |
| 326 << "OnSuspend: Emitting all frames and destroying compression session."; |
| 327 encoder_->EmitFrames(); |
| 328 encoder_->DestroyCompressionSession(); |
| 329 } |
| 330 |
| 331 void H264VideoToolboxEncoder::PowerObserver::OnResume() { |
| 332 VLOG(1) << "OnResume: Resetting compression session."; |
| 333 encoder_->ResetCompressionSession(); |
| 334 } |
| 335 |
303 // static | 336 // static |
304 bool H264VideoToolboxEncoder::IsSupported( | 337 bool H264VideoToolboxEncoder::IsSupported( |
305 const VideoSenderConfig& video_config) { | 338 const VideoSenderConfig& video_config) { |
306 return video_config.codec == CODEC_VIDEO_H264 && VideoToolboxGlue::Get(); | 339 return video_config.codec == CODEC_VIDEO_H264 && VideoToolboxGlue::Get(); |
307 } | 340 } |
308 | 341 |
309 H264VideoToolboxEncoder::H264VideoToolboxEncoder( | 342 H264VideoToolboxEncoder::H264VideoToolboxEncoder( |
310 const scoped_refptr<CastEnvironment>& cast_environment, | 343 const scoped_refptr<CastEnvironment>& cast_environment, |
311 const VideoSenderConfig& video_config, | 344 const VideoSenderConfig& video_config, |
312 const StatusChangeCallback& status_change_cb) | 345 const StatusChangeCallback& status_change_cb) |
313 : cast_environment_(cast_environment), | 346 : cast_environment_(cast_environment), |
314 videotoolbox_glue_(VideoToolboxGlue::Get()), | 347 videotoolbox_glue_(VideoToolboxGlue::Get()), |
315 video_config_(video_config), | 348 video_config_(video_config), |
316 status_change_cb_(status_change_cb), | 349 status_change_cb_(status_change_cb), |
| 350 power_observer_(this), |
317 last_frame_id_(kStartFrameId), | 351 last_frame_id_(kStartFrameId), |
318 encode_next_frame_as_keyframe_(false), | 352 encode_next_frame_as_keyframe_(false), |
319 weak_factory_(this) { | 353 weak_factory_(this) { |
320 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 354 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
321 DCHECK(!status_change_cb_.is_null()); | 355 DCHECK(!status_change_cb_.is_null()); |
322 | 356 |
323 OperationalStatus operational_status = | 357 OperationalStatus operational_status = |
324 H264VideoToolboxEncoder::IsSupported(video_config) | 358 H264VideoToolboxEncoder::IsSupported(video_config) |
325 ? STATUS_INITIALIZED | 359 ? STATUS_INITIALIZED |
326 : STATUS_UNSUPPORTED_CODEC; | 360 : STATUS_UNSUPPORTED_CODEC; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); | 731 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); |
698 | 732 |
699 encoder->cast_environment_->PostTask( | 733 encoder->cast_environment_->PostTask( |
700 CastEnvironment::MAIN, FROM_HERE, | 734 CastEnvironment::MAIN, FROM_HERE, |
701 base::Bind(request->frame_encoded_callback, | 735 base::Bind(request->frame_encoded_callback, |
702 base::Passed(&encoded_frame))); | 736 base::Passed(&encoded_frame))); |
703 } | 737 } |
704 | 738 |
705 } // namespace cast | 739 } // namespace cast |
706 } // namespace media | 740 } // namespace media |
OLD | NEW |