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