void restoreVolume() { switch (getMode()) { case AudioManager.MODE_IN_CALL: int oldring = PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getInt("oldring", 0); if (oldring > 0) setStreamVolume( AudioManager.STREAM_RING, (int) (am.getStreamMaxVolume(AudioManager.STREAM_RING) * org.sipdroid.sipua.ui.Settings.getEarGain() * 3 / 4), 0); track.setStereoVolume( AudioTrack.getMaxVolume() * (ogain = org.sipdroid.sipua.ui.Settings.getEarGain() * 2), AudioTrack.getMaxVolume() * org.sipdroid.sipua.ui.Settings.getEarGain() * 2); if (gain == 0 || ogain <= 1) gain = ogain; break; case AudioManager.MODE_NORMAL: track.setStereoVolume(AudioTrack.getMaxVolume(), AudioTrack.getMaxVolume()); break; } setStreamVolume( stream(), PreferenceManager.getDefaultSharedPreferences(Receiver.mContext) .getInt( "volume" + speakermode, am.getStreamMaxVolume(stream()) * (speakermode == AudioManager.MODE_NORMAL ? 4 : 3) / 4), 0); }
public static synchronized void ringback(boolean ringback) { if (ringback && ringbackPlayer == null) { AudioManager am = (AudioManager) Receiver.mContext.getSystemService(Context.AUDIO_SERVICE); oldvol = am.getStreamVolume(AudioManager.STREAM_MUSIC); setMode(speakermode); enableBluetooth( PreferenceManager.getDefaultSharedPreferences(Receiver.mContext) .getBoolean( org.sipdroid.sipua.ui.Settings.PREF_BLUETOOTH, org.sipdroid.sipua.ui.Settings.DEFAULT_BLUETOOTH)); am.setStreamVolume( stream(), PreferenceManager.getDefaultSharedPreferences(Receiver.mContext) .getInt( "volume" + speakermode, am.getStreamMaxVolume(stream()) * (speakermode == AudioManager.MODE_NORMAL ? 4 : 3) / 4), 0); ringbackPlayer = new ToneGenerator( stream(), (int) (ToneGenerator.MAX_VOLUME * 2 * org.sipdroid.sipua.ui.Settings.getEarGain())); ringbackPlayer.startTone(ToneGenerator.TONE_SUP_RINGTONE); } else if (!ringback && ringbackPlayer != null) { ringbackPlayer.stopTone(); ringbackPlayer.release(); ringbackPlayer = null; if (Receiver.call_state == UserAgent.UA_STATE_IDLE) { AudioManager am = (AudioManager) Receiver.mContext.getSystemService(Context.AUDIO_SERVICE); restoreMode(); enableBluetooth(false); am.setStreamVolume(AudioManager.STREAM_MUSIC, oldvol, 0); oldvol = -1; } } }
/** 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(); }