import java.io.*; import java.net.Socket; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; public class JEAGIStreamer { // Process ID so that we can open up the audio stream. // Unfortunately, Java doesn't let us know this so we // have to wrap this app in a shell script that will pass it in int pid = 0; // EAGI Streams BufferedReader bin; // input stream from asterisk OutputStream out; // communication with asterisk OutputStream err; // communication with asterisk FileInputStream ais; // for the audio AudioInputStream fais; // for the audio from asterisk AudioInputStream tos; // for the mp3 output // Socket Streams for communicating with server (receiver of audio stream) //DataInputStream dsin; //DataOutputStream dsos; OutputStream sos; InputStream sis; OutputStream fsos; // Socket Connection String serverIP = "128.122.151.82"; int serverPort = 8001; String password = "xxxxxx"; String mountpoint = "/xxxxxx"; Socket thisSocket; boolean socketAlive = false; // Threads Thread sendingThread; Thread readingThread; // MPEG Encoding private static final AudioFormat.Encoding MPEG1L3 = new AudioFormat.Encoding("MPEG1L3"); private static final AudioFileFormat.Type MP3 = new AudioFileFormat.Type("MP3", "mp3"); public static void main(String[] args) { if (args.length > 2) { JEAGIStreamer jeagi = new JEAGIStreamer(Integer.parseInt(args[0]),args[1],Integer.parseInt(args[2])); } else if (args.length > 0) { JEAGIStreamer jeagi = new JEAGIStreamer(Integer.parseInt(args[0])); } else { System.out.println("Need to pass in the EAGI process id"); System.exit(-1); } } public JEAGIStreamer(int pid) { this(pid,null,-1); } public JEAGIStreamer(int pid, String serverIPAddress, int serverPort) { // EAGI Streams out = System.out; err = System.err; bin = new BufferedReader(new InputStreamReader(System.in)); if (serverIPAddress != null) { this.serverIP = serverIPAddress; } if (serverPort != -1) { this.serverPort = serverPort; } this.pid = pid; try { System.out.println("SAY NUMBER 1 \"*#\""); File audioFile = new File("/proc/" + pid + "/fd/3"); //File audioFile = new File("/home/sve204/asterisk_agi/useragi/sve204/agi_examples/newtest.wav"); ais = new FileInputStream(audioFile); //ais = AudioSystem.getAudioInputStream(audioFile); AudioFormat afis = new AudioFormat( 8000, 16, 1, true, false ); fais = new AudioInputStream(ais,afis,AudioSystem.NOT_SPECIFIED); //fais = new AudioInputStream(ais,afis,audioFile.length()); AudioFormat sourceFormat = fais.getFormat(); //System.out.println("Input format: " + sourceFormat); tos = AudioSystem.getAudioInputStream(MPEG1L3, fais); System.out.println("SAY NUMBER 2 \"*#\""); // Socket Connection thisSocket = new Socket(this.serverIP, this.serverPort); socketAlive = true; // Socket Streams sis = thisSocket.getInputStream(); sos = thisSocket.getOutputStream(); readingThread = new Thread(new ReadingThread()); readingThread.start(); String connection = "SOURCE " + password + " " + mountpoint + "\n" + "x-audiocast-name: redialing\nx-audiocast-url: http://itp.nyu.edu/~sve204/redial/\nx-audiocast-genre: none\nx-audiocast-bitrate: 8000\nx-audiocast-public 0\nx-audiocast-descripion: \n\n"; sos.write(connection.getBytes()); // For Testing File outFile = new File("/home/sve204/asterisk_agi/useragi/sve204/agi_examples/new_outtest.mp3"); fsos = new FileOutputStream(outFile); System.out.println("SAY NUMBER 3 \"*#\""); String writeOut = "WAIT FOR DIGIT 10000\n"; //out.println(writeOut); out.write(writeOut.getBytes()); // Thread sendingThread = new Thread(new SendingThread()); sendingThread.start(); sendingThread.setPriority(Thread.MAX_PRIORITY); } catch (Exception e) { System.out.println("SAY ALPHA ERROR \"*#\""); e.printStackTrace(); } } //long totalBytes = 0; class SendingThread implements Runnable { public void run() { try { while (socketAlive) { /*int availableBytes = tos.available(); byte[] inBytes = new byte[availableBytes]; int inByte = tos.read(inBytes); totalBytes += availableBytes;*/ long pretime = System.nanoTime();//System.currentTimeMillis(); int inByte = tos.read(); if (inByte == -1) { socketAlive = false; break; } //sos.write(inBytes); //totalBytes++; fsos.write(inByte); sos.write(inByte); fsos.flush(); sos.flush(); //System.out.print("."); long nowtime = System.nanoTime(); //currentTimeMillis(); long elapsedtime = nowtime - pretime; long delaytime = 100000 - elapsedtime; /*if (delaytime > 0) { try { Thread.sleep(0,(int)delaytime); } catch (Exception e) { e.printStackTrace(); } }*/ //System.out.print(" " + totalBytes); } thisSocket.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } //long totalBytes = 0; class ReadingThread implements Runnable { public void run() { try { char rin = 0; //StringBuffer buf = new StringBuffer(); while (socketAlive) { rin = (char) sis.read(); //buf.append(rin); System.out.print(rin); } } catch (IOException ioe) { ioe.printStackTrace(); } } } /* public boolean output(String str) { try { ps.writeBytes(str); ps.flush(); return true; } catch (Exception e) { return false; } } public boolean output(byte bb) { try { ps.write(bb); ps.flush(); return true; } catch (Exception e) { return false; } } public boolean output(byte bytes[], int offset, int length) { try { ps.write(bytes, offset, length); ps.flush(); return true; } catch (Exception e) { return false; } } */ }