slesTestDecodeToBuffQueue.cpp revision 7f5cc1afe49395fefaad9b2bbd728a45d1bfda6a
1ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/*
2ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project
3ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi *
4ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
5ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * you may not use this file except in compliance with the License.
6ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * You may obtain a copy of the License at
7ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi *
8ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
9ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi *
10ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
11ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
12ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * See the License for the specific language governing permissions and
14ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * limitations under the License.
15ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi */
16ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
17ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Audio Decode Test
18ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
19ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel TriviFirst run the program from shell:
20ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi  # slesTest_decodeToBuffQueue /sdcard/myFile.mp3 4
21ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
22ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel TriviThese use adb on host to retrieve the decoded file:
23ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi  % adb pull /sdcard/myFile.mp3.raw myFile.raw
24ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
25ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel TriviHow to examine the output with Audacity:
26ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi Project / Import raw data
27ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi Select myFile.raw file, then click Open button
28ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi Choose these options:
29ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi  Signed 16-bit PCM
30ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi  Little-endian
31ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi  1 Channel (Mono) / 2 Channels (Stereo) based on the selected file
32ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi  Sample rate same as the selected file
33ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi Click Import button
34ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
35ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi*/
36ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
37ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
38ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <stdlib.h>
39ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <stdio.h>
40ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <string.h>
41ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <unistd.h>
42ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <sys/time.h>
43ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <fcntl.h>
44ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <utils/threads.h>
45ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
46ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <SLES/OpenSLES.h>
47ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <SLES/OpenSLES_Android.h>
48ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#include <SLES/OpenSLES_AndroidConfiguration.h>
49ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
50ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE and SL_IID_PREFETCHSTATUS
517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi * on the AudioPlayer object for decoding, SL_IID_METADATAEXTRACTION for retrieving the
527f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi * format of the decoded audio */
537f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define NUM_EXPLICIT_INTERFACES_FOR_PLAYER 3
54ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
55ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Size of the decode buffer queue */
56ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define NB_BUFFERS_IN_QUEUE 4
57ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Size of each buffer in the queue */
58ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define BUFFER_SIZE_IN_SAMPLES 1152 // number of samples per MP3 frame
59ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define BUFFER_SIZE_IN_BYTES   (2*BUFFER_SIZE_IN_SAMPLES)
60ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
61ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Local storage for decoded audio data */
62ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviint8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES];
63ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
64ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* destination for decoded data */
65ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivistatic FILE* gFp;
66ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
67ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* to display the number of decode iterations */
68ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivistatic int counter=0;
69ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
70ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* to signal to the test app the end of the stream to decode has been reached */
71ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivibool eos = false;
72ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviandroid::Mutex eosLock;
73ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviandroid::Condition eosCondition;
74ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
75ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* used to detect errors likely to have occured when the OpenSL ES framework fails to open
76ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * a resource, for instance because a file URI is invalid, or an HTTP server doesn't respond.
77ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi */
78ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define PREFETCHEVENT_ERROR_CANDIDATE \
79ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE)
80ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
81ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
82ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Exits the application if an error is encountered */
83ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
84ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
85ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
86ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
87ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
887f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        fprintf(stdout, "Error code %u encountered at line %d, exiting\n", result, line);
89ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        exit(EXIT_FAILURE);
90ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
91ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
92ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
937f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi/* Used to signal prefetching failures */
947f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool prefetchError = false;
957f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
96ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
97ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Structure for passing information to callback function */
98ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivitypedef struct CallbackCntxt_ {
99ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLPlayItf playItf;
100ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLuint32  size;
101ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLint8*   pDataBase;    // Base address of local audio data storage
102ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLint8*   pData;        // Current address of local audio data storage
103ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi} CallbackCntxt;
104ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
105ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
106ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid SignalEos() {
107ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    android::Mutex::Autolock autoLock(eosLock);
108ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    eos = true;
109ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    eosCondition.signal();
110ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
111ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
112ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
113ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Callback for "prefetch" events, here used to detect audio resource opening errors */
114ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid PrefetchEventCallback( SLPrefetchStatusItf caller,  void *pContext, SLuint32 event)
115ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
116ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLpermille level = 0;
117ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*caller)->GetFillLevel(caller, &level);
118ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLuint32 status;
11958432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten    //fprintf(stdout, "PrefetchEventCallback: received event %u\n", event);
120ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*caller)->GetPrefetchStatus(caller, &status);
121ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if ((PREFETCHEVENT_ERROR_CANDIDATE == (event & PREFETCHEVENT_ERROR_CANDIDATE))
122ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) {
123ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "PrefetchEventCallback: Error while prefetching data, exiting\n");
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        prefetchError = true;
125ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SignalEos();
126ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
127ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
128ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
129ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
130ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Callback for "playback" events, i.e. event happening during decoding */
131ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid DecCallback(
132ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLPlayItf caller,
133ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        void *pContext,
134ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLuint32 event)
135ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
136ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_PLAYEVENT_HEADATEND & event) {
137ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "SL_PLAYEVENT_HEADATEND reached\n");
138ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SignalEos();
139ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
140ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
141ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_PLAYEVENT_HEADATNEWPOS & event) {
142ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLmillisecond pMsec = 0;
143ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        (*caller)->GetPosition(caller, &pMsec);
14458432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stdout, "SL_PLAYEVENT_HEADATNEWPOS current position=%ums\n", pMsec);
145ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
146ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
147ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_PLAYEVENT_HEADATMARKER & event) {
148ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLmillisecond pMsec = 0;
149ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        (*caller)->GetPosition(caller, &pMsec);
15058432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stdout, "SL_PLAYEVENT_HEADATMARKER current position=%ums\n", pMsec);
151ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
152ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
153ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
154ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
155ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Callback for decoding buffer queue events */
156ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid DecBufferQueueCallback(
157ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLAndroidSimpleBufferQueueItf queueItf,
158ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        void *pContext)
159ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
160ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    counter++;
1617f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    fprintf(stdout, "DecBufferQueueCallback called (iteration %d)   ", counter);
162ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
163ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    CallbackCntxt *pCntxt = (CallbackCntxt*)pContext;
164ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
165ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Save the decoded data  */
1661fd0cd18598d76e9a0f9e6675e4d988be41644f7Jean-Michel Trivi    if (fwrite(pCntxt->pDataBase, 1, BUFFER_SIZE_IN_BYTES, gFp) < BUFFER_SIZE_IN_BYTES) {
167ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "Error writing to output file, signaling EOS\n");
168ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SignalEos();
169ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        return;
170ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
171ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
172ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Increase data pointer by buffer size */
173ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pCntxt->pData += BUFFER_SIZE_IN_BYTES;
174ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
175ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) {
176ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        pCntxt->pData = pCntxt->pDataBase;
177ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
178ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
179ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) );
180ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    // Note: adding a sleep here or any sync point is a way to slow down the decoding, or
181ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    //  synchronize it with some other event, as the OpenSL ES framework will block until the
182ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    //  buffer queue callback return to proceed with the decoding.
183ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
1847f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#if 0
1857f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* Example buffer queue state display */
186ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLAndroidSimpleBufferQueueState decQueueState;
187ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError( (*queueItf)->GetState(queueItf, &decQueueState) );
188ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
189ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stderr, "\DecBufferQueueCallback now has pCntxt->pData=%p queue: "
19058432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten            "count=%u playIndex=%u\n",
191ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            pCntxt->pData, decQueueState.count, decQueueState.index);
1927f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#endif
1937f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1947f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#if 0
1957f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* Example of duration display in callback where we use the callback context for the SLPlayItf*/
1967f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
1977f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLresult result = (*pCntxt->playItf)->GetDuration(pCntxt->playItf, &durationInMsec);
1987f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    ExitOnError(result);
1997f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (durationInMsec == SL_TIME_UNKNOWN) {
2007f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        fprintf(stdout, "Content duration is unknown (in dec callback)\n");
2017f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
2027f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        fprintf(stdout, "Content duration is %ums (in dec callback)\n",
2037f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi                durationInMsec);
2047f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
2057f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#endif
206ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
207ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
208ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
209ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
210ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Decode an audio path by opening a file descriptor on that path  */
211ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid TestDecToBuffQueue( SLObjectItf sl, const char* path)
212ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
213ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    size_t len = strlen((const char *) path);
214ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    char* outputPath = (char*) malloc(len + 4 + 1); // save room to concatenate ".raw"
215ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (NULL == outputPath) {
216ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
217ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
218ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    memcpy(outputPath, path, len + 1);
219ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    strcat(outputPath, ".raw");
220ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    gFp = fopen(outputPath, "w");
221ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (NULL == gFp) {
222ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
223ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
224ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
225ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLresult  result;
226ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLEngineItf EngineItf;
227ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
228ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Objects this application uses: one audio player */
229ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLObjectItf  player;
230ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
231ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Interfaces for the audio player */
232ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLAndroidSimpleBufferQueueItf decBuffQueueItf;
233ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLPrefetchStatusItf           prefetchItf;
234ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLPlayItf                     playItf;
2357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLMetadataExtractionItf       mdExtrItf;
236ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
237ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Source of audio data for the decoding */
238ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataSource      decSource;
239ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataLocator_URI decUri;
240ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataFormat_MIME decMime;
241ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
242ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Data sink for decoded audio */
243ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataSink                decDest;
244ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataLocator_AndroidSimpleBufferQueue decBuffQueue;
245ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataFormat_PCM          pcm;
246ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
247ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_PLAYER];
248ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_PLAYER];
249ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
250ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the SL Engine Interface which is implicit */
251ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
252ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
253ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
254ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
255ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_PLAYER ; i++) {
256ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
257ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        iidArray[i] = SL_IID_NULL;
258ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
259ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
260ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
261ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
262ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Configuration of the player  */
263ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
264ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Request the AndroidSimpleBufferQueue interface */
265ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
266ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
267ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Request the PrefetchStatus interface */
268ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    required[1] = SL_BOOLEAN_TRUE;
269ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    iidArray[1] = SL_IID_PREFETCHSTATUS;
2707f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* Request the PrefetchStatus interface */
2717f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    required[2] = SL_BOOLEAN_TRUE;
2727f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    iidArray[2] = SL_IID_METADATAEXTRACTION;
273ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
274ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Setup the data source */
275ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decUri.locatorType = SL_DATALOCATOR_URI;
276ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decUri.URI = (SLchar*)path;
277ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decMime.formatType = SL_DATAFORMAT_MIME;
278ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /*     this is how ignored mime information is specified, according to OpenSL ES spec
279ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
280ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decMime.mimeType      = (SLchar*)NULL;
281ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decMime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
282ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decSource.pLocator = (void *) &decUri;
283ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decSource.pFormat  = (void *) &decMime;
284ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
285ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Setup the data sink */
286ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
287ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE;
288ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /*    set up the format of the data in the buffer queue */
289ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.formatType = SL_DATAFORMAT_PCM;
290ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    // FIXME valid value required but currently ignored
291ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.numChannels = 1;
2927f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    pcm.samplesPerSec = SL_SAMPLINGRATE_8;
293ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
294ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.containerSize = 16;
295ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.channelMask = SL_SPEAKER_FRONT_LEFT;
296ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
297ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
298ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decDest.pLocator = (void *) &decBuffQueue;
299ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decDest.pFormat = (void * ) &pcm;
300ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
301ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Create the audio player */
302ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &decSource, &decDest,
303ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            NUM_EXPLICIT_INTERFACES_FOR_PLAYER, iidArray, required);
304ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
305ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Player created\n");
306ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
307ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Realize the player in synchronous mode. */
308ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->Realize(player, SL_BOOLEAN_FALSE);
309ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
310ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Player realized\n");
311ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
312ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the play interface which is implicit */
313ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
314ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
315ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
316ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Set up the player callback to get events during the decoding */
317ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    // FIXME currently ignored
318ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetMarkerPosition(playItf, 2000);
319ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
320ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetPositionUpdatePeriod(playItf, 500);
321ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
322ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetCallbackEventsMask(playItf,
323ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            SL_PLAYEVENT_HEADATMARKER | SL_PLAYEVENT_HEADATNEWPOS | SL_PLAYEVENT_HEADATEND);
324ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
325ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->RegisterCallback(playItf, DecCallback, NULL);
326ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
327ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Play callback registered\n");
328ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
329ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the buffer queue interface which was explicitly requested */
330ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
331ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            (void*)&decBuffQueueItf);
332ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
333ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
334ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the prefetch status interface which was explicitly requested */
335ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
336ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
337ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
338ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
339ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Initialize the callback and its context for the decoding buffer queue */
340ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    CallbackCntxt cntxt;
341ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.playItf = playItf;
342ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.pDataBase = (int8_t*)&pcmData;
343ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.pData = cntxt.pDataBase;
344ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.size = sizeof(pcmData);
345ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*decBuffQueueItf)->RegisterCallback(decBuffQueueItf, DecBufferQueueCallback, &cntxt);
346ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
347ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
348ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Enqueue buffers to map the region of memory allocated to store the decoded data */
349ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout,"Enqueueing buffer ");
350ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) {
351ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout,"%d \n", i);
352ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        result = (*decBuffQueueItf)->Enqueue(decBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES);
353ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        ExitOnError(result);
354ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        cntxt.pData += BUFFER_SIZE_IN_BYTES;
355ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
356ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout,"\n");
357ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.pData = cntxt.pDataBase;
358ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
359ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
360ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Initialize the callback for prefetch errors, if we can't open the resource to decode */
361ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf);
362ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
363ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, PREFETCHEVENT_ERROR_CANDIDATE);
364ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
365ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
366ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
3677f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* Prefetch the data so we can get information about the format before starting to decode */
3687f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /*     1/ cause the player to prefetch the data */
3697f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
3707f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    ExitOnError(result);
3717f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /*     2/ block until data has been prefetched */
3727f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
3737f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLuint32 timeOutIndex = 50; // time out prefetching after 5s
3747f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0) &&
3757f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            !prefetchError) {
3767f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        usleep(10 * 1000);
3777f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
3787f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        timeOutIndex--;
3797f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
3807f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (timeOutIndex == 0 || prefetchError) {
3817f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        fprintf(stderr, "Failure to prefetch data in time, exiting\n");
3827f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ExitOnError(SL_RESULT_CONTENT_NOT_FOUND);
3837f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
3847f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3857f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* ------------------------------------------------------ */
3867f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* Display duration */
3877f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
3887f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    result = (*playItf)->GetDuration(playItf, &durationInMsec);
3897f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    ExitOnError(result);
3907f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (durationInMsec == SL_TIME_UNKNOWN) {
3917f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        fprintf(stdout, "Content duration is unknown\n");
3927f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
3937f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        fprintf(stdout, "Content duration is %ums\n", durationInMsec);
3947f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
3957f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3967f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* ------------------------------------------------------ */
3977f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* Display the metadata obtained from the decoder */
3987f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //   This is for test / demonstration purposes only where we discover the key and value sizes
3997f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //   of a PCM decoder. An application that would want to directly get access to those values
4007f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //   can make assumptions about the size of the keys and their matching values (all SLuint32)
4017f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_METADATAEXTRACTION,
4027f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            (void*)&mdExtrItf);
4037f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    ExitOnError(result);
4047f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLuint32 itemCount;
4057f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    result = (*mdExtrItf)->GetItemCount(mdExtrItf, &itemCount);
4067f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLuint32 i, keySize, valueSize;
4077f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SLMetadataInfo *keyInfo, *value;
4087f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    for(i=0 ; i<itemCount ; i++) {
4097f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        keyInfo = NULL; keySize = 0;
4107f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        value = NULL;   valueSize = 0;
4117f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        result = (*mdExtrItf)->GetKeySize(mdExtrItf, i, &keySize);
4127f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ExitOnError(result);
4137f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        result = (*mdExtrItf)->GetValueSize(mdExtrItf, i, &valueSize);
4147f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ExitOnError(result);
4157f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        keyInfo = (SLMetadataInfo*) malloc(keySize);
4167f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        value   = (SLMetadataInfo*) malloc(valueSize);
4177f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        if ((NULL != keyInfo) && (NULL != value)) {
4187f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            result = (*mdExtrItf)->GetKey(mdExtrItf, i, keySize, keyInfo);
4197f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            ExitOnError(result);
4207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            result = (*mdExtrItf)->GetValue(mdExtrItf, i, valueSize, value);
4217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            ExitOnError(result);
4227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            if ((value->encoding == SL_CHARACTERENCODING_BINARY)
4237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi                    && (value->size == sizeof(SLuint32))) {
4247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi                fprintf(stdout, "key[%d] size=%d, name=%s \tvalue size=%d value=%d\n",
4257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi                        i, keyInfo->size, keyInfo->data, value->size, *((SLuint32*)value->data));
4267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            }
4277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            free(keyInfo);
4287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            free(value);
4297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        }
4307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
4317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
4327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    /* ------------------------------------------------------ */
433ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Start decoding */
434ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
435ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
436ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Starting to decode\n");
437ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
438ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Decode until the end of the stream is reached */
439ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    {
440ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        android::Mutex::Autolock autoLock(eosLock);
441ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        while (!eos) {
442ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            eosCondition.wait(eosLock);
443ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        }
444ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
445ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "EOS signaled\n");
446ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
447ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
448ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* End of decoding */
449ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
450ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Stop decoding */
451ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
452ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
453ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Stopped decoding\n");
454ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
455ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Destroy the AudioPlayer object */
456ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*player)->Destroy(player);
457ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
4587f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //sleep(1);
4597f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
460ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fclose(gFp);
461ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
462ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
463ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
464ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviint main(int argc, char* const argv[])
465ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
466ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLresult    result;
467ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLObjectItf sl;
468ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
469ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf and SLAndroidSimpleBufferQueueItf ",
470ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            argv[0]);
471ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "on an AudioPlayer object to decode a URI to PCM\n");
472ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
473ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (argc != 2) {
474ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "Usage: \t%s source_file\n", argv[0]);
475ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "Example: \"%s /sdcard/myFile.mp3\n", argv[0]);
476ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        exit(EXIT_FAILURE);
477ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
478ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
479ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLEngineOption EngineOption[] = {
480ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
481ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    };
482ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
483ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
484ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
485ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
486ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
487ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
488ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
489ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
490ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    TestDecToBuffQueue(sl, argv[1]);
491ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
492ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Shutdown OpenSL ES */
493ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*sl)->Destroy(sl);
494ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
495ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    return EXIT_SUCCESS;
496ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
497