Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 18 | |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 21 | #include <utils/Timers.h> |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/KeyedVector.h> |
| 24 | #include <hardware_legacy/AudioPolicyManagerBase.h> |
| 25 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 26 | |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 27 | namespace android_audio_legacy { |
| 28 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 29 | // ---------------------------------------------------------------------------- |
| 30 | |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 31 | class AudioPolicyManager: public AudioPolicyManagerBase |
| 32 | { |
| 33 | |
| 34 | public: |
| 35 | AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 36 | : AudioPolicyManagerBase(clientInterface) {} |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 37 | |
| 38 | virtual ~AudioPolicyManager() {} |
| 39 | |
| 40 | // AudioPolicyInterface |
| 41 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
| 42 | AudioSystem::device_connection_state state, |
| 43 | const char *device_address); |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 44 | uint32_t checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc, |
| 45 | audio_devices_t prevDevice, |
| 46 | uint32_t delayMs); |
| 47 | |
| 48 | void setStrategyMute(routing_strategy strategy, |
| 49 | bool on, |
| 50 | audio_io_handle_t output, |
| 51 | int delayMs = 0, |
| 52 | audio_devices_t device = (audio_devices_t)0); |
| 53 | void setStreamMute(int stream, bool on, audio_io_handle_t output, |
| 54 | int delayMs = 0, |
| 55 | audio_devices_t device = (audio_devices_t)0); |
| 56 | |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 57 | virtual AudioSystem::device_connection_state getDeviceConnectionState(audio_devices_t device, |
| 58 | const char *device_address); |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 59 | virtual void setPhoneState(int state); |
| 60 | virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config); |
| 61 | virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream, |
| 62 | uint32_t samplingRate = 0, |
| 63 | uint32_t format = AudioSystem::FORMAT_DEFAULT, |
| 64 | uint32_t channels = 0, |
| 65 | AudioSystem::output_flags flags = |
| 66 | AudioSystem::OUTPUT_FLAG_INDIRECT); |
| 67 | virtual status_t startOutput(audio_io_handle_t output, |
| 68 | AudioSystem::stream_type stream, |
| 69 | int session = 0); |
| 70 | virtual status_t stopOutput(audio_io_handle_t output, |
| 71 | AudioSystem::stream_type stream, |
| 72 | int session = 0); |
| 73 | virtual void releaseOutput(audio_io_handle_t output); |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 74 | virtual audio_io_handle_t getInput(int inputSource, |
| 75 | uint32_t samplingRate, |
| 76 | uint32_t format, |
| 77 | uint32_t channels, |
| 78 | AudioSystem::audio_in_acoustics acoustics); |
| 79 | |
| 80 | // indicates to the audio policy manager that the input starts being used. |
| 81 | virtual status_t startInput(audio_io_handle_t input); |
| 82 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 83 | // indicates to the audio policy manager that the input stops being used. |
| 84 | virtual status_t stopInput(audio_io_handle_t input); |
| 85 | virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, |
| 86 | int index, |
| 87 | audio_devices_t device); |
| 88 | protected: |
| 89 | // return the strategy corresponding to a given stream type |
| 90 | static routing_strategy getStrategy(AudioSystem::stream_type stream); |
| 91 | |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 92 | // return appropriate device for streams handled by the specified strategy according to current |
| 93 | // phone state, connected devices... |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 94 | // if fromCache is true, the device is returned from mDeviceForStrategy[], |
| 95 | // otherwise it is determine by current state |
| 96 | // (device connected,phone state, force use, a2dp output...) |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 97 | // This allows to: |
| 98 | // 1 speed up process when the state is stable (when starting or stopping an output) |
| 99 | // 2 access to either current device selection (fromCache == true) or |
| 100 | // "future" device selection (fromCache == false) when called from a context |
| 101 | // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 102 | // before updateDevicesAndOutputs() is called. |
| 103 | virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy, |
| 104 | bool fromCache = true); |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 105 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 106 | // change the route of the specified output. Returns the number of ms we have slept to |
| 107 | // allow new routing to take effect in certain cases. |
| 108 | uint32_t setOutputDevice(audio_io_handle_t output, |
| 109 | audio_devices_t device, |
| 110 | bool force = false, |
| 111 | int delayMs = 0); |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 112 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 113 | // select input device corresponding to requested audio source |
| 114 | virtual audio_devices_t getDeviceForInputSource(int inputSource); |
| 115 | |
| 116 | // check that volume change is permitted, compute and send new volume to audio hardware |
| 117 | status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false); |
| 118 | |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 119 | |
| 120 | // when a device is connected, checks if an open output can be routed |
| 121 | // to this device. If none is open, tries to open one of the available outputs. |
| 122 | // Returns an output suitable to this device or 0. |
| 123 | // when a device is disconnected, checks if an output is not used any more and |
| 124 | // returns its handle if any. |
| 125 | // transfers the audio tracks and effects from one output thread to another accordingly. |
| 126 | status_t checkOutputsForDevice(audio_devices_t device, |
| 127 | AudioSystem::device_connection_state state, |
| 128 | SortedVector<audio_io_handle_t>& outputs); |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 129 | // manages A2DP output suspend/restore according to phone state and BT SCO usage |
| 130 | void checkA2dpSuspend(); |
| 131 | |
| 132 | // returns the A2DP output handle if it is open or 0 otherwise |
| 133 | audio_io_handle_t getA2dpOutput(); |
| 134 | |
| 135 | // returns true if give output is direct output |
| 136 | bool isDirectOutput(audio_io_handle_t output); |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 137 | |
| 138 | virtual AudioPolicyManagerBase::IOProfile* getProfileForDirectOutput( |
| 139 | audio_devices_t device, |
| 140 | uint32_t samplingRate, |
| 141 | uint32_t format, |
| 142 | uint32_t channelMask, |
| 143 | audio_output_flags_t flags); |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 144 | bool isCompatibleProfile(AudioPolicyManagerBase::IOProfile *profile, |
| 145 | audio_devices_t device, |
| 146 | uint32_t samplingRate, |
| 147 | uint32_t format, |
| 148 | uint32_t channelMask, |
| 149 | audio_output_flags_t flags); |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 150 | // selects the most appropriate device on output for current state |
| 151 | // must be called every time a condition that affects the device choice for a given output is |
| 152 | // changed: connected device, phone state, force use, output start, output stop.. |
| 153 | // see getDeviceForStrategy() for the use of fromCache parameter |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 154 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 155 | audio_devices_t getNewDevice(audio_io_handle_t output, bool fromCache); |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 156 | |
| 157 | |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 158 | // returns the category the device belongs to with regard to volume curve management |
| 159 | static device_category getDeviceCategory(audio_devices_t device); |
| 160 | |
| 161 | // extract one device relevant for volume control from multiple device selection |
| 162 | static audio_devices_t getDeviceForVolume(audio_devices_t device); |
| 163 | // true is current platform implements a back microphone |
| 164 | virtual bool hasBackMicrophone() const { return false; } |
| 165 | // true is current platform supports suplication of notifications and ringtones over A2DP output |
| 166 | virtual bool a2dpUsedForSonification() const { return true; } |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 167 | |
| 168 | private: |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 169 | |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 170 | void handleNotificationRoutingForStream(AudioSystem::stream_type stream); |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 171 | bool platform_is_Fusion3(); |
Daniel Hillenbrand | d81f36f | 2013-04-06 18:56:57 +0000 | [diff] [blame] | 172 | }; |
Mike Grissom | e0f7b47 | 2013-08-08 20:25:51 -0700 | [diff] [blame] | 173 | }; |