Use BaresipService to play all sounds

This commit is contained in:
Juha Heinanen
2021-10-31 17:32:54 +02:00
parent a759484226
commit 2fa07d5421
6 changed files with 41 additions and 32 deletions
+7 -28
View File
@@ -18,8 +18,6 @@ typedef struct baresip_context {
BaresipContext g_ctx; BaresipContext g_ctx;
struct play *play = NULL;
static int vprintf_null(const char *p, size_t size, void *arg) { static int vprintf_null(const char *p, size_t size, void *arg) {
(void)p; (void)p;
(void)size; (void)size;
@@ -122,10 +120,10 @@ static const char *ua_event_reg_str(enum ua_event ev)
static const char *translate_errorcode(uint16_t scode) static const char *translate_errorcode(uint16_t scode)
{ {
switch (scode) { switch (scode) {
case 404: return NULL; /* ignore */ case 404: return ""; /* ignore */
case 486: return "busy.wav"; case 486: return "busy";
case 487: return NULL; /* ignore */ case 487: return ""; /* ignore */
default: return "error.wav"; default: return "error";
} }
} }
@@ -158,13 +156,12 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
{ {
(void)arg; (void)arg;
const char *event; const char *event;
const char *tone;
char event_buf[256]; char event_buf[256];
char ua_buf[32]; char ua_buf[32];
char call_buf[32]; char call_buf[32];
int len; int len;
struct player *player = baresip_player();
LOGD("ua event (%s) %s\n", uag_event_str(ev), prm); LOGD("ua event (%s) %s\n", uag_event_str(ev), prm);
switch (ev) { switch (ev) {
@@ -179,10 +176,6 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
len = re_snprintf(event_buf, sizeof event_buf, "registering failed,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "registering failed,%s", prm);
break; break;
case UA_EVENT_CALL_INCOMING: case UA_EVENT_CALL_INCOMING:
//if (list_count(ua_calls(ua)) > 1) {
// play = mem_deref(play);
// (void)play_file(&play, player, "callwaiting.wav", 3, NULL, NULL);
//}
len = re_snprintf(event_buf, sizeof event_buf, "%s", "call incoming"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call incoming");
break; break;
case UA_EVENT_CALL_OUTGOING: case UA_EVENT_CALL_OUTGOING:
@@ -197,15 +190,12 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
len = re_snprintf(event_buf, sizeof event_buf, "call %sed", prm); len = re_snprintf(event_buf, sizeof event_buf, "call %sed", prm);
break; break;
case UA_EVENT_CALL_RINGING: case UA_EVENT_CALL_RINGING:
play = mem_deref(play);
(void)play_file(&play, player, "ringback.wav", -1, NULL, NULL);
len = re_snprintf(event_buf, sizeof event_buf, "%s", "call ringing"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call ringing");
break; break;
case UA_EVENT_CALL_PROGRESS: case UA_EVENT_CALL_PROGRESS:
len = re_snprintf(event_buf, sizeof event_buf, "%s", "call progress"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call progress");
break; break;
case UA_EVENT_CALL_ESTABLISHED: case UA_EVENT_CALL_ESTABLISHED:
play = mem_deref(play);
len = re_snprintf(event_buf, sizeof event_buf, "%s", "call established"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call established");
break; break;
case UA_EVENT_CALL_MENC: case UA_EVENT_CALL_MENC:
@@ -225,15 +215,8 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
len = re_snprintf(event_buf, sizeof event_buf, "refer failed,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "refer failed,%s", prm);
break; break;
case UA_EVENT_CALL_CLOSED: case UA_EVENT_CALL_CLOSED:
play = mem_deref(play); tone = call_scode(call) ? translate_errorcode(call_scode(call)) : "";
if (call_scode(call)) { len = re_snprintf(event_buf, sizeof event_buf, "call closed,%s,%s", prm, tone);
const char *tone;
tone = translate_errorcode(call_scode(call));
if (tone) {
(void)play_file(&play, player, tone, 1, NULL, NULL);
}
}
len = re_snprintf(event_buf, sizeof event_buf, "call closed,%s", prm);
break; break;
case UA_EVENT_MWI_NOTIFY: case UA_EVENT_MWI_NOTIFY:
len = re_snprintf(event_buf, sizeof event_buf, "mwi notify,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "mwi notify,%s", prm);
@@ -500,8 +483,6 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
// net_debug_log(); // net_debug_log();
play_set_path(baresip_player(), path);
err = ua_init("baresip v" BARESIP_VERSION " (" ARCH "/" OS ")", err = ua_init("baresip v" BARESIP_VERSION " (" ARCH "/" OS ")",
true, true, true); true, true, true);
if (err) { if (err) {
@@ -571,7 +552,6 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
LOGI("main loop exit\n"); LOGI("main loop exit\n");
} }
play = mem_deref(play);
mq = mem_deref(mq); mq = mem_deref(mq);
LOGD("closing ..."); LOGD("closing ...");
@@ -1299,7 +1279,6 @@ Java_com_tutpro_baresip_Api_ua_1answer(JNIEnv *env, jobject thiz, jstring javaUA
LOGD("answering call %s/%s\n", native_ua, native_call); LOGD("answering call %s/%s\n", native_ua, native_call);
struct ua *ua = (struct ua *) strtoul(native_ua, NULL, 10); struct ua *ua = (struct ua *) strtoul(native_ua, NULL, 10);
struct call *call = (struct call *) strtoul(native_call, NULL, 10); struct call *call = (struct call *) strtoul(native_call, NULL, 10);
play = mem_deref(play);
re_thread_enter(); re_thread_enter();
ua_answer(ua, call, (enum vidmode)javaVidMode); ua_answer(ua, call, (enum vidmode)javaVidMode);
re_thread_leave(); re_thread_leave();
@@ -34,6 +34,7 @@ import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import kotlin.concurrent.schedule import kotlin.concurrent.schedule
import kotlin.math.roundToInt import kotlin.math.roundToInt
import android.media.MediaPlayer
class BaresipService: Service() { class BaresipService: Service() {
@@ -62,6 +63,7 @@ class BaresipService: Service() {
private var activeNetwork: Network? = null private var activeNetwork: Network? = null
private var hotSpotIsEnabled = false private var hotSpotIsEnabled = false
private var hotSpotAddresses = mapOf<String, String>() private var hotSpotAddresses = mapOf<String, String>()
private var mediaPlayer: MediaPlayer? = null
@SuppressLint("WakelockTimeout") @SuppressLint("WakelockTimeout")
override fun onCreate() { override fun onCreate() {
@@ -300,10 +302,7 @@ class BaresipService: Service() {
updateDnsServers() updateDnsServers()
val assets = arrayOf( val assets = arrayOf("accounts", "config", "contacts")
"accounts", "config", "contacts", "busy.wav", "callwaiting.wav",
"error.wav", "ringback.wav"
)
var file = File(filesPath) var file = File(filesPath)
if (!file.exists()) { if (!file.exists()) {
Log.d(TAG, "Creating baresip directory") Log.d(TAG, "Creating baresip directory")
@@ -524,6 +523,11 @@ class BaresipService: Service() {
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION, requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION,
AudioAttributes.CONTENT_TYPE_SPEECH) AudioAttributes.CONTENT_TYPE_SPEECH)
setCallVolume() setCallVolume()
if (mediaPlayer == null ) {
mediaPlayer = MediaPlayer.create(this, R.raw.ringback)
mediaPlayer!!.isLooping = true
mediaPlayer!!.start()
}
return return
} }
"call incoming" -> { "call incoming" -> {
@@ -534,6 +538,7 @@ class BaresipService: Service() {
Manifest.permission.RECORD_AUDIO)) { Manifest.permission.RECORD_AUDIO)) {
Log.d(TAG, "Auto-rejecting incoming call $uap/$callp/$peerUri") Log.d(TAG, "Auto-rejecting incoming call $uap/$callp/$peerUri")
Api.ua_hangup(uap, callp, 486, "Busy Here") Api.ua_hangup(uap, callp, 486, "Busy Here")
playNTimes(R.raw.callwaiting, 1)
if (ua.account.callHistory) { if (ua.account.callHistory) {
CallHistory.add(CallHistory(aor, peerUri, "in", false)) CallHistory.add(CallHistory(aor, peerUri, "in", false))
CallHistory.save() CallHistory.save()
@@ -634,6 +639,7 @@ class BaresipService: Service() {
} }
"call answered" -> { "call answered" -> {
stopRinging() stopRinging()
stopMediaPlayer()
am.mode = AudioManager.MODE_IN_COMMUNICATION am.mode = AudioManager.MODE_IN_COMMUNICATION
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION, requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION,
AudioAttributes.CONTENT_TYPE_SPEECH) AudioAttributes.CONTENT_TYPE_SPEECH)
@@ -739,6 +745,13 @@ class BaresipService: Service() {
} }
Log.d(TAG, "AoR $aor call $callp is closed") Log.d(TAG, "AoR $aor call $callp is closed")
stopRinging() stopRinging()
stopMediaPlayer()
when (ev[2]) {
"busy" ->
playNTimes(R.raw.busy, 1)
"error" ->
playNTimes(R.raw.error, 1)
}
call.remove() call.remove()
if (Call.calls().size == 0) { if (Call.calls().size == 0) {
resetCallVolume() resetCallVolume()
@@ -1133,6 +1146,23 @@ class BaresipService: Service() {
rt.stop() rt.stop()
} }
private fun playNTimes(raw: Int, count: Int) {
if (mediaPlayer == null ) {
mediaPlayer = MediaPlayer.create(this, raw)
mediaPlayer!!.setOnCompletionListener {
stopMediaPlayer()
if (count > 1) playNTimes(raw, count - 1)
}
mediaPlayer!!.start()
}
}
private fun stopMediaPlayer() {
mediaPlayer?.stop()
mediaPlayer?.release()
mediaPlayer = null
}
private fun setCallVolume() { private fun setCallVolume() {
if (callVolume != 0 && origVolume.isEmpty()) { if (callVolume != 0 && origVolume.isEmpty()) {
for (streamType in listOf(AudioManager.STREAM_MUSIC, AudioManager.STREAM_VOICE_CALL)) { for (streamType in listOf(AudioManager.STREAM_MUSIC, AudioManager.STREAM_VOICE_CALL)) {