useragent.java

来自「结构非常清晰的SIP协议栈」· Java 代码 · 共 778 行 · 第 1/3 页

JAVA
778
字号
      printLog("CONFIRMED/CALL",LogLevel.HIGH);
      changeStatus(UA_ONCALL);
      // play "on" sound
      if (clip_on!=null) clip_on.replay();
      launchMediaApplication();
      if (user_profile.hangup_time>0) this.automaticHangup(user_profile.hangup_time); 
   }


   /** Callback function called when arriving a 2xx (re-invite/modify accepted) */
   public void onCallReInviteAccepted(Call call, String sdp, Message resp)
   {  printLog("onCallReInviteAccepted()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("RE-INVITE-ACCEPTED/CALL",LogLevel.HIGH);
   }


   /** Callback function called when arriving a 4xx (re-invite/modify failure) */
   public void onCallReInviteRefused(Call call, String reason, Message resp)
   {  printLog("onCallReInviteRefused()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("RE-INVITE-REFUSED ("+reason+")/CALL",LogLevel.HIGH);
      if (listener!=null) listener.onUaCallFailed(this);
   }


   /** Callback function called when arriving a 4xx (call failure) */
   public void onCallRefused(Call call, String reason, Message resp)
   {  printLog("onCallRefused()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("REFUSED ("+reason+")",LogLevel.HIGH);
      changeStatus(UA_IDLE);
      if (call==call_transfer)
      {  StatusLine status_line=resp.getStatusLine();
         int code=status_line.getCode();
         //String reason=status_line.getReason();
         this.call.notify(code,reason);
         call_transfer=null;
      }
      // play "off" sound
      if (clip_off!=null) clip_off.replay();
      if (listener!=null) listener.onUaCallFailed(this);
   }


   /** Callback function called when arriving a 3xx (call redirection) */
   public void onCallRedirection(Call call, String reason, Vector contact_list, Message resp)
   {  printLog("onCallRedirection()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("REDIRECTION ("+reason+")",LogLevel.HIGH);
      call.call(((String)contact_list.elementAt(0))); 
   }


   /** Callback function that may be overloaded (extended). Called when arriving a CANCEL request */
   public void onCallCanceling(Call call, Message cancel)
   {  printLog("onCallCanceling()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("CANCEL",LogLevel.HIGH);
      changeStatus(UA_IDLE);
      // stop ringing
      if (clip_ring!=null) clip_ring.stop();
      // play "off" sound
      if (clip_off!=null) clip_off.replay();
      if (listener!=null) listener.onUaCallCancelled(this);
   }


   /** Callback function called when arriving a BYE request */
   public void onCallClosing(Call call, Message bye)
   {  printLog("onCallClosing()",LogLevel.LOW);
      if (call!=this.call && call!=call_transfer) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      if (call!=call_transfer && call_transfer!=null)
      {  printLog("CLOSE PREVIOUS CALL",LogLevel.HIGH);
         this.call=call_transfer;
         call_transfer=null;
         return;
      }
      // else
      printLog("CLOSE",LogLevel.HIGH);
      closeMediaApplication();
      // play "off" sound
      if (clip_off!=null) clip_off.replay();
      if (listener!=null) listener.onUaCallClosed(this);
      changeStatus(UA_IDLE);
   }


   /** Callback function called when arriving a response after a BYE request (call closed) */
   public void onCallClosed(Call call, Message resp)
   {  printLog("onCallClosed()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("CLOSE/OK",LogLevel.HIGH);
      if (listener!=null) listener.onUaCallClosed(this);
      changeStatus(UA_IDLE);
   }

   /** Callback function called when the invite expires */
   public void onCallTimeout(Call call)
   {  printLog("onCallTimeout()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("NOT FOUND/TIMEOUT",LogLevel.HIGH);
      changeStatus(UA_IDLE);
      if (call==call_transfer)
      {  int code=408;
         String reason="Request Timeout";
         this.call.notify(code,reason);
         call_transfer=null;
      }
      // play "off" sound
      if (clip_off!=null) clip_off.replay();
      if (listener!=null) listener.onUaCallFailed(this);
   }



   // ****************** ExtendedCall callback functions ******************

   /** Callback function called when arriving a new REFER method (transfer request) */
   public void onCallTransfer(ExtendedCall call, NameAddress refer_to, NameAddress refered_by, Message refer)
   {  printLog("onCallTransfer()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("Transfer to "+refer_to.toString(),LogLevel.HIGH);
      call.acceptTransfer();
      call_transfer=new ExtendedCall(sip_provider,user_profile.from_url,user_profile.contact_url,this);
      call_transfer.call(refer_to.toString(),local_session);
   }

   /** Callback function called when a call transfer is accepted. */
   public void onCallTransferAccepted(ExtendedCall call, Message resp)
   {  printLog("onCallTransferAccepted()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("Transfer accepted",LogLevel.HIGH);
   }

   /** Callback function called when a call transfer is refused. */
   public void onCallTransferRefused(ExtendedCall call, String reason, Message resp)
   {  printLog("onCallTransferRefused()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("Transfer refused",LogLevel.HIGH);
   }

   /** Callback function called when a call transfer is successfully completed */
   public void onCallTransferSuccess(ExtendedCall call, Message notify)
   {  printLog("onCallTransferSuccess()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("Transfer successed",LogLevel.HIGH);
      call.hangup();
      if (listener!=null) listener.onUaCallTrasferred(this);
   }

   /** Callback function called when a call transfer is NOT sucessfully completed */
   public void onCallTransferFailure(ExtendedCall call, String reason, Message notify)
   {  printLog("onCallTransferFailure()",LogLevel.LOW);
      if (call!=this.call) {  printLog("NOT the current call",LogLevel.LOW);  return;  }
      printLog("Transfer failed",LogLevel.HIGH);
   }


   // ************************* Schedule events ***********************

   /** Schedules a re-inviting event after <i>delay_time</i> secs. */
   void reInvite(final String contact_url, final int delay_time)
   {  SessionDescriptor sdp=new SessionDescriptor(local_session);
      final SessionDescriptor new_sdp=new SessionDescriptor(sdp.getOrigin(),sdp.getSessionName(),new ConnectionField("IP4","0.0.0.0"),new TimeField());
      new_sdp.addMediaDescriptors(sdp.getMediaDescriptors());
      (new Thread() {  public void run() {  runReInvite(contact_url,new_sdp.toString(),delay_time);  }  }).start();
   }
    
   /** Re-invite. */
   private void runReInvite(String contact, String body, int delay_time)
   {  try
      {  if (delay_time>0) Thread.sleep(delay_time*1000);
         printLog("RE-INVITING/MODIFING");
         if (call!=null && call.isOnCall())
         {  printLog("REFER/TRANSFER");
            call.modify(contact,body);
         }
      }
      catch (Exception e) { e.printStackTrace(); }
   }


   /** Schedules a call-transfer event after <i>delay_time</i> secs. */
   void callTransfer(final String transfer_to, final int delay_time)
   {  (new Thread() {  public void run() {  runCallTransfer(transfer_to,delay_time);  }  }).start();
   }

   /** Call-transfer. */
   private void runCallTransfer(String transfer_to, int delay_time)
   {  try
      {  if (delay_time>0) Thread.sleep(delay_time*1000);
         if (call!=null && call.isOnCall())
         {  printLog("REFER/TRANSFER");
            call.transfer(transfer_to);
         }
      }
      catch (Exception e) { e.printStackTrace(); }
   }


   /** Schedules an automatic answer event after <i>delay_time</i> secs. */
   void automaticAccept(final int delay_time)
   {  (new Thread() {  public void run() {  runAutomaticAccept(delay_time);  }  }).start();
   }

   /** Automatic answer. */
   private void runAutomaticAccept(int delay_time)
   {  try
      {  if (delay_time>0) Thread.sleep(delay_time*1000);
         if (call!=null)
         {  printLog("AUTOMATIC-ANSWER");
            accept();
         }
      }
      catch (Exception e) { e.printStackTrace(); }
   }


   /** Schedules an automatic hangup event after <i>delay_time</i> secs. */
   void automaticHangup(final int delay_time)
   {  (new Thread() {  public void run() {  runAutomaticHangup(delay_time);  }  }).start();
   }

   /** Automatic hangup. */
   private void runAutomaticHangup(int delay_time)
   {  try
      {  if (delay_time>0) Thread.sleep(delay_time*1000);
         if (call!=null && call.isOnCall())
         {  printLog("AUTOMATIC-HANGUP");
            hangup();
            listen();
         }
      }
      catch (Exception e) { e.printStackTrace(); }
   }


   // ****************************** Logs *****************************

   /** Adds a new string to the default Log */
   void printLog(String str)
   {  printLog(str,LogLevel.HIGH);
   }

   /** Adds a new string to the default Log */
   void printLog(String str, int level)
   {  if (log!=null) log.println("UA: "+str,level+SipStack.LOG_LEVEL_UA);  
      if ((user_profile==null || !user_profile.no_prompt) && level<=LogLevel.HIGH) System.out.println("UA: "+str);
   }

   /** Adds the Exception message to the default Log */
   void printException(Exception e,int level)
   {  if (log!=null) log.printException(e,level+SipStack.LOG_LEVEL_UA);
   }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?