Hi Team
I would like to remove some codec for some connections between endpoints with SDOPS funktion sdp_keep_codecs_by_name()
Is there anything like an intersection function? Is there an easy way to build such a function with existing functionality?
example:
$var(src_codecs) = "PCMA,G722,telephony-event"; $var(dst_codecs) = "PCMA,G729,AMR-WB,telephony-event"; $dlg_var(codec_filter) = intersection($var(src_codecs),$var(dst_codecs));
$dlg_var(codec_filter) should become the intersection of both lists: => "PCMA,telephony-event"
In Requests and manage_reply I then could use to make sure only codecs supported by both endpoints are allowed through.
if (sdp_content()) { sdp_keep_codecs_by_name($dlg_var(codec_filter),"audio"); }
For those who are questioning, why I try to do this:
We have a mobile operator on one end and it generates a HUGE SDP with all codecs supported by the mobile devices. This sometimes leads to fragmented UDP packets either getting truncated or not getting through some customer firewall at all.
Mit freundlichen Grüssen
-Benoît Panizzon-
Is there anything like an intersection function? Is there an easy way to build such a function with existing functionality?
I came up with this, wondering if there is a more elegant solution:
$var(i) = 0; $var(A) = "PCMA,AMR-WB,telephone-event"; $var(B) = "G722,PCMA,G729,telephone-event"; $var(inters) = ""; $var(separator) = ""; while ($(var(A){s.select,$var(i),,}) != "") { if (in_list("$var(A)","$var(B)",",")) { $var(inters) = $var(inters) + $var(separator) + $var(A); $var(separator) = ","; } $var(i) = $var(i) + 1; } xlog("L_INFO", "$cfg(route): XXXXXXX DEBUG: INTERSECITON of $var(A) and $var(B) is $var(inters) \n");
Mit freundlichen Grüssen
-Benoît Panizzon-
Salut Benoit!
you could use KEMI and Javascript. Here is my 2 cents (didn't try, though...just streaming out of my head :) )
function get_codec_intersection(src, dst) { const a = new Set(src.split(",").map(s => s.trim()).filter(Boolean)); const out = []; dst.split(",").map(s => s.trim()).filter(Boolean).forEach(c => { if (a.has(c)) out.push(c); });
return out.join(","); }
Or maybe straight Kamailio scripting (not tested neither... but I use similar function/route block for other processing, just had to adapt it for your use case)
route[GET_CODEC_INTERSECTION] { # copy originals $var(src) = $var(src_codecs); $var(dst) = $var(dst_codecs); //replace comma by semicolon, less risk of parsing failure $var(src) = $(var(src){re.subst/,/;/g}); $var(dst) = $(var(dst){re.subst/,/;/g});
$var(out) = ""; $var(n) = $(var(src){param.count,;}); $var(i) = 0;
while($var(i) < $var(n)) { # Get codec name at index i from the ';' list $var(_c) = $(var(src){param.name,$var(i),;}); $var(_c) = $(var(_c){s.trim});
# If codec exists in dst list, add to output if($(var(dst){param.in,$var(_c),;}) == 1) { if($var(out) == "") { $var(out) = $var(_c); } else { $var(out) = $var(out) + "," + $var(_c); } } $var(i) = $var(i) + 1; } $dlg_var(codec_filter) = $var(out); }
I would go for the KEMI version, even if there is a slight extra runtime increase.
Atenciosamente / Kind Regards / Cordialement / Un saludo,
*Sérgio Charrua*
*www.kahea.ai http://www.kahea.ai / www.voip.pt http://www.voip.pt*
*OpenTelecom* - Consulting for Telecoms, Lda Tel.: +351 callto:+351+91+104+12+6691 631 11 44
Email : *sergio.charrua@voip.pt sergio.charrua@voip.pt*
This message and any files or documents attached are strictly confidential or otherwise legally protected.
It is intended only for the individual or entity named. If you are not the named addressee or have received this email in error, please inform the sender immediately, delete it from your system and do not copy or disclose it or its contents or use it for any purpose. Please also note that transmission cannot be guaranteed to be secure or error-free.
On Mon, Feb 16, 2026 at 2:51 PM Benoit Panizzon via sr-users < sr-users@lists.kamailio.org> wrote:
Is there anything like an intersection function? Is there an easy way to build such a function with existing functionality?
I came up with this, wondering if there is a more elegant solution:
$var(i) = 0; $var(A) = "PCMA,AMR-WB,telephone-event"; $var(B) = "G722,PCMA,G729,telephone-event"; $var(inters) = ""; $var(separator) = ""; while ($(var(A){s.select,$var(i),,}) != "") { if (in_list("$var(A)","$var(B)",",")) { $var(inters) = $var(inters) + $var(separator) + $var(A); $var(separator) = ","; } $var(i) = $var(i) + 1; } xlog("L_INFO", "$cfg(route): XXXXXXX DEBUG: INTERSECITON of $var(A) and $var(B) is $var(inters) \n");
Mit freundlichen Grüssen
-Benoît Panizzon-
I m p r o W a r e A G - Leiter Commerce Kunden ______________________________________________________
Zurlindenstrasse 29 Tel +41 61 826 93 00 CH-4133 Pratteln Fax +41 61 826 93 01 Schweiz Web http://www.imp.ch ______________________________________________________ __________________________________________________________ Kamailio - Users Mailing List - Non Commercial Discussions -- sr-users@lists.kamailio.org To unsubscribe send an email to sr-users-leave@lists.kamailio.org Important: keep the mailing list in the recipients, do not reply only to the sender!