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