slesTestVirtualizerPath.cpp revision 01e9f5fa4698856f92bcfd88188ee4c8397b22db
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#ifdef ANDROID
18#define LOG_NDEBUG 0
19#define LOG_TAG "slesTest_virtualizer"
20
21#include <utils/Log.h>
22#else
23#define LOGV printf
24#endif
25
26#include <getopt.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <unistd.h>
31#include <sys/time.h>
32#include <fcntl.h>
33
34#include "SLES/OpenSLES.h"
35#ifdef ANDROID
36#include "SLES/OpenSLES_Android.h"
37#endif
38
39
40#define MAX_NUMBER_INTERFACES 3
41
42#define TIME_S_BETWEEN_VIRT_ON_OFF 3
43
44//-----------------------------------------------------------------
45/* Exits the application if an error is encountered */
46#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
47
48void ExitOnErrorFunc( SLresult result , int line)
49{
50    if (SL_RESULT_SUCCESS != result) {
51        fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line);
52        exit(1);
53    }
54}
55
56
57//-----------------------------------------------------------------
58
59/* Play an audio path by opening a file descriptor on that path  */
60void TestVirtualizerPathFromFD( SLObjectItf sl, const char* path, int16_t virtStrength)
61{
62    SLresult  result;
63    SLEngineItf EngineItf;
64
65    /* Objects this application uses: one player and an ouput mix */
66    SLObjectItf  player, outputMix;
67
68    /* Source of audio data to play */
69    SLDataSource            audioSource;
70#ifdef ANDROID
71    SLDataLocator_AndroidFD locatorFd;
72#else
73    SLDataLocator_URI       locatorUri;
74#endif
75    SLDataFormat_MIME       mime;
76
77    /* Data sinks for the audio player */
78    SLDataSink               audioSink;
79    SLDataLocator_OutputMix  locator_outputmix;
80
81    /* Play and PrefetchStatus interfaces for the audio player */
82    SLPlayItf              playItf;
83    SLPrefetchStatusItf    prefetchItf;
84    SLVirtualizerItf       virtItf;
85
86    SLboolean required[MAX_NUMBER_INTERFACES];
87    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
88
89    /* Get the SL Engine Interface which is implicit */
90    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
91    ExitOnError(result);
92
93    /* Initialize arrays required[] and iidArray[] */
94    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
95        required[i] = SL_BOOLEAN_FALSE;
96        iidArray[i] = SL_IID_NULL;
97    }
98
99    /* ------------------------------------------------------ */
100    /* Configuration of the output mix  */
101
102    /* Create Output Mix object to be used by the player */
103     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
104     ExitOnError(result);
105
106    /* Realize the Output Mix object in synchronous mode */
107    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
108    ExitOnError(result);
109
110    /* Setup the data sink structure */
111    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
112    locator_outputmix.outputMix   = outputMix;
113    audioSink.pLocator            = (void*)&locator_outputmix;
114    audioSink.pFormat             = NULL;
115
116    /* ------------------------------------------------------ */
117    /* Configuration of the player  */
118
119    /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */
120    /*  (SLPlayItf is implicit) */
121    required[0] = SL_BOOLEAN_TRUE;
122    iidArray[0] = SL_IID_PREFETCHSTATUS;
123    required[1] = SL_BOOLEAN_TRUE;
124    iidArray[1] = SL_IID_VIRTUALIZER;
125
126#ifdef ANDROID
127    /* Setup the data source structure for the URI */
128    locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
129    int fd = open(path, O_RDONLY);
130    if (fd == -1) {
131        ExitOnError(SL_RESULT_RESOURCE_ERROR);
132    }
133    locatorFd.fd = (SLint32) fd;
134    locatorFd.length = SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE;
135    locatorFd.offset = 0;
136#else
137    locatorUri.locatorType = SL_DATALOCATOR_URI;
138    locatorUri.URI = (SLchar *) path;
139#endif
140
141    mime.formatType = SL_DATAFORMAT_MIME;
142    /*     this is how ignored mime information is specified, according to OpenSL ES spec
143     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
144    mime.mimeType      = (SLchar*)NULL;
145    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
146
147    audioSource.pFormat  = (void*)&mime;
148#ifdef ANDROID
149    audioSource.pLocator = (void*)&locatorFd;
150#else
151    audioSource.pLocator = (void*)&locatorUri;
152#endif
153
154    /* Create the audio player */
155    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
156            iidArray, required);
157    ExitOnError(result);
158
159    /* Realize the player in synchronous mode. */
160    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
161    fprintf(stdout, "URI example: after Realize\n");
162
163    /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/
164    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
165    ExitOnError(result);
166
167    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
168    ExitOnError(result);
169
170    result = (*player)->GetInterface(player, SL_IID_VIRTUALIZER, (void*)&virtItf);
171    ExitOnError(result);
172
173    fprintf(stdout, "Player configured\n");
174
175    /* ------------------------------------------------------ */
176    /* Playback and test */
177
178    /* Start the data prefetching by setting the player to the paused state */
179    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
180    ExitOnError(result);
181
182    /* Wait until there's data to play */
183    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
184    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
185        usleep(100 * 1000);
186        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
187        ExitOnError(result);
188    }
189
190    /* Get duration */
191    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
192    result = (*playItf)->GetDuration(playItf, &durationInMsec);
193    ExitOnError(result);
194    if (durationInMsec == SL_TIME_UNKNOWN) {
195        durationInMsec = 5000;
196    }
197
198    /* Start playback */
199    fprintf(stdout, "Starting to play\n");
200    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
201    ExitOnError(result);
202
203    /* Configure Virtualizer */
204    SLboolean strengthSupported = SL_BOOLEAN_FALSE;
205    result = (*virtItf)->IsStrengthSupported(virtItf, &strengthSupported);
206    ExitOnError(result);
207    if (SL_BOOLEAN_FALSE == strengthSupported) {
208        fprintf(stdout, "Virtualizer strength is not supported on this platform. Too bad!\n");
209    } else {
210        fprintf(stdout, "Virtualizer strength is supported, setting strength to %d\n",
211                virtStrength);
212        result = (*virtItf)->SetStrength(virtItf, virtStrength);
213        ExitOnError(result);
214    }
215
216    SLpermille strength = 0;
217    result = (*virtItf)->GetRoundedStrength(virtItf, &strength);
218    ExitOnError(result);
219    fprintf(stdout, "Rounded strength of virt = %d\n", strength);
220
221
222    /* Switch Virtualizer on/off every TIME_S_BETWEEN_VIRT_ON_OFF seconds */
223    SLboolean enabled = SL_BOOLEAN_TRUE;
224    result = (*virtItf)->SetEnabled(virtItf, enabled);
225    ExitOnError(result);
226    for(unsigned int j=0 ; j<(durationInMsec/(1000*TIME_S_BETWEEN_VIRT_ON_OFF)) ; j++) {
227        usleep(TIME_S_BETWEEN_VIRT_ON_OFF * 1000 * 1000);
228        result = (*virtItf)->IsEnabled(virtItf, &enabled);
229        ExitOnError(result);
230        enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE;
231        result = (*virtItf)->SetEnabled(virtItf, enabled);
232        if (SL_BOOLEAN_TRUE == enabled) {
233            fprintf(stdout, "Virtualizer on\n");
234        } else {
235            fprintf(stdout, "Virtualizer off\n");
236        }
237        ExitOnError(result);
238    }
239
240    /* Make sure player is stopped */
241    fprintf(stdout, "Stopping playback\n");
242    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
243    ExitOnError(result);
244
245    /* Destroy the player */
246    (*player)->Destroy(player);
247
248    /* Destroy Output Mix object */
249    (*outputMix)->Destroy(outputMix);
250
251#ifdef ANDROID
252    close(fd);
253#endif
254}
255
256//-----------------------------------------------------------------
257int main(int argc, char* const argv[])
258{
259    LOGV("Starting %s\n", argv[0]);
260
261    SLresult    result;
262    SLObjectItf sl;
263
264    fprintf(stdout, "OpenSL ES test %s: exercises SLVirtualizerItf ", argv[0]);
265    fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
266    fprintf(stdout, "Plays the sound file designated by the given path, ");
267    fprintf(stdout, "and applies a virtualization effect of the specified strength,\n");
268    fprintf(stdout, "where strength is an integer value between 0 and 1000.\n");
269    fprintf(stdout, "Every %d seconds, the Virtualizer will be turned on and off.\n",
270            TIME_S_BETWEEN_VIRT_ON_OFF);
271
272    if (argc < 3) {
273        fprintf(stdout, "Usage: \t%s path virtualization_strength\n", argv[0]);
274        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 1000\" \n", argv[0]);
275        exit(1);
276    }
277
278    SLEngineOption EngineOption[] = {
279            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
280    };
281
282    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
283    ExitOnError(result);
284
285    /* Realizing the SL Engine in synchronous mode. */
286    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
287    ExitOnError(result);
288
289    // intentionally not checking that argv[2], the virtualizer strength, is between 0 and 1000
290    TestVirtualizerPathFromFD(sl, argv[1], (int16_t)atoi(argv[2]));
291
292    /* Shutdown OpenSL ES */
293    (*sl)->Destroy(sl);
294    exit(0);
295
296    return 0;
297}
298