1 /*
2  * Copyright (C) 2017 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 #ifndef _Included_org_drrickorang_loopback_loopback
18 #define _Included_org_drrickorang_loopback_loopback
19 
20 #include <stdbool.h>
21 #include <time.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef struct {
28     int* timeStampsMs;          // Array of milliseconds since first callback
29     short* callbackDurations;   // Array of milliseconds between callback and previous callback
30     short index;                // Current write position
31     struct timespec startTime;  // Time of first callback {seconds,nanoseconds}
32     int capacity;               // Total number of callback times/lengths that can be recorded
33     bool exceededCapacity;      // Set only if late callbacks come after array is full
34 } callbackTimeStamps;
35 
36 #define NANOS_PER_SECOND 1000000000
37 #define NANOS_PER_MILLI 1000000
38 #define MILLIS_PER_SECOND 1000
39 
40 enum STATUS_ENUM {
41     STATUS_SUCCESS = 0,
42     STATUS_FAIL = 1
43 };
44 
45 enum JAVA_CONSTANTS_ENUM {
46     // Must match constant 'range' in BufferPeriod.java
47     RANGE = 1002,
48     // Must match constants in Constant.java
49     TEST_TYPE_LATENCY = 222,
50     TEST_TYPE_BUFFER_PERIOD = 223,
51     AUDIO_THREAD_TYPE_JAVA = 0,
52     AUDIO_THREAD_TYPE_NATIVE_SLES = 1,
53     AUDIO_THREAD_TYPE_NATIVE_AAUDIO = 2,
54 };
55 
56 typedef struct {
57     int (*computeDefaultSettings)(int performanceMode, int *samplingRate,
58             int *playerBufferFrameCount, int *recorderBufferFrameCount);
59     int (*init)(void **ppCtx, int samplingRate, int frameCount, int micSource,
60             int performanceMode,
61             int testType, double frequency1, char* byteBufferPtr, int byteBufferLength,
62             short* loopbackTone, int maxRecordedLateCallbacks, int ignoreFirstFrames);
63     int (*destroy)(void **ppCtx);
64     int (*processNext)(void *pCtx, double *pSamples, long maxSamples);
65     int* (*getRecorderBufferPeriod)(void *pCtx);
66     int (*getRecorderMaxBufferPeriod)(void *pCtx);
67     int64_t (*getRecorderVarianceBufferPeriod)(void *pCtx);
68     int* (*getPlayerBufferPeriod)(void *pCtx);
69     int (*getPlayerMaxBufferPeriod)(void *pCtx);
70     int64_t (*getPlayerVarianceBufferPeriod)(void *pCtx);
71     int (*getCaptureRank)(void *pCtx);
72     int (*getPlayerTimeStampsAndExpectedBufferPeriod)(void *pCtx, callbackTimeStamps **ppTSs);
73     int (*getRecorderTimeStampsAndExpectedBufferPeriod)(void *pCtx, callbackTimeStamps **ppTSs);
74 } native_engine_t;
75 
76 typedef struct {
77     void *context;
78     native_engine_t *methods;
79 } native_engine_instance_t;
80 
81 enum NATIVE_ENGINE_ENUM {
82     NATIVE_ENGINE_SLES = 0,
83     NATIVE_ENGINE_AAUDIO = 1,
84     NATIVE_ENGINE_COUNT = NATIVE_ENGINE_AAUDIO + 1
85 };
86 
87 extern native_engine_t sEngines[NATIVE_ENGINE_COUNT];
88 
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 #endif  // _Included_org_drrickorang_loopback_loopback
94