• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 package android.net.rtp.cts;
17 
18 import android.content.Context;
19 import android.media.AudioManager;
20 import android.net.rtp.AudioCodec;
21 import android.net.rtp.AudioGroup;
22 import android.net.rtp.AudioStream;
23 import android.net.rtp.RtpStream;
24 import android.test.AndroidTestCase;
25 import android.util.Log;
26 
27 import java.net.DatagramPacket;
28 import java.net.DatagramSocket;
29 import java.net.InetAddress;
30 
31 public class AudioGroupTest extends AndroidTestCase {
32 
33     private static final String TAG = AudioGroupTest.class.getSimpleName();
34 
35     private AudioManager mAudioManager;
36 
37     private AudioStream mStreamA;
38     private DatagramSocket mSocketA;
39     private AudioStream mStreamB;
40     private DatagramSocket mSocketB;
41     private AudioGroup mGroup;
42 
43     @Override
setUp()44     public void setUp() throws Exception {
45         mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
46         mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
47 
48         InetAddress local = InetAddress.getByName("::1");
49 
50         mStreamA = new AudioStream(local);
51         mStreamA.setMode(RtpStream.MODE_NORMAL);
52         mStreamA.setCodec(AudioCodec.PCMU);
53         mSocketA = new DatagramSocket();
54         mSocketA.connect(mStreamA.getLocalAddress(), mStreamA.getLocalPort());
55         mStreamA.associate(mSocketA.getLocalAddress(), mSocketA.getLocalPort());
56 
57         mStreamB = new AudioStream(local);
58         mStreamB.setMode(RtpStream.MODE_NORMAL);
59         mStreamB.setCodec(AudioCodec.PCMU);
60         mSocketB = new DatagramSocket();
61         mSocketB.connect(mStreamB.getLocalAddress(), mStreamB.getLocalPort());
62         mStreamB.associate(mSocketB.getLocalAddress(), mSocketB.getLocalPort());
63 
64         mGroup = new AudioGroup();
65     }
66 
67     @Override
tearDown()68     public void tearDown() throws Exception {
69         mGroup.clear();
70         mStreamA.release();
71         mSocketA.close();
72         mStreamB.release();
73         mSocketB.close();
74         mAudioManager.setMode(AudioManager.MODE_NORMAL);
75     }
76 
assertPacket(DatagramSocket socket, int length)77     private void assertPacket(DatagramSocket socket, int length) throws Exception {
78         DatagramPacket packet = new DatagramPacket(new byte[length + 1], length + 1);
79         socket.setSoTimeout(3000);
80         socket.receive(packet);
81         assertEquals(packet.getLength(), length);
82     }
83 
drain(DatagramSocket socket)84     private void drain(DatagramSocket socket) throws Exception {
85         DatagramPacket packet = new DatagramPacket(new byte[1], 1);
86         socket.setSoTimeout(1);
87         try {
88             // Drain the socket by retrieving all the packets queued on it.
89             // A SocketTimeoutException will be thrown when it becomes empty.
90             while (true) {
91                 socket.receive(packet);
92             }
93         } catch (Exception e) {
94             // ignore.
95         }
96     }
97 
testTraffic()98     public void testTraffic() throws Exception {
99         mStreamA.join(mGroup);
100         assertPacket(mSocketA, 12 + 160);
101 
102         mStreamB.join(mGroup);
103         assertPacket(mSocketB, 12 + 160);
104 
105         mStreamA.join(null);
106         drain(mSocketA);
107 
108         drain(mSocketB);
109         assertPacket(mSocketB, 12 + 160);
110 
111         mStreamA.join(mGroup);
112         assertPacket(mSocketA, 12 + 160);
113     }
114 
testSetMode()115     public void testSetMode() throws Exception {
116         mGroup.setMode(AudioGroup.MODE_NORMAL);
117         assertEquals(mGroup.getMode(), AudioGroup.MODE_NORMAL);
118 
119         mGroup.setMode(AudioGroup.MODE_MUTED);
120         assertEquals(mGroup.getMode(), AudioGroup.MODE_MUTED);
121 
122         mStreamA.join(mGroup);
123         mStreamB.join(mGroup);
124 
125         mGroup.setMode(AudioGroup.MODE_NORMAL);
126         assertEquals(mGroup.getMode(), AudioGroup.MODE_NORMAL);
127 
128         mGroup.setMode(AudioGroup.MODE_MUTED);
129         assertEquals(mGroup.getMode(), AudioGroup.MODE_MUTED);
130     }
131 
testAdd()132     public void testAdd() throws Exception {
133         mStreamA.join(mGroup);
134         assertEquals(mGroup.getStreams().length, 1);
135 
136         mStreamB.join(mGroup);
137         assertEquals(mGroup.getStreams().length, 2);
138 
139         mStreamA.join(mGroup);
140         assertEquals(mGroup.getStreams().length, 2);
141     }
142 
testRemove()143     public void testRemove() throws Exception {
144         mStreamA.join(mGroup);
145         assertEquals(mGroup.getStreams().length, 1);
146 
147         mStreamA.join(null);
148         assertEquals(mGroup.getStreams().length, 0);
149 
150         mStreamA.join(mGroup);
151         assertEquals(mGroup.getStreams().length, 1);
152     }
153 
testClear()154     public void testClear() throws Exception {
155         mStreamA.join(mGroup);
156         mStreamB.join(mGroup);
157         mGroup.clear();
158 
159         assertEquals(mGroup.getStreams().length, 0);
160         assertFalse(mStreamA.isBusy());
161         assertFalse(mStreamB.isBusy());
162     }
163 
testDoubleClear()164     public void testDoubleClear() throws Exception {
165         mStreamA.join(mGroup);
166         mStreamB.join(mGroup);
167         mGroup.clear();
168         mGroup.clear();
169     }
170 }
171