-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.20150326.c
233 lines (230 loc) · 9.85 KB
/
main.20150326.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
//#include <string.h>
#include <unistd.h>
//#include <sys/time.h>
#include <stdbool.h>
#include "SLES/OpenSLES.h"
#include "SLES/OpenSLES_Android.h"
#define MAX_NUMBER_INTERFACES 2
#define PREFETCHEVENT_ERROR_CANDIDATE \
(SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE)
#define NB_BUFFERS 16
#define MPEG2_TS_BLOCK_SIZE 188
#define BUFFER_SIZE 20*MPEG2_TS_BLOCK_SIZE
/* Where we store the data to play */
char dataCache[BUFFER_SIZE * NB_BUFFERS];
/* From where we read the data to play */
FILE *file;
/* Has the app reached the end of the file */
bool reachedEof = false;
//-----------------------------------------------------------------
//* Exits the application if an error is encountered */
#define CheckErr(x) ExitOnErrorFunc(x,__LINE__)
void ExitOnErrorFunc( SLresult result , int line)
{
if (SL_RESULT_SUCCESS != result) {
fprintf(stderr, "%lu error code encountered at line %d, exiting\n", (long unsigned int) result, line);
exit(EXIT_FAILURE);
}
}
bool prefetchError = false;
//-----------------------------------------------------------------
/* AndroidBufferQueueItf callback for an audio player */
SLresult AndroidBufferQueueCallback(
SLAndroidSimpleBufferQueueItf caller,
void *pContext, /* input */
//const void *pBufferData, /* input */
void *pBufferData, /* input */
SLuint32 dataSize, /* input */
SLuint32 dataUsed, /* input */
const SLAndroidBufferItem *pItems,/* input */
SLuint32 itemsLength /* input */)
{
// assert(BUFFER_SIZE <= dataSize);
fprintf(stderr, "000000000000000");
size_t nbRead = fread((void*)pBufferData, 1, BUFFER_SIZE, file);
if (nbRead > 0) {
//(*caller)->Enqueue(caller, pBufferData /*pData*/, nbRead /*dataLength*/, NULL /*pMsg*/, 0 /*msgLength*/);
(*caller)->Enqueue(caller, pBufferData , (void *) nbRead );
} else if (!reachedEof) {
// signal EOS
SLAndroidBufferItem msgEos;
msgEos.itemKey = SL_ANDROID_ITEMKEY_EOS;
msgEos.itemSize = 0;
// EOS message has no parameters, so the total size of the message is the size of the key
// plus the size if itemSize, both SLuint32
//(*caller)->Enqueue(caller, NULL /*pData*/, 0 /*dataLength*/, &msgEos /*pMsg*/, sizeof(SLuint32)*2 /*msgLength*/);
(*caller)->Enqueue(caller, NULL /*pData*/, 0 /*dataLength*/);
reachedEof = true;
}
return SL_RESULT_SUCCESS;
}
//-----------------------------------------------------------------
/* Play some music from a URI */
void TestPlayStream( SLObjectItf sl, const char* path)
{
SLEngineItf EngineItf;
SLint32 numOutputs = 0;
SLuint32 deviceID = 0;
SLresult res;
SLDataSource audioSource;
SLDataLocator_AndroidBufferQueue streamLocator;
SLDataFormat_MIME mime;
SLDataSink audioSink;
SLDataLocator_OutputMix locator_outputmix;
SLObjectItf player;
SLPlayItf playItf;
SLVolumeItf volItf;
SLAndroidSimpleBufferQueueItf abqItf;
SLObjectItf OutputMix;
SLboolean required[MAX_NUMBER_INTERFACES];
SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
int playTimeInSec = 60;
file = fopen(path, "rb");
/* Get the SL Engine Interface which is implicit */
res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
CheckErr(res);
/* Initialize arrays required[] and iidArray[] */
int i;
for (i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
required[i] = SL_BOOLEAN_FALSE;
iidArray[i] = SL_IID_NULL;
}
// Set arrays required[] and iidArray[] for VOLUME and PREFETCHSTATUS interface
required[0] = SL_BOOLEAN_TRUE;
iidArray[0] = SL_IID_VOLUME;
required[1] = SL_BOOLEAN_TRUE;
//iidArray[1] = SL_IID_ANDROIDBUFFERQUEUE;
iidArray[1] = SL_IID_BUFFERQUEUE;
// Create Output Mix object to be used by player
res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
iidArray, required); CheckErr(res);
// Realizing the Output Mix object in synchronous mode.
res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
CheckErr(res);
/* Setup the data source structure for the URI */
streamLocator.locatorType = SL_DATALOCATOR_ANDROIDBUFFERQUEUE;
streamLocator.numBuffers = NB_BUFFERS;
mime.formatType = SL_DATAFORMAT_MIME;
mime.mimeType = (SLchar *) "video/mp2ts";//(SLchar*)NULL;
mime.containerType = SL_CONTAINERTYPE_MPEG_TS;
audioSource.pFormat = (void *)&mime;
audioSource.pLocator = (void *)&streamLocator;
/* Setup the data sink structure */
locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
locator_outputmix.outputMix = OutputMix;
audioSink.pLocator = (void *)&locator_outputmix;
audioSink.pFormat = NULL;
/* Create the audio player */
res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink,
MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res);
/* Realizing the player in synchronous mode. */
res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res);
fprintf(stdout, "URI example: after Realize\n");
/* Get interfaces */
res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); CheckErr(res);
res = (*player)->GetInterface(player, SL_IID_VOLUME, (void*)&volItf); CheckErr(res);
//res = (*player)->GetInterface(player, SL_IID_ANDROIDBUFFERQUEUE, (void*)&abqItf);
res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE, (void*)&abqItf);
CheckErr(res);
fprintf(stderr, "9991");
res = (*abqItf)->RegisterCallback(abqItf, AndroidBufferQueueCallback,
// context is not used in the example, but can be used to track who registered
// the buffer queue callback
NULL /*pContext*/); CheckErr(res);
fprintf(stderr, "9992");
/* Display duration */
SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
res = (*playItf)->GetDuration(playItf, &durationInMsec);
CheckErr(res);
if (durationInMsec == SL_TIME_UNKNOWN) {
fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n");
} else {
fprintf(stdout, "Content duration is %lu ms (before starting to prefetch)\n", (long unsigned int) durationInMsec);
}
/* Set the player volume */
res = (*volItf)->SetVolumeLevel( volItf, 0);//-300);
CheckErr(res);
/* Play the URI */
/* first cause the player to prefetch the data */
fprintf(stdout, "Before set to PAUSED\n");
res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
fprintf(stdout, "After set to PAUSED\n");
CheckErr(res);
/* Fill our cache */
fprintf(stderr, "1111");
if (fread(dataCache, 1, BUFFER_SIZE * NB_BUFFERS, file) <= 0) {
fprintf(stderr, "Error filling cache, exiting\n");
goto destroyRes;
}
fprintf(stderr, "222");
/* Enqueue the content of our cache before starting to play,
* we don't want to starve the player */
for (i=0 ; i < NB_BUFFERS ; i++) {
//res = (*abqItf)->Enqueue(abqItf, dataCache + i*BUFFER_SIZE, BUFFER_SIZE);
//CheckErr(res);
}
fprintf(stderr, "333");
fprintf(stdout, "URI example: starting to play\n");
res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
CheckErr(res);
/* Wait as long as the duration of the content before stopping */
fprintf(stdout, "Letting playback go on for %d sec\n", playTimeInSec);
usleep(playTimeInSec /*s*/ * 1000 * 1000);
/* Make sure player is stopped */
fprintf(stdout, "URI example: stopping playback\n");
res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
CheckErr(res);
fprintf(stdout, "sleeping to verify playback stopped\n");
usleep(2 /*s*/ * 1000 * 1000);
destroyRes:
/* Destroy the player */
(*player)->Destroy(player);
/* Destroy Output Mix object */
(*OutputMix)->Destroy(OutputMix);
fclose(file);
}
//-----------------------------------------------------------------
int main(int argc, char* const argv[])
{
SLresult res;
SLObjectItf sl;
fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf, SLAndroidBufferQueue \n",
argv[0]);
fprintf(stdout, "and AudioPlayer with SL_DATALOCATOR_ANDROIDBUFFERQUEUE source / OutputMix sink\n");
fprintf(stdout, "Plays a sound and stops after its reported duration\n\n");
if (argc == 1) {
fprintf(stdout, "Usage: %s path \n\t%s url\n", argv[0], argv[0]);
fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" or \"%s file:///sdcard/my.mp3\"\n",
argv[0], argv[0]);
exit(EXIT_FAILURE);
}
SLEngineOption EngineOption[] = {
{(SLuint32) SL_ENGINEOPTION_THREADSAFE,
(SLuint32) SL_BOOLEAN_TRUE}};
res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
CheckErr(res);
/* Realizing the SL Engine in synchronous mode. */
res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
CheckErr(res);
TestPlayStream(sl, argv[1]);
/* Shutdown OpenSL ES */
(*sl)->Destroy(sl);
return EXIT_SUCCESS;
}