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 /*isInput*/);
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,ParcelingAudioProfile)78 TEST(AudioFoundationParcelableTest, ParcelingAudioProfile) {
79     sp<AudioProfile> profile = getAudioProfileVectorForTest()[0];
80     auto conv = legacy2aidl_AudioProfile(profile, false /*isInput*/);
81     ASSERT_TRUE(conv.ok());
82     auto convBack = aidl2legacy_AudioProfile(conv.value(), false /*isInput*/);
83     ASSERT_TRUE(convBack.ok());
84     ASSERT_TRUE(profile->equals(convBack.value()));
85 }
86 
TEST(AudioFoundationParcelableTest,ParcelingAudioProfileVector)87 TEST(AudioFoundationParcelableTest, ParcelingAudioProfileVector) {
88     AudioProfileVector profiles = getAudioProfileVectorForTest();
89     auto conv = legacy2aidl_AudioProfileVector(profiles, false /*isInput*/);
90     ASSERT_TRUE(conv.ok());
91     auto convBack = aidl2legacy_AudioProfileVector(conv.value(), false /*isInput*/);
92     ASSERT_TRUE(convBack.ok());
93     ASSERT_TRUE(profiles.equals(convBack.value()));
94 }
95 
TEST(AudioFoundationParcelableTest,ParcelingAudioGain)96 TEST(AudioFoundationParcelableTest, ParcelingAudioGain) {
97     sp<AudioGain> audioGain = getAudioGainsForTest()[0];
98     auto conv = legacy2aidl_AudioGain(audioGain);
99     ASSERT_TRUE(conv.ok());
100     auto convBack = aidl2legacy_AudioGain(conv.value());
101     ASSERT_TRUE(convBack.ok());
102     ASSERT_TRUE(audioGain->equals(convBack.value()));
103 }
104 
TEST(AudioFoundationParcelableTest,ParcelingAudioGains)105 TEST(AudioFoundationParcelableTest, ParcelingAudioGains) {
106     AudioGains audioGains = getAudioGainsForTest();
107     auto conv = legacy2aidl_AudioGains(audioGains);
108     ASSERT_TRUE(conv.ok());
109     auto convBack = aidl2legacy_AudioGains(conv.value());
110     ASSERT_TRUE(convBack.ok());
111     ASSERT_TRUE(audioGains.equals(convBack.value()));
112 }
113 
TEST(AudioFoundationParcelableTest,ParcelingAudioPort)114 TEST(AudioFoundationParcelableTest, ParcelingAudioPort) {
115     sp<AudioPort> audioPort = new AudioPort(
116             "AudioPortName", AUDIO_PORT_TYPE_DEVICE, AUDIO_PORT_ROLE_SINK);
117     audioPort->setGains(getAudioGainsForTest());
118     audioPort->setAudioProfiles(getAudioProfileVectorForTest());
119 
120     media::AudioPortFw parcelable;
121     ASSERT_EQ(NO_ERROR, audioPort->writeToParcelable(&parcelable));
122     sp<AudioPort> audioPortFromParcel = new AudioPort(
123             "", AUDIO_PORT_TYPE_NONE, AUDIO_PORT_ROLE_NONE);
124     ASSERT_EQ(NO_ERROR, audioPortFromParcel->readFromParcelable(parcelable));
125     ASSERT_TRUE(audioPortFromParcel->equals(audioPort));
126 }
127 
TEST(AudioFoundationParcelableTest,ParcelingAudioPortConfig)128 TEST(AudioFoundationParcelableTest, ParcelingAudioPortConfig) {
129     const bool isInput = false;
130     Parcel data;
131     sp<AudioPortConfig> audioPortConfig = new AudioPortConfigTestStub();
132     audioPortConfig->applyAudioPortConfig(&TEST_AUDIO_PORT_CONFIG);
133     media::audio::common::AudioPortConfig parcelable{};
134     ASSERT_EQ(NO_ERROR, audioPortConfig->writeToParcelable(&parcelable, isInput));
135     ASSERT_EQ(NO_ERROR, data.writeParcelable(parcelable));
136     data.setDataPosition(0);
137     media::audio::common::AudioPortConfig parcelableFromParcel{};
138     ASSERT_EQ(NO_ERROR, data.readParcelable(&parcelableFromParcel));
139     sp<AudioPortConfig> audioPortConfigFromParcel = new AudioPortConfigTestStub();
140     ASSERT_EQ(NO_ERROR, audioPortConfigFromParcel->readFromParcelable(
141                     parcelableFromParcel, isInput));
142     ASSERT_TRUE(audioPortConfigFromParcel->equals(audioPortConfig, isInput));
143 }
144 
TEST(AudioFoundationParcelableTest,ParcelingDeviceDescriptorBase)145 TEST(AudioFoundationParcelableTest, ParcelingDeviceDescriptorBase) {
146     sp<DeviceDescriptorBase> desc = new DeviceDescriptorBase(AUDIO_DEVICE_OUT_SPEAKER);
147     desc->setGains(getAudioGainsForTest());
148     desc->setAudioProfiles(getAudioProfileVectorForTest());
149     desc->applyAudioPortConfig(&TEST_AUDIO_PORT_CONFIG);
150     desc->setAddress("DeviceDescriptorBaseTestAddress");
151     ASSERT_EQ(desc->setEncapsulationModes(1 << AUDIO_ENCAPSULATION_MODE_HANDLE), NO_ERROR);
152     ASSERT_EQ(desc->setEncapsulationMetadataTypes(
153             AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS), NO_ERROR);
154 
155     media::AudioPortFw parcelable;
156     ASSERT_EQ(NO_ERROR, desc->writeToParcelable(&parcelable));
157     sp<DeviceDescriptorBase> descFromParcel = new DeviceDescriptorBase(AUDIO_DEVICE_NONE);
158     ASSERT_EQ(NO_ERROR, descFromParcel->readFromParcelable(parcelable));
159     ASSERT_TRUE(descFromParcel->equals(desc));
160 }
161 
162 } // namespace android
163