12

I am trying to implemented the VoIP application using the AudioGroup and AudioStream classes of the android.net.rtp package. But my application not function properly. After "Join" the "AudioGroup" class object with the "AudioStream" object, its send udp packets successfully. I checked that using the packet analyzer. But voice is not hear from the phone. I run my application in 2 phones and try communicate voice between them.

In below I mention my source code.

public class MainActivity extends Activity {
private AudioStream audioStream;
private AudioGroup audioGroup;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
   audioGroup = new AudioGroup();
   audioGroup.setMode(AudioGroup.MODE_NORMAL);        
   audioStream = new AudioStream(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)4 }));
   audioStream.setCodec(AudioCodec.PCMU);
   audioStream.setMode(RtpStream.MODE_NORMAL);
   audioStream.associate(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)2 }), 5004);
   audioStream.join(audioGroup);
   AudioManager Audio =  (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
   Audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
} 
catch (SocketException e) { e.printStackTrace();} 
catch (UnknownHostException e) { e.printStackTrace();} 
catch (Exception ex) { ex.printStackTrace();}
}

I set this permissions in the Manifestfile.

<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.sip.voip" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />

I am using the Samsung GALAXY S3 phone with Android 4.0 OS

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
user1576358
  • 179
  • 1
  • 3
  • 5

3 Answers3

6

The trick is to get the port mapping correct. You need to use the port number from audioStream.getLocalPort() and send this port number to the peer in the SDP packet as SIP signalling.

Check out this example application which implements sip functionality https://github.com/Mobicents/restcomm-android-sdk/tree/master/Examples/JAIN%20SIP

Arslan Mehboob
  • 1,012
  • 1
  • 9
  • 21
5

I used the same code you submitted, and got it working with minor changes. Basically i found the problem was getting the port number correct.

When creating the audioStream the port number seems to be random. At Android developer I found: Note that the local port is assigned automatically to conform with RFC 3550.

What I did was I started the application on one phone first and used audioStream.getLocalPort() to find the port number. Then I connected to this port using the other one. This resulted in two-way communication, even if i only had the correct port number on one phone.

Hope this helps.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
Nils Erik
  • 176
  • 1
  • 3
  • Thank you very much for your response. Your answer is great. But I want to set the RTP local port no manually. How I modify the "AudioStream" class to set the RTP local port from the code. Thanks in advanced. – user1576358 Dec 14 '12 at 04:37
  • Sorry, but I have no idea. I wanted to do this myself, but I have not succeeded. It seems like it is not possible - that it must be randomly generated at start up. However, if you use for example SIP you can send the port number and other information (eg. Audiocodec) prior to association. – Nils Erik Dec 22 '12 at 13:40
  • I successfully managed to get devices to stream between themselves using this plus my own signalling protocol, to exchange the parameters of the stream between the clients (port numbers and codecs etc.) – Nova Entropy Oct 25 '13 at 14:31
  • @tristan2468 can you explain how did you do that ? – Arash Khoeini Apr 08 '15 at 11:42
  • I used a simple java `ServerSocket` to listen on a known port number and then created my own protocol where the devices exchange the port numbers of the RTP streams from Android so that they know where to connect to. – Nova Entropy Apr 08 '15 at 12:10
1

I think you should set the speaker on!

Maybe you can use the following method:

audioManager.setSpeakerphoneOn(true);
Steve P.
  • 14,489
  • 8
  • 42
  • 72
jane
  • 81
  • 3