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