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.

SIPSessionDescription.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package model;
  7. import java.util.ArrayList;
  8. import java.util.Vector;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.sdp.SdpFactory;
  12. import javax.sdp.SessionDescription;
  13. import javax.sip.RequestEvent;
  14. import logger.OhmLogger;
  15. /**
  16. *
  17. * @author Jan
  18. */
  19. public class SIPSessionDescription
  20. {
  21. private SdpFactory sdpFactory;
  22. private SessionDescription sessionDescription;
  23. private Vector mediavec;
  24. private String cods[];
  25. private String[][] codecs;
  26. private static final Logger lgSessionDescription = OhmLogger.getLogger();
  27. public SIPSessionDescription(String myName, String myIPAddress,
  28. RequestEvent requestEvent, Boolean anfrage)
  29. {
  30. codecs = new String[4][2]; // Alle unterstützen Codecs eintragen
  31. codecs[0][0] = "0";
  32. codecs[0][1] = "PCMU/8000";
  33. codecs[1][0] = "4";
  34. codecs[1][1] = "G723/8000";
  35. codecs[2][0] = "8";
  36. codecs[2][1] = "PCMA/8000";
  37. codecs[3][0] = "18";
  38. codecs[3][1] = "G729/8000";
  39. cods = new String[codecs.length];
  40. for (int i = 0; i < 4; i++)//Übertragung der RTP-Values in neues Array
  41. {
  42. cods[i] = codecs[i][0];
  43. }
  44. mediavec = new Vector();
  45. try
  46. {
  47. sdpFactory = SdpFactory.getInstance();
  48. sessionDescription = sdpFactory.createSessionDescription();
  49. sessionDescription.setOrigin(sdpFactory.createOrigin(myName, 8000, 8000, "IN", "IP4", myIPAddress));
  50. sessionDescription.setSessionName(sdpFactory.createSessionName("SIP Call"));
  51. sessionDescription.setConnection(sdpFactory.createConnection("IN", "IP4", myIPAddress));
  52. //Media Body
  53. if (anfrage == true)
  54. {
  55. mediavec.add(sdpFactory.createMediaDescription("audio", 6022, 1, "RTP/AVP", cods)); //(Übertragungstyp, Port, anzahl der Ports, Verbindungstyp,..)
  56. mediavec.add(sdpFactory.createAttribute("sendrecv", null));
  57. for (int i = 0; i == 4; i++)
  58. {
  59. System.out.println("Codecs: " + codecs[i][0] + " " + codecs[i][1]);
  60. mediavec.add(sdpFactory.createAttribute("rtpmap", codecs[i][0] + " " + codecs[i][1]));
  61. }
  62. }
  63. else//Vergleich von eigenen Codecs mit Codecs der anruft -> Rückgabe entsprechend wählen
  64. {
  65. String[] reqbody = (requestEvent.getRequest()).toString().split("\\s|" + System.getProperty("line.seperator"));
  66. ArrayList<String> reqSDP = new ArrayList<>();
  67. for (String req : reqbody)
  68. {
  69. if (req.startsWith("a=rtpmap"))
  70. {
  71. reqSDP.add(req.replace("a=rtpmap:", ""));
  72. }
  73. }
  74. boolean temp = true; //evtl noch was besseres da so erster Codec nicht bester verwendet wird
  75. int position = 0;
  76. for (String stcodec : cods)
  77. {
  78. System.out.println("------Auswahl des codecs----");
  79. if (reqSDP.contains(stcodec) && temp == true)
  80. {
  81. String tempcodec[]= new String [1];
  82. tempcodec[0] = codecs[position][1];
  83. mediavec.add(sdpFactory.createMediaDescription("audio", 6022, 1, "RTP/AVP", tempcodec)); //(Übertragungstyp, Port, anzahl der Ports, Verbindungstyp,..)
  84. mediavec.add(sdpFactory.createAttribute("sendrecv", null));
  85. mediavec.add(sdpFactory.createAttribute("rtpmap", codecs[position][0] + " " + codecs[position][1]));
  86. temp = false;
  87. }
  88. position++;
  89. }
  90. }
  91. sessionDescription.setAttributes(mediavec);
  92. }
  93. catch (Exception ex)
  94. {
  95. lgSessionDescription.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
  96. }
  97. }
  98. /**
  99. * @return the sessionDescription
  100. */
  101. public SessionDescription getSessionDescription()
  102. {
  103. return sessionDescription;
  104. }
  105. }