📄 sdpmessage.java
字号:
/* * This file was derived from libdissipate, an open source SIP library. The original file * was modified on 1/23/2001. Please see * http://www.div8.net/dissipate for more information. * * Copyright (c) 2000 Billy Biggs <bbiggs@div8.net> * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This library 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 Library General Public * License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * *//** * class SdpMessage * * Class for representing an SDP message. * * This code has been generated using C2J++ * C2J++ is based on Chris Laffra's C2J (laffra@watson.ibm.com) * Read general disclaimer distributed with C2J++ before using this code * For information about C2J++, send mail to Ilya_Tilevich@ibi.com */package org.mitre.jsip;import java.util.Date;public class SdpMessage{ /** * Construct a blank SDP message object. */ public SdpMessage() { valid = true; port = 0; payload_types = new int[1]; payload_types[0] = 0; // set gsm as a default } /** * Parse the given SDP message. * * @param parseinput */ public SdpMessage(String parseinput) { parseInput( parseinput ); } /** * Returns true if the parsed SDP message is valid. * * @return bool */ public boolean isValid() { return valid; } /** * Returns true if this SDP implies the call is on hold. * * @return bool */ public boolean isOnHold() { return ( ipaddress.trim() == "0.0.0.0" ); } /** * Returns the unique session name. This is the s= line of SDP. * * @return String */ public String getName() { return sessionname; } /** * Returns the IP address of the session described. * * @return String */ public String getIpAddress() { return ipaddress; } /** * Returns the port on which media is being received. * * @return int */ public int getPort() { return port; } /** * Sets the IP address to which media should be directed. * * @param newaddr */ public void setIpAddress(String newaddr) { ipaddress = newaddr; } /** * Sets the port on which media is to be received. * * @param newport */ public void setPort(int newport) { port = newport; } /** * Sets the name of the session (the SDP s= line). * * @param newname */ public void setName(String newname) { sessionname = newname; } /** * Sets the username (for the SDP o= line). * * @param username */ public void setUsername(String username) { this.username = username; } /** * Sets the payload types (for the SDP m= line) * * @param types The payload types the application can use */ public void setPayloadTypes(int[] types) { payload_types = new int[types.length]; System.arraycopy(types, 0, payload_types, 0, types.length); } /** * Parses the given input as an SDP message. * * @param parseinput */ public void parseInput(String parseinput) { String input = parseinput; String curline; String portstr; String codecstr; valid = false; port = 0; ipaddress = null; // Guarentee termination input += '\n'; if( input.substring( 0, input.indexOf( '\n' ) - 1 ) != "v=0" ) { return; } input = input.substring( input.indexOf( '\n' ) + 1 ); while( input.length() > 0 ) { curline = input.substring( 0, input.indexOf( '\n' ) - 1 ); input = input.substring( input.indexOf( '\n' ) + 1 ); if( curline.substring( 0, curline.indexOf( '=' ) - 1 ) == "c" ) { ipaddress = curline.substring( curline.indexOf( "IP4" ) + 4 ).trim(); } if( curline.substring( 0, curline.indexOf( '=' ) - 1 ) == "m" ) { portstr = curline.substring( curline.indexOf( "audio" ) + 5 ); portstr = portstr.substring( 0, portstr.indexOf( "RTP" ) - 1 ).trim(); // fix for ericsson if( portstr.indexOf( '/' ) >= 0 ) { if (SipClient.DEBUG) System.out.println( "SdpMessage: Uh-oh, ericsson is calling again" ); portstr = portstr.substring( 0, portstr.indexOf( '/' ) - 1 ).trim(); } codecstr = curline.substring( curline.indexOf( "AVP" ) + 4 ).trim(); codecstr = " " + codecstr + " "; // ???? if( codecstr.indexOf( " 0 " ) >= 0 ) { try { port = Integer.parseInt(portstr); if (SipClient.DEBUG) System.out.println( "SdpMessage: We found a match! (port " + port + ")" ); } catch (NumberFormatException nfe) { System.err.println("couldn't parse port string. Setting to default"); port = 5060; } } } } valid = true; } /** * Builds and returns an SDP message given the contents. * * @return String */ public String message() { Date date = new Date(); // to make a timestamp. String msg = ""; // Version msg += "v=0\r\n"; // Session Id msg += "o=" + username + " " + String.valueOf( date.getTime() ) + " 0 IN IP4 " + SipUtil.getLocalFqdn() + "\r\n"; // Session Name msg += "s=" + sessionname + "\r\n"; // Connection Info msg += "c=IN IP4 " + ipaddress + "\r\n"; // Time active msg += "t=0 0\r\n"; // Media information msg += "m=audio " + String.valueOf( port ) + " RTP/AVP " + createPayloadString() + "\r\n"; // rtpmap msg += "a=rtpmap:0 PCMU/8000\r\n"; // more fixes for nortel msg += "a=ptime:20\r\n"; return msg; } private String createPayloadString() { String ret = ""; for (int i = 0; i < payload_types.length; i++) ret += String.valueOf(payload_types[i]) + " "; return ret; } /** * test out SdpMessage */ public static void main(String[] args) { int [] payloadTypes = {0, 3}; String mcastAddr = "225.5.5.5"; int mcastPort = 25555; // create a message from scratch and print it out SdpMessage sdpMessage = new SdpMessage(); sdpMessage.setName("jSIP"); sdpMessage.setUsername("steve"); sdpMessage.setPayloadTypes(payloadTypes); sdpMessage.setIpAddress(mcastAddr); sdpMessage.setPort(mcastPort); System.out.println("Test message is\n" + sdpMessage.message()); } // class variables private boolean valid; private String sessionname; private String ipaddress; private String username = "Unknown"; private int port; private int[] payload_types;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -