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