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