SHARE
TWEET

Java VOIP

a guest Apr 24th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Main;
  2.  
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6. import java.util.Base64;
  7.  
  8. import javax.sound.sampled.AudioFormat;
  9. import javax.sound.sampled.AudioSystem;
  10. import javax.sound.sampled.DataLine;
  11. import javax.sound.sampled.SourceDataLine;
  12. import javax.sound.sampled.TargetDataLine;
  13.  
  14. public class main {
  15.    
  16.     static DatagramSocket Client = null;
  17.     static String RHost = "127.0.0.1:2600";
  18.     public static void main(String[] args) {
  19.         // TODO Auto-generated method stub
  20.         try {
  21.             AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
  22.             Client = new DatagramSocket(2600);
  23.            
  24.             Thread t = new Thread(new Runnable() { public void run() {
  25.                 try {
  26.                     DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  27.                     TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
  28.                     targetLine.open();
  29.                     targetLine.start();
  30.                     byte[] targetData = new byte[targetLine.getBufferSize() / 5];
  31.                     while(!Client.isClosed()) {
  32.                         try {
  33.                             targetLine.read(targetData, 0, targetData.length);
  34.                             String A1 = "|"+Base64.getEncoder().encodeToString(targetData) + "|";
  35.                             DatagramPacket sendPacket = new DatagramPacket(A1.getBytes(),0, A1.getBytes().length, InetAddress.getByName(RHost.split(":")[0]), Integer.parseInt(RHost.split(":")[1]));
  36.                             Client.send(sendPacket);
  37.                         }catch(Exception e) {}
  38.                         }
  39.                 }catch(Exception e) {}
  40.             }});
  41.             t.start();
  42.            
  43.            
  44.              DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  45.              SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
  46.              sourceLine.open();
  47.              sourceLine.start();
  48.             while(true) {
  49.                  try {
  50.                      byte[] receiveData = new byte[204800];
  51.                        DatagramPacket receivePacket = new DatagramPacket(receiveData,0, receiveData.length);
  52.                        Client.receive(receivePacket);
  53.                        String RawData = new String((receivePacket.getData()));
  54.                        InetAddress IPAddress = receivePacket.getAddress();
  55.                        int port = receivePacket.getPort();
  56.                        String RHost = IPAddress.toString().replace("/", "") + ":" + port;
  57.                        byte[] d1=Base64.getDecoder().decode(RawData.split("\\|")[1]);
  58.                        sourceLine.write(d1,0,d1.length);
  59.                  }catch(Exception e) {}
  60.             }
  61.            
  62.         } catch (Exception e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.         }
  66.     }
  67.    
  68.    
  69.    
  70.  
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top