libosmo-mgcp: cleanup audio codex alloc

No need to complicate audio codes with pointers, our "usual" string is
barely larger than a poointer, five times smaller than a talloc header,
and most importantly not really optional anyway...

Change-Id: Icc41643050a5e1ca3c66f307d60b6911ba1b8032
This commit is contained in:
Eric Wild 2021-08-09 23:26:48 +02:00
parent f936e10f07
commit 2ebcf5c34a
3 changed files with 18 additions and 22 deletions

View File

@ -81,8 +81,8 @@ struct mgcp_rtp_codec {
uint32_t frame_duration_den; uint32_t frame_duration_den;
int payload_type; int payload_type;
char *audio_name; char audio_name[64];
char *subtype_name; char subtype_name[64];
bool param_present; bool param_present;
struct mgcp_codec_param param; struct mgcp_codec_param param;

View File

@ -89,15 +89,13 @@ static void codec_init(struct mgcp_rtp_codec *codec)
.frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN, .frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN,
.rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE, .rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE,
.channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS, .channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS,
.subtype_name = "",
.audio_name = "",
}; };
} }
static void codec_free(struct mgcp_rtp_codec *codec) static void codec_free(struct mgcp_rtp_codec *codec)
{ {
if (codec->subtype_name)
talloc_free(codec->subtype_name);
if (codec->audio_name)
talloc_free(codec->audio_name);
*codec = (struct mgcp_rtp_codec){}; *codec = (struct mgcp_rtp_codec){};
} }
@ -124,10 +122,8 @@ int mgcp_codec_add(struct mgcp_conn_rtp *conn, int payload_type, const char *aud
{ {
int rate; int rate;
int channels; int channels;
char audio_codec[64];
struct mgcp_rtp_codec *codec; struct mgcp_rtp_codec *codec;
unsigned int pt_offset = conn->end.codecs_assigned; unsigned int pt_offset = conn->end.codecs_assigned;
void *ctx = conn->conn;
/* The amount of codecs we can store is limited, make sure we do not /* The amount of codecs we can store is limited, make sure we do not
* overrun this limit. */ * overrun this limit. */
@ -160,16 +156,16 @@ int mgcp_codec_add(struct mgcp_conn_rtp *conn, int payload_type, const char *aud
if (!audio_name) { if (!audio_name) {
switch (payload_type) { switch (payload_type) {
case 0: case 0:
audio_name = talloc_strdup(ctx, "PCMU/8000/1"); strcpy(codec->audio_name, "PCMU/8000/1");
break; break;
case 3: case 3:
audio_name = talloc_strdup(ctx, "GSM/8000/1"); strcpy(codec->audio_name, "GSM/8000/1");
break; break;
case 8: case 8:
audio_name = talloc_strdup(ctx, "PCMA/8000/1"); strcpy(codec->audio_name, "PCMA/8000/1");
break; break;
case 18: case 18:
audio_name = talloc_strdup(ctx, "G729/8000/1"); strcpy(codec->audio_name, "G729/8000/1");
break; break;
default: default:
/* The given payload type is not known to us, or it /* The given payload type is not known to us, or it
@ -179,36 +175,36 @@ int mgcp_codec_add(struct mgcp_conn_rtp *conn, int payload_type, const char *aud
payload_type); payload_type);
goto error; goto error;
} }
} else {
strncpy(codec->audio_name, audio_name, sizeof(codec->audio_name));
} }
/* Now we extract the codec subtype name, rate and channels. The latter /* Now we extract the codec subtype name, rate and channels. The latter
* two are optional. If they are not present we use the safe defaults * two are optional. If they are not present we use the safe defaults
* above. */ * above. */
if (strlen(audio_name) >= sizeof(audio_codec)) { if (strlen(codec->audio_name) >= sizeof(codec->subtype_name)) {
LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(audio_name, -1)); LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(codec->audio_name, -1));
goto error; goto error;
} }
channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS; channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE; rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
if (sscanf(audio_name, "%63[^/]/%d/%d", audio_codec, &rate, &channels) < 1) { if (sscanf(codec->audio_name, "%63[^/]/%d/%d", codec->subtype_name, &rate, &channels) < 1) {
LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(audio_name, -1)); LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(codec->audio_name, -1));
goto error; goto error;
} }
/* Note: We only accept configurations with one audio channel! */ /* Note: We only accept configurations with one audio channel! */
if (channels != 1) { if (channels != 1) {
LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n", LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n",
osmo_quote_str(audio_name, -1)); osmo_quote_str(codec->audio_name, -1));
goto error; goto error;
} }
codec->rate = rate; codec->rate = rate;
codec->channels = channels; codec->channels = channels;
codec->subtype_name = talloc_strdup(ctx, audio_codec);
codec->audio_name = talloc_strdup(ctx, audio_name);
codec->payload_type = payload_type; codec->payload_type = payload_type;
if (!strcmp(audio_codec, "G729")) { if (!strcmp(codec->subtype_name, "G729")) {
codec->frame_duration_num = 10; codec->frame_duration_num = 10;
codec->frame_duration_den = 1000; codec->frame_duration_den = 1000;
} else { } else {
@ -287,7 +283,7 @@ static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct m
/* A codec name must be set, if not, this might mean that the codec /* A codec name must be set, if not, this might mean that the codec
* (payload type) that was assigned is unknown to us so we must stop * (payload type) that was assigned is unknown to us so we must stop
* here. */ * here. */
if (!codec->subtype_name) if (!strlen(codec->subtype_name))
return false; return false;
/* FIXME: implement meaningful checks to make sure that the given codec /* FIXME: implement meaningful checks to make sure that the given codec

View File

@ -439,7 +439,7 @@ int mgcp_parse_sdp_data(const struct mgcp_endpoint *endp,
for (i = 0; i < codecs_used; i++) { for (i = 0; i < codecs_used; i++) {
LOGPC(DLMGCP, LOGL_NOTICE, "%d=%s", LOGPC(DLMGCP, LOGL_NOTICE, "%d=%s",
rtp->codecs[i].payload_type, rtp->codecs[i].payload_type,
rtp->codecs[i].subtype_name ? rtp-> codecs[i].subtype_name : "unknown"); strlen(rtp->codecs[i].subtype_name) ? rtp->codecs[i].subtype_name : "unknown");
LOGPC(DLMGCP, LOGL_NOTICE, " "); LOGPC(DLMGCP, LOGL_NOTICE, " ");
} }
LOGPC(DLMGCP, LOGL_NOTICE, "\n"); LOGPC(DLMGCP, LOGL_NOTICE, "\n");