1cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi/*
2cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project
3cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi *
4cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
5cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * you may not use this file except in compliance with the License.
6cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * You may obtain a copy of the License at
7cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi *
8cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
9cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi *
10cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
11cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
12cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * See the License for the specific language governing permissions and
14cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi * limitations under the License.
15cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi */
16cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
17cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#include <stdlib.h>
18cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#include <stdio.h>
19cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#include <string.h>
20cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#include <unistd.h>
21cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#include <sys/time.h>
22cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#include <fcntl.h>
23cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
24c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES.h>
2591bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
26c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES_Android.h>
2791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
28cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
29cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
30cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#define MAX_NUMBER_INTERFACES 3
31cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
32cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#define TIME_S_BETWEEN_VIRT_ON_OFF 3
33cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
34cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi//-----------------------------------------------------------------
35cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi/* Exits the application if an error is encountered */
36cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
37cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
38cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
39cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi{
40cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
4158432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stderr, "%u error code encountered at line %d, exiting\n", result, line);
42c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        exit(EXIT_FAILURE);
43cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
44cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi}
45cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
46cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
47cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi//-----------------------------------------------------------------
48cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
49cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi/* Play an audio path by opening a file descriptor on that path  */
507c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kastenvoid TestVirtualizerPathFromFD( SLObjectItf sl, const char* path, int16_t virtStrength,
517c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        bool alwaysOn)
52cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi{
53cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLresult  result;
54cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLEngineItf EngineItf;
55cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
56cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Objects this application uses: one player and an ouput mix */
57cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLObjectItf  player, outputMix;
58cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
59cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Source of audio data to play */
60cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLDataSource            audioSource;
6191bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
62cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLDataLocator_AndroidFD locatorFd;
6391bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
6491bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    SLDataLocator_URI       locatorUri;
6591bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
66cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLDataFormat_MIME       mime;
67cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
68cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Data sinks for the audio player */
69cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLDataSink               audioSink;
70cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLDataLocator_OutputMix  locator_outputmix;
71cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
72cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Play and PrefetchStatus interfaces for the audio player */
73cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLPlayItf              playItf;
74cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLPrefetchStatusItf    prefetchItf;
75cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLVirtualizerItf       virtItf;
76cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
77cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLboolean required[MAX_NUMBER_INTERFACES];
78cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
79cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
80cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Get the SL Engine Interface which is implicit */
81cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
82cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
83cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
84cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
85cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
86cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
87cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        iidArray[i] = SL_IID_NULL;
88cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
89cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
90cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* ------------------------------------------------------ */
91cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Configuration of the output mix  */
92cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
93cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Create Output Mix object to be used by the player */
94c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 0, iidArray, required);
95cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi     ExitOnError(result);
96cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
97cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Realize the Output Mix object in synchronous mode */
98cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
99cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
100cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
101cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Setup the data sink structure */
102cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
103cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    locator_outputmix.outputMix   = outputMix;
104cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    audioSink.pLocator            = (void*)&locator_outputmix;
105cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    audioSink.pFormat             = NULL;
106cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
107cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* ------------------------------------------------------ */
108cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Configuration of the player  */
109cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
110cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */
111cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /*  (SLPlayItf is implicit) */
112cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
113cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    iidArray[0] = SL_IID_PREFETCHSTATUS;
114cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    required[1] = SL_BOOLEAN_TRUE;
115cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    iidArray[1] = SL_IID_VIRTUALIZER;
116cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
11791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
118cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Setup the data source structure for the URI */
119cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
120cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    int fd = open(path, O_RDONLY);
121cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    if (fd == -1) {
122cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
123cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
124cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    locatorFd.fd = (SLint32) fd;
125cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    locatorFd.length = SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE;
126cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    locatorFd.offset = 0;
12791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
12891bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    locatorUri.locatorType = SL_DATALOCATOR_URI;
12991bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    locatorUri.URI = (SLchar *) path;
13091bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
131cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
132cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    mime.formatType = SL_DATAFORMAT_MIME;
133cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /*     this is how ignored mime information is specified, according to OpenSL ES spec
134cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
135cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    mime.mimeType      = (SLchar*)NULL;
136cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
137cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
138cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    audioSource.pFormat  = (void*)&mime;
13991bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
140cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    audioSource.pLocator = (void*)&locatorFd;
14191bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
14291bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    audioSource.pLocator = (void*)&locatorUri;
14391bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
144cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
145cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Create the audio player */
146cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
147cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi            iidArray, required);
148cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
149cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
150cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Realize the player in synchronous mode. */
151cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
152cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "URI example: after Realize\n");
153cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
154cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/
155cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
156cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
157cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
158cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
159cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
160cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
161cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_VIRTUALIZER, (void*)&virtItf);
162cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
163cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
164cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "Player configured\n");
165cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
166cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* ------------------------------------------------------ */
167cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Playback and test */
168cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
169cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Start the data prefetching by setting the player to the paused state */
170cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
171cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
172cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
173cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Wait until there's data to play */
174cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
175cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
176cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        usleep(100 * 1000);
177cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
178cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        ExitOnError(result);
179cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
180cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
181cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Get duration */
182cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
183cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*playItf)->GetDuration(playItf, &durationInMsec);
184cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
185cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    if (durationInMsec == SL_TIME_UNKNOWN) {
186cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        durationInMsec = 5000;
187cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
188cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
189cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Start playback */
190cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "Starting to play\n");
191cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
192cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
193cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
194cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Configure Virtualizer */
195cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLboolean strengthSupported = SL_BOOLEAN_FALSE;
196cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*virtItf)->IsStrengthSupported(virtItf, &strengthSupported);
197cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
198cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    if (SL_BOOLEAN_FALSE == strengthSupported) {
199cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        fprintf(stdout, "Virtualizer strength is not supported on this platform. Too bad!\n");
200cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    } else {
20101e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten        fprintf(stdout, "Virtualizer strength is supported, setting strength to %d\n",
20201e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                virtStrength);
203cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        result = (*virtItf)->SetStrength(virtItf, virtStrength);
204cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        ExitOnError(result);
205cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
206cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
207cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLpermille strength = 0;
208cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*virtItf)->GetRoundedStrength(virtItf, &strength);
209cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
210cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "Rounded strength of virt = %d\n", strength);
211cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
212cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
2137c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    /* Switch Virtualizer on/off every TIME_S_BETWEEN_VIRT_ON_OFF seconds unless always on */
2147c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    SLboolean previousEnabled = SL_BOOLEAN_FALSE;
215cb5ef4dc25d2f526898d12f008d551877347b9b8Jean-Michel Trivi    for(unsigned int j=0 ; j<(durationInMsec/(1000*TIME_S_BETWEEN_VIRT_ON_OFF)) ; j++) {
2167c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        SLboolean enabled;
217cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        result = (*virtItf)->IsEnabled(virtItf, &enabled);
218cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        ExitOnError(result);
2197c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        enabled = alwaysOn || !enabled;
2207c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        if (enabled != previousEnabled) {
2217c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten            result = (*virtItf)->SetEnabled(virtItf, enabled);
2227c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten            ExitOnError(result);
2237c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten            previousEnabled = enabled;
2247c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten            if (SL_BOOLEAN_TRUE == enabled) {
2257c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten                fprintf(stdout, "Virtualizer on\n");
2267c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten            } else {
2277c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten                fprintf(stdout, "Virtualizer off\n");
2287c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten            }
229cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        }
2307c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        usleep(TIME_S_BETWEEN_VIRT_ON_OFF * 1000 * 1000);
231cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
232cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
233cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Make sure player is stopped */
234cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "Stopping playback\n");
235cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
236cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
237cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
238cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Destroy the player */
239cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    (*player)->Destroy(player);
240cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
241cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Destroy Output Mix object */
242cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    (*outputMix)->Destroy(outputMix);
243cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
24491bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
245cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    close(fd);
24691bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
247cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi}
248cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
249cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi//-----------------------------------------------------------------
250cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Triviint main(int argc, char* const argv[])
251cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi{
2527c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    const char *programName = argv[0];
253cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLresult    result;
254cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLObjectItf sl;
255cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
2567c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    fprintf(stdout, "OpenSL ES test %s: exercises SLVirtualizerItf ", programName);
257cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
258cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "Plays the sound file designated by the given path, ");
259cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "and applies a virtualization effect of the specified strength,\n");
260cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    fprintf(stdout, "where strength is an integer value between 0 and 1000.\n");
2617c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    fprintf(stdout, "Every %d seconds, the Virtualizer will be turned on and off,\n",
262cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi            TIME_S_BETWEEN_VIRT_ON_OFF);
2637c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    fprintf(stdout, "unless the --always-on option is specified before the path.\n");
2647c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten
2657c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    bool alwaysOn = false;
2667c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    if (argc >= 2 && !strcmp(argv[1], "--always-on")) {
2677c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        alwaysOn = true;
2687c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        --argc;
2697c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        ++argv;
2707c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    }
271cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
272cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    if (argc < 3) {
2737c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        fprintf(stdout, "Usage: \t%s [--always-on] path virtualization_strength\n", programName);
2747c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 1000\" \n", programName);
275c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        exit(EXIT_FAILURE);
276cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    }
277cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
278cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    SLEngineOption EngineOption[] = {
279cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
280cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    };
281cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
282cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
283cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
284cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
285cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
286cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
287cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    ExitOnError(result);
288cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
289cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    // intentionally not checking that argv[2], the virtualizer strength, is between 0 and 1000
2907c7511aa96e54d94df836357fbbf76c681f20a26Glenn Kasten    TestVirtualizerPathFromFD(sl, argv[1], (int16_t)atoi(argv[2]), alwaysOn);
291cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
292cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    /* Shutdown OpenSL ES */
293cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi    (*sl)->Destroy(sl);
294cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi
295c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten    return EXIT_SUCCESS;
296cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi}
297