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