834
|
1 |
/*
|
|
2 |
* Copyright (C) 2009 The Sipdroid Open Source Project
|
|
3 |
* Copyright (C) 2005 Luca Veltri - University of Parma - Italy
|
|
4 |
*
|
|
5 |
* This file is part of Sipdroid (http://www.sipdroid.org)
|
|
6 |
*
|
|
7 |
* Sipdroid is free software; you can redistribute it and/or modify
|
|
8 |
* it under the terms of the GNU General Public License as published by
|
|
9 |
* the Free Software Foundation; either version 3 of the License, or
|
|
10 |
* (at your option) any later version.
|
|
11 |
*
|
|
12 |
* This source code is distributed in the hope that it will be useful,
|
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
* GNU General Public License for more details.
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License
|
|
18 |
* along with this source code; if not, write to the Free Software
|
|
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
*/
|
|
21 |
|
835
|
22 |
package org.sipdroid.net;
|
834
|
23 |
|
|
24 |
import java.net.InetAddress;
|
|
25 |
import java.net.DatagramPacket;
|
|
26 |
import java.io.IOException;
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
/**
|
|
31 |
* RtpSocket implements a RTP socket for receiving and sending RTP packets.
|
|
32 |
* <p>
|
|
33 |
* RtpSocket is associated to a DatagramSocket that is used to send and/or
|
|
34 |
* receive RtpPackets.
|
|
35 |
*/
|
|
36 |
public class RtpSocket {
|
835
|
37 |
/** UDP socket */
|
|
38 |
SipdroidSocket socket;
|
|
39 |
DatagramPacket datagram;
|
834
|
40 |
|
835
|
41 |
/** Remote address */
|
|
42 |
InetAddress r_addr;
|
|
43 |
|
|
44 |
/** Remote port */
|
|
45 |
int r_port;
|
834
|
46 |
|
835
|
47 |
/** Creates a new RTP socket (only receiver) */
|
|
48 |
public RtpSocket(SipdroidSocket datagram_socket) {
|
|
49 |
socket = datagram_socket;
|
|
50 |
r_addr = null;
|
|
51 |
r_port = 0;
|
|
52 |
datagram = new DatagramPacket(new byte[1],1);
|
|
53 |
}
|
834
|
54 |
|
835
|
55 |
/** Creates a new RTP socket (sender and receiver) */
|
|
56 |
public RtpSocket(SipdroidSocket datagram_socket,
|
|
57 |
InetAddress remote_address, int remote_port) {
|
|
58 |
socket = datagram_socket;
|
|
59 |
r_addr = remote_address;
|
|
60 |
r_port = remote_port;
|
|
61 |
datagram = new DatagramPacket(new byte[1],1);
|
|
62 |
}
|
834
|
63 |
|
835
|
64 |
/** Returns the RTP SipdroidSocket */
|
|
65 |
public SipdroidSocket getDatagramSocket() {
|
|
66 |
return socket;
|
|
67 |
}
|
834
|
68 |
|
835
|
69 |
/** Receives a RTP packet from this socket */
|
|
70 |
public void receive(RtpPacket rtpp) throws IOException {
|
|
71 |
datagram.setData(rtpp.packet);
|
|
72 |
datagram.setLength(rtpp.packet.length);
|
|
73 |
socket.receive(datagram);
|
|
74 |
if (!socket.isConnected())
|
|
75 |
socket.connect(datagram.getAddress(),datagram.getPort());
|
|
76 |
rtpp.packet_len = datagram.getLength();
|
|
77 |
}
|
834
|
78 |
|
835
|
79 |
/** Sends a RTP packet from this socket */
|
|
80 |
public void send(RtpPacket rtpp) throws IOException {
|
|
81 |
datagram.setData(rtpp.packet);
|
|
82 |
datagram.setLength(rtpp.packet_len);
|
|
83 |
datagram.setAddress(r_addr);
|
|
84 |
datagram.setPort(r_port);
|
|
85 |
socket.send(datagram);
|
|
86 |
}
|
834
|
87 |
|
835
|
88 |
/** Closes this socket */
|
|
89 |
public void close() { // socket.close();
|
|
90 |
}
|
834
|
91 |
|
|
92 |
}
|