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 <assert.h> 18#include <pthread.h> 19#include <stdlib.h> 20#include <stdio.h> 21#include <string.h> 22#include <unistd.h> 23#include <sys/time.h> 24 25#include <SLES/OpenSLES.h> 26 27 28#define MAX_NUMBER_INTERFACES 2 29 30#define REPETITIONS 4 31 32// These are extensions to OpenSL ES 1.0.1 values 33 34#define SL_PREFETCHSTATUS_UNKNOWN 0 35#define SL_PREFETCHSTATUS_ERROR ((SLuint32) -1) 36 37// Mutex and condition shared with main program to protect prefetch_status 38 39static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 40static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 41SLuint32 prefetch_status = SL_PREFETCHSTATUS_UNKNOWN; 42 43/* used to detect errors likely to have occured when the OpenSL ES framework fails to open 44 * a resource, for instance because a file URI is invalid, or an HTTP server doesn't respond. 45 */ 46#define PREFETCHEVENT_ERROR_CANDIDATE \ 47 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE) 48 49//----------------------------------------------------------------- 50//* Exits the application if an error is encountered */ 51#define CheckErr(x) ExitOnErrorFunc(x,__LINE__) 52 53void ExitOnErrorFunc( SLresult result , int line) 54{ 55 if (SL_RESULT_SUCCESS != result) { 56 fprintf(stderr, "%u error code encountered at line %d, exiting\n", result, line); 57 exit(EXIT_FAILURE); 58 } 59} 60 61//----------------------------------------------------------------- 62/* PrefetchStatusItf callback for an audio player */ 63void PrefetchEventCallback( SLPrefetchStatusItf caller, void *pContext, SLuint32 event) 64{ 65 SLresult result; 66 // pContext is unused here, so we pass NULL 67 assert(pContext == NULL); 68 SLpermille level = 0; 69 result = (*caller)->GetFillLevel(caller, &level); 70 CheckErr(result); 71 SLuint32 status; 72 result = (*caller)->GetPrefetchStatus(caller, &status); 73 CheckErr(result); 74 if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 75 fprintf(stdout, "\t\tPrefetchEventCallback: Buffer fill level is = %d\n", level); 76 } 77 if (event & SL_PREFETCHEVENT_STATUSCHANGE) { 78 fprintf(stdout, "\t\tPrefetchEventCallback: Prefetch Status is = %u\n", status); 79 } 80 SLuint32 new_prefetch_status; 81 if ((event & PREFETCHEVENT_ERROR_CANDIDATE) == PREFETCHEVENT_ERROR_CANDIDATE 82 && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) { 83 fprintf(stdout, "\t\tPrefetchEventCallback: Error while prefetching data, exiting\n"); 84 new_prefetch_status = SL_PREFETCHSTATUS_ERROR; 85 } else if (event == SL_PREFETCHEVENT_STATUSCHANGE && 86 status == SL_PREFETCHSTATUS_SUFFICIENTDATA) { 87 new_prefetch_status = status; 88 } else { 89 return; 90 } 91 int ok; 92 ok = pthread_mutex_lock(&mutex); 93 assert(ok == 0); 94 prefetch_status = new_prefetch_status; 95 ok = pthread_cond_signal(&cond); 96 assert(ok == 0); 97 ok = pthread_mutex_unlock(&mutex); 98 assert(ok == 0); 99} 100 101 102//----------------------------------------------------------------- 103/* PlayItf callback for playback events */ 104void PlayEventCallback( 105 SLPlayItf caller __unused, 106 void *pContext, 107 SLuint32 event) 108{ 109 // pContext is unused here, so we pass NULL 110 assert(NULL == pContext); 111 if (SL_PLAYEVENT_HEADATEND == event) { 112 printf("SL_PLAYEVENT_HEADATEND reached\n"); 113 } else { 114 fprintf(stderr, "Unexpected play event 0x%x", event); 115 } 116} 117 118 119//----------------------------------------------------------------- 120 121/* Play some music from a URI */ 122void TestLoopUri( SLObjectItf sl, const char* path) 123{ 124 SLEngineItf EngineItf; 125 126 SLresult res; 127 128 SLDataSource audioSource; 129 SLDataLocator_URI uri; 130 SLDataFormat_MIME mime; 131 132 SLDataSink audioSink; 133 SLDataLocator_OutputMix locator_outputmix; 134 135 SLObjectItf player; 136 SLPlayItf playItf; 137 SLSeekItf seekItf; 138 SLPrefetchStatusItf prefetchItf; 139 140 SLObjectItf OutputMix; 141 142 SLboolean required[MAX_NUMBER_INTERFACES]; 143 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 144 145 /* Get the SL Engine Interface which is implicit */ 146 res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 147 CheckErr(res); 148 149 /* Initialize arrays required[] and iidArray[] */ 150 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 151 required[i] = SL_BOOLEAN_FALSE; 152 iidArray[i] = SL_IID_NULL; 153 } 154 155 required[0] = SL_BOOLEAN_TRUE; 156 iidArray[0] = SL_IID_VOLUME; 157 // Create Output Mix object to be used by player 158 res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, 159 iidArray, required); CheckErr(res); 160 161 // Realizing the Output Mix object in synchronous mode. 162 res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); 163 CheckErr(res); 164 165 /* Setup the data source structure for the URI */ 166 uri.locatorType = SL_DATALOCATOR_URI; 167 uri.URI = (SLchar*) path; 168 mime.formatType = SL_DATAFORMAT_MIME; 169 mime.mimeType = (SLchar*)NULL; 170 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 171 172 audioSource.pFormat = (void *)&mime; 173 audioSource.pLocator = (void *)&uri; 174 175 /* Setup the data sink structure */ 176 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 177 locator_outputmix.outputMix = OutputMix; 178 audioSink.pLocator = (void *)&locator_outputmix; 179 audioSink.pFormat = NULL; 180 181 /* Create the audio player */ 182 required[0] = SL_BOOLEAN_TRUE; 183 iidArray[0] = SL_IID_SEEK; 184 required[1] = SL_BOOLEAN_TRUE; 185 iidArray[1] = SL_IID_PREFETCHSTATUS; 186 res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 187 MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res); 188 189 /* Realizing the player in synchronous mode. */ 190 res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res); 191 fprintf(stdout, "URI example: after Realize\n"); 192 193 /* Get interfaces */ 194 res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 195 CheckErr(res); 196 197 res = (*player)->GetInterface(player, SL_IID_SEEK, (void*)&seekItf); 198 CheckErr(res); 199 200 res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 201 CheckErr(res); 202 res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, NULL); 203 CheckErr(res); 204 res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, 205 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 206 CheckErr(res); 207 208 /* Configure fill level updates every 5 percent */ 209 (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50); 210 211 /* Set up the player callback to get head-at-end events */ 212 res = (*playItf)->SetCallbackEventsMask(playItf, SL_PLAYEVENT_HEADATEND); 213 CheckErr(res); 214 res = (*playItf)->RegisterCallback(playItf, PlayEventCallback, NULL); 215 CheckErr(res); 216 217 /* Display duration */ 218 SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 219 res = (*playItf)->GetDuration(playItf, &durationInMsec); 220 CheckErr(res); 221 if (durationInMsec == SL_TIME_UNKNOWN) { 222 fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n"); 223 } else { 224 fprintf(stdout, "Content duration is %u ms (before starting to prefetch)\n", 225 durationInMsec); 226 } 227 228 /* Loop on the whole of the content */ 229 res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN); 230 CheckErr(res); 231 232 /* Play the URI */ 233 /* first cause the player to prefetch the data */ 234 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 235 CheckErr(res); 236 237 // wait for prefetch status callback to indicate either sufficient data or error 238 pthread_mutex_lock(&mutex); 239 while (prefetch_status == SL_PREFETCHSTATUS_UNKNOWN) { 240 pthread_cond_wait(&cond, &mutex); 241 } 242 pthread_mutex_unlock(&mutex); 243 if (prefetch_status == SL_PREFETCHSTATUS_ERROR) { 244 fprintf(stderr, "Error during prefetch, exiting\n"); 245 goto destroyRes; 246 } 247 248 /* Display duration again, */ 249 res = (*playItf)->GetDuration(playItf, &durationInMsec); 250 CheckErr(res); 251 if (durationInMsec == SL_TIME_UNKNOWN) { 252 fprintf(stdout, "Content duration is unknown (after prefetch completed)\n"); 253 } else { 254 fprintf(stdout, "Content duration is %u ms (after prefetch completed)\n", durationInMsec); 255 } 256 257 /* Start playing */ 258 fprintf(stdout, "starting to play\n"); 259 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 260 CheckErr(res); 261 262 /* Wait as long as the duration of the content, times the repetitions, 263 * before stopping the loop */ 264 usleep( (REPETITIONS-1) * durationInMsec * 1100); 265 res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_FALSE, 0, SL_TIME_UNKNOWN); 266 CheckErr(res); 267 fprintf(stdout, "As of now, stopped looping (sound shouldn't repeat from now on)\n"); 268 /* wait some more to make sure it doesn't repeat */ 269 usleep(durationInMsec * 1000); 270 271 /* Stop playback */ 272 fprintf(stdout, "stopping playback\n"); 273 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 274 CheckErr(res); 275 276destroyRes: 277 278 /* Destroy the player */ 279 (*player)->Destroy(player); 280 281 /* Destroy Output Mix object */ 282 (*OutputMix)->Destroy(OutputMix); 283} 284 285//----------------------------------------------------------------- 286int main(int argc, char* const argv[]) 287{ 288 SLresult res; 289 SLObjectItf sl; 290 291 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLSeekItf ", argv[0]); 292 fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n"); 293 fprintf(stdout, "Plays a sound and loops it %d times.\n\n", REPETITIONS); 294 295 if (argc == 1) { 296 fprintf(stdout, "Usage: \n\t%s path \n\t%s url\n", argv[0], argv[0]); 297 fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" or \"%s file:///sdcard/my.mp3\"\n", 298 argv[0], argv[0]); 299 exit(EXIT_FAILURE); 300 } 301 302 SLEngineOption EngineOption[] = { 303 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, 304 (SLuint32) SL_BOOLEAN_TRUE}}; 305 306 res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 307 CheckErr(res); 308 /* Realizing the SL Engine in synchronous mode. */ 309 res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 310 CheckErr(res); 311 312 TestLoopUri(sl, argv[1]); 313 314 /* Shutdown OpenSL ES */ 315 (*sl)->Destroy(sl); 316 317 return EXIT_SUCCESS; 318} 319