blob: f0f02370f815ad8ae834c8f707a787831adc9f21 [file] [log] [blame]
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +00001/*
2 * Copyright (C) 2009 The Android Open Source Project
Mike Grissome0f7b472013-08-08 20:25:51 -07003 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +00004 *
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 Grissome0f7b472013-08-08 20:25:51 -070018
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +000019#include <stdint.h>
20#include <sys/types.h>
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +000021#include <utils/Timers.h>
22#include <utils/Errors.h>
23#include <utils/KeyedVector.h>
24#include <hardware_legacy/AudioPolicyManagerBase.h>
25
Mike Grissome0f7b472013-08-08 20:25:51 -070026
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +000027namespace android_audio_legacy {
28
Mike Grissome0f7b472013-08-08 20:25:51 -070029// ----------------------------------------------------------------------------
30
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +000031class AudioPolicyManager: public AudioPolicyManagerBase
32{
33
34public:
35 AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Mike Grissome0f7b472013-08-08 20:25:51 -070036 : AudioPolicyManagerBase(clientInterface) {}
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +000037
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 Grissome0f7b472013-08-08 20:25:51 -070044 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 Hillenbrandd81f36f2013-04-06 18:56:57 +000057 virtual AudioSystem::device_connection_state getDeviceConnectionState(audio_devices_t device,
58 const char *device_address);
Mike Grissome0f7b472013-08-08 20:25:51 -070059 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 Hillenbrandd81f36f2013-04-06 18:56:57 +000074 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 Grissome0f7b472013-08-08 20:25:51 -070083 // 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);
88protected:
89 // return the strategy corresponding to a given stream type
90 static routing_strategy getStrategy(AudioSystem::stream_type stream);
91
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +000092 // return appropriate device for streams handled by the specified strategy according to current
93 // phone state, connected devices...
Mike Grissome0f7b472013-08-08 20:25:51 -070094 // 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 Hillenbrandd81f36f2013-04-06 18:56:57 +000097 // 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 Grissome0f7b472013-08-08 20:25:51 -0700102 // before updateDevicesAndOutputs() is called.
103 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
104 bool fromCache = true);
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +0000105
Mike Grissome0f7b472013-08-08 20:25:51 -0700106 // 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 Hillenbrandd81f36f2013-04-06 18:56:57 +0000112
Mike Grissome0f7b472013-08-08 20:25:51 -0700113 // 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 Hillenbrandd81f36f2013-04-06 18:56:57 +0000119
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 Grissome0f7b472013-08-08 20:25:51 -0700129 // 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 Hillenbrandd81f36f2013-04-06 18:56:57 +0000137
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 Hillenbrandd81f36f2013-04-06 18:56:57 +0000144 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 Grissome0f7b472013-08-08 20:25:51 -0700150 // 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 Hillenbrandd81f36f2013-04-06 18:56:57 +0000154
Mike Grissome0f7b472013-08-08 20:25:51 -0700155 audio_devices_t getNewDevice(audio_io_handle_t output, bool fromCache);
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +0000156
157
Mike Grissome0f7b472013-08-08 20:25:51 -0700158 // 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 Hillenbrandd81f36f2013-04-06 18:56:57 +0000167
168private:
Mike Grissome0f7b472013-08-08 20:25:51 -0700169
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +0000170 void handleNotificationRoutingForStream(AudioSystem::stream_type stream);
Mike Grissome0f7b472013-08-08 20:25:51 -0700171 bool platform_is_Fusion3();
Daniel Hillenbrandd81f36f2013-04-06 18:56:57 +0000172};
Mike Grissome0f7b472013-08-08 20:25:51 -0700173};