Home | History | Annotate | Download | only in listening
      1 /*
      2  * Copyright (C) 2010 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 // This test program tortures the seek APIs by positioning "randomly" in a file.
     18 // It needs as input a permuted .wav and .map produced by the permute tool.
     19 
     20 #include "SLES/OpenSLES.h"
     21 #include <assert.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <unistd.h>
     25 
     26 #define ASSERT_EQ(x, y) assert((x) == (y))
     27 
     28 int main(int argc, char **argv)
     29 {
     30     if (argc != 3) {
     31         fprintf(stderr, "usage: %s file.wav file.map\n", argv[0]);
     32         fprintf(stderr, "  where file.wav and file.map are created by the permute tool\n");
     33         return EXIT_FAILURE;
     34     }
     35 
     36     SLresult result;
     37 
     38     // create engine
     39     SLObjectItf engineObject;
     40     result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
     41     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     42     result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
     43     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     44     SLEngineItf engineEngine;
     45     result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
     46     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     47 
     48     // create output mix
     49     SLObjectItf outputmixObject;
     50     result = (*engineEngine)->CreateOutputMix(engineEngine, &outputmixObject, 0, NULL, NULL);
     51     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     52     result = (*outputmixObject)->Realize(outputmixObject, SL_BOOLEAN_FALSE);
     53     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     54 
     55     // create an audio player with URI source and output mix sink
     56     SLDataSource audiosrc;
     57     SLDataSink audiosnk;
     58     SLDataLocator_OutputMix locator_outputmix;
     59     SLDataLocator_URI locator_uri;
     60     locator_uri.locatorType = SL_DATALOCATOR_URI;
     61     locator_uri.URI = (SLchar *) argv[1];
     62     locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
     63     locator_outputmix.outputMix = outputmixObject;
     64     audiosrc.pLocator = &locator_uri;
     65     audiosrc.pFormat = NULL;
     66     audiosnk.pLocator = &locator_outputmix;
     67     audiosnk.pFormat = NULL;
     68     SLObjectItf playerObject;
     69     SLInterfaceID ids[1] = {SL_IID_SEEK};
     70     SLboolean flags[1] = {SL_BOOLEAN_TRUE};
     71     result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk,
     72             1, ids, flags);
     73     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     74     result = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
     75     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     76     SLPlayItf playerPlay;
     77     result = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay);
     78     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     79     SLSeekItf playerSeek;
     80     result = (*playerObject)->GetInterface(playerObject, SL_IID_SEEK, &playerSeek);
     81     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     82     SLmillisecond duration;
     83     result = (*playerPlay)->GetDuration(playerPlay, &duration);
     84     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     85     result = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PAUSED);
     86     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     87     result = (*playerPlay)->GetDuration(playerPlay, &duration);
     88     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     89     result = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING);
     90     ASSERT_EQ(SL_RESULT_SUCCESS, result);
     91 
     92 #if 1
     93     // play back a file in permuted order using the seek map
     94     FILE *fp_map = fopen(argv[2], "r");
     95     if (fp_map != NULL) {
     96         unsigned position, duration;
     97         while (fscanf(fp_map, "%u %u", &position, &duration) == 2) {
     98             printf("%u %u\n", position, duration);
     99             result = (*playerSeek)->SetPosition(playerSeek, (SLmillisecond) position,
    100                     SL_SEEKMODE_ACCURATE);
    101             ASSERT_EQ(SL_RESULT_SUCCESS, result);
    102             if (duration > 0)
    103                 usleep(duration * 1000);
    104         }
    105     }
    106 #else
    107     set_conio_terminal_mode();
    108 
    109     // loop repeatedly, inflicting seek pain each cycle
    110     for (;;) {
    111         if (kbhit()) {
    112             switch (getch()) {
    113             case 'q':
    114                 goto out;
    115             }
    116         }
    117         SLmillisecond delay = 100 + (rand() & 8191);
    118         printf("sleep %u\n", (unsigned) delay);
    119         usleep(delay * 1000);
    120         SLmillisecond newPos = duration * ((rand() & 65535) / 65536.0);
    121         printf("seek %u\n", (unsigned) newPos);
    122         result = (*playerSeek)->SetPosition(playerSeek, newPos, SL_SEEKMODE_ACCURATE);
    123         ASSERT_EQ(SL_RESULT_SUCCESS, result);
    124         SLmillisecond nowPos;
    125         result = (*playerPlay)->GetPosition(playerPlay, &nowPos);
    126         ASSERT_EQ(SL_RESULT_SUCCESS, result);
    127         printf("now %u\n", (unsigned) newPos);
    128     }
    129 out:
    130 #endif
    131 
    132     return EXIT_SUCCESS;
    133 }
    134