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

View File

@ -18,8 +18,6 @@ typedef struct baresip_context {
BaresipContext g_ctx;
struct play *play = NULL;
static int vprintf_null(const char *p, size_t size, void *arg) {
(void)p;
(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)
{
switch (scode) {
case 404: return NULL; /* ignore */
case 486: return "busy.wav";
case 487: return NULL; /* ignore */
default: return "error.wav";
case 404: return ""; /* ignore */
case 486: return "busy";
case 487: return ""; /* ignore */
default: return "error";
}
}
@ -158,13 +156,12 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
{
(void)arg;
const char *event;
const char *tone;
char event_buf[256];
char ua_buf[32];
char call_buf[32];
int len;
struct player *player = baresip_player();
LOGD("ua event (%s) %s\n", uag_event_str(ev), prm);
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);
break;
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");
break;
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);
break;
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");
break;
case UA_EVENT_CALL_PROGRESS:
len = re_snprintf(event_buf, sizeof event_buf, "%s", "call progress");
break;
case UA_EVENT_CALL_ESTABLISHED:
play = mem_deref(play);
len = re_snprintf(event_buf, sizeof event_buf, "%s", "call established");
break;
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);
break;
case UA_EVENT_CALL_CLOSED:
play = mem_deref(play);
if (call_scode(call)) {
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);
tone = call_scode(call) ? translate_errorcode(call_scode(call)) : "";
len = re_snprintf(event_buf, sizeof event_buf, "call closed,%s,%s", prm, tone);
break;
case UA_EVENT_MWI_NOTIFY:
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();
play_set_path(baresip_player(), path);
err = ua_init("baresip v" BARESIP_VERSION " (" ARCH "/" OS ")",
true, true, true);
if (err) {
@ -571,7 +552,6 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
LOGI("main loop exit\n");
}
play = mem_deref(play);
mq = mem_deref(mq);
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);
struct ua *ua = (struct ua *) strtoul(native_ua, NULL, 10);
struct call *call = (struct call *) strtoul(native_call, NULL, 10);
play = mem_deref(play);
re_thread_enter();
ua_answer(ua, call, (enum vidmode)javaVidMode);
re_thread_leave();

View File

@ -34,6 +34,7 @@ import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.schedule
import kotlin.math.roundToInt
import android.media.MediaPlayer
class BaresipService: Service() {
@ -62,6 +63,7 @@ class BaresipService: Service() {
private var activeNetwork: Network? = null
private var hotSpotIsEnabled = false
private var hotSpotAddresses = mapOf<String, String>()
private var mediaPlayer: MediaPlayer? = null
@SuppressLint("WakelockTimeout")
override fun onCreate() {
@ -300,10 +302,7 @@ class BaresipService: Service() {
updateDnsServers()
val assets = arrayOf(
"accounts", "config", "contacts", "busy.wav", "callwaiting.wav",
"error.wav", "ringback.wav"
)
val assets = arrayOf("accounts", "config", "contacts")
var file = File(filesPath)
if (!file.exists()) {
Log.d(TAG, "Creating baresip directory")
@ -524,6 +523,11 @@ class BaresipService: Service() {
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION,
AudioAttributes.CONTENT_TYPE_SPEECH)
setCallVolume()
if (mediaPlayer == null ) {
mediaPlayer = MediaPlayer.create(this, R.raw.ringback)
mediaPlayer!!.isLooping = true
mediaPlayer!!.start()
}
return
}
"call incoming" -> {
@ -534,6 +538,7 @@ class BaresipService: Service() {
Manifest.permission.RECORD_AUDIO)) {
Log.d(TAG, "Auto-rejecting incoming call $uap/$callp/$peerUri")
Api.ua_hangup(uap, callp, 486, "Busy Here")
playNTimes(R.raw.callwaiting, 1)
if (ua.account.callHistory) {
CallHistory.add(CallHistory(aor, peerUri, "in", false))
CallHistory.save()
@ -634,6 +639,7 @@ class BaresipService: Service() {
}
"call answered" -> {
stopRinging()
stopMediaPlayer()
am.mode = AudioManager.MODE_IN_COMMUNICATION
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION,
AudioAttributes.CONTENT_TYPE_SPEECH)
@ -739,6 +745,13 @@ class BaresipService: Service() {
}
Log.d(TAG, "AoR $aor call $callp is closed")
stopRinging()
stopMediaPlayer()
when (ev[2]) {
"busy" ->
playNTimes(R.raw.busy, 1)
"error" ->
playNTimes(R.raw.error, 1)
}
call.remove()
if (Call.calls().size == 0) {
resetCallVolume()
@ -1133,6 +1146,23 @@ class BaresipService: Service() {
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() {
if (callVolume != 0 && origVolume.isEmpty()) {
for (streamType in listOf(AudioManager.STREAM_MUSIC, AudioManager.STREAM_VOICE_CALL)) {