• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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_sles
18 #define _Included_org_drrickorang_loopback_sles
19 
20 #include <SLES/OpenSLES.h>
21 #include <SLES/OpenSLES_Android.h>
22 #include <pthread.h>
23 #include <android/log.h>
24 #include <jni.h>
25 #include <stdbool.h>
26 
27 //struct audio_utils_fifo;
28 #define SLES_PRINTF(...)  __android_log_print(ANDROID_LOG_INFO, "sles_jni", __VA_ARGS__);
29 
30 #include <audio_utils/fifo.h>
31 
32 #include "loopback_sles.h"
33 
34 typedef struct {
35     int* buffer_period;
36     struct timespec previous_time;
37     struct timespec current_time;
38     int buffer_count;
39     int max_buffer_period;
40 
41     volatile int32_t captureRank;   // Set > 0 when the callback requests a systrace/bug report
42 
43     int measurement_count; // number of measurements which were actually recorded
44     int64_t SDM; // sum of squares of deviations from the expected mean
45     int64_t var; // variance in nanoseconds^2
46 } bufferStats;
47 
48 //TODO fix this
49 typedef struct {
50     SLuint32 rxBufCount;     // -r#
51     SLuint32 txBufCount;     // -t#
52     SLuint32 bufSizeInFrames;  // -f#
53     SLuint32 channels;       // -c#
54     SLuint32 sampleRate; // -s#
55     SLuint32 exitAfterSeconds; // -e#
56     SLuint32 freeBufCount;   // calculated
57     SLuint32 bufSizeInBytes; // calculated
58     int injectImpulse; // -i#i
59     size_t totalDiscardedInputFrames;   // total number of input frames discarded
60     int ignoreFirstFrames;
61 
62     // Storage area for the buffer queues
63     char **rxBuffers;
64     char **txBuffers;
65     char **freeBuffers;
66 
67     // Buffer indices
68     SLuint32 rxFront;    // oldest recording
69     SLuint32 rxRear;     // next to be recorded
70     SLuint32 txFront;    // oldest playing
71     SLuint32 txRear;     // next to be played
72     SLuint32 freeFront;  // oldest free
73     SLuint32 freeRear;   // next to be freed
74 
75     struct audio_utils_fifo fifo;   // jitter buffer between recorder and player callbacks,
76                                     // to mitigate unpredictable phase difference between these,
77                                     // or even concurrent callbacks on two CPU cores
78     struct audio_utils_fifo fifo2;  // For sending data to java code (to plot it)
79     short *fifo2Buffer;
80     short *fifoBuffer;
81     SLAndroidSimpleBufferQueueItf recorderBufferQueue;
82     SLBufferQueueItf playerBufferQueue;
83 
84     //other things that belong here
85     SLObjectItf playerObject;
86     SLObjectItf recorderObject;
87     SLObjectItf outputmixObject;
88     SLObjectItf engineObject;
89 
90     bufferStats recorderBufferStats;
91     bufferStats playerBufferStats;
92 
93     int testType;
94     double frequency1;
95     double bufferTestPhase1;
96     int count;
97     char* byteBufferPtr;
98     int byteBufferLength;
99 
100     short* loopbackTone;
101 
102     callbackTimeStamps recorderTimeStamps;
103     callbackTimeStamps playerTimeStamps;
104     short expectedBufferPeriod;
105 } sles_data;
106 
107 // how late in ms a callback must be to trigger a systrace/bugreport
108 #define LATE_CALLBACK_CAPTURE_THRESHOLD 4
109 #define LATE_CALLBACK_OUTLIER_THRESHOLD 1
110 #define BUFFER_PERIOD_DISCARD 10
111 #define BUFFER_PERIOD_DISCARD_FULL_DUPLEX_PARTNER 2
112 
113 #endif //_Included_org_drrickorang_loopback_sles
114