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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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[]; //Hilfsarray für [RTP-Notation]
  25. private String[][] codecs; //[RTP-Notation][Codec/SampelRate]
  26. private static final Logger lgSessionDescription = OhmLogger.getLogger();
  27. public SIPSessionDescription(String myName, String myIPAddress,
  28. RequestEvent requestEvent, Boolean anfrage)
  29. {
  30. //Hier alle unterstützen Codecs eintragen [RTP-Notation][Codec/SampelRate]
  31. codecs = new String[4][2];
  32. codecs[0][0] = "0";
  33. codecs[0][1] = "PCMU/8000";
  34. codecs[1][0] = "4";
  35. codecs[1][1] = "G723/8000";
  36. codecs[2][0] = "8";
  37. codecs[2][1] = "PCMA/8000";
  38. codecs[3][0] = "18";
  39. codecs[3][1] = "G729/8000";
  40. cods = new String[codecs.length];
  41. for (int i = 0; i < 4; i++)//Übertragung der RTP-Values in neues Array
  42. {
  43. cods[i] = codecs[i][0];
  44. }
  45. mediavec = new Vector();
  46. try
  47. {
  48. sdpFactory = SdpFactory.getInstance();
  49. sessionDescription = sdpFactory.createSessionDescription();
  50. sessionDescription.setOrigin(sdpFactory.createOrigin(myName, 8000, 8000, "IN", "IP4", myIPAddress));
  51. sessionDescription.setSessionName(sdpFactory.createSessionName("SIP Call"));
  52. sessionDescription.setConnection(sdpFactory.createConnection("IN", "IP4", myIPAddress));
  53. //Media Body
  54. if (anfrage == true)
  55. {
  56. mediavec.add(sdpFactory.createMediaDescription("audio", 6022, 1, "RTP/AVP", cods)); //(Übertragungstyp, Port, anzahl der Ports, Verbindungstyp,..)
  57. mediavec.add(sdpFactory.createAttribute("sendrecv", null));
  58. for (int i = 0; i == 4; i++)
  59. {
  60. System.out.println("Codecs: " + codecs[i][0] + " " + codecs[i][1]);
  61. mediavec.add(sdpFactory.createAttribute("rtpmap", codecs[i][0] + " " + codecs[i][1]));
  62. }
  63. }
  64. else//Vergleich von eigenen Codecs mit Codecs der anruft -> Rückgabe entsprechend wählen
  65. {
  66. String[] reqbody = (requestEvent.getRequest()).toString().split("\\s|" + System.getProperty("line.seperator"));
  67. ArrayList<String> reqSDP = new ArrayList<>();
  68. for (String req : reqbody)
  69. {
  70. if (req.startsWith("a=rtpmap"))
  71. {
  72. reqSDP.add(req.replace("a=rtpmap:", ""));
  73. }
  74. }
  75. boolean temp = true; //evtl noch was besseres da so erster Codec nicht bester verwendet wird
  76. int position = 0;
  77. for (String stcodec : cods)
  78. {
  79. System.out.println("------Auswahl des codecs----");
  80. if (reqSDP.contains(stcodec) && temp == true)
  81. {
  82. String tempcodec[]= new String [1];
  83. tempcodec[0] = codecs[position][1];
  84. mediavec.add(sdpFactory.createMediaDescription("audio", 6022, 1, "RTP/AVP", tempcodec)); //(Übertragungstyp, Port, anzahl der Ports, Verbindungstyp,..)
  85. mediavec.add(sdpFactory.createAttribute("sendrecv", null));
  86. mediavec.add(sdpFactory.createAttribute("rtpmap", codecs[position][0] + " " + codecs[position][1]));
  87. temp = false;
  88. }
  89. position++;
  90. }
  91. }
  92. sessionDescription.setAttributes(mediavec);
  93. lgSessionDescription.info("SDP Header erstellt");
  94. }
  95. catch (Exception ex)
  96. {
  97. lgSessionDescription.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
  98. }
  99. }
  100. /**
  101. * @return the sessionDescription
  102. */
  103. public SessionDescription getSessionDescription()
  104. {
  105. return sessionDescription;
  106. }
  107. }