Exemplo n.º 1
0
  protected void setupSIP(Context context, SIP5060ProvisioningRequest req) throws IOException {

    AppProperties props = new AppProperties(context);

    // Setup the SIP preferences
    SharedPreferences settings =
        context.getSharedPreferences(RegisterAccount.PREFS_FILE, Context.MODE_PRIVATE);

    SharedPreferences sipSettings =
        context.getSharedPreferences(Settings.sharedPrefsFile, Context.MODE_PRIVATE);
    Editor edSIP = sipSettings.edit();

    String num = req.getPhoneNumber();

    LumicallDataSource ds = new LumicallDataSource(context);
    ds.open();
    SIPIdentity sipIdentity = createSIPIdentity(props, settings, req);
    for (SIPIdentity s : ds.getSIPIdentities()) {
      if (s.getUri().equals(sipIdentity.getUri())) sipIdentity.setId(s.getId());
    }
    ds.persistSIPIdentity(sipIdentity);
    ds.deleteSIP5060ProvisioningRequest(req);
    ds.close();
    edSIP.putString(Settings.PREF_SIP, Long.toString(sipIdentity.getId()));
    if (!sipSettings.contains(Settings.PREF_TEL)) edSIP.putString(Settings.PREF_TEL, "-1");

    /* edSIP.putString(Settings.PREF_USERNAME, settings.getString(RegisterAccount.PREF_PHONE_NUMBER, null));
    edSIP.putString(Settings.PREF_PASSWORD, settings.getString(RegisterAccount.PREF_SECRET, null));
    edSIP.putString(Settings.PREF_SERVER, DEFAULT_SIP_SERVER);
    edSIP.putString(Settings.PREF_DOMAIN, DEFAULT_SIP_DOMAIN);
    edSIP.putString(Settings.PREF_PROTOCOL, "tcp");  // FIXME - change to TLS
    edSIP.putBoolean(Settings.PREF_STUN, true);
    edSIP.putString(Settings.PREF_STUN_SERVER, DEFAULT_STUN_SERVER);
    edSIP.putString(Settings.PREF_STUN_SERVER_PORT, "" + DEFAULT_STUN_SERVER_PORT); */
    edSIP.putBoolean(Settings.PREF_WLAN, true);
    edSIP.putBoolean(Settings.PREF_EDGE, true);
    edSIP.putBoolean(Settings.PREF_3G, true);
    edSIP.putBoolean(Settings.PREF_ON, true);

    if (edSIP.commit()) Log.v(TAG, "Configured prefs for number " + num);
    else {
      Log.e(TAG, "error while committing preferences");
    }

    // Receiver.engine(context).updateDNS();
    Receiver.engine(context).halt();
    Receiver.engine(context).StartEngine();
  }
  public int speaker(int mode) {
    int old = speakermode;

    if ((Receiver.headset > 0 || Receiver.docked > 0 || Receiver.bluetooth > 0)
        && mode != Receiver.speakermode()) return old;
    if (mode == old) return old;
    enableBluetooth(false);
    saveVolume();
    setMode(speakermode = mode);
    setCodec();
    restoreVolume();
    if (mode == AudioManager.MODE_NORMAL && Thread.currentThread().getName().equals("main"))
      Toast.makeText(Receiver.mContext, R.string.help_speakerphone, Toast.LENGTH_LONG).show();
    return old;
  }
  /** Runs it in a new Thread. */
  public void run() {
    boolean nodata =
        PreferenceManager.getDefaultSharedPreferences(Receiver.mContext)
            .getBoolean(
                org.sipdroid.sipua.ui.Settings.PREF_NODATA,
                org.sipdroid.sipua.ui.Settings.DEFAULT_NODATA);
    keepon =
        PreferenceManager.getDefaultSharedPreferences(Receiver.mContext)
            .getBoolean(
                org.sipdroid.sipua.ui.Settings.PREF_KEEPON,
                org.sipdroid.sipua.ui.Settings.DEFAULT_KEEPON);

    if (rtp_socket == null) {
      if (DEBUG) println("ERROR: RTP socket is null");
      return;
    }

    byte[] buffer = new byte[BUFFER_SIZE + 12];
    rtp_packet = new RtpPacket(buffer, 0);

    if (DEBUG) println("Reading blocks of max " + buffer.length + " bytes");

    running = true;
    enableBluetooth(
        PreferenceManager.getDefaultSharedPreferences(Receiver.mContext)
            .getBoolean(
                org.sipdroid.sipua.ui.Settings.PREF_BLUETOOTH,
                org.sipdroid.sipua.ui.Settings.DEFAULT_BLUETOOTH));
    restored = false;

    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
    am = (AudioManager) Receiver.mContext.getSystemService(Context.AUDIO_SERVICE);
    cr = Receiver.mContext.getContentResolver();
    saveSettings();
    Settings.System.putInt(
        cr, Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
    am.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
    am.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
    if (oldvol == -1) oldvol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
    initMode();
    setCodec();
    short lin[] = new short[BUFFER_SIZE];
    short lin2[] = new short[BUFFER_SIZE];
    int server, headroom, todo, len = 0, m = 1, expseq, getseq, vm = 1, gap, gseq;
    ToneGenerator tg =
        new ToneGenerator(
            AudioManager.STREAM_VOICE_CALL,
            (int) (ToneGenerator.MAX_VOLUME * 2 * org.sipdroid.sipua.ui.Settings.getEarGain()));
    track.play();
    System.gc();
    empty();
    lockFirst = true;
    while (running) {
      lock(true);
      if (Receiver.call_state == UserAgent.UA_STATE_HOLD) {
        lock(false);
        tg.stopTone();
        track.pause();
        while (running && Receiver.call_state == UserAgent.UA_STATE_HOLD) {
          try {
            sleep(1000);
          } catch (InterruptedException e1) {
          }
        }
        track.play();
        System.gc();
        timeout = 1;
        luser = luser2 = -8000 * mu;
      }
      try {
        rtp_socket.receive(rtp_packet);
        if (timeout != 0) {
          tg.stopTone();
          track.pause();
          for (int i = maxjitter * 2; i > 0; i -= BUFFER_SIZE)
            write(lin2, 0, i > BUFFER_SIZE ? BUFFER_SIZE : i);
          cnt += maxjitter * 2;
          track.play();
          empty();
        }
        timeout = 0;
      } catch (IOException e) {
        if (timeout == 0 && nodata) {
          tg.startTone(ToneGenerator.TONE_SUP_RINGTONE);
        }
        rtp_socket.getDatagramSocket().disconnect();
        if (++timeout > 60) {
          Receiver.engine(Receiver.mContext).rejectcall();
          break;
        }
      }
      if (running && timeout == 0) {
        gseq = rtp_packet.getSequenceNumber();
        if (seq == gseq) {
          m++;
          continue;
        }
        gap = (gseq - seq) & 0xff;
        if (gap > 240) continue;
        server = track.getPlaybackHeadPosition();
        headroom = user - server;

        if (headroom > 2 * jitter) cnt += len;
        else cnt = 0;

        if (lserver == server) cnt2++;
        else cnt2 = 0;

        if (cnt <= 500 * mu
            || cnt2 >= 2
            || headroom - jitter < len
            || p_type.codec.number() != 8
            || p_type.codec.number() != 0) {
          if (rtp_packet.getPayloadType() != p_type.number
              && p_type.change(rtp_packet.getPayloadType())) {
            saveVolume();
            setCodec();
            restoreVolume();
            codec = p_type.codec.getTitle();
          }
          len = p_type.codec.decode(buffer, lin, rtp_packet.getPayloadLength());

          // Call recording: Save incoming.
          // Data is in buffer lin, from 0 to len.
          if (call_recorder != null) call_recorder.writeIncoming(lin, 0, len);

          if (speakermode == AudioManager.MODE_NORMAL) calc(lin, 0, len);
          else if (gain > 1) calc2(lin, 0, len);
        }

        avgheadroom = avgheadroom * 0.99 + (double) headroom * 0.01;
        if (avgcnt++ > 300)
          devheadroom = devheadroom * 0.999 + Math.pow(Math.abs(headroom - avgheadroom), 2) * 0.001;
        if (headroom < 250 * mu) {
          late++;
          newjitter(true);
          System.out.println("RTP:underflow " + (int) Math.sqrt(devheadroom));
          todo = jitter - headroom;
          write(lin2, 0, todo > BUFFER_SIZE ? BUFFER_SIZE : todo);
        }

        if (cnt > 500 * mu && cnt2 < 2) {
          todo = headroom - jitter;
          if (todo < len) write(lin, todo, len - todo);
        } else write(lin, 0, len);

        if (seq != 0) {
          getseq = gseq & 0xff;
          expseq = ++seq & 0xff;
          if (m == RtpStreamSender.m) vm = m;
          gap = (getseq - expseq) & 0xff;
          if (gap > 0) {
            System.out.println("RTP:lost");
            if (gap > 100) gap = 1;
            loss += gap;
            lost += gap;
            good += gap - 1;
            loss2++;
          } else {
            if (m < vm) {
              loss++;
              loss2++;
            }
          }
          good++;
          if (good > 110) {
            good *= 0.99;
            lost *= 0.99;
            loss *= 0.99;
            loss2 *= 0.99;
            late *= 0.99;
          }
        }
        m = 1;
        seq = gseq;

        if (user >= luser + 8000 * mu
            && (Receiver.call_state == UserAgent.UA_STATE_INCALL
                || Receiver.call_state == UserAgent.UA_STATE_OUTGOING_CALL)) {
          if (luser == -8000 * mu || getMode() != speakermode) {
            saveVolume();
            setMode(speakermode);
            restoreVolume();
          }
          luser = user;
          if (user >= luser2 + 160000 * mu) newjitter(false);
        }
        lserver = server;
      }
    }
    lock(false);
    track.stop();
    track.release();
    tg.stopTone();
    tg.release();
    saveVolume();
    am.setStreamVolume(AudioManager.STREAM_MUSIC, oldvol, 0);
    restoreSettings();
    enableBluetooth(false);
    am.setStreamVolume(AudioManager.STREAM_MUSIC, oldvol, 0);
    oldvol = -1;
    p_type.codec.close();
    rtp_socket.close();
    rtp_socket = null;
    codec = "";

    // Call recording: stop incoming receive.
    if (call_recorder != null) {
      call_recorder.stopIncoming();
      call_recorder = null;
    }

    if (DEBUG) println("rtp receiver terminated");

    cleanupBluetooth();
  }