urimime.c revision 711332800108ad6e0e594796e5f8db0da3eff402
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 audio player configurations with URI data source and MIME data format
18
19#include <assert.h>
20#include <math.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include "SLES/OpenSLES.h"
25#ifdef ANDROID
26#include "SLES/OpenSLES_Android.h"
27#endif
28
29int main(int argc, char **argv)
30{
31    if (argc != 2) {
32        fprintf(stderr, "usage: %s URI\n", argv[0]);
33        return EXIT_FAILURE;
34    }
35
36    SLresult result;
37    SLObjectItf engineObject;
38
39    // create engine
40    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
41    assert(SL_RESULT_SUCCESS == result);
42    SLEngineItf engineEngine;
43    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
44    assert(SL_RESULT_SUCCESS == result);
45    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
46    assert(SL_RESULT_SUCCESS == result);
47
48    // create output mix
49    SLObjectItf outputMixObject;
50    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
51    assert(SL_RESULT_SUCCESS == result);
52    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
53    assert(SL_RESULT_SUCCESS == result);
54
55    // configure audio source
56    SLDataLocator_URI loc_uri;
57    loc_uri.locatorType = SL_DATALOCATOR_URI;
58    loc_uri.URI = argv[1];
59    SLDataFormat_MIME format_mime;
60    format_mime.formatType = SL_DATAFORMAT_MIME;
61    format_mime.mimeType = NULL;
62    format_mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
63    SLDataSource audioSrc;
64    audioSrc.pLocator = &loc_uri;
65    audioSrc.pFormat = &format_mime;
66
67    // configure audio sink
68    SLDataLocator_OutputMix loc_outmix;
69    loc_outmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
70    loc_outmix.outputMix = outputMixObject;
71    SLDataSink audioSnk;
72    audioSnk.pLocator = &loc_outmix;
73    audioSnk.pFormat = NULL;
74
75    // create audio player, requesting a buffer queue interface
76    SLuint32 numInterfaces = 1;
77    SLInterfaceID ids[1];
78    SLboolean req[1];
79    ids[0] = SL_IID_BUFFERQUEUE;
80    req[0] = SL_BOOLEAN_TRUE;
81    SLObjectItf playerObject;
82    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
83            &audioSnk, numInterfaces, ids, req);
84    assert(SL_RESULT_FEATURE_UNSUPPORTED == result);
85    assert(NULL == playerObject);
86#ifdef ANDROID
87    ids[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
88    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
89            &audioSnk, numInterfaces, ids, req);
90    assert(SL_RESULT_FEATURE_UNSUPPORTED == result);
91    assert(NULL == playerObject);
92#endif
93
94    // create audio player, without requesting a buffer queue interface
95    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioSrc,
96            &audioSnk, 0, NULL, NULL);
97    assert(SL_RESULT_SUCCESS == result);
98
99    // realize the player
100    result = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
101    assert(SL_RESULT_SUCCESS == result);
102
103    // get the play interface
104    SLPlayItf playerPlay;
105    result = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay);
106    assert(SL_RESULT_SUCCESS == result);
107
108    // get the buffer queue interface
109    SLBufferQueueItf playerBufferQueue;
110    result = (*playerObject)->GetInterface(playerObject, SL_IID_BUFFERQUEUE, &playerBufferQueue);
111    assert(SL_RESULT_FEATURE_UNSUPPORTED == result);
112    assert(NULL == playerBufferQueue);
113#ifdef ANDROID
114    SLAndroidSimpleBufferQueueItf playerAndroidSimpleBufferQueue;
115    result = (*playerObject)->GetInterface(playerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
116            &playerAndroidSimpleBufferQueue);
117    assert(SL_RESULT_FEATURE_UNSUPPORTED == result);
118    assert(NULL == playerAndroidSimpleBufferQueue);
119#endif
120
121    // get the player duration
122    SLmillisecond duration;
123    result = (*playerPlay)->GetDuration(playerPlay, &duration);
124    assert(SL_RESULT_SUCCESS == result);
125    if (SL_TIME_UNKNOWN == duration)
126        printf("Duration: unknown\n");
127    else
128        printf("Duration: %.1f\n", duration / 1000.0f);
129
130    // set the player's state to playing
131    result = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING);
132    assert(SL_RESULT_SUCCESS == result);
133
134    // wait for the playback to finish
135    for (;;) {
136        SLuint32 playState;
137        result = (*playerPlay)->GetPlayState(playerPlay, &playState);
138        assert(SL_RESULT_SUCCESS == result);
139        if (SL_PLAYSTATE_PLAYING != playState) {
140            break;
141        }
142        usleep(10000);
143    }
144
145    // get the player duration
146    result = (*playerPlay)->GetDuration(playerPlay, &duration);
147    assert(SL_RESULT_SUCCESS == result);
148    if (SL_TIME_UNKNOWN == duration)
149        printf("Duration: unknown\n");
150    else
151        printf("Duration: %.1f\n", duration / 1000.0f);
152
153    // destroy audio player
154    (*playerObject)->Destroy(playerObject);
155
156    // destroy output mix
157    (*outputMixObject)->Destroy(outputMixObject);
158
159    // destroy engine
160    (*engineObject)->Destroy(engineObject);
161
162    return EXIT_SUCCESS;
163}
164