1 /*
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 #define LOG_TAG "AudioPolicyManagerGeneric"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 #include "AudioPolicyManagerGeneric.h"
21 #include <media/mediarecorder.h>
22
23 namespace android {
24
25
26 // ----------------------------------------------------------------------------
27 // AudioPolicyInterface implementation
28 // ----------------------------------------------------------------------------
29
30
setDeviceConnectionState(AudioSystem::audio_devices device,AudioSystem::device_connection_state state,const char * device_address)31 status_t AudioPolicyManagerGeneric::setDeviceConnectionState(AudioSystem::audio_devices device,
32 AudioSystem::device_connection_state state,
33 const char *device_address)
34 {
35
36 LOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
37
38 // connect/disconnect only 1 device at a time
39 if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
40
41 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
42 LOGE("setDeviceConnectionState() invalid address: %s", device_address);
43 return BAD_VALUE;
44 }
45
46 // handle output devices
47 if (AudioSystem::isOutputDevice(device)) {
48 switch (state)
49 {
50 // handle output device connection
51 case AudioSystem::DEVICE_STATE_AVAILABLE:
52 if (mAvailableOutputDevices & device) {
53 LOGW("setDeviceConnectionState() device already connected: %x", device);
54 return INVALID_OPERATION;
55 }
56 LOGV("setDeviceConnectionState() connecting device %x", device);
57
58 // register new device as available
59 mAvailableOutputDevices |= device;
60 break;
61 // handle output device disconnection
62 case AudioSystem::DEVICE_STATE_UNAVAILABLE:
63 if (!(mAvailableOutputDevices & device)) {
64 LOGW("setDeviceConnectionState() device not connected: %x", device);
65 return INVALID_OPERATION;
66 }
67 LOGV("setDeviceConnectionState() disconnecting device %x", device);
68 // remove device from available output devices
69 mAvailableOutputDevices &= ~device;
70 break;
71
72 default:
73 LOGE("setDeviceConnectionState() invalid state: %x", state);
74 return BAD_VALUE;
75 }
76 return NO_ERROR;
77 }
78 // handle input devices
79 if (AudioSystem::isInputDevice(device)) {
80 switch (state)
81 {
82 // handle input device connection
83 case AudioSystem::DEVICE_STATE_AVAILABLE:
84 if (mAvailableInputDevices & device) {
85 LOGW("setDeviceConnectionState() device already connected: %d", device);
86 return INVALID_OPERATION;
87 }
88 mAvailableInputDevices |= device;
89 break;
90
91 // handle input device disconnection
92 case AudioSystem::DEVICE_STATE_UNAVAILABLE:
93 if (!(mAvailableInputDevices & device)) {
94 LOGW("setDeviceConnectionState() device not connected: %d", device);
95 return INVALID_OPERATION;
96 }
97 mAvailableInputDevices &= ~device;
98 break;
99
100 default:
101 LOGE("setDeviceConnectionState() invalid state: %x", state);
102 return BAD_VALUE;
103 }
104 return NO_ERROR;
105 }
106
107 LOGW("setDeviceConnectionState() invalid device: %x", device);
108 return BAD_VALUE;
109 }
110
getDeviceConnectionState(AudioSystem::audio_devices device,const char * device_address)111 AudioSystem::device_connection_state AudioPolicyManagerGeneric::getDeviceConnectionState(AudioSystem::audio_devices device,
112 const char *device_address)
113 {
114 AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
115 String8 address = String8(device_address);
116 if (AudioSystem::isOutputDevice(device)) {
117 if (device & mAvailableOutputDevices) {
118 state = AudioSystem::DEVICE_STATE_AVAILABLE;
119 }
120 } else if (AudioSystem::isInputDevice(device)) {
121 if (device & mAvailableInputDevices) {
122 state = AudioSystem::DEVICE_STATE_AVAILABLE;
123 }
124 }
125
126 return state;
127 }
128
setPhoneState(int state)129 void AudioPolicyManagerGeneric::setPhoneState(int state)
130 {
131 LOGV("setPhoneState() state %d", state);
132 uint32_t newDevice = 0;
133 if (state < 0 || state >= AudioSystem::NUM_MODES) {
134 LOGW("setPhoneState() invalid state %d", state);
135 return;
136 }
137
138 if (state == mPhoneState ) {
139 LOGW("setPhoneState() setting same state %d", state);
140 return;
141 }
142 // store previous phone state for management of sonification strategy below
143 int oldState = mPhoneState;
144 mPhoneState = state;
145
146 // if leaving or entering in call state, handle special case of active streams
147 // pertaining to sonification strategy see handleIncallSonification()
148 if (state == AudioSystem::MODE_IN_CALL ||
149 oldState == AudioSystem::MODE_IN_CALL) {
150 bool starting = (state == AudioSystem::MODE_IN_CALL) ? true : false;
151 LOGV("setPhoneState() in call state management: new state is %d", state);
152 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
153 handleIncallSonification(stream, starting);
154 }
155 }
156 }
157
setRingerMode(uint32_t mode,uint32_t mask)158 void AudioPolicyManagerGeneric::setRingerMode(uint32_t mode, uint32_t mask)
159 {
160 LOGV("setRingerMode() mode %x, mask %x", mode, mask);
161
162 mRingerMode = mode;
163 }
164
setForceUse(AudioSystem::force_use usage,AudioSystem::forced_config config)165 void AudioPolicyManagerGeneric::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
166 {
167 LOGV("setForceUse) usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
168 mForceUse[usage] = config;
169 }
170
getForceUse(AudioSystem::force_use usage)171 AudioSystem::forced_config AudioPolicyManagerGeneric::getForceUse(AudioSystem::force_use usage)
172 {
173 return mForceUse[usage];
174 }
175
setSystemProperty(const char * property,const char * value)176 void AudioPolicyManagerGeneric::setSystemProperty(const char* property, const char* value)
177 {
178 LOGV("setSystemProperty() property %s, value %s", property, value);
179 if (strcmp(property, "ro.camera.sound.forced") == 0) {
180 if (atoi(value)) {
181 LOGV("ENFORCED_AUDIBLE cannot be muted");
182 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
183 } else {
184 LOGV("ENFORCED_AUDIBLE can be muted");
185 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
186 }
187 }
188 }
189
getOutput(AudioSystem::stream_type stream,uint32_t samplingRate,uint32_t format,uint32_t channels,AudioSystem::output_flags flags)190 audio_io_handle_t AudioPolicyManagerGeneric::getOutput(AudioSystem::stream_type stream,
191 uint32_t samplingRate,
192 uint32_t format,
193 uint32_t channels,
194 AudioSystem::output_flags flags)
195 {
196 LOGV("getOutput() stream %d, samplingRate %d, format %d, channels %x, flags %x", stream, samplingRate, format, channels, flags);
197
198 #ifdef AUDIO_POLICY_TEST
199 if (mCurOutput != 0) {
200 LOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channels %x, mDirectOutput %d",
201 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
202
203 if (mTestOutputs[mCurOutput] == 0) {
204 LOGV("getOutput() opening test output");
205 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
206 outputDesc->mDevice = mTestDevice;
207 outputDesc->mSamplingRate = mTestSamplingRate;
208 outputDesc->mFormat = mTestFormat;
209 outputDesc->mChannels = mTestChannels;
210 outputDesc->mLatency = mTestLatencyMs;
211 outputDesc->mFlags = (AudioSystem::output_flags)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
212 outputDesc->mRefCount[stream] = 0;
213 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(&outputDesc->mDevice,
214 &outputDesc->mSamplingRate,
215 &outputDesc->mFormat,
216 &outputDesc->mChannels,
217 &outputDesc->mLatency,
218 outputDesc->mFlags);
219 if (mTestOutputs[mCurOutput]) {
220 AudioParameter outputCmd = AudioParameter();
221 outputCmd.addInt(String8("set_id"),mCurOutput);
222 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
223 mOutputs.add(mTestOutputs[mCurOutput], outputDesc);
224 }
225 }
226 return mTestOutputs[mCurOutput];
227 }
228 #endif //AUDIO_POLICY_TEST
229 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
230 (format != 0 && !AudioSystem::isLinearPCM(format)) ||
231 (channels != 0 && channels != AudioSystem::CHANNEL_OUT_MONO && channels != AudioSystem::CHANNEL_OUT_STEREO)) {
232 return 0;
233 }
234
235 return mHardwareOutput;
236 }
237
startOutput(audio_io_handle_t output,AudioSystem::stream_type stream)238 status_t AudioPolicyManagerGeneric::startOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
239 {
240 LOGV("startOutput() output %d, stream %d", output, stream);
241 ssize_t index = mOutputs.indexOfKey(output);
242 if (index < 0) {
243 LOGW("startOutput() unknow output %d", output);
244 return BAD_VALUE;
245 }
246
247 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
248
249 // handle special case for sonification while in call
250 if (mPhoneState == AudioSystem::MODE_IN_CALL) {
251 handleIncallSonification(stream, true);
252 }
253
254 // incremenent usage count for this stream on the requested output:
255 outputDesc->changeRefCount(stream, 1);
256 return NO_ERROR;
257 }
258
stopOutput(audio_io_handle_t output,AudioSystem::stream_type stream)259 status_t AudioPolicyManagerGeneric::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
260 {
261 LOGV("stopOutput() output %d, stream %d", output, stream);
262 ssize_t index = mOutputs.indexOfKey(output);
263 if (index < 0) {
264 LOGW("stopOutput() unknow output %d", output);
265 return BAD_VALUE;
266 }
267
268 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
269
270 // handle special case for sonification while in call
271 if (mPhoneState == AudioSystem::MODE_IN_CALL) {
272 handleIncallSonification(stream, false);
273 }
274
275 if (outputDesc->isUsedByStream(stream)) {
276 // decrement usage count of this stream on the output
277 outputDesc->changeRefCount(stream, -1);
278 return NO_ERROR;
279 } else {
280 LOGW("stopOutput() refcount is already 0 for output %d", output);
281 return INVALID_OPERATION;
282 }
283 }
284
releaseOutput(audio_io_handle_t output)285 void AudioPolicyManagerGeneric::releaseOutput(audio_io_handle_t output)
286 {
287 LOGV("releaseOutput() %d", output);
288 ssize_t index = mOutputs.indexOfKey(output);
289 if (index < 0) {
290 LOGW("releaseOutput() releasing unknown output %d", output);
291 return;
292 }
293
294 #ifdef AUDIO_POLICY_TEST
295 int testIndex = testOutputIndex(output);
296 if (testIndex != 0) {
297 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
298 if (outputDesc->refCount() == 0) {
299 mpClientInterface->closeOutput(output);
300 delete mOutputs.valueAt(index);
301 mOutputs.removeItem(output);
302 mTestOutputs[testIndex] = 0;
303 }
304 }
305 #endif //AUDIO_POLICY_TEST
306 }
307
getInput(int inputSource,uint32_t samplingRate,uint32_t format,uint32_t channels,AudioSystem::audio_in_acoustics acoustics)308 audio_io_handle_t AudioPolicyManagerGeneric::getInput(int inputSource,
309 uint32_t samplingRate,
310 uint32_t format,
311 uint32_t channels,
312 AudioSystem::audio_in_acoustics acoustics)
313 {
314 audio_io_handle_t input = 0;
315 uint32_t device;
316
317 LOGV("getInput() inputSource %d, samplingRate %d, format %d, channels %x, acoustics %x", inputSource, samplingRate, format, channels, acoustics);
318
319 AudioInputDescriptor *inputDesc = new AudioInputDescriptor();
320 inputDesc->mDevice = AudioSystem::DEVICE_IN_BUILTIN_MIC;
321 inputDesc->mSamplingRate = samplingRate;
322 inputDesc->mFormat = format;
323 inputDesc->mChannels = channels;
324 inputDesc->mAcoustics = acoustics;
325 inputDesc->mRefCount = 0;
326 input = mpClientInterface->openInput(&inputDesc->mDevice,
327 &inputDesc->mSamplingRate,
328 &inputDesc->mFormat,
329 &inputDesc->mChannels,
330 inputDesc->mAcoustics);
331
332 // only accept input with the exact requested set of parameters
333 if ((samplingRate != inputDesc->mSamplingRate) ||
334 (format != inputDesc->mFormat) ||
335 (channels != inputDesc->mChannels)) {
336 LOGV("getOutput() failed opening input: samplingRate %d, format %d, channels %d",
337 samplingRate, format, channels);
338 mpClientInterface->closeInput(input);
339 delete inputDesc;
340 return 0;
341 }
342 mInputs.add(input, inputDesc);
343 return input;
344 }
345
startInput(audio_io_handle_t input)346 status_t AudioPolicyManagerGeneric::startInput(audio_io_handle_t input)
347 {
348 LOGV("startInput() input %d", input);
349 ssize_t index = mInputs.indexOfKey(input);
350 if (index < 0) {
351 LOGW("startInput() unknow input %d", input);
352 return BAD_VALUE;
353 }
354 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
355
356 #ifdef AUDIO_POLICY_TEST
357 if (mTestInput == 0)
358 #endif //AUDIO_POLICY_TEST
359 {
360 // refuse 2 active AudioRecord clients at the same time
361 for (size_t i = 0; i < mInputs.size(); i++) {
362 if (mInputs.valueAt(i)->mRefCount > 0) {
363 LOGW("startInput() input %d, other input %d already started", input, mInputs.keyAt(i));
364 return INVALID_OPERATION;
365 }
366 }
367 }
368
369 inputDesc->mRefCount = 1;
370 return NO_ERROR;
371 }
372
stopInput(audio_io_handle_t input)373 status_t AudioPolicyManagerGeneric::stopInput(audio_io_handle_t input)
374 {
375 LOGV("stopInput() input %d", input);
376 ssize_t index = mInputs.indexOfKey(input);
377 if (index < 0) {
378 LOGW("stopInput() unknow input %d", input);
379 return BAD_VALUE;
380 }
381 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
382
383 if (inputDesc->mRefCount == 0) {
384 LOGW("stopInput() input %d already stopped", input);
385 return INVALID_OPERATION;
386 } else {
387 inputDesc->mRefCount = 0;
388 return NO_ERROR;
389 }
390 }
391
releaseInput(audio_io_handle_t input)392 void AudioPolicyManagerGeneric::releaseInput(audio_io_handle_t input)
393 {
394 LOGV("releaseInput() %d", input);
395 ssize_t index = mInputs.indexOfKey(input);
396 if (index < 0) {
397 LOGW("releaseInput() releasing unknown input %d", input);
398 return;
399 }
400 mpClientInterface->closeInput(input);
401 delete mInputs.valueAt(index);
402 mInputs.removeItem(input);
403 }
404
405
406
initStreamVolume(AudioSystem::stream_type stream,int indexMin,int indexMax)407 void AudioPolicyManagerGeneric::initStreamVolume(AudioSystem::stream_type stream,
408 int indexMin,
409 int indexMax)
410 {
411 LOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
412 mStreams[stream].mIndexMin = indexMin;
413 mStreams[stream].mIndexMax = indexMax;
414 }
415
setStreamVolumeIndex(AudioSystem::stream_type stream,int index)416 status_t AudioPolicyManagerGeneric::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
417 {
418
419 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
420 return BAD_VALUE;
421 }
422
423 LOGV("setStreamVolumeIndex() stream %d, index %d", stream, index);
424 mStreams[stream].mIndexCur = index;
425
426 // do not change actual stream volume if the stream is muted
427 if (mStreams[stream].mMuteCount != 0) {
428 return NO_ERROR;
429 }
430
431 // Do not changed in call volume if bluetooth is connected and vice versa
432 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
433 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
434 LOGV("setStreamVolumeIndex() cannot set stream %d volume with force use = %d for comm",
435 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
436 return INVALID_OPERATION;
437 }
438
439 // compute and apply stream volume on all outputs according to connected device
440 for (size_t i = 0; i < mOutputs.size(); i++) {
441 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
442 uint32_t device = outputDesc->device();
443
444 float volume = computeVolume((int)stream, index, device);
445
446 LOGV("setStreamVolume() for output %d stream %d, volume %f", mOutputs.keyAt(i), stream, volume);
447 mpClientInterface->setStreamVolume(stream, volume, mOutputs.keyAt(i));
448 }
449 return NO_ERROR;
450 }
451
getStreamVolumeIndex(AudioSystem::stream_type stream,int * index)452 status_t AudioPolicyManagerGeneric::getStreamVolumeIndex(AudioSystem::stream_type stream, int *index)
453 {
454 if (index == 0) {
455 return BAD_VALUE;
456 }
457 LOGV("getStreamVolumeIndex() stream %d", stream);
458 *index = mStreams[stream].mIndexCur;
459 return NO_ERROR;
460 }
461
dump(int fd)462 status_t AudioPolicyManagerGeneric::dump(int fd)
463 {
464 const size_t SIZE = 256;
465 char buffer[SIZE];
466 String8 result;
467
468 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
469 result.append(buffer);
470 snprintf(buffer, SIZE, " Hardware Output: %d\n", mHardwareOutput);
471 result.append(buffer);
472 snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
473 result.append(buffer);
474 snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
475 result.append(buffer);
476 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
477 result.append(buffer);
478 snprintf(buffer, SIZE, " Ringer mode: %d\n", mRingerMode);
479 result.append(buffer);
480 snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
481 result.append(buffer);
482 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
483 result.append(buffer);
484 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
485 result.append(buffer);
486 write(fd, result.string(), result.size());
487
488 snprintf(buffer, SIZE, "\nOutputs dump:\n");
489 write(fd, buffer, strlen(buffer));
490 for (size_t i = 0; i < mOutputs.size(); i++) {
491 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
492 write(fd, buffer, strlen(buffer));
493 mOutputs.valueAt(i)->dump(fd);
494 }
495
496 snprintf(buffer, SIZE, "\nInputs dump:\n");
497 write(fd, buffer, strlen(buffer));
498 for (size_t i = 0; i < mInputs.size(); i++) {
499 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
500 write(fd, buffer, strlen(buffer));
501 mInputs.valueAt(i)->dump(fd);
502 }
503
504 snprintf(buffer, SIZE, "\nStreams dump:\n");
505 write(fd, buffer, strlen(buffer));
506 snprintf(buffer, SIZE, " Stream Index Min Index Max Index Cur Mute Count Can be muted\n");
507 write(fd, buffer, strlen(buffer));
508 for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
509 snprintf(buffer, SIZE, " %02d", i);
510 mStreams[i].dump(buffer + 3, SIZE);
511 write(fd, buffer, strlen(buffer));
512 }
513
514 return NO_ERROR;
515 }
516
517 // ----------------------------------------------------------------------------
518 // AudioPolicyManagerGeneric
519 // ----------------------------------------------------------------------------
520
521 // --- class factory
522
AudioPolicyManagerGeneric(AudioPolicyClientInterface * clientInterface)523 AudioPolicyManagerGeneric::AudioPolicyManagerGeneric(AudioPolicyClientInterface *clientInterface)
524 :
525 #ifdef AUDIO_POLICY_TEST
526 Thread(false),
527 #endif //AUDIO_POLICY_TEST
528 mPhoneState(AudioSystem::MODE_NORMAL), mRingerMode(0)
529 {
530 mpClientInterface = clientInterface;
531
532 for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
533 mForceUse[i] = AudioSystem::FORCE_NONE;
534 }
535
536 // devices available by default are speaker, ear piece and microphone
537 mAvailableOutputDevices = AudioSystem::DEVICE_OUT_SPEAKER;
538 mAvailableInputDevices = AudioSystem::DEVICE_IN_BUILTIN_MIC;
539
540 // open hardware output
541 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
542 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
543 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
544 &outputDesc->mSamplingRate,
545 &outputDesc->mFormat,
546 &outputDesc->mChannels,
547 &outputDesc->mLatency,
548 outputDesc->mFlags);
549
550 if (mHardwareOutput == 0) {
551 LOGE("Failed to initialize hardware output stream, samplingRate: %d, format %d, channels %d",
552 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
553 } else {
554 mOutputs.add(mHardwareOutput, outputDesc);
555 }
556
557 #ifdef AUDIO_POLICY_TEST
558 AudioParameter outputCmd = AudioParameter();
559 outputCmd.addInt(String8("set_id"), 0);
560 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
561
562 mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER;
563 mTestSamplingRate = 44100;
564 mTestFormat = AudioSystem::PCM_16_BIT;
565 mTestChannels = AudioSystem::CHANNEL_OUT_STEREO;
566 mTestLatencyMs = 0;
567 mCurOutput = 0;
568 mDirectOutput = false;
569 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
570 mTestOutputs[i] = 0;
571 }
572
573 const size_t SIZE = 256;
574 char buffer[SIZE];
575 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
576 run(buffer, ANDROID_PRIORITY_AUDIO);
577 #endif //AUDIO_POLICY_TEST
578 }
579
~AudioPolicyManagerGeneric()580 AudioPolicyManagerGeneric::~AudioPolicyManagerGeneric()
581 {
582 #ifdef AUDIO_POLICY_TEST
583 exit();
584 #endif //AUDIO_POLICY_TEST
585
586 for (size_t i = 0; i < mOutputs.size(); i++) {
587 mpClientInterface->closeOutput(mOutputs.keyAt(i));
588 delete mOutputs.valueAt(i);
589 }
590 mOutputs.clear();
591 for (size_t i = 0; i < mInputs.size(); i++) {
592 mpClientInterface->closeInput(mInputs.keyAt(i));
593 delete mInputs.valueAt(i);
594 }
595 mInputs.clear();
596 }
597
598 #ifdef AUDIO_POLICY_TEST
threadLoop()599 bool AudioPolicyManagerGeneric::threadLoop()
600 {
601 LOGV("entering threadLoop()");
602 while (!exitPending())
603 {
604 String8 command;
605 int valueInt;
606 String8 value;
607
608 Mutex::Autolock _l(mLock);
609 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
610
611 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
612 AudioParameter param = AudioParameter(command);
613
614 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
615 valueInt != 0) {
616 LOGV("Test command %s received", command.string());
617 String8 target;
618 if (param.get(String8("target"), target) != NO_ERROR) {
619 target = "Manager";
620 }
621 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
622 param.remove(String8("test_cmd_policy_output"));
623 mCurOutput = valueInt;
624 }
625 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
626 param.remove(String8("test_cmd_policy_direct"));
627 if (value == "false") {
628 mDirectOutput = false;
629 } else if (value == "true") {
630 mDirectOutput = true;
631 }
632 }
633 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
634 param.remove(String8("test_cmd_policy_input"));
635 mTestInput = valueInt;
636 }
637
638 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
639 param.remove(String8("test_cmd_policy_format"));
640 int format = AudioSystem::INVALID_FORMAT;
641 if (value == "PCM 16 bits") {
642 format = AudioSystem::PCM_16_BIT;
643 } else if (value == "PCM 8 bits") {
644 format = AudioSystem::PCM_8_BIT;
645 } else if (value == "Compressed MP3") {
646 format = AudioSystem::MP3;
647 }
648 if (format != AudioSystem::INVALID_FORMAT) {
649 if (target == "Manager") {
650 mTestFormat = format;
651 } else if (mTestOutputs[mCurOutput] != 0) {
652 AudioParameter outputParam = AudioParameter();
653 outputParam.addInt(String8("format"), format);
654 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
655 }
656 }
657 }
658 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
659 param.remove(String8("test_cmd_policy_channels"));
660 int channels = 0;
661
662 if (value == "Channels Stereo") {
663 channels = AudioSystem::CHANNEL_OUT_STEREO;
664 } else if (value == "Channels Mono") {
665 channels = AudioSystem::CHANNEL_OUT_MONO;
666 }
667 if (channels != 0) {
668 if (target == "Manager") {
669 mTestChannels = channels;
670 } else if (mTestOutputs[mCurOutput] != 0) {
671 AudioParameter outputParam = AudioParameter();
672 outputParam.addInt(String8("channels"), channels);
673 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
674 }
675 }
676 }
677 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
678 param.remove(String8("test_cmd_policy_sampleRate"));
679 if (valueInt >= 0 && valueInt <= 96000) {
680 int samplingRate = valueInt;
681 if (target == "Manager") {
682 mTestSamplingRate = samplingRate;
683 } else if (mTestOutputs[mCurOutput] != 0) {
684 AudioParameter outputParam = AudioParameter();
685 outputParam.addInt(String8("sampling_rate"), samplingRate);
686 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
687 }
688 }
689 }
690
691 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
692 param.remove(String8("test_cmd_policy_reopen"));
693
694 mpClientInterface->closeOutput(mHardwareOutput);
695 delete mOutputs.valueFor(mHardwareOutput);
696 mOutputs.removeItem(mHardwareOutput);
697
698 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
699 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
700 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
701 &outputDesc->mSamplingRate,
702 &outputDesc->mFormat,
703 &outputDesc->mChannels,
704 &outputDesc->mLatency,
705 outputDesc->mFlags);
706 if (mHardwareOutput == 0) {
707 LOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
708 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
709 } else {
710 AudioParameter outputCmd = AudioParameter();
711 outputCmd.addInt(String8("set_id"), 0);
712 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
713 mOutputs.add(mHardwareOutput, outputDesc);
714 }
715 }
716
717
718 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
719 }
720 }
721 return false;
722 }
723
exit()724 void AudioPolicyManagerGeneric::exit()
725 {
726 {
727 AutoMutex _l(mLock);
728 requestExit();
729 mWaitWorkCV.signal();
730 }
731 requestExitAndWait();
732 }
733
testOutputIndex(audio_io_handle_t output)734 int AudioPolicyManagerGeneric::testOutputIndex(audio_io_handle_t output)
735 {
736 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
737 if (output == mTestOutputs[i]) return i;
738 }
739 return 0;
740 }
741 #endif //AUDIO_POLICY_TEST
742
743 // ---
744
getStrategy(AudioSystem::stream_type stream)745 AudioPolicyManagerGeneric::routing_strategy AudioPolicyManagerGeneric::getStrategy(AudioSystem::stream_type stream)
746 {
747 // stream to strategy mapping
748 switch (stream) {
749 case AudioSystem::VOICE_CALL:
750 case AudioSystem::BLUETOOTH_SCO:
751 return STRATEGY_PHONE;
752 case AudioSystem::RING:
753 case AudioSystem::NOTIFICATION:
754 case AudioSystem::ALARM:
755 case AudioSystem::ENFORCED_AUDIBLE:
756 return STRATEGY_SONIFICATION;
757 case AudioSystem::DTMF:
758 return STRATEGY_DTMF;
759 default:
760 LOGE("unknown stream type");
761 case AudioSystem::SYSTEM:
762 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
763 // while key clicks are played produces a poor result
764 case AudioSystem::TTS:
765 case AudioSystem::MUSIC:
766 return STRATEGY_MEDIA;
767 }
768 }
769
770
computeVolume(int stream,int index,uint32_t device)771 float AudioPolicyManagerGeneric::computeVolume(int stream, int index, uint32_t device)
772 {
773 float volume = 1.0;
774
775 StreamDescriptor &streamDesc = mStreams[stream];
776
777 // Force max volume if stream cannot be muted
778 if (!streamDesc.mCanBeMuted) index = streamDesc.mIndexMax;
779
780 int volInt = (100 * (index - streamDesc.mIndexMin)) / (streamDesc.mIndexMax - streamDesc.mIndexMin);
781 volume = AudioSystem::linearToLog(volInt);
782
783 return volume;
784 }
785
setStreamMute(int stream,bool on,audio_io_handle_t output)786 void AudioPolicyManagerGeneric::setStreamMute(int stream, bool on, audio_io_handle_t output)
787 {
788 LOGV("setStreamMute() stream %d, mute %d, output %d", stream, on, output);
789
790 StreamDescriptor &streamDesc = mStreams[stream];
791
792 if (on) {
793 if (streamDesc.mMuteCount++ == 0) {
794 if (streamDesc.mCanBeMuted) {
795 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, 0, output);
796 }
797 }
798 } else {
799 if (streamDesc.mMuteCount == 0) {
800 LOGW("setStreamMute() unmuting non muted stream!");
801 return;
802 }
803 if (--streamDesc.mMuteCount == 0) {
804 uint32_t device = mOutputs.valueFor(output)->mDevice;
805 float volume = computeVolume(stream, streamDesc.mIndexCur, device);
806 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output);
807 }
808 }
809 }
810
handleIncallSonification(int stream,bool starting)811 void AudioPolicyManagerGeneric::handleIncallSonification(int stream, bool starting)
812 {
813 // if the stream pertains to sonification strategy and we are in call we must
814 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
815 // in the device used for phone strategy and play the tone if the selected device does not
816 // interfere with the device used for phone strategy
817 if (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) {
818 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mHardwareOutput);
819 LOGV("handleIncallSonification() stream %d starting %d device %x", stream, starting, outputDesc->mDevice);
820 if (outputDesc->isUsedByStream((AudioSystem::stream_type)stream)) {
821 if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
822 LOGV("handleIncallSonification() low visibility");
823 setStreamMute(stream, starting, mHardwareOutput);
824 } else {
825 if (starting) {
826 mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
827 } else {
828 mpClientInterface->stopTone();
829 }
830 }
831 }
832 }
833 }
834
835
836 // --- AudioOutputDescriptor class implementation
837
AudioOutputDescriptor()838 AudioPolicyManagerGeneric::AudioOutputDescriptor::AudioOutputDescriptor()
839 : mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0),
840 mFlags((AudioSystem::output_flags)0), mDevice(0)
841 {
842 // clear usage count for all stream types
843 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
844 mRefCount[i] = 0;
845 }
846 }
847
device()848 uint32_t AudioPolicyManagerGeneric::AudioOutputDescriptor::device()
849 {
850 return mDevice;
851 }
852
changeRefCount(AudioSystem::stream_type stream,int delta)853 void AudioPolicyManagerGeneric::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
854 {
855 if ((delta + (int)mRefCount[stream]) < 0) {
856 LOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
857 mRefCount[stream] = 0;
858 return;
859 }
860 mRefCount[stream] += delta;
861 LOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
862 }
863
refCount()864 uint32_t AudioPolicyManagerGeneric::AudioOutputDescriptor::refCount()
865 {
866 uint32_t refcount = 0;
867 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
868 refcount += mRefCount[i];
869 }
870 return refcount;
871 }
872
dump(int fd)873 status_t AudioPolicyManagerGeneric::AudioOutputDescriptor::dump(int fd)
874 {
875 const size_t SIZE = 256;
876 char buffer[SIZE];
877 String8 result;
878
879 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
880 result.append(buffer);
881 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
882 result.append(buffer);
883 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
884 result.append(buffer);
885 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
886 result.append(buffer);
887 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
888 result.append(buffer);
889 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
890 result.append(buffer);
891 snprintf(buffer, SIZE, " Stream refCount\n");
892 result.append(buffer);
893 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
894 snprintf(buffer, SIZE, " %02d %d\n", i, mRefCount[i]);
895 result.append(buffer);
896 }
897 write(fd, result.string(), result.size());
898
899 return NO_ERROR;
900 }
901
902 // --- AudioInputDescriptor class implementation
903
AudioInputDescriptor()904 AudioPolicyManagerGeneric::AudioInputDescriptor::AudioInputDescriptor()
905 : mSamplingRate(0), mFormat(0), mChannels(0),
906 mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice(0), mRefCount(0)
907 {
908 }
909
dump(int fd)910 status_t AudioPolicyManagerGeneric::AudioInputDescriptor::dump(int fd)
911 {
912 const size_t SIZE = 256;
913 char buffer[SIZE];
914 String8 result;
915
916 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
917 result.append(buffer);
918 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
919 result.append(buffer);
920 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
921 result.append(buffer);
922 snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics);
923 result.append(buffer);
924 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
925 result.append(buffer);
926 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
927 result.append(buffer);
928 write(fd, result.string(), result.size());
929
930 return NO_ERROR;
931 }
932
933 // --- StreamDescriptor class implementation
934
dump(char * buffer,size_t size)935 void AudioPolicyManagerGeneric::StreamDescriptor::dump(char* buffer, size_t size)
936 {
937 snprintf(buffer, size, " %02d %02d %02d %02d %d\n",
938 mIndexMin,
939 mIndexMax,
940 mIndexCur,
941 mMuteCount,
942 mCanBeMuted);
943 }
944
945 }; // namespace android
946