blob: 0fb79ebb92ea4ca6a5ba0404c5a6a2fd9e6b4bbf [file] [log] [blame]
/*
* Copyright (C) 2013-2014 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "MediaSessionManagerCocoa.h"
#if USE(AUDIO_SESSION) && PLATFORM(COCOA)
#include "AudioSession.h"
#include "DeprecatedGlobalSettings.h"
#include "HTMLMediaElement.h"
#include "Logging.h"
#include "MediaPlayer.h"
#include "MediaStrategy.h"
#include "NowPlayingInfo.h"
#include "PlatformMediaSession.h"
#include "PlatformStrategies.h"
#include <wtf/BlockObjCExceptions.h>
#include <wtf/Function.h>
#include "MediaRemoteSoftLink.h"
static const size_t kWebAudioBufferSize = 128;
static const size_t kLowPowerVideoBufferSize = 4096;
namespace WebCore {
#if PLATFORM(MAC)
std::unique_ptr<PlatformMediaSessionManager> PlatformMediaSessionManager::create()
{
return makeUnique<MediaSessionManagerCocoa>();
}
#endif // !PLATFORM(MAC)
MediaSessionManagerCocoa::MediaSessionManagerCocoa()
: m_systemSleepListener(PAL::SystemSleepListener::create(*this))
{
}
void MediaSessionManagerCocoa::updateSessionState()
{
int videoCount = count(PlatformMediaSession::MediaType::Video);
int videoAudioCount = count(PlatformMediaSession::MediaType::VideoAudio);
int audioCount = count(PlatformMediaSession::MediaType::Audio);
int webAudioCount = count(PlatformMediaSession::MediaType::WebAudio);
int captureCount = countActiveAudioCaptureSources();
ALWAYS_LOG(LOGIDENTIFIER, "types: "
"AudioCapture(", captureCount, "), "
"Video(", videoCount, "), "
"Audio(", audioCount, "), "
"VideoAudio(", videoAudioCount, "), "
"WebAudio(", webAudioCount, ")");
if (webAudioCount)
AudioSession::sharedSession().setPreferredBufferSize(kWebAudioBufferSize);
// In case of audio capture, we want to grab 20 ms chunks to limit the latency so that it is not noticeable by users
// while having a large enough buffer so that the audio rendering remains stable, hence a computation based on sample rate.
else if (captureCount)
AudioSession::sharedSession().setPreferredBufferSize(AudioSession::sharedSession().sampleRate() / 50);
else if ((videoAudioCount || audioCount) && DeprecatedGlobalSettings::lowPowerVideoAudioBufferSizeEnabled()) {
// FIXME: <http://webkit.org/b/116725> Figure out why enabling the code below
// causes media LayoutTests to fail on 10.8.
size_t bufferSize;
if (m_audioHardwareListener && m_audioHardwareListener->outputDeviceSupportsLowPowerMode())
bufferSize = kLowPowerVideoBufferSize;
else
bufferSize = kWebAudioBufferSize;
AudioSession::sharedSession().setPreferredBufferSize(bufferSize);
}
if (!DeprecatedGlobalSettings::shouldManageAudioSessionCategory())
return;
bool hasAudibleAudioOrVideoMediaType = false;
forEachSession([&hasAudibleAudioOrVideoMediaType] (auto& session) mutable {
auto type = session.mediaType();
if ((type == PlatformMediaSession::MediaType::VideoAudio || type == PlatformMediaSession::MediaType::Audio) && session.canProduceAudio() && session.hasPlayedSinceLastInterruption())
hasAudibleAudioOrVideoMediaType = true;
if (session.isPlayingToWirelessPlaybackTarget())
hasAudibleAudioOrVideoMediaType = true;
});
RouteSharingPolicy policy = RouteSharingPolicy::Default;
AudioSession::CategoryType category = AudioSession::None;
if (captureCount)
category = AudioSession::PlayAndRecord;
else if (hasAudibleAudioOrVideoMediaType) {
category = AudioSession::MediaPlayback;
policy = RouteSharingPolicy::LongFormAudio;
} else if (webAudioCount)
category = AudioSession::AmbientSound;
ALWAYS_LOG(LOGIDENTIFIER, "setting category = ", category, ", policy = ", policy);
AudioSession::sharedSession().setCategory(category, policy);
}
void MediaSessionManagerCocoa::beginInterruption(PlatformMediaSession::InterruptionType type)
{
if (type == PlatformMediaSession::InterruptionType::SystemInterruption) {
forEachSession([] (auto& session) {
session.clearHasPlayedSinceLastInterruption();
});
}
PlatformMediaSessionManager::beginInterruption(type);
}
void MediaSessionManagerCocoa::prepareToSendUserMediaPermissionRequest()
{
providePresentingApplicationPIDIfNecessary();
}
void MediaSessionManagerCocoa::scheduleUpdateNowPlayingInfo()
{
if (!m_nowPlayingUpdateTaskQueue.hasPendingTasks())
m_nowPlayingUpdateTaskQueue.enqueueTask(std::bind(&MediaSessionManagerCocoa::updateNowPlayingInfo, this));
}
bool MediaSessionManagerCocoa::sessionWillBeginPlayback(PlatformMediaSession& session)
{
if (!PlatformMediaSessionManager::sessionWillBeginPlayback(session))
return false;
scheduleUpdateNowPlayingInfo();
return true;
}
void MediaSessionManagerCocoa::sessionDidEndRemoteScrubbing(const PlatformMediaSession&)
{
scheduleUpdateNowPlayingInfo();
}
void MediaSessionManagerCocoa::addSession(PlatformMediaSession& session)
{
if (!m_remoteCommandListener)
m_remoteCommandListener = RemoteCommandListener::create(*this);
if (!m_audioHardwareListener)
m_audioHardwareListener = AudioHardwareListener::create(*this);
PlatformMediaSessionManager::addSession(session);
}
void MediaSessionManagerCocoa::removeSession(PlatformMediaSession& session)
{
PlatformMediaSessionManager::removeSession(session);
if (hasNoSession()) {
m_remoteCommandListener = nullptr;
m_audioHardwareListener = nullptr;
}
scheduleUpdateNowPlayingInfo();
}
void MediaSessionManagerCocoa::setCurrentSession(PlatformMediaSession& session)
{
PlatformMediaSessionManager::setCurrentSession(session);
if (m_remoteCommandListener)
m_remoteCommandListener->updateSupportedCommands();
}
void MediaSessionManagerCocoa::sessionWillEndPlayback(PlatformMediaSession& session, DelayCallingUpdateNowPlaying delayCallingUpdateNowPlaying)
{
PlatformMediaSessionManager::sessionWillEndPlayback(session, delayCallingUpdateNowPlaying);
if (delayCallingUpdateNowPlaying == DelayCallingUpdateNowPlaying::No) {
updateNowPlayingInfo();
return;
}
scheduleUpdateNowPlayingInfo();
}
void MediaSessionManagerCocoa::clientCharacteristicsChanged(PlatformMediaSession& session)
{
ALWAYS_LOG(LOGIDENTIFIER, session.logIdentifier());
scheduleUpdateNowPlayingInfo();
}
void MediaSessionManagerCocoa::sessionCanProduceAudioChanged()
{
PlatformMediaSessionManager::sessionCanProduceAudioChanged();
scheduleUpdateNowPlayingInfo();
}
void MediaSessionManagerCocoa::clearNowPlayingInfo()
{
if (canLoad_MediaRemote_MRMediaRemoteSetNowPlayingVisibility())
MRMediaRemoteSetNowPlayingVisibility(MRMediaRemoteGetLocalOrigin(), MRNowPlayingClientVisibilityNeverVisible);
MRMediaRemoteSetCanBeNowPlayingApplication(false);
MRMediaRemoteSetNowPlayingInfo(nullptr);
MRMediaRemoteSetNowPlayingApplicationPlaybackStateForOrigin(MRMediaRemoteGetLocalOrigin(), kMRPlaybackStateStopped, dispatch_get_main_queue(), ^(MRMediaRemoteError error) {
#if LOG_DISABLED
UNUSED_PARAM(error);
#else
if (error)
WTFLogAlways("MRMediaRemoteSetNowPlayingApplicationPlaybackStateForOrigin(stopped) failed with error %d", error);
#endif
});
}
void MediaSessionManagerCocoa::setNowPlayingInfo(bool setAsNowPlayingApplication, const NowPlayingInfo& nowPlayingInfo)
{
if (setAsNowPlayingApplication)
MRMediaRemoteSetCanBeNowPlayingApplication(true);
auto info = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
if (!nowPlayingInfo.title.isEmpty())
CFDictionarySetValue(info.get(), kMRMediaRemoteNowPlayingInfoTitle, nowPlayingInfo.title.createCFString().get());
if (std::isfinite(nowPlayingInfo.duration) && nowPlayingInfo.duration != MediaPlayer::invalidTime()) {
auto cfDuration = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &nowPlayingInfo.duration));
CFDictionarySetValue(info.get(), kMRMediaRemoteNowPlayingInfoDuration, cfDuration.get());
}
double rate = nowPlayingInfo.isPlaying ? 1 : 0;
auto cfRate = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &rate));
CFDictionarySetValue(info.get(), kMRMediaRemoteNowPlayingInfoPlaybackRate, cfRate.get());
auto lastUpdatedNowPlayingInfoUniqueIdentifier = nowPlayingInfo.uniqueIdentifier.toUInt64();
auto cfIdentifier = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberLongLongType, &lastUpdatedNowPlayingInfoUniqueIdentifier));
CFDictionarySetValue(info.get(), kMRMediaRemoteNowPlayingInfoUniqueIdentifier, cfIdentifier.get());
if (std::isfinite(nowPlayingInfo.currentTime) && nowPlayingInfo.currentTime != MediaPlayer::invalidTime() && nowPlayingInfo.supportsSeeking) {
auto cfCurrentTime = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &nowPlayingInfo.currentTime));
CFDictionarySetValue(info.get(), kMRMediaRemoteNowPlayingInfoElapsedTime, cfCurrentTime.get());
}
if (canLoad_MediaRemote_MRMediaRemoteSetParentApplication() && !nowPlayingInfo.sourceApplicationIdentifier.isEmpty())
MRMediaRemoteSetParentApplication(MRMediaRemoteGetLocalOrigin(), nowPlayingInfo.sourceApplicationIdentifier.createCFString().get());
MRPlaybackState playbackState = (nowPlayingInfo.isPlaying) ? kMRPlaybackStatePlaying : kMRPlaybackStatePaused;
MRMediaRemoteSetNowPlayingApplicationPlaybackStateForOrigin(MRMediaRemoteGetLocalOrigin(), playbackState, dispatch_get_main_queue(), ^(MRMediaRemoteError error) {
#if LOG_DISABLED
UNUSED_PARAM(error);
#else
WTFLogAlways("MRMediaRemoteSetNowPlayingApplicationPlaybackStateForOrigin(playing) failed with error %d", error);
#endif
});
MRMediaRemoteSetNowPlayingInfo(info.get());
if (canLoad_MediaRemote_MRMediaRemoteSetNowPlayingVisibility()) {
MRNowPlayingClientVisibility visibility = nowPlayingInfo.allowsNowPlayingControlsVisibility ? MRNowPlayingClientVisibilityAlwaysVisible : MRNowPlayingClientVisibilityNeverVisible;
MRMediaRemoteSetNowPlayingVisibility(MRMediaRemoteGetLocalOrigin(), visibility);
}
}
PlatformMediaSession* MediaSessionManagerCocoa::nowPlayingEligibleSession()
{
// FIXME: Fix this layering violation.
if (auto element = HTMLMediaElement::bestMediaElementForShowingPlaybackControlsManager(MediaElementSession::PlaybackControlsPurpose::NowPlaying))
return &element->mediaSession();
return nullptr;
}
void MediaSessionManagerCocoa::updateNowPlayingInfo()
{
#if USE(MEDIAREMOTE)
if (!isMediaRemoteFrameworkAvailable())
return;
BEGIN_BLOCK_OBJC_EXCEPTIONS
Optional<NowPlayingInfo> nowPlayingInfo;
if (auto* session = nowPlayingEligibleSession())
nowPlayingInfo = session->nowPlayingInfo();
if (!nowPlayingInfo) {
ALWAYS_LOG(LOGIDENTIFIER, "clearing now playing info");
platformStrategies()->mediaStrategy().clearNowPlayingInfo();
m_registeredAsNowPlayingApplication = false;
m_nowPlayingActive = false;
m_lastUpdatedNowPlayingTitle = emptyString();
m_lastUpdatedNowPlayingDuration = NAN;
m_lastUpdatedNowPlayingElapsedTime = NAN;
m_lastUpdatedNowPlayingInfoUniqueIdentifier = { };
return;
}
ALWAYS_LOG(LOGIDENTIFIER, "title = \"", nowPlayingInfo->title, "\", isPlaying = ", nowPlayingInfo->isPlaying, ", duration = ", nowPlayingInfo->duration, ", now = ", nowPlayingInfo->currentTime, ", id = ", nowPlayingInfo->uniqueIdentifier.toUInt64(), ", registered = ", m_registeredAsNowPlayingApplication);
platformStrategies()->mediaStrategy().setNowPlayingInfo(!m_registeredAsNowPlayingApplication, *nowPlayingInfo);
if (!m_registeredAsNowPlayingApplication) {
m_registeredAsNowPlayingApplication = true;
providePresentingApplicationPIDIfNecessary();
}
if (!nowPlayingInfo->title.isEmpty())
m_lastUpdatedNowPlayingTitle = nowPlayingInfo->title;
double duration = nowPlayingInfo->duration;
if (std::isfinite(duration) && duration != MediaPlayer::invalidTime())
m_lastUpdatedNowPlayingDuration = duration;
m_lastUpdatedNowPlayingInfoUniqueIdentifier = nowPlayingInfo->uniqueIdentifier;
double currentTime = nowPlayingInfo->currentTime;
if (std::isfinite(currentTime) && currentTime != MediaPlayer::invalidTime() && nowPlayingInfo->supportsSeeking)
m_lastUpdatedNowPlayingElapsedTime = currentTime;
m_nowPlayingActive = nowPlayingInfo->allowsNowPlayingControlsVisibility;
END_BLOCK_OBJC_EXCEPTIONS
#endif // USE(MEDIAREMOTE)
}
} // namespace WebCore
#endif // USE(AUDIO_SESSION) && PLATFORM(COCOA)