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 /* 18 * Copyright (c) 2009 The Khronos Group Inc. 19 * 20 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 21 * software and /or associated documentation files (the "Materials "), to deal in the 22 * Materials without restriction, including without limitation the rights to use, copy, 23 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, 24 * and to permit persons to whom the Materials are furnished to do so, subject to 25 * the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be included 28 * in all copies or substantial portions of the Materials. 29 * 30 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 34 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 35 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 36 * CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE 37 * MATERIALS. 38 */ 39 40 #include <stdlib.h> 41 #include <stdio.h> 42 //#include <string.h> 43 #include <unistd.h> 44 //#include <sys/time.h> 45 46 #include "SLES/OpenSLES.h" 47 48 49 #define MAX_NUMBER_INTERFACES 2 50 51 #define PREFETCHEVENT_ERROR_CANDIDATE \ 52 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE) 53 54 //----------------------------------------------------------------- 55 //* Exits the application if an error is encountered */ 56 #define CheckErr(x) ExitOnErrorFunc(x,__LINE__) 57 58 void ExitOnErrorFunc( SLresult result , int line) 59 { 60 if (SL_RESULT_SUCCESS != result) { 61 fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line); 62 exit(EXIT_FAILURE); 63 } 64 } 65 66 bool prefetchError = false; 67 68 //----------------------------------------------------------------- 69 /* PrefetchStatusItf callback for an audio player */ 70 void PrefetchEventCallback( SLPrefetchStatusItf caller, void *pContext, SLuint32 event) 71 { 72 SLpermille level = 0; 73 (*caller)->GetFillLevel(caller, &level); 74 SLuint32 status; 75 //fprintf(stdout, "PrefetchEventCallback: received event %lu\n", event); 76 (*caller)->GetPrefetchStatus(caller, &status); 77 if ((PREFETCHEVENT_ERROR_CANDIDATE == (event & PREFETCHEVENT_ERROR_CANDIDATE)) 78 && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) { 79 fprintf(stdout, "PrefetchEventCallback: Error while prefetching data, exiting\n"); 80 prefetchError = true; 81 } 82 if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 83 fprintf(stdout, "PrefetchEventCallback: Buffer fill level is = %d\n", level); 84 } 85 if (event & SL_PREFETCHEVENT_STATUSCHANGE) { 86 fprintf(stdout, "PrefetchEventCallback: Prefetch Status is = %lu\n", status); 87 } 88 89 } 90 91 92 //----------------------------------------------------------------- 93 94 /* Play some music from a URI */ 95 void TestPlayUri( SLObjectItf sl, const char* path) 96 { 97 SLEngineItf EngineItf; 98 99 SLint32 numOutputs = 0; 100 SLuint32 deviceID = 0; 101 102 SLresult res; 103 104 SLDataSource audioSource; 105 SLDataLocator_URI uri; 106 SLDataFormat_MIME mime; 107 108 SLDataSink audioSink; 109 SLDataLocator_OutputMix locator_outputmix; 110 111 SLObjectItf player; 112 SLPlayItf playItf; 113 SLVolumeItf volItf; 114 SLPrefetchStatusItf prefetchItf; 115 116 SLObjectItf OutputMix; 117 118 SLboolean required[MAX_NUMBER_INTERFACES]; 119 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 120 121 /* Get the SL Engine Interface which is implicit */ 122 res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 123 CheckErr(res); 124 125 /* Initialize arrays required[] and iidArray[] */ 126 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 127 required[i] = SL_BOOLEAN_FALSE; 128 iidArray[i] = SL_IID_NULL; 129 } 130 131 // Set arrays required[] and iidArray[] for VOLUME and PREFETCHSTATUS interface 132 required[0] = SL_BOOLEAN_TRUE; 133 iidArray[0] = SL_IID_VOLUME; 134 required[1] = SL_BOOLEAN_TRUE; 135 iidArray[1] = SL_IID_PREFETCHSTATUS; 136 // Create Output Mix object to be used by player 137 res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, 138 iidArray, required); CheckErr(res); 139 140 // Realizing the Output Mix object in synchronous mode. 141 res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); 142 CheckErr(res); 143 144 /* Setup the data source structure for the URI */ 145 uri.locatorType = SL_DATALOCATOR_URI; 146 uri.URI = (SLchar*) path; 147 mime.formatType = SL_DATAFORMAT_MIME; 148 mime.mimeType = (SLchar*)NULL; 149 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 150 151 audioSource.pFormat = (void *)&mime; 152 audioSource.pLocator = (void *)&uri; 153 154 /* Setup the data sink structure */ 155 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 156 locator_outputmix.outputMix = OutputMix; 157 audioSink.pLocator = (void *)&locator_outputmix; 158 audioSink.pFormat = NULL; 159 160 /* Create the audio player */ 161 res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 162 MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res); 163 164 /* Realizing the player in synchronous mode. */ 165 res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res); 166 fprintf(stdout, "URI example: after Realize\n"); 167 168 /* Get interfaces */ 169 res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 170 CheckErr(res); 171 172 res = (*player)->GetInterface(player, SL_IID_VOLUME, (void*)&volItf); 173 CheckErr(res); 174 175 res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 176 CheckErr(res); 177 res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf); 178 CheckErr(res); 179 res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, 180 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 181 CheckErr(res); 182 183 /* Configure fill level updates every 5 percent */ 184 (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50); 185 186 /* Display duration */ 187 SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 188 res = (*playItf)->GetDuration(playItf, &durationInMsec); 189 CheckErr(res); 190 if (durationInMsec == SL_TIME_UNKNOWN) { 191 fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n"); 192 } else { 193 fprintf(stdout, "Content duration is %lu ms (before starting to prefetch)\n", 194 durationInMsec); 195 } 196 197 /* Set the player volume */ 198 res = (*volItf)->SetVolumeLevel( volItf, -300); 199 CheckErr(res); 200 201 /* Play the URI */ 202 /* first cause the player to prefetch the data */ 203 fprintf(stdout, "Before set to PAUSED\n"); 204 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 205 fprintf(stdout, "After set to PAUSED\n"); 206 CheckErr(res); 207 208 /* wait until there's data to play */ 209 //SLpermille fillLevel = 0; 210 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 211 SLuint32 timeOutIndex = 100; // 10s 212 while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0) && 213 !prefetchError) { 214 usleep(100 * 1000); 215 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 216 timeOutIndex--; 217 } 218 219 if (timeOutIndex == 0 || prefetchError) { 220 fprintf(stderr, "We\'re done waiting, failed to prefetch data in time, exiting\n"); 221 goto destroyRes; 222 } 223 224 /* Display duration again, */ 225 res = (*playItf)->GetDuration(playItf, &durationInMsec); 226 CheckErr(res); 227 if (durationInMsec == SL_TIME_UNKNOWN) { 228 fprintf(stdout, "Content duration is unknown (after prefetch completed)\n"); 229 } else { 230 fprintf(stdout, "Content duration is %lu ms (after prefetch completed)\n", durationInMsec); 231 } 232 233 fprintf(stdout, "URI example: starting to play\n"); 234 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 235 CheckErr(res); 236 237 /* Wait as long as the duration of the content before stopping */ 238 usleep(durationInMsec * 1000); 239 240 /* Make sure player is stopped */ 241 fprintf(stdout, "URI example: stopping playback\n"); 242 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 243 CheckErr(res); 244 245 destroyRes: 246 247 /* Destroy the player */ 248 (*player)->Destroy(player); 249 250 /* Destroy Output Mix object */ 251 (*OutputMix)->Destroy(OutputMix); 252 } 253 254 //----------------------------------------------------------------- 255 int main(int argc, char* const argv[]) 256 { 257 SLresult res; 258 SLObjectItf sl; 259 260 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf ", argv[0]); 261 fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n"); 262 fprintf(stdout, "Plays a sound and stops after its reported duration\n\n"); 263 264 if (argc == 1) { 265 fprintf(stdout, "Usage: %s path \n\t%s url\n", argv[0], argv[0]); 266 fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" or \"%s file:///sdcard/my.mp3\"\n", 267 argv[0], argv[0]); 268 exit(EXIT_FAILURE); 269 } 270 271 SLEngineOption EngineOption[] = { 272 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, 273 (SLuint32) SL_BOOLEAN_TRUE}}; 274 275 res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 276 CheckErr(res); 277 /* Realizing the SL Engine in synchronous mode. */ 278 res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 279 CheckErr(res); 280 281 TestPlayUri(sl, argv[1]); 282 283 /* Shutdown OpenSL ES */ 284 (*sl)->Destroy(sl); 285 286 return EXIT_SUCCESS; 287 } 288