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 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 : STATUS_UNSUPPORTED_CODEC; | 327 : STATUS_UNSUPPORTED_CODEC; |
327 cast_environment_->PostTask( | 328 cast_environment_->PostTask( |
328 CastEnvironment::MAIN, FROM_HERE, | 329 CastEnvironment::MAIN, FROM_HERE, |
329 base::Bind(status_change_cb_, operational_status)); | 330 base::Bind(status_change_cb_, operational_status)); |
330 | 331 |
331 if (operational_status == STATUS_INITIALIZED) { | 332 if (operational_status == STATUS_INITIALIZED) { |
332 video_frame_factory_ = | 333 video_frame_factory_ = |
333 scoped_refptr<VideoFrameFactoryImpl>(new VideoFrameFactoryImpl( | 334 scoped_refptr<VideoFrameFactoryImpl>(new VideoFrameFactoryImpl( |
334 weak_factory_.GetWeakPtr(), cast_environment_)); | 335 weak_factory_.GetWeakPtr(), cast_environment_)); |
335 } | 336 } |
337 | |
338 // Register for power state changes. | |
339 auto power_monitor = base::PowerMonitor::Get(); | |
340 if (power_monitor) { | |
341 power_monitor->AddObserver(this); | |
342 VLOG(1) << "Registered for power state changes."; | |
343 } else { | |
344 DLOG(WARNING) << "No power monitor. Encoder may fail if power state change " | |
345 "goes undetected."; | |
346 } | |
336 } | 347 } |
337 | 348 |
338 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { | 349 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { |
339 DestroyCompressionSession(); | 350 DestroyCompressionSession(); |
351 auto power_monitor = base::PowerMonitor::Get(); | |
352 if (power_monitor) | |
353 power_monitor->RemoveObserver(this); | |
340 } | 354 } |
341 | 355 |
342 void H264VideoToolboxEncoder::ResetCompressionSession() { | 356 void H264VideoToolboxEncoder::ResetCompressionSession() { |
343 DCHECK(thread_checker_.CalledOnValidThread()); | 357 DCHECK(thread_checker_.CalledOnValidThread()); |
344 | 358 |
345 // Notify that we're resetting the encoder. | 359 // Notify that we're resetting the encoder. |
346 cast_environment_->PostTask( | 360 cast_environment_->PostTask( |
347 CastEnvironment::MAIN, FROM_HERE, | 361 CastEnvironment::MAIN, FROM_HERE, |
348 base::Bind(status_change_cb_, STATUS_CODEC_REINIT_PENDING)); | 362 base::Bind(status_change_cb_, STATUS_CODEC_REINIT_PENDING)); |
349 | 363 |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 if (!compression_session_) | 616 if (!compression_session_) |
603 return; | 617 return; |
604 | 618 |
605 OSStatus status = videotoolbox_glue_->VTCompressionSessionCompleteFrames( | 619 OSStatus status = videotoolbox_glue_->VTCompressionSessionCompleteFrames( |
606 compression_session_, CoreMediaGlue::CMTime{0, 0, 0, 0}); | 620 compression_session_, CoreMediaGlue::CMTime{0, 0, 0, 0}); |
607 if (status != noErr) { | 621 if (status != noErr) { |
608 DLOG(ERROR) << " VTCompressionSessionCompleteFrames failed: " << status; | 622 DLOG(ERROR) << " VTCompressionSessionCompleteFrames failed: " << status; |
609 } | 623 } |
610 } | 624 } |
611 | 625 |
626 void H264VideoToolboxEncoder::OnSuspend() { | |
627 VLOG(1) | |
628 << "OnSuspend: Emitting all frames and destroying compression session."; | |
629 EmitFrames(); | |
630 DestroyCompressionSession(); | |
631 } | |
632 | |
633 void H264VideoToolboxEncoder::OnResume() { | |
634 VLOG(1) << "OnResume: Resetting compression session."; | |
635 ResetCompressionSession(); | |
miu
2015/04/22 18:18:47
I just realized: You shouldn't call ResetCompressi
jfroy
2015/04/22 18:24:36
Thanks for pointing this out. I'll update the code
| |
636 } | |
637 | |
612 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, | 638 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, |
613 int32_t value) { | 639 int32_t value) { |
614 base::ScopedCFTypeRef<CFNumberRef> cfvalue( | 640 base::ScopedCFTypeRef<CFNumberRef> cfvalue( |
615 CFNumberCreate(nullptr, kCFNumberSInt32Type, &value)); | 641 CFNumberCreate(nullptr, kCFNumberSInt32Type, &value)); |
616 return videotoolbox_glue_->VTSessionSetProperty(compression_session_, key, | 642 return videotoolbox_glue_->VTSessionSetProperty(compression_session_, key, |
617 cfvalue) == noErr; | 643 cfvalue) == noErr; |
618 } | 644 } |
619 | 645 |
620 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, bool value) { | 646 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, bool value) { |
621 CFBooleanRef cfvalue = (value) ? kCFBooleanTrue : kCFBooleanFalse; | 647 CFBooleanRef cfvalue = (value) ? kCFBooleanTrue : kCFBooleanFalse; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
691 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); | 717 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); |
692 | 718 |
693 encoder->cast_environment_->PostTask( | 719 encoder->cast_environment_->PostTask( |
694 CastEnvironment::MAIN, FROM_HERE, | 720 CastEnvironment::MAIN, FROM_HERE, |
695 base::Bind(request->frame_encoded_callback, | 721 base::Bind(request->frame_encoded_callback, |
696 base::Passed(&encoded_frame))); | 722 base::Passed(&encoded_frame))); |
697 } | 723 } |
698 | 724 |
699 } // namespace cast | 725 } // namespace cast |
700 } // namespace media | 726 } // namespace media |
OLD | NEW |