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 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/time.h>
22 #include <fcntl.h>
23
24 #include "SLES/OpenSLES.h"
25 #ifdef ANDROID
26 #include "SLES/OpenSLES_Android.h"
27 #endif
28
29
30 #define MAX_NUMBER_INTERFACES 3
31
32 #define TIME_S_BETWEEN_EQ_ON_OFF 3
33
34 //-----------------------------------------------------------------
35 /* Exits the application if an error is encountered */
36 #define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
37
ExitOnErrorFunc(SLresult result,int line)38 void ExitOnErrorFunc( SLresult result , int line)
39 {
40 if (SL_RESULT_SUCCESS != result) {
41 fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line);
42 exit(EXIT_FAILURE);
43 }
44 }
45
46
47 //-----------------------------------------------------------------
48
49 /* Play an audio path by opening a file descriptor on that path */
TestEQPathFromFD(SLObjectItf sl,const char * path,SLAint64 offset,SLAint64 size)50 void TestEQPathFromFD( SLObjectItf sl, const char* path
51 #ifdef ANDROID
52 , SLAint64 offset, SLAint64 size
53 #endif
54 )
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 #ifdef ANDROID
65 SLDataLocator_AndroidFD locatorFd;
66 #else
67 SLDataLocator_URI locatorUri;
68 #endif
69 SLDataFormat_MIME mime;
70
71 /* Data sinks for the audio player */
72 SLDataSink audioSink;
73 SLDataLocator_OutputMix locator_outputmix;
74
75 /* Play and PrefetchStatus interfaces for the audio player */
76 SLPlayItf playItf;
77 SLPrefetchStatusItf prefetchItf;
78 SLEqualizerItf eqItf;
79
80 SLboolean required[MAX_NUMBER_INTERFACES];
81 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
82
83 /* Get the SL Engine Interface which is implicit */
84 result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
85 ExitOnError(result);
86
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;
91 }
92
93 /* ------------------------------------------------------ */
94 /* Configuration of the output mix */
95
96 /* Create Output Mix object to be used by the player */
97 result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
98 ExitOnError(result);
99
100 /* Realize the Output Mix object in synchronous mode */
101 result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
102 ExitOnError(result);
103
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;
109
110 /* ------------------------------------------------------ */
111 /* Configuration of the player */
112
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;
119
120 /* Setup the data source structure for the URI */
121 #ifdef ANDROID
122 locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
123 int fd = open(path, O_RDONLY);
124 if (fd == -1) {
125 ExitOnError(SL_RESULT_RESOURCE_ERROR);
126 }
127 locatorFd.fd = (SLint32) fd;
128 locatorFd.length = size;
129 locatorFd.offset = offset;
130 #else
131 locatorUri.locatorType = SL_DATALOCATOR_URI;
132 locatorUri.URI = (SLchar *) path;
133 #endif
134
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;
140
141 audioSource.pFormat = (void*)&mime;
142 #ifdef ANDROID
143 audioSource.pLocator = (void*)&locatorFd;
144 #else
145 audioSource.pLocator = (void*)&locatorUri;
146 #endif
147
148 /* Create the audio player */
149 result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
150 iidArray, required);
151 ExitOnError(result);
152
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");
156
157 /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/
158 result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
159 ExitOnError(result);
160
161 result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
162 ExitOnError(result);
163
164 result = (*player)->GetInterface(player, SL_IID_EQUALIZER, (void*)&eqItf);
165 ExitOnError(result);
166
167 fprintf(stdout, "Player configured\n");
168
169 /* ------------------------------------------------------ */
170 /* Playback and test */
171
172 /* Start the data prefetching by setting the player to the paused state */
173 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
174 ExitOnError(result);
175
176 /* Wait until there's data to play */
177 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
178 while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
179 usleep(100 * 1000);
180 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
181 ExitOnError(result);
182 }
183
184 /* Get duration */
185 SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
186 result = (*playItf)->GetDuration(playItf, &durationInMsec);
187 ExitOnError(result);
188 if (durationInMsec == SL_TIME_UNKNOWN) {
189 durationInMsec = 5000;
190 }
191
192 /* Start playback */
193 fprintf(stdout, "Starting to play\n");
194 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
195 ExitOnError(result);
196
197 /* Configure EQ */
198 SLuint16 nbPresets, preset, nbBands = 0;
199 result = (*eqItf)->GetNumberOfBands(eqItf, &nbBands);
200 ExitOnError(result);
201 result = (*eqItf)->GetNumberOfPresets(eqItf, &nbPresets);
202 ExitOnError(result);
203 /* Start from a preset */
204 preset = nbPresets > 2 ? 2 : 0;
205 result = (*eqItf)->UsePreset(eqItf, preset);
206
207 preset = 1977;
208 result = (*eqItf)->GetCurrentPreset(eqItf, &preset);
209 ExitOnError(result);
210 if (SL_EQUALIZER_UNDEFINED == preset) {
211 fprintf(stderr, "Using SL_EQUALIZER_UNDEFINED preset, unexpected here!\n");
212 } else {
213 fprintf(stdout, "Using preset %d\n", preset);
214 }
215
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);
219 ExitOnError(result);
220 fprintf(stdout, "Band level range = %dmB to %dmB\n", minLevel, maxLevel);
221
222 SLuint16 b = 0;
223 for(b = 0 ; b < nbBands/2 ; b++) {
224 result = (*eqItf)->SetBandLevel(eqItf, b, minLevel);
225 ExitOnError(result);
226 }
227 for(b = nbBands/2 ; b < nbBands ; b++) {
228 result = (*eqItf)->SetBandLevel(eqItf, b, maxLevel);
229 ExitOnError(result);
230 }
231
232 SLmillibel level = 0;
233 for(b = 0 ; b < nbBands ; b++) {
234 result = (*eqItf)->GetBandLevel(eqItf, b, &level);
235 ExitOnError(result);
236 fprintf(stdout, "Band %d level = %dmB\n", b, level);
237 }
238
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);
242 ExitOnError(result);
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);
246 ExitOnError(result);
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");
251 } else {
252 fprintf(stdout, "EQ off\n");
253 }
254 ExitOnError(result);
255 }
256
257 /* Make sure player is stopped */
258 fprintf(stdout, "Stopping playback\n");
259 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
260 ExitOnError(result);
261
262 /* Destroy the player */
263 (*player)->Destroy(player);
264
265 /* Destroy Output Mix object */
266 (*outputMix)->Destroy(outputMix);
267
268 #ifdef ANDROID
269 close(fd);
270 #endif
271 }
272
273 //-----------------------------------------------------------------
main(int argc,char * const argv[])274 int main(int argc, char* const argv[])
275 {
276 SLresult result;
277 SLObjectItf sl;
278
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);
286
287 #ifdef ANDROID
288 if (argc < 3)
289 #else
290 if (argc < 1)
291 #endif
292 {
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]);
295 exit(EXIT_FAILURE);
296 }
297
298 SLEngineOption EngineOption[] = {
299 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
300 };
301
302 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
303 ExitOnError(result);
304
305 /* Realizing the SL Engine in synchronous mode. */
306 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
307 ExitOnError(result);
308
309 #ifdef ANDROID
310 if (argc == 3) {
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);
314 } else {
315 TestEQPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]), (SLAint64)atoi(argv[3]));
316 }
317 #else
318 TestEQPathFromFD(sl, argv[1]);
319 #endif
320
321 /* Shutdown OpenSL ES */
322 (*sl)->Destroy(sl);
323
324 return EXIT_SUCCESS;
325 }
326