blob: 2b9b4fa72a9f4c1ab5b3b0b20ccf3bfb56c40c78 [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();
Mikhail Naganovcc56e472023-02-02 17:48:01 -080052 mCbPlayback.clear();
Ram Mohane2e53c92021-12-14 21:53:44 +053053 mPlayback.clear();
54 }
55 if (mCapture) {
56 mCapture->stop();
Mikhail Naganovcc56e472023-02-02 17:48:01 -080057 mCbRecord.clear();
Ram Mohane2e53c92021-12-14 21:53:44 +053058 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
Mikhail Naganov13bc7462023-03-15 13:31:03 -0700217 bool sourceFound = false;
Ram Mohane2e53c92021-12-14 21:53:44 +0530218 for (const auto& port : ports) {
219 if (port.role != AUDIO_PORT_ROLE_SOURCE || port.type != AUDIO_PORT_TYPE_DEVICE) continue;
Mikhail Naganov13bc7462023-03-15 13:31:03 -0700220 if (port.ext.device.type != AUDIO_DEVICE_IN_FM_TUNER) continue;
221 sourceFound = true;
Ram Mohane2e53c92021-12-14 21:53:44 +0530222 sourcePortConfig = port.active_config;
223
224 bool patchFound;
225
226 // start audio source.
227 status_t ret =
228 AudioSystem::startAudioSource(&sourcePortConfig, &attributes, &sourcePortHandle);
Mikhail Naganov13bc7462023-03-15 13:31:03 -0700229 EXPECT_EQ(OK, ret) << "AudioSystem::startAudioSource for source "
230 << audio_device_to_string(port.ext.device.type) << " failed";
231 if (ret != OK) continue;
Ram Mohane2e53c92021-12-14 21:53:44 +0530232
233 // verify that patch is established by the source port.
234 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(port.id, patchFound));
235 EXPECT_EQ(true, patchFound);
236 EXPECT_NE(sourcePortHandle, AUDIO_PORT_HANDLE_NONE);
237
238 if (sourcePortHandle != AUDIO_PORT_HANDLE_NONE) {
239 ret = AudioSystem::stopAudioSource(sourcePortHandle);
Mikhail Naganov13bc7462023-03-15 13:31:03 -0700240 EXPECT_EQ(OK, ret) << "AudioSystem::stopAudioSource failed for handle "
241 << sourcePortHandle;
Ram Mohane2e53c92021-12-14 21:53:44 +0530242 }
243
244 // verify that no source port patch exists.
245 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(port.id, patchFound));
246 EXPECT_EQ(false, patchFound);
247 }
Mikhail Naganov13bc7462023-03-15 13:31:03 -0700248 if (!sourceFound) {
249 GTEST_SKIP() << "No ports suitable for testing";
250 }
Ram Mohane2e53c92021-12-14 21:53:44 +0530251}
252
253TEST_F(AudioSystemTest, CreateAndReleaseAudioPatch) {
254 status_t status;
255 struct audio_patch audioPatch;
256 std::vector<struct audio_port_v7> ports;
257 audio_patch_handle_t audioPatchHandle = AUDIO_PATCH_HANDLE_NONE;
258
259 bool patchFound = false;
260 audio_port_v7 sourcePort{};
261 audio_port_v7 sinkPort{};
262
263 audioPatch.id = 0;
264 audioPatch.num_sources = 1;
265 audioPatch.num_sinks = 1;
266
267 status = listAudioPorts(ports);
268 ASSERT_EQ(OK, status);
269 if (ports.empty()) {
270 GTEST_SKIP() << "No output devices returned by the audio system";
271 }
272
273 for (const auto& port : ports) {
274 if (port.role == AUDIO_PORT_ROLE_SOURCE && port.type == AUDIO_PORT_TYPE_DEVICE) {
275 sourcePort = port;
276 }
277 if (port.role == AUDIO_PORT_ROLE_SINK && port.type == AUDIO_PORT_TYPE_DEVICE &&
278 port.ext.device.type == AUDIO_DEVICE_OUT_SPEAKER) {
279 sinkPort = port;
280 }
281 }
282
283 audioPatch.sources[0] = sourcePort.active_config;
284 audioPatch.sinks[0] = sinkPort.active_config;
285
286 status = AudioSystem::createAudioPatch(&audioPatch, &audioPatchHandle);
287 EXPECT_EQ(OK, status) << "AudioSystem::createAudiopatch failed between source "
288 << sourcePort.ext.device.address << " and sink "
289 << sinkPort.ext.device.address;
290
291 // verify that patch is established between source and the sink.
292 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(sourcePort.id, patchFound));
293 EXPECT_EQ(true, patchFound);
294
295 EXPECT_NE(AUDIO_PORT_HANDLE_NONE, audioPatchHandle);
296 status = AudioSystem::releaseAudioPatch(audioPatchHandle);
297 EXPECT_EQ(OK, status) << "AudioSystem::releaseAudioPatch failed between source "
298 << sourcePort.ext.device.address << " and sink "
299 << sinkPort.ext.device.address;
300
301 // verify that no patch is established between source and the sink after releaseAudioPatch.
302 ASSERT_NO_FATAL_FAILURE(anyPatchContainsInputDevice(sourcePort.id, patchFound));
303 EXPECT_EQ(false, patchFound);
304}
305
306TEST_F(AudioSystemTest, GetAudioPort) {
307 std::vector<struct audio_port_v7> ports;
308 status_t status = listAudioPorts(ports);
309 ASSERT_EQ(OK, status);
310 for (const auto& port : ports) {
311 audio_port_v7 portTest{.id = port.id};
312 EXPECT_EQ(OK, AudioSystem::getAudioPort(&portTest));
313 EXPECT_TRUE(audio_ports_v7_are_equal(&portTest, &port));
314 }
315}
316
317TEST_F(AudioSystemTest, TestPhoneState) {
318 uid_t uid = getuid();
319 EXPECT_EQ(OK, AudioSystem::setPhoneState(AUDIO_MODE_RINGTONE, uid));
320 audio_mode_t state = AudioSystem::getPhoneState();
321 EXPECT_EQ(AUDIO_MODE_RINGTONE, state);
322 EXPECT_EQ(OK, AudioSystem::setPhoneState(AUDIO_MODE_IN_COMMUNICATION, uid));
323 state = AudioSystem::getPhoneState();
324 EXPECT_EQ(AUDIO_MODE_IN_COMMUNICATION, state);
325 EXPECT_EQ(OK, AudioSystem::setPhoneState(AUDIO_MODE_NORMAL, uid));
326 state = AudioSystem::getPhoneState();
327 EXPECT_EQ(AUDIO_MODE_NORMAL, state);
328}
329
330TEST_F(AudioSystemTest, GetDirectProfilesForAttributes) {
331 std::vector<audio_profile> audioProfiles;
332 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
333 attributes.usage = AUDIO_USAGE_MEDIA;
334 attributes.content_type = AUDIO_CONTENT_TYPE_MUSIC;
335 EXPECT_EQ(BAD_VALUE, AudioSystem::getDirectProfilesForAttributes(nullptr, nullptr));
336 EXPECT_EQ(BAD_VALUE, AudioSystem::getDirectProfilesForAttributes(nullptr, &audioProfiles));
337 EXPECT_EQ(BAD_VALUE, AudioSystem::getDirectProfilesForAttributes(&attributes, nullptr));
338 EXPECT_EQ(NO_ERROR, AudioSystem::getDirectProfilesForAttributes(&attributes, &audioProfiles));
339}
340
341bool isPublicStrategy(const AudioProductStrategy& strategy) {
342 bool result = true;
François Gaffie1e2b56f2022-04-01 14:34:29 +0200343 for (auto& attribute : strategy.getVolumeGroupAttributes()) {
Ram Mohane2e53c92021-12-14 21:53:44 +0530344 if (attribute.getAttributes() == AUDIO_ATTRIBUTES_INITIALIZER &&
345 (uint32_t(attribute.getStreamType()) >= AUDIO_STREAM_PUBLIC_CNT)) {
346 result = false;
347 break;
348 }
349 }
350 return result;
351}
352
353TEST_F(AudioSystemTest, DevicesForRoleAndStrategy) {
354 std::vector<struct audio_port_v7> ports;
355 status_t status = listAudioPorts(ports);
356 ASSERT_EQ(OK, status);
357
358 std::vector<struct audio_port_v7> devicePorts;
359 for (const auto& port : ports) {
360 if (port.type == AUDIO_PORT_TYPE_DEVICE && audio_is_output_device(port.ext.device.type)) {
361 devicePorts.push_back(port);
362 }
363 }
364 if (devicePorts.empty()) {
365 GTEST_SKIP() << "No output devices returned by the audio system";
366 }
367
368 AudioProductStrategyVector strategies;
369 EXPECT_EQ(OK, AudioSystem::listAudioProductStrategies(strategies));
370 if (strategies.empty()) {
371 GTEST_SKIP() << "No strategies returned by the audio system";
372 }
373
374 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
375 attributes.usage = AUDIO_USAGE_MEDIA;
376
377 bool hasStrategyForMedia = false;
378 AudioProductStrategy mediaStrategy;
379 for (const auto& strategy : strategies) {
380 if (!isPublicStrategy(strategy)) continue;
381
François Gaffie1e2b56f2022-04-01 14:34:29 +0200382 for (const auto& att : strategy.getVolumeGroupAttributes()) {
Ram Mohane2e53c92021-12-14 21:53:44 +0530383 if (strategy.attributesMatches(att.getAttributes(), attributes)) {
384 hasStrategyForMedia = true;
385 mediaStrategy = strategy;
386 break;
387 }
388 }
389 }
390
391 if (!hasStrategyForMedia) {
392 GTEST_SKIP() << "No strategies returned for music media";
393 }
394
395 AudioDeviceTypeAddrVector devices;
396 EXPECT_EQ(BAD_VALUE, AudioSystem::getDevicesForRoleAndStrategy(PRODUCT_STRATEGY_NONE,
397 DEVICE_ROLE_PREFERRED, devices));
398 EXPECT_EQ(BAD_VALUE, AudioSystem::getDevicesForRoleAndStrategy(mediaStrategy.getId(),
399 DEVICE_ROLE_NONE, devices));
400 status = AudioSystem::getDevicesForRoleAndStrategy(mediaStrategy.getId(), DEVICE_ROLE_PREFERRED,
401 devices);
402 if (status == NAME_NOT_FOUND) {
403 AudioDeviceTypeAddrVector outputDevices;
404 for (const auto& port : devicePorts) {
405 if (port.ext.device.type == AUDIO_DEVICE_OUT_SPEAKER) {
406 const AudioDeviceTypeAddr outputDevice(port.ext.device.type,
407 port.ext.device.address);
408 outputDevices.push_back(outputDevice);
409 }
410 }
411 EXPECT_EQ(OK, AudioSystem::setDevicesRoleForStrategy(mediaStrategy.getId(),
412 DEVICE_ROLE_PREFERRED, outputDevices));
413 EXPECT_EQ(OK, AudioSystem::getDevicesForRoleAndStrategy(mediaStrategy.getId(),
414 DEVICE_ROLE_PREFERRED, devices));
415 EXPECT_EQ(devices, outputDevices);
Paul Wang5d7cdb52022-11-22 09:45:06 +0000416 EXPECT_EQ(OK, AudioSystem::clearDevicesRoleForStrategy(mediaStrategy.getId(),
417 DEVICE_ROLE_PREFERRED));
Ram Mohane2e53c92021-12-14 21:53:44 +0530418 EXPECT_EQ(NAME_NOT_FOUND, AudioSystem::getDevicesForRoleAndStrategy(
419 mediaStrategy.getId(), DEVICE_ROLE_PREFERRED, devices));
420 }
421}
422
423TEST_F(AudioSystemTest, VolumeIndexForAttributes) {
424 AudioVolumeGroupVector groups;
425 EXPECT_EQ(OK, AudioSystem::listAudioVolumeGroups(groups));
426 for (const auto& group : groups) {
427 if (group.getAudioAttributes().empty()) continue;
428 const audio_attributes_t attr = group.getAudioAttributes()[0];
429 if (attr == AUDIO_ATTRIBUTES_INITIALIZER) continue;
430 audio_stream_type_t streamType = AudioSystem::attributesToStreamType(attr);
431 if (streamType >= AUDIO_STREAM_PUBLIC_CNT) continue;
432
433 volume_group_t vg;
434 EXPECT_EQ(OK, AudioSystem::getVolumeGroupFromAudioAttributes(attr, vg));
435 EXPECT_EQ(group.getId(), vg);
436
437 int index;
438 EXPECT_EQ(OK,
439 AudioSystem::getVolumeIndexForAttributes(attr, index, AUDIO_DEVICE_OUT_SPEAKER));
440
441 int indexTest;
442 EXPECT_EQ(OK, AudioSystem::getStreamVolumeIndex(streamType, &indexTest,
443 AUDIO_DEVICE_OUT_SPEAKER));
444 EXPECT_EQ(index, indexTest);
445 }
446}
447
448TEST_F(AudioSystemTest, DevicesRoleForCapturePreset) {
449 std::vector<struct audio_port_v7> ports;
450 status_t status = listAudioPorts(ports);
451 ASSERT_EQ(OK, status);
452
453 if (ports.empty()) {
454 GTEST_SKIP() << "No ports returned by the audio system";
455 }
456
457 audio_devices_t inDeviceA = AUDIO_DEVICE_IN_BUILTIN_MIC;
458 audio_devices_t inDeviceB = AUDIO_DEVICE_IN_BUILTIN_MIC;
459 for (const auto& port : ports) {
460 if (port.role != AUDIO_PORT_ROLE_SOURCE || port.type != AUDIO_PORT_TYPE_DEVICE) continue;
461 if (port.ext.device.type == inDeviceA) continue;
462 inDeviceB = port.ext.device.type;
463 break;
464 }
465 const audio_source_t audioSource = AUDIO_SOURCE_MIC;
466 const device_role_t role = DEVICE_ROLE_PREFERRED;
467 const AudioDeviceTypeAddr inputDevice(inDeviceA, "");
468 const AudioDeviceTypeAddrVector inputDevices = {inputDevice};
469 const AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, "");
470 const AudioDeviceTypeAddrVector outputDevices = {outputDevice};
471
472 // Test invalid device when setting
473 EXPECT_EQ(BAD_VALUE,
474 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, outputDevices));
475 EXPECT_EQ(BAD_VALUE,
476 AudioSystem::addDevicesRoleForCapturePreset(audioSource, role, outputDevices));
477 EXPECT_EQ(BAD_VALUE,
478 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, outputDevices));
479
480 // Test invalid role
481 AudioDeviceTypeAddrVector devices;
482 EXPECT_EQ(BAD_VALUE, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource,
483 DEVICE_ROLE_NONE, devices));
484 EXPECT_EQ(BAD_VALUE, AudioSystem::setDevicesRoleForCapturePreset(audioSource, DEVICE_ROLE_NONE,
485 inputDevices));
486 EXPECT_EQ(BAD_VALUE, AudioSystem::addDevicesRoleForCapturePreset(audioSource, DEVICE_ROLE_NONE,
487 inputDevices));
488 EXPECT_EQ(BAD_VALUE, AudioSystem::removeDevicesRoleForCapturePreset(
489 audioSource, DEVICE_ROLE_NONE, inputDevices));
490 EXPECT_EQ(BAD_VALUE,
491 AudioSystem::clearDevicesRoleForCapturePreset(audioSource, DEVICE_ROLE_NONE));
492
493 // Without setting, call get/remove/clear must fail
494 EXPECT_EQ(NAME_NOT_FOUND,
495 AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
496 EXPECT_TRUE(devices.empty());
497 EXPECT_EQ(NAME_NOT_FOUND,
498 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, devices));
499 EXPECT_EQ(NAME_NOT_FOUND, AudioSystem::clearDevicesRoleForCapturePreset(audioSource, role));
500
501 // Test set/get devices role
502 EXPECT_EQ(NO_ERROR,
503 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, inputDevices));
504 ASSERT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
505 EXPECT_EQ(devices, inputDevices);
506
507 // Test setting will change the previously set devices
508 const AudioDeviceTypeAddr inputDevice2 = AudioDeviceTypeAddr(inDeviceB, "");
509 AudioDeviceTypeAddrVector inputDevices2 = {inputDevice2};
510 EXPECT_EQ(NO_ERROR,
511 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, inputDevices2));
512 devices.clear();
513 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
514 EXPECT_EQ(devices, inputDevices2);
515
516 // Test add devices
517 EXPECT_EQ(NO_ERROR,
518 AudioSystem::addDevicesRoleForCapturePreset(audioSource, role, inputDevices));
519 devices.clear();
520 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
521 EXPECT_EQ(2, devices.size());
522 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice) != devices.end());
523 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice2) != devices.end());
524
525 // Test remove devices
526 EXPECT_EQ(NO_ERROR,
527 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, inputDevices));
528 devices.clear();
529 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
530 EXPECT_EQ(devices, inputDevices2);
531
532 // Test remove devices that are not set as the device role
533 EXPECT_EQ(BAD_VALUE,
534 AudioSystem::removeDevicesRoleForCapturePreset(audioSource, role, inputDevices));
535
536 // Test clear devices
537 EXPECT_EQ(NO_ERROR, AudioSystem::clearDevicesRoleForCapturePreset(audioSource, role));
538 devices.clear();
539 EXPECT_EQ(NAME_NOT_FOUND,
540 AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
541
542 AudioDeviceTypeAddrVector inputDevices3 = {inputDevice, inputDevice2};
543 EXPECT_EQ(NO_ERROR,
544 AudioSystem::setDevicesRoleForCapturePreset(audioSource, role, inputDevices3));
545 devices.clear();
546 EXPECT_EQ(NO_ERROR, AudioSystem::getDevicesForRoleAndCapturePreset(audioSource, role, devices));
547 EXPECT_EQ(2, devices.size());
548 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice) != devices.end());
549 EXPECT_TRUE(std::find(devices.begin(), devices.end(), inputDevice2) != devices.end());
550 EXPECT_EQ(NO_ERROR, AudioSystem::clearDevicesRoleForCapturePreset(audioSource, role));
551}
552
553TEST_F(AudioSystemTest, UidDeviceAffinities) {
554 uid_t uid = getuid();
555
556 // Test invalid device for example audio_is_input_device
557 AudioDeviceTypeAddr inputDevice(AUDIO_DEVICE_IN_BUILTIN_MIC, "");
558 AudioDeviceTypeAddrVector inputDevices = {inputDevice};
559 EXPECT_EQ(BAD_VALUE, AudioSystem::setUidDeviceAffinities(uid, inputDevices));
560
561 // Test valid device for example audio_is_output_device
562 AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, "");
563 AudioDeviceTypeAddrVector outputDevices = {outputDevice};
564 EXPECT_EQ(NO_ERROR, AudioSystem::setUidDeviceAffinities(uid, outputDevices));
565 EXPECT_EQ(NO_ERROR, AudioSystem::removeUidDeviceAffinities(uid));
566}
567
568TEST_F(AudioSystemTest, UserIdDeviceAffinities) {
569 int userId = 200;
570
571 // Test invalid device for example audio_is_input_device
572 AudioDeviceTypeAddr inputDevice(AUDIO_DEVICE_IN_BUILTIN_MIC, "");
573 AudioDeviceTypeAddrVector inputDevices = {inputDevice};
574 EXPECT_EQ(BAD_VALUE, AudioSystem::setUserIdDeviceAffinities(userId, inputDevices));
575
576 // Test valid device for ezample audio_is_output_device
577 AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, "");
578 AudioDeviceTypeAddrVector outputDevices = {outputDevice};
579 EXPECT_EQ(NO_ERROR, AudioSystem::setUserIdDeviceAffinities(userId, outputDevices));
580 EXPECT_EQ(NO_ERROR, AudioSystem::removeUserIdDeviceAffinities(userId));
581}