From 469efd0af395c3f86c60a9d6c24be7b57842e3a3 Mon Sep 17 00:00:00 2001
From: George Yang <g.yang@genexis.eu>
Date: Tue, 23 Jan 2024 10:56:41 +0000
Subject: [PATCH] Fix ast_channel references for call transfer

It caused the pjsip channel not released after attended call transfer before this fix.
---
 src/channels/chan_voicemngr.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/channels/chan_voicemngr.c b/src/channels/chan_voicemngr.c
index ecb496b..fe1bf68 100644
--- a/src/channels/chan_voicemngr.c
+++ b/src/channels/chan_voicemngr.c
@@ -802,7 +802,7 @@ static void chan_voicemngr_modify_codec(struct chan_voicemngr_subchannel *sub) {
 			    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)));
 			}
-			ao2_ref(bridged_chan, -1);
+			ast_channel_unref(bridged_chan);
 		} else if (ast_strlen_zero(ast_channel_codec_get(sub->owner))) {
 			//return if has no codec set
 			ast_channel_unlock(sub->owner);
@@ -929,6 +929,7 @@ static int chan_voicemngr_indicate(struct ast_channel *ast, int condition, const
 						ast_log(LOG_ERROR, "ast_transfer() is not supported on the peer channel\n");
 				}
 				ast_channel_unlock(bridged_chan);
+				ast_channel_unref(bridged_chan);
 			} else {
 				ast_log(LOG_ERROR, "can't get the peer channel, unattended call transfer will not be proceeded\n");
 			}
@@ -5611,6 +5612,13 @@ static void chan_voicemngr_attended_call_transfer(struct chan_voicemngr_subchann
 			} else {
 				ast_log(LOG_ERROR, "can't get the peer channel\n");
 			}
+
+			if (bridged_chan_inactive) {
+				ast_channel_unref(bridged_chan_inactive);
+			}
+			if (bridged_chan_active) {
+				ast_channel_unref(bridged_chan_active);
+			}
 		}
 	}
 	else
@@ -5921,7 +5929,7 @@ static int chan_voicemngr_get_sip_client_id(struct chan_voicemngr_subchannel *su
 			bridged_chan = ast_channel_bridge_peer(sub->owner);
 			if (bridged_chan) {
 				res = sscanf(ast_channel_name(bridged_chan), "PJSIP/sip%d", &sip_client_id);
-				ao2_ref(bridged_chan, -1);
+				ast_channel_unref(bridged_chan);
 				if (sip_client_id >= 0 && sip_client_id < MAX_SIP_CLIENTS && res == 1) {
 					sub->sip_client_id = sip_client_id;
 				}
-- 
GitLab