slesTestPlayStream.cpp revision d158d31a6bbb06426b71c3d097b7768bc3fb79a3
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#include <stdlib.h> 19#include <stdio.h> 20//#include <string.h> 21#include <unistd.h> 22//#include <sys/time.h> 23 24#include "SLES/OpenSLES.h" 25#include "SLES/OpenSLES_Android.h" 26 27 28#define MAX_NUMBER_INTERFACES 2 29 30#define PREFETCHEVENT_ERROR_CANDIDATE \ 31 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE) 32 33#define NB_BUFFERS 16 34#define MPEG2_TS_BLOCK_SIZE 188 35#define BUFFER_SIZE 20*MPEG2_TS_BLOCK_SIZE 36 37/* Where we store the data to play */ 38char dataCache[BUFFER_SIZE * NB_BUFFERS]; 39/* From where we read the data to play */ 40FILE *file; 41 42//----------------------------------------------------------------- 43//* Exits the application if an error is encountered */ 44#define CheckErr(x) ExitOnErrorFunc(x,__LINE__) 45 46void ExitOnErrorFunc( SLresult result , int line) 47{ 48 if (SL_RESULT_SUCCESS != result) { 49 fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line); 50 exit(EXIT_FAILURE); 51 } 52} 53 54bool prefetchError = false; 55 56//----------------------------------------------------------------- 57/* AndroidBufferQueueItf callback for an audio player */ 58SLresult AndroidBufferQueueCallback( 59 SLAndroidBufferQueueItf caller, 60 void *pContext, /* input */ 61 const void *pBufferData, /* input */ 62 SLuint32 dataSize, /* input */ 63 SLuint32 dataUsed, /* input */ 64 const SLAndroidBufferItem *pItems,/* input */ 65 SLuint32 itemsLength /* input */) 66{ 67 // assert(BUFFER_SIZE <= dataSize); 68 size_t nbRead = fread((void*)pBufferData, 1, BUFFER_SIZE, file); 69 if (nbRead > 0) { 70 (*caller)->Enqueue(caller, 71 pBufferData /*pData*/, 72 nbRead /*dataLength*/, 73 NULL /*pMsg*/, 74 0 /*msgLength*/); 75 } else { 76 // signal EOS 77 SLAndroidBufferItem msgEos; 78 msgEos.itemKey = SL_ANDROID_ITEMKEY_EOS; 79 msgEos.itemSize = 0; 80 // EOS message has no parameters, so the total size of the message is the size of the key 81 // plus the size if itemSize, both SLuint32 82 (*caller)->Enqueue(caller, NULL /*pData*/, 0 /*dataLength*/, 83 &msgEos /*pMsg*/, 84 sizeof(SLuint32)*2 /*msgLength*/); 85 } 86 87 return SL_RESULT_SUCCESS; 88} 89 90 91//----------------------------------------------------------------- 92 93/* Play some music from a URI */ 94void TestPlayStream( SLObjectItf sl, const char* path) 95{ 96 SLEngineItf EngineItf; 97 98 SLint32 numOutputs = 0; 99 SLuint32 deviceID = 0; 100 101 SLresult res; 102 103 SLDataSource audioSource; 104 SLDataLocator_AndroidBufferQueue streamLocator; 105 SLDataFormat_MIME mime; 106 107 SLDataSink audioSink; 108 SLDataLocator_OutputMix locator_outputmix; 109 110 SLObjectItf player; 111 SLPlayItf playItf; 112 SLVolumeItf volItf; 113 SLAndroidBufferQueueItf abqItf; 114 115 SLObjectItf OutputMix; 116 117 SLboolean required[MAX_NUMBER_INTERFACES]; 118 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 119 120 int playTimeInSec = 90; 121 122 file = fopen(path, "rb"); 123 124 /* Get the SL Engine Interface which is implicit */ 125 res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 126 CheckErr(res); 127 128 /* Initialize arrays required[] and iidArray[] */ 129 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 130 required[i] = SL_BOOLEAN_FALSE; 131 iidArray[i] = SL_IID_NULL; 132 } 133 134 // Set arrays required[] and iidArray[] for VOLUME and PREFETCHSTATUS interface 135 required[0] = SL_BOOLEAN_TRUE; 136 iidArray[0] = SL_IID_VOLUME; 137 required[1] = SL_BOOLEAN_TRUE; 138 iidArray[1] = SL_IID_ANDROIDBUFFERQUEUE; 139 // Create Output Mix object to be used by player 140 res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, 141 iidArray, required); CheckErr(res); 142 143 // Realizing the Output Mix object in synchronous mode. 144 res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); 145 CheckErr(res); 146 147 /* Setup the data source structure for the URI */ 148 streamLocator.locatorType = SL_DATALOCATOR_ANDROIDBUFFERQUEUE; 149 streamLocator.numBuffers = NB_BUFFERS; 150 mime.formatType = SL_DATAFORMAT_MIME; 151 mime.mimeType = (SLchar *) "video/mp2ts";//(SLchar*)NULL; 152 mime.containerType = SL_CONTAINERTYPE_MPEG_TS; 153 154 audioSource.pFormat = (void *)&mime; 155 audioSource.pLocator = (void *)&streamLocator; 156 157 /* Setup the data sink structure */ 158 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 159 locator_outputmix.outputMix = OutputMix; 160 audioSink.pLocator = (void *)&locator_outputmix; 161 audioSink.pFormat = NULL; 162 163 /* Create the audio player */ 164 res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 165 MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res); 166 167 /* Realizing the player in synchronous mode. */ 168 res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res); 169 fprintf(stdout, "URI example: after Realize\n"); 170 171 /* Get interfaces */ 172 res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); CheckErr(res); 173 174 res = (*player)->GetInterface(player, SL_IID_VOLUME, (void*)&volItf); CheckErr(res); 175 176 res = (*player)->GetInterface(player, SL_IID_ANDROIDBUFFERQUEUE, (void*)&abqItf); 177 CheckErr(res); 178 179 res = (*abqItf)->RegisterCallback(abqItf, AndroidBufferQueueCallback, 180 // context is not used in the example, but can be used to track who registered 181 // the buffer queue callback 182 NULL /*pContext*/); CheckErr(res); 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, 0);//-300); 197 CheckErr(res); 198 199 200 /* Play the URI */ 201 /* first cause the player to prefetch the data */ 202 fprintf(stdout, "Before set to PAUSED\n"); 203 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 204 fprintf(stdout, "After set to PAUSED\n"); 205 CheckErr(res); 206 207 208 /* Fill our cache */ 209 if (fread(dataCache, 1, BUFFER_SIZE * NB_BUFFERS, file) <= 0) { 210 fprintf(stderr, "Error filling cache, exiting\n"); 211 goto destroyRes; 212 } 213 /* Enqueue the content of our cache before starting to play, 214 * we don't want to starve the player */ 215 for (int i=0 ; i < NB_BUFFERS ; i++) { 216 res = (*abqItf)->Enqueue(abqItf, dataCache + i*BUFFER_SIZE, BUFFER_SIZE, NULL, 0); 217 CheckErr(res); 218 } 219 220 221 222 223 /* wait until there's data to play */ 224 //SLpermille fillLevel = 0; 225 /* 226 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 227 SLuint32 timeOutIndex = 2; 228 while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0) && 229 !prefetchError) { 230 usleep(1 * 1000 * 1000); // 1s 231 //(*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 232 timeOutIndex--; 233 } 234 235 if (timeOutIndex == 0 || prefetchError) { 236 fprintf(stderr, "We\'re done waiting, failed to prefetch data in time, exiting\n"); 237 goto destroyRes; 238 } 239*/ 240 241 /* Display duration again, */ 242/* res = (*playItf)->GetDuration(playItf, &durationInMsec); 243 CheckErr(res); 244 if (durationInMsec == SL_TIME_UNKNOWN) { 245 fprintf(stdout, "Content duration is unknown (after prefetch completed)\n"); 246 } else { 247 fprintf(stdout, "Content duration is %lu ms (after prefetch completed)\n", durationInMsec); 248 } 249*/ 250 251 fprintf(stdout, "URI example: starting to play\n"); 252 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 253 CheckErr(res); 254 255 /* Wait as long as the duration of the content before stopping */ 256 fprintf(stdout, "Letting playback go on for %d sec\n", playTimeInSec); 257 usleep(playTimeInSec /*s*/ * 1000 * 1000); 258 259 260 /* Make sure player is stopped */ 261 fprintf(stdout, "URI example: stopping playback\n"); 262 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 263 CheckErr(res); 264 265 fprintf(stdout, "sleeping to verify playback stopped\n"); 266 usleep(2 /*s*/ * 1000 * 1000); 267 268destroyRes: 269 270 /* Destroy the player */ 271 (*player)->Destroy(player); 272 273 /* Destroy Output Mix object */ 274 (*OutputMix)->Destroy(OutputMix); 275 276 fclose(file); 277} 278 279//----------------------------------------------------------------- 280int main(int argc, char* const argv[]) 281{ 282 SLresult res; 283 SLObjectItf sl; 284 285 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf, SLAndroidBufferQueue \n", 286 argv[0]); 287 fprintf(stdout, "and AudioPlayer with SL_DATALOCATOR_ANDROIDBUFFERQUEUE source / OutputMix sink\n"); 288 fprintf(stdout, "Plays a sound and stops after its reported duration\n\n"); 289 290 if (argc == 1) { 291 fprintf(stdout, "Usage: %s path \n\t%s url\n", argv[0], argv[0]); 292 fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" or \"%s file:///sdcard/my.mp3\"\n", 293 argv[0], argv[0]); 294 exit(EXIT_FAILURE); 295 } 296 297 SLEngineOption EngineOption[] = { 298 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, 299 (SLuint32) SL_BOOLEAN_TRUE}}; 300 301 res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 302 CheckErr(res); 303 /* Realizing the SL Engine in synchronous mode. */ 304 res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 305 CheckErr(res); 306 307 TestPlayStream(sl, argv[1]); 308 309 /* Shutdown OpenSL ES */ 310 (*sl)->Destroy(sl); 311 312 return EXIT_SUCCESS; 313} 314