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