blob: b9234ec39c10f842790f6c80416651fce2002291 [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>
Eric Laurenta553c252009-07-17 12:17:14 -070024
25namespace android {
26
27class String8;
28
29// ----------------------------------------------------------------------------
30
31class AudioPolicyService: public BnAudioPolicyService, public AudioPolicyClientInterface, public IBinder::DeathRecipient
32{
33
34public:
35 static void instantiate();
36
37 virtual status_t dump(int fd, const Vector<String16>& args);
38
39 //
40 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
41 //
42
43 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
44 AudioSystem::device_connection_state state,
45 const char *device_address);
46 virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device,
47 const char *device_address);
48 virtual status_t setPhoneState(int state);
49 virtual status_t setRingerMode(uint32_t mode, uint32_t mask);
50 virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
51 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
52 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
53 uint32_t samplingRate = 0,
54 uint32_t format = AudioSystem::FORMAT_DEFAULT,
55 uint32_t channels = 0,
56 AudioSystem::output_flags flags = AudioSystem::OUTPUT_FLAG_INDIRECT);
57 virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
58 virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
59 virtual void releaseOutput(audio_io_handle_t output);
60 virtual audio_io_handle_t getInput(int inputSource,
61 uint32_t samplingRate = 0,
62 uint32_t format = AudioSystem::FORMAT_DEFAULT,
63 uint32_t channels = 0,
64 AudioSystem::audio_in_acoustics acoustics = (AudioSystem::audio_in_acoustics)0);
65 virtual status_t startInput(audio_io_handle_t input);
66 virtual status_t stopInput(audio_io_handle_t input);
67 virtual void releaseInput(audio_io_handle_t input);
68 virtual status_t initStreamVolume(AudioSystem::stream_type stream,
69 int indexMin,
70 int indexMax);
71 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index);
72 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index);
73
74 virtual status_t onTransact(
75 uint32_t code,
76 const Parcel& data,
77 Parcel* reply,
78 uint32_t flags);
79
80 // IBinder::DeathRecipient
81 virtual void binderDied(const wp<IBinder>& who);
82
83 //
84 // AudioPolicyClientInterface
85 //
86 virtual audio_io_handle_t openOutput(uint32_t *pDevices,
87 uint32_t *pSamplingRate,
88 uint32_t *pFormat,
89 uint32_t *pChannels,
90 uint32_t *pLatencyMs,
91 AudioSystem::output_flags flags);
92 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
93 virtual status_t closeOutput(audio_io_handle_t output);
94 virtual status_t suspendOutput(audio_io_handle_t output);
95 virtual status_t restoreOutput(audio_io_handle_t output);
96 virtual audio_io_handle_t openInput(uint32_t *pDevices,
97 uint32_t *pSamplingRate,
98 uint32_t *pFormat,
99 uint32_t *pChannels,
100 uint32_t acoustics);
101 virtual status_t closeInput(audio_io_handle_t input);
Eric Laurent327c27b2009-08-27 00:48:47 -0700102 virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700103 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output);
Eric Laurent327c27b2009-08-27 00:48:47 -0700104 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700105 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
106 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream);
107 virtual status_t stopTone();
Eric Laurent415f3e22009-10-21 08:14:22 -0700108 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700109
110private:
111 AudioPolicyService();
112 virtual ~AudioPolicyService();
113
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800114 status_t dumpInternals(int fd);
115
Eric Laurenta553c252009-07-17 12:17:14 -0700116 // Thread used for tone playback and to send audio config commands to audio flinger
117 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone()
118 // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause
119 // calls to AudioPolicyService and an attempt to lock mLock.
120 // For audio config commands, it is necessary because audio flinger requires that the calling process (user)
121 // has permission to modify audio settings.
122 class AudioCommandThread : public Thread {
Eric Laurent327c27b2009-08-27 00:48:47 -0700123 class AudioCommand;
Eric Laurenta553c252009-07-17 12:17:14 -0700124 public:
125
126 // commands for tone AudioCommand
127 enum {
128 START_TONE,
129 STOP_TONE,
130 SET_VOLUME,
Eric Laurent415f3e22009-10-21 08:14:22 -0700131 SET_PARAMETERS,
132 SET_VOICE_VOLUME
Eric Laurenta553c252009-07-17 12:17:14 -0700133 };
134
135 AudioCommandThread ();
136 virtual ~AudioCommandThread();
137
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800138 status_t dump(int fd);
139
Eric Laurenta553c252009-07-17 12:17:14 -0700140 // Thread virtuals
141 virtual void onFirstRef();
142 virtual bool threadLoop();
143
144 void exit();
145 void startToneCommand(int type = 0, int stream = 0);
146 void stopToneCommand();
Eric Laurent327c27b2009-08-27 00:48:47 -0700147 status_t volumeCommand(int stream, float volume, int output, int delayMs = 0);
148 status_t parametersCommand(int ioHandle, const String8& keyValuePairs, int delayMs = 0);
Eric Laurent415f3e22009-10-21 08:14:22 -0700149 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurent327c27b2009-08-27 00:48:47 -0700150 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700151
152 private:
153 // descriptor for requested tone playback event
154 class AudioCommand {
Eric Laurent3fdb1262009-11-07 00:01:32 -0800155
Eric Laurenta553c252009-07-17 12:17:14 -0700156 public:
Eric Laurent3fdb1262009-11-07 00:01:32 -0800157 AudioCommand()
158 : mCommand(-1) {}
159
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800160 void dump(char* buffer, size_t size);
161
Eric Laurenta553c252009-07-17 12:17:14 -0700162 int mCommand; // START_TONE, STOP_TONE ...
Eric Laurent327c27b2009-08-27 00:48:47 -0700163 nsecs_t mTime; // time stamp
164 Condition mCond; // condition for status return
165 status_t mStatus; // command status
166 bool mWaitStatus; // true if caller is waiting for status
167 void *mParam; // command parameter (ToneData, VolumeData, ParametersData)
Eric Laurenta553c252009-07-17 12:17:14 -0700168 };
169
170 class ToneData {
171 public:
172 int mType; // tone type (START_TONE only)
173 int mStream; // stream type (START_TONE only)
174 };
175
176 class VolumeData {
177 public:
178 int mStream;
179 float mVolume;
Eric Laurentddb78e72009-07-28 08:44:33 -0700180 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700181 };
Eric Laurent415f3e22009-10-21 08:14:22 -0700182
Eric Laurenta553c252009-07-17 12:17:14 -0700183 class ParametersData {
184 public:
Eric Laurentddb78e72009-07-28 08:44:33 -0700185 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700186 String8 mKeyValuePairs;
187 };
188
Eric Laurent415f3e22009-10-21 08:14:22 -0700189 class VoiceVolumeData {
190 public:
191 float mVolume;
192 };
Eric Laurenta553c252009-07-17 12:17:14 -0700193
194 Mutex mLock;
195 Condition mWaitWorkCV;
Eric Laurent327c27b2009-08-27 00:48:47 -0700196 Vector <AudioCommand *> mAudioCommands; // list of pending commands
Eric Laurenta553c252009-07-17 12:17:14 -0700197 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent3fdb1262009-11-07 00:01:32 -0800198 AudioCommand mLastCommand;
Eric Laurenta553c252009-07-17 12:17:14 -0700199 };
200
201 // Internal dump utilities.
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800202 status_t dumpPermissionDenial(int fd);
Eric Laurenta553c252009-07-17 12:17:14 -0700203
204
205 Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing device
206 // connection stated our routing
207 AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager
208 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread
Eric Laurent327c27b2009-08-27 00:48:47 -0700209 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurenta553c252009-07-17 12:17:14 -0700210};
211
212}; // namespace android
213
214#endif // ANDROID_AUDIOPOLICYSERVICE_H
215
216
217
218
219
220
221
222