MediaSessionManagerCocoa.cpp [plain text]
#include "config.h"
#include "MediaSessionManagerCocoa.h"
#if USE(AUDIO_SESSION)
#include "AudioSession.h"
#include "DeprecatedGlobalSettings.h"
#include "Logging.h"
#include <wtf/Function.h>
using namespace WebCore;
static const size_t kWebAudioBufferSize = 128;
static const size_t kLowPowerVideoBufferSize = 4096;
void MediaSessionManagerCocoa::updateSessionState()
{
LOG(Media, "PlatformMediaSessionManager::scheduleUpdateSessionState() - types: Video(%d), Audio(%d), WebAudio(%d)", count(PlatformMediaSession::Video), count(PlatformMediaSession::Audio), count(PlatformMediaSession::WebAudio));
if (has(PlatformMediaSession::WebAudio))
AudioSession::sharedSession().setPreferredBufferSize(kWebAudioBufferSize);
else if (has(PlatformMediaSession::MediaStreamCapturingAudio))
AudioSession::sharedSession().setPreferredBufferSize(AudioSession::sharedSession().sampleRate() / 50);
else if ((has(PlatformMediaSession::Video) || has(PlatformMediaSession::Audio)) && DeprecatedGlobalSettings::lowPowerVideoAudioBufferSizeEnabled()) {
size_t bufferSize;
if (audioHardwareListener() && audioHardwareListener()->outputDeviceSupportsLowPowerMode())
bufferSize = kLowPowerVideoBufferSize;
else
bufferSize = kWebAudioBufferSize;
AudioSession::sharedSession().setPreferredBufferSize(bufferSize);
}
if (!DeprecatedGlobalSettings::shouldManageAudioSessionCategory())
return;
bool hasWebAudioType = false;
bool hasAudibleAudioOrVideoMediaType = false;
bool hasAudioCapture = anyOfSessions([&hasWebAudioType, &hasAudibleAudioOrVideoMediaType] (PlatformMediaSession& session, size_t) mutable {
auto type = session.mediaType();
if (type == PlatformMediaSession::WebAudio)
hasWebAudioType = true;
if ((type == PlatformMediaSession::VideoAudio || type == PlatformMediaSession::Audio) && session.canProduceAudio() && session.hasPlayedSinceLastInterruption())
hasAudibleAudioOrVideoMediaType = true;
if (session.isPlayingToWirelessPlaybackTarget())
hasAudibleAudioOrVideoMediaType = true;
return (type == PlatformMediaSession::MediaStreamCapturingAudio);
});
if (hasAudioCapture)
AudioSession::sharedSession().setCategory(AudioSession::PlayAndRecord);
else if (hasAudibleAudioOrVideoMediaType)
AudioSession::sharedSession().setCategory(AudioSession::MediaPlayback);
else if (hasWebAudioType)
AudioSession::sharedSession().setCategory(AudioSession::AmbientSound);
else
AudioSession::sharedSession().setCategory(AudioSession::None);
}
void MediaSessionManagerCocoa::beginInterruption(PlatformMediaSession::InterruptionType type)
{
if (type == PlatformMediaSession::InterruptionType::SystemInterruption) {
forEachSession([] (PlatformMediaSession& session, size_t) {
session.clearHasPlayedSinceLastInterruption();
});
}
PlatformMediaSessionManager::beginInterruption(type);
}
#endif // USE(AUDIO_SESSION)