Home | History | Annotate | Download | only in audio
      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 
     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 
     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         default:
     98             EXPECT_TRUE(false);
     99     }
    100     status = setPortParam(omxNode, OMX_IndexParamAudioPcm, portIndex, &param);
    101     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    102 }
    103 
    104 void setupMP3Port(sp<IOmxNode> omxNode, OMX_U32 portIndex,
    105                   OMX_AUDIO_MP3STREAMFORMATTYPE eFormat, int32_t nChannels,
    106                   int32_t nBitRate, int32_t nSampleRate) {
    107     OMX_AUDIO_PARAM_MP3TYPE param;
    108     android::hardware::media::omx::V1_0::Status status;
    109     status = getPortParam(omxNode, OMX_IndexParamAudioMp3, portIndex, &param);
    110     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    111     param.nChannels = nChannels;
    112     param.nBitRate = nBitRate;
    113     param.nSampleRate = nSampleRate;
    114     param.nAudioBandWidth = 0;
    115     param.eChannelMode = (nChannels == 1) ? OMX_AUDIO_ChannelModeMono
    116                                           : OMX_AUDIO_ChannelModeStereo;
    117     param.eFormat = eFormat;
    118     status = setPortParam(omxNode, OMX_IndexParamAudioMp3, portIndex, &param);
    119     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    120 }
    121 
    122 void setupFLACPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nChannels,
    123                    int32_t nSampleRate, int32_t nCompressionLevel) {
    124     android::hardware::media::omx::V1_0::Status status;
    125     OMX_AUDIO_PARAM_FLACTYPE param;
    126     status = getPortParam(omxNode, OMX_IndexParamAudioFlac, portIndex, &param);
    127     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    128     param.nChannels = nChannels;
    129     param.nSampleRate = nSampleRate;
    130     param.nCompressionLevel = nCompressionLevel;
    131     status = setPortParam(omxNode, OMX_IndexParamAudioFlac, portIndex, &param);
    132     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    133 }
    134 
    135 void setupOPUSPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nChannels,
    136                    int32_t nBitRate, int32_t nSampleRate) {
    137     android::hardware::media::omx::V1_0::Status status;
    138     OMX_AUDIO_PARAM_ANDROID_OPUSTYPE param;
    139     status =
    140         getPortParam(omxNode, (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidOpus,
    141                      portIndex, &param);
    142     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    143     param.nChannels = nChannels;
    144     param.nBitRate = nBitRate;
    145     param.nSampleRate = nSampleRate;
    146     status =
    147         setPortParam(omxNode, (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidOpus,
    148                      portIndex, &param);
    149     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    150 }
    151 
    152 OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
    153     if (isAMRWB) {
    154         if (bps <= 6600) return OMX_AUDIO_AMRBandModeWB0;
    155         if (bps <= 8850) return OMX_AUDIO_AMRBandModeWB1;
    156         if (bps <= 12650) return OMX_AUDIO_AMRBandModeWB2;
    157         if (bps <= 14250) return OMX_AUDIO_AMRBandModeWB3;
    158         if (bps <= 15850) return OMX_AUDIO_AMRBandModeWB4;
    159         if (bps <= 18250) return OMX_AUDIO_AMRBandModeWB5;
    160         if (bps <= 19850) return OMX_AUDIO_AMRBandModeWB6;
    161         if (bps <= 23050) return OMX_AUDIO_AMRBandModeWB7;
    162         return OMX_AUDIO_AMRBandModeWB8;
    163     } else {
    164         if (bps <= 4750) return OMX_AUDIO_AMRBandModeNB0;
    165         if (bps <= 5150) return OMX_AUDIO_AMRBandModeNB1;
    166         if (bps <= 5900) return OMX_AUDIO_AMRBandModeNB2;
    167         if (bps <= 6700) return OMX_AUDIO_AMRBandModeNB3;
    168         if (bps <= 7400) return OMX_AUDIO_AMRBandModeNB4;
    169         if (bps <= 7950) return OMX_AUDIO_AMRBandModeNB5;
    170         if (bps <= 10200) return OMX_AUDIO_AMRBandModeNB6;
    171         return OMX_AUDIO_AMRBandModeNB7;
    172     }
    173 }
    174 
    175 void setupAMRPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nBitRate,
    176                   bool isAMRWB) {
    177     android::hardware::media::omx::V1_0::Status status;
    178     OMX_AUDIO_PARAM_AMRTYPE param;
    179     status = getPortParam(omxNode, OMX_IndexParamAudioAmr, portIndex, &param);
    180     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    181     param.nChannels = 1;
    182     param.nBitRate = nBitRate;
    183     param.eAMRBandMode = pickModeFromBitRate(isAMRWB, nBitRate);
    184     status = setPortParam(omxNode, OMX_IndexParamAudioAmr, portIndex, &param);
    185     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    186 }
    187 
    188 void setupVORBISPort(sp<IOmxNode> omxNode, OMX_U32 portIndex, int32_t nChannels,
    189                      int32_t nBitRate, int32_t nSampleRate, int32_t nQuality) {
    190     android::hardware::media::omx::V1_0::Status status;
    191     OMX_AUDIO_PARAM_VORBISTYPE param;
    192     status =
    193         getPortParam(omxNode, OMX_IndexParamAudioVorbis, portIndex, &param);
    194     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    195     param.nChannels = nChannels;
    196     param.nBitRate = nBitRate;
    197     param.nSampleRate = nSampleRate;
    198     param.nQuality = nQuality;
    199     status =
    200         setPortParam(omxNode, OMX_IndexParamAudioVorbis, portIndex, &param);
    201     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    202 }
    203 
    204 void setupAACPort(sp<IOmxNode> omxNode, OMX_U32 portIndex,
    205                   OMX_AUDIO_AACPROFILETYPE eAACProfile,
    206                   OMX_AUDIO_AACSTREAMFORMATTYPE eAACStreamFormat,
    207                   int32_t nChannels, int32_t nBitRate, int32_t nSampleRate) {
    208     android::hardware::media::omx::V1_0::Status status;
    209     OMX_AUDIO_PARAM_AACPROFILETYPE param;
    210     status = getPortParam(omxNode, OMX_IndexParamAudioAac, portIndex, &param);
    211     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    212     param.nChannels = nChannels;
    213     param.nSampleRate = nSampleRate;
    214     param.nBitRate = nBitRate;
    215     param.eAACProfile = eAACProfile;
    216     param.eAACStreamFormat = eAACStreamFormat;
    217     param.eChannelMode = (nChannels == 1) ? OMX_AUDIO_ChannelModeMono
    218                                           : OMX_AUDIO_ChannelModeStereo;
    219     status = setPortParam(omxNode, OMX_IndexParamAudioAac, portIndex, &param);
    220     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
    221 }
    222