- Moved call related API functions to Call class.

This commit is contained in:
Juha Heinanen
2020-04-28 17:21:37 +03:00
parent ac32540a9b
commit 2d39c284b7
6 changed files with 98 additions and 33 deletions
@@ -25,16 +25,6 @@ object Api {
external fun ua_set_media_af(uap: String, af: Int)
external fun account_debug(accp: String)
external fun ua_debug(uap: String)
external fun call_hold(callp: String): Int
external fun call_unhold(callp: String): Int
external fun call_connect(callp: String, peer_uri: String): Int
external fun call_peeruri(callp: String): String
external fun call_send_digit(callp: String, digit: Char): Int
external fun call_notify_sipfrag(callp: String, code: Int, reason: String)
external fun call_start_audio(callp: String)
external fun call_stop_audio(callp: String)
external fun call_audio_codecs(callp: String): String
external fun call_status(callp: String): String
external fun message_send(uap: String, peer_uri: String, message: String, time: String): Int
external fun reload_config(): Int
external fun cmd_exec(cmd: String): Int
@@ -375,7 +375,7 @@ class BaresipService: Service() {
if (call == null)
Log.w(LOG_TAG, "onStartCommand did not find call $callp")
else
Api.call_notify_sipfrag(callp, 603, "Decline")
call.notifySipfrag(603, "Decline")
nm.cancel(TRANSFER_NOTIFICATION_ID)
}
@@ -556,7 +556,12 @@ class BaresipService: Service() {
return
}
"call incoming" -> {
val peerUri = Api.call_peeruri(callp)
val call = Call.find(callp)
if (call == null) {
Log.w(LOG_TAG, "Incoming call $callp is not found")
return
}
val peerUri = call.peerUri()
if ((Call.calls().size > 0) ||
(tm.callState != TelephonyManager.CALL_STATE_IDLE) ||
!Utils.checkPermission(applicationContext,
@@ -11,10 +11,66 @@ class Call(val callp: String, val ua: UserAgent, val peerURI: String, val dir: S
var zid = ""
var hasHistory = false
fun connect(uri: String): Int {
return call_connect(callp, uri)
}
fun startAudio() {
call_start_audio(callp)
}
fun stopAudio() {
call_stop_audio(callp)
}
fun hold(): Int {
return call_hold(callp)
}
fun unhold(): Int {
return call_unhold(callp)
}
fun sendDigit(digit: Char): Int {
return call_send_digit(callp, digit)
}
fun notifySipfrag(code: Int, reason: String) {
call_notify_sipfrag(callp, code, reason)
}
fun hasVideo(): Boolean {
return call_has_video(callp)
}
fun peerUri(): String {
return call_peeruri(callp)
}
fun status(): String {
return call_status(callp)
}
fun audioCodecs(): String {
return call_audio_codecs(callp)
}
init {
if (ua.account.mediaEnc != "") security = R.drawable.box_red
}
private external fun call_connect(callp: String, peer_uri: String): Int
private external fun call_hold(callp: String): Int
private external fun call_unhold(callp: String): Int
private external fun call_peeruri(callp: String): String
private external fun call_send_digit(callp: String, digit: Char): Int
private external fun call_notify_sipfrag(callp: String, code: Int, reason: String)
private external fun call_start_audio(callp: String)
private external fun call_stop_audio(callp: String)
private external fun call_audio_codecs(callp: String): String
private external fun call_status(callp: String): String
private external fun call_has_video(callp: String): Boolean
companion object {
fun calls(): ArrayList<Call> {
@@ -310,12 +310,12 @@ class MainActivity : AppCompatActivity() {
val call = Call.uaCalls(ua, "")[0]
if (call.onhold) {
Log.d("Baresip", "AoR $aor resuming call ${call.callp} with ${callUri.text}")
Api.call_unhold(call.callp)
call.unhold()
call.onhold = false
holdButton.setImageResource(R.drawable.pause)
} else {
Log.d("Baresip", "AoR $aor holding call ${call.callp} with ${callUri.text}")
Api.call_hold(call.callp)
call.hold()
call.onhold = true
holdButton.setImageResource(R.drawable.play)
}
@@ -325,8 +325,8 @@ class MainActivity : AppCompatActivity() {
val ua = UserAgent.uas()[aorSpinner.selectedItemPosition]
val calls = Call.uaCalls(ua, "")
if (calls.size > 0) {
val status = Api.call_status(calls[0].callp)
val codecs = Api.call_audio_codecs(calls[0].callp)
val status = calls[0].status()
val codecs = calls[0].audioCodecs()
if (status.contains('[') && status.contains(']') &&
status.contains('=') && codecs.contains(',')) {
val duration = status.split("[")[1].split("]")[0]
@@ -762,7 +762,7 @@ class MainActivity : AppCompatActivity() {
}
transferDialog.setNegativeButton(getString(R.string.no)) { dialog, _ ->
if (call in Call.calls())
Api.call_notify_sipfrag(callp, 603, "Decline")
call.notifySipfrag(603, "Decline")
dialog.dismiss()
}
transferDialog.create().show()
@@ -1220,20 +1220,20 @@ class MainActivity : AppCompatActivity() {
Call.calls().add(newCall)
Api.ua_hangup(ua.uap, call.callp, 0, "")
// Api.call_stop_audio(call.callp)
val err = Api.call_connect(newCallp, uri)
val err = newCall.connect(uri)
if (err == 0) {
Api.call_start_audio(newCallp)
newCall.startAudio()
if (ua != UserAgent.uas()[aorSpinner.selectedItemPosition])
spinToAor(ua.account.aor)
showCall(ua)
} else {
Api.call_start_audio(call.callp)
call.startAudio()
Log.e("Baresip", "call_connect $newCallp failed with error $err")
Api.call_notify_sipfrag(call.callp, 500, "Call Error")
call.notifySipfrag(500, "Call Error")
}
} else {
Log.e("Baresip", "ua_call_alloc ${ua.uap}/${call.callp} failed")
Api.call_notify_sipfrag(call.callp, 500, "Call Error")
call.notifySipfrag(500, "Call Error")
}
}
@@ -322,9 +322,14 @@ object Utils {
val text = sequence.subSequence(start, start + count).toString()
if (text.length > 0) {
val digit = text[0]
Log.d("Baresip", "Got DTMF digit '$digit'")
if (((digit >= '0') && (digit <= '9')) || (digit == '*') || (digit == '#'))
Api.call_send_digit(callp, digit)
val call = Call.find(callp)
if (call == null) {
Log.w("Baresip", "dtmfWatcher did not find call $callp")
} else {
Log.d("Baresip", "Got DTMF digit '$digit'")
if (((digit >= '0') && (digit <= '9')) || (digit == '*') || (digit == '#'))
call.sendDigit(digit)
}
}
}
override fun afterTextChanged(sequence: Editable) {