1 /*
2  * Copyright (C) 2020 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 package org.hyphonate.megaaudio.recorder;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 import android.os.Message;
22 
23 /**
24  * Defines a super-class for apps to receive notifications of recording events. Concrete
25  * subclasses need to implement the <code>handleMessage(Message)</code> method.
26  */
27 public class JavaSinkHandler extends Handler {
28     @SuppressWarnings("unused") private static final String TAG = JavaSinkHandler.class.getSimpleName();
29     @SuppressWarnings("unused") private static final boolean LOG = false;
30 
31     protected JavaRecorder mRecorder;
32 
33     private AudioSink mSink = null;
34 
JavaSinkHandler(JavaRecorder recorder, AudioSink sink, Looper looper)35     public JavaSinkHandler(JavaRecorder recorder, AudioSink sink, Looper looper) {
36         super(looper);
37         mRecorder = recorder;
38         mSink = sink;
39     }
40 
41     /**
42      * Recording Event IDs.
43      */
44     /** Sent when recording has started */
45     public static final int MSG_START = 0;
46     /** Sent when a recording buffer has been filled */
47     public static final int MSG_BUFFER_FILL = 1;
48     /** Sent when recording has been stopped */
49     public static final int MSG_STOP = 2;
50 
51     @Override
handleMessage(Message msg)52     public void handleMessage (Message msg) {
53         switch (msg.what) {
54             case MSG_START:
55                 if (mSink != null) {
56                     mSink.start();
57                 }
58                 break;
59 
60             case MSG_BUFFER_FILL:
61                 if (mSink != null) {
62                     mSink.push(mRecorder.getDataBuffer(),
63                             mRecorder.getNumBufferFrames(), mRecorder.getChannelCount());
64                 }
65                 break;
66 
67             case MSG_STOP:
68                 if (mSink != null) {
69                     // arg1 has the number of samples from the last read.
70                     mSink.stop(msg.arg1);
71                 }
72                 break;
73         }
74     }
75 }
76