1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AudioSession.h"
28
29#if USE(AUDIO_SESSION) && PLATFORM(MAC)
30
31#include "FloatConversion.h"
32#include "Logging.h"
33#include "NotImplemented.h"
34#include <CoreAudio/AudioHardware.h>
35
36namespace WebCore {
37
38static AudioDeviceID defaultDevice()
39{
40    AudioDeviceID deviceID = kAudioDeviceUnknown;
41    UInt32 infoSize = sizeof(deviceID);
42
43    AudioObjectPropertyAddress defaultOutputDeviceAddress = {
44        kAudioHardwarePropertyDefaultOutputDevice,
45        kAudioObjectPropertyScopeGlobal,
46        kAudioObjectPropertyElementMaster };
47    OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultOutputDeviceAddress, 0, 0, &infoSize, (void*)&deviceID);
48    if (result)
49        return 0; // error
50    return deviceID;
51}
52
53class AudioSessionPrivate {
54public:
55};
56
57AudioSession::AudioSession()
58    : m_private(std::make_unique<AudioSessionPrivate>())
59{
60}
61
62AudioSession::~AudioSession()
63{
64}
65
66AudioSession::CategoryType AudioSession::category() const
67{
68    notImplemented();
69    return None;
70}
71
72void AudioSession::setCategory(CategoryType)
73{
74    notImplemented();
75}
76
77AudioSession::CategoryType AudioSession::categoryOverride() const
78{
79    notImplemented();
80    return None;
81}
82
83void AudioSession::setCategoryOverride(CategoryType)
84{
85    notImplemented();
86}
87
88float AudioSession::sampleRate() const
89{
90    Float64 nominalSampleRate;
91    UInt32 nominalSampleRateSize = sizeof(Float64);
92
93    AudioObjectPropertyAddress nominalSampleRateAddress = {
94        kAudioDevicePropertyNominalSampleRate,
95        kAudioObjectPropertyScopeGlobal,
96        kAudioObjectPropertyElementMaster };
97    OSStatus result = AudioObjectGetPropertyData(defaultDevice(), &nominalSampleRateAddress, 0, 0, &nominalSampleRateSize, (void*)&nominalSampleRate);
98    if (result)
99        return 0;
100
101    return narrowPrecisionToFloat(nominalSampleRate);
102}
103
104size_t AudioSession::numberOfOutputChannels() const
105{
106    notImplemented();
107    return 0;
108}
109
110void AudioSession::setActive(bool)
111{
112    notImplemented();
113}
114
115size_t AudioSession::preferredBufferSize() const
116{
117    UInt32 bufferSize;
118    UInt32 bufferSizeSize = sizeof(bufferSize);
119
120    AudioObjectPropertyAddress preferredBufferSizeAddress = {
121        kAudioDevicePropertyBufferFrameSizeRange,
122        kAudioObjectPropertyScopeGlobal,
123        kAudioObjectPropertyElementMaster };
124    OSStatus result = AudioObjectGetPropertyData(defaultDevice(), &preferredBufferSizeAddress, 0, 0, &bufferSizeSize, &bufferSize);
125
126    if (result)
127        return 0;
128    return bufferSize;
129}
130
131void AudioSession::setPreferredBufferSize(size_t bufferSize)
132{
133    AudioValueRange bufferSizeRange = {0, 0};
134    UInt32 bufferSizeRangeSize = sizeof(AudioValueRange);
135    AudioObjectPropertyAddress bufferSizeRangeAddress = {
136        kAudioDevicePropertyBufferFrameSizeRange,
137        kAudioObjectPropertyScopeGlobal,
138        kAudioObjectPropertyElementMaster
139    };
140    OSStatus result = AudioObjectGetPropertyData(defaultDevice(), &bufferSizeRangeAddress, 0, 0, &bufferSizeRangeSize, &bufferSizeRange);
141    if (result)
142        return;
143
144    size_t minBufferSize = static_cast<size_t>(bufferSizeRange.mMinimum);
145    size_t maxBufferSize = static_cast<size_t>(bufferSizeRange.mMaximum);
146    UInt32 bufferSizeOut = std::min(maxBufferSize, std::max(minBufferSize, bufferSize));
147
148    AudioObjectPropertyAddress preferredBufferSizeAddress = {
149        kAudioDevicePropertyBufferFrameSize,
150        kAudioObjectPropertyScopeGlobal,
151        kAudioObjectPropertyElementMaster };
152
153    result = AudioObjectSetPropertyData(defaultDevice(), &preferredBufferSizeAddress, 0, 0, sizeof(bufferSizeOut), (void*)&bufferSizeOut);
154
155#if LOG_DISABLED
156    UNUSED_PARAM(result);
157#else
158    if (result)
159        LOG(Media, "AudioSession::setPreferredBufferSize(%zu) - failed with error %d", bufferSize, static_cast<int>(result));
160    else
161        LOG(Media, "AudioSession::setPreferredBufferSize(%zu)", bufferSize);
162#endif
163}
164
165}
166
167#endif // USE(AUDIO_SESSION) && PLATFORM(MAC)
168