You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SoundSenderDemo.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* This file is based on
  2. * http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml
  3. * Please see the site for license information.
  4. */
  5. package model;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import javax.sound.sampled.AudioFormat;
  9. import javax.sound.sampled.AudioInputStream;
  10. import javax.sound.sampled.AudioSystem;
  11. import javax.sound.sampled.DataLine;
  12. import javax.sound.sampled.FloatControl;
  13. import javax.sound.sampled.LineUnavailableException;
  14. import javax.sound.sampled.SourceDataLine;
  15. import javax.sound.sampled.UnsupportedAudioFileException;
  16. import java.lang.String;
  17. import static java.lang.Thread.sleep;
  18. import java.net.DatagramSocket;
  19. import java.util.Enumeration;
  20. import java.net.InetAddress;
  21. import java.net.InetSocketAddress;
  22. import java.net.SocketException;
  23. import java.net.UnknownHostException;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import jlibrtp.*;
  27. /**
  28. * @author Arne Kepp
  29. */
  30. public class SoundSenderDemo implements RTPAppIntf {
  31. SIPmodel model;
  32. public RTPSession rtpSession = null;
  33. static int pktCount = 0;
  34. static int dataCount = 0;
  35. private String filename;
  36. private final int EXTERNAL_BUFFER_SIZE = 1024;
  37. SourceDataLine auline;
  38. private Position curPosition;
  39. boolean local;
  40. enum Position {
  41. LEFT, RIGHT, NORMAL
  42. };
  43. public SoundSenderDemo() {}
  44. public SoundSenderDemo(boolean isLocal) throws UnknownHostException, InterruptedException {
  45. this.model = model;
  46. DatagramSocket rtpSocket = null;
  47. DatagramSocket rtcpSocket = null;
  48. //rtpSocket.close();
  49. //rtcpSocket.close();
  50. // try {
  51. // rtpSocket = new DatagramSocket(model.getProxyRTPPort());
  52. // } catch (SocketException ex) {
  53. // Logger.getLogger(SoundSenderDemo.class.getName()).log(Level.SEVERE, null, ex);
  54. // System.out.println("RTPSession failed to obtain port");
  55. // }
  56. // try {
  57. // rtcpSocket = new DatagramSocket(model.getProxyRTPPort()+1);
  58. // } catch (SocketException ex) {
  59. // Logger.getLogger(SoundSenderDemo.class.getName()).log(Level.SEVERE, null, ex);
  60. // System.out.println("RTCPSession failed to obtain port");
  61. // }
  62. InetAddress myIP = InetAddress.getByName("192.168.100.247");
  63. try {
  64. rtpSocket = new DatagramSocket(5004, myIP);
  65. rtcpSocket = new DatagramSocket(5005, myIP);
  66. } catch (SocketException e) {
  67. System.out.println(e);
  68. System.out.println("RTPSession failed to obtain port");
  69. rtpSocket.close();
  70. rtcpSocket.close();
  71. }
  72. /*
  73. try {
  74. rtpSocket = new DatagramSocket(null);
  75. InetSocketAddress myInetSocketAddress = new InetSocketAddress(5004);
  76. rtpSocket.bind(myInetSocketAddress);
  77. sleep(2000);
  78. } catch (SocketException e) {
  79. System.out.println("RTPSession failed to obtain port");
  80. rtpSocket.close();
  81. rtcpSocket.close();
  82. }
  83. */
  84. rtpSession = new RTPSession(rtpSocket, rtcpSocket);
  85. rtpSession.RTPSessionRegister(this, null, null);
  86. System.out.println("CNAME: " + rtpSession.CNAME());
  87. this.local = isLocal;
  88. }
  89. /**
  90. * @param args
  91. */
  92. public void SendDemo(String[] args) throws UnknownHostException, InterruptedException {
  93. args = new String[4];
  94. args[1] = "192.168.100.11";
  95. args[0] = "C:\\Users\\Tim\\Desktop\\VoIP PA\\project\\VoIPProjekt\\src\\SoundDateien\\RingingPhone.wav";
  96. args[2] = "5004";
  97. args[3] = "5005";
  98. for (int i = 0; i < args.length; i++) {
  99. System.out.println("args[" + i + "]" + args[i]);
  100. }
  101. // if (args.length == 0) {
  102. // args = new String[4];
  103. // args[1] = "192.168.100.249";
  104. // args[0] = "C:\\Users\\Tim\\Desktop\\VoIP PA\\project\\VoIPProjekt\\src\\SoundDateien\\RingingPhone.wav";
  105. // args[2] = "5004";
  106. // args[3] = "5005";
  107. // }
  108. SoundSenderDemo aDemo;
  109. aDemo = new SoundSenderDemo(true);
  110. Participant p = new Participant("192.168.100.11", Integer.parseInt(args[2]), Integer.parseInt(args[2]) + 1);
  111. aDemo.rtpSession.addParticipant(p);
  112. aDemo.filename = args[0];
  113. aDemo.run();
  114. System.out.println("pktCount: " + pktCount);
  115. }
  116. public void receiveData(DataFrame dummy1, Participant dummy2) {
  117. // We don't expect any data.
  118. }
  119. public void userEvent(int type, Participant[] participant) {
  120. //Do nothing
  121. }
  122. public int frameSize(int payloadType) {
  123. return 1;
  124. }
  125. public void run() {
  126. if (RTPSession.rtpDebugLevel > 1) {
  127. System.out.println("-> Run()");
  128. }
  129. File soundFile = new File(filename);
  130. if (!soundFile.exists()) {
  131. System.err.println("Wave file not found: " + filename);
  132. return;
  133. }
  134. AudioInputStream audioInputStream = null;
  135. try {
  136. audioInputStream = AudioSystem.getAudioInputStream(soundFile);
  137. } catch (UnsupportedAudioFileException e1) {
  138. e1.printStackTrace();
  139. return;
  140. } catch (IOException e1) {
  141. e1.printStackTrace();
  142. return;
  143. }
  144. //AudioFormat format = audioInputStream.getFormat();
  145. AudioFormat.Encoding encoding = new AudioFormat.Encoding("PCM_SIGNED");
  146. AudioFormat format = new AudioFormat(encoding, ((float) 8000.0), 16, 1, 2, ((float) 8000.0), false);
  147. System.out.println(format.toString());
  148. if (!this.local) {
  149. // To time the output correctly, we also play at the input:
  150. auline = null;
  151. DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  152. try {
  153. auline = (SourceDataLine) AudioSystem.getLine(info);
  154. auline.open(format);
  155. } catch (LineUnavailableException e) {
  156. e.printStackTrace();
  157. return;
  158. } catch (Exception e) {
  159. e.printStackTrace();
  160. return;
  161. }
  162. if (auline.isControlSupported(FloatControl.Type.PAN)) {
  163. FloatControl pan = (FloatControl) auline
  164. .getControl(FloatControl.Type.PAN);
  165. if (this.curPosition == Position.RIGHT) {
  166. pan.setValue(1.0f);
  167. } else if (this.curPosition == Position.LEFT) {
  168. pan.setValue(-1.0f);
  169. }
  170. }
  171. auline.start();
  172. }
  173. int nBytesRead = 0;
  174. byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
  175. long start = System.currentTimeMillis();
  176. try {
  177. while (nBytesRead != -1 && pktCount < 200) {
  178. nBytesRead = audioInputStream.read(abData, 0, abData.length);
  179. if (nBytesRead >= 0) {
  180. rtpSession.sendData(abData);
  181. //if(!this.local) {
  182. //auline.write(abData, 0, abData.length);
  183. //dataCount += abData.length;
  184. //if(pktCount % 10 == 0) {
  185. // System.out.println("pktCount:" + pktCount + " dataCount:" + dataCount);
  186. //
  187. // long test = 0;
  188. // for(int i=0; i<abData.length; i++) {
  189. // test += abData[i];
  190. // }
  191. // System.out.println(Long.toString(test));
  192. //}
  193. pktCount++;
  194. //if(pktCount == 100) {
  195. // System.out.println("Time!!!!!!!!! " + Long.toString(System.currentTimeMillis()));
  196. //}
  197. //System.out.println("yep");
  198. }
  199. if (pktCount == 100) {
  200. Enumeration<Participant> iter = this.rtpSession.getParticipants();
  201. System.out.println("iter " + iter.hasMoreElements());
  202. Participant p = null;
  203. //while (iter.hasMoreElements()) {
  204. p = iter.nextElement();
  205. String name = "name";
  206. byte[] nameBytes = name.getBytes();
  207. String data = "abcd";
  208. byte[] dataBytes = data.getBytes();
  209. int ret = rtpSession.sendRTCPAppPacket(p.getSSRC(), 0, nameBytes, dataBytes);
  210. System.out.println("!!!!!!!!!!!! ADDED APPLICATION SPECIFIC " + ret);
  211. continue;
  212. //}
  213. // if (p == null) {
  214. // System.out.println("No participant with SSRC available :(");
  215. // }
  216. }
  217. }
  218. } catch (IOException e) {
  219. e.printStackTrace();
  220. return;
  221. }
  222. System.out.println("Time: " + (System.currentTimeMillis() - start) / 1000 + " s");
  223. try {
  224. Thread.sleep(200);
  225. } catch (Exception e) {
  226. }
  227. this.rtpSession.endSession();
  228. try {
  229. Thread.sleep(2000);
  230. } catch (Exception e) {
  231. }
  232. if (RTPSession.rtpDebugLevel > 1) {
  233. System.out.println("<- Run()");
  234. }
  235. }
  236. }