blob: c77aeb9ced1416eaa3f1d26661ee0c9176e348c5 [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/*
3 * (C) 2019-2023 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * 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
63void codec_filter_init(struct codec_filter *codec_filter)
64{
65 *codec_filter = (struct codec_filter){};
66}
67
68void codec_filter_set_ran(struct codec_filter *codec_filter, enum osmo_rat_type ran_type)
69{
70 codec_filter->ran = (struct sdp_audio_codecs){};
71
72 switch (ran_type) {
73 default:
74 case OSMO_RAT_GERAN_A:
75 sdp_add_all_geran_codecs(&codec_filter->ran);
76 break;
77
78 case OSMO_RAT_UTRAN_IU:
79 sdp_add_all_utran_codecs(&codec_filter->ran);
80 break;
81 }
82}
83
84void codec_filter_set_ms_from_bc(struct codec_filter *codec_filter, const struct gsm_mncc_bearer_cap *ms_bearer_cap)
85{
86 codec_filter->ms = (struct sdp_audio_codecs){0};
87 if (ms_bearer_cap)
88 sdp_audio_codecs_from_bearer_cap(&codec_filter->ms, ms_bearer_cap);
89}
90
91void codec_filter_set_bss(struct codec_filter *codec_filter,
92 const struct gsm0808_speech_codec_list *codec_list_bss_supported)
93{
94 codec_filter->bss = (struct sdp_audio_codecs){};
95 if (codec_list_bss_supported)
96 sdp_audio_codecs_from_speech_codec_list(&codec_filter->bss, codec_list_bss_supported);
97}
98
99int codec_filter_set_remote(struct codec_filter *codec_filter, const char *remote_sdp)
100{
101 return sdp_msg_from_sdp_str(&codec_filter->remote, remote_sdp);
102}
103
104void codec_filter_set_local_rtp(struct codec_filter *codec_filter, const struct osmo_sockaddr_str *rtp)
105{
106 if (!rtp)
107 codec_filter->result.rtp = (struct osmo_sockaddr_str){0};
108 else
109 codec_filter->result.rtp = *rtp;
110}
111
112/* Render intersections of all known audio codec constraints to reach a resulting choice of favorite audio codec, plus
113 * possible set of alternative audio codecs, in codec_filter->result. (The result.rtp address remains unchanged.) */
114int codec_filter_run(struct codec_filter *codec_filter)
115{
116 struct sdp_audio_codecs *r = &codec_filter->result.audio_codecs;
117 struct sdp_audio_codec *a = &codec_filter->assignment;
118 *r = codec_filter->ran;
119 if (codec_filter->ms.count)
120 sdp_audio_codecs_intersection(r, &codec_filter->ms, false);
121 if (codec_filter->bss.count)
122 sdp_audio_codecs_intersection(r, &codec_filter->bss, false);
123 if (codec_filter->remote.audio_codecs.count)
124 sdp_audio_codecs_intersection(r, &codec_filter->remote.audio_codecs, true);
125
126#if 0
127 /* Future: If osmo-msc were able to trigger a re-assignment after the remote side has picked a codec mismatching
128 * the initial Assignment, then this code here would make sense: keep the other codecs as available to choose
129 * from, but put the currently assigned codec in the first position. So far we only offer the single assigned
130 * codec, because we have no way to deal with the remote side picking a different codec.
131 * Another approach would be to postpone assignment until we know the codecs from the remote side. */
132 if (sdp_audio_codec_is_set(a)) {
133 /* Assignment has completed, the chosen codec should be the first of the resulting SDP.
134 * Make sure this is actually listed in the result SDP and move to first place. */
135 struct sdp_audio_codec *select = sdp_audio_codecs_by_descr(r, a);
136
137 if (!select) {
138 /* Not present. Add. */
139 if (sdp_audio_codec_by_payload_type(r, a->payload_type, false)) {
140 /* Oh crunch, that payload type number is already in use.
141 * Find an unused one. */
142 for (a->payload_type = 96; a->payload_type <= 127; a->payload_type++) {
143 if (!sdp_audio_codec_by_payload_type(r, a->payload_type, false))
144 break;
145 }
146
147 if (a->payload_type > 127)
148 return -ENOSPC;
149 }
150 select = sdp_audio_codecs_add_copy(r, a);
151 }
152
153 sdp_audio_codecs_select(r, select);
154 }
155#else
156 /* Currently, osmo-msc does not trigger re-assignment if the remote side has picked a codec that is different
157 * from the already assigned codec.
158 * So, if locally, Assignment has already chosen a codec, this is the single definitive result to be used
159 * towards the CN. */
160 if (sdp_audio_codec_is_set(a)) {
161 /* Assignment has completed, the chosen codec should be the the only possible one. */
162 *r = (struct sdp_audio_codecs){};
163 sdp_audio_codecs_add_copy(r, a);
164 }
165#endif
166 return 0;
167}
168
169int codec_filter_to_str_buf(char *buf, size_t buflen, const struct codec_filter *codec_filter)
170{
171 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
172 OSMO_STRBUF_APPEND(sb, sdp_msg_to_str_buf, &codec_filter->result);
173 OSMO_STRBUF_PRINTF(sb, " (from:");
174
175 if (sdp_audio_codec_is_set(&codec_filter->assignment)) {
176 OSMO_STRBUF_PRINTF(sb, " assigned=");
177 OSMO_STRBUF_APPEND(sb, sdp_audio_codec_to_str_buf, &codec_filter->assignment);
178 }
179
180 if (codec_filter->remote.audio_codecs.count
181 || osmo_sockaddr_str_is_nonzero(&codec_filter->remote.rtp)) {
182 OSMO_STRBUF_PRINTF(sb, " remote=");
183 OSMO_STRBUF_APPEND(sb, sdp_msg_to_str_buf, &codec_filter->remote);
184 }
185
186 if (codec_filter->ms.count) {
187 OSMO_STRBUF_PRINTF(sb, " MS={");
188 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &codec_filter->ms);
189 OSMO_STRBUF_PRINTF(sb, "}");
190 }
191
192 if (codec_filter->bss.count) {
193 OSMO_STRBUF_PRINTF(sb, " bss={");
194 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &codec_filter->bss);
195 OSMO_STRBUF_PRINTF(sb, "}");
196 }
197
198 OSMO_STRBUF_PRINTF(sb, " RAN={");
199 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &codec_filter->ran);
200 OSMO_STRBUF_PRINTF(sb, "}");
201
202 OSMO_STRBUF_PRINTF(sb, ")");
203
204 return sb.chars_needed;
205}
206
207char *codec_filter_to_str_c(void *ctx, const struct codec_filter *codec_filter)
208{
209 OSMO_NAME_C_IMPL(ctx, 128, "codec_filter_to_str_c-ERROR", codec_filter_to_str_buf, codec_filter)
210}
211
212const char *codec_filter_to_str(const struct codec_filter *codec_filter)
213{
214 return codec_filter_to_str_c(OTC_SELECT, codec_filter);
215}