1/* 2 * Copyright (C) 2011 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/* AAC ADTS Decode Test 18 19First run the program from shell: 20 # slesTestDecodeAac /sdcard/myFile.adts 21 22Expected output: 23 OpenSL ES test slesTestDecodeAac: decodes a file containing AAC ADTS data 24 Player created 25 Player realized 26 Enqueueing initial empty buffers to receive decoded PCM data 0 1 27 Enqueueing initial full buffers of encoded ADTS data 0 1 28 Starting to decode 29 Frame counters: encoded=4579 decoded=4579 30 31These use adb on host to retrieve the decoded file: 32 % adb pull /sdcard/myFile.adts.raw myFile.raw 33 34How to examine the output with Audacity: 35 Project / Import raw data 36 Select myFile.raw file, then click Open button 37 Choose these options: 38 Signed 16-bit PCM 39 Little-endian 40 1 Channel (Mono) / 2 Channels (Stereo) based on the PCM information obtained when decoding 41 Sample rate based on the PCM information obtained when decoding 42 Click Import button 43 44*/ 45 46#define QUERY_METADATA 47 48#include <assert.h> 49#include <stdlib.h> 50#include <stdio.h> 51#include <string.h> 52#include <unistd.h> 53#include <sys/time.h> 54#include <fcntl.h> 55#include <pthread.h> 56#include <sys/mman.h> 57#include <sys/stat.h> 58#include <unistd.h> 59#include <cpustats/CentralTendencyStatistics.h> 60 61#include <SLES/OpenSLES.h> 62#include <SLES/OpenSLES_Android.h> 63 64/* Explicitly requesting SL_IID_ANDROIDBUFFERQUEUE and SL_IID_ANDROIDSIMPLEBUFFERQUEUE 65 * on the AudioPlayer object for decoding, and 66 * SL_IID_METADATAEXTRACTION for retrieving the format of the decoded audio. 67 */ 68#define NUM_EXPLICIT_INTERFACES_FOR_PLAYER 4 69 70/* Number of decoded samples produced by one AAC frame; defined by the standard */ 71#define SAMPLES_PER_AAC_FRAME 1024 72/* Size of the encoded AAC ADTS buffer queue */ 73#define NB_BUFFERS_IN_ADTS_QUEUE 2 // 2 to 4 is typical 74 75/* Size of the decoded PCM buffer queue */ 76#define NB_BUFFERS_IN_PCM_QUEUE 2 // 2 to 4 is typical 77/* Size of each PCM buffer in the queue */ 78#define BUFFER_SIZE_IN_BYTES (2*sizeof(short)*SAMPLES_PER_AAC_FRAME) 79 80/* Local storage for decoded PCM audio data */ 81int8_t pcmData[NB_BUFFERS_IN_PCM_QUEUE * BUFFER_SIZE_IN_BYTES]; 82 83/* destination for decoded data */ 84static FILE* outputFp; 85 86#ifdef QUERY_METADATA 87/* metadata key index for the PCM format information we want to retrieve */ 88static int channelCountKeyIndex = -1; 89static int sampleRateKeyIndex = -1; 90static int bitsPerSampleKeyIndex = -1; 91static int containerSizeKeyIndex = -1; 92static int channelMaskKeyIndex = -1; 93static int endiannessKeyIndex = -1; 94/* size of the struct to retrieve the PCM format metadata values: the values we're interested in 95 * are SLuint32, but it is saved in the data field of a SLMetadataInfo, hence the larger size. 96 * Note that this size is queried and displayed at l.XXX for demonstration/test purposes. 97 * */ 98#define PCM_METADATA_VALUE_SIZE 32 99/* we only want to query / display the PCM format once */ 100static bool formatQueried = false; 101#endif 102 103/* to signal to the test app that the end of the encoded ADTS stream has been reached */ 104bool eos = false; 105bool endOfEncodedStream = false; 106 107void *ptr; 108unsigned char *frame; 109size_t filelen; 110size_t encodedFrames = 0; 111size_t encodedSamples = 0; 112size_t decodedFrames = 0; 113size_t decodedSamples = 0; 114size_t totalEncodeCompletions = 0; // number of Enqueue completions received 115CentralTendencyStatistics frameStats; 116size_t pauseFrame = 0; // pause after this many decoded frames, zero means don't pause 117SLboolean createRaw = SL_BOOLEAN_TRUE; // whether to create a .raw file containing PCM data 118 119/* constant to identify a buffer context which is the end of the stream to decode */ 120static const int kEosBufferCntxt = 1980; // a magic value we can compare against 121 122/* protects shared variables */ 123pthread_mutex_t eosLock = PTHREAD_MUTEX_INITIALIZER; 124pthread_cond_t eosCondition = PTHREAD_COND_INITIALIZER; 125 126// These are extensions to OpenMAX AL 1.0.1 values 127 128#define PREFETCHSTATUS_UNKNOWN ((SLuint32) 0) 129#define PREFETCHSTATUS_ERROR ((SLuint32) (-1)) 130 131// Mutex and condition shared with main program to protect prefetch_status 132 133static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 134static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 135SLuint32 prefetch_status = PREFETCHSTATUS_UNKNOWN; 136 137/* used to detect errors likely to have occured when the OpenSL ES framework fails to open 138 * a resource, for instance because a file URI is invalid, or an HTTP server doesn't respond. 139 */ 140#define PREFETCHEVENT_ERROR_CANDIDATE \ 141 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE) 142 143//----------------------------------------------------------------- 144/* Exits the application if an error is encountered */ 145#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 146 147void ExitOnErrorFunc( SLresult result , int line) 148{ 149 if (SL_RESULT_SUCCESS != result) { 150 fprintf(stderr, "Error code %u encountered at line %d, exiting\n", result, line); 151 exit(EXIT_FAILURE); 152 } 153} 154 155//----------------------------------------------------------------- 156/* Callback for "prefetch" events, here used to detect audio resource opening errors */ 157void PrefetchEventCallback(SLPrefetchStatusItf caller, void *pContext, SLuint32 event) 158{ 159 // pContext is unused here, so we pass NULL 160 assert(pContext == NULL); 161 SLpermille level = 0; 162 SLresult result; 163 result = (*caller)->GetFillLevel(caller, &level); 164 ExitOnError(result); 165 SLuint32 status; 166 result = (*caller)->GetPrefetchStatus(caller, &status); 167 ExitOnError(result); 168 printf("prefetch level=%d status=0x%x event=%d\n", level, status, event); 169 SLuint32 new_prefetch_status; 170 if ((PREFETCHEVENT_ERROR_CANDIDATE == (event & PREFETCHEVENT_ERROR_CANDIDATE)) 171 && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) { 172 printf("PrefetchEventCallback: Error while prefetching data, exiting\n"); 173 new_prefetch_status = PREFETCHSTATUS_ERROR; 174 } else if (event == SL_PREFETCHEVENT_STATUSCHANGE) { 175 new_prefetch_status = status; 176 } else { 177 return; 178 } 179 int ok; 180 ok = pthread_mutex_lock(&mutex); 181 assert(ok == 0); 182 prefetch_status = new_prefetch_status; 183 ok = pthread_cond_signal(&cond); 184 assert(ok == 0); 185 ok = pthread_mutex_unlock(&mutex); 186 assert(ok == 0); 187} 188 189//----------------------------------------------------------------- 190/* Structure for passing information to callback function */ 191typedef struct CallbackCntxt_ { 192#ifdef QUERY_METADATA 193 SLMetadataExtractionItf metaItf; 194#endif 195 SLPlayItf playItf; 196 SLint8* pDataBase; // Base address of local audio data storage 197 SLint8* pData; // Current address of local audio data storage 198} CallbackCntxt; 199 200// used to notify when SL_PLAYEVENT_HEADATEND event is received 201static pthread_mutex_t head_mutex = PTHREAD_MUTEX_INITIALIZER; 202static pthread_cond_t head_cond = PTHREAD_COND_INITIALIZER; 203static SLboolean head_atend = SL_BOOLEAN_FALSE; 204 205//----------------------------------------------------------------- 206/* Callback for SLPlayItf through which we receive the SL_PLAYEVENT_HEADATEND event */ 207void PlayCallback(SLPlayItf caller, void *pContext __unused, SLuint32 event) { 208 SLmillisecond position; 209 SLresult res = (*caller)->GetPosition(caller, &position); 210 ExitOnError(res); 211 if (event & SL_PLAYEVENT_HEADATMARKER) { 212 printf("SL_PLAYEVENT_HEADATMARKER position=%u ms\n", position); 213 } 214 if (event & SL_PLAYEVENT_HEADATNEWPOS) { 215 printf("SL_PLAYEVENT_HEADATNEWPOS position=%u ms\n", position); 216 } 217 if (event & SL_PLAYEVENT_HEADATEND) { 218 printf("SL_PLAYEVENT_HEADATEND position=%u ms, all decoded data has been received\n", 219 position); 220 pthread_mutex_lock(&head_mutex); 221 head_atend = SL_BOOLEAN_TRUE; 222 pthread_cond_signal(&head_cond); 223 pthread_mutex_unlock(&head_mutex); 224 } 225} 226 227//----------------------------------------------------------------- 228/* Callback for AndroidBufferQueueItf through which we supply ADTS buffers */ 229SLresult AndroidBufferQueueCallback( 230 SLAndroidBufferQueueItf caller, 231 void *pCallbackContext __unused, /* input */ 232 void *pBufferContext, /* input */ 233 void *pBufferData __unused, /* input */ 234 SLuint32 dataSize __unused, /* input */ 235 SLuint32 dataUsed __unused, /* input */ 236 const SLAndroidBufferItem *pItems __unused, /* input */ 237 SLuint32 itemsLength __unused /* input */) 238{ 239 // mutex on all global variables 240 pthread_mutex_lock(&eosLock); 241 SLresult res; 242 243 // for demonstration purposes: 244 // verify what type of information was enclosed in the processed buffer 245 if (NULL != pBufferContext) { 246 if (&kEosBufferCntxt == pBufferContext) { 247 fprintf(stdout, "EOS was processed\n"); 248 } 249 } 250 251 ++totalEncodeCompletions; 252 if (endOfEncodedStream) { 253 // we continue to receive acknowledgement after each buffer was processed 254 if (pBufferContext == (void *) &kEosBufferCntxt) { 255 printf("Received EOS completion after EOS\n"); 256 } else if (pBufferContext == NULL) { 257 printf("Received ADTS completion after EOS\n"); 258 } else { 259 fprintf(stderr, "Received acknowledgement after EOS with unexpected context %p\n", 260 pBufferContext); 261 } 262 } else if (filelen == 0) { 263 // signal EOS to the decoder rather than just starving it 264 printf("Enqueue EOS: encoded frames=%zu, decoded frames=%zu\n", encodedFrames, 265 decodedFrames); 266 printf("You should now see %u ADTS completion%s followed by 1 EOS completion\n", 267 NB_BUFFERS_IN_ADTS_QUEUE - 1, NB_BUFFERS_IN_ADTS_QUEUE != 2 ? "s" : ""); 268 SLAndroidBufferItem msgEos; 269 msgEos.itemKey = SL_ANDROID_ITEMKEY_EOS; 270 msgEos.itemSize = 0; 271 // EOS message has no parameters, so the total size of the message is the size of the key 272 // plus the size of itemSize, both SLuint32 273 res = (*caller)->Enqueue(caller, (void *)&kEosBufferCntxt /*pBufferContext*/, 274 NULL /*pData*/, 0 /*dataLength*/, 275 &msgEos /*pMsg*/, 276 sizeof(SLuint32)*2 /*msgLength*/); 277 ExitOnError(res); 278 endOfEncodedStream = true; 279 // verify that we are at start of an ADTS frame 280 } else if (!(filelen < 7 || frame[0] != 0xFF || (frame[1] & 0xF0) != 0xF0)) { 281 if (pBufferContext != NULL) { 282 fprintf(stderr, "Received acknowledgement before EOS with unexpected context %p\n", 283 pBufferContext); 284 } 285 unsigned framelen = ((frame[3] & 3) << 11) | (frame[4] << 3) | (frame[5] >> 5); 286 if (framelen <= filelen) { 287 // push more data to the queue 288 res = (*caller)->Enqueue(caller, NULL /*pBufferContext*/, 289 frame, framelen, NULL, 0); 290 ExitOnError(res); 291 frame += framelen; 292 filelen -= framelen; 293 ++encodedFrames; 294 encodedSamples += SAMPLES_PER_AAC_FRAME; 295 frameStats.sample(framelen); 296 } else { 297 fprintf(stderr, 298 "partial ADTS frame at EOF discarded; offset=%zu, framelen=%u, filelen=%zu\n", 299 frame - (unsigned char *) ptr, framelen, filelen); 300 frame += filelen; 301 filelen = 0; 302 } 303 } else { 304 fprintf(stderr, "corrupt ADTS frame encountered; offset=%zu, filelen=%zu\n", 305 frame - (unsigned char *) ptr, filelen); 306 frame += filelen; 307 filelen = 0; 308 } 309 pthread_mutex_unlock(&eosLock); 310 311 return SL_RESULT_SUCCESS; 312} 313 314//----------------------------------------------------------------- 315/* Callback for decoding buffer queue events */ 316void DecPlayCallback( 317 SLAndroidSimpleBufferQueueItf queueItf, 318 void *pContext) 319{ 320 // mutex on all global variables 321 pthread_mutex_lock(&eosLock); 322 323 CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; 324 325 /* Save the decoded data to output file */ 326 if (outputFp != NULL && fwrite(pCntxt->pData, 1, BUFFER_SIZE_IN_BYTES, outputFp) 327 < BUFFER_SIZE_IN_BYTES) { 328 fprintf(stderr, "Error writing to output file"); 329 } 330 331 /* Re-enqueue the now empty buffer */ 332 SLresult res; 333 res = (*queueItf)->Enqueue(queueItf, pCntxt->pData, BUFFER_SIZE_IN_BYTES); 334 ExitOnError(res); 335 336 /* Increase data pointer by buffer size, with circular wraparound */ 337 pCntxt->pData += BUFFER_SIZE_IN_BYTES; 338 if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_PCM_QUEUE * BUFFER_SIZE_IN_BYTES)) { 339 pCntxt->pData = pCntxt->pDataBase; 340 } 341 342 // Note: adding a sleep here or any sync point is a way to slow down the decoding, or 343 // synchronize it with some other event, as the OpenSL ES framework will block until the 344 // buffer queue callback return to proceed with the decoding. 345 346#ifdef QUERY_METADATA 347 /* Example: query of the decoded PCM format */ 348 if (!formatQueried) { 349 /* memory to receive the PCM format metadata */ 350 union { 351 SLMetadataInfo pcmMetaData; 352 char withData[PCM_METADATA_VALUE_SIZE]; 353 } u; 354 res = (*pCntxt->metaItf)->GetValue(pCntxt->metaItf, sampleRateKeyIndex, 355 PCM_METADATA_VALUE_SIZE, &u.pcmMetaData); ExitOnError(res); 356 // Note: here we could verify the following: 357 // u.pcmMetaData->encoding == SL_CHARACTERENCODING_BINARY 358 // u.pcmMetaData->size == sizeof(SLuint32) 359 // but the call was successful for the PCM format keys, so those conditions are implied 360 printf("sample rate = %d\n", *((SLuint32*)u.pcmMetaData.data)); 361 res = (*pCntxt->metaItf)->GetValue(pCntxt->metaItf, channelCountKeyIndex, 362 PCM_METADATA_VALUE_SIZE, &u.pcmMetaData); ExitOnError(res); 363 printf("channel count = %d\n", *((SLuint32*)u.pcmMetaData.data)); 364 res = (*pCntxt->metaItf)->GetValue(pCntxt->metaItf, bitsPerSampleKeyIndex, 365 PCM_METADATA_VALUE_SIZE, &u.pcmMetaData); ExitOnError(res); 366 printf("bits per sample = %d bits\n", *((SLuint32*)u.pcmMetaData.data)); 367 res = (*pCntxt->metaItf)->GetValue(pCntxt->metaItf, containerSizeKeyIndex, 368 PCM_METADATA_VALUE_SIZE, &u.pcmMetaData); ExitOnError(res); 369 printf("container size = %d bits\n", *((SLuint32*)u.pcmMetaData.data)); 370 res = (*pCntxt->metaItf)->GetValue(pCntxt->metaItf, channelMaskKeyIndex, 371 PCM_METADATA_VALUE_SIZE, &u.pcmMetaData); ExitOnError(res); 372 printf("channel mask = 0x%X (0x3=front left | front right, 0x4=front center)\n", 373 *((SLuint32*)u.pcmMetaData.data)); 374 res = (*pCntxt->metaItf)->GetValue(pCntxt->metaItf, endiannessKeyIndex, 375 PCM_METADATA_VALUE_SIZE, &u.pcmMetaData); ExitOnError(res); 376 printf("endianness = %d (1=big, 2=little)\n", *((SLuint32*)u.pcmMetaData.data)); 377 formatQueried = true; 378 } 379#endif 380 381 ++decodedFrames; 382 decodedSamples += SAMPLES_PER_AAC_FRAME; 383 384 /* Periodically ask for position and duration */ 385 if ((decodedFrames % 1000 == 0) || endOfEncodedStream) { 386 SLmillisecond position; 387 res = (*pCntxt->playItf)->GetPosition(pCntxt->playItf, &position); 388 ExitOnError(res); 389 SLmillisecond duration; 390 res = (*pCntxt->playItf)->GetDuration(pCntxt->playItf, &duration); 391 ExitOnError(res); 392 if (duration == SL_TIME_UNKNOWN) { 393 printf("After %zu encoded %zu decoded frames: position is %u ms, duration is " 394 "unknown as expected\n", 395 encodedFrames, decodedFrames, position); 396 } else { 397 printf("After %zu encoded %zu decoded frames: position is %u ms, duration is " 398 "surprisingly %u ms\n", 399 encodedFrames, decodedFrames, position, duration); 400 } 401 } 402 403 if (endOfEncodedStream && decodedSamples >= encodedSamples) { 404 eos = true; 405 pthread_cond_signal(&eosCondition); 406 } 407 pthread_mutex_unlock(&eosLock); 408} 409 410//----------------------------------------------------------------- 411 412/* Decode an audio path by opening a file descriptor on that path */ 413void TestDecToBuffQueue( SLObjectItf sl, const char *path, int fd) 414{ 415 // check what kind of object it is 416 int ok; 417 struct stat statbuf; 418 ok = fstat(fd, &statbuf); 419 if (ok < 0) { 420 perror(path); 421 return; 422 } 423 424 // verify that's it is a file 425 if (!S_ISREG(statbuf.st_mode)) { 426 fprintf(stderr, "%s: not an ordinary file\n", path); 427 return; 428 } 429 430 // map file contents into memory to make it easier to access the ADTS frames directly 431 ptr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, (off_t) 0); 432 if (ptr == MAP_FAILED) { 433 perror(path); 434 return; 435 } 436 frame = (unsigned char *) ptr; 437 filelen = statbuf.st_size; 438 439 // create PCM .raw file 440 if (createRaw) { 441 size_t len = strlen((const char *) path); 442 char* outputPath = (char*) malloc(len + 4 + 1); // save room to concatenate ".raw" 443 if (NULL == outputPath) { 444 ExitOnError(SL_RESULT_RESOURCE_ERROR); 445 } 446 memcpy(outputPath, path, len + 1); 447 strcat(outputPath, ".raw"); 448 outputFp = fopen(outputPath, "w"); 449 if (NULL == outputFp) { 450 // issue an error message, but continue the decoding anyway 451 perror(outputPath); 452 } 453 free(outputPath); 454 } else { 455 outputFp = NULL; 456 } 457 458 SLresult res; 459 SLEngineItf EngineItf; 460 461 /* Objects this application uses: one audio player */ 462 SLObjectItf player; 463 464 /* Interfaces for the audio player */ 465 SLPlayItf playItf; 466#ifdef QUERY_METADATA 467 /* to retrieve the decoded PCM format */ 468 SLMetadataExtractionItf mdExtrItf; 469#endif 470 /* to retrieve the PCM samples */ 471 SLAndroidSimpleBufferQueueItf decBuffQueueItf; 472 /* to queue the AAC data to decode */ 473 SLAndroidBufferQueueItf aacBuffQueueItf; 474 /* for prefetch status */ 475 SLPrefetchStatusItf prefetchItf; 476 477 SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_PLAYER]; 478 SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_PLAYER]; 479 480 /* Get the SL Engine Interface which is implicit */ 481 res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 482 ExitOnError(res); 483 484 /* Initialize arrays required[] and iidArray[] */ 485 unsigned int i; 486 for (i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_PLAYER ; i++) { 487 required[i] = SL_BOOLEAN_FALSE; 488 iidArray[i] = SL_IID_NULL; 489 } 490 491 /* ------------------------------------------------------ */ 492 /* Configuration of the player */ 493 494 /* Request the AndroidSimpleBufferQueue interface */ 495 required[0] = SL_BOOLEAN_TRUE; 496 iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE; 497 /* Request the AndroidBufferQueue interface */ 498 required[1] = SL_BOOLEAN_TRUE; 499 iidArray[1] = SL_IID_ANDROIDBUFFERQUEUESOURCE; 500 /* Request the PrefetchStatus interface */ 501 required[2] = SL_BOOLEAN_TRUE; 502 iidArray[2] = SL_IID_PREFETCHSTATUS; 503#ifdef QUERY_METADATA 504 /* Request the MetadataExtraction interface */ 505 required[3] = SL_BOOLEAN_TRUE; 506 iidArray[3] = SL_IID_METADATAEXTRACTION; 507#endif 508 509 /* Setup the data source for queueing AAC buffers of ADTS data */ 510 SLDataLocator_AndroidBufferQueue loc_srcAbq = { 511 SL_DATALOCATOR_ANDROIDBUFFERQUEUE /*locatorType*/, 512 NB_BUFFERS_IN_ADTS_QUEUE /*numBuffers*/}; 513 SLDataFormat_MIME format_srcMime = { 514 SL_DATAFORMAT_MIME /*formatType*/, 515 SL_ANDROID_MIME_AACADTS /*mimeType*/, 516 SL_CONTAINERTYPE_RAW /*containerType*/}; 517 SLDataSource decSource = {&loc_srcAbq /*pLocator*/, &format_srcMime /*pFormat*/}; 518 519 /* Setup the data sink, a buffer queue for buffers of PCM data */ 520 SLDataLocator_AndroidSimpleBufferQueue loc_destBq = { 521 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE/*locatorType*/, 522 NB_BUFFERS_IN_PCM_QUEUE /*numBuffers*/ }; 523 524 /* declare we're decoding to PCM, the parameters after that need to be valid, 525 but are ignored, the decoded format will match the source */ 526 SLDataFormat_PCM format_destPcm = { /*formatType*/ SL_DATAFORMAT_PCM, /*numChannels*/ 1, 527 /*samplesPerSec*/ SL_SAMPLINGRATE_8, /*pcm.bitsPerSample*/ SL_PCMSAMPLEFORMAT_FIXED_16, 528 /*/containerSize*/ 16, /*channelMask*/ SL_SPEAKER_FRONT_LEFT, 529 /*endianness*/ SL_BYTEORDER_LITTLEENDIAN }; 530 SLDataSink decDest = {&loc_destBq /*pLocator*/, &format_destPcm /*pFormat*/}; 531 532 /* Create the audio player */ 533 res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &decSource, &decDest, 534#ifdef QUERY_METADATA 535 NUM_EXPLICIT_INTERFACES_FOR_PLAYER, 536#else 537 NUM_EXPLICIT_INTERFACES_FOR_PLAYER - 1, 538#endif 539 iidArray, required); 540 ExitOnError(res); 541 printf("Player created\n"); 542 543 /* Realize the player in synchronous mode. */ 544 res = (*player)->Realize(player, SL_BOOLEAN_FALSE); 545 ExitOnError(res); 546 printf("Player realized\n"); 547 548 /* Get the play interface which is implicit */ 549 res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 550 ExitOnError(res); 551 552 /* Enable callback when position passes through a marker (SL_PLAYEVENT_HEADATMARKER) */ 553 res = (*playItf)->SetMarkerPosition(playItf, 5000); 554 ExitOnError(res); 555 556 /* Enable callback for periodic position updates (SL_PLAYEVENT_HEADATNEWPOS) */ 557 res = (*playItf)->SetPositionUpdatePeriod(playItf, 3000); 558 ExitOnError(res); 559 560 /* Use the play interface to set up a callback for the SL_PLAYEVENT_HEAD* events */ 561 res = (*playItf)->SetCallbackEventsMask(playItf, 562 SL_PLAYEVENT_HEADATMARKER | SL_PLAYEVENT_HEADATNEWPOS | SL_PLAYEVENT_HEADATEND); 563 ExitOnError(res); 564 res = (*playItf)->RegisterCallback(playItf, PlayCallback /*callback*/, NULL /*pContext*/); 565 ExitOnError(res); 566 567 /* Get the position before prefetch; should be zero */ 568 SLmillisecond position; 569 res = (*playItf)->GetPosition(playItf, &position); 570 ExitOnError(res); 571 if (position == 0) { 572 printf("The position before prefetch is zero as expected\n"); 573 } else if (position == SL_TIME_UNKNOWN) { 574 printf("That's surprising the position before prefetch is unknown"); 575 } else { 576 printf("That's surprising the position before prefetch is %u ms\n", position); 577 } 578 579 /* Get the duration before prefetch; should be unknown */ 580 SLmillisecond duration; 581 res = (*playItf)->GetDuration(playItf, &duration); 582 ExitOnError(res); 583 if (duration == SL_TIME_UNKNOWN) { 584 printf("The duration before prefetch is unknown as expected\n"); 585 } else { 586 printf("That's surprising the duration before prefetch is %u ms\n", duration); 587 } 588 589 /* Get the buffer queue interface which was explicitly requested */ 590 res = (*player)->GetInterface(player, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, (void*)&decBuffQueueItf); 591 ExitOnError(res); 592 593 /* Get the Android buffer queue interface which was explicitly requested */ 594 res = (*player)->GetInterface(player, SL_IID_ANDROIDBUFFERQUEUESOURCE, (void*)&aacBuffQueueItf); 595 ExitOnError(res); 596 597 /* Get the prefetch status interface which was explicitly requested */ 598 res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 599 ExitOnError(res); 600 601#ifdef QUERY_METADATA 602 /* Get the metadata extraction interface which was explicitly requested */ 603 res = (*player)->GetInterface(player, SL_IID_METADATAEXTRACTION, (void*)&mdExtrItf); 604 ExitOnError(res); 605#endif 606 607 /* ------------------------------------------------------ */ 608 /* Initialize the callback and its context for the buffer queue of the decoded PCM */ 609 CallbackCntxt sinkCntxt; 610 sinkCntxt.playItf = playItf; 611#ifdef QUERY_METADATA 612 sinkCntxt.metaItf = mdExtrItf; 613#endif 614 sinkCntxt.pDataBase = (int8_t*)&pcmData; 615 sinkCntxt.pData = sinkCntxt.pDataBase; 616 res = (*decBuffQueueItf)->RegisterCallback(decBuffQueueItf, DecPlayCallback, &sinkCntxt); 617 ExitOnError(res); 618 619 /* Enqueue buffers to map the region of memory allocated to store the decoded data */ 620 printf("Enqueueing initial empty buffers to receive decoded PCM data"); 621 for(i = 0 ; i < NB_BUFFERS_IN_PCM_QUEUE ; i++) { 622 printf(" %d", i); 623 res = (*decBuffQueueItf)->Enqueue(decBuffQueueItf, sinkCntxt.pData, BUFFER_SIZE_IN_BYTES); 624 ExitOnError(res); 625 sinkCntxt.pData += BUFFER_SIZE_IN_BYTES; 626 if (sinkCntxt.pData >= sinkCntxt.pDataBase + 627 (NB_BUFFERS_IN_PCM_QUEUE * BUFFER_SIZE_IN_BYTES)) { 628 sinkCntxt.pData = sinkCntxt.pDataBase; 629 } 630 } 631 printf("\n"); 632 633 /* ------------------------------------------------------ */ 634 /* Initialize the callback for prefetch errors, if we can't open the resource to decode */ 635 res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, NULL); 636 ExitOnError(res); 637 res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, PREFETCHEVENT_ERROR_CANDIDATE); 638 ExitOnError(res); 639 640 /* Initialize the callback for the Android buffer queue of the encoded data */ 641 res = (*aacBuffQueueItf)->RegisterCallback(aacBuffQueueItf, AndroidBufferQueueCallback, NULL); 642 ExitOnError(res); 643 644 /* Enqueue the content of our encoded data before starting to play, 645 we don't want to starve the player initially */ 646 printf("Enqueueing initial full buffers of encoded ADTS data"); 647 for (i=0 ; i < NB_BUFFERS_IN_ADTS_QUEUE ; i++) { 648 if (filelen < 7 || frame[0] != 0xFF || (frame[1] & 0xF0) != 0xF0) { 649 printf("\ncorrupt ADTS frame encountered; offset %zu bytes\n", 650 frame - (unsigned char *) ptr); 651 // Note that prefetch will detect this error soon when it gets a premature EOF 652 break; 653 } 654 unsigned framelen = ((frame[3] & 3) << 11) | (frame[4] << 3) | (frame[5] >> 5); 655 printf(" %d (%u bytes)", i, framelen); 656 res = (*aacBuffQueueItf)->Enqueue(aacBuffQueueItf, NULL /*pBufferContext*/, 657 frame, framelen, NULL, 0); 658 ExitOnError(res); 659 frame += framelen; 660 filelen -= framelen; 661 ++encodedFrames; 662 encodedSamples += SAMPLES_PER_AAC_FRAME; 663 frameStats.sample(framelen); 664 } 665 printf("\n"); 666 667#ifdef QUERY_METADATA 668 /* ------------------------------------------------------ */ 669 /* Get and display the metadata key names for the decoder */ 670 // This is for test / demonstration purposes only where we discover the key and value sizes 671 // of a PCM decoder. An application that would want to directly get access to those values 672 // can make assumptions about the size of the keys and their matching values (all SLuint32), 673 // but it should not make assumptions about the key indices as these are subject to change. 674 // Note that we don't get the metadata values yet; that happens in the first decode callback. 675 SLuint32 itemCount; 676 res = (*mdExtrItf)->GetItemCount(mdExtrItf, &itemCount); 677 ExitOnError(res); 678 printf("itemCount=%u\n", itemCount); 679 SLuint32 keySize, valueSize; 680 SLMetadataInfo *keyInfo, *value; 681 for(i=0 ; i<itemCount ; i++) { 682 keyInfo = NULL; keySize = 0; 683 value = NULL; valueSize = 0; 684 res = (*mdExtrItf)->GetKeySize(mdExtrItf, i, &keySize); 685 ExitOnError(res); 686 res = (*mdExtrItf)->GetValueSize(mdExtrItf, i, &valueSize); 687 ExitOnError(res); 688 keyInfo = (SLMetadataInfo*) malloc(keySize); 689 if (NULL != keyInfo) { 690 res = (*mdExtrItf)->GetKey(mdExtrItf, i, keySize, keyInfo); 691 ExitOnError(res); 692 printf("key[%d] size=%d, name=%s \tvalue size=%d encoding=0x%X langCountry=%s\n", 693 i, keyInfo->size, keyInfo->data, valueSize, keyInfo->encoding, 694 keyInfo->langCountry); 695 /* find out the key index of the metadata we're interested in */ 696 if (!strcmp((char*)keyInfo->data, ANDROID_KEY_PCMFORMAT_NUMCHANNELS)) { 697 channelCountKeyIndex = i; 698 } else if (!strcmp((char*)keyInfo->data, ANDROID_KEY_PCMFORMAT_SAMPLERATE)) { 699 sampleRateKeyIndex = i; 700 } else if (!strcmp((char*)keyInfo->data, ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE)) { 701 bitsPerSampleKeyIndex = i; 702 } else if (!strcmp((char*)keyInfo->data, ANDROID_KEY_PCMFORMAT_CONTAINERSIZE)) { 703 containerSizeKeyIndex = i; 704 } else if (!strcmp((char*)keyInfo->data, ANDROID_KEY_PCMFORMAT_CHANNELMASK)) { 705 channelMaskKeyIndex = i; 706 } else if (!strcmp((char*)keyInfo->data, ANDROID_KEY_PCMFORMAT_ENDIANNESS)) { 707 endiannessKeyIndex = i; 708 } else { 709 printf("Unknown key %s ignored\n", (char *)keyInfo->data); 710 } 711 free(keyInfo); 712 } 713 } 714 if (channelCountKeyIndex != -1) { 715 printf("Key %s is at index %d\n", 716 ANDROID_KEY_PCMFORMAT_NUMCHANNELS, channelCountKeyIndex); 717 } else { 718 fprintf(stderr, "Unable to find key %s\n", ANDROID_KEY_PCMFORMAT_NUMCHANNELS); 719 } 720 if (sampleRateKeyIndex != -1) { 721 printf("Key %s is at index %d\n", 722 ANDROID_KEY_PCMFORMAT_SAMPLERATE, sampleRateKeyIndex); 723 } else { 724 fprintf(stderr, "Unable to find key %s\n", ANDROID_KEY_PCMFORMAT_SAMPLERATE); 725 } 726 if (bitsPerSampleKeyIndex != -1) { 727 printf("Key %s is at index %d\n", 728 ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE, bitsPerSampleKeyIndex); 729 } else { 730 fprintf(stderr, "Unable to find key %s\n", ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE); 731 } 732 if (containerSizeKeyIndex != -1) { 733 printf("Key %s is at index %d\n", 734 ANDROID_KEY_PCMFORMAT_CONTAINERSIZE, containerSizeKeyIndex); 735 } else { 736 fprintf(stderr, "Unable to find key %s\n", ANDROID_KEY_PCMFORMAT_CONTAINERSIZE); 737 } 738 if (channelMaskKeyIndex != -1) { 739 printf("Key %s is at index %d\n", 740 ANDROID_KEY_PCMFORMAT_CHANNELMASK, channelMaskKeyIndex); 741 } else { 742 fprintf(stderr, "Unable to find key %s\n", ANDROID_KEY_PCMFORMAT_CHANNELMASK); 743 } 744 if (endiannessKeyIndex != -1) { 745 printf("Key %s is at index %d\n", 746 ANDROID_KEY_PCMFORMAT_ENDIANNESS, endiannessKeyIndex); 747 } else { 748 fprintf(stderr, "Unable to find key %s\n", ANDROID_KEY_PCMFORMAT_ENDIANNESS); 749 } 750#endif 751 752 // set the player's state to paused, to start prefetching 753 printf("Setting play state to PAUSED\n"); 754 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED); 755 ExitOnError(res); 756 757 // wait for prefetch status callback to indicate either sufficient data or error 758 printf("Awaiting prefetch complete\n"); 759 pthread_mutex_lock(&mutex); 760 while (prefetch_status == PREFETCHSTATUS_UNKNOWN) { 761 pthread_cond_wait(&cond, &mutex); 762 } 763 pthread_mutex_unlock(&mutex); 764 if (prefetch_status == PREFETCHSTATUS_ERROR) { 765 fprintf(stderr, "Error during prefetch, exiting\n"); 766 goto destroyRes; 767 } 768 printf("Prefetch is complete\n"); 769 770 /* ------------------------------------------------------ */ 771 /* Start decoding */ 772 printf("Starting to decode\n"); 773 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING); 774 ExitOnError(res); 775 776 /* Decode until the end of the stream is reached */ 777 printf("Awaiting notification that all encoded buffers have been enqueued\n"); 778 pthread_mutex_lock(&eosLock); 779 while (!eos) { 780 if (pauseFrame > 0) { 781 if (decodedFrames >= pauseFrame) { 782 pauseFrame = 0; 783 printf("Pausing after decoded frame %zu for 10 seconds\n", decodedFrames); 784 pthread_mutex_unlock(&eosLock); 785 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED); 786 ExitOnError(res); 787 sleep(10); 788 printf("Resuming\n"); 789 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING); 790 ExitOnError(res); 791 pthread_mutex_lock(&eosLock); 792 } else { 793 pthread_mutex_unlock(&eosLock); 794 usleep(10*1000); 795 pthread_mutex_lock(&eosLock); 796 } 797 } else { 798 pthread_cond_wait(&eosCondition, &eosLock); 799 } 800 } 801 pthread_mutex_unlock(&eosLock); 802 printf("All encoded buffers have now been enqueued, but there's still more to do\n"); 803 804 /* This just means done enqueueing; there may still more data in decode queue! */ 805 pthread_mutex_lock(&head_mutex); 806 while (!head_atend) { 807 pthread_cond_wait(&head_cond, &head_mutex); 808 } 809 pthread_mutex_unlock(&head_mutex); 810 printf("Decode is now finished\n"); 811 812 pthread_mutex_lock(&eosLock); 813 printf("Frame counters: encoded=%zu decoded=%zu\n", encodedFrames, decodedFrames); 814 printf("Sample counters: encoded=%zu decoded=%zu\n", encodedSamples, decodedSamples); 815 printf("Total encode completions received: actual=%zu, expected=%zu\n", 816 totalEncodeCompletions, encodedFrames+1/*EOS*/); 817 pthread_mutex_unlock(&eosLock); 818 819 /* Get the final position and duration */ 820 res = (*playItf)->GetPosition(playItf, &position); 821 ExitOnError(res); 822 res = (*playItf)->GetDuration(playItf, &duration); 823 ExitOnError(res); 824 if (duration == SL_TIME_UNKNOWN) { 825 printf("The final position is %u ms, duration is unknown\n", position); 826 } else { 827 printf("The final position is %u ms, duration is %u ms\n", position, duration); 828 } 829 830 printf("Frame length statistics:\n"); 831 printf(" n = %u frames\n", frameStats.n()); 832 printf(" mean = %.1f bytes\n", frameStats.mean()); 833 printf(" minimum = %.1f bytes\n", frameStats.minimum()); 834 printf(" maximum = %.1f bytes\n", frameStats.maximum()); 835 printf(" stddev = %.1f bytes\n", frameStats.stddev()); 836 837 /* ------------------------------------------------------ */ 838 /* End of decoding */ 839 840destroyRes: 841 /* Destroy the AudioPlayer object */ 842 (*player)->Destroy(player); 843 844 if (outputFp != NULL) { 845 fclose(outputFp); 846 } 847 848 // unmap the ADTS AAC file from memory 849 ok = munmap(ptr, statbuf.st_size); 850 if (0 != ok) { 851 perror(path); 852 } 853} 854 855//----------------------------------------------------------------- 856int main(int argc, char* const argv[]) 857{ 858 SLresult res; 859 SLObjectItf sl; 860 861 printf("OpenSL ES test %s: decodes a file containing AAC ADTS data\n", argv[0]); 862 863 if (argc != 2) { 864 printf("Usage: \t%s source_file\n", argv[0]); 865 printf("Example: \"%s /sdcard/myFile.adts\n", argv[0]); 866 exit(EXIT_FAILURE); 867 } 868 869 // open pathname of encoded ADTS AAC file to get a file descriptor 870 int fd; 871 fd = open(argv[1], O_RDONLY); 872 if (fd < 0) { 873 perror(argv[1]); 874 return EXIT_FAILURE; 875 } 876 877 SLEngineOption EngineOption[] = { 878 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 879 }; 880 881 res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 882 ExitOnError(res); 883 884 /* Realizing the SL Engine in synchronous mode. */ 885 res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 886 ExitOnError(res); 887 888 TestDecToBuffQueue(sl, argv[1], fd); 889 890 /* Shutdown OpenSL ES */ 891 (*sl)->Destroy(sl); 892 893 // close the file 894 (void) close(fd); 895 896 return EXIT_SUCCESS; 897} 898