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