1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <sys/types.h>
20 
21 #include "AudioPort.h"
22 #include <RoutingStrategy.h>
23 #include <utils/Errors.h>
24 #include <utils/Timers.h>
25 #include <utils/KeyedVector.h>
26 #include <system/audio.h>
27 #include "AudioSourceDescriptor.h"
28 
29 namespace android {
30 
31 class IOProfile;
32 class AudioMix;
33 class AudioPolicyClientInterface;
34 class DeviceDescriptor;
35 
36 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
37 // and keep track of the usage of this output by each audio stream type.
38 class AudioOutputDescriptor: public AudioPortConfig
39 {
40 public:
41     AudioOutputDescriptor(const sp<AudioPort>& port,
42                           AudioPolicyClientInterface *clientInterface);
~AudioOutputDescriptor()43     virtual ~AudioOutputDescriptor() {}
44 
45     status_t    dump(int fd);
46     void        log(const char* indent);
47 
48     audio_port_handle_t getId() const;
49     virtual audio_devices_t device() const;
50     virtual bool sharesHwModuleWith(const sp<AudioOutputDescriptor>& outputDesc);
51     virtual audio_devices_t supportedDevices();
isDuplicated()52     virtual bool isDuplicated() const { return false; }
latency()53     virtual uint32_t latency() { return 0; }
54     virtual bool isFixedVolume(audio_devices_t device);
subOutput1()55     virtual sp<AudioOutputDescriptor> subOutput1() { return 0; }
subOutput2()56     virtual sp<AudioOutputDescriptor> subOutput2() { return 0; }
57     virtual bool setVolume(float volume,
58                            audio_stream_type_t stream,
59                            audio_devices_t device,
60                            uint32_t delayMs,
61                            bool force);
62     virtual void changeRefCount(audio_stream_type_t stream, int delta);
63 
64     bool isActive(uint32_t inPastMs = 0) const;
65     bool isStreamActive(audio_stream_type_t stream,
66                         uint32_t inPastMs = 0,
67                         nsecs_t sysTime = 0) const;
68 
69     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
70                            const struct audio_port_config *srcConfig = NULL) const;
getAudioPort()71     virtual sp<AudioPort> getAudioPort() const { return mPort; }
72     virtual void toAudioPort(struct audio_port *port) const;
73 
74     audio_module_handle_t getModuleHandle() const;
75 
getPatchHandle()76     audio_patch_handle_t getPatchHandle() const { return mPatchHandle; };
setPatchHandle(audio_patch_handle_t handle)77     void setPatchHandle(audio_patch_handle_t handle) { mPatchHandle = handle; };
78 
79     sp<AudioPort>       mPort;
80     audio_devices_t mDevice;                   // current device this output is routed to
81     uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
82     nsecs_t mStopTime[AUDIO_STREAM_CNT];
83     float mCurVolume[AUDIO_STREAM_CNT];   // current stream volume in dB
84     int mMuteCount[AUDIO_STREAM_CNT];     // mute request counter
85     bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
86                                         // device selection. See checkDeviceMuteStrategies()
87     AudioPolicyClientInterface *mClientInterface;
88 
89 protected:
90     audio_patch_handle_t mPatchHandle;
91     audio_port_handle_t mId;
92 };
93 
94 // Audio output driven by a software mixer in audio flinger.
95 class SwAudioOutputDescriptor: public AudioOutputDescriptor
96 {
97 public:
98     SwAudioOutputDescriptor(const sp<IOProfile>& profile,
99                             AudioPolicyClientInterface *clientInterface);
~SwAudioOutputDescriptor()100     virtual ~SwAudioOutputDescriptor() {}
101 
102     status_t    dump(int fd);
103 
104     void setIoHandle(audio_io_handle_t ioHandle);
105 
106     virtual audio_devices_t device() const;
107     virtual bool sharesHwModuleWith(const sp<AudioOutputDescriptor>& outputDesc);
108     virtual audio_devices_t supportedDevices();
109     virtual uint32_t latency();
isDuplicated()110     virtual bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
111     virtual bool isFixedVolume(audio_devices_t device);
subOutput1()112     virtual sp<AudioOutputDescriptor> subOutput1() { return mOutput1; }
subOutput2()113     virtual sp<AudioOutputDescriptor> subOutput2() { return mOutput2; }
114     virtual void changeRefCount(audio_stream_type_t stream, int delta);
115     virtual bool setVolume(float volume,
116                            audio_stream_type_t stream,
117                            audio_devices_t device,
118                            uint32_t delayMs,
119                            bool force);
120 
121     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
122                            const struct audio_port_config *srcConfig = NULL) const;
123     virtual void toAudioPort(struct audio_port *port) const;
124 
125     const sp<IOProfile> mProfile;          // I/O profile this output derives from
126     audio_io_handle_t mIoHandle;           // output handle
127     uint32_t mLatency;                  //
128     audio_output_flags_t mFlags;   //
129     AudioMix *mPolicyMix;             // non NULL when used by a dynamic policy
130     sp<SwAudioOutputDescriptor> mOutput1;    // used by duplicated outputs: first output
131     sp<SwAudioOutputDescriptor> mOutput2;    // used by duplicated outputs: second output
132     uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
133     audio_session_t mDirectClientSession; // session id of the direct output client
134     uint32_t mGlobalRefCount;  // non-stream-specific ref count
135 };
136 
137 // Audio output driven by an input device directly.
138 class HwAudioOutputDescriptor: public AudioOutputDescriptor
139 {
140 public:
141     HwAudioOutputDescriptor(const sp<AudioSourceDescriptor>& source,
142                             AudioPolicyClientInterface *clientInterface);
~HwAudioOutputDescriptor()143     virtual ~HwAudioOutputDescriptor() {}
144 
145     status_t    dump(int fd);
146 
147     virtual audio_devices_t supportedDevices();
148     virtual bool setVolume(float volume,
149                            audio_stream_type_t stream,
150                            audio_devices_t device,
151                            uint32_t delayMs,
152                            bool force);
153 
154     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
155                            const struct audio_port_config *srcConfig = NULL) const;
156     virtual void toAudioPort(struct audio_port *port) const;
157 
158     const sp<AudioSourceDescriptor> mSource;
159 
160 };
161 
162 class SwAudioOutputCollection :
163         public DefaultKeyedVector< audio_io_handle_t, sp<SwAudioOutputDescriptor> >
164 {
165 public:
166     bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
167 
168     /**
169      * return whether a stream is playing remotely, override to change the definition of
170      * local/remote playback, used for instance by notification manager to not make
171      * media players lose audio focus when not playing locally
172      * For the base implementation, "remotely" means playing during screen mirroring which
173      * uses an output for playback with a non-empty, non "0" address.
174      */
175     bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
176 
177     /**
178      * returns the A2DP output handle if it is open or 0 otherwise
179      */
180     audio_io_handle_t getA2dpOutput() const;
181 
182     sp<SwAudioOutputDescriptor> getOutputFromId(audio_port_handle_t id) const;
183 
184     sp<SwAudioOutputDescriptor> getPrimaryOutput() const;
185 
186     /**
187      * return true if any output is playing anything besides the stream to ignore
188      */
189     bool isAnyOutputActive(audio_stream_type_t streamToIgnore) const;
190 
191     audio_devices_t getSupportedDevices(audio_io_handle_t handle) const;
192 
193     status_t dump(int fd) const;
194 };
195 
196 class HwAudioOutputCollection :
197         public DefaultKeyedVector< audio_io_handle_t, sp<HwAudioOutputDescriptor> >
198 {
199 public:
200     bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
201 
202     /**
203      * return true if any output is playing anything besides the stream to ignore
204      */
205     bool isAnyOutputActive(audio_stream_type_t streamToIgnore) const;
206 
207     status_t dump(int fd) const;
208 };
209 
210 
211 }; // namespace android
212