slesTestEqFdPath.cpp revision 4c71179974933c5c36cbfc3e8227c8df63248d91
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#define LOG_NDEBUG 0
18#define LOG_TAG "slesTest_seekFdPath"
19
20#include <utils/Log.h>
21#include <getopt.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/time.h>
27#include <fcntl.h>
28
29#include "SLES/OpenSLES.h"
30#include "SLES/OpenSLES_Android.h"
31
32
33#define MAX_NUMBER_INTERFACES 3
34#define MAX_NUMBER_OUTPUT_DEVICES 6
35
36#define TIME_S_BETWEEN_EQ_ON_OFF 3
37
38static int testMode;
39//-----------------------------------------------------------------
40/* Exits the application if an error is encountered */
41#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
42
43void ExitOnErrorFunc( SLresult result , int line)
44{
45    if (SL_RESULT_SUCCESS != result) {
46        fprintf(stdout, "%lu error code encountered at line %d, exiting\n", result, line);
47        exit(1);
48    }
49}
50
51
52//-----------------------------------------------------------------
53
54/* Play an audio path by opening a file descriptor on that path  */
55void TestEQPathFromFD( SLObjectItf sl, const char* path, SLAint64 offset, SLAint64 size)
56{
57    SLresult  result;
58    SLEngineItf EngineItf;
59
60    /* Objects this application uses: one player and an ouput mix */
61    SLObjectItf  player, outputMix;
62
63    /* Source of audio data to play */
64    SLDataSource            audioSource;
65    SLDataLocator_AndroidFD locatorFd;
66    SLDataFormat_MIME       mime;
67
68    /* Data sinks for the audio player */
69    SLDataSink               audioSink;
70    SLDataLocator_OutputMix  locator_outputmix;
71
72    /* Play and PrefetchStatus interfaces for the audio player */
73    SLPlayItf              playItf;
74    SLPrefetchStatusItf    prefetchItf;
75    SLEqualizerItf         eqItf;
76
77    SLboolean required[MAX_NUMBER_INTERFACES];
78    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
79
80    /* Get the SL Engine Interface which is implicit */
81    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
82    ExitOnError(result);
83
84    /* Initialize arrays required[] and iidArray[] */
85    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
86        required[i] = SL_BOOLEAN_FALSE;
87        iidArray[i] = SL_IID_NULL;
88    }
89
90    /* ------------------------------------------------------ */
91    /* Configuration of the output mix  */
92
93    /* Create Output Mix object to be used by the player */
94     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
95     ExitOnError(result);
96
97    /* Realize the Output Mix object in synchronous mode */
98    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
99    ExitOnError(result);
100
101    /* Setup the data sink structure */
102    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
103    locator_outputmix.outputMix   = outputMix;
104    audioSink.pLocator            = (void*)&locator_outputmix;
105    audioSink.pFormat             = NULL;
106
107    /* ------------------------------------------------------ */
108    /* Configuration of the player  */
109
110    /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */
111    /*  (SLPlayItf is implicit) */
112    required[0] = SL_BOOLEAN_TRUE;
113    iidArray[0] = SL_IID_PREFETCHSTATUS;
114    required[1] = SL_BOOLEAN_TRUE;
115    iidArray[1] = SL_IID_EQUALIZER;
116
117    /* Setup the data source structure for the URI */
118    locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
119    int fd = open(path, O_RDONLY);
120    if (fd == -1) {
121        ExitOnError(SL_RESULT_RESOURCE_ERROR);
122    }
123    locatorFd.fd = (SLint32) fd;
124    locatorFd.length = size;
125    locatorFd.offset = offset;
126
127    mime.formatType = SL_DATAFORMAT_MIME;
128    /*     this is how ignored mime information is specified, according to OpenSL ES spec
129     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
130    mime.mimeType      = (SLchar*)NULL;
131    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
132
133    audioSource.pFormat  = (void*)&mime;
134    audioSource.pLocator = (void*)&locatorFd;
135
136    /* Create the audio player */
137    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
138            iidArray, required);
139    ExitOnError(result);
140
141    /* Realize the player in synchronous mode. */
142    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
143    fprintf(stdout, "URI example: after Realize\n");
144
145    /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/
146    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
147    ExitOnError(result);
148
149    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
150    ExitOnError(result);
151
152    result = (*player)->GetInterface(player, SL_IID_EQUALIZER, (void*)&eqItf);
153    ExitOnError(result);
154
155    fprintf(stdout, "Player configured\n");
156
157    /* ------------------------------------------------------ */
158    /* Playback and test */
159
160    /* Start the data prefetching by setting the player to the paused state */
161    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
162    ExitOnError(result);
163
164    /* Wait until there's data to play */
165    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
166    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
167        usleep(100 * 1000);
168        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
169        ExitOnError(result);
170    }
171
172    /* Get duration */
173    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
174    result = (*playItf)->GetDuration(playItf, &durationInMsec);
175    ExitOnError(result);
176    if (durationInMsec == SL_TIME_UNKNOWN) {
177        durationInMsec = 5000;
178    }
179
180    /* Start playback */
181    fprintf(stdout, "Starting to play\n");
182    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
183    ExitOnError(result);
184
185    /* Configure EQ */
186    SLuint16 nbPresets, preset, nbBands = 0;
187    result = (*eqItf)->GetNumberOfBands(eqItf, &nbBands);
188    ExitOnError(result);
189    result = (*eqItf)->GetNumberOfPresets(eqItf, &nbPresets);
190    ExitOnError(result);
191    /*    Start from a preset  */
192    preset = nbPresets > 2 ?  2 : 0;
193    result = (*eqItf)->UsePreset(eqItf, preset);
194
195    preset = 1977;
196    result = (*eqItf)->GetCurrentPreset(eqItf, &preset);
197    ExitOnError(result);
198    if (SL_EQUALIZER_UNDEFINED == preset) {
199        fprintf(stderr, "Using SL_EQUALIZER_UNDEFINED preset, unexpected here!\n");
200    } else {
201        fprintf(stdout, "Using preset %d\n", preset);
202    }
203
204    /*    Tweak it so it's obvious it gets turned on/off later */
205    SLmillibel minLevel, maxLevel = 0;
206    result = (*eqItf)->GetBandLevelRange(eqItf, &minLevel, &maxLevel);
207    ExitOnError(result);
208    fprintf(stdout, "Band level range = %dmB to %dmB\n", minLevel, maxLevel);
209
210    SLuint16 b = 0;
211    for(b = 0 ; b < nbBands/2 ; b++) {
212        result = (*eqItf)->SetBandLevel(eqItf, b, minLevel);
213        ExitOnError(result);
214    }
215    for(b = nbBands/2 ; b < nbBands ; b++) {
216        result = (*eqItf)->SetBandLevel(eqItf, b, maxLevel);
217        ExitOnError(result);
218    }
219
220    SLmillibel level = 0;
221    for(b = 0 ; b < nbBands ; b++) {
222        result = (*eqItf)->GetBandLevel(eqItf, b, &level);
223        ExitOnError(result);
224        fprintf(stdout, "Band %d level = %dmB\n", b, level);
225    }
226
227    /* Switch EQ on/off every TIME_S_BETWEEN_EQ_ON_OFF seconds */
228    SLboolean enabled = SL_BOOLEAN_TRUE;
229    result = (*eqItf)->SetEnabled(eqItf, enabled);
230    ExitOnError(result);
231    for(unsigned int j=0 ; j<(durationInMsec/1000*TIME_S_BETWEEN_EQ_ON_OFF) ; j++) {
232        usleep(TIME_S_BETWEEN_EQ_ON_OFF * 1000 * 1000);
233        result = (*eqItf)->IsEnabled(eqItf, &enabled);
234        ExitOnError(result);
235        enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE;
236        result = (*eqItf)->SetEnabled(eqItf, enabled);
237        if (SL_BOOLEAN_TRUE == enabled) {
238            fprintf(stdout, "EQ on\n");
239        } else {
240            fprintf(stdout, "EQ off\n");
241        }
242        ExitOnError(result);
243    }
244
245    /* Make sure player is stopped */
246    fprintf(stdout, "Stopping playback\n");
247    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
248    ExitOnError(result);
249
250    /* Destroy the player */
251    (*player)->Destroy(player);
252
253    /* Destroy Output Mix object */
254    (*outputMix)->Destroy(outputMix);
255
256    close(fd);
257}
258
259//-----------------------------------------------------------------
260int main(int argc, char* const argv[])
261{
262    LOGV("Starting %s\n", argv[0]);
263
264    SLresult    result;
265    SLObjectItf sl;
266
267    fprintf(stdout, "OpenSL ES test %s: exercises SLEqualizerItf ", argv[0]);
268    fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
269    fprintf(stdout, "Plays the sound file designated by the given path, ");
270    fprintf(stdout, "starting at the specified offset, and using the specified length.\n");
271    fprintf(stdout, "Omit the length of the file for it to be computed by the system.\n");
272    fprintf(stdout, "Every %d seconds, the EQ will be turned on and off.\n", TIME_S_BETWEEN_EQ_ON_OFF);
273
274    if (argc < 3) {
275        fprintf(stdout, "Usage: \t%s path offsetInBytes [sizeInBytes]\n", argv[0]);
276        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 0 344460\" \n", argv[0]);
277        exit(1);
278    }
279
280    SLEngineOption EngineOption[] = {
281            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
282    };
283
284    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
285    ExitOnError(result);
286
287    /* Realizing the SL Engine in synchronous mode. */
288    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
289    ExitOnError(result);
290
291    if (argc == 3) {
292        fprintf(stdout, "\nno file size given, using SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE\n\n");
293        TestEQPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]),
294                SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE);
295    } else {
296        TestEQPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]), (SLAint64)atoi(argv[3]));
297    }
298
299    /* Shutdown OpenSL ES */
300    (*sl)->Destroy(sl);
301    exit(0);
302
303    return 0;
304}
305