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 virtual audio_devices_t device() const; 105 virtual bool sharesHwModuleWith(const sp<AudioOutputDescriptor>& outputDesc); 106 virtual audio_devices_t supportedDevices(); 107 virtual uint32_t latency(); isDuplicated()108 virtual bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); } 109 virtual bool isFixedVolume(audio_devices_t device); subOutput1()110 virtual sp<AudioOutputDescriptor> subOutput1() { return mOutput1; } subOutput2()111 virtual sp<AudioOutputDescriptor> subOutput2() { return mOutput2; } 112 virtual void changeRefCount(audio_stream_type_t stream, int delta); 113 virtual bool setVolume(float volume, 114 audio_stream_type_t stream, 115 audio_devices_t device, 116 uint32_t delayMs, 117 bool force); 118 119 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 120 const struct audio_port_config *srcConfig = NULL) const; 121 virtual void toAudioPort(struct audio_port *port) const; 122 123 status_t open(const audio_config_t *config, 124 audio_devices_t device, 125 const String8& address, 126 audio_stream_type_t stream, 127 audio_output_flags_t flags, 128 audio_io_handle_t *output); 129 // Called when a stream is about to be started 130 // Note: called before changeRefCount(1); 131 status_t start(); 132 // Called after a stream is stopped. 133 // Note: called after changeRefCount(-1); 134 void stop(); 135 void close(); 136 status_t openDuplicating(const sp<SwAudioOutputDescriptor>& output1, 137 const sp<SwAudioOutputDescriptor>& output2, 138 audio_io_handle_t *ioHandle); 139 140 const sp<IOProfile> mProfile; // I/O profile this output derives from 141 audio_io_handle_t mIoHandle; // output handle 142 uint32_t mLatency; // 143 audio_output_flags_t mFlags; // 144 AudioMix *mPolicyMix; // non NULL when used by a dynamic policy 145 sp<SwAudioOutputDescriptor> mOutput1; // used by duplicated outputs: first output 146 sp<SwAudioOutputDescriptor> mOutput2; // used by duplicated outputs: second output 147 uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only) 148 audio_session_t mDirectClientSession; // session id of the direct output client 149 uint32_t mGlobalRefCount; // non-stream-specific ref count 150 }; 151 152 // Audio output driven by an input device directly. 153 class HwAudioOutputDescriptor: public AudioOutputDescriptor 154 { 155 public: 156 HwAudioOutputDescriptor(const sp<AudioSourceDescriptor>& source, 157 AudioPolicyClientInterface *clientInterface); ~HwAudioOutputDescriptor()158 virtual ~HwAudioOutputDescriptor() {} 159 160 status_t dump(int fd); 161 162 virtual audio_devices_t supportedDevices(); 163 virtual bool setVolume(float volume, 164 audio_stream_type_t stream, 165 audio_devices_t device, 166 uint32_t delayMs, 167 bool force); 168 169 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 170 const struct audio_port_config *srcConfig = NULL) const; 171 virtual void toAudioPort(struct audio_port *port) const; 172 173 const sp<AudioSourceDescriptor> mSource; 174 175 }; 176 177 class SwAudioOutputCollection : 178 public DefaultKeyedVector< audio_io_handle_t, sp<SwAudioOutputDescriptor> > 179 { 180 public: 181 bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 182 183 /** 184 * return whether a stream is playing remotely, override to change the definition of 185 * local/remote playback, used for instance by notification manager to not make 186 * media players lose audio focus when not playing locally 187 * For the base implementation, "remotely" means playing during screen mirroring which 188 * uses an output for playback with a non-empty, non "0" address. 189 */ 190 bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 191 192 /** 193 * return whether a stream is playing, but not on a "remote" device. 194 * Override to change the definition of a local/remote playback. 195 * Used for instance by policy manager to alter the speaker playback ("speaker safe" behavior) 196 * when media plays or not locally. 197 * For the base implementation, "remotely" means playing during screen mirroring. 198 */ 199 bool isStreamActiveLocally(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 200 201 /** 202 * returns the A2DP output handle if it is open or 0 otherwise 203 */ 204 audio_io_handle_t getA2dpOutput() const; 205 206 /** 207 * returns true if primary HAL supports A2DP Offload 208 */ 209 bool isA2dpOffloadedOnPrimary() const; 210 211 /** 212 * returns true if A2DP is supported (either via hardware offload or software encoding) 213 */ 214 bool isA2dpSupported() const; 215 216 sp<SwAudioOutputDescriptor> getOutputFromId(audio_port_handle_t id) const; 217 218 sp<SwAudioOutputDescriptor> getPrimaryOutput() const; 219 220 /** 221 * return true if any output is playing anything besides the stream to ignore 222 */ 223 bool isAnyOutputActive(audio_stream_type_t streamToIgnore) const; 224 225 audio_devices_t getSupportedDevices(audio_io_handle_t handle) const; 226 227 status_t dump(int fd) const; 228 }; 229 230 class HwAudioOutputCollection : 231 public DefaultKeyedVector< audio_io_handle_t, sp<HwAudioOutputDescriptor> > 232 { 233 public: 234 bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 235 236 /** 237 * return true if any output is playing anything besides the stream to ignore 238 */ 239 bool isAnyOutputActive(audio_stream_type_t streamToIgnore) const; 240 241 status_t dump(int fd) const; 242 }; 243 244 245 } // namespace android 246