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.

RTPSendSingleString.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Java RTP Library (jlibrtp)
  3. * Copyright (C) 2006 Arne Kepp
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. package model;
  20. import java.net.DatagramSocket;
  21. import jlibrtp.*;
  22. /**
  23. * <p>
  24. * This is an example of how to set up a Unicast session.</p>
  25. * <p>
  26. * It does not accept any input arguments and is therefore of limited practical
  27. * value, but it shows the basics.</p>
  28. *
  29. * <p>
  30. * The class has to implement RTPAppIntf.</p>
  31. *
  32. * @author Arne Kepp
  33. */
  34. public class RTPSendSingleString implements RTPAppIntf {
  35. /**
  36. * Holds a RTPSession instance
  37. */
  38. RTPSession rtpSession = null;
  39. public RTPSendSingleString() {
  40. }
  41. /**
  42. * A minimal constructor
  43. */
  44. public RTPSendSingleString(RTPSession rtpSession) {
  45. this.rtpSession = rtpSession;
  46. }
  47. // RTPAppIntf All of the following are documented in the JavaDocs
  48. /**
  49. * Used to receive data from the RTP Library. We expect no data
  50. */
  51. public void receiveData(DataFrame frame, Participant p) {
  52. /**
  53. * This concatenates all received packets for a single timestamp into a
  54. * single byte[]
  55. */
  56. byte[] data = frame.getConcatenatedData();
  57. /**
  58. * This returns the CNAME, if any, associated with the SSRC that was
  59. * provided in the RTP packets received.
  60. */
  61. String cname = p.getCNAME();
  62. System.out.println("Received data from " + cname);
  63. System.out.println(new String(data));
  64. }
  65. /**
  66. * Used to communicate updates to the user database through RTCP
  67. */
  68. public void userEvent(int type, Participant[] participant) {
  69. //Do nothing
  70. }
  71. /**
  72. * How many packets make up a complete frame for the payload type?
  73. */
  74. public int frameSize(int payloadType) {
  75. return 1;
  76. }
  77. // RTPAppIntf/
  78. public static void main(String[] args) {
  79. // 1. Create sockets for the RTPSession
  80. DatagramSocket rtpSocket = null;
  81. DatagramSocket rtcpSocket = null;
  82. try {
  83. rtpSocket = new DatagramSocket(5004);
  84. rtcpSocket = new DatagramSocket(5005);
  85. } catch (Exception e) {
  86. System.out.println(e);
  87. System.out.println("RTPSession failed to obtain port");
  88. }
  89. // 2. Create the RTP session
  90. RTPSession rtpSession = new RTPSession(rtpSocket, rtcpSocket);
  91. // 3. Instantiate the application object
  92. RTPSendSingleString uex = new RTPSendSingleString(rtpSession);
  93. // 4. Add participants we want to notify upon registration
  94. // a. Hopefully nobody is listening on this port.
  95. Participant part = new Participant("192.168.100.11", 5004, 5005);
  96. rtpSession.addParticipant(part);
  97. // 5. Register the callback interface, this launches RTCP threads too
  98. // The two null parameters are for the RTCP and debug interfaces, not use here
  99. rtpSession.RTPSessionRegister(uex, null, null);
  100. // Wait 2500 ms, because of the initial RTCP wait
  101. try {
  102. Thread.sleep(2000);
  103. } catch (Exception e) {
  104. System.out.println(e);
  105. }
  106. // Note: The wait is optional, but insures SDES packets
  107. // receive participants before continuing
  108. // 6. Send some data
  109. String str = "Hi there!";
  110. rtpSession.sendData(str.getBytes());
  111. // 7. Terminate the session, takes a few ms to kill threads in order.
  112. rtpSession.endSession();
  113. //This may result in "Sleep interrupted" messages, ignore them
  114. }
  115. }