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