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")
29     private static final String TAG = JavaSinkHandler.class.getSimpleName();
30     @SuppressWarnings("unused")
31     private static final boolean LOG = false;
32 
33     protected JavaRecorder mRecorder;
34 
35     private AudioSink mSink = null;
36 
JavaSinkHandler(JavaRecorder recorder, AudioSink sink, Looper looper)37     public JavaSinkHandler(JavaRecorder recorder, AudioSink sink, Looper looper) {
38         super(looper);
39         mRecorder = recorder;
40         mSink = sink;
41     }
42 
43     /**
44      * Recording Event IDs.
45      */
46     /** Sent when recording has started */
47     public static final int MSG_START = 0;
48     /** Sent when a recording buffer has been filled */
49     public static final int MSG_BUFFER_FILL = 1;
50     /** Sent when recording has been stopped */
51     public static final int MSG_STOP = 2;
52 
53     @Override
handleMessage(Message msg)54     public void handleMessage (Message msg) {
55         switch (msg.what) {
56             case MSG_START:
57                 if (mSink != null) {
58                     mSink.start();
59                 }
60                 break;
61 
62             case MSG_BUFFER_FILL:
63                 if (mSink != null) {
64                     mSink.push(mRecorder.getDataBuffer(),
65                             mRecorder.getNumExchangeFrames(), mRecorder.getChannelCount());
66                 }
67                 break;
68 
69             case MSG_STOP:
70                 if (mSink != null) {
71                     // arg1 has the number of samples from the last read.
72                     mSink.stop(msg.arg1);
73                 }
74                 break;
75         }
76     }
77 }
78