blob: 54af1f1e026583e1023c51640ca780a2c0ce1713 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
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 <hardware_legacy/AudioPolicyInterface.h>
22#include <media/ToneGenerator.h>
23#include <utils/Vector.h>
24
25namespace android {
26
27class String8;
28
29// ----------------------------------------------------------------------------
30
Eric Laurentde070132010-07-13 04:45:46 -070031class AudioPolicyService: public BnAudioPolicyService, public AudioPolicyClientInterface,
32 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070033{
34
35public:
36 static void instantiate();
37
38 virtual status_t dump(int fd, const Vector<String16>& args);
39
40 //
41 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
42 //
43
44 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
45 AudioSystem::device_connection_state state,
46 const char *device_address);
Eric Laurentde070132010-07-13 04:45:46 -070047 virtual AudioSystem::device_connection_state getDeviceConnectionState(
48 AudioSystem::audio_devices device,
49 const char *device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -070050 virtual status_t setPhoneState(int state);
51 virtual status_t setRingerMode(uint32_t mode, uint32_t mask);
52 virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
53 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
54 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
55 uint32_t samplingRate = 0,
56 uint32_t format = AudioSystem::FORMAT_DEFAULT,
57 uint32_t channels = 0,
Eric Laurentde070132010-07-13 04:45:46 -070058 AudioSystem::output_flags flags =
59 AudioSystem::OUTPUT_FLAG_INDIRECT);
60 virtual status_t startOutput(audio_io_handle_t output,
61 AudioSystem::stream_type stream,
62 int session = 0);
63 virtual status_t stopOutput(audio_io_handle_t output,
64 AudioSystem::stream_type stream,
65 int session = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070066 virtual void releaseOutput(audio_io_handle_t output);
67 virtual audio_io_handle_t getInput(int inputSource,
68 uint32_t samplingRate = 0,
69 uint32_t format = AudioSystem::FORMAT_DEFAULT,
70 uint32_t channels = 0,
Eric Laurentde070132010-07-13 04:45:46 -070071 AudioSystem::audio_in_acoustics acoustics =
72 (AudioSystem::audio_in_acoustics)0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 virtual status_t startInput(audio_io_handle_t input);
74 virtual status_t stopInput(audio_io_handle_t input);
75 virtual void releaseInput(audio_io_handle_t input);
76 virtual status_t initStreamVolume(AudioSystem::stream_type stream,
77 int indexMin,
78 int indexMax);
79 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index);
80 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index);
81
Eric Laurentde070132010-07-13 04:45:46 -070082 virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream);
83
84 virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc);
85 virtual status_t registerEffect(effect_descriptor_t *desc,
86 audio_io_handle_t output,
87 uint32_t strategy,
88 int session,
89 int id);
90 virtual status_t unregisterEffect(int id);
Eric Laurenteda6c362011-02-02 09:33:30 -080091 virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
Eric Laurentde070132010-07-13 04:45:46 -070092
Mathias Agopian65ab4712010-07-14 17:59:35 -070093 virtual status_t onTransact(
94 uint32_t code,
95 const Parcel& data,
96 Parcel* reply,
97 uint32_t flags);
98
99 // IBinder::DeathRecipient
100 virtual void binderDied(const wp<IBinder>& who);
101
102 //
103 // AudioPolicyClientInterface
104 //
105 virtual audio_io_handle_t openOutput(uint32_t *pDevices,
106 uint32_t *pSamplingRate,
107 uint32_t *pFormat,
108 uint32_t *pChannels,
109 uint32_t *pLatencyMs,
110 AudioSystem::output_flags flags);
Eric Laurentde070132010-07-13 04:45:46 -0700111 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
112 audio_io_handle_t output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700113 virtual status_t closeOutput(audio_io_handle_t output);
114 virtual status_t suspendOutput(audio_io_handle_t output);
115 virtual status_t restoreOutput(audio_io_handle_t output);
116 virtual audio_io_handle_t openInput(uint32_t *pDevices,
117 uint32_t *pSamplingRate,
118 uint32_t *pFormat,
119 uint32_t *pChannels,
120 uint32_t acoustics);
121 virtual status_t closeInput(audio_io_handle_t input);
Eric Laurentde070132010-07-13 04:45:46 -0700122 virtual status_t setStreamVolume(AudioSystem::stream_type stream,
123 float volume,
124 audio_io_handle_t output,
125 int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700126 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output);
Eric Laurentde070132010-07-13 04:45:46 -0700127 virtual void setParameters(audio_io_handle_t ioHandle,
128 const String8& keyValuePairs,
129 int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
131 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream);
132 virtual status_t stopTone();
133 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Eric Laurentde070132010-07-13 04:45:46 -0700134 virtual status_t moveEffects(int session,
135 audio_io_handle_t srcOutput,
136 audio_io_handle_t dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137
138private:
139 AudioPolicyService();
140 virtual ~AudioPolicyService();
141
142 status_t dumpInternals(int fd);
143
144 // Thread used for tone playback and to send audio config commands to audio flinger
145 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone()
146 // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause
147 // calls to AudioPolicyService and an attempt to lock mLock.
148 // For audio config commands, it is necessary because audio flinger requires that the calling process (user)
149 // has permission to modify audio settings.
150 class AudioCommandThread : public Thread {
151 class AudioCommand;
152 public:
153
154 // commands for tone AudioCommand
155 enum {
156 START_TONE,
157 STOP_TONE,
158 SET_VOLUME,
159 SET_PARAMETERS,
160 SET_VOICE_VOLUME
161 };
162
163 AudioCommandThread (String8 name);
164 virtual ~AudioCommandThread();
165
166 status_t dump(int fd);
167
168 // Thread virtuals
169 virtual void onFirstRef();
170 virtual bool threadLoop();
171
172 void exit();
173 void startToneCommand(int type = 0, int stream = 0);
174 void stopToneCommand();
175 status_t volumeCommand(int stream, float volume, int output, int delayMs = 0);
176 status_t parametersCommand(int ioHandle, const String8& keyValuePairs, int delayMs = 0);
177 status_t voiceVolumeCommand(float volume, int delayMs = 0);
178 void insertCommand_l(AudioCommand *command, int delayMs = 0);
179
180 private:
181 // descriptor for requested tone playback event
182 class AudioCommand {
183
184 public:
185 AudioCommand()
186 : mCommand(-1) {}
187
188 void dump(char* buffer, size_t size);
189
190 int mCommand; // START_TONE, STOP_TONE ...
191 nsecs_t mTime; // time stamp
192 Condition mCond; // condition for status return
193 status_t mStatus; // command status
194 bool mWaitStatus; // true if caller is waiting for status
195 void *mParam; // command parameter (ToneData, VolumeData, ParametersData)
196 };
197
198 class ToneData {
199 public:
200 int mType; // tone type (START_TONE only)
201 int mStream; // stream type (START_TONE only)
202 };
203
204 class VolumeData {
205 public:
206 int mStream;
207 float mVolume;
208 int mIO;
209 };
210
211 class ParametersData {
212 public:
213 int mIO;
214 String8 mKeyValuePairs;
215 };
216
217 class VoiceVolumeData {
218 public:
219 float mVolume;
220 };
221
222 Mutex mLock;
223 Condition mWaitWorkCV;
224 Vector <AudioCommand *> mAudioCommands; // list of pending commands
225 ToneGenerator *mpToneGenerator; // the tone generator
226 AudioCommand mLastCommand; // last processed command (used by dump)
227 String8 mName; // string used by wake lock fo delayed commands
228 };
229
230 // Internal dump utilities.
231 status_t dumpPermissionDenial(int fd);
232
233
Eric Laurenteda6c362011-02-02 09:33:30 -0800234 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
235 // device connection state or routing
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236 AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager
237 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread
238 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread
239};
240
241}; // namespace android
242
243#endif // ANDROID_AUDIOPOLICYSERVICE_H
244
245
246
247
248
249
250
251