Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2011 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 /**
     18  * Test for testing the creation of OpenSL ES objects under various configurations (determined
     19  * by their data source and sink types) that are expected to be supported.
     20  * The tests verify the creation and completion of the call to Realize() for the following objects:
     21  *   - Engine
     22  *   - OutputMix
     23  *   - AudioPlayer:
     24  *       * source is URI
     25  *       * source is FD
     26  *       * source is BufferQueue of PCM buffers
     27  *       * source is AndroidBufferQueue of MP2TS buffers
     28  *       * source is URI, sink is BufferQueue of PCM buffers
     29  *       * source is FD, sink is BufferQueue of PCM buffers
     30  *       * source is AndroidBufferQueue of AAC ADTS buffers, sink is BufferQueue of PCM buffers
     31  *   - AudioRecorder
     32  *       * source is IO device, sink is BufferQueue of PCM buffers
     33  */
     34 
     35 #define LOG_NDEBUG 0
     36 #define LOG_TAG "SLObjectCreationTest"
     37 
     38 #include <gtest/gtest.h>
     39 #include <utils/Log.h>
     40 
     41 #include "SLES/OpenSLES.h"
     42 #include "SLES/OpenSLES_Android.h"
     43 #include "OpenSLESUT.h"
     44 
     45 //-----------------------------------------------------------------
     46 /* Checks for error and displays the error code if any */
     47 bool IsOk(SLresult res) {
     48     if (SL_RESULT_SUCCESS != res) {
     49         const char *str = slesutResultToString(res);
     50         if (NULL == str)
     51             str = "unknown";
     52         fprintf(stderr, "IsOk failure: %s (0x%x), exiting\n", str, res);
     53         return false;
     54     }
     55     return true;
     56 }
     57 
     58 //-----------------------------------------------------------------
     59 class SLObjectCreationTest : public ::testing::Test {
     60 
     61 protected:
     62     SLresult res;
     63     SLObjectItf engineObj, outputMixObj, audioPlayerObj;
     64     SLEngineItf engineItf;
     65 
     66     SLDataSource audioSource;
     67     SLDataSink   audioSink;
     68     SLDataLocator_URI locatorUriSrc;
     69     SLDataLocator_AndroidBufferQueue locatorAbqSrc;
     70     SLDataLocator_AndroidFD locatorFdSrc;
     71     SLDataFormat_MIME formatMimeSrc;
     72 
     73     SLDataLocator_OutputMix locatorOutputmixSnk;
     74     SLDataLocator_AndroidSimpleBufferQueue locatorBqSnk;
     75     SLDataFormat_PCM formatPcmSnk;
     76 
     77     SLObjectCreationTest() { }
     78 
     79     virtual ~SLObjectCreationTest() { }
     80 
     81     /* Test setup*/
     82     virtual void SetUp() {
     83         ALOGV("Test Setup()");
     84         res = SL_RESULT_UNKNOWN_ERROR;
     85         engineItf = NULL;
     86         engineObj = NULL;
     87         outputMixObj = NULL;
     88         audioPlayerObj = NULL;
     89         // Engine creation
     90         res = slCreateEngine(&engineObj, 0, NULL, 0, NULL, NULL);
     91         ASSERT_TRUE(IsOk(res));
     92         res = (*engineObj)->Realize(engineObj, SL_BOOLEAN_FALSE);
     93         ASSERT_TRUE(IsOk(res));
     94         res = (*engineObj)->GetInterface(engineObj, SL_IID_ENGINE, &engineItf);
     95         ASSERT_TRUE(IsOk(res));
     96         ASSERT_TRUE(NULL != engineItf);
     97     }
     98 
     99     virtual void TearDown() {
    100         ALOGV("Test TearDown()");
    101         if (audioPlayerObj) {
    102             (*audioPlayerObj)->Destroy(audioPlayerObj);
    103             audioPlayerObj = NULL;
    104         }
    105         if (outputMixObj) {
    106             (*outputMixObj)->Destroy(outputMixObj);
    107             outputMixObj = NULL;
    108         }
    109         if (engineObj){
    110             (*engineObj)->Destroy(engineObj);
    111             engineObj = NULL;
    112         }
    113     }
    114 
    115     //---------------------------------------------------------------------------------------------
    116     // Test implementation convenience methods (to avoid code duplication)
    117 
    118     void AudioPlayerCreation() {
    119         res = (*engineItf)->CreateAudioPlayer(engineItf, &audioPlayerObj,
    120                 &audioSource, &audioSink, 0, NULL/*iidArray*/, NULL/*required*/);
    121         ASSERT_TRUE(IsOk(res));
    122         ASSERT_TRUE(NULL != audioPlayerObj);
    123         res = (*audioPlayerObj)->Realize(audioPlayerObj, SL_BOOLEAN_FALSE);
    124         ASSERT_TRUE(IsOk(res));
    125     }
    126 
    127     void OutputMixSinkInitialization() {
    128         locatorOutputmixSnk.locatorType = SL_DATALOCATOR_OUTPUTMIX;
    129         locatorOutputmixSnk.outputMix = outputMixObj; // created in OutputMixCreation()
    130         audioSink.pLocator = &locatorOutputmixSnk;
    131         audioSink.pFormat = NULL;
    132     }
    133 
    134     void UriSourceInitialization() {
    135         locatorUriSrc.locatorType = SL_DATALOCATOR_URI;
    136         locatorUriSrc.URI = (SLchar*) "/dummyPath/dummyFile.mp3";
    137         formatMimeSrc.formatType = SL_DATAFORMAT_MIME;
    138         formatMimeSrc.mimeType = (SLchar *) NULL;
    139         formatMimeSrc.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
    140         audioSource.pLocator = &locatorUriSrc;
    141         audioSource.pFormat = &formatMimeSrc;
    142     }
    143 
    144     void FdSourceInitialization() {
    145         locatorFdSrc.locatorType = SL_DATALOCATOR_ANDROIDFD;
    146         locatorFdSrc.fd = (SLint32) 1;// a positive value to fake a valid FD
    147         locatorFdSrc.length = 10;
    148         locatorFdSrc.offset = 0;
    149         formatMimeSrc.formatType = SL_DATAFORMAT_MIME;
    150         formatMimeSrc.mimeType = (SLchar *) NULL;
    151         formatMimeSrc.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
    152         audioSource.pLocator = &locatorFdSrc;
    153         audioSource.pFormat = &formatMimeSrc;
    154     }
    155 
    156     void PcmBqSinkInitialization() {
    157         locatorBqSnk.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
    158         locatorBqSnk.numBuffers = 16;
    159         formatPcmSnk.formatType = SL_DATAFORMAT_PCM;
    160         formatPcmSnk.numChannels = 1;
    161         formatPcmSnk.samplesPerSec = SL_SAMPLINGRATE_8;
    162         formatPcmSnk.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
    163         formatPcmSnk.containerSize = 16;
    164         formatPcmSnk.channelMask = SL_SPEAKER_FRONT_LEFT;
    165         formatPcmSnk.endianness = SL_BYTEORDER_LITTLEENDIAN;
    166         audioSink.pLocator = (void *) &locatorBqSnk;
    167         audioSink.pFormat  = (void *) &formatPcmSnk;
    168     }
    169 
    170     //---------------------------------------------------------------------------------------------
    171     // Tests
    172 
    173     /* Test case for creating an AudioPlayer object */
    174     void OutputMixCreation() {
    175         res = (*engineItf)->CreateOutputMix(engineItf, &outputMixObj,
    176                 0, NULL/*iidArray*/, NULL/*required*/);
    177         ASSERT_TRUE(IsOk(res));
    178         ASSERT_TRUE(NULL != outputMixObj);
    179         res = (*outputMixObj)->Realize(outputMixObj, SL_BOOLEAN_FALSE);
    180         ASSERT_TRUE(IsOk(res));
    181     }
    182 
    183     /* Test case for creating an AudioPlayer object that plays from a URI */
    184     void AudioPlayerFromUriCreation() {
    185         // source: URI
    186         UriSourceInitialization();
    187         // sink: OutputMix
    188         OutputMixSinkInitialization();
    189         // AudioPlayer creation
    190         AudioPlayerCreation();
    191     }
    192 
    193     /* Test case for creating an AudioPlayer object that plays from a FD */
    194     void AudioPlayerFromFdCreation() {
    195         // source: FD
    196         FdSourceInitialization();
    197         // sink: OutputMix
    198         OutputMixSinkInitialization();
    199         // AudioPlayer creation
    200         AudioPlayerCreation();
    201     }
    202 
    203     /* Test case for creating an AudioPlayer object that plays from a PCM BufferQueue */
    204     void AudioPlayerFromPcmBqCreation() {
    205         // source: PCM BufferQueue
    206         SLDataLocator_BufferQueue locatorBufferQueue;
    207         locatorBufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
    208         locatorBufferQueue.numBuffers = 16;
    209         SLDataFormat_PCM pcm;
    210         pcm.formatType = SL_DATAFORMAT_PCM;
    211         pcm.numChannels = 2;
    212         pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
    213         pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
    214         pcm.containerSize = 16;
    215         pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
    216         pcm.endianness  = SL_BYTEORDER_LITTLEENDIAN;
    217         audioSource.pLocator = &locatorBufferQueue;
    218         audioSource.pFormat = &pcm;
    219 
    220         // sink: OutputMix
    221         OutputMixSinkInitialization();
    222         // AudioPlayer creation
    223         AudioPlayerCreation();
    224     }
    225 
    226     /* Test case for creating an AudioPlayer object that plays from Transport Stream ABQ */
    227     void AudioPlayerFromTsAbqCreation() {
    228         // source: transport stream in an AndroidBufferQueue
    229         locatorAbqSrc.locatorType  = SL_DATALOCATOR_ANDROIDBUFFERQUEUE;
    230         locatorAbqSrc.numBuffers   = 16;
    231         formatMimeSrc.formatType    = SL_DATAFORMAT_MIME;
    232         formatMimeSrc.mimeType      = (SLchar *) "video/mp2ts";
    233         formatMimeSrc.containerType = SL_CONTAINERTYPE_MPEG_TS;
    234         audioSource.pFormat  = (void *)&formatMimeSrc;
    235         audioSource.pLocator = (void *)&locatorAbqSrc;
    236 
    237         // sink: OutputMix
    238         OutputMixSinkInitialization();
    239         // AudioPlayer creation
    240         AudioPlayerCreation();
    241     }
    242 
    243     /* Test case for creating an AudioPlayer object that decodes from a URI to a PCM BQ */
    244     void AudioPlayerFromUriToPcmBqCreation() {
    245         // source: URI
    246         UriSourceInitialization();
    247         // sink: PCM BufferQueue
    248         PcmBqSinkInitialization();
    249         // AudioPlayer creation
    250         AudioPlayerCreation();
    251     }
    252 
    253     /* Test case for creating an AudioPlayer object that decodes from a FD to a PCM BQ */
    254     void AudioPlayerFromFdToPcmBqCreation() {
    255         // source: FD
    256         FdSourceInitialization();
    257         // sink: PCM BufferQueue
    258         PcmBqSinkInitialization();
    259         // AudioPlayer creation
    260         AudioPlayerCreation();
    261     }
    262 
    263     /* Test case for creating an AudioPlayer object that decodes from a ADTS ABQ to a PCM BQ */
    264     void AudioPlayerFromAdtsAbqToPcmBqCreation() {
    265         // source: ADTS AndroidBufferQueue
    266         locatorAbqSrc.locatorType = SL_DATALOCATOR_ANDROIDBUFFERQUEUE;
    267         locatorAbqSrc.numBuffers  = 16;
    268         formatMimeSrc.formatType    =  SL_DATAFORMAT_MIME;
    269         formatMimeSrc.mimeType      = (SLchar *)"audio/aac-adts";
    270         formatMimeSrc.containerType = SL_CONTAINERTYPE_RAW;
    271         audioSource.pLocator = (void *) &locatorAbqSrc;
    272         audioSource.pFormat  = (void *) &formatMimeSrc;
    273 
    274         // sink: PCM BufferQueue
    275         PcmBqSinkInitialization();
    276         // AudioPlayer creation
    277         AudioPlayerCreation();
    278     }
    279 
    280     /* Test case for creating an AudioRecorder object */
    281     void AudioRecorderCreation(bool doNotRealize = false) {
    282         // source: IO device
    283         SLDataLocator_IODevice locatorIoDeviceSrc;
    284         locatorIoDeviceSrc.locatorType = SL_DATALOCATOR_IODEVICE;
    285         locatorIoDeviceSrc.deviceType = SL_IODEVICE_AUDIOINPUT;
    286         locatorIoDeviceSrc.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT;
    287         locatorIoDeviceSrc.device = NULL;
    288         audioSource.pLocator = (void *) &locatorIoDeviceSrc;
    289         audioSource.pFormat  = NULL;
    290 
    291         // sink: PCM BufferQueue
    292         PcmBqSinkInitialization();
    293 
    294         // AudioRecorder creation
    295         SLObjectItf audioRecorderObj = NULL;
    296         res = (*engineItf)->CreateAudioRecorder(engineItf, &audioRecorderObj,
    297                 &audioSource, &audioSink, 0, NULL/*iidArray*/, NULL/*required*/);
    298         ASSERT_TRUE(IsOk(res));
    299         ASSERT_TRUE(NULL != audioRecorderObj);
    300         if (!doNotRealize) {
    301             res = (*audioRecorderObj)->Realize(audioRecorderObj, SL_BOOLEAN_FALSE);
    302             ASSERT_TRUE(IsOk(res));
    303         }
    304 
    305         // AudioRecorder destruction
    306         (*audioRecorderObj)->Destroy(audioRecorderObj);
    307     }
    308 };
    309 
    310 //-------------------------------------------------------------------------------------------------
    311 TEST_F(SLObjectCreationTest, testEngineCreation) {
    312     ALOGV("Test Fixture: EngineCreation");
    313     // nothing to do here that isn't done in SetUp()
    314 }
    315 
    316 TEST_F(SLObjectCreationTest, testOutputMixCreation) {
    317     ALOGV("Test Fixture: OutputMixCreation");
    318     OutputMixCreation();
    319 }
    320 
    321 TEST_F(SLObjectCreationTest, testAudioPlayerFromUriCreation) {
    322     ALOGV("Test Fixture: AudioPlayerFromUriCreation");
    323     // required for AudioPlayer creation
    324     OutputMixCreation();
    325     AudioPlayerFromUriCreation();
    326 }
    327 
    328 TEST_F(SLObjectCreationTest, testAudioPlayerFromFdCreation) {
    329     ALOGV("Test Fixture: AudioPlayerFromFdCreation");
    330     // required for AudioPlayer creation
    331     OutputMixCreation();
    332     AudioPlayerFromFdCreation();
    333 }
    334 
    335 TEST_F(SLObjectCreationTest, testAudioPlayerFromPcmBqCreation) {
    336     ALOGV("Test Fixture: AudioPlayerFromPcmBqCreation");
    337     // required for AudioPlayer creation
    338     OutputMixCreation();
    339     AudioPlayerFromPcmBqCreation();
    340 }
    341 
    342 TEST_F(SLObjectCreationTest, testAudioPlayerFromTsAbqCreation) {
    343     ALOGV("Test Fixture: AudioPlayerFromTsAbqCreation");
    344     // required for AudioPlayer creation
    345     OutputMixCreation();
    346     AudioPlayerFromTsAbqCreation();
    347 }
    348 
    349 TEST_F(SLObjectCreationTest, testAudioPlayerFromUriToPcmBqCreation) {
    350     ALOGV("Test Fixture: AudioPlayerFromUriToPcmBqCreation");
    351     AudioPlayerFromUriToPcmBqCreation();
    352 }
    353 
    354 TEST_F(SLObjectCreationTest, testAudioPlayerFromFdToPcmBqCreation) {
    355     ALOGV("Test Fixture: AudioPlayerFromFdToPcmBqCreation");
    356     AudioPlayerFromFdToPcmBqCreation();
    357 }
    358 
    359 TEST_F(SLObjectCreationTest, testAudioPlayerFromAdtsAbqToPcmBqCreation) {
    360     ALOGV("Test Fixture: AudioPlayerFromAdtsAbqToPcmBqCreation");
    361     AudioPlayerFromAdtsAbqToPcmBqCreation();
    362 }
    363 
    364 TEST_F(SLObjectCreationTest, testAudioRecorderCreation) {
    365     ALOGV("Test Fixture: AudioRecorderCreation");
    366     // cannot Realize as native test cannot have necessary permission.
    367     AudioRecorderCreation(true);
    368 }
    369 
    370 int main(int argc, char **argv) {
    371     testing::InitGoogleTest(&argc, argv);
    372 
    373     return RUN_ALL_TESTS();
    374 }
    375