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 "OpenSLESUT.h"
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
main(int argc __unused,char ** argv __unused)24 int main(int argc __unused, char **argv __unused)
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     result = slQueryNumSupportedEngineInterfaces(NULL);
32     assert(SL_RESULT_PARAMETER_INVALID == result);
33 
34     printf("Engine number of supported interfaces %u\n", numSupportedInterfaces);
35     SLInterfaceID *engine_ids = calloc(numSupportedInterfaces+1, sizeof(SLInterfaceID));
36     assert(engine_ids != NULL);
37     SLboolean *engine_req = calloc(numSupportedInterfaces+1, sizeof(SLboolean));
38     assert(engine_req != NULL);
39 
40     printf("Display the ID of each available interface\n");
41     SLuint32 index;
42     for (index = 0; index < numSupportedInterfaces + 1; ++index) {
43         SLInterfaceID interfaceID;
44         memset(&interfaceID, 0x55, sizeof(interfaceID));
45         result = slQuerySupportedEngineInterfaces(index, &interfaceID);
46         if (index < numSupportedInterfaces) {
47             assert(SL_RESULT_SUCCESS == result);
48             printf("interface[%u] ", index);
49             slesutPrintIID(interfaceID);
50             engine_ids[index] = interfaceID;
51             engine_req[index] = SL_BOOLEAN_TRUE;
52         } else {
53             assert(SL_RESULT_PARAMETER_INVALID == result);
54         }
55         result = slQuerySupportedEngineInterfaces(index, NULL);
56         assert(SL_RESULT_PARAMETER_INVALID == result);
57     }
58 
59     printf("Create an engine and request all available interfaces\n");
60     SLObjectItf engineObject;
61     if (0 < numSupportedInterfaces) {
62         printf("Create engine with numSupportedInterfaces > 0 but NULL pointers\n");
63         result = slCreateEngine(&engineObject, 0, NULL, numSupportedInterfaces, engine_ids, NULL);
64         assert(SL_RESULT_PARAMETER_INVALID == result);
65         assert(NULL == engineObject);
66         result = slCreateEngine(&engineObject, 0, NULL, numSupportedInterfaces, NULL, engine_req);
67         assert(SL_RESULT_PARAMETER_INVALID == result);
68         assert(NULL == engineObject);
69     }
70 
71     printf("Create engine with no place to return the new engine object\n");
72     result = slCreateEngine(NULL, 0, NULL, numSupportedInterfaces, engine_ids, engine_req);
73     assert(SL_RESULT_PARAMETER_INVALID == result);
74 
75     printf("Create engine with NULL interface pointer\n");
76     SLInterfaceID null_id[1] = {NULL};
77     SLboolean null_req[1] = {SL_BOOLEAN_FALSE};
78     result = slCreateEngine(&engineObject, 0, NULL, 1, null_id, null_req);
79     assert(SL_RESULT_PARAMETER_INVALID == result);
80     assert(NULL == engineObject);
81 
82     printf("Create an engine with numOptions > 0 but NULL pointer\n");
83     result = slCreateEngine(&engineObject, 1, NULL, 0, NULL, NULL);
84     assert(SL_RESULT_PARAMETER_INVALID == result);
85     assert(NULL == engineObject);
86     SLEngineOption options[2];
87     options[0].feature = 0x12345;
88     options[0].data = 0;
89 
90     printf("Create engine with non-sensical option\n");
91     result = slCreateEngine(&engineObject, 1, options, 0, NULL, NULL);
92     assert(SL_RESULT_PARAMETER_INVALID == result);
93     assert(NULL == engineObject);
94 
95     printf("Create an engine and require non-sensical volume interface\n");
96     engine_ids[numSupportedInterfaces] = SL_IID_VOLUME;
97     engine_req[numSupportedInterfaces] = SL_BOOLEAN_TRUE;
98     result = slCreateEngine(&engineObject, 0, NULL, numSupportedInterfaces+1, engine_ids,
99             engine_req);
100     assert(SL_RESULT_FEATURE_UNSUPPORTED == result);
101     assert(NULL == engineObject);
102 
103     printf("Create an engine and politely request a non-sensical interface with options\n");
104     engine_req[numSupportedInterfaces] = SL_BOOLEAN_FALSE;
105     options[0].feature = SL_ENGINEOPTION_THREADSAFE;
106     options[0].data = (SLuint32) SL_BOOLEAN_TRUE;
107     options[1].feature = SL_ENGINEOPTION_LOSSOFCONTROL;
108     options[1].data = (SLuint32) SL_BOOLEAN_FALSE;
109     result = slCreateEngine(&engineObject, 2, options, numSupportedInterfaces+1, engine_ids,
110             engine_req);
111     assert(SL_RESULT_SUCCESS == result);
112     printf("Engine object %p\n", engineObject);
113 
114     printf("Get each available interface before realization\n");
115     for (index = 0; index < numSupportedInterfaces; ++index) {
116         void *interface = NULL;
117         // Use the interface ID as returned by slQuerySupportedEngineInterfaces
118         result = (*engineObject)->GetInterface(engineObject, engine_ids[index], &interface);
119         assert(SL_RESULT_SUCCESS == result || SL_RESULT_PRECONDITIONS_VIOLATED == result);
120         if (SL_RESULT_SUCCESS == result) {
121             printf("interface available pre-realize: ");
122             slesutPrintIID(engine_ids[index]);
123         }
124     }
125 
126     printf("Destroy engine before realization\n");
127     (*engineObject)->Destroy(engineObject);
128 
129     printf("Create engine again\n");
130     result = slCreateEngine(&engineObject, 0, NULL, numSupportedInterfaces, engine_ids, engine_req);
131     assert(SL_RESULT_SUCCESS == result);
132     printf("Engine object %p\n", engineObject);
133 
134     printf("Realize the engine\n");
135     result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
136     assert(SL_RESULT_SUCCESS == result);
137 
138     printf("Get each available interface after realization\n");
139     for (index = 0; index < numSupportedInterfaces; ++index) {
140         void *interface = NULL;
141         result = (*engineObject)->GetInterface(engineObject, engine_ids[index], &interface);
142         assert(SL_RESULT_SUCCESS == result);
143         printf("interface[%u] %p\n", index, interface);
144         // Use a copy of the interface ID to make sure lookup is not purely relying on address
145         void *interface_again = NULL;
146         struct SLInterfaceID_ copy = *engine_ids[index];
147         result = (*engineObject)->GetInterface(engineObject, &copy, &interface_again);
148         assert(SL_RESULT_SUCCESS == result);
149         // Calling GetInterface multiple times should return the same interface
150         assert(interface_again == interface);
151     }
152 
153     SLObjectItf engineObject2;
154 #if 0
155     printf("Create too many engines\n");
156     result = slCreateEngine(&engineObject2, 0, NULL, 0, NULL, NULL);
157     assert(SL_RESULT_RESOURCE_ERROR == result);
158     assert(NULL == engineObject2);
159 #endif
160 
161     printf("Destroy engine\n");
162     (*engineObject)->Destroy(engineObject);
163 
164     printf("Now should be able to create another engine\n");
165     result = slCreateEngine(&engineObject2, 0, NULL, 0, NULL, NULL);
166     assert(SL_RESULT_SUCCESS == result);
167 
168     printf("Exit without destroying engine -- examine log for expected error message\n");
169     free(engine_ids);
170     free(engine_req);
171     return EXIT_SUCCESS;
172 }
173