1 | /* |
2 | * Copyright (C) 2013-2019 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 | #pragma once |
27 | |
28 | #if USE(AUDIO_SESSION) |
29 | |
30 | #include <memory> |
31 | #include <wtf/HashSet.h> |
32 | #include <wtf/NeverDestroyed.h> |
33 | #include <wtf/Noncopyable.h> |
34 | #include <wtf/text/WTFString.h> |
35 | |
36 | namespace WebCore { |
37 | |
38 | class AudioSessionPrivate; |
39 | |
40 | enum class RouteSharingPolicy : uint8_t { |
41 | Default, |
42 | LongFormAudio, |
43 | Independent, |
44 | LongFormVideo |
45 | }; |
46 | |
47 | class AudioSession { |
48 | WTF_MAKE_NONCOPYABLE(AudioSession); |
49 | public: |
50 | WEBCORE_EXPORT static AudioSession& sharedSession(); |
51 | |
52 | enum CategoryType { |
53 | None, |
54 | AmbientSound, |
55 | SoloAmbientSound, |
56 | MediaPlayback, |
57 | RecordAudio, |
58 | PlayAndRecord, |
59 | AudioProcessing, |
60 | }; |
61 | WEBCORE_EXPORT void setCategory(CategoryType, RouteSharingPolicy); |
62 | WEBCORE_EXPORT CategoryType category() const; |
63 | |
64 | void setCategoryOverride(CategoryType); |
65 | CategoryType categoryOverride() const; |
66 | |
67 | RouteSharingPolicy routeSharingPolicy() const; |
68 | String routingContextUID() const; |
69 | |
70 | float sampleRate() const; |
71 | size_t bufferSize() const; |
72 | size_t numberOfOutputChannels() const; |
73 | |
74 | bool tryToSetActive(bool); |
75 | |
76 | WEBCORE_EXPORT size_t preferredBufferSize() const; |
77 | void setPreferredBufferSize(size_t); |
78 | |
79 | class MutedStateObserver { |
80 | public: |
81 | virtual ~MutedStateObserver() = default; |
82 | |
83 | virtual void hardwareMutedStateDidChange(AudioSession*) = 0; |
84 | }; |
85 | |
86 | void addMutedStateObserver(MutedStateObserver*); |
87 | void removeMutedStateObserver(MutedStateObserver*); |
88 | |
89 | bool isMuted() const; |
90 | void handleMutedStateChange(); |
91 | |
92 | bool isActive() const { return m_active; } |
93 | |
94 | private: |
95 | friend class NeverDestroyed<AudioSession>; |
96 | AudioSession(); |
97 | ~AudioSession(); |
98 | |
99 | bool tryToSetActiveInternal(bool); |
100 | |
101 | std::unique_ptr<AudioSessionPrivate> m_private; |
102 | HashSet<MutedStateObserver*> m_observers; |
103 | bool m_active { false }; // Used only for testing. |
104 | }; |
105 | |
106 | String convertEnumerationToString(RouteSharingPolicy); |
107 | String convertEnumerationToString(AudioSession::CategoryType); |
108 | |
109 | } // namespace WebCore |
110 | |
111 | namespace WTF { |
112 | template<> struct EnumTraits<WebCore::RouteSharingPolicy> { |
113 | using values = EnumValues< |
114 | WebCore::RouteSharingPolicy, |
115 | WebCore::RouteSharingPolicy::Default, |
116 | WebCore::RouteSharingPolicy::LongFormAudio, |
117 | WebCore::RouteSharingPolicy::Independent, |
118 | WebCore::RouteSharingPolicy::LongFormVideo |
119 | >; |
120 | }; |
121 | |
122 | template<typename Type> |
123 | struct LogArgument; |
124 | |
125 | template <> |
126 | struct LogArgument<WebCore::RouteSharingPolicy> { |
127 | static String toString(const WebCore::RouteSharingPolicy policy) |
128 | { |
129 | return convertEnumerationToString(policy); |
130 | } |
131 | }; |
132 | |
133 | template <> |
134 | struct LogArgument<WebCore::AudioSession::CategoryType> { |
135 | static String toString(const WebCore::AudioSession::CategoryType category) |
136 | { |
137 | return convertEnumerationToString(category); |
138 | } |
139 | }; |
140 | |
141 | } // namespace WTF |
142 | |
143 | #endif // USE(AUDIO_SESSION) |
144 | |