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 <assert.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include "SLES/OpenSLES.h"
22#include "SLES/OpenSLESUT.h"
23
24int main(int arg, char **argv)
25{
26    SLresult result;
27
28    printf("Create engine\n");
29    SLObjectItf engineObject;
30    // create engine
31    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
32    assert(SL_RESULT_SUCCESS == result);
33    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
34    assert(SL_RESULT_SUCCESS == result);
35    SLEngineItf engineEngine;
36    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
37    assert(SL_RESULT_SUCCESS == result);
38    SLuint32 i;
39    // loop through both valid and invalid object IDs
40    SLuint32 objectID;
41    // Test object IDs from one less than the first valid object
42    // ID, up to one more than the last valid object ID. This way
43    // we can test for both valid and invalid object IDs at both
44    // ends. If more objects are added, be sure to update the macros.
45#define FIRST_VALID SL_OBJECTID_ENGINE
46#define LAST_VALID  SL_OBJECTID_METADATAEXTRACTOR
47    for (objectID = FIRST_VALID - 1; objectID <= LAST_VALID + 1; ++objectID) {
48        printf("object ID %lx", objectID);
49        const char *string = slesutObjectIDToString(objectID);
50        if (NULL != string)
51            printf(" (%s)", string);
52        printf(":\n");
53        result = (*engineEngine)->QueryNumSupportedInterfaces(engineEngine, objectID, NULL);
54        assert(SL_RESULT_PARAMETER_INVALID == result);
55        SLuint32 numSupportedInterfaces = 12345;
56        result = (*engineEngine)->QueryNumSupportedInterfaces(engineEngine, objectID,
57                &numSupportedInterfaces);
58        SLInterfaceID interfaceID;
59        if (SL_RESULT_FEATURE_UNSUPPORTED == result) {
60            printf("  unsupported\n");
61            result = (*engineEngine)->QuerySupportedInterfaces(engineEngine, objectID, 0,
62                &interfaceID);
63            assert(SL_RESULT_FEATURE_UNSUPPORTED == result);
64            assert(NULL == interfaceID);
65            continue;
66        }
67        assert(SL_RESULT_SUCCESS == result);
68        printf("numSupportedInterfaces %lu\n", numSupportedInterfaces);
69        for (i = 0; i < numSupportedInterfaces + 1; ++i) {
70            result = (*engineEngine)->QuerySupportedInterfaces(engineEngine, objectID, i, NULL);
71            assert(SL_RESULT_PARAMETER_INVALID == result);
72            result = (*engineEngine)->QuerySupportedInterfaces(engineEngine, objectID, i,
73                    &interfaceID);
74            if (i < numSupportedInterfaces) {
75                assert(SL_RESULT_SUCCESS == result);
76                printf("    interface %lu ", i);
77                slesutPrintIID(interfaceID);
78            } else {
79                assert(SL_RESULT_PARAMETER_INVALID == result);
80            }
81        }
82    }
83    // query number of extensions
84    result = (*engineEngine)->QueryNumSupportedExtensions(engineEngine, NULL);
85    assert(SL_RESULT_PARAMETER_INVALID == result);
86    SLuint32 numExtensions = 0x12345;
87    result = (*engineEngine)->QueryNumSupportedExtensions(engineEngine, &numExtensions);
88    assert(SL_RESULT_SUCCESS == result);
89    printf("numExtensions = %lu\n", numExtensions);
90    // query names of the extensions
91    for (i = 0; i < numExtensions + 1; ++i) {
92        SLchar extensionName[32];
93        result = (*engineEngine)->QuerySupportedExtension(engineEngine, i, extensionName, NULL);
94        assert(SL_RESULT_PARAMETER_INVALID == result);
95        SLint16 nameLength = -1;
96        result = (*engineEngine)->QuerySupportedExtension(engineEngine, i, NULL, &nameLength);
97        if (i < numExtensions) {
98            assert(SL_RESULT_SUCCESS == result);
99            printf("    extension[%lu] length = %u\n", i, nameLength);
100        } else {
101            assert(SL_RESULT_PARAMETER_INVALID == result);
102            assert(0 == nameLength);
103        }
104        memset(extensionName, 'X', sizeof(extensionName));
105        nameLength = -1;
106        result = (*engineEngine)->QuerySupportedExtension(engineEngine, i, extensionName,
107                &nameLength);
108        if (i < numExtensions) {
109            assert(SL_RESULT_BUFFER_INSUFFICIENT == result);
110        } else {
111            assert(SL_RESULT_PARAMETER_INVALID == result);
112        }
113        assert('X' == extensionName[0]);
114        nameLength = 0;
115        result = (*engineEngine)->QuerySupportedExtension(engineEngine, i, extensionName,
116                &nameLength);
117        if (i < numExtensions) {
118            assert(SL_RESULT_BUFFER_INSUFFICIENT == result);
119        } else {
120            assert(SL_RESULT_PARAMETER_INVALID == result);
121        }
122        assert('X' == extensionName[0]);
123        nameLength = 1;
124        result = (*engineEngine)->QuerySupportedExtension(engineEngine, i, extensionName,
125                &nameLength);
126        if (i < numExtensions) {
127            assert(SL_RESULT_BUFFER_INSUFFICIENT == result);
128            assert('\0' == extensionName[0]);
129        } else {
130            assert(SL_RESULT_PARAMETER_INVALID == result);
131        }
132        assert('X' == extensionName[1]);
133        nameLength = sizeof(extensionName);
134        result = (*engineEngine)->QuerySupportedExtension(engineEngine, i, extensionName,
135                &nameLength);
136        if (i < numExtensions) {
137            assert(SL_RESULT_SUCCESS == result);
138            assert((1 <= nameLength) && (nameLength <= (SLint16) sizeof(extensionName)));
139            printf("    extension[%lu] = \"%.*s\"\n", i, nameLength, extensionName);
140        } else {
141            assert(SL_RESULT_PARAMETER_INVALID == result);
142            assert(0 == nameLength);
143        }
144    }
145    // check if extension is supported
146    SLboolean isSupported = SL_BOOLEAN_TRUE;
147    result = (*engineEngine)->IsExtensionSupported(engineEngine, NULL, &isSupported);
148    assert(SL_RESULT_PARAMETER_INVALID == result);
149    assert(SL_BOOLEAN_FALSE == isSupported);
150    SLchar *unsupportedExt = (SLchar *) "fish";
151    result = (*engineEngine)->IsExtensionSupported(engineEngine, unsupportedExt, NULL);
152    assert(SL_RESULT_PARAMETER_INVALID == result);
153    isSupported = SL_BOOLEAN_TRUE;
154    result = (*engineEngine)->IsExtensionSupported(engineEngine, unsupportedExt, &isSupported);
155    assert(SL_RESULT_SUCCESS == result);
156    assert(SL_BOOLEAN_FALSE == isSupported);
157    SLchar *supportedExt;
158#ifdef ANDROID
159    supportedExt = (SLchar *) "ANDROID_NDK_LEVEL_9";
160#else
161    supportedExt = (SLchar *) "WILHELM_DESKTOP";
162#endif
163    isSupported = SL_BOOLEAN_FALSE;
164    result = (*engineEngine)->IsExtensionSupported(engineEngine, supportedExt, &isSupported);
165    assert(SL_RESULT_SUCCESS == result);
166    assert(SL_BOOLEAN_TRUE == isSupported);
167    // create an extension object with no place to put the new object
168    result = (*engineEngine)->CreateExtensionObject(engineEngine, NULL, NULL, 0x123, 0, NULL, NULL);
169    assert(SL_RESULT_PARAMETER_INVALID == result);
170    // create an extension object, which is unsupported
171    SLObjectItf extensionObject;
172    result = (*engineEngine)->CreateExtensionObject(engineEngine, &extensionObject, NULL, 0x123, 0,
173            NULL, NULL);
174    assert(SL_RESULT_FEATURE_UNSUPPORTED == result);
175    assert(NULL == extensionObject);
176    // destroy engine
177    (*engineObject)->Destroy(engineObject);
178    return EXIT_SUCCESS;
179}
180