blob: ede62cccecf7d70cdd472eb5f415985cefec33a6 [file] [log] [blame]
Ram Mohan1b120aa2021-12-14 21:53:44 +05301/*
2 * Copyright (C) 2021 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 "AudioSystemTest"
18
19#include <string.h>
20
21#include <gtest/gtest.h>
22#include <media/IAudioFlinger.h>
23#include <utils/Log.h>
24
25#include "audio_test_utils.h"
26
27using namespace android;
28
29void anyPatchContainsInputDevice(audio_port_handle_t deviceId, bool& res) {
30 std::vector<struct audio_patch> patches;
31 status_t status = listAudioPatches(patches);
32 ASSERT_EQ(OK, status);
33 res = false;
34 for (const auto& patch : patches) {
35 if (patchContainsInputDevice(deviceId, patch)) {
36 res = true;
37 return;
38 }
39 }
40}
41
42class AudioSystemTest : public ::testing::Test {
43 public:
44 void SetUp() override {
45 mAF = AudioSystem::get_audio_flinger();
46 ASSERT_NE(mAF, nullptr) << "Permission denied";
47 }
48
49 void TearDown() override {
50 if (mPlayback) {
51 mPlayback->stop();
Mikhail Naganovcc56e472023-02-02 17:48:01 -080052 if (auto handle = mPlayback->getAudioTrackHandle(); handle) {
53 handle->removeAudioDeviceCallback(mCbPlayback);
54 }
55 mCbPlayback.clear();
Ram Mohan1b120aa2021-12-14 21:53:44 +053056 mPlayback.clear();
57 }
58 if (mCapture) {
59 mCapture->stop();
Mikhail Naganovcc56e472023-02-02 17:48:01 -080060 if (auto handle = mCapture->getAudioRecordHandle(); handle) {
61 handle->removeAudioDeviceCallback(mCbRecord);
62 }
63 mCbRecord.clear();
Ram Mohan1b120aa2021-12-14 21:53:44 +053064 mCapture.clear();
65 }
66 }
67
68 void createPlaybackSession(void);
69 void createRecordSession(void);
70
71 sp<IAudioFlinger> mAF;
72 sp<AudioPlayback> mPlayback;
73 sp<OnAudioDeviceUpdateNotifier> mCbPlayback;
74 sp<AudioCapture> mCapture;
75 sp<OnAudioDeviceUpdateNotifier> mCbRecord;
76};
77
78void AudioSystemTest::createPlaybackSession(void) {
79 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
80 attributes.usage = AUDIO_USAGE_MEDIA;
81 attributes.content_type = AUDIO_CONTENT_TYPE_MUSIC;
82 mPlayback = sp<AudioPlayback>::make(48000, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO,
83 AUDIO_OUTPUT_FLAG_FAST, AUDIO_SESSION_NONE,
84 AudioTrack::TRANSFER_SHARED, &attributes);
85 ASSERT_NE(nullptr, mPlayback);
86 ASSERT_EQ(NO_ERROR, mPlayback->loadResource("/data/local/tmp/bbb_2ch_24kHz_s16le.raw"));
87 EXPECT_EQ(NO_ERROR, mPlayback->create());
88 mCbPlayback = sp<OnAudioDeviceUpdateNotifier>::make();
89 EXPECT_EQ(OK, mPlayback->getAudioTrackHandle()->addAudioDeviceCallback(mCbPlayback));
90 EXPECT_EQ(NO_ERROR, mPlayback->start());
91 EXPECT_EQ(OK, mPlayback->onProcess());
92 EXPECT_EQ(OK, mCbPlayback->waitForAudioDeviceCb());
93}
94
95void AudioSystemTest::createRecordSession(void) {
96 mCapture = new AudioCapture(AUDIO_SOURCE_DEFAULT, 44100, AUDIO_FORMAT_PCM_8_24_BIT,
97 AUDIO_CHANNEL_IN_MONO, AUDIO_INPUT_FLAG_FAST);
98 ASSERT_NE(nullptr, mCapture);
99 ASSERT_EQ(OK, mCapture->create()) << "record creation failed";
100 mCbRecord = sp<OnAudioDeviceUpdateNotifier>::make();
101 EXPECT_EQ(OK, mCapture->getAudioRecordHandle()->addAudioDeviceCallback(mCbRecord));
102 EXPECT_EQ(OK, mCapture->start()) << "record creation failed";
103 EXPECT_EQ(OK, mCbRecord->waitForAudioDeviceCb());
104}
105
106// UNIT TESTS
107TEST_F(AudioSystemTest, CheckServerSideValues) {
108 ASSERT_NO_FATAL_FAILURE(createPlaybackSession());
109 EXPECT_GT(mAF->sampleRate(mCbPlayback->mAudioIo), 0);
110 EXPECT_NE(mAF->format(mCbPlayback->mAudioIo), AUDIO_FORMAT_INVALID);
111 EXPECT_GT(mAF->frameCount(mCbPlayback->mAudioIo), 0);
112 size_t frameCountHal, frameCountHalCache;
113 frameCountHal = mAF->frameCountHAL(mCbPlayback->mAudioIo);
114 EXPECT_GT(frameCountHal, 0);
115 EXPECT_EQ(OK, AudioSystem::getFrameCountHAL(mCbPlayback->mAudioIo, &frameCountHalCache));
116 EXPECT_EQ(frameCountHal, frameCountHalCache);
117 EXPECT_GT(mAF->latency(mCbPlayback->mAudioIo), 0);
118 // client side latency is at least server side latency
119 EXPECT_LE(mAF->latency(mCbPlayback->mAudioIo), mPlayback->getAudioTrackHandle()->latency());
120
121 ASSERT_NO_FATAL_FAILURE(createRecordSession());
122 EXPECT_GT(mAF->sampleRate(mCbRecord->mAudioIo), 0);
123 // EXPECT_NE(mAF->format(mCbRecord->mAudioIo), AUDIO_FORMAT_INVALID);
124 EXPECT_GT(mAF->frameCount(mCbRecord->mAudioIo), 0);
125 EXPECT_GT(mAF->frameCountHAL(mCbRecord->mAudioIo), 0);
126 frameCountHal = mAF->frameCountHAL(mCbRecord->mAudioIo);
127 EXPECT_GT(frameCountHal, 0);
128 EXPECT_EQ(OK, AudioSystem::getFrameCountHAL(mCbRecord->mAudioIo, &frameCountHalCache));
129 EXPECT_EQ(frameCountHal, frameCountHalCache);
130 // EXPECT_GT(mAF->latency(mCbRecord->mAudioIo), 0);
131 // client side latency is at least server side latency
132 // EXPECT_LE(mAF->latency(mCbRecord->mAudioIo), mCapture->getAudioRecordHandle()->latency());
133
134 EXPECT_GT(AudioSystem::getPrimaryOutputSamplingRate(), 0); // first fast mixer sample rate
135 EXPECT_GT(AudioSystem::getPrimaryOutputFrameCount(), 0); // fast mixer frame count
136}
137
138TEST_F(AudioSystemTest, GetSetMasterVolume) {
139 ASSERT_NO_FATAL_FAILURE(createPlaybackSession());
140 float origVol, tstVol;
141 EXPECT_EQ(NO_ERROR, AudioSystem::getMasterVolume(&origVol));
142 float newVol;
143 if (origVol + 0.2f > 1.0f) {
144 newVol = origVol - 0.2f;
145 } else {
146 newVol = origVol + 0.2f;
147 }
148 EXPECT_EQ(NO_ERROR, AudioSystem::setMasterVolume(newVol));
149 EXPECT_EQ(NO_ERROR, AudioSystem::getMasterVolume(&tstVol));
150 EXPECT_EQ(newVol, tstVol);
151 EXPECT_EQ(NO_ERROR, AudioSystem::setMasterVolume(origVol));
152 EXPECT_EQ(NO_ERROR, AudioSystem::getMasterVolume(&tstVol));
153 EXPECT_EQ(origVol, tstVol);
154}
155
156TEST_F(AudioSystemTest, GetSetMasterMute) {
157 ASSERT_NO_FATAL_FAILURE(createPlaybackSession());
158 bool origMuteState, tstMuteState;
159 EXPECT_EQ(NO_ERROR, AudioSystem::getMasterMute(&origMuteState));
160 EXPECT_EQ(NO_ERROR, AudioSystem::setMasterMute(!origMuteState));
161 EXPECT_EQ(NO_ERROR, AudioSystem::getMasterMute(&tstMuteState));
162 EXPECT_EQ(!origMuteState, tstMuteState);
163 EXPECT_EQ(NO_ERROR, AudioSystem::setMasterMute(origMuteState));
164 EXPECT_EQ(NO_ERROR, AudioSystem::getMasterMute(&tstMuteState));
165 EXPECT_EQ(origMuteState, tstMuteState);
166}
167
168TEST_F(AudioSystemTest, GetSetMicMute) {
169 ASSERT_NO_FATAL_FAILURE(createPlaybackSession());
170 bool origMuteState, tstMuteState;
171 EXPECT_EQ(NO_ERROR, AudioSystem::isMicrophoneMuted(&origMuteState));
172 EXPECT_EQ(NO_ERROR, AudioSystem::muteMicrophone(!origMuteState));
173 EXPECT_EQ(NO_ERROR, AudioSystem::isMicrophoneMuted(&tstMuteState));
174 EXPECT_EQ(!origMuteState, tstMuteState);
175 EXPECT_EQ(NO_ERROR, AudioSystem::muteMicrophone(origMuteState));
176 EXPECT_EQ(NO_ERROR, AudioSystem::isMicrophoneMuted(&tstMuteState));
177 EXPECT_EQ(origMuteState, tstMuteState);
178}
179
180TEST_F(AudioSystemTest, GetSetMasterBalance) {
181 ASSERT_NO_FATAL_FAILURE(createPlaybackSession());
182 float origBalance, tstBalance;
183 EXPECT_EQ(OK, AudioSystem::getMasterBalance(&origBalance));
184 float newBalance;
185 if (origBalance + 0.2f > 1.0f) {
186 newBalance = origBalance - 0.2f;
187 } else {
188 newBalance = origBalance + 0.2f;
189 }
190 EXPECT_EQ(OK, AudioSystem::setMasterBalance(newBalance));
191 EXPECT_EQ(OK, AudioSystem::getMasterBalance(&tstBalance));
192 EXPECT_EQ(newBalance, tstBalance);
193 EXPECT_EQ(OK, AudioSystem::setMasterBalance(origBalance));
194 EXPECT_EQ(OK, AudioSystem::getMasterBalance(&tstBalance));
195 EXPECT_EQ(origBalance, tstBalance);
196}
197
198TEST_F(AudioSystemTest, GetStreamVolume) {
199 ASSERT_NO_FATAL_FAILURE(createPlaybackSession());
200 float origStreamVol;
201 EXPECT_EQ(NO_ERROR, AudioSystem::getStreamVolume(AUDIO_STREAM_MUSIC, &origStreamVol,
202 mCbPlayback->mAudioIo));
203}
204
205TEST_F(AudioSystemTest, GetStreamMute) {
206 ASSERT_NO_FATAL_FAILURE(createPlaybackSession());
207 bool origMuteState;
208 EXPECT_EQ(NO_ERROR, AudioSystem::getStreamMute(AUDIO_STREAM_MUSIC, &origMuteState));
209}
210
211TEST_F(AudioSystemTest, StartAndStopAudioSource) {
212 std::vector<struct audio_port_v7> ports;
213 audio_port_config sourcePortConfig;
214 audio_attributes_t attributes = AudioSystem::streamTypeToAttributes(AUDIO_STREAM_MUSIC);
215 audio_port_handle_t sourcePortHandle = AUDIO_PORT_HANDLE_NONE;
216
217 status_t status = listAudioPorts(ports);
218 ASSERT_EQ(OK, status);
219 if (ports.empty()) {
220 GTEST_SKIP() << "No ports returned by the audio system";
221 }
222
223 for (const auto& port : ports) {
224 if (port.role != AUDIO_PORT_ROLE_SOURCE || port.type != AUDIO_PORT_TYPE_DEVICE) continue;
225 sourcePortConfig = port.active_config;
226
227 bool patchFound;
228
229 // start audio source.
230 status_t ret =
231 AudioSystem::startAudioSource(&sourcePortConfig, &attributes, &sourcePortHandle);
232 EXPECT_EQ(OK, ret) << "AudioSystem::startAudioSource for source " << port.ext.device.address
233 << " failed";
234
235 // verify that patch is established by the source port.
236 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(port.id, patchFound));
237 EXPECT_EQ(true, patchFound);
238 EXPECT_NE(sourcePortHandle, AUDIO_PORT_HANDLE_NONE);
239
240 if (sourcePortHandle != AUDIO_PORT_HANDLE_NONE) {
241 ret = AudioSystem::stopAudioSource(sourcePortHandle);
242 EXPECT_EQ(OK, ret) << "AudioSystem::stopAudioSource for handle failed";
243 }
244
245 // verify that no source port patch exists.
246 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(port.id, patchFound));
247 EXPECT_EQ(false, patchFound);
248 }
249}
250
251TEST_F(AudioSystemTest, CreateAndReleaseAudioPatch) {
252 status_t status;
253 struct audio_patch audioPatch;
254 std::vector<struct audio_port_v7> ports;
255 audio_patch_handle_t audioPatchHandle = AUDIO_PATCH_HANDLE_NONE;
256
257 bool patchFound = false;
258 audio_port_v7 sourcePort{};
259 audio_port_v7 sinkPort{};
260
261 audioPatch.id = 0;
262 audioPatch.num_sources = 1;
263 audioPatch.num_sinks = 1;
264
265 status = listAudioPorts(ports);
266 ASSERT_EQ(OK, status);
267 if (ports.empty()) {
268 GTEST_SKIP() << "No output devices returned by the audio system";
269 }
270
271 for (const auto& port : ports) {
272 if (port.role == AUDIO_PORT_ROLE_SOURCE && port.type == AUDIO_PORT_TYPE_DEVICE) {
273 sourcePort = port;
274 }
275 if (port.role == AUDIO_PORT_ROLE_SINK && port.type == AUDIO_PORT_TYPE_DEVICE &&
276 port.ext.device.type == AUDIO_DEVICE_OUT_SPEAKER) {
277 sinkPort = port;
278 }
279 }
280
281 audioPatch.sources[0] = sourcePort.active_config;
282 audioPatch.sinks[0] = sinkPort.active_config;
283
284 status = AudioSystem::createAudioPatch(&audioPatch, &audioPatchHandle);
285 EXPECT_EQ(OK, status) << "AudioSystem::createAudiopatch failed between source "
286 << sourcePort.ext.device.address << " and sink "
287 << sinkPort.ext.device.address;
288
289 // verify that patch is established between source and the sink.
290 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(sourcePort.id, patchFound));
291 EXPECT_EQ(true, patchFound);
292
293 EXPECT_NE(AUDIO_PORT_HANDLE_NONE, audioPatchHandle);
294 status = AudioSystem::releaseAudioPatch(audioPatchHandle);
295 EXPECT_EQ(OK, status) << "AudioSystem::releaseAudioPatch failed between source "
296 << sourcePort.ext.device.address << " and sink "
297 << sinkPort.ext.device.address;
298
299 // verify that no patch is established between source and the sink after releaseAudioPatch.
300 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(sourcePort.id, patchFound));
301 EXPECT_EQ(false, patchFound);
302}
303
304TEST_F(AudioSystemTest, GetAudioPort) {
305 std::vector<struct audio_port_v7> ports;
306 status_t status = listAudioPorts(ports);
307 ASSERT_EQ(OK, status);
308 for (const auto& port : ports) {
309 audio_port_v7 portTest{.id = port.id};
310 EXPECT_EQ(OK, AudioSystem::getAudioPort(&portTest));
311 EXPECT_TRUE(audio_ports_v7_are_equal(&portTest, &port));
312 }
313}
314
315TEST_F(AudioSystemTest, TestPhoneState) {
316 uid_t uid = getuid();
317 EXPECT_EQ(OK, AudioSystem::setPhoneState(AUDIO_MODE_RINGTONE, uid));
318 audio_mode_t state = AudioSystem::getPhoneState();
319 EXPECT_EQ(AUDIO_MODE_RINGTONE, state);
320 EXPECT_EQ(OK, AudioSystem::setPhoneState(AUDIO_MODE_IN_COMMUNICATION, uid));
321 state = AudioSystem::getPhoneState();
322 EXPECT_EQ(AUDIO_MODE_IN_COMMUNICATION, state);
323 EXPECT_EQ(OK, AudioSystem::setPhoneState(AUDIO_MODE_NORMAL, uid));
324 state = AudioSystem::getPhoneState();
325 EXPECT_EQ(AUDIO_MODE_NORMAL, state);
326}
327
328TEST_F(AudioSystemTest, GetDirectProfilesForAttributes) {
329 std::vector<audio_profile> audioProfiles;
330 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
331 attributes.usage = AUDIO_USAGE_MEDIA;
332 attributes.content_type = AUDIO_CONTENT_TYPE_MUSIC;
333 EXPECT_EQ(BAD_VALUE, AudioSystem::getDirectProfilesForAttributes(nullptr, nullptr));
334 EXPECT_EQ(BAD_VALUE, AudioSystem::getDirectProfilesForAttributes(nullptr, &audioProfiles));
335 EXPECT_EQ(BAD_VALUE, AudioSystem::getDirectProfilesForAttributes(&attributes, nullptr));
336 EXPECT_EQ(NO_ERROR, AudioSystem::getDirectProfilesForAttributes(&attributes, &audioProfiles));
337}
338
339bool isPublicStrategy(const AudioProductStrategy& strategy) {
340 bool result = true;
341 for (auto& attribute : strategy.getAudioAttributes()) {
342 if (attribute.getAttributes() == AUDIO_ATTRIBUTES_INITIALIZER &&
343 (uint32_t(attribute.getStreamType()) >= AUDIO_STREAM_PUBLIC_CNT)) {
344 result = false;
345 break;
346 }
347 }
348 return result;
349}
350
351TEST_F(AudioSystemTest, DevicesForRoleAndStrategy) {
352 std::vector<struct audio_port_v7> ports;
353 status_t status = listAudioPorts(ports);
354 ASSERT_EQ(OK, status);
355
356 std::vector<struct audio_port_v7> devicePorts;
357 for (const auto& port : ports) {
358 if (port.type == AUDIO_PORT_TYPE_DEVICE && audio_is_output_device(port.ext.device.type)) {
359 devicePorts.push_back(port);
360 }
361 }
362 if (devicePorts.empty()) {
363 GTEST_SKIP() << "No output devices returned by the audio system";
364 }
365
366 AudioProductStrategyVector strategies;
367 EXPECT_EQ(OK, AudioSystem::listAudioProductStrategies(strategies));
368 if (strategies.empty()) {
369 GTEST_SKIP() << "No strategies returned by the audio system";
370 }
371
372 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
373 attributes.usage = AUDIO_USAGE_MEDIA;
374
375 bool hasStrategyForMedia = false;
376 AudioProductStrategy mediaStrategy;
377 for (const auto& strategy : strategies) {
378 if (!isPublicStrategy(strategy)) continue;
379
380 for (const auto& att : strategy.getAudioAttributes()) {
381 if (strategy.attributesMatches(att.getAttributes(), attributes)) {
382 hasStrategyForMedia = true;
383 mediaStrategy = strategy;
384 break;
385 }
386 }
387 }
388
389 if (!hasStrategyForMedia) {
390 GTEST_SKIP() << "No strategies returned for music media";
391 }
392
393 AudioDeviceTypeAddrVector devices;
394 EXPECT_EQ(BAD_VALUE, AudioSystem::getDevicesForRoleAndStrategy(PRODUCT_STRATEGY_NONE,
395 DEVICE_ROLE_PREFERRED, devices));
396 EXPECT_EQ(BAD_VALUE, AudioSystem::getDevicesForRoleAndStrategy(mediaStrategy.getId(),
397 DEVICE_ROLE_NONE, devices));
398 status = AudioSystem::getDevicesForRoleAndStrategy(mediaStrategy.getId(), DEVICE_ROLE_PREFERRED,
399 devices);
400 if (status == NAME_NOT_FOUND) {
401 AudioDeviceTypeAddrVector outputDevices;
402 for (const auto& port : devicePorts) {
403 if (port.ext.device.type == AUDIO_DEVICE_OUT_SPEAKER) {
404 const AudioDeviceTypeAddr outputDevice(port.ext.device.type,
405 port.ext.device.address);
406 outputDevices.push_back(outputDevice);
407 }
408 }
409 EXPECT_EQ(OK, AudioSystem::setDevicesRoleForStrategy(mediaStrategy.getId(),
410 DEVICE_ROLE_PREFERRED, outputDevices));
411 EXPECT_EQ(OK, AudioSystem::getDevicesForRoleAndStrategy(mediaStrategy.getId(),
412 DEVICE_ROLE_PREFERRED, devices));
413 EXPECT_EQ(devices, outputDevices);
414 EXPECT_EQ(OK, AudioSystem::removeDevicesRoleForStrategy(mediaStrategy.getId(),
415 DEVICE_ROLE_PREFERRED));
416 EXPECT_EQ(NAME_NOT_FOUND, AudioSystem::getDevicesForRoleAndStrategy(
417 mediaStrategy.getId(), DEVICE_ROLE_PREFERRED, devices));
418 }
419}
420
421TEST_F(AudioSystemTest, VolumeIndexForAttributes) {
422 AudioVolumeGroupVector groups;
423 EXPECT_EQ(OK, AudioSystem::listAudioVolumeGroups(groups));
424 for (const auto& group : groups) {
425 if (group.getAudioAttributes().empty()) continue;
426 const audio_attributes_t attr = group.getAudioAttributes()[0];
427 if (attr == AUDIO_ATTRIBUTES_INITIALIZER) continue;
428 audio_stream_type_t streamType = AudioSystem::attributesToStreamType(attr);
429 if (streamType >= AUDIO_STREAM_PUBLIC_CNT) continue;
430
431 volume_group_t vg;
432 EXPECT_EQ(OK, AudioSystem::getVolumeGroupFromAudioAttributes(attr, vg));
433 EXPECT_EQ(group.getId(), vg);
434
435 int index;
436 EXPECT_EQ(OK,
437 AudioSystem::getVolumeIndexForAttributes(attr, index, AUDIO_DEVICE_OUT_SPEAKER));
438
439 int indexTest;
440 EXPECT_EQ(OK, AudioSystem::getStreamVolumeIndex(streamType, &indexTest,
441 AUDIO_DEVICE_OUT_SPEAKER));
442 EXPECT_EQ(index, indexTest);
443 }
444}
445
446TEST_F(AudioSystemTest, DevicesRoleForCapturePreset) {
447 std::vector<struct audio_port_v7> ports;
448 status_t status = listAudioPorts(ports);
449 ASSERT_EQ(OK, status);
450
451 if (ports.empty()) {
452 GTEST_SKIP() << "No ports returned by the audio system";
453 }
454
455 audio_devices_t inDeviceA = AUDIO_DEVICE_IN_BUILTIN_MIC;
456 audio_devices_t inDeviceB = AUDIO_DEVICE_IN_BUILTIN_MIC;
457 for (const auto& port : ports) {
458 if (port.role != AUDIO_PORT_ROLE_SOURCE || port.type != AUDIO_PORT_TYPE_DEVICE) continue;
459 if (port.ext.device.type == inDeviceA) continue;
460 inDeviceB = port.ext.device.type;
461 break;
462 }
463 const audio_source_t audioSource = AUDIO_SOURCE_MIC;
464 const device_role_t role = DEVICE_ROLE_PREFERRED;
465 const AudioDeviceTypeAddr inputDevice(inDeviceA, "");
466 const AudioDeviceTypeAddrVector inputDevices = {inputDevice};
467 const AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, "");
468 const AudioDeviceTypeAddrVector outputDevices = {outputDevice};
469
470 // Test invalid device when setting
471 EXPECT_EQ(BAD_VALUE,
472 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, outputDevices));
473 EXPECT_EQ(BAD_VALUE,
474 AudioSystem::addDevicesRoleForCapturePreset(audioSource, role, outputDevices));
475 EXPECT_EQ(BAD_VALUE,
476 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, outputDevices));
477
478 // Test invalid role
479 AudioDeviceTypeAddrVector devices;
480 EXPECT_EQ(BAD_VALUE, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource,
481 DEVICE_ROLE_NONE, devices));
482 EXPECT_EQ(BAD_VALUE, AudioSystem::setDevicesRoleForCapturePreset(audioSource, DEVICE_ROLE_NONE,
483 inputDevices));
484 EXPECT_EQ(BAD_VALUE, AudioSystem::addDevicesRoleForCapturePreset(audioSource, DEVICE_ROLE_NONE,
485 inputDevices));
486 EXPECT_EQ(BAD_VALUE, AudioSystem::removeDevicesRoleForCapturePreset(
487 audioSource, DEVICE_ROLE_NONE, inputDevices));
488 EXPECT_EQ(BAD_VALUE,
489 AudioSystem::clearDevicesRoleForCapturePreset(audioSource, DEVICE_ROLE_NONE));
490
491 // Without setting, call get/remove/clear must fail
492 EXPECT_EQ(NAME_NOT_FOUND,
493 AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
494 EXPECT_TRUE(devices.empty());
495 EXPECT_EQ(NAME_NOT_FOUND,
496 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, devices));
497 EXPECT_EQ(NAME_NOT_FOUND, AudioSystem::clearDevicesRoleForCapturePreset(audioSource, role));
498
499 // Test set/get devices role
500 EXPECT_EQ(NO_ERROR,
501 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, inputDevices));
502 ASSERT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
503 EXPECT_EQ(devices, inputDevices);
504
505 // Test setting will change the previously set devices
506 const AudioDeviceTypeAddr inputDevice2 = AudioDeviceTypeAddr(inDeviceB, "");
507 AudioDeviceTypeAddrVector inputDevices2 = {inputDevice2};
508 EXPECT_EQ(NO_ERROR,
509 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, inputDevices2));
510 devices.clear();
511 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
512 EXPECT_EQ(devices, inputDevices2);
513
514 // Test add devices
515 EXPECT_EQ(NO_ERROR,
516 AudioSystem::addDevicesRoleForCapturePreset(audioSource, role, inputDevices));
517 devices.clear();
518 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
519 EXPECT_EQ(2, devices.size());
520 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice) != devices.end());
521 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice2) != devices.end());
522
523 // Test remove devices
524 EXPECT_EQ(NO_ERROR,
525 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, inputDevices));
526 devices.clear();
527 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
528 EXPECT_EQ(devices, inputDevices2);
529
530 // Test remove devices that are not set as the device role
531 EXPECT_EQ(BAD_VALUE,
532 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, inputDevices));
533
534 // Test clear devices
535 EXPECT_EQ(NO_ERROR, AudioSystem::clearDevicesRoleForCapturePreset(audioSource, role));
536 devices.clear();
537 EXPECT_EQ(NAME_NOT_FOUND,
538 AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
539
540 AudioDeviceTypeAddrVector inputDevices3 = {inputDevice, inputDevice2};
541 EXPECT_EQ(NO_ERROR,
542 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, inputDevices3));
543 devices.clear();
544 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
545 EXPECT_EQ(2, devices.size());
546 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice) != devices.end());
547 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice2) != devices.end());
548 EXPECT_EQ(NO_ERROR, AudioSystem::clearDevicesRoleForCapturePreset(audioSource, role));
549}
550
551TEST_F(AudioSystemTest, UidDeviceAffinities) {
552 uid_t uid = getuid();
553
554 // Test invalid device for example audio_is_input_device
555 AudioDeviceTypeAddr inputDevice(AUDIO_DEVICE_IN_BUILTIN_MIC, "");
556 AudioDeviceTypeAddrVector inputDevices = {inputDevice};
557 EXPECT_EQ(BAD_VALUE, AudioSystem::setUidDeviceAffinities(uid, inputDevices));
558
559 // Test valid device for example audio_is_output_device
560 AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, "");
561 AudioDeviceTypeAddrVector outputDevices = {outputDevice};
562 EXPECT_EQ(NO_ERROR, AudioSystem::setUidDeviceAffinities(uid, outputDevices));
563 EXPECT_EQ(NO_ERROR, AudioSystem::removeUidDeviceAffinities(uid));
564}
565
566TEST_F(AudioSystemTest, UserIdDeviceAffinities) {
567 int userId = 200;
568
569 // Test invalid device for example audio_is_input_device
570 AudioDeviceTypeAddr inputDevice(AUDIO_DEVICE_IN_BUILTIN_MIC, "");
571 AudioDeviceTypeAddrVector inputDevices = {inputDevice};
572 EXPECT_EQ(BAD_VALUE, AudioSystem::setUserIdDeviceAffinities(userId, inputDevices));
573
574 // Test valid device for ezample audio_is_output_device
575 AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, "");
576 AudioDeviceTypeAddrVector outputDevices = {outputDevice};
577 EXPECT_EQ(NO_ERROR, AudioSystem::setUserIdDeviceAffinities(userId, outputDevices));
578 EXPECT_EQ(NO_ERROR, AudioSystem::removeUserIdDeviceAffinities(userId));
579}