blob: 7511f9026b917293877454d222c5df0d86b7f813 [file] [log] [blame]
Neels Hofmeyr63881a32022-01-13 18:34:52 +01001/* Filter/overlay codec selections for a voice call, across MS, RAN and CN limitations */
2/*
Vadim Yanitskiy999a5932023-05-18 17:22:26 +07003 * (C) 2019-2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Neels Hofmeyr63881a32022-01-13 18:34:52 +01004 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * SPDX-License-Identifier: AGPL-3.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <osmocom/gsm/protocol/gsm_08_08.h>
25
26#include <osmocom/msc/codec_filter.h>
27#include <osmocom/msc/codec_mapping.h>
28#include <osmocom/msc/debug.h>
29
30/* Add all known payload types encountered in GSM networks */
31static void sdp_add_all_geran_codecs(struct sdp_audio_codecs *ac)
32{
33 /* In order of preference. TODO: make configurable */
34 static const enum gsm48_bcap_speech_ver mobile_codecs[] = {
35 GSM48_BCAP_SV_AMR_F /*!< 4 GSM FR V3 (FR AMR) */,
36 GSM48_BCAP_SV_AMR_H /*!< 5 GSM HR V3 (HR_AMR) */,
37 GSM48_BCAP_SV_EFR /*!< 2 GSM FR V2 (GSM EFR) */,
38 GSM48_BCAP_SV_FR /*!< 0 GSM FR V1 (GSM FR) */,
39 GSM48_BCAP_SV_HR /*!< 1 GSM HR V1 (GSM HR) */,
40 };
41 int i;
42 for (i = 0; i < ARRAY_SIZE(mobile_codecs); i++)
43 sdp_audio_codecs_add_speech_ver(ac, mobile_codecs[i]);
44}
45
46/* Add all known AMR payload types encountered in UTRAN networks */
47static void sdp_add_all_utran_codecs(struct sdp_audio_codecs *ac)
48{
49 /* In order of preference. TODO: make configurable */
50 static const enum gsm48_bcap_speech_ver utran_codecs[] = {
51 GSM48_BCAP_SV_AMR_F /*!< 4 GSM FR V3 (FR AMR) */,
52 GSM48_BCAP_SV_AMR_H /*!< 5 GSM HR V3 (HR_AMR) */,
53 GSM48_BCAP_SV_AMR_OH /*!< 11 GSM HR V6 (OHR AMR) */,
54 GSM48_BCAP_SV_AMR_FW /*!< 8 GSM FR V5 (FR AMR-WB) */,
55 GSM48_BCAP_SV_AMR_OFW /*!< 6 GSM FR V4 (OFR AMR-WB) */,
56 GSM48_BCAP_SV_AMR_OHW /*!< 7 GSM HR V4 (OHR AMR-WB) */,
57 };
58 int i;
59 for (i = 0; i < ARRAY_SIZE(utran_codecs); i++)
60 sdp_audio_codecs_add_speech_ver(ac, utran_codecs[i]);
61}
62
Neels Hofmeyr63881a32022-01-13 18:34:52 +010063void codec_filter_set_ran(struct codec_filter *codec_filter, enum osmo_rat_type ran_type)
64{
65 codec_filter->ran = (struct sdp_audio_codecs){};
66
67 switch (ran_type) {
68 default:
69 case OSMO_RAT_GERAN_A:
70 sdp_add_all_geran_codecs(&codec_filter->ran);
71 break;
72
73 case OSMO_RAT_UTRAN_IU:
74 sdp_add_all_utran_codecs(&codec_filter->ran);
75 break;
76 }
77}
78
Neels Hofmeyr63881a32022-01-13 18:34:52 +010079void codec_filter_set_bss(struct codec_filter *codec_filter,
80 const struct gsm0808_speech_codec_list *codec_list_bss_supported)
81{
82 codec_filter->bss = (struct sdp_audio_codecs){};
83 if (codec_list_bss_supported)
84 sdp_audio_codecs_from_speech_codec_list(&codec_filter->bss, codec_list_bss_supported);
85}
86
Neels Hofmeyr63881a32022-01-13 18:34:52 +010087/* Render intersections of all known audio codec constraints to reach a resulting choice of favorite audio codec, plus
88 * possible set of alternative audio codecs, in codec_filter->result. (The result.rtp address remains unchanged.) */
Oliver Smithc63c3a02023-05-24 10:48:07 +020089int codec_filter_run(struct codec_filter *codec_filter, struct sdp_msg *result, const struct sdp_msg *remote)
Neels Hofmeyr63881a32022-01-13 18:34:52 +010090{
Oliver Smithc63c3a02023-05-24 10:48:07 +020091 struct sdp_audio_codecs *r = &result->audio_codecs;
Neels Hofmeyr63881a32022-01-13 18:34:52 +010092 struct sdp_audio_codec *a = &codec_filter->assignment;
93 *r = codec_filter->ran;
94 if (codec_filter->ms.count)
95 sdp_audio_codecs_intersection(r, &codec_filter->ms, false);
96 if (codec_filter->bss.count)
97 sdp_audio_codecs_intersection(r, &codec_filter->bss, false);
Oliver Smith593cd882023-05-24 10:40:19 +020098 if (remote->audio_codecs.count)
99 sdp_audio_codecs_intersection(r, &remote->audio_codecs, true);
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100100
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100101 if (sdp_audio_codec_is_set(a)) {
102 /* Assignment has completed, the chosen codec should be the first of the resulting SDP.
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100103 * If present, make sure this is listed in first place.
104 * If 'select' is NULL, the assigned codec is not present in the intersection of possible choices for
105 * TFO. Just omit the assigned codec from the filter result, and it is the CC code's responsibility to
106 * detect this and assign a working codec instead. */
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100107 struct sdp_audio_codec *select = sdp_audio_codecs_by_descr(r, a);
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100108 if (select)
109 sdp_audio_codecs_select(r, select);
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100110 }
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100111 return 0;
112}
113
Oliver Smith593cd882023-05-24 10:40:19 +0200114int codec_filter_to_str_buf(char *buf, size_t buflen, const struct codec_filter *codec_filter,
Oliver Smithc63c3a02023-05-24 10:48:07 +0200115 const struct sdp_msg *result, const struct sdp_msg *remote)
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100116{
117 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
Oliver Smithc63c3a02023-05-24 10:48:07 +0200118 OSMO_STRBUF_APPEND(sb, sdp_msg_to_str_buf, result);
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100119 OSMO_STRBUF_PRINTF(sb, " (from:");
120
121 if (sdp_audio_codec_is_set(&codec_filter->assignment)) {
122 OSMO_STRBUF_PRINTF(sb, " assigned=");
123 OSMO_STRBUF_APPEND(sb, sdp_audio_codec_to_str_buf, &codec_filter->assignment);
124 }
125
Oliver Smith593cd882023-05-24 10:40:19 +0200126 if (remote->audio_codecs.count
127 || osmo_sockaddr_str_is_nonzero(&remote->rtp)) {
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100128 OSMO_STRBUF_PRINTF(sb, " remote=");
Oliver Smith593cd882023-05-24 10:40:19 +0200129 OSMO_STRBUF_APPEND(sb, sdp_msg_to_str_buf, remote);
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100130 }
131
132 if (codec_filter->ms.count) {
133 OSMO_STRBUF_PRINTF(sb, " MS={");
134 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &codec_filter->ms);
135 OSMO_STRBUF_PRINTF(sb, "}");
136 }
137
138 if (codec_filter->bss.count) {
139 OSMO_STRBUF_PRINTF(sb, " bss={");
140 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &codec_filter->bss);
141 OSMO_STRBUF_PRINTF(sb, "}");
142 }
143
144 OSMO_STRBUF_PRINTF(sb, " RAN={");
145 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &codec_filter->ran);
146 OSMO_STRBUF_PRINTF(sb, "}");
147
148 OSMO_STRBUF_PRINTF(sb, ")");
149
150 return sb.chars_needed;
151}
152
Oliver Smithc63c3a02023-05-24 10:48:07 +0200153char *codec_filter_to_str_c(void *ctx, const struct codec_filter *codec_filter, const struct sdp_msg *result,
154 const struct sdp_msg *remote)
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100155{
Oliver Smithc63c3a02023-05-24 10:48:07 +0200156 OSMO_NAME_C_IMPL(ctx, 128, "codec_filter_to_str_c-ERROR", codec_filter_to_str_buf, codec_filter, result, remote)
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100157}
158
Oliver Smithc63c3a02023-05-24 10:48:07 +0200159const char *codec_filter_to_str(const struct codec_filter *codec_filter, const struct sdp_msg *result,
160 const struct sdp_msg *remote)
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100161{
Oliver Smithc63c3a02023-05-24 10:48:07 +0200162 return codec_filter_to_str_c(OTC_SELECT, codec_filter, result, remote);
Neels Hofmeyr63881a32022-01-13 18:34:52 +0100163}