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