1 /*
2 * Copyright (C) 2019 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 "audiofoundation_parcelable_test"
18
19 #include <gtest/gtest.h>
20
21 #include <binder/IServiceManager.h>
22 #include <binder/Parcelable.h>
23 #include <binder/ProcessState.h>
24 #include <media/AudioGain.h>
25 #include <media/AudioPort.h>
26 #include <media/AudioProfile.h>
27 #include <media/DeviceDescriptorBase.h>
28 #include <utils/Log.h>
29 #include <utils/String16.h>
30
31 namespace android {
32
33 static const audio_port_config TEST_AUDIO_PORT_CONFIG = {
34 .id = 0,
35 .role = AUDIO_PORT_ROLE_SINK,
36 .type = AUDIO_PORT_TYPE_DEVICE,
37 .config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | AUDIO_PORT_CONFIG_CHANNEL_MASK |
38 AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_GAIN,
39 .sample_rate = 48000,
40 .channel_mask = AUDIO_CHANNEL_OUT_STEREO,
41 .format = AUDIO_FORMAT_PCM_16_BIT,
42 .gain = {
43 .index = 0,
44 .mode = AUDIO_GAIN_MODE_JOINT,
45 .channel_mask = AUDIO_CHANNEL_OUT_STEREO,
46 }
47 };
48
49 class AudioPortConfigTestStub : public AudioPortConfig {
50 public:
getAudioPort() const51 sp<AudioPort> getAudioPort() const override { return nullptr; }
52 };
53
getAudioGainsForTest()54 AudioGains getAudioGainsForTest() {
55 AudioGains audioGains;
56 sp<AudioGain> audioGain = new AudioGain(0 /*index*/, false /*useInChannelMask*/);
57 audioGain->setMode(AUDIO_GAIN_MODE_JOINT);
58 audioGain->setChannelMask(AUDIO_CHANNEL_OUT_STEREO);
59 audioGain->setMinValueInMb(-3200);
60 audioGain->setMaxValueInMb(600);
61 audioGain->setDefaultValueInMb(0);
62 audioGain->setStepValueInMb(100);
63 audioGain->setMinRampInMs(100);
64 audioGain->setMaxRampInMs(500);
65 audioGains.push_back(audioGain);
66 return audioGains;
67 }
68
getAudioProfileVectorForTest()69 AudioProfileVector getAudioProfileVectorForTest() {
70 AudioProfileVector audioProfiles;
71 sp<AudioProfile> audioProfile = AudioProfile::createFullDynamic();
72 audioProfile->setChannels({AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO});
73 audioProfile->setSampleRates({48000});
74 audioProfiles.add(audioProfile);
75 return audioProfiles;
76 }
77
TEST(AudioFoundationParcelableTest,ParcelingAudioGain)78 TEST(AudioFoundationParcelableTest, ParcelingAudioGain) {
79 Parcel data;
80 AudioGains audioGains = getAudioGainsForTest();
81
82 ASSERT_EQ(data.writeParcelable(audioGains), NO_ERROR);
83 data.setDataPosition(0);
84 AudioGains audioGainsFromParcel;
85 ASSERT_EQ(data.readParcelable(&audioGainsFromParcel), NO_ERROR);
86 ASSERT_TRUE(audioGainsFromParcel.equals(audioGains));
87 }
88
TEST(AudioFoundationParcelableTest,ParcelingAudioProfileVector)89 TEST(AudioFoundationParcelableTest, ParcelingAudioProfileVector) {
90 Parcel data;
91 AudioProfileVector audioProfiles = getAudioProfileVectorForTest();
92
93 ASSERT_EQ(data.writeParcelable(audioProfiles), NO_ERROR);
94 data.setDataPosition(0);
95 AudioProfileVector audioProfilesFromParcel;
96 ASSERT_EQ(data.readParcelable(&audioProfilesFromParcel), NO_ERROR);
97 ASSERT_TRUE(audioProfilesFromParcel.equals(audioProfiles));
98 }
99
TEST(AudioFoundationParcelableTest,ParcelingAudioPort)100 TEST(AudioFoundationParcelableTest, ParcelingAudioPort) {
101 Parcel data;
102 sp<AudioPort> audioPort = new AudioPort(
103 "AudioPortName", AUDIO_PORT_TYPE_DEVICE, AUDIO_PORT_ROLE_SINK);
104 audioPort->setGains(getAudioGainsForTest());
105 audioPort->setAudioProfiles(getAudioProfileVectorForTest());
106
107 ASSERT_EQ(data.writeParcelable(*audioPort), NO_ERROR);
108 data.setDataPosition(0);
109 sp<AudioPort> audioPortFromParcel = new AudioPort(
110 "", AUDIO_PORT_TYPE_NONE, AUDIO_PORT_ROLE_NONE);
111 ASSERT_EQ(data.readParcelable(audioPortFromParcel.get()), NO_ERROR);
112 ASSERT_TRUE(audioPortFromParcel->equals(audioPort));
113 }
114
TEST(AudioFoundationParcelableTest,ParcelingAudioPortConfig)115 TEST(AudioFoundationParcelableTest, ParcelingAudioPortConfig) {
116 Parcel data;
117 sp<AudioPortConfig> audioPortConfig = new AudioPortConfigTestStub();
118 audioPortConfig->applyAudioPortConfig(&TEST_AUDIO_PORT_CONFIG);
119
120 ASSERT_EQ(data.writeParcelable(*audioPortConfig), NO_ERROR);
121 data.setDataPosition(0);
122 sp<AudioPortConfig> audioPortConfigFromParcel = new AudioPortConfigTestStub();
123 ASSERT_EQ(data.readParcelable(audioPortConfigFromParcel.get()), NO_ERROR);
124 ASSERT_TRUE(audioPortConfigFromParcel->equals(audioPortConfig));
125 }
126
TEST(AudioFoundationParcelableTest,ParcelingDeviceDescriptorBase)127 TEST(AudioFoundationParcelableTest, ParcelingDeviceDescriptorBase) {
128 Parcel data;
129 sp<DeviceDescriptorBase> desc = new DeviceDescriptorBase(AUDIO_DEVICE_OUT_SPEAKER);
130 desc->setGains(getAudioGainsForTest());
131 desc->setAudioProfiles(getAudioProfileVectorForTest());
132 desc->applyAudioPortConfig(&TEST_AUDIO_PORT_CONFIG);
133 desc->setAddress("DeviceDescriptorBaseTestAddress");
134 ASSERT_EQ(desc->setEncapsulationModes(1 << AUDIO_ENCAPSULATION_MODE_HANDLE), NO_ERROR);
135 ASSERT_EQ(desc->setEncapsulationMetadataTypes(
136 AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS), NO_ERROR);
137
138 ASSERT_EQ(data.writeParcelable(*desc), NO_ERROR);
139 data.setDataPosition(0);
140 sp<DeviceDescriptorBase> descFromParcel = new DeviceDescriptorBase(AUDIO_DEVICE_NONE);
141 ASSERT_EQ(data.readParcelable(descFromParcel.get()), NO_ERROR);
142 ASSERT_TRUE(descFromParcel->equals(desc));
143 }
144
145 } // namespace android
146