blob: 47173dd7d6b3ccfd3954faf6afb49feef280a769 [file] [log] [blame]
Eric Laurent9d91ad52009-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>
23
24namespace android {
25
26class String8;
27
28// ----------------------------------------------------------------------------
29
30class AudioPolicyService: public BnAudioPolicyService, public AudioPolicyClientInterface, public IBinder::DeathRecipient
31{
32
33public:
34 static void instantiate();
35
36 virtual status_t dump(int fd, const Vector<String16>& args);
37
38 //
39 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
40 //
41
42 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
43 AudioSystem::device_connection_state state,
44 const char *device_address);
45 virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device,
46 const char *device_address);
47 virtual status_t setPhoneState(int state);
48 virtual status_t setRingerMode(uint32_t mode, uint32_t mask);
49 virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
50 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
51 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
52 uint32_t samplingRate = 0,
53 uint32_t format = AudioSystem::FORMAT_DEFAULT,
54 uint32_t channels = 0,
55 AudioSystem::output_flags flags = AudioSystem::OUTPUT_FLAG_INDIRECT);
56 virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
57 virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
58 virtual void releaseOutput(audio_io_handle_t output);
59 virtual audio_io_handle_t getInput(int inputSource,
60 uint32_t samplingRate = 0,
61 uint32_t format = AudioSystem::FORMAT_DEFAULT,
62 uint32_t channels = 0,
63 AudioSystem::audio_in_acoustics acoustics = (AudioSystem::audio_in_acoustics)0);
64 virtual status_t startInput(audio_io_handle_t input);
65 virtual status_t stopInput(audio_io_handle_t input);
66 virtual void releaseInput(audio_io_handle_t input);
67 virtual status_t initStreamVolume(AudioSystem::stream_type stream,
68 int indexMin,
69 int indexMax);
70 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index);
71 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index);
72
73 virtual status_t onTransact(
74 uint32_t code,
75 const Parcel& data,
76 Parcel* reply,
77 uint32_t flags);
78
79 // IBinder::DeathRecipient
80 virtual void binderDied(const wp<IBinder>& who);
81
82 //
83 // AudioPolicyClientInterface
84 //
85 virtual audio_io_handle_t openOutput(uint32_t *pDevices,
86 uint32_t *pSamplingRate,
87 uint32_t *pFormat,
88 uint32_t *pChannels,
89 uint32_t *pLatencyMs,
90 AudioSystem::output_flags flags);
91 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
92 virtual status_t closeOutput(audio_io_handle_t output);
93 virtual status_t suspendOutput(audio_io_handle_t output);
94 virtual status_t restoreOutput(audio_io_handle_t output);
95 virtual audio_io_handle_t openInput(uint32_t *pDevices,
96 uint32_t *pSamplingRate,
97 uint32_t *pFormat,
98 uint32_t *pChannels,
99 uint32_t acoustics);
100 virtual status_t closeInput(audio_io_handle_t input);
101 virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output);
102 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output);
103 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
104 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
105 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream);
106 virtual status_t stopTone();
107
108private:
109 AudioPolicyService();
110 virtual ~AudioPolicyService();
111
112 static const char *sAudioPolicyLibrary;
113 static const char *sAudioPolicyGenericLibrary;
114 // Thread used for tone playback and to send audio config commands to audio flinger
115 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone()
116 // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause
117 // calls to AudioPolicyService and an attempt to lock mLock.
118 // For audio config commands, it is necessary because audio flinger requires that the calling process (user)
119 // has permission to modify audio settings.
120 class AudioCommandThread : public Thread {
121 public:
122
123 // commands for tone AudioCommand
124 enum {
125 START_TONE,
126 STOP_TONE,
127 SET_VOLUME,
128 SET_PARAMETERS
129 };
130
131 AudioCommandThread ();
132 virtual ~AudioCommandThread();
133
134 // Thread virtuals
135 virtual void onFirstRef();
136 virtual bool threadLoop();
137
138 void exit();
139 void startToneCommand(int type = 0, int stream = 0);
140 void stopToneCommand();
141 status_t volumeCommand(int stream, float volume, void *output);
142 status_t parametersCommand(void *ioHandle, const String8& keyValuePairs);
143
144 private:
145 // descriptor for requested tone playback event
146 class AudioCommand {
147 public:
148 int mCommand; // START_TONE, STOP_TONE ...
149 void *mParam;
150 };
151
152 class ToneData {
153 public:
154 int mType; // tone type (START_TONE only)
155 int mStream; // stream type (START_TONE only)
156 };
157
158 class VolumeData {
159 public:
160 int mStream;
161 float mVolume;
162 void *mIO;
163 };
164 class ParametersData {
165 public:
166 void *mIO;
167 String8 mKeyValuePairs;
168 };
169
170
171 Mutex mLock;
172 Condition mWaitWorkCV;
173 Vector<AudioCommand *> mAudioCommands; // list of pending tone events
174 Condition mCommandCond;
175 status_t mCommandStatus;
176 ToneGenerator *mpToneGenerator; // the tone generator
177 };
178
179 // Internal dump utilities.
180 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
181
182
183 Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing device
184 // connection stated our routing
185 AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager
186 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread
187 sp <AudioCommandThread> mTonePlaybacThread; // tone playback thread
188 void *mpPolicyManagerLibHandle;
189};
190
191}; // namespace android
192
193#endif // ANDROID_AUDIOPOLICYSERVICE_H
194
195
196
197
198
199
200
201