@Override public void onDestroy() { Log.w(TAG, "Destroyed Location update Service"); toneGenerator.release(); unregisterActionReceiver(); cleanUp(); stopForeground(true); super.onDestroy(); }
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(); }
@Override public synchronized void onPause() { super.onPause(); mToneGenerator.release(); }
@Override public void run() { Log.d(THIS_FILE, "InCallTonePlayer.run(toneId = " + mToneId + ")..."); int toneType; // passed to ToneGenerator.startTone() int toneVolume; // passed to the ToneGenerator constructor int toneLengthMillis; switch (mToneId) { case TONE_CALL_WAITING: toneType = ToneGenerator.TONE_SUP_CALL_WAITING; toneVolume = TONE_RELATIVE_VOLUME_HIPRI; toneLengthMillis = 5000; break; case TONE_BUSY: toneType = ToneGenerator.TONE_SUP_BUSY; toneVolume = TONE_RELATIVE_VOLUME_HIPRI; toneLengthMillis = 4000; break; case TONE_CONGESTION: toneType = ToneGenerator.TONE_SUP_CONGESTION; toneVolume = TONE_RELATIVE_VOLUME_HIPRI; toneLengthMillis = 4000; break; case TONE_BATTERY_LOW: // For now, use ToneGenerator.TONE_PROP_ACK (two quick // beeps). TODO: is there some other ToneGenerator // tone that would be more appropriate here? Or // should we consider adding a new custom tone? toneType = ToneGenerator.TONE_PROP_ACK; toneVolume = TONE_RELATIVE_VOLUME_HIPRI; toneLengthMillis = 1000; break; case TONE_CALL_ENDED: toneType = ToneGenerator.TONE_PROP_PROMPT; toneVolume = TONE_RELATIVE_VOLUME_LOPRI; toneLengthMillis = 2000; break; default: throw new IllegalArgumentException("Bad toneId: " + mToneId); } // If the mToneGenerator creation fails, just continue without it. // It is // a local audio signal, and is not as important. ToneGenerator toneGenerator; try { toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, toneVolume); // if (DBG) log("- created toneGenerator: " + toneGenerator); } catch (RuntimeException e) { Log.w(THIS_FILE, "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e); toneGenerator = null; } // Using the ToneGenerator (with the CALL_WAITING / BUSY / // CONGESTION tones at least), the ToneGenerator itself knows // the right pattern of tones to play; we do NOT need to // manually start/stop each individual tone, or manually // insert the correct delay between tones. (We just start it // and let it run for however long we want the tone pattern to // continue.) // // TODO: When we stop the ToneGenerator in the middle of a // "tone pattern", it sounds bad if we cut if off while the // tone is actually playing. Consider adding API to the // ToneGenerator to say "stop at the next silent part of the // pattern", or simply "play the pattern N times and then // stop." if (toneGenerator != null) { toneGenerator.startTone(toneType); SystemClock.sleep(toneLengthMillis); toneGenerator.stopTone(); Log.v(THIS_FILE, "- InCallTonePlayer: done playing."); toneGenerator.release(); } }
/** Set the audio mode as in call */ @SuppressWarnings("deprecation") private synchronized void actualSetAudioInCall() { // Ensure not already set if (isSetAudioMode) { return; } stopRing(); saveAudioState(); // Set the rest of the phone in a better state to not interferate with // current call // Do that only if we were not already in silent mode /* * Not needed anymore with on flight gsm call capture * if(audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) { * audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, * AudioManager.VIBRATE_SETTING_ON); * audioManager.setVibrateSetting(AudioManager * .VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); * audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); } */ // LOCKS // Wifi management if necessary ContentResolver ctntResolver = service.getContentResolver(); Settings.System.putInt( ctntResolver, Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER); // Acquire wifi lock WifiManager wman = (WifiManager) service.getSystemService(Context.WIFI_SERVICE); if (wifiLock == null) { wifiLock = wman.createWifiLock( (Compatibility.isCompatible(9)) ? WifiManager.WIFI_MODE_FULL_HIGH_PERF : WifiManager.WIFI_MODE_FULL, "com.csipsimple.InCallLock"); wifiLock.setReferenceCounted(false); } WifiInfo winfo = wman.getConnectionInfo(); if (winfo != null) { DetailedState dstate = WifiInfo.getDetailedStateOf(winfo.getSupplicantState()); // We assume that if obtaining ip addr, we are almost connected so // can keep wifi lock if (dstate == DetailedState.OBTAINING_IPADDR || dstate == DetailedState.CONNECTED) { if (!wifiLock.isHeld()) { wifiLock.acquire(); } } // This wake lock purpose is to prevent PSP wifi mode if (service.getPrefs().getPreferenceBooleanValue(SipConfigManager.KEEP_AWAKE_IN_CALL)) { if (screenLock == null) { PowerManager pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE); screenLock = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "com.csipsimple.onIncomingCall.SCREEN"); screenLock.setReferenceCounted(false); } // Ensure single lock if (!screenLock.isHeld()) { screenLock.acquire(); } } } if (!useWebRTCImpl) { // Audio routing int targetMode = getAudioTargetMode(); Log.d(THIS_FILE, "Set mode audio in call to " + targetMode); if (service.getPrefs().generateForSetCall()) { boolean needOutOfSilent = (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT); if (needOutOfSilent) { audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); } ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, 1); toneGenerator.startTone(41 /* ToneGenerator.TONE_CDMA_CONFIRM */); toneGenerator.stopTone(); toneGenerator.release(); // Restore silent mode if (needOutOfSilent) { audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); } } // Set mode if (targetMode != AudioManager.MODE_IN_CALL && useSgsWrkAround) { // For galaxy S we need to set in call mode before to reset // stack audioManager.setMode(AudioManager.MODE_IN_CALL); } audioManager.setMode(targetMode); // Routing if (service.getPrefs().useRoutingApi()) { audioManager.setRouting( targetMode, userWantSpeaker ? AudioManager.ROUTE_SPEAKER : AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); } else { audioManager.setSpeakerphoneOn(userWantSpeaker ? true : false); } audioManager.setMicrophoneMute(false); if (bluetoothWrapper != null && userWantBluetooth && bluetoothWrapper.canBluetooth()) { Log.d(THIS_FILE, "Try to enable bluetooth"); bluetoothWrapper.setBluetoothOn(true); } } else { // WebRTC implementation for routing int apiLevel = Compatibility.getApiLevel(); // SetAudioMode // ***IMPORTANT*** When the API level for honeycomb (H) has been // decided, // the condition should be changed to include API level 8 to H-1. if (android.os.Build.BRAND.equalsIgnoreCase("Samsung") && (8 == apiLevel)) { // Set Samsung specific VoIP mode for 2.2 devices int mode = 4; audioManager.setMode(mode); if (audioManager.getMode() != mode) { Log.e(THIS_FILE, "Could not set audio mode for Samsung device"); } } // SetPlayoutSpeaker if ((3 == apiLevel) || (4 == apiLevel)) { // 1.5 and 1.6 devices if (userWantSpeaker) { // route audio to back speaker audioManager.setMode(AudioManager.MODE_NORMAL); } else { // route audio to earpiece audioManager.setMode(AudioManager.MODE_IN_CALL); } } else { // 2.x devices if ((android.os.Build.BRAND.equalsIgnoreCase("samsung")) && ((5 == apiLevel) || (6 == apiLevel) || (7 == apiLevel))) { // Samsung 2.0, 2.0.1 and 2.1 devices if (userWantSpeaker) { // route audio to back speaker audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.setSpeakerphoneOn(userWantSpeaker); } else { // route audio to earpiece audioManager.setSpeakerphoneOn(userWantSpeaker); audioManager.setMode(AudioManager.MODE_NORMAL); } } else { // Non-Samsung and Samsung 2.2 and up devices audioManager.setSpeakerphoneOn(userWantSpeaker); } } } // Set stream solo/volume/focus int inCallStream = Compatibility.getInCallStream(userWantBluetooth); if (doFocusAudio) { if (!accessibilityManager.isEnabled()) { audioManager.setStreamSolo(inCallStream, true); } audioFocusWrapper.focus(userWantBluetooth); } Log.d(THIS_FILE, "Initial volume level : " + service.getPrefs().getInitialVolumeLevel()); setStreamVolume( inCallStream, (int) (audioManager.getStreamMaxVolume(inCallStream) * service.getPrefs().getInitialVolumeLevel()), 0); isSetAudioMode = true; // System.gc(); }
public void onDestroy() { toneGenerator.release(); }