callprocessing.java
来自「java 开发的sip软电话 源码 jain sip」· Java 代码 · 共 1,497 行 · 第 1/4 页
JAVA
1,497 行
call.setState(Call.DISCONNECTED);
sipManCallback.fireCallRejectedRemotely(
"Server returned a NOT IMPLEMENTED Response",
response
);
}
finally
{
}
}
//-------------------------------- User Initiated processing ---------------------------------
Call invite(String callee, String sdpContent) throws
CommunicationsException
{
try
{
callee = callee.trim();
//Remove excessive characters from phone numbers such as ' ','(',')','-'
String excessiveChars = Utils.getProperty("net.java.mais.sip.EXCESSIVE_URI_CHARACTERS");
//---------------------------------------------------------------------------
//un ugly hack to override old xml configurations (todo: remove at some point)
//define excessive chars for sipphone.com users
String isSipphone = Utils.getProperty("net.java.mais.sipphone.IS_RUNNING_SIPPHONE");
if(excessiveChars == null && isSipphone != null && isSipphone.equalsIgnoreCase("true"))
{
excessiveChars = "( )-";
PropertiesDepot.setProperty("net.java.mais.sip.EXCESSIVE_URI_CHARACTERS", excessiveChars);
PropertiesDepot.storeProperties();
}
//---------------------------------------------------------------------------
if(excessiveChars != null )
{
StringBuffer calleeBuff = new StringBuffer(callee);
for(int i = 0; i < excessiveChars.length(); i++)
{
String charToDeleteStr = excessiveChars.substring(i, i+1);
int charIndex = -1;
while( (charIndex = calleeBuff.indexOf(charToDeleteStr)) != -1)
calleeBuff.delete(charIndex, charIndex + 1);
}
callee = calleeBuff.toString();
}
//Handle default domain name (i.e. transform 1234 -> 1234@sip.com
String defaultDomainName =
Utils.getProperty("net.java.mais.sip.DEFAULT_DOMAIN_NAME");
if (defaultDomainName != null //no sip scheme
&& !callee.trim().startsWith("tel:")
&& callee.indexOf('@') == -1 //most probably a sip uri
) {
callee = callee + "@" + defaultDomainName;
}
//Let's be uri fault tolerant
if (callee.toLowerCase().indexOf("sip:") == -1 //no sip scheme
&& callee.indexOf('@') != -1 //most probably a sip uri
) {
callee = "sip:" + callee;
}
//Request URI
URI requestURI;
try {
requestURI = sipManCallback.addressFactory.createURI(callee);
}
catch (ParseException ex) {
throw new CommunicationsException(callee +
" is not a legal SIP uri!", ex);
}
//Call ID
CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
//CSeq
CSeqHeader cSeqHeader;
try {
cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1,
Request.INVITE);
}
catch (ParseException ex) {
//Shouldn't happen
throw new CommunicationsException(
"An unexpected erro occurred while"
+ "constructing the CSeqHeadder", ex);
}
catch (InvalidArgumentException ex) {
//Shouldn't happen
throw new CommunicationsException(
"An unexpected erro occurred while"
+ "constructing the CSeqHeadder", ex);
}
//FromHeader
FromHeader fromHeader = sipManCallback.getFromHeader();
//ToHeader
Address toAddress = sipManCallback.addressFactory.createAddress(
requestURI);
ToHeader toHeader;
try {
toHeader = sipManCallback.headerFactory.createToHeader(
toAddress, null);
}
catch (ParseException ex) {
//Shouldn't happen
throw new CommunicationsException(
"Null is not an allowed tag for the to header!", ex);
}
//ViaHeaders
ArrayList viaHeaders = sipManCallback.getLocalViaHeaders();
//MaxForwards
MaxForwardsHeader maxForwards = sipManCallback.getMaxForwardsHeader();
//Contact
ContactHeader contactHeader = sipManCallback.getContactHeader();
Request invite = null;
try {
invite = sipManCallback.messageFactory.createRequest(requestURI,
Request.INVITE,
callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders,
maxForwards);
}
catch (ParseException ex) {
throw new CommunicationsException(
"Failed to create invite Request!", ex);
}
//
invite.addHeader(contactHeader);
//Content
ContentTypeHeader contentTypeHeader = null;
try {
//content type should be application/sdp (not applications)
//reported by Oleg Shevchenko (Miratech)
contentTypeHeader =
sipManCallback.headerFactory.createContentTypeHeader(
"application", "sdp");
}
catch (ParseException ex) {
//Shouldn't happen
throw new CommunicationsException(
"Failed to create a content type header for the INVITE request",
ex);
}
try {
invite.setContent(sdpContent, contentTypeHeader);
}
catch (ParseException ex) {
throw new CommunicationsException(
"Failed to parse sdp data while creating invite request!", ex);
}
//Transaction
ClientTransaction inviteTransaction;
try {
inviteTransaction = sipManCallback.sipProvider.
getNewClientTransaction(invite);
}
catch (TransactionUnavailableException ex) {
throw new CommunicationsException(
"Failed to create inviteTransaction.\n" +
"This is most probably a network connection error.", ex);
}
try {
inviteTransaction.sendRequest();
}
catch (SipException ex) {
throw new CommunicationsException(
"An error occurred while sending invite request", ex);
}
Call call = callDispatcher.createCall(inviteTransaction.
getDialog(), invite);
call.setState(Call.DIALING);
return call;
}
finally
{
}
}
// Hold
public void hold(int callID, String sdpContent) throws
CommunicationsException
{
try
{
Call call = callDispatcher.getCall(callID);
Request invite = null;
invite = call.getLastRequest();
int cseq = ((CSeq)(invite.getHeader(CSeq.NAME))).getSequenceNumber()+1;
invite.removeHeader(CSeq.NAME);
try{
invite.addHeader(sipManCallback.headerFactory.createCSeqHeader(cseq,Request.INVITE));
}catch(Exception e){
}
ContentTypeHeader contentTypeHeader = null;
try {
//content type should be application/sdp (not applications)
//reported by Oleg Shevchenko (Miratech)
contentTypeHeader =
sipManCallback.headerFactory.createContentTypeHeader(
"application", "sdp");
}
catch (ParseException ex) {
//Shouldn't happen
throw new CommunicationsException(
"Failed to create a content type header for the INVITE request",
ex);
}
try {
invite.setContent(sdpContent, contentTypeHeader);
}
catch (ParseException ex) {
throw new CommunicationsException(
"Failed to parse sdp data while creating invite request!", ex);
}
//Transaction
ClientTransaction inviteTransaction;
try {
sipManCallback.sipProvider.sendRequest(invite);
call.setLastRequest(invite);
}
catch(SipException ee){
System.out.println("SIP>: "+ee.toString());
}
/*
try {
inviteTransaction.sendRequest();
}
catch (SipException ex) {
throw new CommunicationsException(
"An error occurred while sending invite request", ex);
}
*/
return ;
}
finally
{
}
}
//send DTMF
void sendDTMF(int callID,String digit)
{
try
{
Call call = callDispatcher.getCall(callID);
Dialog dialog = call.getDialog();
sendNumDTMF(dialog,digit);
//
}catch(CommunicationsException e){
}
finally
{
}
} //send DTMF
//end call
void endCall(int callID) throws CommunicationsException{
try
{
Call call = callDispatcher.getCall(callID);
if (call == null) {
throw new CommunicationsException(
"Could not find call with id=" +
callID);
}
Dialog dialog = call.getDialog();
if (call.getState().equals(Call.CONNECTED)
|| call.getState().equals(Call.RECONNECTED)) {
call.setState(Call.DISCONNECTED);
sayBye(dialog);
}
else if (call.getState().equals(Call.DIALING)
|| call.getState().equals(Call.RINGING)) {
if (dialog.getFirstTransaction() != null) {
try {
//Someone knows about us. Let's be polite and say we are leaving
sayCancel(dialog);
}
catch (CommunicationsException ex) {
//something went wrong let's just tell the others
sipManCallback.fireCommunicationsError(
new CommunicationsException(
"Could not send the CANCEL request! "
+ "Remote party won't know we're leaving!",
ex));
}
}
call.setState(Call.DISCONNECTED);
}
else if (call.getState().equals(Call.ALERTING)) {
call.setState(Call.DISCONNECTED);
sayBusyHere(dialog);
}
//For FAILE and BUSY we only need to update CALL_STATUS
else if (call.getState().equals(Call.BUSY)) {
call.setState(Call.DISCONNECTED);
}
else if (call.getState().equals(Call.FAILED)) {
call.setState(Call.DISCONNECTED);
}
else {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException
("Could not determine call state!");
}
}
finally
{
}
} //end call
// send dtmf
private void sendNumDTMF(Dialog dialog,String digit) throws CommunicationsException
{
try
{
Request request = dialog.getFirstTransaction().getRequest();
Request info = null;
String body ="Signal="+digit+"\nDuration=160";
String contentType="application/dtmf-relay";
String[] contentTypeTab = contentType.split("/");
ContentTypeHeader contentTypeHeader = null;
try {
info = dialog.createRequest(Request.INFO);
try {
contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader(contentTypeTab[0], contentTypeTab[1]);
info.setContent(body,contentTypeHeader);
}
catch (ParseException ex) {
throw new CommunicationsException(
"ContentType Header must look like type/subtype!", ex);
}
}
catch (SipException ex) {
throw new CommunicationsException(
"Failed to create bye request!",
ex);
}
ClientTransaction clientTransaction = null;
try {
clientTransaction =
sipManCallback.sipProvider.getNewClientTransaction(info);
}
catch (TransactionUnavailableException ex) {
throw new CommunicationsException(
"Failed to construct a client transaction from the INFO request",
ex);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?