blob: faad893ea96897f59f9113c5a049fc4b8c3b59fd [file] [log] [blame]
Eric Laurenta553c252009-07-17 12:17:14 -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>
Eric Laurent327c27b2009-08-27 00:48:47 -070023#include <utils/Vector.h>
Glenn Kasten3515a332011-02-03 16:55:26 -080024#include <binder/BinderService.h>
Eric Laurenta553c252009-07-17 12:17:14 -070025
26namespace android {
27
28class String8;
29
30// ----------------------------------------------------------------------------
31
Glenn Kasten3515a332011-02-03 16:55:26 -080032class AudioPolicyService :
33 public BinderService<AudioPolicyService>,
34 public BnAudioPolicyService,
35 public AudioPolicyClientInterface,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070036 public IBinder::DeathRecipient
Eric Laurenta553c252009-07-17 12:17:14 -070037{
Glenn Kasten3515a332011-02-03 16:55:26 -080038 friend class BinderService<AudioPolicyService>;
Eric Laurenta553c252009-07-17 12:17:14 -070039
40public:
Glenn Kasten3515a332011-02-03 16:55:26 -080041 // for BinderService
42 static const char *getServiceName() { return "media.audio_policy"; }
Eric Laurenta553c252009-07-17 12:17:14 -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 Laurent8ed6ed02010-07-13 04:45:46 -070053 virtual AudioSystem::device_connection_state getDeviceConnectionState(
54 AudioSystem::audio_devices device,
55 const char *device_address);
Eric Laurenta553c252009-07-17 12:17:14 -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 Laurent8ed6ed02010-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);
Eric Laurenta553c252009-07-17 12:17:14 -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 Laurent8ed6ed02010-07-13 04:45:46 -070077 AudioSystem::audio_in_acoustics acoustics =
78 (AudioSystem::audio_in_acoustics)0);
Eric Laurenta553c252009-07-17 12:17:14 -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 Laurent8ed6ed02010-07-13 04:45:46 -070088 virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream);
Glenn Kasten8b4b97a2011-02-04 13:54:26 -080089 virtual uint32_t getDevicesForStream(AudioSystem::stream_type stream);
Eric Laurent8ed6ed02010-07-13 04:45:46 -070090
91 virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc);
92 virtual status_t registerEffect(effect_descriptor_t *desc,
93 audio_io_handle_t output,
94 uint32_t strategy,
95 int session,
96 int id);
97 virtual status_t unregisterEffect(int id);
Eric Laurent25101b02011-02-02 09:33:30 -080098 virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
Eric Laurent8ed6ed02010-07-13 04:45:46 -070099
Eric Laurenta553c252009-07-17 12:17:14 -0700100 virtual status_t onTransact(
101 uint32_t code,
102 const Parcel& data,
103 Parcel* reply,
104 uint32_t flags);
105
106 // IBinder::DeathRecipient
107 virtual void binderDied(const wp<IBinder>& who);
108
109 //
110 // AudioPolicyClientInterface
111 //
112 virtual audio_io_handle_t openOutput(uint32_t *pDevices,
113 uint32_t *pSamplingRate,
114 uint32_t *pFormat,
115 uint32_t *pChannels,
116 uint32_t *pLatencyMs,
117 AudioSystem::output_flags flags);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700118 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
119 audio_io_handle_t output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700120 virtual status_t closeOutput(audio_io_handle_t output);
121 virtual status_t suspendOutput(audio_io_handle_t output);
122 virtual status_t restoreOutput(audio_io_handle_t output);
123 virtual audio_io_handle_t openInput(uint32_t *pDevices,
124 uint32_t *pSamplingRate,
125 uint32_t *pFormat,
126 uint32_t *pChannels,
127 uint32_t acoustics);
128 virtual status_t closeInput(audio_io_handle_t input);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700129 virtual status_t setStreamVolume(AudioSystem::stream_type stream,
130 float volume,
131 audio_io_handle_t output,
132 int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700133 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700134 virtual void setParameters(audio_io_handle_t ioHandle,
135 const String8& keyValuePairs,
136 int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700137 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
138 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream);
139 virtual status_t stopTone();
Eric Laurent415f3e22009-10-21 08:14:22 -0700140 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700141 virtual status_t moveEffects(int session,
142 audio_io_handle_t srcOutput,
143 audio_io_handle_t dstOutput);
Eric Laurenta553c252009-07-17 12:17:14 -0700144
145private:
146 AudioPolicyService();
147 virtual ~AudioPolicyService();
148
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800149 status_t dumpInternals(int fd);
150
Eric Laurenta553c252009-07-17 12:17:14 -0700151 // Thread used for tone playback and to send audio config commands to audio flinger
152 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone()
153 // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause
154 // calls to AudioPolicyService and an attempt to lock mLock.
155 // For audio config commands, it is necessary because audio flinger requires that the calling process (user)
156 // has permission to modify audio settings.
157 class AudioCommandThread : public Thread {
Eric Laurent327c27b2009-08-27 00:48:47 -0700158 class AudioCommand;
Eric Laurenta553c252009-07-17 12:17:14 -0700159 public:
160
161 // commands for tone AudioCommand
162 enum {
163 START_TONE,
164 STOP_TONE,
165 SET_VOLUME,
Eric Laurent415f3e22009-10-21 08:14:22 -0700166 SET_PARAMETERS,
167 SET_VOICE_VOLUME
Eric Laurenta553c252009-07-17 12:17:14 -0700168 };
169
Eric Laurentcef3cd72009-12-10 01:03:50 -0800170 AudioCommandThread (String8 name);
Eric Laurenta553c252009-07-17 12:17:14 -0700171 virtual ~AudioCommandThread();
172
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800173 status_t dump(int fd);
174
Eric Laurenta553c252009-07-17 12:17:14 -0700175 // Thread virtuals
176 virtual void onFirstRef();
177 virtual bool threadLoop();
178
179 void exit();
180 void startToneCommand(int type = 0, int stream = 0);
181 void stopToneCommand();
Eric Laurent327c27b2009-08-27 00:48:47 -0700182 status_t volumeCommand(int stream, float volume, int output, int delayMs = 0);
183 status_t parametersCommand(int ioHandle, const String8& keyValuePairs, int delayMs = 0);
Eric Laurent415f3e22009-10-21 08:14:22 -0700184 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurent327c27b2009-08-27 00:48:47 -0700185 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700186
187 private:
188 // descriptor for requested tone playback event
189 class AudioCommand {
Eric Laurent3fdb1262009-11-07 00:01:32 -0800190
Eric Laurenta553c252009-07-17 12:17:14 -0700191 public:
Eric Laurent3fdb1262009-11-07 00:01:32 -0800192 AudioCommand()
193 : mCommand(-1) {}
194
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800195 void dump(char* buffer, size_t size);
196
Eric Laurenta553c252009-07-17 12:17:14 -0700197 int mCommand; // START_TONE, STOP_TONE ...
Eric Laurent327c27b2009-08-27 00:48:47 -0700198 nsecs_t mTime; // time stamp
199 Condition mCond; // condition for status return
200 status_t mStatus; // command status
201 bool mWaitStatus; // true if caller is waiting for status
202 void *mParam; // command parameter (ToneData, VolumeData, ParametersData)
Eric Laurenta553c252009-07-17 12:17:14 -0700203 };
204
205 class ToneData {
206 public:
207 int mType; // tone type (START_TONE only)
208 int mStream; // stream type (START_TONE only)
209 };
210
211 class VolumeData {
212 public:
213 int mStream;
214 float mVolume;
Eric Laurentddb78e72009-07-28 08:44:33 -0700215 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700216 };
Eric Laurent415f3e22009-10-21 08:14:22 -0700217
Eric Laurenta553c252009-07-17 12:17:14 -0700218 class ParametersData {
219 public:
Eric Laurentddb78e72009-07-28 08:44:33 -0700220 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700221 String8 mKeyValuePairs;
222 };
223
Eric Laurent415f3e22009-10-21 08:14:22 -0700224 class VoiceVolumeData {
225 public:
226 float mVolume;
227 };
Eric Laurenta553c252009-07-17 12:17:14 -0700228
229 Mutex mLock;
230 Condition mWaitWorkCV;
Eric Laurent327c27b2009-08-27 00:48:47 -0700231 Vector <AudioCommand *> mAudioCommands; // list of pending commands
Eric Laurenta553c252009-07-17 12:17:14 -0700232 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurentcef3cd72009-12-10 01:03:50 -0800233 AudioCommand mLastCommand; // last processed command (used by dump)
234 String8 mName; // string used by wake lock fo delayed commands
Eric Laurenta553c252009-07-17 12:17:14 -0700235 };
236
237 // Internal dump utilities.
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800238 status_t dumpPermissionDenial(int fd);
Eric Laurenta553c252009-07-17 12:17:14 -0700239
240
Eric Laurent25101b02011-02-02 09:33:30 -0800241 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
242 // device connection state or routing
Eric Laurenta553c252009-07-17 12:17:14 -0700243 AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager
244 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread
Eric Laurent327c27b2009-08-27 00:48:47 -0700245 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurenta553c252009-07-17 12:17:14 -0700246};
247
248}; // namespace android
249
250#endif // ANDROID_AUDIOPOLICYSERVICE_H