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