File: mpa.c

package info (click to toggle)
baresip 1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,328 kB
  • sloc: ansic: 53,606; cpp: 2,268; makefile: 332; objc: 320; python: 259; sh: 40; xml: 19
file content (246 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
 * @file mpa.c mpa Audio Codec
 *
 * Copyright (C) 2016 Symonics GmbH
 */

#include <re.h>
#include <baresip.h>
#include <ctype.h>
#include <string.h>
#include "mpa.h"
#include <mpg123.h>

/**
 * @defgroup mpa mpa
 *
 * The mpa audio codec
 *
 * Supported version:
 *      libmpg123   1.16.0 or later
 *      libtwolame  0.3.13 or later
 *      libmp3lame  3.100 or later
 *
 * References:
 *
 *    RFC 2250  RTP Payload Format for the mpa Speech and Audio Codec
 *
 */

/*
4.1.17. Registration of MIME media type audio/MPA

   MIME media type name: audio

   MIME subtype name: MPA (MPEG audio)

   Required parameters: None

   Optional parameters:
       layer: which layer of MPEG audio encoding; permissible values
       are 1, 2, 3.

       samplerate: the rate at which audio is sampled.  MPEG-1 audio
       supports sampling rates of 32, 44.1, and 48 kHz; MPEG-2
       supports sampling rates of 16, 22.05 and 24 kHz.  This parameter
       is separate from the RTP timestamp clock rate which is always
       90000 Hz for MPA.

       mode: permissible values are "stereo", "joint_stereo",
       "single_channel", "dual_channel".  The "channels" parameter
       does not apply to MPA.  It is undefined to put a number of
       channels in the SDP rtpmap attribute for MPA.

       bitrate: the data rate for the audio bit stream.

       ptime: RECOMMENDED duration of each packet in milliseconds.

       maxptime: maximum duration of each packet in milliseconds.

       Parameters which are omitted are left to the encoder to choose
       based on the session bandwidth, configuration information, or
       other constraints.  The selected layer as well as the sampling
       rate and mode are indicated in the payload so receivers can
       process the data without these parameters being specified
       externally.

   Encoding considerations:
       This type is only defined for transfer via RTP [RFC 3550].

   Security considerations: See Section 5 of RFC 3555

   Interoperability considerations: none

   Published specification: RFC 3551

   Applications which use this media type:
       Audio and video streaming and conferencing tools.

*/


static bool mpa_mirror;
static char fmtp[256] = "";
static char fmtp_mirror[256];


static int mpa_fmtp_enc(struct mbuf *mb, const struct sdp_format *fmt,
			 bool offer, void *arg)
{
	bool mirror;

	(void)arg;
	(void)offer;

	if (!mb || !fmt)
		return 0;

	mirror = !offer && str_isset(fmtp_mirror);

	return mbuf_printf(mb, "a=fmtp:%s %s\r\n",
			   fmt->id, mirror ? fmtp_mirror : fmtp);
}


static struct aucodec mpa = {
	.pt        = "14",
	.name      = "MPA",
	.srate     = MPA_IORATE,
	.crate     = MPA_RTPRATE,
	.ch        = 2,
	.pch       = 1,
/* MPA does not expect channels count, even those it is stereo */
	.fmtp      = "layer=2",
	.encupdh   = mpa_encode_update,
	.ench      = mpa_encode_frm,
	.decupdh   = mpa_decode_update,
	.dech      = mpa_decode_frm,
};


void mpa_mirror_params(const char *x)
{
	if (!mpa_mirror)
		return;

	info("mpa: mirror parameters: \"%s\"\n", x);

	str_ncpy(fmtp_mirror, x, sizeof(fmtp_mirror));
}


static int module_init(void)
{
	struct conf *conf = conf_cur();
	uint32_t value;
	static char mode[30];
	int res;

	/** generate fmtp string based on config file */

	str_ncpy(mode, mpa.fmtp, sizeof(mode));

	if (0 == conf_get_u32(conf, "mpa_layer", &value)) {
		if (value<2 || value>3) {
			warning("MPA layer 2 or 3 are allowed.");
			return EINVAL;
		}
		(void)re_snprintf(fmtp+strlen(fmtp),
			sizeof(fmtp)-strlen(fmtp),
			"layer=%d", value);
	}
	if (0 == conf_get_u32(conf, "mpa_samplerate", &value)) {
		switch (value) {
			case 32000:
			case 44100:
			case 48000:
			case 16000:
			case 22050:
			case 24000:
				break;
			default:
				warning("MPA samplerates of 16, 22.05, 24, 32,"
					" 44.1, and 48 kHz are allowed.\n");
				return EINVAL;
		}
		(void)re_snprintf(fmtp+strlen(fmtp),
			sizeof(fmtp)-strlen(fmtp),
			";samplerate=%d", value);
	}
	if (0 == conf_get_u32(conf, "mpa_bitrate", &value)) {
		if (value<8000 || value>384000) {
			warning("MPA bitrate between 8000 and "
				"384000 are allowed.\n");
			return EINVAL;
		}

		(void)re_snprintf(fmtp+strlen(fmtp),
			sizeof(fmtp)-strlen(fmtp),
			";bitrate=%d", value);
	}
	if (0 == conf_get_str(conf, "mpa_mode", mode, sizeof(mode))) {
		char *p = mode;
		while (*p) {
			*p = tolower(*p);
			++p;
		}

		if (strcmp(mode,"stereo")
			&& strcmp(mode,"joint_stereo")
			&& strcmp(mode,"single_channel")
			&& strcmp(mode,"dual_channel")) {
			warning("MPA mode: Permissible values are stereo, "
			    "joint_stereo, single_channel, dual_channel.\n");
			return EINVAL;
		}

		(void)re_snprintf(fmtp+strlen(fmtp),
			sizeof(fmtp)-strlen(fmtp),
			";mode=%s", mode);
	}

	if (fmtp[0]==';')
		mpa.fmtp = fmtp+1;
	else
		mpa.fmtp = fmtp;

	(void)conf_get_bool(conf, "mpa_mirror", &mpa_mirror);

	if (mpa_mirror) {
		mpa.fmtp = NULL;
		mpa.fmtp_ench = mpa_fmtp_enc;
	}

	/* init decoder library */
	res = mpg123_init();
	if (res != MPG123_OK) {
		warning("MPA libmpg123 init error %s\n",
			mpg123_plain_strerror(res));
		return ENODEV;
	}

	aucodec_register(baresip_aucodecl(), &mpa);

#ifdef DEBUG
	info("MPA init with %s\n",mpa.fmtp);
#endif
	return 0;
}


static int module_close(void)
{
	aucodec_unregister(&mpa);

	mpg123_exit();

	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(mpa) = {
	"MPA",
	"audio codec",
	module_init,
	module_close,
};