Merge pull request #16254 from kencooke/macos-appnap-disabler

BUGZ-1117: On Mac OS, disable App Nap to prevent audio glitches
This commit is contained in:
Maia Hansen 2019-10-01 10:53:03 -07:00 committed by GitHub
commit 220123ff3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 2 deletions

View file

@ -70,12 +70,16 @@ file(GLOB_RECURSE INTERFACE_SRCS "src/*.cpp" "src/*.h")
GroupSources("src")
list(APPEND INTERFACE_SRCS ${RESOURCES_RCC})
# grab the Objective-C sources on OS X
if (APPLE)
file(GLOB_RECURSE INTERFACE_OBJCPP_SRCS "src/*.m" "src/*.mm")
list(APPEND INTERFACE_SRCS ${INTERFACE_OBJCPP_SRCS})
endif ()
# Add SpeechRecognizer if on Windows or OS X, otherwise remove
if (WIN32)
# Use .cpp and .h files as is.
elseif (APPLE)
file(GLOB INTERFACE_OBJCPP_SRCS "src/SpeechRecognizer.mm")
set(INTERFACE_SRCS ${INTERFACE_SRCS} ${INTERFACE_OBJCPP_SRCS})
get_filename_component(SPEECHRECOGNIZER_CPP "src/SpeechRecognizer.cpp" ABSOLUTE)
list(REMOVE_ITEM INTERFACE_SRCS ${SPEECHRECOGNIZER_CPP})
else ()

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,28 @@
//
// 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
//
#include <QtGlobal>
#ifdef Q_OS_MAC
#include "AppNapDisabler.h"
#import <AppKit/AppKit.h>
AppNapDisabler::AppNapDisabler() {
_activity = [[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityUserInitiated 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")