AudioPolicyService.h revision 7c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIOPOLICYSERVICE_H
18#define ANDROID_AUDIOPOLICYSERVICE_H
19
20#include <cutils/misc.h>
21#include <cutils/config_utils.h>
22#include <utils/Vector.h>
23#include <utils/SortedVector.h>
24#include <binder/BinderService.h>
25#include <system/audio.h>
26#include <system/audio_policy.h>
27#include <hardware/audio_policy.h>
28#include <media/IAudioPolicyService.h>
29#include <media/ToneGenerator.h>
30#include <media/AudioEffect.h>
31
32namespace android {
33
34class String8;
35
36// ----------------------------------------------------------------------------
37
38class AudioPolicyService :
39    public BinderService<AudioPolicyService>,
40    public BnAudioPolicyService,
41//    public AudioPolicyClientInterface,
42    public IBinder::DeathRecipient
43{
44    friend class BinderService<AudioPolicyService>;
45
46public:
47    // for BinderService
48    static const char *getServiceName() { return "media.audio_policy"; }
49
50    virtual status_t    dump(int fd, const Vector<String16>& args);
51
52    //
53    // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
54    //
55
56    virtual status_t setDeviceConnectionState(audio_devices_t device,
57                                              audio_policy_dev_state_t state,
58                                              const char *device_address);
59    virtual audio_policy_dev_state_t getDeviceConnectionState(
60                                                                audio_devices_t device,
61                                                                const char *device_address);
62    virtual status_t setPhoneState(int state);
63    virtual status_t setRingerMode(uint32_t mode, uint32_t mask);
64    virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
65    virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
66    virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
67                                        uint32_t samplingRate = 0,
68                                        uint32_t format = AUDIO_FORMAT_DEFAULT,
69                                        uint32_t channels = 0,
70                                        audio_policy_output_flags_t flags =
71                                            AUDIO_POLICY_OUTPUT_FLAG_INDIRECT);
72    virtual status_t startOutput(audio_io_handle_t output,
73                                 audio_stream_type_t stream,
74                                 int session = 0);
75    virtual status_t stopOutput(audio_io_handle_t output,
76                                audio_stream_type_t stream,
77                                int session = 0);
78    virtual void releaseOutput(audio_io_handle_t output);
79    virtual audio_io_handle_t getInput(int inputSource,
80                                    uint32_t samplingRate = 0,
81                                    uint32_t format = AUDIO_FORMAT_DEFAULT,
82                                    uint32_t channels = 0,
83                                    audio_in_acoustics_t acoustics =
84                                            (audio_in_acoustics_t)0,
85                                    int audioSession = 0);
86    virtual status_t startInput(audio_io_handle_t input);
87    virtual status_t stopInput(audio_io_handle_t input);
88    virtual void releaseInput(audio_io_handle_t input);
89    virtual status_t initStreamVolume(audio_stream_type_t stream,
90                                      int indexMin,
91                                      int indexMax);
92    virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, int index);
93    virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, int *index);
94
95    virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
96    virtual uint32_t getDevicesForStream(audio_stream_type_t stream);
97
98    virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc);
99    virtual status_t registerEffect(effect_descriptor_t *desc,
100                                    audio_io_handle_t io,
101                                    uint32_t strategy,
102                                    int session,
103                                    int id);
104    virtual status_t unregisterEffect(int id);
105    virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
106
107    virtual     status_t    onTransact(
108                                uint32_t code,
109                                const Parcel& data,
110                                Parcel* reply,
111                                uint32_t flags);
112
113    // IBinder::DeathRecipient
114    virtual     void        binderDied(const wp<IBinder>& who);
115
116    //
117    // Helpers for the struct audio_policy_service_ops implementation.
118    // This is used by the audio policy manager for certain operations that
119    // are implemented by the policy service.
120    //
121    virtual void setParameters(audio_io_handle_t ioHandle,
122                               const char *keyValuePairs,
123                               int delayMs);
124
125    virtual status_t setStreamVolume(audio_stream_type_t stream,
126                                     float volume,
127                                     audio_io_handle_t output,
128                                     int delayMs = 0);
129    virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
130    virtual status_t stopTone();
131    virtual status_t setVoiceVolume(float volume, int delayMs = 0);
132
133private:
134                        AudioPolicyService();
135    virtual             ~AudioPolicyService();
136
137            status_t dumpInternals(int fd);
138
139    // Thread used for tone playback and to send audio config commands to audio flinger
140    // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone()
141    // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause
142    // calls to AudioPolicyService and an attempt to lock mLock.
143    // For audio config commands, it is necessary because audio flinger requires that the calling process (user)
144    // has permission to modify audio settings.
145    class AudioCommandThread : public Thread {
146        class AudioCommand;
147    public:
148
149        // commands for tone AudioCommand
150        enum {
151            START_TONE,
152            STOP_TONE,
153            SET_VOLUME,
154            SET_PARAMETERS,
155            SET_VOICE_VOLUME
156        };
157
158        AudioCommandThread (String8 name);
159        virtual             ~AudioCommandThread();
160
161                    status_t    dump(int fd);
162
163        // Thread virtuals
164        virtual     void        onFirstRef();
165        virtual     bool        threadLoop();
166
167                    void        exit();
168                    void        startToneCommand(int type = 0, int stream = 0);
169                    void        stopToneCommand();
170                    status_t    volumeCommand(int stream, float volume, int output, int delayMs = 0);
171                    status_t    parametersCommand(int ioHandle, const char *keyValuePairs, int delayMs = 0);
172                    status_t    voiceVolumeCommand(float volume, int delayMs = 0);
173                    void        insertCommand_l(AudioCommand *command, int delayMs = 0);
174
175    private:
176        // descriptor for requested tone playback event
177        class AudioCommand {
178
179        public:
180            AudioCommand()
181            : mCommand(-1) {}
182
183            void dump(char* buffer, size_t size);
184
185            int mCommand;   // START_TONE, STOP_TONE ...
186            nsecs_t mTime;  // time stamp
187            Condition mCond; // condition for status return
188            status_t mStatus; // command status
189            bool mWaitStatus; // true if caller is waiting for status
190            void *mParam;     // command parameter (ToneData, VolumeData, ParametersData)
191        };
192
193        class ToneData {
194        public:
195            int mType;      // tone type (START_TONE only)
196            int mStream;    // stream type (START_TONE only)
197        };
198
199        class VolumeData {
200        public:
201            int mStream;
202            float mVolume;
203            int mIO;
204        };
205
206        class ParametersData {
207        public:
208            int mIO;
209            String8 mKeyValuePairs;
210        };
211
212        class VoiceVolumeData {
213        public:
214            float mVolume;
215        };
216
217        Mutex   mLock;
218        Condition mWaitWorkCV;
219        Vector <AudioCommand *> mAudioCommands; // list of pending commands
220        ToneGenerator *mpToneGenerator;     // the tone generator
221        AudioCommand mLastCommand;          // last processed command (used by dump)
222        String8 mName;                      // string used by wake lock fo delayed commands
223    };
224
225    class EffectDesc {
226    public:
227        EffectDesc() {}
228        virtual ~EffectDesc() {}
229        char *mName;
230        effect_uuid_t mUuid;
231        Vector <effect_param_t *> mParams;
232    };
233
234    class InputSourceDesc {
235    public:
236        InputSourceDesc() {}
237        virtual ~InputSourceDesc() {}
238        Vector <EffectDesc *> mEffects;
239    };
240
241
242    class InputDesc {
243    public:
244        InputDesc() {}
245        virtual ~InputDesc() {}
246        int mSessionId;
247        Vector< sp<AudioEffect> >mEffects;
248    };
249
250    static const char *kInputSourceNames[AUDIO_SOURCE_CNT -1];
251
252    void setPreProcessorEnabled(InputDesc *inputDesc, bool enabled);
253    status_t loadPreProcessorConfig(const char *path);
254    status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
255    EffectDesc *loadEffect(cnode *root);
256    status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
257    audio_source_t inputSourceNameToEnum(const char *name);
258    InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
259    void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
260    effect_param_t *loadEffectParameter(cnode *root);
261    size_t readParamValue(cnode *node,
262                          char *param,
263                          size_t *curSize,
264                          size_t *totSize);
265    size_t growParamSize(char *param,
266                         size_t size,
267                         size_t *curSize,
268                         size_t *totSize);
269
270    // Internal dump utilities.
271    status_t dumpPermissionDenial(int fd);
272
273
274    mutable Mutex mLock;    // prevents concurrent access to AudioPolicy manager functions changing
275                            // device connection state  or routing
276    sp <AudioCommandThread> mAudioCommandThread;    // audio commands thread
277    sp <AudioCommandThread> mTonePlaybackThread;     // tone playback thread
278    struct audio_policy_device *mpAudioPolicyDev;
279    struct audio_policy *mpAudioPolicy;
280    KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
281    KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
282};
283
284}; // namespace android
285
286#endif // ANDROID_AUDIOPOLICYSERVICE_H
287