1 /*
2  * Copyright (C) 2017 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 "media_omx_hidl_audio_test_common"
18 #include <android-base/logging.h>
19 
20 #include <android/hardware/media/omx/1.0/IOmx.h>
21 #include <android/hardware/media/omx/1.0/IOmxNode.h>
22 #include <android/hardware/media/omx/1.0/IOmxObserver.h>
23 #include <android/hardware/media/omx/1.0/types.h>
24 #include <android/hidl/allocator/1.0/IAllocator.h>
25 #include <android/hidl/memory/1.0/IMapper.h>
26 #include <android/hidl/memory/1.0/IMemory.h>
27 
28 using ::android::hardware::media::omx::V1_0::IOmx;
29 using ::android::hardware::media::omx::V1_0::IOmxObserver;
30 using ::android::hardware::media::omx::V1_0::IOmxNode;
31 using ::android::hardware::media::omx::V1_0::Message;
32 using ::android::hardware::media::omx::V1_0::CodecBuffer;
33 using ::android::hardware::media::omx::V1_0::PortMode;
34 using ::android::hidl::allocator::V1_0::IAllocator;
35 using ::android::hidl::memory::V1_0::IMemory;
36 using ::android::hidl::memory::V1_0::IMapper;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::hardware::hidl_vec;
40 using ::android::hardware::hidl_string;
41 using ::android::sp;
42 
43 #include <VtsHalHidlTargetTestBase.h>
44 #include <hidlmemory/mapping.h>
45 #include <media_audio_hidl_test_common.h>
46 #include <media_hidl_test_common.h>
47 #include <memory>
48 
setAudioPortFormat(sp<IOmxNode> omxNode,OMX_U32 portIndex,OMX_AUDIO_CODINGTYPE eEncoding)49 Return<android::hardware::media::omx::V1_0::Status> setAudioPortFormat(
50     sp<IOmxNode> omxNode, OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE eEncoding) {
51     OMX_U32 index = 0;
52     OMX_AUDIO_PARAM_PORTFORMATTYPE portFormat;
53     std::vector<OMX_AUDIO_CODINGTYPE> arrEncoding;
54     android::hardware::media::omx::V1_0::Status status;
55 
56     while (1) {
57         portFormat.nIndex = index;
58         status = getPortParam(omxNode, OMX_IndexParamAudioPortFormat, portIndex,
59                               &portFormat);
60         if (status != ::android::hardware::media::omx::V1_0::Status::OK) break;
61         arrEncoding.push_back(portFormat.eEncoding);
62         index++;
63         if (index == 512) {
64             // enumerated way too many formats, highly unusual for this to
65             // happen.
66             EXPECT_LE(index, 512U)
67                 << "Expecting OMX_ErrorNoMore but not received";
68             break;
69         }
70     }
71     if (!index) return status;
72     for (index = 0; index < arrEncoding.size(); index++) {
73         if (arrEncoding[index] == eEncoding) {
74             portFormat.eEncoding = arrEncoding[index];
75             break;
76         }
77     }
78     if (index == arrEncoding.size()) {
79         ALOGE("setting default Port format %x", (int)arrEncoding[0]);
80         portFormat.eEncoding = arrEncoding[0];
81     }
82     // In setParam call nIndex shall be ignored as per omx-il specification.
83     // see how this holds up by corrupting nIndex
84     portFormat.nIndex = RANDOM_INDEX;
85     status = setPortParam(omxNode, OMX_IndexParamAudioPortFormat, portIndex,
86                           &portFormat);
87     return status;
88 }
89 
enumerateProfile(sp<IOmxNode> omxNode,OMX_U32 portIndex,std::vector<int32_t> * arrProfile)90 void enumerateProfile(sp<IOmxNode> omxNode, OMX_U32 portIndex,
91                       std::vector<int32_t>* arrProfile) {
92     android::hardware::media::omx::V1_0::Status status;
93     OMX_AUDIO_PARAM_ANDROID_PROFILETYPE param;
94     param.nProfileIndex = 0;
95     arrProfile->clear();
96     while (1) {
97         status = getPortParam(
98             omxNode, (OMX_INDEXTYPE)OMX_IndexParamAudioProfileQuerySupported,
99             portIndex, &param);
100         if (status != ::android::hardware::media::omx::V1_0::Status::OK) break;
101         arrProfile->push_back(static_cast<int32_t>(param.eProfile));
102         param.nProfileIndex++;
103         if (param.nProfileIndex == 512) {
104             // enumerated way too many, highly unusual for this to happen.
105             EXPECT_LE(param.nProfileIndex, 512U)
106                 << "Expecting OMX_ErrorNoMore but not received";
107             break;
108         }
109     }
110 }
111 
setupPCMPort(sp<IOmxNode> omxNode,OMX_U32 portIndex,int32_t nChannels,OMX_NUMERICALDATATYPE eNumData,int32_t nBitPerSample,int32_t nSamplingRate,OMX_AUDIO_PCMMODETYPE ePCMMode)112 void setupPCMPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nChannels,
113                   OMX_NUMERICALDATATYPE eNumData, int32_t nBitPerSample,
114                   int32_t nSamplingRate, OMX_AUDIO_PCMMODETYPE ePCMMode) {
115     OMX_AUDIO_PARAM_PCMMODETYPE param;
116     android::hardware::media::omx::V1_0::Status status;
117     status = getPortParam(omxNode, OMX_IndexParamAudioPcm, portIndex, &param);
118     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
119     param.nChannels = nChannels;
120     param.eNumData = eNumData;
121     param.eEndian = OMX_EndianLittle;
122     param.bInterleaved = OMX_TRUE;
123     param.nBitPerSample = nBitPerSample;
124     param.nSamplingRate = nSamplingRate;
125     param.ePCMMode = ePCMMode;
126     switch (nChannels) {
127         case 1:
128             param.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
129             break;
130         case 2:
131             param.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
132             param.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
133             break;
134         default:
135             EXPECT_TRUE(false);
136     }
137     status = setPortParam(omxNode, OMX_IndexParamAudioPcm, portIndex, &param);
138     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
139 }
140 
setupMP3Port(sp<IOmxNode> omxNode,OMX_U32 portIndex,OMX_AUDIO_MP3STREAMFORMATTYPE eFormat,int32_t nChannels,int32_t nBitRate,int32_t nSampleRate)141 void setupMP3Port(sp<IOmxNode> omxNode, OMX_U32 portIndex,
142                   OMX_AUDIO_MP3STREAMFORMATTYPE eFormat, int32_t nChannels,
143                   int32_t nBitRate, int32_t nSampleRate) {
144     OMX_AUDIO_PARAM_MP3TYPE param;
145     android::hardware::media::omx::V1_0::Status status;
146     status = getPortParam(omxNode, OMX_IndexParamAudioMp3, portIndex, &param);
147     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
148     param.nChannels = nChannels;
149     param.nBitRate = nBitRate;
150     param.nSampleRate = nSampleRate;
151     param.nAudioBandWidth = 0;
152     param.eChannelMode = (nChannels == 1) ? OMX_AUDIO_ChannelModeMono
153                                           : OMX_AUDIO_ChannelModeStereo;
154     param.eFormat = eFormat;
155     status = setPortParam(omxNode, OMX_IndexParamAudioMp3, portIndex, &param);
156     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
157 }
158 
setupFLACPort(sp<IOmxNode> omxNode,OMX_U32 portIndex,int32_t nChannels,int32_t nSampleRate,int32_t nCompressionLevel)159 void setupFLACPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nChannels,
160                    int32_t nSampleRate, int32_t nCompressionLevel) {
161     android::hardware::media::omx::V1_0::Status status;
162     OMX_AUDIO_PARAM_FLACTYPE param;
163     status = getPortParam(omxNode, OMX_IndexParamAudioFlac, portIndex, &param);
164     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
165     param.nChannels = nChannels;
166     param.nSampleRate = nSampleRate;
167     param.nCompressionLevel = nCompressionLevel;
168     status = setPortParam(omxNode, OMX_IndexParamAudioFlac, portIndex, &param);
169     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
170 }
171 
setupOPUSPort(sp<IOmxNode> omxNode,OMX_U32 portIndex,int32_t nChannels,int32_t nBitRate,int32_t nSampleRate)172 void setupOPUSPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nChannels,
173                    int32_t nBitRate, int32_t nSampleRate) {
174     android::hardware::media::omx::V1_0::Status status;
175     OMX_AUDIO_PARAM_ANDROID_OPUSTYPE param;
176     status =
177         getPortParam(omxNode, (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidOpus,
178                      portIndex, &param);
179     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
180     param.nChannels = nChannels;
181     param.nBitRate = nBitRate;
182     param.nSampleRate = nSampleRate;
183     status =
184         setPortParam(omxNode, (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidOpus,
185                      portIndex, &param);
186     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
187 }
188 
pickModeFromBitRate(bool isAMRWB,int32_t bps)189 OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
190     if (isAMRWB) {
191         if (bps <= 6600) return OMX_AUDIO_AMRBandModeWB0;
192         if (bps <= 8850) return OMX_AUDIO_AMRBandModeWB1;
193         if (bps <= 12650) return OMX_AUDIO_AMRBandModeWB2;
194         if (bps <= 14250) return OMX_AUDIO_AMRBandModeWB3;
195         if (bps <= 15850) return OMX_AUDIO_AMRBandModeWB4;
196         if (bps <= 18250) return OMX_AUDIO_AMRBandModeWB5;
197         if (bps <= 19850) return OMX_AUDIO_AMRBandModeWB6;
198         if (bps <= 23050) return OMX_AUDIO_AMRBandModeWB7;
199         return OMX_AUDIO_AMRBandModeWB8;
200     } else {
201         if (bps <= 4750) return OMX_AUDIO_AMRBandModeNB0;
202         if (bps <= 5150) return OMX_AUDIO_AMRBandModeNB1;
203         if (bps <= 5900) return OMX_AUDIO_AMRBandModeNB2;
204         if (bps <= 6700) return OMX_AUDIO_AMRBandModeNB3;
205         if (bps <= 7400) return OMX_AUDIO_AMRBandModeNB4;
206         if (bps <= 7950) return OMX_AUDIO_AMRBandModeNB5;
207         if (bps <= 10200) return OMX_AUDIO_AMRBandModeNB6;
208         return OMX_AUDIO_AMRBandModeNB7;
209     }
210 }
211 
setupAMRPort(sp<IOmxNode> omxNode,OMX_U32 portIndex,int32_t nBitRate,bool isAMRWB)212 void setupAMRPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nBitRate,
213                   bool isAMRWB) {
214     android::hardware::media::omx::V1_0::Status status;
215     OMX_AUDIO_PARAM_AMRTYPE param;
216     status = getPortParam(omxNode, OMX_IndexParamAudioAmr, portIndex, &param);
217     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
218     param.nChannels = 1;
219     param.nBitRate = nBitRate;
220     param.eAMRBandMode = pickModeFromBitRate(isAMRWB, nBitRate);
221     status = setPortParam(omxNode, OMX_IndexParamAudioAmr, portIndex, &param);
222     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
223 }
224 
setupVORBISPort(sp<IOmxNode> omxNode,OMX_U32 portIndex,int32_t nChannels,int32_t nBitRate,int32_t nSampleRate,int32_t nQuality)225 void setupVORBISPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nChannels,
226                      int32_t nBitRate, int32_t nSampleRate, int32_t nQuality) {
227     android::hardware::media::omx::V1_0::Status status;
228     OMX_AUDIO_PARAM_VORBISTYPE param;
229     status =
230         getPortParam(omxNode, OMX_IndexParamAudioVorbis, portIndex, &param);
231     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
232     param.nChannels = nChannels;
233     param.nBitRate = nBitRate;
234     param.nSampleRate = nSampleRate;
235     param.nQuality = nQuality;
236     status =
237         setPortParam(omxNode, OMX_IndexParamAudioVorbis, portIndex, &param);
238     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
239 }
240 
setupAACPort(sp<IOmxNode> omxNode,OMX_U32 portIndex,OMX_AUDIO_AACPROFILETYPE eAACProfile,OMX_AUDIO_AACSTREAMFORMATTYPE eAACStreamFormat,int32_t nChannels,int32_t nBitRate,int32_t nSampleRate)241 void setupAACPort(sp<IOmxNode> omxNode, OMX_U32 portIndex,
242                   OMX_AUDIO_AACPROFILETYPE eAACProfile,
243                   OMX_AUDIO_AACSTREAMFORMATTYPE eAACStreamFormat,
244                   int32_t nChannels, int32_t nBitRate, int32_t nSampleRate) {
245     android::hardware::media::omx::V1_0::Status status;
246     OMX_AUDIO_PARAM_AACPROFILETYPE param;
247     status = getPortParam(omxNode, OMX_IndexParamAudioAac, portIndex, &param);
248     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
249     param.nChannels = nChannels;
250     param.nSampleRate = nSampleRate;
251     param.nBitRate = nBitRate;
252     param.eAACProfile = eAACProfile;
253     param.eAACStreamFormat = eAACStreamFormat;
254     param.eChannelMode = (nChannels == 1) ? OMX_AUDIO_ChannelModeMono
255                                           : OMX_AUDIO_ChannelModeStereo;
256     status = setPortParam(omxNode, OMX_IndexParamAudioAac, portIndex, &param);
257     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
258 }
259