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