On Mac OS, disable App Nap to prevent audio glitches while running in the background

This commit is contained in:
Ken Cooke 2019-09-26 08:52:35 -07:00
parent 1e94a82ee6
commit ae62b96f67
3 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,24 @@
//
// AppNapDisabler.h
// interface/src
//
// Copyright 2019 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_AppNapDisabler_h
#define hifi_AppNapDisabler_h
#import <objc/objc-runtime.h>
class AppNapDisabler {
public:
AppNapDisabler();
~AppNapDisabler();
private:
id _activity;
};
#endif // hifi_AppNapDisabler_h

View file

@ -0,0 +1,27 @@
//
// AppNapDisabler.mm
// interface/src
//
// Copyright 2019 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifdef Q_OS_MAC
#include "AppNapDisabler.h"
#import <AppKit/AppKit.h>
AppNapDisabler::AppNapDisabler() {
_activity = [[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityBackground reason:@"Audio is in use"];
[_activity retain];
}
AppNapDisabler::~AppNapDisabler() {
[[NSProcessInfo processInfo] endActivity:_activity];
[_activity release];
}
#endif // Q_OS_MAC

View file

@ -265,6 +265,12 @@ extern "C" {
#include "AndroidHelper.h"
#endif
#if defined(Q_OS_MAC)
// On Mac OS, disable App Nap to prevent audio glitches while running in the background
#include "AppNapDisabler.h"
static AppNapDisabler appNapDisabler; // disabled, while in scope
#endif
#include "graphics/RenderEventHandler.h"
Q_LOGGING_CATEGORY(trace_app_input_mouse, "trace.app.input.mouse")