AudioGroup.java revision b8790323473bef75a27d2da6fde2497b3bfe19eb
1/*
2 * Copyright (C) 2010 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
17package android.net.rtp;
18
19import java.util.HashMap;
20import java.util.Map;
21
22/**
23 */
24/** @hide */
25public class AudioGroup {
26    public static final int MODE_ON_HOLD = 0;
27    public static final int MODE_MUTED = 1;
28    public static final int MODE_NORMAL = 2;
29    public static final int MODE_EC_ENABLED = 3;
30
31    private final Map<AudioStream, Integer> mStreams;
32    private int mMode = MODE_ON_HOLD;
33
34    private int mNative;
35    static {
36        System.loadLibrary("rtp_jni");
37    }
38
39    public AudioGroup() {
40        mStreams = new HashMap<AudioStream, Integer>();
41    }
42
43    public int getMode() {
44        return mMode;
45    }
46
47    public synchronized native void setMode(int mode);
48
49    synchronized void add(AudioStream stream, AudioCodec codec, int codecType, int dtmfType) {
50        if (!mStreams.containsKey(stream)) {
51            try {
52                int socket = stream.dup();
53                add(stream.getMode(), socket,
54                        stream.getRemoteAddress().getHostAddress(), stream.getRemotePort(),
55                        codec.name, codec.sampleRate, codec.sampleCount, codecType, dtmfType);
56                mStreams.put(stream, socket);
57            } catch (NullPointerException e) {
58                throw new IllegalStateException(e);
59            }
60        }
61    }
62
63    private native void add(int mode, int socket, String remoteAddress, int remotePort,
64            String codecName, int sampleRate, int sampleCount, int codecType, int dtmfType);
65
66    synchronized void remove(AudioStream stream) {
67        Integer socket = mStreams.remove(stream);
68        if (socket != null) {
69            remove(socket);
70        }
71    }
72
73    private native void remove(int socket);
74
75    /**
76     * Sends a DTMF digit to every {@link AudioStream} in this group. Currently
77     * only event {@code 0} to {@code 15} are supported.
78     *
79     * @throws IllegalArgumentException if the event is invalid.
80     */
81    public native synchronized void sendDtmf(int event);
82
83    public synchronized void reset() {
84        remove(-1);
85    }
86
87    @Override
88    protected void finalize() throws Throwable {
89        reset();
90        super.finalize();
91    }
92}
93