blob: d9b5ada5671c69ac81ecad712a1fd75b71f26186 [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#include <media/ToneGenerator.h>
22#include <utils/Vector.h>
Glenn Kastend2dcb082011-02-03 16:55:26 -080023#include <binder/BinderService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024
Dima Zavin64760242011-05-11 14:15:23 -070025#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070026#include <hardware/audio_policy.h>
27#include <hardware/audio_policy_hal.h>
28
Mathias Agopian65ab4712010-07-14 17:59:35 -070029namespace android {
30
31class String8;
32
33// ----------------------------------------------------------------------------
34
Glenn Kastend2dcb082011-02-03 16:55:26 -080035class AudioPolicyService :
36 public BinderService<AudioPolicyService>,
37 public BnAudioPolicyService,
Dima Zavinfce7a472011-04-19 22:30:36 -070038// public AudioPolicyClientInterface,
Eric Laurentde070132010-07-13 04:45:46 -070039 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070040{
Glenn Kastend2dcb082011-02-03 16:55:26 -080041 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
43public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080044 // for BinderService
45 static const char *getServiceName() { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070046
47 virtual status_t dump(int fd, const Vector<String16>& args);
48
49 //
50 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
51 //
52
Dima Zavinfce7a472011-04-19 22:30:36 -070053 virtual status_t setDeviceConnectionState(audio_devices_t device,
54 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -070055 const char *device_address);
Dima Zavinfce7a472011-04-19 22:30:36 -070056 virtual audio_policy_dev_state_t getDeviceConnectionState(
57 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070058 const char *device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -070059 virtual status_t setPhoneState(int state);
60 virtual status_t setRingerMode(uint32_t mode, uint32_t mask);
Dima Zavinfce7a472011-04-19 22:30:36 -070061 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,
Mathias Agopian65ab4712010-07-14 17:59:35 -070064 uint32_t samplingRate = 0,
Dima Zavinfce7a472011-04-19 22:30:36 -070065 uint32_t format = AUDIO_FORMAT_DEFAULT,
Mathias Agopian65ab4712010-07-14 17:59:35 -070066 uint32_t channels = 0,
Dima Zavinfce7a472011-04-19 22:30:36 -070067 audio_policy_output_flags_t flags =
68 AUDIO_POLICY_OUTPUT_FLAG_INDIRECT);
Eric Laurentde070132010-07-13 04:45:46 -070069 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070070 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070071 int session = 0);
72 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070073 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070074 int session = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070075 virtual void releaseOutput(audio_io_handle_t output);
76 virtual audio_io_handle_t getInput(int inputSource,
77 uint32_t samplingRate = 0,
Dima Zavinfce7a472011-04-19 22:30:36 -070078 uint32_t format = AUDIO_FORMAT_DEFAULT,
Mathias Agopian65ab4712010-07-14 17:59:35 -070079 uint32_t channels = 0,
Dima Zavinfce7a472011-04-19 22:30:36 -070080 audio_in_acoustics_t acoustics =
81 (audio_in_acoustics_t)0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070082 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);
Dima Zavinfce7a472011-04-19 22:30:36 -070085 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070086 int indexMin,
87 int indexMax);
Dima Zavinfce7a472011-04-19 22:30:36 -070088 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, int index);
89 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, int *index);
Mathias Agopian65ab4712010-07-14 17:59:35 -070090
Dima Zavinfce7a472011-04-19 22:30:36 -070091 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
92 virtual uint32_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -070093
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);
Eric Laurenteda6c362011-02-02 09:33:30 -0800101 virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
Eric Laurentde070132010-07-13 04:45:46 -0700102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103 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 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700113 // 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.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700116 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700117 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,
Eric Laurentde070132010-07-13 04:45:46 -0700122 float volume,
123 audio_io_handle_t output,
124 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700125 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700126 virtual status_t stopTone();
127 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700128
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);
Dima Zavinfce7a472011-04-19 22:30:36 -0700167 status_t parametersCommand(int ioHandle, const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168 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
Eric Laurenteda6c362011-02-02 09:33:30 -0800225 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
226 // device connection state or routing
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread
228 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread
Dima Zavinfce7a472011-04-19 22:30:36 -0700229
230 struct audio_policy_device *mpAudioPolicyDev;
231 struct audio_policy *mpAudioPolicy;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232};
233
234}; // namespace android
235
236#endif // ANDROID_AUDIOPOLICYSERVICE_H