engine.c revision 1d8ab068ca8a72771514b443b42003ca06623d5e
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#include "SLES/OpenSLES.h"
18#include "SLES/OpenSLESUT.h"
19#include <assert.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24int main(int argc, char **argv)
25{
26    printf("Get number of available engine interfaces\n");
27    SLresult result;
28    SLuint32 numSupportedInterfaces = 12345;
29    result = slQueryNumSupportedEngineInterfaces(&numSupportedInterfaces);
30    assert(SL_RESULT_SUCCESS == result);
31    printf("Engine number of supported interfaces %lu\n", numSupportedInterfaces);
32    SLInterfaceID *engine_ids = calloc(numSupportedInterfaces, sizeof(SLInterfaceID));
33    assert(engine_ids != NULL);
34    SLboolean *engine_req = calloc(numSupportedInterfaces, sizeof(SLboolean));
35    assert(engine_req != NULL);
36    printf("Display the ID of each available interface\n");
37    SLuint32 index;
38    for (index = 0; index < numSupportedInterfaces + 1; ++index) {
39        SLInterfaceID interfaceID;
40        memset(&interfaceID, 0x55, sizeof(interfaceID));
41        result = slQuerySupportedEngineInterfaces(index, &interfaceID);
42        if (index < numSupportedInterfaces) {
43            assert(SL_RESULT_SUCCESS == result);
44            printf("interface[%lu] ", index);
45            slesutPrintIID(interfaceID);
46            engine_ids[index] = interfaceID;
47            engine_req[index] = SL_BOOLEAN_TRUE;
48        } else {
49            assert(SL_RESULT_PARAMETER_INVALID == result);
50        }
51    }
52    printf("Create an engine and request all available interfaces\n");
53    SLObjectItf engineObject;
54    result = slCreateEngine(&engineObject, 0, NULL, numSupportedInterfaces, engine_ids, engine_req);
55    assert(SL_RESULT_SUCCESS == result);
56    printf("Engine object %p\n", engineObject);
57    printf("Get each available interface before realization\n");
58    for (index = 0; index < numSupportedInterfaces; ++index) {
59        void *interface = NULL;
60        // Use the interface ID as returned by slQuerySupportedEngineInterfaces
61        result = (*engineObject)->GetInterface(engineObject, engine_ids[index], &interface);
62        assert(SL_RESULT_PRECONDITIONS_VIOLATED == result);
63    }
64    printf("Destroy engine before realization\n");
65    (*engineObject)->Destroy(engineObject);
66    printf("Create engine again\n");
67    result = slCreateEngine(&engineObject, 0, NULL, numSupportedInterfaces, engine_ids, engine_req);
68    assert(SL_RESULT_SUCCESS == result);
69    printf("Engine object %p\n", engineObject);
70    printf("Realize the engine\n");
71    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
72    assert(SL_RESULT_SUCCESS == result);
73    printf("Get each available interface after realization\n");
74    for (index = 0; index < numSupportedInterfaces; ++index) {
75        void *interface = NULL;
76        result = (*engineObject)->GetInterface(engineObject, engine_ids[index], &interface);
77        assert(SL_RESULT_SUCCESS == result);
78        printf("interface[%lu] %p\n", index, interface);
79        // Use a copy of the interface ID to make sure lookup is not purely relying on address
80        void *interface_again = NULL;
81        struct SLInterfaceID_ copy = *engine_ids[index];
82        result = (*engineObject)->GetInterface(engineObject, &copy, &interface_again);
83        assert(SL_RESULT_SUCCESS == result);
84        // Calling GetInterface multiple times should return the same interface
85        assert(interface_again == interface);
86    }
87    printf("Destroy engine\n");
88    (*engineObject)->Destroy(engineObject);
89    return EXIT_SUCCESS;
90}
91