15b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi/*
25b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project
35b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi *
45b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
55b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * you may not use this file except in compliance with the License.
65b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * You may obtain a copy of the License at
75b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi *
85b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
95b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi *
105b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
115b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
125b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * See the License for the specific language governing permissions and
145b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi * limitations under the License.
155b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi */
165b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
175b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#include <stdlib.h>
185b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#include <stdio.h>
195b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#include <string.h>
205b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#include <unistd.h>
215b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#include <sys/time.h>
225b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#include <fcntl.h>
235b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
24c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES.h>
25c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES_Android.h>
265b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
275b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
28c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten#define MAX_NUMBER_INTERFACES 1
295b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
305b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#define GUID_DISPLAY_LENGTH 35
315b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#define FX_NAME_LENGTH 64
325b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
335b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivistatic int testMode;
345b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi//-----------------------------------------------------------------
355b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi/* Exits the application if an error is encountered */
365b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
375b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
385b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
395b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi{
405b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
4158432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stderr, "%u error code encountered at line %d, exiting\n", result, line);
42c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        exit(EXIT_FAILURE);
435b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    }
445b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi}
455b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
465b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi//-----------------------------------------------------------------
475b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivivoid guidToString(const SLInterfaceID guid, char *str) {
485b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    if ((NULL == guid) || (NULL == str)) {
495b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi        return;
505b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    }
5158432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten    snprintf(str, GUID_DISPLAY_LENGTH, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
525b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->time_low,
535b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->time_mid,
545b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->time_hi_and_version,
555b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->clock_seq,
565b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->node[0],
575b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->node[1],
585b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->node[2],
595b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->node[3],
605b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->node[4],
615b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            guid->node[5]);
625b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi}
635b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
645b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi//-----------------------------------------------------------------
655b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
665b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi/* Query available effects on Android  */
675b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivivoid TestGenericFxCapabilities(  )
685b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi{
695b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
705b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLresult    result;
715b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLObjectItf sl;
725b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
735b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* ------------------------------------------------------ */
745b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* Engine configuration and creation */
755b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
765b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLEngineOption EngineOption[] = {
775b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
785b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    };
795b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
805b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLboolean required[MAX_NUMBER_INTERFACES];
815b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
825b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
835b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
845b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
855b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
865b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi        iidArray[i] = SL_IID_NULL;
875b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    }
885b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
895b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    iidArray[0] = SL_IID_ANDROIDEFFECTCAPABILITIES;
905b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
915b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
925b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
935b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 1, iidArray, required);
945b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    ExitOnError(result);
955b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
965b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
975b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
985b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    ExitOnError(result);
995b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1005b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1015b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLEngineItf EngineItf;
1025b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLAndroidEffectCapabilitiesItf EffectLibItf;
1035b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1045b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* Get the SL Engine interface which is implicit */
1055b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
1065b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    ExitOnError(result);
1075b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1085b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* Get the Android Effect Capabilities interface */
1095b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ANDROIDEFFECTCAPABILITIES, (void*)&EffectLibItf);
1105b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    ExitOnError(result);
1115b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1125b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* ------------------------------------------------------ */
1135b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* Query the effect library */
1145b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1155b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLuint32 nbEffects = 0;
1165b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    result = (*EffectLibItf)->QueryNumEffects(EffectLibItf, &nbEffects);
1175b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    ExitOnError(result);
11858432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten    fprintf(stdout, "Effect library contains %d effects:\n", nbEffects);
1195b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
120c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten    SLchar effectName[FX_NAME_LENGTH+1];
1215b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLuint16 effectNameLength = FX_NAME_LENGTH;
1225b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    char typeString[GUID_DISPLAY_LENGTH];
1235b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    char implString[GUID_DISPLAY_LENGTH];
1245b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1255b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLInterfaceID effectType, effectImplementation;
1265b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    for (SLuint32 i = 0 ; i < nbEffects ; i++ ) {
12758432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stdout,"- effect %d: ", i);
128c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        memset(effectName, 'Z', FX_NAME_LENGTH+1);
129c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        effectNameLength = FX_NAME_LENGTH;
1305b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi        result = (*EffectLibItf)->QueryEffect(EffectLibItf, i,
1315b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi                &effectType, &effectImplementation, effectName, &effectNameLength);
132c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        if ('Z' != effectName[FX_NAME_LENGTH]) {
133c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten            fprintf(stderr, "QueryEffect wrote beyond end of buffer\n");
134c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten            continue;
135c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        }
1365b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi        ExitOnError(result);
137c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        printf("length=%u ", effectNameLength);
138c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        if (FX_NAME_LENGTH < effectNameLength) {
139c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten            printf(" (>max) ");
140c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten            effectNameLength = FX_NAME_LENGTH;
141c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        }
1425b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi        guidToString(effectType, typeString);
1435b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi        guidToString(effectImplementation, implString);
144c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        effectName[FX_NAME_LENGTH - 1] = '\0';
1457126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten        fprintf(stdout, " type=%s, impl=%s name=%.*s \n", typeString, implString, effectNameLength,
1467126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten                effectName);
1475b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    }
1485b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1495b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    /* Shutdown OpenSL ES */
1505b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi     (*sl)->Destroy(sl);
1515b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi}
1525b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1535b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi//-----------------------------------------------------------------
1545b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Triviint main(int argc, char* const argv[])
1555b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi{
1565b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLresult    result;
1575b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    SLObjectItf sl;
1585b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1595b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    fprintf(stdout, "OpenSL ES test %s: exercises SLAndroidEffectCapabilitiesItf.\n", argv[0]);
1605b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi
1615b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi    TestGenericFxCapabilities();
16223cdb9057c15ec9e0f9f1c63d6b80af84691c597Jean-Michel Trivi
163c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten    return EXIT_SUCCESS;
1645b09aa864215690580b18eb802927e8cc7eb4861Jean-Michel Trivi}
165