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