From 8fcae4873563a0cda8be60ca0ab6908f37a89eb4 Mon Sep 17 00:00:00 2001
From: Grzegorz Sluja <grzegorz.sluja@iopsys.eu>
Date: Mon, 29 Jul 2024 15:22:58 +0200
Subject: [PATCH] Process the internal call codec update the same as for
 external call

Sometimes the bridged_chan is not yet available during the
chan_voicemngr_modify_codec() and for internal call there is a
possibility that one side  is setting configured codec (g722 for
example) while other side which does not have yet bridged_chan available
use alaw. To prevent this codec mismatch, do not set alaw codec for
internal as the configured codec is always available for both sides.
---
 src/channels/chan_voicemngr.c | 34 +++++-----------------------------
 1 file changed, 5 insertions(+), 29 deletions(-)

diff --git a/src/channels/chan_voicemngr.c b/src/channels/chan_voicemngr.c
index 24162ff..9336c72 100644
--- a/src/channels/chan_voicemngr.c
+++ b/src/channels/chan_voicemngr.c
@@ -795,35 +795,11 @@ static void chan_voicemngr_modify_codec(struct chan_voicemngr_subchannel *sub) {
 			ast_channel_lock(sub->owner);
 			ast_channel_ptime_set(sub->owner, ast_channel_ptime_get(bridged_chan) ? ast_channel_ptime_get(bridged_chan) : default_ptime);
 			ast_channel_unlock(sub->owner);
-			if (strncmp(ast_channel_name(bridged_chan), "TELCHAN", 5) == 0 ) {
-				// local chan, internal call, using alaw.
-				ast_log(LOG_NOTICE, "INTERNAL CALL, %s\n", ast_channel_name(bridged_chan));
-				ast_channel_lock(sub->owner);
-				ast_channel_codec_set(sub->owner, "alaw");
-				ast_channel_unlock(sub->owner);
-				ast_channel_lock(bridged_chan);
-				ast_channel_codec_set(bridged_chan, "alaw");
-				ast_channel_unlock(bridged_chan);
-				// set sip client to 0 for internal call
-				sub->sip_client_id = 0;
-				struct ast_format_cap *caps;
-				caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
-				if (caps) {
-					ast_format_cap_append(caps, map_rtpname_to_format("alaw"), 0);
-					ast_channel_lock(bridged_chan);
-					ast_channel_nativeformats_set(bridged_chan, caps);
-					ast_channel_unlock(bridged_chan);
-					ao2_ref(caps, -1);
-				} else {
-					ao2_cleanup(caps);
-				}
-			} else {
 			// get codec from bridged_chan's writeformat if not set
-				ast_channel_lock(sub->owner);
-				ast_channel_codec_set(sub->owner, !ast_strlen_zero(ast_channel_codec_get(bridged_chan)) ?
-					ast_channel_codec_get(bridged_chan) : ast_format_get_name(ast_channel_writeformat(bridged_chan)));
-				ast_channel_unlock(sub->owner);
-			}
+			ast_channel_lock(sub->owner);
+			ast_channel_codec_set(sub->owner, !ast_strlen_zero(ast_channel_codec_get(bridged_chan)) ?
+				ast_channel_codec_get(bridged_chan) : ast_format_get_name(ast_channel_writeformat(bridged_chan)));
+			ast_channel_unlock(sub->owner);
 			ast_channel_unref(bridged_chan);
 		} else if (ast_strlen_zero(ast_channel_codec_get(sub->owner))) {
 			//return if has no codec set
@@ -860,7 +836,7 @@ static void chan_voicemngr_modify_codec(struct chan_voicemngr_subchannel *sub) {
 		data.mask = data.mask|UBUS_DATA_PTIME_BIT;
 		/* push parm_update to voicemngr */
 		endpt_connection_data(sub->parent->line_id, sub->call_id, "parm_update", &data);
-		ast_log(LOG_NOTICE, "connection parm_update, codec: %d, ptime: %d",data.codec, data.ptime);
+		ast_log(LOG_NOTICE, "connection parm_update, codec: %d, ptime: %d\n",data.codec, data.ptime);
 
 		if (data.mask & UBUS_DATA_CODEC_BIT) {
 			sub->updated_codec = 1;
-- 
GitLab