Index: remoting/host/capturer_mac.cc |
diff --git a/remoting/host/capturer_mac.cc b/remoting/host/capturer_mac.cc |
index 42b362afe3629843b6030a0a956235d9a9931d74..d24932c091eee260ebef26ca2ed80e73b88f1b34 100644 |
--- a/remoting/host/capturer_mac.cc |
+++ b/remoting/host/capturer_mac.cc |
@@ -6,6 +6,7 @@ |
#include <ApplicationServices/ApplicationServices.h> |
#include <dlfcn.h> |
+#include <IOKit/pwr_mgt/IOPMLib.h> |
#include <OpenGL/CGLMacro.h> |
#include <OpenGL/OpenGL.h> |
#include <stddef.h> |
@@ -15,6 +16,8 @@ |
#include "base/mac/scoped_cftyperef.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/synchronization/waitable_event.h" |
+#include "base/time.h" |
+#include "base/timer.h" |
#include "remoting/base/util.h" |
#include "remoting/host/capturer_helper.h" |
@@ -23,6 +26,8 @@ namespace remoting { |
namespace { |
+const int64 kPowerAssertionTimeoutMs = 5 * 1000; // 5 seconds |
+ |
SkIRect CGRectToSkIRect(const CGRect& rect) { |
SkIRect sk_rect = { |
SkScalarRound(rect.origin.x), |
@@ -192,6 +197,9 @@ class CapturerMac : public Capturer { |
void *user_parameter); |
void ReleaseBuffers(); |
+ void RefreshPowerAssertion(); |
+ void ReleasePowerAssertion(); |
+ |
CGLContextObj cgl_context_; |
static const int kNumBuffers = 2; |
scoped_pixel_buffer_object pixel_buffer_object_; |
@@ -221,6 +229,12 @@ class CapturerMac : public Capturer { |
// Will be non-null on lion. |
CGDisplayCreateImageFunc display_create_image_func_; |
+ // Power management assertion to prevent the screen from sleeping. |
+ IOPMAssertionID power_assertion_id_; |
+ |
+ // Timer to remove the power management assertion on inactivity |
+ base::OneShotTimer<CapturerMac> power_assertion_timer_; |
+ |
DISALLOW_COPY_AND_ASSIGN(CapturerMac); |
}; |
@@ -230,7 +244,8 @@ CapturerMac::CapturerMac() |
last_buffer_(NULL), |
pixel_format_(media::VideoFrame::RGB32), |
display_configuration_capture_event_(false, true), |
- display_create_image_func_(NULL) { |
+ display_create_image_func_(NULL), |
+ power_assertion_id_(kIOPMNullAssertionID) { |
} |
CapturerMac::~CapturerMac() { |
@@ -242,6 +257,7 @@ CapturerMac::~CapturerMac() { |
if (err != kCGErrorSuccess) { |
LOG(ERROR) << "CGDisplayRemoveReconfigurationCallback " << err; |
} |
+ ReleasePowerAssertion(); |
} |
bool CapturerMac::Init() { |
@@ -360,6 +376,8 @@ void CapturerMac::CaptureInvalidRegion( |
// Only allow captures when the display configuration is not occurring. |
scoped_refptr<CaptureData> data; |
+ RefreshPowerAssertion(); |
+ |
// Critical section shared with DisplaysReconfigured(...). |
CHECK(display_configuration_capture_event_.TimedWait( |
base::TimeDelta::FromSeconds(kDisplayReconfigurationTimeoutInSeconds))); |
@@ -626,6 +644,36 @@ void CapturerMac::DisplaysReconfiguredCallback( |
capturer->DisplaysReconfigured(display, flags); |
} |
+// Creates or refreshes a power management assertion to prevent the display from |
+// going to sleep. |
+void CapturerMac::RefreshPowerAssertion() { |
+ if (power_assertion_timer_.IsRunning()) { |
+ DCHECK(power_assertion_id_ != kIOPMNullAssertionID); |
+ power_assertion_timer_.Reset(); |
+ return; |
+ } |
+ |
+ IOReturn result = IOPMAssertionCreate(kIOPMAssertionTypeNoDisplaySleep, |
+ kIOPMAssertionLevelOn, |
+ &power_assertion_id_); |
+ |
+ if (result == kIOReturnSuccess) { |
+ power_assertion_timer_.Start(FROM_HERE, |
+ base::TimeDelta::FromMilliseconds( |
+ kPowerAssertionTimeoutMs), |
+ this, |
+ &CapturerMac::ReleasePowerAssertion); |
+ } |
+} |
+ |
+void CapturerMac::ReleasePowerAssertion() { |
+ if (power_assertion_id_ != kIOPMNullAssertionID) { |
+ IOPMAssertionRelease(power_assertion_id_); |
+ power_assertion_id_ = kIOPMNullAssertionID; |
+ power_assertion_timer_.Stop(); |
+ } |
+} |
+ |
} // namespace |
// static |