2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "SLES/OpenSLES.h"
26 #include "SLES/OpenSLES_Android.h"
30 #define MAX_NUMBER_INTERFACES 3
32 #define TIME_S_BETWEEN_EQ_ON_OFF 3
34 //-----------------------------------------------------------------
35 /* Exits the application if an error is encountered */
36 #define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
38 void ExitOnErrorFunc( SLresult result , int line)
40 if (SL_RESULT_SUCCESS != result) {
41 fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line);
47 //-----------------------------------------------------------------
49 /* Play an audio path by opening a file descriptor on that path */
50 void TestEQPathFromFD( SLObjectItf sl, const char* path
52 , SLAint64 offset, SLAint64 size
57 SLEngineItf EngineItf;
59 /* Objects this application uses: one player and an ouput mix */
60 SLObjectItf player, outputMix;
62 /* Source of audio data to play */
63 SLDataSource audioSource;
65 SLDataLocator_AndroidFD locatorFd;
67 SLDataLocator_URI locatorUri;
69 SLDataFormat_MIME mime;
71 /* Data sinks for the audio player */
73 SLDataLocator_OutputMix locator_outputmix;
75 /* Play and PrefetchStatus interfaces for the audio player */
77 SLPrefetchStatusItf prefetchItf;
80 SLboolean required[MAX_NUMBER_INTERFACES];
81 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
83 /* Get the SL Engine Interface which is implicit */
84 result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
87 /* Initialize arrays required[] and iidArray[] */
88 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
89 required[i] = SL_BOOLEAN_FALSE;
90 iidArray[i] = SL_IID_NULL;
93 /* ------------------------------------------------------ */
94 /* Configuration of the output mix */
96 /* Create Output Mix object to be used by the player */
97 result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
100 /* Realize the Output Mix object in synchronous mode */
101 result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
104 /* Setup the data sink structure */
105 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
106 locator_outputmix.outputMix = outputMix;
107 audioSink.pLocator = (void*)&locator_outputmix;
108 audioSink.pFormat = NULL;
110 /* ------------------------------------------------------ */
111 /* Configuration of the player */
113 /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */
114 /* (SLPlayItf is implicit) */
115 required[0] = SL_BOOLEAN_TRUE;
116 iidArray[0] = SL_IID_PREFETCHSTATUS;
117 required[1] = SL_BOOLEAN_TRUE;
118 iidArray[1] = SL_IID_EQUALIZER;
120 /* Setup the data source structure for the URI */
122 locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
123 int fd = open(path, O_RDONLY);
125 ExitOnError(SL_RESULT_RESOURCE_ERROR);
127 locatorFd.fd = (SLint32) fd;
128 locatorFd.length = size;
129 locatorFd.offset = offset;
131 locatorUri.locatorType = SL_DATALOCATOR_URI;
132 locatorUri.URI = (SLchar *) path;
135 mime.formatType = SL_DATAFORMAT_MIME;
136 /* this is how ignored mime information is specified, according to OpenSL ES spec
137 * in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
138 mime.mimeType = (SLchar*)NULL;
139 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
141 audioSource.pFormat = (void*)&mime;
143 audioSource.pLocator = (void*)&locatorFd;
145 audioSource.pLocator = (void*)&locatorUri;
148 /* Create the audio player */
149 result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
153 /* Realize the player in synchronous mode. */
154 result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
155 fprintf(stdout, "URI example: after Realize\n");
157 /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/
158 result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
161 result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
164 result = (*player)->GetInterface(player, SL_IID_EQUALIZER, (void*)&eqItf);
167 fprintf(stdout, "Player configured\n");
169 /* ------------------------------------------------------ */
170 /* Playback and test */
172 /* Start the data prefetching by setting the player to the paused state */
173 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
176 /* Wait until there's data to play */
177 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
178 while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
180 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
185 SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
186 result = (*playItf)->GetDuration(playItf, &durationInMsec);
188 if (durationInMsec == SL_TIME_UNKNOWN) {
189 durationInMsec = 5000;
193 fprintf(stdout, "Starting to play\n");
194 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
198 SLuint16 nbPresets, preset, nbBands = 0;
199 result = (*eqItf)->GetNumberOfBands(eqItf, &nbBands);
201 result = (*eqItf)->GetNumberOfPresets(eqItf, &nbPresets);
203 /* Start from a preset */
204 preset = nbPresets > 2 ? 2 : 0;
205 result = (*eqItf)->UsePreset(eqItf, preset);
208 result = (*eqItf)->GetCurrentPreset(eqItf, &preset);
210 if (SL_EQUALIZER_UNDEFINED == preset) {
211 fprintf(stderr, "Using SL_EQUALIZER_UNDEFINED preset, unexpected here!\n");
213 fprintf(stdout, "Using preset %d\n", preset);
216 /* Tweak it so it's obvious it gets turned on/off later */
217 SLmillibel minLevel, maxLevel = 0;
218 result = (*eqItf)->GetBandLevelRange(eqItf, &minLevel, &maxLevel);
220 fprintf(stdout, "Band level range = %dmB to %dmB\n", minLevel, maxLevel);
223 for(b = 0 ; b < nbBands/2 ; b++) {
224 result = (*eqItf)->SetBandLevel(eqItf, b, minLevel);
227 for(b = nbBands/2 ; b < nbBands ; b++) {
228 result = (*eqItf)->SetBandLevel(eqItf, b, maxLevel);
232 SLmillibel level = 0;
233 for(b = 0 ; b < nbBands ; b++) {
234 result = (*eqItf)->GetBandLevel(eqItf, b, &level);
236 fprintf(stdout, "Band %d level = %dmB\n", b, level);
239 /* Switch EQ on/off every TIME_S_BETWEEN_EQ_ON_OFF seconds */
240 SLboolean enabled = SL_BOOLEAN_TRUE;
241 result = (*eqItf)->SetEnabled(eqItf, enabled);
243 for(unsigned int j=0 ; j<(durationInMsec/(1000*TIME_S_BETWEEN_EQ_ON_OFF)) ; j++) {
244 usleep(TIME_S_BETWEEN_EQ_ON_OFF * 1000 * 1000);
245 result = (*eqItf)->IsEnabled(eqItf, &enabled);
247 enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE;
248 result = (*eqItf)->SetEnabled(eqItf, enabled);
249 if (SL_BOOLEAN_TRUE == enabled) {
250 fprintf(stdout, "EQ on\n");
252 fprintf(stdout, "EQ off\n");
257 /* Make sure player is stopped */
258 fprintf(stdout, "Stopping playback\n");
259 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
262 /* Destroy the player */
263 (*player)->Destroy(player);
265 /* Destroy Output Mix object */
266 (*outputMix)->Destroy(outputMix);
273 //-----------------------------------------------------------------
274 int main(int argc, char* const argv[])
279 fprintf(stdout, "OpenSL ES test %s: exercises SLEqualizerItf ", argv[0]);
280 fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
281 fprintf(stdout, "Plays the sound file designated by the given path, ");
282 fprintf(stdout, "starting at the specified offset, and using the specified length.\n");
283 fprintf(stdout, "Omit the length of the file for it to be computed by the system.\n");
284 fprintf(stdout, "Every %d seconds, the EQ will be turned on and off.\n",
285 TIME_S_BETWEEN_EQ_ON_OFF);
293 fprintf(stdout, "Usage: \t%s path offsetInBytes [sizeInBytes]\n", argv[0]);
294 fprintf(stdout, "Example: \"%s /sdcard/my.mp3 0 344460\" \n", argv[0]);
298 SLEngineOption EngineOption[] = {
299 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
302 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
305 /* Realizing the SL Engine in synchronous mode. */
306 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
311 fprintf(stdout, "\nno file size given, using SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE\n\n");
312 TestEQPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]),
313 SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE);
315 TestEQPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]), (SLAint64)atoi(argv[3]));
318 TestEQPathFromFD(sl, argv[1]);
321 /* Shutdown OpenSL ES */