Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Unified Diff: media/cast/sender/h264_vt_encoder.cc

Issue 1094403002: Add power monitoring to the Cast VideoToolbox encoder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase on latest version of 1094403002. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/cast/sender/h264_vt_encoder.h ('k') | media/cast/sender/h264_vt_encoder_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/cast/sender/h264_vt_encoder.cc
diff --git a/media/cast/sender/h264_vt_encoder.cc b/media/cast/sender/h264_vt_encoder.cc
index e109bcfca57f930c57ef0edaf07b61a25b58de0a..38d671b620e9a2a83db0a7ed5d247cf55432a862 100644
--- a/media/cast/sender/h264_vt_encoder.cc
+++ b/media/cast/sender/h264_vt_encoder.cc
@@ -13,6 +13,7 @@
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
+#include "base/power_monitor/power_monitor.h"
#include "base/synchronization/lock.h"
#include "media/base/mac/corevideo_glue.h"
#include "media/base/mac/video_frame_mac.h"
@@ -321,6 +322,7 @@ H264VideoToolboxEncoder::H264VideoToolboxEncoder(
status_change_cb_(status_change_cb),
last_frame_id_(kStartFrameId),
encode_next_frame_as_keyframe_(false),
+ power_suspended_(false),
weak_factory_(this) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
DCHECK(!status_change_cb_.is_null());
@@ -334,19 +336,44 @@ H264VideoToolboxEncoder::H264VideoToolboxEncoder(
base::Bind(status_change_cb_, operational_status));
if (operational_status == STATUS_INITIALIZED) {
+ // Create the shared video frame factory. It persists for the combined
+ // lifetime of the encoder and all video frame factory proxies created by
+ // |CreateVideoFrameFactory| that reference it.
video_frame_factory_ =
scoped_refptr<VideoFrameFactoryImpl>(new VideoFrameFactoryImpl(
weak_factory_.GetWeakPtr(), cast_environment_));
+
+ // Register for power state changes.
+ auto power_monitor = base::PowerMonitor::Get();
+ if (power_monitor) {
+ power_monitor->AddObserver(this);
+ VLOG(1) << "Registered for power state changes.";
+ } else {
+ DLOG(WARNING) << "No power monitor. Process suspension will invalidate "
+ "the encoder.";
+ }
}
}
H264VideoToolboxEncoder::~H264VideoToolboxEncoder() {
DestroyCompressionSession();
+
+ // If video_frame_factory_ is not null, the encoder registered for power state
+ // changes in the ctor and it must now unregister.
+ if (video_frame_factory_) {
+ auto power_monitor = base::PowerMonitor::Get();
+ if (power_monitor)
+ power_monitor->RemoveObserver(this);
+ }
}
void H264VideoToolboxEncoder::ResetCompressionSession() {
DCHECK(thread_checker_.CalledOnValidThread());
+ // Ignore reset requests while power suspended.
+ if (power_suspended_)
+ return;
+
// Notify that we're resetting the encoder.
cast_environment_->PostTask(
CastEnvironment::MAIN, FROM_HERE,
@@ -625,6 +652,27 @@ void H264VideoToolboxEncoder::EmitFrames() {
}
}
+void H264VideoToolboxEncoder::OnSuspend() {
+ VLOG(1)
+ << "OnSuspend: Emitting all frames and destroying compression session.";
+ EmitFrames();
+ DestroyCompressionSession();
+ power_suspended_ = true;
+}
+
+void H264VideoToolboxEncoder::OnResume() {
+ power_suspended_ = false;
+
+ // Reset the compression session only if the frame size is not zero (which
+ // will obviously fail). It is possible for the frame size to be zero if no
+ // frame was submitted for encoding or requested from the video frame factory
+ // before suspension.
+ if (!frame_size_.IsEmpty()) {
+ VLOG(1) << "OnResume: Resetting compression session.";
+ ResetCompressionSession();
+ }
+}
+
bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key,
int32_t value) {
base::ScopedCFTypeRef<CFNumberRef> cfvalue(
« no previous file with comments | « media/cast/sender/h264_vt_encoder.h ('k') | media/cast/sender/h264_vt_encoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698