/** Handle a answer request */
  public boolean handleAnswerRequest(Interlocutor interlocutor) {

    // cancel call request if no Media Locator
    if (PhoneManager.isUseStaticLocator() && PhoneManager.isUsingMediaLocator()) {
      return false;
    }

    PhoneManager.setUsingMediaLocator(true);

    SessionDescription sdpData = null;
    try {
      sdpData = mediaManager.generateSdpDescription();
      interlocutor.getCall().setLocalSdpDescription(sdpData);
    } catch (MediaException ex) {
      try {
        sipManager.sendServerInternalError(interlocutor.getID());
      } catch (CommunicationsException ex1) {
        Log.error("handleAnswerRequest", ex1);
      }
      return false;
    }
    try {
      sipManager.answerCall(interlocutor.getID(), sdpData.toString());
    } catch (CommunicationsException exc) {
      Log.error("handleAnswerRequest", exc);
      return false;
    }
    return true;
  }
 /** Handle a exit request */
 public void handleExitRequest() {
   if (mediaManager != null) {}
   // SIP unregister
   if (sipManager != null) {
     try {
       sipManager.endAllCalls();
     } catch (CommunicationsException exc) {
       Log.error("handleExitRequest", exc);
     } catch (Throwable exc) {
       Log.error("handleExitRequest", exc);
     }
     try {
       sipManager.unregister();
     } catch (CommunicationsException exc) {
       Log.error("handleExitRequest", exc);
     } catch (Throwable exc) {
       Log.error("handleExitRequest", exc);
     }
     try {
       sipManager.stop();
     } catch (Exception exc) {
       Log.error("handleExitRequest", exc);
     }
   }
   NetworkAddressManager.shutDown();
 }
  /** Create the softphone handlers and stack */
  public void createSoftPhone(String server) throws MediaException {

    this.server = server;
    SIPConfig.setServer(server);

    if (sipManager != null) {
      destroySoftPhone();
    }

    sipManager = new SipManager();
    softPhoneMedia = new SoftPhoneMedia();
    softPhoneSecurity = new SoftPhoneSecurity();

    sipManager.addCommunicationsListener(this);
    sipManager.setSecurityAuthority(softPhoneSecurity);

    try {
      // put in a seperate thread
      sipManager.start();
      if (sipManager.isStarted()) {
        Log.debug("createSoftPhone", "SIP STARTED");
      }
    } catch (CommunicationsException exc) {
      Log.error("createSoftPhone", exc);
    }
  }
 /** Handle an UnregisterRequest */
 public void handleUnregisterRequest() {
   if (sipManager != null) {
     try {
       sipManager.endAllCalls();
       sipManager.unregister();
     } catch (Exception e) {
       Log.error("handleUnregisterRequest", e);
     }
   }
 }
  /** Handle a dial request */
  public void handleDialRequest(String phoneNumber) {
    try {

      System.err.println(
          "Audio Static:"
              + PhoneManager.isUseStaticLocator()
              + " Using:"
              + PhoneManager.isUsingMediaLocator());

      // cancel call request if no Media Locator
      if (PhoneManager.isUseStaticLocator() && PhoneManager.isUsingMediaLocator()) {
        return;
      }

      PhoneManager.setUsingMediaLocator(true);

      SessionDescription sdpData = mediaManager.generateSdpDescription();

      Call call = sipManager.establishCall(phoneNumber, sdpData.toString());

      if (call == null) return;

      call.setLocalSdpDescription(sdpData);

      call.addStateChangeListener(this);
      Interlocutor interlocutor = new Interlocutor();
      interlocutor.setCall(call);

      guiManager.addInterlocutor(interlocutor);
    } catch (Exception e) {
      Log.error("handleDialRequest", e);
    }
  }
 /**
  * Send the dtmf digit to the sip server.
  *
  * @param callID the caller id
  * @param digit the digit typed.
  */
 void sendDTMFDigit(int callID, String digit) {
   try {
     sipManager.sendDTMF(callID, digit);
   } catch (CommunicationsException exc) {
     Log.error("sendDTMFDigit", exc);
   }
 }
  /** Destroys the softphone handlers and stack */
  public void destroySoftPhone() {

    try {
      sipManager.stop();
    } catch (Exception exc) {
      Log.error("destroySoftPhone", exc);
    }
  }
  /**
   * Handle a Hold request
   *
   * @param iui the InterlocutorUI
   * @param mic true to place on hold.
   * @param cam true to place camera on hold.
   */
  public void handleHold(InterlocutorUI iui, boolean mic, boolean cam) {

    try {
      sipManager.hold(
          iui.getID(), mediaManager.generateHoldSdpDescription(mic, mic, iui.getCall()), mic, cam);
    } catch (Exception e) {
      Log.error("handleHold", e);
    }
  }
 /**
  * Handle a Re-Register Request. This method will only have effect if the user has successfully
  * registered beforeat least once before.
  */
 public void handleReRegisterRequest() {
   if (this.password != null && !this.username.equals("")) {
     try {
       sipManager.startRegisterProcess(username, authUserName, password);
     } catch (CommunicationsException exc) {
       Log.error("handleRegisterRequest", exc);
     }
   }
 }
 /** Handle a hangup request */
 public boolean handleHangupRequest(Interlocutor interlocutor) {
   boolean hangupOk = true;
   try {
     sipManager.endCall(interlocutor.getID());
   } catch (CommunicationsException exc) {
     guiManager.remove(interlocutor);
     hangupOk = false;
     Log.error("handleHangupRequest", exc);
   }
   return hangupOk;
 }
  /**
   * Handle an RegisterRequest
   *
   * @param username username
   * @param authUserName Authorization username
   * @param password password
   */
  public void handleRegisterRequest(String username, String authUserName, String password) {

    this.authUserName = authUserName;
    this.username = username;
    this.password = password;

    try {
      sipManager.startRegisterProcess(username, authUserName, password);
    } catch (CommunicationsException exc) {
      Log.error("handleRegisterRequest", exc);
    }
  }
  public void handleTransfer(int callID, String callee) {

    sipManager.transfer(callID, callee);
  }
  /**
   * Fired when call state changes
   *
   * @param evt CallStateEvent
   */
  public void callStateChanged(CallStateEvent evt) {
    try {

      for (SoftPhoneListener sfl : softPhoneListeners) {
        sfl.callStateChanged(evt);
      }

      Call call = evt.getSourceCall();
      Log.debug("callStateChanged", evt.getOldState() + " -> " + evt.getNewState());
      if (evt.getNewState() == Call.CONNECTED) {
        // sipManager.setBusy(true);

        if (call.getAudioReceiverChannel() != null) call.getAudioReceiverChannel().stop();

        if (evt.getOldState() == Call.MOVING_REMOTELY) {
          AudioMediaSession audioMediaSession = evt.getSourceCall().getAudioMediaSession();
          if (call.getAudioReceiverChannel() != null) call.getAudioReceiverChannel().stop();

          if (audioMediaSession != null) {
            audioMediaSession.stopTrasmit();
            audioMediaSession.stopReceive();
          }
          VideoMediaSession videoMediaSession = evt.getSourceCall().getVideoMediaSession();
          if (videoMediaSession != null) {
            videoMediaSession.stopTrasmit();
            videoMediaSession.stopReceive();
          }
          PhoneManager.setUsingMediaLocator(false);
        }

        int localAudioPort = -1;
        int localVideoPort = -1;

        Vector<MediaDescription> mediaDescriptions =
            call.getLocalSdpDescription().getMediaDescriptions(true);
        for (MediaDescription mediaDescription : mediaDescriptions) {
          if (mediaDescription.getMedia().getMediaType().equals("audio"))
            localAudioPort = mediaDescription.getMedia().getMediaPort();
          else if (mediaDescription.getMedia().getMediaType().equals("video"))
            localVideoPort = mediaDescription.getMedia().getMediaPort();
        }

        AudioMediaSession audioMediaSession =
            mediaManager.createAudioMediaSession(
                call.getRemoteSdpDescription().toString(), localAudioPort);
        call.setAudioMediaSession(audioMediaSession);

        if (audioMediaSession != null) {
          audioMediaSession.startTrasmit();
          audioMediaSession.startReceive();
        }

        // If remote client have video
        if (localVideoPort > 0) {
          if (SettingsManager.getLocalPreferences().getVideoDevice() != null
              && !"".equals(SettingsManager.getLocalPreferences().getVideoDevice())) {
            VideoMediaSession videoMediaSession =
                mediaManager.createVideoMediaSession(
                    call.getRemoteSdpDescription().toString(), localVideoPort);
            if (videoMediaSession != null) {
              videoMediaSession.startTrasmit();
              videoMediaSession.startReceive();
              call.setVideoMediaSession(videoMediaSession);
            }
          }
        }

        evt.getSourceCall().start();

        Log.debug("MEDIA STREAMS OPENED");

      } else if (evt.getNewState() == Call.RINGING) {

        if (call.getRemoteSdpDescription() != null && !call.getRemoteSdpDescription().equals("")) {

          Log.debug("STATE", call.getRemoteSdpDescription().toString());

          int localPort =
              ((MediaDescription) (call.getLocalSdpDescription().getMediaDescriptions(true).get(0)))
                  .getMedia()
                  .getMediaPort();
          int destPort =
              ((MediaDescription)
                      (call.getRemoteSdpDescription().getMediaDescriptions(true).get(0)))
                  .getMedia()
                  .getMediaPort();
          String destIp = call.getRemoteSdpDescription().getConnection().getAddress();

          AudioReceiverChannel audioReceiverChannel =
              mediaManager.createAudioReceiverChannel(localPort, destIp, destPort, (destPort + 1));
          call.setAudioReceiverChannel(audioReceiverChannel);

          if (audioReceiverChannel != null) audioReceiverChannel.start();
        }

      } else if (evt.getNewState() == Call.DISCONNECTED) {
        sipManager.setBusy(false);

        AudioMediaSession audioMediaSession = evt.getSourceCall().getAudioMediaSession();
        if (audioMediaSession != null) {
          audioMediaSession.stopTrasmit();
          audioMediaSession.stopReceive();
        }
        if (call.getAudioReceiverChannel() != null) call.getAudioReceiverChannel().stop();
        VideoMediaSession videoMediaSession = evt.getSourceCall().getVideoMediaSession();
        if (videoMediaSession != null) {
          videoMediaSession.stopTrasmit();
          videoMediaSession.stopReceive();
        }

        PhoneManager.setUsingMediaLocator(false);

      } else if (evt.getNewState() == Call.FAILED) {
        call.setState(Call.DISCONNECTED);
        if (call.getAudioReceiverChannel() != null) call.getAudioReceiverChannel().stop();

        CallRejectedEvent rejectEvt =
            new CallRejectedEvent("Disconnected", call.getLastRequest(), call);

        for (SoftPhoneListener softPhoneListener : softPhoneListeners) {
          softPhoneListener.callRejectedRemotely(rejectEvt);
        }

        PhoneManager.setUsingMediaLocator(false);
      }
    } catch (Exception e) {
      Log.error("callStateChanged", e);
    }
  }
 /**
  * Add a CommunicationsListener
  *
  * @param communicationsListener CommunicationsListener
  */
 public void addCommunicationsListener(CommunicationsListener communicationsListener) {
   sipManager.addCommunicationsListener(communicationsListener);
 }