/*
* Copyright (C) 2009 The Sipdroid Open Source Project
* Copyright (C) 2005 Luca Veltri - University of Parma - Italy
*
* This file is part of Sipdroid (http://www.sipdroid.org)
*
* Sipdroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/import java.io.IOException;import java.net.DatagramPacket;import java.net.InetAddress;/**
* RtpSocket implements a RTP socket for receiving and sending RTP packets.
* <p>
* RtpSocket is associated to a DatagramSocket that is used to send and/or
* receive RtpPackets.
*/publicclassRtpSocket{/**
* UDP socket
*/
SipdroidSocket socket;
DatagramPacket datagram;/**
* Remote address
*/
InetAddress r_addr;/**
* Remote port
*/int r_port;/**
* Creates a new RTP socket (only receiver)
*/publicRtpSocket(SipdroidSocket datagram_socket){
socket = datagram_socket;
r_addr = null;
r_port =0;
datagram =newDatagramPacket(newbyte[1],1);}/**
* Creates a new RTP socket (sender and receiver)
*/publicRtpSocket(SipdroidSocket datagram_socket,
InetAddress remote_address,int remote_port){
socket = datagram_socket;
r_addr = remote_address;
r_port = remote_port;
datagram =newDatagramPacket(newbyte[1],1);}/**
* Returns the RTP SipdroidSocket
*/public SipdroidSocket getDatagramSocket(){return socket;}/**
* Receives a RTP packet from this socket
*/publicvoidreceive(RtpPacket rtpp)throws IOException {
datagram.setData(rtpp.packet);
datagram.setLength(rtpp.packet.length);
socket.receive(datagram);if(!socket.isConnected())
socket.connect(datagram.getAddress(), datagram.getPort());
rtpp.packet_len = datagram.getLength();}/**
* Sends a RTP packet from this socket
*/publicvoidsend(RtpPacket rtpp)throws IOException {
datagram.setData(rtpp.packet);
datagram.setLength(rtpp.packet_len);
datagram.setAddress(r_addr);
datagram.setPort(r_port);
socket.send(datagram);}/**
* Closes this socket
*/publicvoidclose(){// socket.close();
socket.close();}}
rtp发送包
/**
* RtpPacket implements a RTP packet.
*/publicclassRtpPacket{/* RTP packet buffer containing both the RTP header and payload */byte[] packet;/* RTP packet length */int packet_len;/* RTP header length */// int header_len;/** Gets the RTP packet */publicbyte[]getPacket(){return packet;}/** Gets the RTP packet length */publicintgetLength(){return packet_len;}/** Gets the RTP header length */publicintgetHeaderLength(){if(packet_len >=12)return12+4*getCscrCount();elsereturn packet_len;// broken packet}/** Gets the RTP header length */publicintgetPayloadLength(){if(packet_len >=12)return packet_len -getHeaderLength();elsereturn0;// broken packet}/** Sets the RTP payload length */publicvoidsetPayloadLength(int len){
packet_len =getHeaderLength()+ len;}// version (V): 2 bits// padding (P): 1 bit// extension (X): 1 bit// CSRC count (CC): 4 bits// marker (M): 1 bit// payload type (PT): 7 bits// sequence number: 16 bits// timestamp: 32 bits// SSRC: 32 bits// CSRC list: 0 to 15 items, 32 bits each/** Gets the version (V) */publicintgetVersion(){if(packet_len >=12)return(packet[0]>>6&0x03);elsereturn0;// broken packet}/** Sets the version (V) */publicvoidsetVersion(int v){if(packet_len >=12)
packet[0]=(byte)((packet[0]&0x3F)|((v &0x03)<<6));}/** Whether has padding (P) */publicbooleanhasPadding(){if(packet_len >=12)returngetBit(packet[0],5);elsereturnfalse;// broken packet}/** Set padding (P) */publicvoidsetPadding(boolean p){if(packet_len >=12)
packet[0]=setBit(p, packet[0],5);}/** Whether has extension (X) */publicbooleanhasExtension(){if(packet_len >=12)returngetBit(packet[0],4);elsereturnfalse;// broken packet}/** Set extension (X) */publicvoidsetExtension(boolean x){if(packet_len >=12)
packet[0]=setBit(x, packet[0],4);}/** Gets the CSCR count (CC) */publicintgetCscrCount(){if(packet_len >=12)return(packet[0]&0x0F);elsereturn0;// broken packet}/** Whether has marker (M) */publicbooleanhasMarker(){if(packet_len >=12)returngetBit(packet[1],7);elsereturnfalse;// broken packet}/** Set marker (M) */publicvoidsetMarker(boolean m){if(packet_len >=12)
packet[1]=setBit(m, packet[1],7);}/** Gets the payload type (PT) */publicintgetPayloadType(){if(packet_len >=12)return(packet[1]&0x7F);elsereturn-1;// broken packet}/** Sets the payload type (PT) */publicvoidsetPayloadType(int pt){if(packet_len >=12)
packet[1]=(byte)((packet[1]&0x80)|(pt &0x7F));}/** Gets the sequence number */publicintgetSequenceNumber(){if(packet_len >=12)returngetInt(packet,2,4);elsereturn0;// broken packet}/** Sets the sequence number */publicvoidsetSequenceNumber(int sn){if(packet_len >=12){setInt(sn, packet,2,4);}}/** Gets the timestamp */publiclonggetTimestamp(){if(packet_len >=12)returngetLong(packet,4,8);elsereturn0;// broken packet}/** Sets the timestamp */publicvoidsetTimestamp(long timestamp){if(packet_len >=12)setLong(timestamp, packet,4,8);}/** Gets the SSCR */publiclonggetSscr(){if(packet_len >=12)returngetLong(packet,8,12);elsereturn0;// broken packet}/** Sets the SSCR */publicvoidsetSscr(long ssrc){if(packet_len >=12)setLong(ssrc, packet,8,12);}/** Gets the CSCR list */publiclong[]getCscrList(){int cc =getCscrCount();long[] cscr =newlong[cc];for(int i =0; i < cc; i++)
cscr[i]=getLong(packet,12+4* i,16+4* i);return cscr;}/** Sets the CSCR list */publicvoidsetCscrList(long[] cscr){if(packet_len >=12){int cc = cscr.length;if(cc >15)
cc =15;
packet[0]=(byte)(((packet[0]>>4)<<4)+ cc);
cscr =newlong[cc];for(int i =0; i < cc; i++)setLong(cscr[i], packet,12+4* i,16+4* i);// header_len=12+4*cc;}}/** Sets the payload */publicvoidsetPayload(byte[] payload,int len){if(packet_len >=12){int header_len =getHeaderLength();for(int i =0; i < len; i++)
packet[header_len + i]= payload[i];
packet_len = header_len + len;}}/** Gets the payload */publicbyte[]getPayload(){int header_len =getHeaderLength();int len = packet_len - header_len;byte[] payload =newbyte[len];for(int i =0; i < len; i++)
payload[i]= packet[header_len + i];return payload;}/** Creates a new RTP packet */publicRtpPacket(byte[] buffer,int packet_length){
packet = buffer;
packet_len = packet_length;if(packet_len <12)
packet_len =12;init(0x0F);}/** init the RTP packet header (only PT) */publicvoidinit(int ptype){init(ptype, Random.nextLong());}/** init the RTP packet header (PT and SSCR) */publicvoidinit(int ptype,long sscr){init(ptype, Random.nextInt(), Random.nextLong(), sscr);}/** init the RTP packet header (PT, SQN, TimeStamp, SSCR) */publicvoidinit(int ptype,int seqn,long timestamp,long sscr){setVersion(2);setPayloadType(ptype);setSequenceNumber(seqn);setTimestamp(timestamp);setSscr(sscr);}// *********************** Private and Static ***********************/** Gets int value *//*private static int getInt(byte b) {
return ((int) b + 256) % 256;
}*//** Gets long value */privatestaticlonggetLong(byte[] data,int begin,int end){long n =0;for(; begin < end; begin++){
n <<=8;
n += data[begin]&0xFF;}return n;}/** Sets long value */privatestaticvoidsetLong(long n,byte[] data,int begin,int end){for(end--; end >= begin; end--){
data[end]=(byte)(n %256);
n >>=8;}}/** Gets Int value */privatestaticintgetInt(byte[] data,int begin,int end){return(int)getLong(data, begin, end);}/** Sets Int value */privatestaticvoidsetInt(int n,byte[] data,int begin,int end){setLong(n, data, begin, end);}/** Gets bit value */privatestaticbooleangetBit(byte b,int bit){//return (b >> bit) == 1;return(b>>bit &0x01)==1;//hzq}/** Sets bit value */privatestaticbytesetBit(boolean value,byte b,int bit){if(value)return(byte)(b |(1<< bit));elsereturn(byte)((b |(1<< bit))^(1<< bit));}}
rtp套接字/* * Copyright (C) 2009 The Sipdroid Open Source Project * Copyright (C) 2005 Luca Veltri - University of Parma - Italy * * This file is part of Sipdroid (http://www.sipdroid.org) * * Sipdroid is free software; you can redistribute it and/or