slesTestDecodeToBuffQueue.cpp revision 58432eb9cea995c69b4f905e68b38c1b8216edeb
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
51ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * on the AudioPlayer object */
52ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define NUM_EXPLICIT_INTERFACES_FOR_PLAYER 2
53ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
54ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Size of the decode buffer queue */
55ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define NB_BUFFERS_IN_QUEUE 4
56ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Size of each buffer in the queue */
57ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define BUFFER_SIZE_IN_SAMPLES 1152 // number of samples per MP3 frame
58ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define BUFFER_SIZE_IN_BYTES   (2*BUFFER_SIZE_IN_SAMPLES)
59ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
60ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Local storage for decoded audio data */
61ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviint8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES];
62ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
63ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* destination for decoded data */
64ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivistatic FILE* gFp;
65ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
66ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* to display the number of decode iterations */
67ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivistatic int counter=0;
68ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
69ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* to signal to the test app the end of the stream to decode has been reached */
70ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivibool eos = false;
71ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviandroid::Mutex eosLock;
72ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviandroid::Condition eosCondition;
73ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
74ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* used to detect errors likely to have occured when the OpenSL ES framework fails to open
75ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi * a resource, for instance because a file URI is invalid, or an HTTP server doesn't respond.
76ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi */
77ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define PREFETCHEVENT_ERROR_CANDIDATE \
78ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE)
79ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
80ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
81ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Exits the application if an error is encountered */
82ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
83ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
84ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
85ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
86ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
8758432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stdout, "%u error code encountered at line %d, exiting\n", result, line);
88ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        exit(EXIT_FAILURE);
89ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
90ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
91ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
92ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
93ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Structure for passing information to callback function */
94ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivitypedef struct CallbackCntxt_ {
95ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLPlayItf playItf;
96ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLuint32  size;
97ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLint8*   pDataBase;    // Base address of local audio data storage
98ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLint8*   pData;        // Current address of local audio data storage
99ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi} CallbackCntxt;
100ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
101ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
102ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid SignalEos() {
103ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    android::Mutex::Autolock autoLock(eosLock);
104ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    eos = true;
105ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    eosCondition.signal();
106ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
107ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
108ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
109ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Callback for "prefetch" events, here used to detect audio resource opening errors */
110ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid PrefetchEventCallback( SLPrefetchStatusItf caller,  void *pContext, SLuint32 event)
111ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
112ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLpermille level = 0;
113ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*caller)->GetFillLevel(caller, &level);
114ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLuint32 status;
11558432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten    //fprintf(stdout, "PrefetchEventCallback: received event %u\n", event);
116ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*caller)->GetPrefetchStatus(caller, &status);
117ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if ((PREFETCHEVENT_ERROR_CANDIDATE == (event & PREFETCHEVENT_ERROR_CANDIDATE))
118ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) {
119ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "PrefetchEventCallback: Error while prefetching data, exiting\n");
120ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SignalEos();
121ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
122ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
123ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
124ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
125ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Callback for "playback" events, i.e. event happening during decoding */
126ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid DecCallback(
127ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLPlayItf caller,
128ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        void *pContext,
129ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLuint32 event)
130ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
131ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_PLAYEVENT_HEADATEND & event) {
132ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "SL_PLAYEVENT_HEADATEND reached\n");
133ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SignalEos();
134ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
135ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
136ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_PLAYEVENT_HEADATNEWPOS & event) {
137ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLmillisecond pMsec = 0;
138ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        (*caller)->GetPosition(caller, &pMsec);
13958432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stdout, "SL_PLAYEVENT_HEADATNEWPOS current position=%ums\n", pMsec);
140ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
141ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
142ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (SL_PLAYEVENT_HEADATMARKER & event) {
143ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLmillisecond pMsec = 0;
144ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        (*caller)->GetPosition(caller, &pMsec);
14558432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stdout, "SL_PLAYEVENT_HEADATMARKER current position=%ums\n", pMsec);
146ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
147ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
148ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
149ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
150ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Callback for decoding buffer queue events */
151ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid DecBufferQueueCallback(
152ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SLAndroidSimpleBufferQueueItf queueItf,
153ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        void *pContext)
154ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
155ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    counter++;
156ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "DecBufferQueueCallback called (iteration %d)\n", counter);
157ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
158ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    CallbackCntxt *pCntxt = (CallbackCntxt*)pContext;
159ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
160ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Save the decoded data  */
1611fd0cd18598d76e9a0f9e6675e4d988be41644f7Jean-Michel Trivi    if (fwrite(pCntxt->pDataBase, 1, BUFFER_SIZE_IN_BYTES, gFp) < BUFFER_SIZE_IN_BYTES) {
162ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "Error writing to output file, signaling EOS\n");
163ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        SignalEos();
164ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        return;
165ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
166ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
167ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Increase data pointer by buffer size */
168ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pCntxt->pData += BUFFER_SIZE_IN_BYTES;
169ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
170ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) {
171ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        pCntxt->pData = pCntxt->pDataBase;
172ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
173ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
174ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) );
175ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    // Note: adding a sleep here or any sync point is a way to slow down the decoding, or
176ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    //  synchronize it with some other event, as the OpenSL ES framework will block until the
177ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    //  buffer queue callback return to proceed with the decoding.
178ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
179ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/*
180ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLAndroidSimpleBufferQueueState decQueueState;
181ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError( (*queueItf)->GetState(queueItf, &decQueueState) );
182ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
183ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stderr, "\DecBufferQueueCallback now has pCntxt->pData=%p queue: "
18458432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten            "count=%u playIndex=%u\n",
185ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            pCntxt->pData, decQueueState.count, decQueueState.index);
186ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi*/
187ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
188ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
189ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
190ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
191ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi/* Decode an audio path by opening a file descriptor on that path  */
192ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivivoid TestDecToBuffQueue( SLObjectItf sl, const char* path)
193ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
194ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    size_t len = strlen((const char *) path);
195ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    char* outputPath = (char*) malloc(len + 4 + 1); // save room to concatenate ".raw"
196ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (NULL == outputPath) {
197ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
198ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
199ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    memcpy(outputPath, path, len + 1);
200ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    strcat(outputPath, ".raw");
201ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    gFp = fopen(outputPath, "w");
202ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (NULL == gFp) {
203ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
204ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
205ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
206ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLresult  result;
207ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLEngineItf EngineItf;
208ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
209ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Objects this application uses: one audio player */
210ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLObjectItf  player;
211ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
212ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Interfaces for the audio player */
213ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLAndroidSimpleBufferQueueItf decBuffQueueItf;
214ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLPrefetchStatusItf           prefetchItf;
215ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLPlayItf                     playItf;
216ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
217ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Source of audio data for the decoding */
218ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataSource      decSource;
219ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataLocator_URI decUri;
220ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataFormat_MIME decMime;
221ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
222ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Data sink for decoded audio */
223ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataSink                decDest;
224ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataLocator_AndroidSimpleBufferQueue decBuffQueue;
225ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLDataFormat_PCM          pcm;
226ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
227ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_PLAYER];
228ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_PLAYER];
229ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
230ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the SL Engine Interface which is implicit */
231ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
232ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
233ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
234ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
235ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_PLAYER ; i++) {
236ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
237ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        iidArray[i] = SL_IID_NULL;
238ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
239ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
240ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
241ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
242ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Configuration of the player  */
243ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
244ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Request the AndroidSimpleBufferQueue interface */
245ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
246ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
247ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Request the PrefetchStatus interface */
248ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    required[1] = SL_BOOLEAN_TRUE;
249ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    iidArray[1] = SL_IID_PREFETCHSTATUS;
250ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
251ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Setup the data source */
252ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decUri.locatorType = SL_DATALOCATOR_URI;
253ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decUri.URI = (SLchar*)path;
254ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decMime.formatType = SL_DATAFORMAT_MIME;
255ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /*     this is how ignored mime information is specified, according to OpenSL ES spec
256ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
257ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decMime.mimeType      = (SLchar*)NULL;
258ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decMime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
259ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decSource.pLocator = (void *) &decUri;
260ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decSource.pFormat  = (void *) &decMime;
261ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
262ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Setup the data sink */
263ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
264ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE;
265ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /*    set up the format of the data in the buffer queue */
266ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.formatType = SL_DATAFORMAT_PCM;
267ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    // FIXME valid value required but currently ignored
268ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.numChannels = 1;
269ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
270ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
271ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.containerSize = 16;
272ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.channelMask = SL_SPEAKER_FRONT_LEFT;
273ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
274ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
275ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decDest.pLocator = (void *) &decBuffQueue;
276ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    decDest.pFormat = (void * ) &pcm;
277ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
278ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Create the audio player */
279ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &decSource, &decDest,
280ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            NUM_EXPLICIT_INTERFACES_FOR_PLAYER, iidArray, required);
281ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
282ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Player created\n");
283ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
284ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Realize the player in synchronous mode. */
285ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->Realize(player, SL_BOOLEAN_FALSE);
286ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
287ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Player realized\n");
288ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
289ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the play interface which is implicit */
290ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
291ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
292ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
293ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Set up the player callback to get events during the decoding */
294ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    // FIXME currently ignored
295ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetMarkerPosition(playItf, 2000);
296ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
297ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetPositionUpdatePeriod(playItf, 500);
298ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
299ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetCallbackEventsMask(playItf,
300ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            SL_PLAYEVENT_HEADATMARKER | SL_PLAYEVENT_HEADATNEWPOS | SL_PLAYEVENT_HEADATEND);
301ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
302ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->RegisterCallback(playItf, DecCallback, NULL);
303ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
304ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Play callback registered\n");
305ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
306ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the buffer queue interface which was explicitly requested */
307ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
308ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            (void*)&decBuffQueueItf);
309ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
310ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
311ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Get the prefetch status interface which was explicitly requested */
312ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
313ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
314ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
315ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
316ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Initialize the callback and its context for the decoding buffer queue */
317ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    CallbackCntxt cntxt;
318ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.playItf = playItf;
319ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.pDataBase = (int8_t*)&pcmData;
320ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.pData = cntxt.pDataBase;
321ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.size = sizeof(pcmData);
322ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*decBuffQueueItf)->RegisterCallback(decBuffQueueItf, DecBufferQueueCallback, &cntxt);
323ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
324ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
325ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Enqueue buffers to map the region of memory allocated to store the decoded data */
326ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout,"Enqueueing buffer ");
327ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) {
328ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout,"%d \n", i);
329ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        result = (*decBuffQueueItf)->Enqueue(decBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES);
330ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        ExitOnError(result);
331ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        cntxt.pData += BUFFER_SIZE_IN_BYTES;
332ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
333ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout,"\n");
334ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    cntxt.pData = cntxt.pDataBase;
335ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
336ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
337ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Initialize the callback for prefetch errors, if we can't open the resource to decode */
338ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf);
339ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
340ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, PREFETCHEVENT_ERROR_CANDIDATE);
341ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
342ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
343ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
344ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Start decoding */
345ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
346ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
347ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Starting to decode\n");
348ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
349ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Decode until the end of the stream is reached */
350ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    {
351ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        android::Mutex::Autolock autoLock(eosLock);
352ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        while (!eos) {
353ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            eosCondition.wait(eosLock);
354ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        }
355ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
356ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "EOS signaled\n");
357ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
358ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* ------------------------------------------------------ */
359ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* End of decoding */
360ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
361ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Stop decoding */
362ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
363ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
364ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "Stopped decoding\n");
365ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
366ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Destroy the AudioPlayer object */
367ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*player)->Destroy(player);
368ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
369ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fclose(gFp);
370ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
371ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
372ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi//-----------------------------------------------------------------
373ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Triviint main(int argc, char* const argv[])
374ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi{
375ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLresult    result;
376ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLObjectItf sl;
377ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
378ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf and SLAndroidSimpleBufferQueueItf ",
379ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            argv[0]);
380ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    fprintf(stdout, "on an AudioPlayer object to decode a URI to PCM\n");
381ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
382ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    if (argc != 2) {
383ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "Usage: \t%s source_file\n", argv[0]);
384ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        fprintf(stdout, "Example: \"%s /sdcard/myFile.mp3\n", argv[0]);
385ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi        exit(EXIT_FAILURE);
386ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    }
387ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
388ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    SLEngineOption EngineOption[] = {
389ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
390ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    };
391ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
392ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
393ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
394ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
395ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
396ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
397ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    ExitOnError(result);
398ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
399ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    TestDecToBuffQueue(sl, argv[1]);
400ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
401ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    /* Shutdown OpenSL ES */
402ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    (*sl)->Destroy(sl);
403ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi
404ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi    return EXIT_SUCCESS;
405ebd4303bd8fb9f1c6a61ff66332248a43b781df5Jean-Michel Trivi}
406