1e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent/*
2e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * Copyright (C) 2009 The Android Open Source Project
3e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent *
4e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
5e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * you may not use this file except in compliance with the License.
6e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * You may obtain a copy of the License at
7e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent *
8e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
9e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent *
10e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * Unless required by applicable law or agreed to in writing, software
11e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
12e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * See the License for the specific language governing permissions and
14e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent * limitations under the License.
15e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent */
16e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
17e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
18e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <stdint.h>
19e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <sys/types.h>
20e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <cutils/config_utils.h>
21e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <cutils/misc.h>
22e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <utils/Timers.h>
23e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <utils/Errors.h>
24e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <utils/KeyedVector.h>
25e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#include <utils/SortedVector.h>
26275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent#include <media/AudioPolicy.h>
273b73df74357b33869b39a1d69427673c780bd805Eric Laurent#include "AudioPolicyInterface.h"
28e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
29e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
303b73df74357b33869b39a1d69427673c780bd805Eric Laurentnamespace android {
31e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
32e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// ----------------------------------------------------------------------------
33e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
34e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
35e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
36e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
37e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define SONIFICATION_HEADSET_VOLUME_MIN  0.016
38e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// Time in milliseconds during which we consider that music is still active after a music
39e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// track was stopped - see computeVolume()
40e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define SONIFICATION_HEADSET_MUSIC_DELAY  5000
41e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// Time in milliseconds after media stopped playing during which we consider that the
42e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// sonification should be as unobtrusive as during the time media was playing.
43e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000
44e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// Time in milliseconds during witch some streams are muted while the audio path
45e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// is switched
46e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define MUTE_TIME_MS 2000
47e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
48e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define NUM_TEST_OUTPUTS 5
49e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
50e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define NUM_VOL_CURVE_KNEES 2
51e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
52e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// Default minimum length allowed for offloading a compressed track
53e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// Can be overridden by the audio.offload.min.duration.secs property
54e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
55e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
561e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent#define MAX_MIXER_SAMPLING_RATE 48000
579a60538b9ee2b097770dcd378281b31f4218ae56Andy Hung#define MAX_MIXER_CHANNEL_COUNT 8
581e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent
59e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// ----------------------------------------------------------------------------
60e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent// AudioPolicyManager implements audio policy manager behavior common to all platforms.
61e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent// ----------------------------------------------------------------------------
62e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
63e07208765fcd5904165e425ec714a25c350a2f40Eric Laurentclass AudioPolicyManager: public AudioPolicyInterface
64e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#ifdef AUDIO_POLICY_TEST
65e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent    , public Thread
66e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#endif //AUDIO_POLICY_TEST
67e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent{
68e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
69e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurentpublic:
70e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent                AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
71e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        virtual ~AudioPolicyManager();
72e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
73e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // AudioPolicyInterface
74e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t setDeviceConnectionState(audio_devices_t device,
753b73df74357b33869b39a1d69427673c780bd805Eric Laurent                                                          audio_policy_dev_state_t state,
76e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                          const char *device_address);
773b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
78e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                                              const char *device_address);
793b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual void setPhoneState(audio_mode_t state);
803b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual void setForceUse(audio_policy_force_use_t usage,
813b73df74357b33869b39a1d69427673c780bd805Eric Laurent                                 audio_policy_forced_cfg_t config);
823b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
83e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual void setSystemProperty(const char* property, const char* value);
84e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t initCheck();
853b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
86e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                            uint32_t samplingRate,
87e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                            audio_format_t format,
88e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                            audio_channel_mask_t channelMask,
893b73df74357b33869b39a1d69427673c780bd805Eric Laurent                                            audio_output_flags_t flags,
90e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                            const audio_offload_info_t *offloadInfo);
91e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent        virtual status_t getOutputForAttr(const audio_attributes_t *attr,
92e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          audio_io_handle_t *output,
93e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          audio_session_t session,
94e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          audio_stream_type_t *stream,
95e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          uint32_t samplingRate,
96e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          audio_format_t format,
97e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          audio_channel_mask_t channelMask,
98e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          audio_output_flags_t flags,
99e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                          const audio_offload_info_t *offloadInfo);
100e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t startOutput(audio_io_handle_t output,
1013b73df74357b33869b39a1d69427673c780bd805Eric Laurent                                     audio_stream_type_t stream,
102e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                     audio_session_t session);
103e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t stopOutput(audio_io_handle_t output,
1043b73df74357b33869b39a1d69427673c780bd805Eric Laurent                                    audio_stream_type_t stream,
105e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                    audio_session_t session);
106e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent        virtual void releaseOutput(audio_io_handle_t output,
107e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                   audio_stream_type_t stream,
108e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                   audio_session_t session);
109caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent        virtual status_t getInputForAttr(const audio_attributes_t *attr,
110caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent                                         audio_io_handle_t *input,
111caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent                                         audio_session_t session,
112caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent                                         uint32_t samplingRate,
113caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent                                         audio_format_t format,
114caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent                                         audio_channel_mask_t channelMask,
11597bb33f58d742539f3382583d7978fca71ffa2d5Jean-Michel Trivi                                         audio_input_flags_t flags,
11697bb33f58d742539f3382583d7978fca71ffa2d5Jean-Michel Trivi                                         input_type_t *inputType);
117e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
118e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // indicates to the audio policy manager that the input starts being used.
1194dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent        virtual status_t startInput(audio_io_handle_t input,
1204dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent                                    audio_session_t session);
121e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
122e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // indicates to the audio policy manager that the input stops being used.
1234dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent        virtual status_t stopInput(audio_io_handle_t input,
1244dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent                                   audio_session_t session);
1254dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent        virtual void releaseInput(audio_io_handle_t input,
1264dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent                                  audio_session_t session);
127d46929666d7e4b1cad45afd7dcb883ec4dd2d49fEric Laurent        virtual void closeAllInputs();
1283b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual void initStreamVolume(audio_stream_type_t stream,
129e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                    int indexMin,
130e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                    int indexMax);
1313b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
132e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                              int index,
133e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                              audio_devices_t device);
1343b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
135e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                              int *index,
136e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                              audio_devices_t device);
137e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
138e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // return the strategy corresponding to a given stream type
1393b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
1405bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi        // return the strategy corresponding to the given audio attributes
1415bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi        virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr);
142e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
143e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // return the enabled output devices for the given stream type
1443b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
145e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
146e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
147e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t registerEffect(const effect_descriptor_t *desc,
148e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                        audio_io_handle_t io,
149e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                        uint32_t strategy,
150e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                        int session,
151e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                        int id);
152e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t unregisterEffect(int id);
153e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t setEffectEnabled(int id, bool enabled);
154e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
1553b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
156e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // return whether a stream is playing remotely, override to change the definition of
157e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //   local/remote playback, used for instance by notification manager to not make
158e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //   media players lose audio focus when not playing locally
1591767df778e20c1395afc63b3a0479bd903e7b9f0Jean-Michel Trivi        //   For the base implementation, "remotely" means playing during screen mirroring which
1601767df778e20c1395afc63b3a0479bd903e7b9f0Jean-Michel Trivi        //   uses an output for playback with a non-empty, non "0" address.
1613b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
162e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual bool isSourceActive(audio_source_t source) const;
163e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
164e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual status_t dump(int fd);
165e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
166e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
167e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
1686a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        virtual status_t listAudioPorts(audio_port_role_t role,
1696a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                        audio_port_type_t type,
1706a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                        unsigned int *num_ports,
1716a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                        struct audio_port *ports,
1726a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                        unsigned int *generation);
1736a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        virtual status_t getAudioPort(struct audio_port *port);
1746a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        virtual status_t createAudioPatch(const struct audio_patch *patch,
1756a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                           audio_patch_handle_t *handle,
1766a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                           uid_t uid);
1776a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
1786a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                              uid_t uid);
1796a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        virtual status_t listAudioPatches(unsigned int *num_patches,
1806a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                          struct audio_patch *patches,
1816a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                          unsigned int *generation);
1826a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        virtual status_t setAudioPortConfig(const struct audio_port_config *config);
1836a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        virtual void clearAudioPatches(uid_t uid);
1846a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent
185df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent        virtual status_t acquireSoundTriggerSession(audio_session_t *session,
186df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent                                               audio_io_handle_t *ioHandle,
187df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent                                               audio_devices_t *device);
188df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent
189df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent        virtual status_t releaseSoundTriggerSession(audio_session_t session);
190df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent
191275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
192275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
193275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent
194e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurentprotected:
195e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
196e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        enum routing_strategy {
197e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            STRATEGY_MEDIA,
198e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            STRATEGY_PHONE,
199e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            STRATEGY_SONIFICATION,
200e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            STRATEGY_SONIFICATION_RESPECTFUL,
201e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            STRATEGY_DTMF,
202e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            STRATEGY_ENFORCED_AUDIBLE,
203d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi            STRATEGY_TRANSMITTED_THROUGH_SPEAKER,
204223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent            STRATEGY_ACCESSIBILITY,
205223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent            STRATEGY_REROUTING,
206e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            NUM_STRATEGIES
207e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
208e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
209e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // 4 points to define the volume attenuation curve, each characterized by the volume
210e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
211e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
212e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
213e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
214e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
215e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        class VolumeCurvePoint
216e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        {
217e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        public:
218e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            int mIndex;
219e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            float mDBAttenuation;
220e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
221e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
222e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // device categories used for volume curve management.
223e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        enum device_category {
224e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            DEVICE_CATEGORY_HEADSET,
225e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            DEVICE_CATEGORY_SPEAKER,
226e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            DEVICE_CATEGORY_EARPIECE,
227ac29afacbb34b92f1948188e5353fce5a252ccb3Jon Eklund            DEVICE_CATEGORY_EXT_MEDIA,
228e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            DEVICE_CATEGORY_CNT
229e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
230e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
2311afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent        class HwModule;
232e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
2331afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent        class AudioGain: public RefBase
2341afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent        {
235951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        public:
236a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            AudioGain(int index, bool useInChannelMask);
2371afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            virtual ~AudioGain() {}
238951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
2391afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            void dump(int fd, int spaces, int index) const;
240951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
241a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            void getDefaultConfig(struct audio_gain_config *config);
242a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            status_t checkConfig(const struct audio_gain_config *config);
243a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            int               mIndex;
2441afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            struct audio_gain mGain;
245a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            bool              mUseInChannelMask;
246951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        };
247951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
2481f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        class AudioPort: public virtual RefBase
249951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        {
250951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        public:
2511afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            AudioPort(const String8& name, audio_port_type_t type,
252a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent                      audio_port_role_t role, const sp<HwModule>& module);
253951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            virtual ~AudioPort() {}
254951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
255951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            virtual void toAudioPort(struct audio_port *port) const;
256951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
257f17026dfef596cf1c8008fda20f1f2ad23a3df3aJean-Michel Trivi            void importAudioPort(const sp<AudioPort> port);
258f17026dfef596cf1c8008fda20f1f2ad23a3df3aJean-Michel Trivi            void clearCapabilities();
259f17026dfef596cf1c8008fda20f1f2ad23a3df3aJean-Michel Trivi
260951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            void loadSamplingRates(char *name);
261951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            void loadFormats(char *name);
262951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            void loadOutChannels(char *name);
263951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            void loadInChannels(char *name);
264951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
2651afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            audio_gain_mode_t loadGainMode(char *name);
266a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            void loadGain(cnode *root, int index);
2671db89b9c2692fd70eb1cc98b7db2f9beed81e66eEric Laurent            virtual void loadGains(cnode *root);
2681afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
269cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // searches for an exact match
270cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            status_t checkExactSamplingRate(uint32_t samplingRate) const;
271cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // searches for a compatible match, and returns the best match via updatedSamplingRate
272cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            status_t checkCompatibleSamplingRate(uint32_t samplingRate,
273cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten                    uint32_t *updatedSamplingRate) const;
274cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // searches for an exact match
275cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            status_t checkExactChannelMask(audio_channel_mask_t channelMask) const;
276cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // searches for a compatible match, currently implemented for input channel masks only
277cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            status_t checkCompatibleChannelMask(audio_channel_mask_t channelMask) const;
278a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            status_t checkFormat(audio_format_t format) const;
279a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            status_t checkGain(const struct audio_gain_config *gainConfig, int index) const;
280a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent
2811e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            uint32_t pickSamplingRate() const;
2821e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            audio_channel_mask_t pickChannelMask() const;
2831e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            audio_format_t pickFormat() const;
2841e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent
2851e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            static const audio_format_t sPcmFormatCompareTable[];
2861e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            static int compareFormats(audio_format_t format1, audio_format_t format2);
2871e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent
2881afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            void dump(int fd, int spaces) const;
2891afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
2901afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            String8           mName;
2911afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            audio_port_type_t mType;
2921afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            audio_port_role_t mRole;
293a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            bool              mUseInChannelMask;
294951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
295951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            // indicates the supported parameters should be read from the output stream
296951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            // after it is opened for the first time
297951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            Vector <uint32_t> mSamplingRates; // supported sampling rates
298951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
299951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            Vector <audio_format_t> mFormats; // supported audio formats
3001afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            Vector < sp<AudioGain> > mGains; // gain controllers
3011f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            sp<HwModule> mModule;                 // audio HW module exposing this I/O stream
3025dbe47139713292bf45bbf4f1a7af0835a5ff368Eric Laurent            uint32_t mFlags; // attribute flags (e.g primary output,
3035dbe47139713292bf45bbf4f1a7af0835a5ff368Eric Laurent                                                // direct output...).
304951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        };
305951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
3061f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        class AudioPortConfig: public virtual RefBase
3071f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        {
3081f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        public:
3091f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            AudioPortConfig();
3101f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            virtual ~AudioPortConfig() {}
3111f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent
312a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent            status_t applyAudioPortConfig(const struct audio_port_config *config,
313a121f90f388343dc48793cbc7eb899aba42e7664Eric Laurent                                          struct audio_port_config *backupConfig = NULL);
3141f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
3151f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent                                   const struct audio_port_config *srcConfig = NULL) const = 0;
316961ec21a5878517ce6db27bad8da269637c45a44Marco Nelissen            virtual sp<AudioPort> getAudioPort() const = 0;
3171f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            uint32_t mSamplingRate;
3181f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            audio_format_t mFormat;
3191f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            audio_channel_mask_t mChannelMask;
3201f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            struct audio_gain_config mGain;
3211f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        };
3221f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent
3231f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent
3246a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        class AudioPatch: public RefBase
3256a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        {
3266a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        public:
3276a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent            AudioPatch(audio_patch_handle_t handle,
3286a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                       const struct audio_patch *patch, uid_t uid) :
3296a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                           mHandle(handle), mPatch(*patch), mUid(uid), mAfPatchHandle(0) {}
3306a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent
3314d41695d45e2c7211899afa94b20e32120b2b7e0Eric Laurent            status_t dump(int fd, int spaces, int index) const;
3324d41695d45e2c7211899afa94b20e32120b2b7e0Eric Laurent
3336a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent            audio_patch_handle_t mHandle;
3346a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent            struct audio_patch mPatch;
3356a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent            uid_t mUid;
3366a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent            audio_patch_handle_t mAfPatchHandle;
3376a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        };
338951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
3391f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        class DeviceDescriptor: public AudioPort, public AudioPortConfig
3403a4311c68348f728558e87b5db67d47605783890Eric Laurent        {
3413a4311c68348f728558e87b5db67d47605783890Eric Laurent        public:
3421f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            DeviceDescriptor(const String8& name, audio_devices_t type);
3431f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent
344951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            virtual ~DeviceDescriptor() {}
345951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
346951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            bool equals(const sp<DeviceDescriptor>& other) const;
3471db89b9c2692fd70eb1cc98b7db2f9beed81e66eEric Laurent
3481db89b9c2692fd70eb1cc98b7db2f9beed81e66eEric Laurent            // AudioPortConfig
3491db89b9c2692fd70eb1cc98b7db2f9beed81e66eEric Laurent            virtual sp<AudioPort> getAudioPort() const { return (AudioPort*) this; }
3501f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
3516a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                   const struct audio_port_config *srcConfig = NULL) const;
3521afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
3531db89b9c2692fd70eb1cc98b7db2f9beed81e66eEric Laurent            // AudioPort
3541db89b9c2692fd70eb1cc98b7db2f9beed81e66eEric Laurent            virtual void loadGains(cnode *root);
355951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            virtual void toAudioPort(struct audio_port *port) const;
3563a4311c68348f728558e87b5db67d47605783890Eric Laurent
3571afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            status_t dump(int fd, int spaces, int index) const;
3583a4311c68348f728558e87b5db67d47605783890Eric Laurent
359951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            audio_devices_t mDeviceType;
3603a4311c68348f728558e87b5db67d47605783890Eric Laurent            String8 mAddress;
361951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            audio_port_handle_t mId;
3623a4311c68348f728558e87b5db67d47605783890Eric Laurent        };
3633a4311c68348f728558e87b5db67d47605783890Eric Laurent
3643a4311c68348f728558e87b5db67d47605783890Eric Laurent        class DeviceVector : public SortedVector< sp<DeviceDescriptor> >
3653a4311c68348f728558e87b5db67d47605783890Eric Laurent        {
3663a4311c68348f728558e87b5db67d47605783890Eric Laurent        public:
367951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            DeviceVector() : SortedVector(), mDeviceTypes(AUDIO_DEVICE_NONE) {}
3683a4311c68348f728558e87b5db67d47605783890Eric Laurent
3693a4311c68348f728558e87b5db67d47605783890Eric Laurent            ssize_t         add(const sp<DeviceDescriptor>& item);
3703a4311c68348f728558e87b5db67d47605783890Eric Laurent            ssize_t         remove(const sp<DeviceDescriptor>& item);
3713a4311c68348f728558e87b5db67d47605783890Eric Laurent            ssize_t         indexOf(const sp<DeviceDescriptor>& item) const;
3723a4311c68348f728558e87b5db67d47605783890Eric Laurent
373951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            audio_devices_t types() const { return mDeviceTypes; }
3743a4311c68348f728558e87b5db67d47605783890Eric Laurent
3753a4311c68348f728558e87b5db67d47605783890Eric Laurent            void loadDevicesFromType(audio_devices_t types);
3761afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            void loadDevicesFromName(char *name, const DeviceVector& declaredDevices);
3771afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
378951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            sp<DeviceDescriptor> getDevice(audio_devices_t type, String8 address) const;
379951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            DeviceVector getDevicesFromType(audio_devices_t types) const;
3806a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent            sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const;
3811afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            sp<DeviceDescriptor> getDeviceFromName(const String8& name) const;
3820fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi            DeviceVector getDevicesFromTypeAddr(audio_devices_t type, String8 address)
3830fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi                    const;
3843a4311c68348f728558e87b5db67d47605783890Eric Laurent
3853a4311c68348f728558e87b5db67d47605783890Eric Laurent        private:
3863a4311c68348f728558e87b5db67d47605783890Eric Laurent            void refreshTypes();
387951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            audio_devices_t mDeviceTypes;
388e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
389e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
390e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // the IOProfile class describes the capabilities of an output or input stream.
391e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // It is currently assumed that all combination of listed parameters are supported.
392e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // It is used by the policy manager to determine if an output or input is suitable for
393e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // a given use case,  open/close it accordingly and connect/disconnect audio tracks
394e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // to/from it.
395951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        class IOProfile : public AudioPort
396e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        {
397e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        public:
3981f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            IOProfile(const String8& name, audio_port_role_t role, const sp<HwModule>& module);
399951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            virtual ~IOProfile();
400e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
401cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // This method is used for both output and input.
402cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // If parameter updatedSamplingRate is non-NULL, it is assigned the actual sample rate.
403cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // For input, flags is interpreted as audio_input_flags_t.
404cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten            // TODO: merge audio_output_flags_t and audio_input_flags_t.
405e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            bool isCompatibleProfile(audio_devices_t device,
406275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                     String8 address,
407e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                     uint32_t samplingRate,
408cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten                                     uint32_t *updatedSamplingRate,
409e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                     audio_format_t format,
410e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                     audio_channel_mask_t channelMask,
4115dbe47139713292bf45bbf4f1a7af0835a5ff368Eric Laurent                                     uint32_t flags) const;
412e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
413e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            void dump(int fd);
414d46929666d7e4b1cad45afd7dcb883ec4dd2d49fEric Laurent            void log();
415e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
4163a4311c68348f728558e87b5db67d47605783890Eric Laurent            DeviceVector  mSupportedDevices; // supported devices
4173a4311c68348f728558e87b5db67d47605783890Eric Laurent                                             // (devices this output can be routed to)
418e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
419e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
4201e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent        class HwModule : public RefBase
4211e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent        {
4221afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent        public:
4231afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent                    HwModule(const char *name);
4241afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent                    ~HwModule();
4251afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
4261afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            status_t loadOutput(cnode *root);
4271afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            status_t loadInput(cnode *root);
4281afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            status_t loadDevice(cnode *root);
4291afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
430275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            status_t addOutputProfile(String8 name, const audio_config_t *config,
431275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                      audio_devices_t device, String8 address);
432275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            status_t removeOutputProfile(String8 name);
433275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            status_t addInputProfile(String8 name, const audio_config_t *config,
434275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                      audio_devices_t device, String8 address);
435275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            status_t removeInputProfile(String8 name);
436275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent
4371afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            void dump(int fd);
4381afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
439eb108a4622825688b02d7afc981014d149913cd8Eric Laurent            const char *const        mName; // base name of the audio HW module (primary, a2dp ...)
440eb108a4622825688b02d7afc981014d149913cd8Eric Laurent            uint32_t                 mHalVersion; // audio HAL API version
441eb108a4622825688b02d7afc981014d149913cd8Eric Laurent            audio_module_handle_t    mHandle;
4421afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            Vector < sp<IOProfile> > mOutputProfiles; // output profiles exposed by this module
4431afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            Vector < sp<IOProfile> > mInputProfiles;  // input profiles exposed by this module
4441afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent            DeviceVector             mDeclaredDevices; // devices declared in audio_policy.conf
4451afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
4461afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent        };
4471afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent
448e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // default volume curve
449e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT];
450e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // default volume curve for media strategy
451e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT];
452ac29afacbb34b92f1948188e5353fce5a252ccb3Jon Eklund        // volume curve for non-media audio on ext media outputs (HDMI, Line, etc)
453ac29afacbb34b92f1948188e5353fce5a252ccb3Jon Eklund        static const VolumeCurvePoint sExtMediaSystemVolumeCurve[AudioPolicyManager::VOLCNT];
454e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // volume curve for media strategy on speakers
455e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT];
456ccd8e4a9da4d0bcc2c715452c5a18fabb23f86acJean-Michel Trivi        static const VolumeCurvePoint sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT];
457e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // volume curve for sonification strategy on speakers
458e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT];
459e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT];
460e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT];
461e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT];
462e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT];
463e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
464e07208765fcd5904165e425ec714a25c350a2f40Eric Laurent        static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
465d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        static const VolumeCurvePoint sLinearVolumeCurve[AudioPolicyManager::VOLCNT];
466d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        static const VolumeCurvePoint sSilentVolumeCurve[AudioPolicyManager::VOLCNT];
467223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        static const VolumeCurvePoint sFullScaleVolumeCurve[AudioPolicyManager::VOLCNT];
468e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // default volume curves per stream and device category. See initializeVolumeCurves()
469e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT];
470e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
471e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
472e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // and keep track of the usage of this output by each audio stream type.
4731f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        class AudioOutputDescriptor: public AudioPortConfig
474e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        {
475e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        public:
476951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            AudioOutputDescriptor(const sp<IOProfile>& profile);
477e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
478e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            status_t    dump(int fd);
479e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
480e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            audio_devices_t device() const;
4813b73df74357b33869b39a1d69427673c780bd805Eric Laurent            void changeRefCount(audio_stream_type_t stream, int delta);
482e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
483e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
484e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            audio_devices_t supportedDevices();
485e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            uint32_t latency();
4861f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            bool sharesHwModuleWith(const sp<AudioOutputDescriptor> outputDesc);
487e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            bool isActive(uint32_t inPastMs = 0) const;
4883b73df74357b33869b39a1d69427673c780bd805Eric Laurent            bool isStreamActive(audio_stream_type_t stream,
489e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                uint32_t inPastMs = 0,
490e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                nsecs_t sysTime = 0) const;
491e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            bool isStrategyActive(routing_strategy strategy,
492e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             uint32_t inPastMs = 0,
493e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             nsecs_t sysTime = 0) const;
494e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
4951f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
4966a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                   const struct audio_port_config *srcConfig = NULL) const;
497961ec21a5878517ce6db27bad8da269637c45a44Marco Nelissen            virtual sp<AudioPort> getAudioPort() const { return mProfile; }
498951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            void toAudioPort(struct audio_port *port) const;
499951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
500951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            audio_port_handle_t mId;
501951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            audio_io_handle_t mIoHandle;              // output handle
502e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            uint32_t mLatency;                  //
503e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            audio_output_flags_t mFlags;   //
504e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            audio_devices_t mDevice;                   // current device this output is routed to
505c722f30eef03e77054395ae122470cf8dba93937Eric Laurent            AudioMix *mPolicyMix;             // non NULL when used by a dynamic policy
506951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            audio_patch_handle_t mPatchHandle;
5073b73df74357b33869b39a1d69427673c780bd805Eric Laurent            uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
5083b73df74357b33869b39a1d69427673c780bd805Eric Laurent            nsecs_t mStopTime[AUDIO_STREAM_CNT];
5091f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            sp<AudioOutputDescriptor> mOutput1;    // used by duplicated outputs: first output
5101f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            sp<AudioOutputDescriptor> mOutput2;    // used by duplicated outputs: second output
5113b73df74357b33869b39a1d69427673c780bd805Eric Laurent            float mCurVolume[AUDIO_STREAM_CNT];   // current stream volume
5123b73df74357b33869b39a1d69427673c780bd805Eric Laurent            int mMuteCount[AUDIO_STREAM_CNT];     // mute request counter
513951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            const sp<IOProfile> mProfile;          // I/O profile this output derives from
514e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
515e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                // device selection. See checkDeviceMuteStrategies()
516e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
517e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
518e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
519e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
520e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // and keep track of the usage of this input.
5211f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        class AudioInputDescriptor: public AudioPortConfig
522e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        {
523e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        public:
524951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            AudioInputDescriptor(const sp<IOProfile>& profile);
525e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
526e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            status_t    dump(int fd);
527e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
528df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            audio_port_handle_t           mId;
529df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            audio_io_handle_t             mIoHandle;       // input handle
530df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            audio_devices_t               mDevice;         // current device this input is routed to
531c722f30eef03e77054395ae122470cf8dba93937Eric Laurent            AudioMix                      *mPolicyMix;     // non NULL when used by a dynamic policy
532df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            audio_patch_handle_t          mPatchHandle;
533df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            uint32_t                      mRefCount;       // number of AudioRecord clients using
534df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent                                                           // this input
535df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            uint32_t                      mOpenRefCount;
536df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            audio_source_t                mInputSource;    // input source selected by application
537df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent                                                           //(mediarecorder.h)
538df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            const sp<IOProfile>           mProfile;        // I/O profile this output derives from
539c722f30eef03e77054395ae122470cf8dba93937Eric Laurent            SortedVector<audio_session_t> mSessions;       // audio sessions attached to this input
540df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent            bool                          mIsSoundTrigger; // used by a soundtrigger capture
541951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
5421f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
5436a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                   const struct audio_port_config *srcConfig = NULL) const;
544961ec21a5878517ce6db27bad8da269637c45a44Marco Nelissen            virtual sp<AudioPort> getAudioPort() const { return mProfile; }
545951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent            void toAudioPort(struct audio_port *port) const;
546e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
547e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
548e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // stream descriptor used for volume control
549e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        class StreamDescriptor
550e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        {
551e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        public:
552e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            StreamDescriptor();
553e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
554e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            int getVolumeIndex(audio_devices_t device);
555e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            void dump(int fd);
556e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
557e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            int mIndexMin;      // min volume index
558e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            int mIndexMax;      // max volume index
559e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            KeyedVector<audio_devices_t, int> mIndexCur;   // current volume index per device
560e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            bool mCanBeMuted;   // true is the stream can be muted
561e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
562e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
563e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
564e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
565e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // stream descriptor used for volume control
5661f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        class EffectDescriptor : public RefBase
567e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        {
568e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        public:
569e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
570e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            status_t dump(int fd);
571e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
572e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            int mIo;                // io the effect is attached to
573e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            routing_strategy mStrategy; // routing strategy the effect is associated to
574e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            int mSession;               // audio session the effect is on
575e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            effect_descriptor_t mDesc;  // effect descriptor
576e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent            bool mEnabled;              // enabled state: CPU load being used or not
577e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        };
578e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
5791f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        void addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc);
5801f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
581e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
582e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // return the strategy corresponding to a given stream type
5833b73df74357b33869b39a1d69427673c780bd805Eric Laurent        static routing_strategy getStrategy(audio_stream_type_t stream);
584e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
585e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // return appropriate device for streams handled by the specified strategy according to current
586e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // phone state, connected devices...
587e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // if fromCache is true, the device is returned from mDeviceForStrategy[],
588e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // otherwise it is determine by current state
589e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // (device connected,phone state, force use, a2dp output...)
590e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // This allows to:
591e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //  1 speed up process when the state is stable (when starting or stopping an output)
592e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //  2 access to either current device selection (fromCache == true) or
593e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // "future" device selection (fromCache == false) when called from a context
594e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //  where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
595e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //  before updateDevicesAndOutputs() is called.
596e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
597e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                     bool fromCache);
598e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
599e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // change the route of the specified output. Returns the number of ms we have slept to
600e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // allow new routing to take effect in certain cases.
601e6b8b27088f461957b4cbc51adbc8b01c41e9de2Hochi Huang        virtual uint32_t setOutputDevice(audio_io_handle_t output,
602e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             audio_devices_t device,
603e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             bool force = false,
6046a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                             int delayMs = 0,
6050fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi                             audio_patch_handle_t *patchHandle = NULL,
6060fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi                             const char* address = NULL);
607951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        status_t resetOutputDevice(audio_io_handle_t output,
6086a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                   int delayMs = 0,
6096a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                   audio_patch_handle_t *patchHandle = NULL);
610951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        status_t setInputDevice(audio_io_handle_t input,
611951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                audio_devices_t device,
6126a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                bool force = false,
6136a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                audio_patch_handle_t *patchHandle = NULL);
6146a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        status_t resetInputDevice(audio_io_handle_t input,
6156a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                                  audio_patch_handle_t *patchHandle = NULL);
616e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
617e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // select input device corresponding to requested audio source
618c73ca6ef04136f28306784ad35f444538f081957Eric Laurent        virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
619e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
620e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // return io handle of active input or 0 if no input is active
621e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //    Only considers inputs from physical devices (e.g. main mic, headset mic) when
622e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //    ignoreVirtualInputs is true.
623e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
624e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
625df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent        uint32_t activeInputsCount() const;
626df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent
627e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // initialize volume curves for each strategy and device category
628e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void initializeVolumeCurves();
629e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
630e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // compute the actual volume for a given stream according to the requested index and a particular
631e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // device
6323b73df74357b33869b39a1d69427673c780bd805Eric Laurent        virtual float computeVolume(audio_stream_type_t stream, int index,
6333b73df74357b33869b39a1d69427673c780bd805Eric Laurent                                    audio_io_handle_t output, audio_devices_t device);
634e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
635e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // check that volume change is permitted, compute and send new volume to audio hardware
636327cb70dcbf3a1f1679aeafaaa62d8532abea86dHochi Huang        virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
637327cb70dcbf3a1f1679aeafaaa62d8532abea86dHochi Huang                                           audio_io_handle_t output,
638327cb70dcbf3a1f1679aeafaaa62d8532abea86dHochi Huang                                           audio_devices_t device,
639327cb70dcbf3a1f1679aeafaaa62d8532abea86dHochi Huang                                           int delayMs = 0, bool force = false);
640e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
641e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // apply all stream volumes to the specified output and device
642e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
643e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
644e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Mute or unmute all streams handled by the specified strategy on the specified output
645e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void setStrategyMute(routing_strategy strategy,
646e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             bool on,
647e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             audio_io_handle_t output,
648e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             int delayMs = 0,
649e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                             audio_devices_t device = (audio_devices_t)0);
650e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
651e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Mute or unmute the stream on the specified output
6523b73df74357b33869b39a1d69427673c780bd805Eric Laurent        void setStreamMute(audio_stream_type_t stream,
653e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                           bool on,
654e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                           audio_io_handle_t output,
655e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                           int delayMs = 0,
656e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                           audio_devices_t device = (audio_devices_t)0);
657e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
658e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // handle special cases for sonification strategy while in call: mute streams or replace by
659e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // a special tone in the device used for communication
6603b73df74357b33869b39a1d69427673c780bd805Eric Laurent        void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
661e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
662e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // true if device is in a telephony or VoIP call
663e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual bool isInCall();
664e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
665e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // true if given state represents a device in a telephony or VoIP call
666e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual bool isStateInCall(int state);
667e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
668e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // when a device is connected, checks if an open output can be routed
669e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // to this device. If none is open, tries to open one of the available outputs.
670e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Returns an output suitable to this device or 0.
671e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // when a device is disconnected, checks if an output is not used any more and
672e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // returns its handle if any.
673e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // transfers the audio tracks and effects from one output thread to another accordingly.
674f17026dfef596cf1c8008fda20f1f2ad23a3df3aJean-Michel Trivi        status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
6753b73df74357b33869b39a1d69427673c780bd805Eric Laurent                                       audio_policy_dev_state_t state,
676e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                       SortedVector<audio_io_handle_t>& outputs,
6773a4311c68348f728558e87b5db67d47605783890Eric Laurent                                       const String8 address);
678e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
679d46929666d7e4b1cad45afd7dcb883ec4dd2d49fEric Laurent        status_t checkInputsForDevice(audio_devices_t device,
680d46929666d7e4b1cad45afd7dcb883ec4dd2d49fEric Laurent                                      audio_policy_dev_state_t state,
681d46929666d7e4b1cad45afd7dcb883ec4dd2d49fEric Laurent                                      SortedVector<audio_io_handle_t>& inputs,
682d46929666d7e4b1cad45afd7dcb883ec4dd2d49fEric Laurent                                      const String8 address);
683d46929666d7e4b1cad45afd7dcb883ec4dd2d49fEric Laurent
684e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // close an output and its companion duplicating output.
685e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void closeOutput(audio_io_handle_t output);
686e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
68705b90f833337ab5f7b16509e5f1d339a04eb5bf6Eric Laurent        // close an input.
68805b90f833337ab5f7b16509e5f1d339a04eb5bf6Eric Laurent        void closeInput(audio_io_handle_t input);
68905b90f833337ab5f7b16509e5f1d339a04eb5bf6Eric Laurent
690e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // checks and if necessary changes outputs used for all strategies.
691e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // must be called every time a condition that affects the output choice for a given strategy
692e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // changes: connected device, phone state, force use...
693e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Must be called before updateDevicesAndOutputs()
694e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void checkOutputForStrategy(routing_strategy strategy);
695e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
696e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Same as checkOutputForStrategy() but for a all strategies in order of priority
697e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void checkOutputForAllStrategies();
698e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
699e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // manages A2DP output suspend/restore according to phone state and BT SCO usage
700e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void checkA2dpSuspend();
701e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
702e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // returns the A2DP output handle if it is open or 0 otherwise
703e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        audio_io_handle_t getA2dpOutput();
704e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
705e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // selects the most appropriate device on output for current state
706e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // must be called every time a condition that affects the device choice for a given output is
707e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // changed: connected device, phone state, force use, output start, output stop..
708e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // see getDeviceForStrategy() for the use of fromCache parameter
709951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        audio_devices_t getNewOutputDevice(audio_io_handle_t output, bool fromCache);
710e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
711e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // updates cache of device used by all strategies (mDeviceForStrategy[])
712e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // must be called every time a condition that affects the device choice for a given strategy is
713e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // changed: connected device, phone state, force use...
714e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
715e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent         // Must be called after checkOutputForAllStrategies()
716e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void updateDevicesAndOutputs();
717e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
718951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        // selects the most appropriate device on input for current state
719951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        audio_devices_t getNewInputDevice(audio_io_handle_t input);
720951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
721e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual uint32_t getMaxEffectsCpuLoad();
722e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual uint32_t getMaxEffectsMemory();
723e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#ifdef AUDIO_POLICY_TEST
724e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        virtual     bool        threadLoop();
725e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                    void        exit();
726e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        int testOutputIndex(audio_io_handle_t output);
727e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#endif //AUDIO_POLICY_TEST
728e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
7291f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        status_t setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled);
730e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
731e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // returns the category the device belongs to with regard to volume curve management
732e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static device_category getDeviceCategory(audio_devices_t device);
733e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
734e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // extract one device relevant for volume control from multiple device selection
735e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static audio_devices_t getDeviceForVolume(audio_devices_t device);
736e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
737e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
7381f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs);
739e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
740e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                           SortedVector<audio_io_handle_t>& outputs2);
741e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
742e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // mute/unmute strategies using an incompatible device combination
743e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // if muting, wait for the audio in pcm buffer to be drained before proceeding
744e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // if unmuting, unmute only after the specified delay
745e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Returns the number of ms waited
74618f2f90cff65c384a4e8292cc3296dafafb43d52Hochi Huang        virtual uint32_t  checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
747e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                            audio_devices_t prevDevice,
748e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                            uint32_t delayMs);
749e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
750e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
7518838a3895c365d443ee22e169ccf45956780c081Eric Laurent                                       audio_output_flags_t flags,
7528838a3895c365d443ee22e169ccf45956780c081Eric Laurent                                       audio_format_t format);
753cbd48023d0a0e3fd59955011538c0087a439f905Glenn Kasten        // samplingRate parameter is an in/out and so may be modified
754951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        sp<IOProfile> getInputProfile(audio_devices_t device,
755275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                      String8 address,
756275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                      uint32_t& samplingRate,
757275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                      audio_format_t format,
758275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                      audio_channel_mask_t channelMask,
759275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent                                      audio_input_flags_t flags);
760951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
761e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                       uint32_t samplingRate,
762e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                       audio_format_t format,
763e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                       audio_channel_mask_t channelMask,
764e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                                       audio_output_flags_t flags);
765e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
766e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
767e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
768e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        bool isNonOffloadableEffectEnabled();
769e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
770045e710f8ce7ce1679b9b5f0adbd3290c4cc6f54Hochi Huang        virtual status_t addAudioPatch(audio_patch_handle_t handle,
7716a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent                               const sp<AudioPatch>& patch);
772045e710f8ce7ce1679b9b5f0adbd3290c4cc6f54Hochi Huang        virtual status_t removeAudioPatch(audio_patch_handle_t handle);
7736a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent
7741f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        sp<AudioOutputDescriptor> getOutputFromId(audio_port_handle_t id) const;
7751f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        sp<AudioInputDescriptor> getInputFromId(audio_port_handle_t id) const;
7761f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        sp<HwModule> getModuleForDevice(audio_devices_t device) const;
7771f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        sp<HwModule> getModuleFromName(const char *name) const;
778c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent        audio_devices_t availablePrimaryOutputDevices();
779c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent        audio_devices_t availablePrimaryInputDevices();
780c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent
781c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent        void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
782c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent
783e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //
784e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Audio policy configuration file parsing (audio_policy.conf)
785e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //
786e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static uint32_t stringToEnum(const struct StringToEnum *table,
787e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                     size_t size,
788e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                     const char *name);
7893a4311c68348f728558e87b5db67d47605783890Eric Laurent        static const char *enumToString(const struct StringToEnum *table,
7903a4311c68348f728558e87b5db67d47605783890Eric Laurent                                      size_t size,
7913a4311c68348f728558e87b5db67d47605783890Eric Laurent                                      uint32_t value);
792e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static bool stringToBool(const char *value);
7935dbe47139713292bf45bbf4f1a7af0835a5ff368Eric Laurent        static uint32_t parseOutputFlagNames(char *name);
7945dbe47139713292bf45bbf4f1a7af0835a5ff368Eric Laurent        static uint32_t parseInputFlagNames(char *name);
795e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static audio_devices_t parseDeviceNames(char *name);
796e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void loadHwModule(cnode *root);
797e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void loadHwModules(cnode *root);
7981f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        void loadGlobalConfig(cnode *root, const sp<HwModule>& module);
799e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        status_t loadAudioPolicyConfig(const char *path);
800e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        void defaultAudioPolicyConfig(void);
801e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
802e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
8036a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        uid_t mUidCached;
804e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        AudioPolicyClientInterface *mpClientInterface;  // audio policy client interface
805e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        audio_io_handle_t mPrimaryOutput;              // primary output handle
806e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // list of descriptors for outputs currently opened
8071f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mOutputs;
808e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // copy of mOutputs before setDeviceConnectionState() opens new outputs
809e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // reset to mOutputs when updateDevicesAndOutputs() is called.
8101f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mPreviousOutputs;
8111f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        DefaultKeyedVector<audio_io_handle_t, sp<AudioInputDescriptor> > mInputs;     // list of input descriptors
8121afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent        DeviceVector  mAvailableOutputDevices; // all available output devices
8131afeecb88bea660b2c10b2096be0fd02433303ceEric Laurent        DeviceVector  mAvailableInputDevices;  // all available input devices
814e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        int mPhoneState;                                                    // current phone state
8153b73df74357b33869b39a1d69427673c780bd805Eric Laurent        audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT];   // current forced use configuration
816e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
8173b73df74357b33869b39a1d69427673c780bd805Eric Laurent        StreamDescriptor mStreams[AUDIO_STREAM_CNT];           // stream descriptors for volume control
818e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        bool    mLimitRingtoneVolume;                                       // limit ringtone volume to music volume if headset connected
819e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
820e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        float   mLastVoiceVolume;                                           // last voice volume value sent to audio HAL
821e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
822e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
823e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
824e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // Maximum memory allocated to audio effects in KB
825e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static const uint32_t MAX_EFFECTS_MEMORY = 512;
826e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
827e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        uint32_t mTotalEffectsMemory;  // current memory used by effects
8281f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        KeyedVector<int, sp<EffectDescriptor> > mEffects;  // list of registered audio effects
829e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        bool    mA2dpSuspended;  // true if A2DP output is suspended
8303a4311c68348f728558e87b5db67d47605783890Eric Laurent        sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
831e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
832e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                                // to boost soft sounds, used to adjust volume curves accordingly
833e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
8341f2f2230900581e5de9cf01a883e5d9338f0df94Eric Laurent        Vector < sp<HwModule> > mHwModules;
8353a4311c68348f728558e87b5db67d47605783890Eric Laurent        volatile int32_t mNextUniqueId;
8366a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        volatile int32_t mAudioPortGeneration;
8376a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent
8386a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        DefaultKeyedVector<audio_patch_handle_t, sp<AudioPatch> > mAudioPatches;
839e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
840df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent        DefaultKeyedVector<audio_session_t, audio_io_handle_t> mSoundTriggerSessions;
841df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871Eric Laurent
842c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent        sp<AudioPatch> mCallTxPatch;
843c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent        sp<AudioPatch> mCallRxPatch;
844c2730ba7c5e9559b7499ef5e0d7742deb18c5110Eric Laurent
845d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
846d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
847d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        enum {
848d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi            STARTING_OUTPUT,
849d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi            STARTING_BEACON,
850d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi            STOPPING_OUTPUT,
851d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi            STOPPING_BEACON
852d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        };
853d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        uint32_t mBeaconMuteRefCount;   // ref count for stream that would mute beacon
854d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
855d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        bool mBeaconMuted;              // has STREAM_TTS been muted
856d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi
857275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        // custom mix entry in mPolicyMixes
858275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        class AudioPolicyMix : public RefBase {
859275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        public:
860275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            AudioPolicyMix() {}
861275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent
862275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            AudioMix    mMix;                   // Audio policy mix descriptor
863275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent            sp<AudioOutputDescriptor> mOutput;  // Corresponding output stream
864275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        };
865275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        DefaultKeyedVector<String8, sp<AudioPolicyMix> > mPolicyMixes; // list of registered mixes
866275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent
867275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent
868e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#ifdef AUDIO_POLICY_TEST
869e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        Mutex   mLock;
870e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        Condition mWaitWorkCV;
871e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
872e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        int             mCurOutput;
873e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        bool            mDirectOutput;
874e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
875e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        int             mTestInput;
876e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        uint32_t        mTestDevice;
877e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        uint32_t        mTestSamplingRate;
878e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        uint32_t        mTestFormat;
879e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        uint32_t        mTestChannels;
880e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        uint32_t        mTestLatencyMs;
881e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent#endif //AUDIO_POLICY_TEST
882e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
883e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent                int indexInUi);
884e6b8b27088f461957b4cbc51adbc8b01c41e9de2Hochi Huang        static bool isVirtualInputDevice(audio_devices_t device);
885e6b8b27088f461957b4cbc51adbc8b01c41e9de2Hochi Huang        uint32_t nextUniqueId();
886e6b8b27088f461957b4cbc51adbc8b01c41e9de2Hochi Huang        uint32_t nextAudioPortGeneration();
887327cb70dcbf3a1f1679aeafaaa62d8532abea86dHochi Huangprivate:
888e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        // updates device caching and output for streams that can influence the
889e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent        //    routing of notifications
8903b73df74357b33869b39a1d69427673c780bd805Eric Laurent        void handleNotificationRoutingForStream(audio_stream_type_t stream);
8910fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi        static bool deviceDistinguishesOnAddress(audio_devices_t device);
8920fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi        // find the outputs on a given output descriptor that have the given address.
8930fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi        // to be called on an AudioOutputDescriptor whose supported devices (as defined
8940fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi        //   in mProfile->mSupportedDevices) matches the device whose address is to be matched.
8950fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi        // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
8960fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi        //   where addresses are used to distinguish between one connected device and another.
8970fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi        void findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
8983190e67d5c80c1e39e3be91784110af1180cd182keunyoung                const audio_devices_t device /*in*/,
8990fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi                const String8 address /*in*/,
9000fb47759256ecdaedbc34c880238bc9d102ef160Jean-Michel Trivi                SortedVector<audio_io_handle_t>& outputs /*out*/);
9016a94d69dc4f32abb53c466a96f905bb199be6417Eric Laurent        uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
9025bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi        // internal method to return the output handle for the given device and format
9035bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi        audio_io_handle_t getOutputForDevice(
9045bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi                audio_devices_t device,
905e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                audio_session_t session,
9065bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi                audio_stream_type_t stream,
9075bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi                uint32_t samplingRate,
9085bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi                audio_format_t format,
9095bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi                audio_channel_mask_t channelMask,
9105bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi                audio_output_flags_t flags,
9115bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi                const audio_offload_info_t *offloadInfo);
9125bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi        // internal function to derive a stream type value from audio attributes
9135bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi        audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
914d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        // return true if any output is playing anything besides the stream to ignore
915d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        bool isAnyOutputActive(audio_stream_type_t streamToIgnore);
916d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
917d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        // returns 0 if no mute/unmute event happened, the largest latency of the device where
918d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        //   the mute/unmute happened
919d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        uint32_t handleEventForBeacon(int event);
920d9cfeb447356cb6334379eaf5da1e49424eb5979Jean-Michel Trivi        uint32_t setBeaconMute(bool mute);
921e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent        bool     isValidAttributes(const audio_attributes_t *paa);
922c73ca6ef04136f28306784ad35f444538f081957Eric Laurent
923c73ca6ef04136f28306784ad35f444538f081957Eric Laurent        // select input device corresponding to requested audio source and return associated policy
924c73ca6ef04136f28306784ad35f444538f081957Eric Laurent        // mix if any. Calls getDeviceForInputSource().
925c73ca6ef04136f28306784ad35f444538f081957Eric Laurent        audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
926c73ca6ef04136f28306784ad35f444538f081957Eric Laurent                                                        AudioMix **policyMix = NULL);
927c73ca6ef04136f28306784ad35f444538f081957Eric Laurent
928c73ca6ef04136f28306784ad35f444538f081957Eric Laurent        // Called by setDeviceConnectionState().
929c73ca6ef04136f28306784ad35f444538f081957Eric Laurent        status_t setDeviceConnectionStateInt(audio_devices_t device,
930c73ca6ef04136f28306784ad35f444538f081957Eric Laurent                                                          audio_policy_dev_state_t state,
931c73ca6ef04136f28306784ad35f444538f081957Eric Laurent                                                          const char *device_address);
932a1d525fbf2c1e0b2c61e5d29f338b0a0d8823436Eric Laurent        sp<DeviceDescriptor>  getDeviceDescriptor(const audio_devices_t device,
933a1d525fbf2c1e0b2c61e5d29f338b0a0d8823436Eric Laurent                                                  const char *device_address);
934a1d525fbf2c1e0b2c61e5d29f338b0a0d8823436Eric Laurent
935e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent};
936e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent
937e552edb33fb5873179ae0a46d9579d1103eb13c6Eric Laurent};
938