add sdp_audio_codec_from_str(), for codec cfg

We have sdp_audio_codec_to_str() in a pretty sane and compact format,
so far for logging. It occured to me that this format is fit for VTY
config arguments.

Add a string-back-to-codec conversion.

- enable testing AMR mode-set and octet-align in msc_vlr_test_call.c, by
  giving convenient codec strings parsed by this new function.

- enable VTY config options for codecs.

Change-Id: Ibf23a8888ab79d3b10b044383cd161f1dfdf7e63
This commit is contained in:
Neels Hofmeyr 2024-02-04 02:55:46 +01:00
parent e9ab27652d
commit b26715c60d
2 changed files with 41 additions and 0 deletions

View File

@ -76,6 +76,8 @@ int sdp_audio_codec_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_
char *sdp_audio_codec_to_str_c(void *ctx, const struct sdp_audio_codec *codec);
const char *sdp_audio_codec_to_str(const struct sdp_audio_codec *codec);
int sdp_audio_codec_from_str(struct sdp_audio_codec *dst, const char *str);
int sdp_audio_codecs_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_codecs *ac);
char *sdp_audio_codecs_to_str_c(void *ctx, const struct sdp_audio_codecs *ac);
const char *sdp_audio_codecs_to_str(const struct sdp_audio_codecs *ac);

View File

@ -685,3 +685,42 @@ void sdp_audio_codecs_set_csd(struct sdp_audio_codecs *ac)
}},
};
}
static void token_copy(char *dst, size_t dst_size, const char *start, const char *end1, const char *end2)
{
const char *end;
if (end1 && end2)
end = OSMO_MIN(end1, end2);
else
end = end1 ? : end2;
if (!end)
end = start + strlen(start);
osmo_strlcpy(dst, start, OSMO_MIN(dst_size, end - start + 1));
}
#define TOKEN_COPY_ARRAY(DST, START, END1, END2) \
token_copy(DST, sizeof(DST), START, END1, END2)
/* Parse a codec string as from sdp_audio_codec_to_str_buf() back to an sdp_audio_codec struct.
* Write the parsed result to *dst.
* The input string is like <subtype_name>[:<fmtp-string>][#<payload-type-nr>],
* for example:
* "FOO:my-fmtp=val;my-other-fmtp=val2#96"
* Note that ';' are separators only within the fmtp string. This function does not separate those. In above example,
* the fmtp string part is "my-fmtp=val;my-other-fmtp=val2" and ends up in dst->ftmp as-is.
* Return 0 on success, negative on failure. */
int sdp_audio_codec_from_str(struct sdp_audio_codec *dst, const char *str)
{
const char *colon = strchr(str, ':');
const char *hash = strchr(str, '#');
*dst = (struct sdp_audio_codec){
.rate = 8000,
};
TOKEN_COPY_ARRAY(dst->subtype_name, str, colon, hash);
if (colon)
TOKEN_COPY_ARRAY(dst->fmtp, colon + 1, hash > colon ? hash : NULL, NULL);
if (hash)
dst->payload_type = atoi(hash + 1);
return 0;
}