title: VoIP RTP Streaming from/to server (in Java) to/from android
body: <p>My target is to have a push-to-talk chat app in GSM/UMTS/LTE networks; initially I wanted use multicast addresses and peer-to-peer without overload the server; unfortunatly, after deep investigation, I discovered that multicast is not allowed in GSM/UMTS/LTE networks, therefore I have to use the server in order to bounce the VoIP packets. I' don't like very much this solution because I have to overload the server, but I didn't find any better solution. If you have an alternative solution is very much apprieciated...</p>
  
  <p>Therefore I have to send VoIP from an android client to a server (PC), and viceversa. The server is in Java, it has to receive the VoIP packets and then to send the VoIP packets to other N clients; the server is a bouncer of VoIP packets. </p>
  
  <p>I developped the code, but it doesn't work; I don't have any error, simply I had very bad VoIP service: I loose a lot of pieces and what I hear is very much noised... Where is the error? I suppose It should be in the server code; the server simply get the packet and resend them, without know that they are VoIP on RTP.</p>
  
  <p>Please find below </p>
  
  <ul>
  <li>the code I use to send the VoIP packets to server. It works because I don't have problems when I use it for individual call sending the VoIP packets directly from Android to Android; the code to receive in android the packets from server is very similar, so I don't recopy it. As you can see I use android.net.rtp .</li>
  <li>the code  I use on the Java server to bounce the VoIP packets</li>
  </ul>
  
  <p>Thank you in advance, Fausto</p>
  
  <p>// ANDROID CODE USED TO SEND VOIP TO SERVER</p>
  
  <pre><code>//Attribute definition
  private static final AudioCodec myAudioCodec_COSTANTE = AudioCodec.PCMU ; 
  private static final int myAudioGroupTX_COSTANTE = AudioGroup.MODE_NORMAL ; 
  private static final int myAudioGroupRX_COSTANTE = AudioGroup.MODE_NORMAL ;
  private static final int myRtpStreamTX_COSTANTE = RtpStream.MODE_SEND_ONLY ;
  private static final int myRtpStreamRX_COSTANTE = RtpStream.MODE_RECEIVE_ONLY ; 
  private static final int myAudioManagerTX_COSTANTE = AudioManager.MODE_IN_COMMUNICATION;
  private static final int myAudioManagerRX_COSTANTE = AudioManager.MODE_IN_COMMUNICATION; 
  
  //Method called for VoIP trasmission
  myAudioStream = new AudioStream(localClientIP);
  myAudioGroup = new AudioGroup();
  myAudioManager =  (AudioManager) myContext.getSystemService(Context.AUDIO_SERVICE);
  
  myAudioGroup.setMode(myAudioGroupTX_COSTANTE);
  myAudioStream.join(null); 
  myAudioStream.setCodec(myAudioCodec_COSTANTE);
  myAudioStream.setMode(myRtpStreamTX_COSTANTE);
  myAudioStream.associate(ipaddress_Server, port_Server)    
  myAudioStream.join(myAudioGroup); 
  myAudioManager.setMode(myAudioManagerTX_COSTANTE);
  myAudioManager.setSpeakerphoneOn(false);
  myAudioManager.setMicrophoneMute(false);
  </code></pre>
  
  <p>//JAVA SERVER CODE USED TO RECEIVE VOIP FROM ANDROID AND TO RESEND IT TO SERVER</p>
  
  <pre><code>DatagramSocket datagramSocket_RX_VoIP= new DatagramSocket();
  DatagramSocket datagramSocket_TX_VoIP= new DatagramSocket();
  int unicast_port_TX_VoIP = 5000 ;
  String unicast_ip_TX_VoIP = "192.168.0.3";
  
      Thread t = new Thread(new Runnable() {
      public void run() {
      try {
          DatagramPacket myPacket;
          while (true) {
              myPacket = ManagePacket.initializePacket(); //Function to prepare the packe ; the problem is not here!!!
              datagramSocket_RX_VoIP.receive(myPacket);
  
              InetAddress ppp =  InetAddress.getByName(unicast_ip_TX_VoIP);
              myPacket.setAddress(ppp);
              myPacket.setPort( unicast_port_TX_VoIP ) ;
              datagramSocket_TX_VoIP.send(myPacket);
          }
  
      }  catch (Exception ex) {
          log.debug("Exception: " + ex.getMessage(), ex);
      }
      }
      });
      t.start();                                        
  </code></pre>