Audio related fixes

This commit is contained in:
Juha Heinanen
2026-05-02 08:52:27 +03:00
parent c309b6cb03
commit 676e8491d4
2 changed files with 31 additions and 47 deletions
@@ -882,24 +882,25 @@ class BaresipService: Service() {
stopMediaPlayer() stopMediaPlayer()
val hasTelecom = ConnectionService.connections.containsKey(callp) || val hasTelecom = ConnectionService.connections.containsKey(callp) ||
ConnectionService.pendingOutgoingConnection != null ConnectionService.pendingOutgoingConnection != null
if (!hasTelecom) if (!hasTelecom) {
setCallVolume() setCallVolume()
ensureCommunicationMode() ensureCommunicationMode()
}
proximitySensing(proximitySensing) proximitySensing(proximitySensing)
} }
"call ringing" -> { "call ringing" -> {
ConnectionService.connections[callp]?.setRinging() if (!telecom) {
ensureCommunicationMode() ensureCommunicationMode()
playRingBack() playRingBack()
}
return return
} }
"call progress" -> { "call progress" -> {
ensureCommunicationMode() if (!telecom) ensureCommunicationMode()
if ((ev[1].toInt() and Api.SDP_RECVONLY) != 0) if ((ev[1].toInt() and Api.SDP_RECVONLY) != 0)
stopMediaPlayer() stopMediaPlayer()
else { else {
ConnectionService.connections[callp]?.setRinging() if (!telecom) playRingBack()
playRingBack()
} }
return return
} }
@@ -939,7 +940,7 @@ class BaresipService: Service() {
) )
if (resourceId != 0) { if (resourceId != 0) {
ensureCommunicationMode() ensureCommunicationMode()
playUnInterrupted(resourceId, 1) if (!telecom) playUnInterrupted(resourceId, 1)
} else { } else {
Log.e(TAG, "Callwaiting tone $name.wav not found") Log.e(TAG, "Callwaiting tone $name.wav not found")
} }
@@ -993,6 +994,7 @@ class BaresipService: Service() {
"call established" -> { "call established" -> {
ConnectionService.connections[callp]?.setActive() ConnectionService.connections[callp]?.setActive()
ensureCommunicationMode() ensureCommunicationMode()
stopMediaPlayer()
nm.cancel(CALL_NOTIFICATION_ID) nm.cancel(CALL_NOTIFICATION_ID)
Log.d(TAG, "AoR $aor call $callp established") Log.d(TAG, "AoR $aor call $callp established")
call!!.status.value = "connected" call!!.status.value = "connected"
@@ -1628,9 +1630,8 @@ class BaresipService: Service() {
.setCustomContentView(notificationLayout) .setCustomContentView(notificationLayout)
val notification = buildStatusNotification() val notification = buildStatusNotification()
try { try {
if (VERSION.SDK_INT >= 34) if (VERSION.SDK_INT >= 29)
startForeground(STATUS_NOTIFICATION_ID, notification, startForeground(STATUS_NOTIFICATION_ID, notification, foregroundServiceType())
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
else else
startForeground(STATUS_NOTIFICATION_ID, notification) startForeground(STATUS_NOTIFICATION_ID, notification)
} catch (e: Exception) { } catch (e: Exception) {
@@ -1728,14 +1729,8 @@ class BaresipService: Service() {
try { try {
if (activeCall != null) { if (activeCall != null) {
val type = if (VERSION.SDK_INT >= 30) {
if (ContextCompat.checkSelfPermission(this, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
FOREGROUND_SERVICE_TYPE_PHONE_CALL or FOREGROUND_SERVICE_TYPE_MICROPHONE
else
FOREGROUND_SERVICE_TYPE_PHONE_CALL
} else 0
if (VERSION.SDK_INT >= 29) if (VERSION.SDK_INT >= 29)
startForeground(STATUS_NOTIFICATION_ID, notification, type) startForeground(STATUS_NOTIFICATION_ID, notification, foregroundServiceType())
else else
startForeground(STATUS_NOTIFICATION_ID, notification) startForeground(STATUS_NOTIFICATION_ID, notification)
isNotificationInCall = true isNotificationInCall = true
@@ -1743,11 +1738,8 @@ class BaresipService: Service() {
if (isNotificationInCall) { if (isNotificationInCall) {
stopForeground(STOP_FOREGROUND_REMOVE) stopForeground(STOP_FOREGROUND_REMOVE)
isNotificationInCall = false isNotificationInCall = false
if (VERSION.SDK_INT >= 34) if (VERSION.SDK_INT >= 29)
startForeground(STATUS_NOTIFICATION_ID, notification, startForeground(STATUS_NOTIFICATION_ID, notification, foregroundServiceType())
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
else if (VERSION.SDK_INT >= 30)
startForeground(STATUS_NOTIFICATION_ID, notification, 0)
else else
startForeground(STATUS_NOTIFICATION_ID, notification) startForeground(STATUS_NOTIFICATION_ID, notification)
} else { } else {
@@ -1762,6 +1754,19 @@ class BaresipService: Service() {
} }
} }
private fun foregroundServiceType(): Int {
return if (VERSION.SDK_INT >= 34) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
} else if (VERSION.SDK_INT >= 30) {
if (ContextCompat.checkSelfPermission(this, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
FOREGROUND_SERVICE_TYPE_PHONE_CALL or FOREGROUND_SERVICE_TYPE_MICROPHONE
else
FOREGROUND_SERVICE_TYPE_PHONE_CALL
} else {
0
}
}
@SuppressLint("WakelockTimeout") @SuppressLint("WakelockTimeout")
private fun updatePartialWakeLock() { private fun updatePartialWakeLock() {
// Hold the wake lock as long as the service is active to ensure // Hold the wake lock as long as the service is active to ensure
@@ -69,6 +69,7 @@ class ConnectionService : ConnectionService() {
Connection.CAPABILITY_HOLD or Connection.CAPABILITY_HOLD or
Connection.CAPABILITY_MERGE_CONFERENCE or Connection.CAPABILITY_MERGE_CONFERENCE or
Connection.CAPABILITY_SWAP_CONFERENCE Connection.CAPABILITY_SWAP_CONFERENCE
connection.audioModeIsVoip = true
val call = Call.ofCallp(callp) val call = Call.ofCallp(callp)
if (call != null) if (call != null)
@@ -76,17 +77,6 @@ class ConnectionService : ConnectionService() {
val ua = UserAgent.ofUap(uap) val ua = UserAgent.ofUap(uap)
if (ua != null) { if (ua != null) {
if (BaresipService.speakerPhone || BaresipService.speakerPhoneAuto) {
BaresipService.speakerPhone = true
@Suppress("DEPRECATION")
connection.setAudioRoute(CallAudioState.ROUTE_SPEAKER)
BaresipService.postServiceEvent(
ServiceEvent("speaker update,true",
arrayListOf(uap, callp),
System.nanoTime()
)
)
}
if (ua.account.answerMode == Api.ANSWERMODE_AUTO) { if (ua.account.answerMode == Api.ANSWERMODE_AUTO) {
Log.d(TAG, "Auto-answering call $callp") Log.d(TAG, "Auto-answering call $callp")
connection.onAnswer() connection.onAnswer()
@@ -128,23 +118,12 @@ class ConnectionService : ConnectionService() {
val connection = BaresipConnection(uap, 0L) val connection = BaresipConnection(uap, 0L)
pendingOutgoingConnection = connection pendingOutgoingConnection = connection
if (BaresipService.speakerPhone || BaresipService.speakerPhoneAuto) {
BaresipService.speakerPhone = true
@Suppress("DEPRECATION")
connection.setAudioRoute(CallAudioState.ROUTE_SPEAKER)
BaresipService.postServiceEvent(
ServiceEvent("speaker update,true",
arrayListOf(uap, 0L),
System.nanoTime()
)
)
}
connection.setAddress(request?.address, TelecomManager.PRESENTATION_ALLOWED) connection.setAddress(request?.address, TelecomManager.PRESENTATION_ALLOWED)
connection.connectionCapabilities = Connection.CAPABILITY_SUPPORT_HOLD or connection.connectionCapabilities = Connection.CAPABILITY_SUPPORT_HOLD or
Connection.CAPABILITY_HOLD or Connection.CAPABILITY_HOLD or
Connection.CAPABILITY_MERGE_CONFERENCE or Connection.CAPABILITY_MERGE_CONFERENCE or
Connection.CAPABILITY_SWAP_CONFERENCE Connection.CAPABILITY_SWAP_CONFERENCE
connection.audioModeIsVoip = true
// Start the SIP connection logic // Start the SIP connection logic
if (uap != 0L) { if (uap != 0L) {