srcsink.c revision 0a058cc3d720cdf3f0f8222472a862258482f34f
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Test various combinations of data sources and sinks
18
19#include <assert.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include "SLES/OpenSLES.h"
23
24int main(int argc, char **argv)
25{
26    SLresult result;
27    SLObjectItf engineObject;
28
29    // create engine
30    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
31    assert(SL_RESULT_SUCCESS == result);
32    SLEngineItf engineEngine;
33    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
34    assert(SL_RESULT_SUCCESS == result);
35    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
36    assert(SL_RESULT_SUCCESS == result);
37
38    // configure a typical audio source of 44.1 kHz stereo 16-bit little endian
39    SLDataLocator_BufferQueue loc_bufq;
40    loc_bufq.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
41    loc_bufq.numBuffers = 1;
42    SLDataFormat_PCM format_pcm;
43    format_pcm.formatType = SL_DATAFORMAT_PCM;
44    format_pcm.numChannels = 2;
45    format_pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
46    format_pcm.bitsPerSample = 16;
47    format_pcm.containerSize = 16;
48    format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
49    format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
50    SLDataSource audioSrc;
51    audioSrc.pLocator = &loc_bufq;
52    audioSrc.pFormat = &format_pcm;
53
54    // configure audio sink
55    SLDataLocator_OutputMix loc_outmix;
56    loc_outmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
57    loc_outmix.outputMix = NULL;
58    SLDataSink audioSnk;
59    audioSnk.pLocator = &loc_outmix;
60    audioSnk.pFormat = NULL;
61
62    // create audio player using a NULL output mix
63    SLInterfaceID ids[1] = {SL_IID_BUFFERQUEUE};
64    SLboolean req[1] = {SL_BOOLEAN_TRUE};
65    SLObjectItf playerObject;
66    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
67            &audioSnk, 1, ids, req);
68    assert(SL_RESULT_PARAMETER_INVALID == result);
69    assert(NULL == playerObject);
70
71    // create audio player using an engine as the output mix
72    loc_outmix.outputMix = engineObject;
73    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
74            &audioSnk, 1, ids, req);
75    assert(SL_RESULT_PARAMETER_INVALID == result);
76    assert(NULL == playerObject);
77
78    // create output mix but don't realize it yet
79    SLObjectItf outputMixObject;
80    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
81    assert(SL_RESULT_SUCCESS == result);
82
83    // create audio player using the unrealized output mix
84    loc_outmix.outputMix = outputMixObject;
85    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
86            &audioSnk, 1, ids, req);
87    assert(SL_RESULT_PRECONDITIONS_VIOLATED == result);
88    assert(NULL == playerObject);
89
90    // now realize the output mix
91    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
92    assert(SL_RESULT_SUCCESS == result);
93
94    // create audio player using the realized output mix
95    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
96            &audioSnk, 1, ids, req);
97    assert(SL_RESULT_SUCCESS == result);
98    assert(NULL != playerObject);
99
100    // destroy player
101    (*playerObject)->Destroy(playerObject);
102
103    // now try to create audio player using various unusual parameters
104
105    // number of channels
106    format_pcm.numChannels = 0;
107    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
108            &audioSnk, 1, ids, req);
109    assert(SL_RESULT_PARAMETER_INVALID == result);
110    assert(NULL == playerObject);
111    format_pcm.numChannels = 3;
112    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
113            &audioSnk, 1, ids, req);
114    assert(SL_RESULT_CONTENT_UNSUPPORTED == result);
115    assert(NULL == playerObject);
116    format_pcm.numChannels = 2;
117
118    // sample rate
119    format_pcm.samplesPerSec = 0;
120    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
121            &audioSnk, 1, ids, req);
122    assert(SL_RESULT_PARAMETER_INVALID == result);
123    assert(NULL == playerObject);
124    format_pcm.samplesPerSec = 1000;
125    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
126            &audioSnk, 1, ids, req);
127    assert(SL_RESULT_CONTENT_UNSUPPORTED == result);
128    assert(NULL == playerObject);
129    format_pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
130
131    // bits per sample
132    format_pcm.bitsPerSample = 17;
133    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
134            &audioSnk, 1, ids, req);
135    assert(SL_RESULT_PARAMETER_INVALID == result);
136    assert(NULL == playerObject);
137    format_pcm.bitsPerSample = 24;
138    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
139            &audioSnk, 1, ids, req);
140    assert(SL_RESULT_CONTENT_UNSUPPORTED == result);
141    assert(NULL == playerObject);
142    format_pcm.bitsPerSample = 16;
143
144    // container size
145    format_pcm.containerSize = 8;
146    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
147            &audioSnk, 1, ids, req);
148#if 0
149    assert(SL_RESULT_PARAMETER_INVALID == result);
150    assert(NULL == playerObject);
151#else
152    if (SL_RESULT_PARAMETER_INVALID == result) {
153        printf("ERROR: expected SL_RESULT_PARAMETER_INVALID\n");
154        if (NULL != playerObject)
155            (*playerObject)->Destroy(playerObject);
156    }
157#endif
158    format_pcm.containerSize = 32;
159    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
160            &audioSnk, 1, ids, req);
161    assert(SL_RESULT_CONTENT_UNSUPPORTED == result);
162    assert(NULL == playerObject);
163    format_pcm.containerSize = 16;
164
165    // channel mask
166    format_pcm.channelMask = 0;
167    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
168            &audioSnk, 1, ids, req);
169    assert(SL_RESULT_SUCCESS == result);
170    assert(NULL != playerObject);
171    (*playerObject)->Destroy(playerObject);
172    format_pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
173    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
174            &audioSnk, 1, ids, req);
175    assert(SL_RESULT_PARAMETER_INVALID == result);
176    assert(NULL == playerObject);
177    format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT |
178            SL_SPEAKER_FRONT_CENTER;
179    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
180            &audioSnk, 1, ids, req);
181    assert(SL_RESULT_PARAMETER_INVALID == result);
182    assert(NULL == playerObject);
183    format_pcm.numChannels = 1;
184    format_pcm.channelMask = 0;
185    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
186            &audioSnk, 1, ids, req);
187    assert(SL_RESULT_SUCCESS == result);
188    assert(NULL != playerObject);
189    (*playerObject)->Destroy(playerObject);
190    format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
191    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
192            &audioSnk, 1, ids, req);
193    assert(SL_RESULT_PARAMETER_INVALID == result);
194    assert(NULL == playerObject);
195    format_pcm.numChannels = 2;
196
197    // endianness
198    format_pcm.endianness = SL_BYTEORDER_BIGENDIAN;
199    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
200            &audioSnk, 1, ids, req);
201#ifdef ANDROID // known bug on SDL
202    assert(SL_RESULT_CONTENT_UNSUPPORTED == result);
203    assert(NULL == playerObject);
204#else
205    if (SL_RESULT_CONTENT_UNSUPPORTED != result) {
206        printf("ERROR: expected SL_RESULT_CONTENT_UNSUPPORTED\n");
207        if (NULL != playerObject)
208            (*playerObject)->Destroy(playerObject);
209    }
210#endif
211    format_pcm.endianness = 0;
212    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
213            &audioSnk, 1, ids, req);
214    assert(SL_RESULT_PARAMETER_INVALID == result);
215    assert(NULL == playerObject);
216    format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
217
218    // destroy output mix
219    (*outputMixObject)->Destroy(outputMixObject);
220
221    // destroy engine
222    (*engineObject)->Destroy(engineObject);
223
224    return EXIT_SUCCESS;
225}
226