slesTestPlayUri.cpp revision c2303eb5497c488db786dcb2b8514db229452536
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//----------------------------------------------------------------- 52//* Exits the application if an error is encountered */ 53#define CheckErr(x) ExitOnErrorFunc(x,__LINE__) 54 55void ExitOnErrorFunc( SLresult result , int line) 56{ 57 if (SL_RESULT_SUCCESS != result) { 58 fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line); 59 exit(EXIT_FAILURE); 60 } 61} 62 63//----------------------------------------------------------------- 64/* PrefetchStatusItf callback for an audio player */ 65void PrefetchEventCallback( SLPrefetchStatusItf caller, void *pContext, SLuint32 event) 66{ 67 SLpermille level = 0; 68 (*caller)->GetFillLevel(caller, &level); 69 SLuint32 status; 70 //fprintf(stdout, "PrefetchEventCallback: received event %lu\n", event); 71 (*caller)->GetPrefetchStatus(caller, &status); 72 if ((event & (SL_PREFETCHEVENT_STATUSCHANGE|SL_PREFETCHEVENT_FILLLEVELCHANGE)) 73 && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) { 74 fprintf(stdout, "PrefetchEventCallback: Error while prefetching data, exiting\n"); 75 //exit(EXIT_FAILURE); 76 } 77 if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 78 fprintf(stdout, "PrefetchEventCallback: Buffer fill level is = %d\n", level); 79 } 80 if (event & SL_PREFETCHEVENT_STATUSCHANGE) { 81 fprintf(stdout, "PrefetchEventCallback: Prefetch Status is = %lu\n", status); 82 } 83 84} 85 86 87//----------------------------------------------------------------- 88 89/* Play some music from a URI */ 90void TestPlayUri( SLObjectItf sl, const char* path) 91{ 92 SLEngineItf EngineItf; 93 94 SLint32 numOutputs = 0; 95 SLuint32 deviceID = 0; 96 97 SLresult res; 98 99 SLDataSource audioSource; 100 SLDataLocator_URI uri; 101 SLDataFormat_MIME mime; 102 103 SLDataSink audioSink; 104 SLDataLocator_OutputMix locator_outputmix; 105 106 SLObjectItf player; 107 SLPlayItf playItf; 108 SLVolumeItf volItf; 109 SLPrefetchStatusItf prefetchItf; 110 111 SLObjectItf OutputMix; 112 113 SLboolean required[MAX_NUMBER_INTERFACES]; 114 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 115 116 /* Get the SL Engine Interface which is implicit */ 117 res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 118 CheckErr(res); 119 120 /* Initialize arrays required[] and iidArray[] */ 121 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 122 required[i] = SL_BOOLEAN_FALSE; 123 iidArray[i] = SL_IID_NULL; 124 } 125 126 // Set arrays required[] and iidArray[] for VOLUME and PREFETCHSTATUS interface 127 required[0] = SL_BOOLEAN_TRUE; 128 iidArray[0] = SL_IID_VOLUME; 129 required[1] = SL_BOOLEAN_TRUE; 130 iidArray[1] = SL_IID_PREFETCHSTATUS; 131 // Create Output Mix object to be used by player 132 res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, 133 iidArray, required); CheckErr(res); 134 135 // Realizing the Output Mix object in synchronous mode. 136 res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); 137 CheckErr(res); 138 139 /* Setup the data source structure for the URI */ 140 uri.locatorType = SL_DATALOCATOR_URI; 141 uri.URI = (SLchar*) path; 142 mime.formatType = SL_DATAFORMAT_MIME; 143 mime.mimeType = (SLchar*)NULL; 144 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 145 146 audioSource.pFormat = (void *)&mime; 147 audioSource.pLocator = (void *)&uri; 148 149 /* Setup the data sink structure */ 150 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 151 locator_outputmix.outputMix = OutputMix; 152 audioSink.pLocator = (void *)&locator_outputmix; 153 audioSink.pFormat = NULL; 154 155 /* Create the audio player */ 156 res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 157 MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res); 158 159 /* Realizing the player in synchronous mode. */ 160 res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res); 161 fprintf(stdout, "URI example: after Realize\n"); 162 163 /* Get interfaces */ 164 res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 165 CheckErr(res); 166 167 res = (*player)->GetInterface(player, SL_IID_VOLUME, (void*)&volItf); 168 CheckErr(res); 169 170 res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 171 CheckErr(res); 172 res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf); 173 CheckErr(res); 174 res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, 175 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 176 CheckErr(res); 177 178 /* Configure fill level updates every 5 percent */ 179 (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50); 180 181 /* Display duration */ 182 SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 183 res = (*playItf)->GetDuration(playItf, &durationInMsec); 184 CheckErr(res); 185 if (durationInMsec == SL_TIME_UNKNOWN) { 186 fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n"); 187 } else { 188 fprintf(stdout, "Content duration is %lu ms (before starting to prefetch)\n", 189 durationInMsec); 190 } 191 192 /* Set the player volume */ 193 res = (*volItf)->SetVolumeLevel( volItf, -300); 194 CheckErr(res); 195 196 /* Play the URI */ 197 /* first cause the player to prefetch the data */ 198 fprintf(stdout, "Before set to PAUSED\n"); 199 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 200 fprintf(stdout, "After set to PAUSED\n"); 201 CheckErr(res); 202 203 /* wait until there's data to play */ 204 //SLpermille fillLevel = 0; 205 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 206 SLuint32 timeOutIndex = 100; // 10s 207 while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0)) { 208 usleep(100 * 1000); 209 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 210 timeOutIndex--; 211 } 212 213 if (timeOutIndex == 0) { 214 fprintf(stderr, "We\'re done waiting, failed to prefetch data in time, exiting\n"); 215 goto destroyRes; 216 } 217 218 /* Display duration again, */ 219 res = (*playItf)->GetDuration(playItf, &durationInMsec); 220 CheckErr(res); 221 if (durationInMsec == SL_TIME_UNKNOWN) { 222 fprintf(stdout, "Content duration is unknown (after prefetch completed)\n"); 223 } else { 224 fprintf(stdout, "Content duration is %lu ms (after prefetch completed)\n", durationInMsec); 225 } 226 227 fprintf(stdout, "URI example: starting to play\n"); 228 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 229 CheckErr(res); 230 231 /* Wait as long as the duration of the content before stopping */ 232 usleep(durationInMsec * 1000); 233 234 /* Make sure player is stopped */ 235 fprintf(stdout, "URI example: stopping playback\n"); 236 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 237 CheckErr(res); 238 239destroyRes: 240 241 /* Destroy the player */ 242 (*player)->Destroy(player); 243 244 /* Destroy Output Mix object */ 245 (*OutputMix)->Destroy(OutputMix); 246} 247 248//----------------------------------------------------------------- 249int main(int argc, char* const argv[]) 250{ 251 SLresult res; 252 SLObjectItf sl; 253 254 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf ", argv[0]); 255 fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n"); 256 fprintf(stdout, "Plays a sound and stops after its reported duration\n\n"); 257 258 if (argc == 1) { 259 fprintf(stdout, "Usage: %s path \n\t%s url\n", argv[0], argv[0]); 260 fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" or \"%s file:///sdcard/my.mp3\"\n", 261 argv[0], argv[0]); 262 exit(EXIT_FAILURE); 263 } 264 265 SLEngineOption EngineOption[] = { 266 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, 267 (SLuint32) SL_BOOLEAN_TRUE}}; 268 269 res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 270 CheckErr(res); 271 /* Realizing the SL Engine in synchronous mode. */ 272 res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 273 CheckErr(res); 274 275 TestPlayUri(sl, argv[1]); 276 277 /* Shutdown OpenSL ES */ 278 (*sl)->Destroy(sl); 279 280 return EXIT_SUCCESS; 281} 282