summaryrefslogtreecommitdiff
path: root/LoopbackApp/app/src/main/java/org/drrickorang/loopback/NativeAudioThread.java
blob: cf39efe160d1185ba7a74e0642ed57edb580912e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drrickorang.loopback;

import java.nio.ByteBuffer;
import java.util.Arrays;

import android.util.Log;
import android.os.Handler;
import android.os.Message;


/**
 * A thread/audio track based audio synth.
 */

public class NativeAudioThread extends Thread {
    private static final String TAG = "NativeAudioThread";

    // for latency test
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_STARTED = 891;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_ERROR = 892;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_COMPLETE = 893;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_COMPLETE_ERRORS = 894;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_STOP = 895;

    // for buffer test
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_STARTED = 896;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_ERROR = 897;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_COMPLETE = 898;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_COMPLETE_ERRORS = 899;
    static final int LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_STOP = 900;

    public boolean  mIsRunning = false;
    public int      mSessionId;
    public double[] mSamples; // store samples that will be shown on WavePlotView
    int             mSamplesIndex;

    private int mTestType;
    private int mSamplingRate;
    private int mMinPlayerBufferSizeInBytes = 0;
    private int mMinRecorderBuffSizeInBytes = 0; // currently not used
    private int mMicSource;

    private boolean mIsRequestStop = false;
    private Handler mMessageHandler;
    private boolean isDestroying = false;
    private boolean hasDestroyingErrors = false;

    // for buffer test
    private int[]   mRecorderBufferPeriod;
    private int     mRecorderMaxBufferPeriod;
    private int[]   mPlayerBufferPeriod;
    private int     mPlayerMaxBufferPeriod;
    private int     mBufferTestWavePlotDurationInSeconds;
    private double  mFrequency1 = Constant.PRIME_FREQUENCY_1;
    private double  mFrequency2 = Constant.PRIME_FREQUENCY_2; // not actually used
    private int     mBufferTestDurationInSeconds;
    private int     mFFTSamplingSize;
    private int     mFFTOverlapSamples;
    private int[]   mAllGlitches;
    private boolean mGlitchingIntervalTooLong;

    private PipeByteBuffer        mPipeByteBuffer;
    private GlitchDetectionThread mGlitchDetectionThread;


    public NativeAudioThread(int samplingRate, int playerBufferInBytes, int recorderBufferInBytes,
                             int micSource, int testType, int bufferTestDurationInSeconds,
                             int bufferTestWavePlotDurationInSeconds) {
        mSamplingRate = samplingRate;
        mMinPlayerBufferSizeInBytes = playerBufferInBytes;
        mMinRecorderBuffSizeInBytes = recorderBufferInBytes;
        mMicSource = micSource;
        mTestType = testType;
        mBufferTestDurationInSeconds = bufferTestDurationInSeconds;
        mBufferTestWavePlotDurationInSeconds = bufferTestWavePlotDurationInSeconds;

        setName("Loopback_NativeAudio");
    }


    //JNI load
    static {
        try {
            System.loadLibrary("loopback");
        } catch (UnsatisfiedLinkError e) {
            log("Error loading loopback JNI library");
            e.printStackTrace();
        }
        /* TODO: gracefully fail/notify if the library can't be loaded */
    }


    //jni calls
    public native long  slesInit(int samplingRate, int frameCount, int micSource,
                                 int testType, double frequency1, ByteBuffer byteBuffer,
                                 short[] sincTone);
    public native int   slesProcessNext(long sles_data, double[] samples, long offset);
    public native int   slesDestroy(long sles_data);

    // to get buffer period data
    public native int[] slesGetRecorderBufferPeriod(long sles_data);
    public native int   slesGetRecorderMaxBufferPeriod(long sles_data);
    public native int[] slesGetPlayerBufferPeriod(long sles_data);
    public native int   slesGetPlayerMaxBufferPeriod(long sles_data);


    public void run() {
        setPriority(Thread.MAX_PRIORITY);
        mIsRunning = true;

        //erase output buffer
        if (mSamples != null)
            mSamples = null;

        //start playing
        log(" Started capture test");
        if (mMessageHandler != null) {
            Message msg = Message.obtain();
            switch (mTestType) {
            case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
                msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_STARTED;
                break;
            case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
                msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_STARTED;
                break;
            }
            mMessageHandler.sendMessage(msg);
        }

        //generate sinc tone use for loopback test
        short loopbackTone[] = new short[mMinPlayerBufferSizeInBytes / Constant.BYTES_PER_FRAME];
        if (mTestType == Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY) {
            ToneGeneration sincToneGen = new RampedSineTone(mSamplingRate,
                    Constant.LOOPBACK_FREQUENCY);
            sincToneGen.generateTone(loopbackTone, loopbackTone.length);
        }

        log(String.format("about to init, sampling rate: %d, buffer:%d", mSamplingRate,
                mMinPlayerBufferSizeInBytes / Constant.BYTES_PER_FRAME));

        // mPipeByteBuffer is only used in buffer test
        mPipeByteBuffer = new PipeByteBuffer(Constant.MAX_SHORTS);
        long startTimeMs = System.currentTimeMillis();
        long sles_data = slesInit(mSamplingRate, mMinPlayerBufferSizeInBytes /
                                  Constant.BYTES_PER_FRAME, mMicSource, mTestType, mFrequency1,
                                  mPipeByteBuffer.getByteBuffer(), loopbackTone);
        log(String.format("sles_data = 0x%X", sles_data));

        if (sles_data == 0) {
            //notify error!!
            log(" ERROR at JNI initialization");
            if (mMessageHandler != null) {
                Message msg = Message.obtain();
                switch (mTestType) {
                case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
                    msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_ERROR;
                    break;
                case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
                    msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_ERROR;
                    break;
                }
                mMessageHandler.sendMessage(msg);
            }
        } else {
            // wait a little bit
            try {
                final int setUpTime = 10;
                sleep(setUpTime); //just to let it start properly
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            int totalSamplesRead = 0;
            switch (mTestType) {
            case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
                final int latencyTestDurationInSeconds = 2;
                int nNewSize = (int) (1.1 * mSamplingRate * latencyTestDurationInSeconds);
                mSamples = new double[nNewSize];
                mSamplesIndex = 0; //reset index
                Arrays.fill(mSamples, 0);

                //TODO use a ByteBuffer to retrieve recorded data instead
                long offset = 0;
                // retrieve native recorder's recorded data
                for (int ii = 0; ii < latencyTestDurationInSeconds; ii++) {
                    log(String.format("block %d...", ii));
                    int samplesRead = slesProcessNext(sles_data, mSamples, offset);
                    totalSamplesRead += samplesRead;
                    offset += samplesRead;
                    log(" [" + ii + "] jni samples read:" + samplesRead +
                        "  currentOffset:" + offset);
                }

                log(String.format(" samplesRead: %d, sampleOffset:%d", totalSamplesRead, offset));
                log("about to destroy...");
                break;
            case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
                //TODO adjust sound level to appropriate level before doing native buffer test
                setUpGlitchDetectionThread();
                long testDurationMs = mBufferTestDurationInSeconds * Constant.MILLIS_PER_SECOND;
                long elapsedTimeMs = System.currentTimeMillis() - startTimeMs;
                while (elapsedTimeMs < testDurationMs) {
                    if (mIsRequestStop) {
                        break;
                    } else {
                        try {
                            final int setUpTime = 100;
                            sleep(setUpTime); //just to let it start properly
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        elapsedTimeMs = System.currentTimeMillis() - startTimeMs;
                    }

                }
                break;


            }

            // collect buffer period data
            mRecorderBufferPeriod = slesGetRecorderBufferPeriod(sles_data);
            mRecorderMaxBufferPeriod = slesGetRecorderMaxBufferPeriod(sles_data);
            mPlayerBufferPeriod = slesGetPlayerBufferPeriod(sles_data);
            mPlayerMaxBufferPeriod = slesGetPlayerMaxBufferPeriod(sles_data);

            // get glitches data only for buffer test
            if (mTestType == Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD) {
                mAllGlitches = mGlitchDetectionThread.getGlitches();
                mSamples = mGlitchDetectionThread.getWaveData();
                mGlitchingIntervalTooLong = mGlitchDetectionThread.getGlitchingIntervalTooLong();
                endDetecting();
            }

            runDestroy(sles_data);

            final int maxTry = 20;
            int tryCount = 0;
            while (isDestroying) {
                try {
                    sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                tryCount++;
                log("destroy try: " + tryCount);

                if (tryCount >= maxTry) {
                    hasDestroyingErrors = true;
                    log("WARNING: waited for max time to properly destroy JNI.");
                    break;
                }
            }
            log(String.format("after destroying. TotalSamplesRead = %d", totalSamplesRead));

            // for buffer test samples won't be read into here
            if (mTestType == Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY
                && totalSamplesRead == 0) {
                //hasDestroyingErrors = true;
                log("Warning: Latency test reads no sample from native recorder!");
            }

            endTest();
        }
    }


    public void requestStopTest() {
        mIsRequestStop = true;
    }


    /** Set up parameters needed for GlitchDetectionThread, then create and run this thread. */
    private void setUpGlitchDetectionThread() {
        final int targetFFTMs = 20; // we want each FFT to cover 20ms of samples
        mFFTSamplingSize = targetFFTMs * mSamplingRate / Constant.MILLIS_PER_SECOND;
        // round to the nearest power of 2
        mFFTSamplingSize = (int) Math.pow(2, Math.round(Math.log(mFFTSamplingSize) / Math.log(2)));

        if (mFFTSamplingSize < 2) {
            mFFTSamplingSize = 2; // mFFTSamplingSize should be at least 2
        }
        mFFTOverlapSamples = mFFTSamplingSize / 2; // mFFTOverlapSamples is half of mFFTSamplingSize

        mGlitchDetectionThread = new GlitchDetectionThread(mFrequency1, mFrequency2, mSamplingRate,
            mFFTSamplingSize, mFFTOverlapSamples, mBufferTestDurationInSeconds,
            mBufferTestWavePlotDurationInSeconds, mPipeByteBuffer);
        mGlitchDetectionThread.start();
    }


    public void endDetecting() {
        mPipeByteBuffer.flush();
        mPipeByteBuffer = null;
        mGlitchDetectionThread.requestStop();
        GlitchDetectionThread tempThread = mGlitchDetectionThread;
        mGlitchDetectionThread = null;
        try {
            tempThread.join(Constant.JOIN_WAIT_TIME_MS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void setMessageHandler(Handler messageHandler) {
        mMessageHandler = messageHandler;
    }


    private void runDestroy(final long sles_data) {
        isDestroying = true;

        //start thread
        final long local_sles_data = sles_data;
        Thread thread = new Thread(new Runnable() {
            public void run() {
                isDestroying = true;
                log("**Start runnable destroy");

                int status = slesDestroy(local_sles_data);
                log(String.format("**End runnable destroy sles delete status: %d", status));
                isDestroying = false;
            }
        });

        thread.start();
        log("end of runDestroy()");
    }


    /** not doing real work, just to keep consistency with LoopbackAudioThread. */
    public void runTest() {

    }


    /** not doing real work, just to keep consistency with LoopbackAudioThread. */
    public void runBufferTest() {

    }


    public void endTest() {
       log("--Ending capture test--");
       if (mMessageHandler != null) {
           Message msg = Message.obtain();
           if (hasDestroyingErrors) {
               switch (mTestType) {
                   case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
                       msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_COMPLETE_ERRORS;
                       break;
                   case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
                       msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_COMPLETE_ERRORS;
                       break;
               }
           } else if (mIsRequestStop) {
               switch (mTestType) {
                   case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
                       msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_STOP;
                       break;
                   case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
                       msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_STOP;
                       break;
               }
           } else {
               switch (mTestType) {
               case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
                   msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_LATENCY_REC_COMPLETE;
                   break;
               case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
                   msg.what = LOOPBACK_NATIVE_AUDIO_THREAD_MESSAGE_BUFFER_REC_COMPLETE;
                   break;
               }
           }

           mMessageHandler.sendMessage(msg);
       }
    }


    public void finish() {
        mIsRunning = false;
    }


    private static void log(String msg) {
        Log.v(TAG, msg);
    }


    double[] getWaveData() {
        return mSamples;
    }


    public int[] getRecorderBufferPeriod() {
        return mRecorderBufferPeriod;
    }


    public int getRecorderMaxBufferPeriod() {
        return mRecorderMaxBufferPeriod;
    }


    public int[] getPlayerBufferPeriod() {
        return mPlayerBufferPeriod;
    }


    public int getPlayerMaxBufferPeriod() {
        return mPlayerMaxBufferPeriod;
    }


    public int[] getNativeAllGlitches() {
        return mAllGlitches;
    }


    public boolean getGlitchingIntervalTooLong() {
        return mGlitchingIntervalTooLong;
    }


    public int getNativeFFTSamplingSize() {
        return mFFTSamplingSize;
    }


    public int getNativeFFTOverlapSamples() {
        return mFFTOverlapSamples;
    }


    public int getDurationInSeconds() {
        return mBufferTestDurationInSeconds;
    }

}