engine.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#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_SUCCESS == result || SL_RESULT_PRECONDITIONS_VIOLATED == result); 63 if (SL_RESULT_SUCCESS == result) { 64 printf("interface available pre-realize: "); 65 slesutPrintIID(engine_ids[index]); 66 } 67 } 68 printf("Destroy engine before realization\n"); 69 (*engineObject)->Destroy(engineObject); 70 printf("Create engine again\n"); 71 result = slCreateEngine(&engineObject, 0, NULL, numSupportedInterfaces, engine_ids, engine_req); 72 assert(SL_RESULT_SUCCESS == result); 73 printf("Engine object %p\n", engineObject); 74 printf("Realize the engine\n"); 75 result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); 76 assert(SL_RESULT_SUCCESS == result); 77 printf("Get each available interface after realization\n"); 78 for (index = 0; index < numSupportedInterfaces; ++index) { 79 void *interface = NULL; 80 result = (*engineObject)->GetInterface(engineObject, engine_ids[index], &interface); 81 assert(SL_RESULT_SUCCESS == result); 82 printf("interface[%lu] %p\n", index, interface); 83 // Use a copy of the interface ID to make sure lookup is not purely relying on address 84 void *interface_again = NULL; 85 struct SLInterfaceID_ copy = *engine_ids[index]; 86 result = (*engineObject)->GetInterface(engineObject, ©, &interface_again); 87 assert(SL_RESULT_SUCCESS == result); 88 // Calling GetInterface multiple times should return the same interface 89 assert(interface_again == interface); 90 } 91 printf("Create too many engines\n"); 92 SLObjectItf engineObject2; 93 result = slCreateEngine(&engineObject2, 0, NULL, 0, NULL, NULL); 94 assert(SL_RESULT_RESOURCE_ERROR == result); 95 assert(NULL == engineObject2); 96 printf("Destroy engine\n"); 97 (*engineObject)->Destroy(engineObject); 98 printf("Now should be able to create another engine\n"); 99 result = slCreateEngine(&engineObject2, 0, NULL, 0, NULL, NULL); 100 assert(SL_RESULT_SUCCESS == result); 101 printf("Exit without destroying engine -- examine log for expected error message\n"); 102 return EXIT_SUCCESS; 103} 104