blob: 0374bc0000a7e9a66e466891d82858e27b0e7e8d [file] [log] [blame]
Nanang Izzuddin8cdba462009-01-29 20:06:28 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjmedia-codec/passthrough.h>
21#include <pjmedia/codec.h>
22#include <pjmedia/errno.h>
23#include <pjmedia/endpoint.h>
24#include <pjmedia/port.h>
25#include <pj/assert.h>
26#include <pj/log.h>
27#include <pj/pool.h>
28#include <pj/string.h>
29#include <pj/os.h>
30
31/*
32 * Only build this file if PJMEDIA_HAS_PASSTHROUGH_CODECS != 0
33 */
34#if defined(PJMEDIA_HAS_PASSTHROUGH_CODECS) && PJMEDIA_HAS_PASSTHROUGH_CODECS!=0
35
36#define THIS_FILE "passthrough.c"
37
38
39/* Prototypes for passthrough codecs factory */
40static pj_status_t test_alloc( pjmedia_codec_factory *factory,
41 const pjmedia_codec_info *id );
42static pj_status_t default_attr( pjmedia_codec_factory *factory,
43 const pjmedia_codec_info *id,
44 pjmedia_codec_param *attr );
45static pj_status_t enum_codecs( pjmedia_codec_factory *factory,
46 unsigned *count,
47 pjmedia_codec_info codecs[]);
48static pj_status_t alloc_codec( pjmedia_codec_factory *factory,
49 const pjmedia_codec_info *id,
50 pjmedia_codec **p_codec);
51static pj_status_t dealloc_codec( pjmedia_codec_factory *factory,
52 pjmedia_codec *codec );
53
54/* Prototypes for passthrough codecs implementation. */
55static pj_status_t codec_init( pjmedia_codec *codec,
56 pj_pool_t *pool );
57static pj_status_t codec_open( pjmedia_codec *codec,
58 pjmedia_codec_param *attr );
59static pj_status_t codec_close( pjmedia_codec *codec );
60static pj_status_t codec_modify(pjmedia_codec *codec,
61 const pjmedia_codec_param *attr );
62static pj_status_t codec_parse( pjmedia_codec *codec,
63 void *pkt,
64 pj_size_t pkt_size,
65 const pj_timestamp *ts,
66 unsigned *frame_cnt,
67 pjmedia_frame frames[]);
68static pj_status_t codec_encode( pjmedia_codec *codec,
69 const struct pjmedia_frame *input,
70 unsigned output_buf_len,
71 struct pjmedia_frame *output);
72static pj_status_t codec_decode( pjmedia_codec *codec,
73 const struct pjmedia_frame *input,
74 unsigned output_buf_len,
75 struct pjmedia_frame *output);
76static pj_status_t codec_recover( pjmedia_codec *codec,
77 unsigned output_buf_len,
78 struct pjmedia_frame *output);
79
80/* Definition for passthrough codecs operations. */
81static pjmedia_codec_op codec_op =
82{
83 &codec_init,
84 &codec_open,
85 &codec_close,
86 &codec_modify,
87 &codec_parse,
88 &codec_encode,
89 &codec_decode,
90 &codec_recover
91};
92
93/* Definition for passthrough codecs factory operations. */
94static pjmedia_codec_factory_op codec_factory_op =
95{
96 &test_alloc,
97 &default_attr,
98 &enum_codecs,
99 &alloc_codec,
100 &dealloc_codec
101};
102
103/* Passthrough codecs factory */
104static struct codec_factory {
105 pjmedia_codec_factory base;
106 pjmedia_endpt *endpt;
107 pj_pool_t *pool;
108 pj_mutex_t *mutex;
109} codec_factory;
110
111/* Passthrough codecs private data. */
112typedef struct codec_private {
113 pj_pool_t *pool; /**< Pool for each instance. */
114 int codec_idx; /**< Codec index. */
115 void *codec_setting; /**< Specific codec setting. */
116 pj_uint16_t avg_frame_size; /**< Average of frame size. */
117} codec_private_t;
118
119
120
121/* CUSTOM CALLBACKS */
122
123/* Parse frames from a packet. Default behaviour of frame parsing is
124 * just separating frames based on calculating frame length derived
125 * from bitrate. Implement this callback when the default behaviour is
126 * unapplicable.
127 */
128typedef pj_status_t (*parse_cb)(codec_private_t *codec_data, void *pkt,
129 pj_size_t pkt_size, const pj_timestamp *ts,
130 unsigned *frame_cnt, pjmedia_frame frames[]);
131
132/* Pack frames into a packet. Default behaviour of packing frames is
133 * just stacking the frames with octet aligned without adding any
134 * payload header. Implement this callback when the default behaviour is
135 * unapplicable.
136 */
137typedef pj_status_t (*pack_cb)(codec_private_t *codec_data,
138 const struct pjmedia_frame_ext *input,
139 unsigned output_buf_len,
140 struct pjmedia_frame *output);
141
142
143/* Custom callback implementations. */
144static pj_status_t parse_amr( codec_private_t *codec_data, void *pkt,
145 pj_size_t pkt_size, const pj_timestamp *ts,
146 unsigned *frame_cnt, pjmedia_frame frames[]);
147static pj_status_t pack_amr ( codec_private_t *codec_data,
148 const struct pjmedia_frame_ext *input,
149 unsigned output_buf_len,
150 struct pjmedia_frame *output);
151
152
153/* Passthrough codec implementation descriptions. */
154static struct codec_desc {
155 int enabled; /* Is this codec enabled? */
156 const char *name; /* Codec name. */
157 pj_uint8_t pt; /* Payload type. */
Benny Prijonof863ca32009-02-17 15:19:45 +0000158 pjmedia_format_id fmt_id; /* Source format. */
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000159 unsigned clock_rate; /* Codec's clock rate. */
160 unsigned channel_count; /* Codec's channel count. */
161 unsigned samples_per_frame; /* Codec's samples count. */
162 unsigned def_bitrate; /* Default bitrate of this codec. */
163 unsigned max_bitrate; /* Maximum bitrate of this codec. */
164 pj_uint8_t frm_per_pkt; /* Default num of frames per packet.*/
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000165 pj_uint8_t vad; /* VAD enabled/disabled. */
166 pj_uint8_t plc; /* PLC enabled/disabled. */
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000167 parse_cb parse; /* Callback to parse bitstream. */
168 pack_cb pack; /* Callback to pack bitstream. */
169 pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */
170}
171
172codec_desc[] =
173{
174# if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
Benny Prijonof863ca32009-02-17 15:19:45 +0000175 {1, "AMR", PJMEDIA_RTP_PT_AMR, PJMEDIA_FORMAT_AMR,
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000176 8000, 1, 160,
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000177 7400, 12200, 2, 1, 1,
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000178 &parse_amr, &pack_amr
179 /*, {1, {{{"octet-align", 11}, {"1", 1}}} } */
180 },
181# endif
182
183# if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
Benny Prijonof863ca32009-02-17 15:19:45 +0000184 {1, "G729", PJMEDIA_RTP_PT_G729, PJMEDIA_FORMAT_G729,
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000185 8000, 1, 80,
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000186 8000, 8000, 2, 1, 1
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000187 },
188# endif
189
190# if PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC
Benny Prijonof863ca32009-02-17 15:19:45 +0000191 {1, "iLBC", PJMEDIA_RTP_PT_ILBC, PJMEDIA_FORMAT_ILBC,
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000192 8000, 1, 240,
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000193 13333, 15200, 1, 1, 1,
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000194 NULL, NULL,
195 {1, {{{"mode", 4}, {"30", 2}}} }
196 },
197# endif
198
199# if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU
Benny Prijonof863ca32009-02-17 15:19:45 +0000200 {1, "PCMU", PJMEDIA_RTP_PT_PCMU, PJMEDIA_FORMAT_PCMU,
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000201 8000, 1, 80,
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000202 64000, 64000, 2, 1, 1
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000203 },
204# endif
205
206# if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA
Benny Prijonof863ca32009-02-17 15:19:45 +0000207 {1, "PCMA", PJMEDIA_RTP_PT_PCMA, PJMEDIA_FORMAT_PCMA,
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000208 8000, 1, 80,
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000209 64000, 64000, 2, 1, 1
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000210 },
211# endif
212};
213
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000214
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000215#if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
216
217#include <pjmedia-codec/amr_helper.h>
218
219typedef struct amr_settings_t {
220 pjmedia_codec_amr_pack_setting enc_setting;
221 pjmedia_codec_amr_pack_setting dec_setting;
222 pj_int8_t enc_mode;
223} amr_settings_t;
224
225
226/* Pack AMR payload */
227static pj_status_t pack_amr ( codec_private_t *codec_data,
228 const struct pjmedia_frame_ext *input,
229 unsigned output_buf_len,
230 struct pjmedia_frame *output)
231{
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000232 enum {MAX_FRAMES_PER_PACKET = 8};
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000233
234 pjmedia_frame frames[MAX_FRAMES_PER_PACKET];
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000235 amr_settings_t* setting = (amr_settings_t*)codec_data->codec_setting;
236 pjmedia_codec_amr_pack_setting *enc_setting = &setting->enc_setting;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000237 pj_uint8_t SID_FT;
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000238 unsigned i;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000239
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000240 pj_assert(input->subframe_cnt <= MAX_FRAMES_PER_PACKET);
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000241
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000242 SID_FT = (pj_uint8_t)(enc_setting->amr_nb? 8 : 9);
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000243
244 /* Get frames */
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000245 for (i = 0; i < input->subframe_cnt; ++i) {
246 pjmedia_frame_ext_subframe *sf;
247 pjmedia_codec_amr_bit_info *info;
248 unsigned len;
249
250 sf = pjmedia_frame_ext_get_subframe(input, i);
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000251 len = (sf->bitlen + 7) >> 3;
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000252
253 info = (pjmedia_codec_amr_bit_info*) &frames[i].bit_info;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000254 pj_bzero(info, sizeof(*info));
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000255
256 if (len == 0) {
257 info->frame_type = (pj_uint8_t)(enc_setting->amr_nb? 14 : 15);
258 } else {
259 info->frame_type = pjmedia_codec_amr_get_mode2(enc_setting->amr_nb,
260 len);
261 }
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000262 info->good_quality = 1;
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000263 info->mode = setting->enc_mode;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000264
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000265 frames[i].buf = sf->data;
266 frames[i].size = len;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000267 }
268
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000269 output->size = output_buf_len;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000270
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000271 return pjmedia_codec_amr_pack(frames, input->subframe_cnt, enc_setting,
272 output->buf, &output->size);
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000273}
274
275
276/* Parse AMR payload into frames. */
277static pj_status_t parse_amr(codec_private_t *codec_data, void *pkt,
278 pj_size_t pkt_size, const pj_timestamp *ts,
279 unsigned *frame_cnt, pjmedia_frame frames[])
280{
281 amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting;
282 pjmedia_codec_amr_pack_setting *setting;
283 pj_status_t status;
284 pj_uint8_t cmr;
285
286 setting = &s->dec_setting;
287
288 status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames,
289 frame_cnt, &cmr);
290 if (status != PJ_SUCCESS)
291 return status;
292
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000293 // CMR is not supported for now.
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000294 /* Check Change Mode Request. */
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000295 //if ((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) {
296 // s->enc_mode = cmr;
297 //}
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000298
299 return PJ_SUCCESS;
300}
301
302#endif /* PJMEDIA_HAS_PASSTROUGH_CODEC_AMR */
303
304
305/*
306 * Initialize and register passthrough codec factory to pjmedia endpoint.
307 */
308PJ_DEF(pj_status_t) pjmedia_codec_passthrough_init( pjmedia_endpt *endpt )
309{
310 pjmedia_codec_mgr *codec_mgr;
311 pj_status_t status;
312
313 if (codec_factory.pool != NULL) {
314 /* Already initialized. */
315 return PJ_SUCCESS;
316 }
317
318 /* Create passthrough codec factory. */
319 codec_factory.base.op = &codec_factory_op;
320 codec_factory.base.factory_data = NULL;
321 codec_factory.endpt = endpt;
322
323 codec_factory.pool = pjmedia_endpt_create_pool(endpt, "Passthrough codecs",
324 4000, 4000);
325 if (!codec_factory.pool)
326 return PJ_ENOMEM;
327
328 /* Create mutex. */
329 status = pj_mutex_create_simple(codec_factory.pool, "Passthrough codecs",
330 &codec_factory.mutex);
331 if (status != PJ_SUCCESS)
332 goto on_error;
333
334 /* Get the codec manager. */
335 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
336 if (!codec_mgr) {
337 status = PJ_EINVALIDOP;
338 goto on_error;
339 }
340
341 /* Register codec factory to endpoint. */
342 status = pjmedia_codec_mgr_register_factory(codec_mgr,
343 &codec_factory.base);
344 if (status != PJ_SUCCESS)
345 goto on_error;
346
347 /* Done. */
348 return PJ_SUCCESS;
349
350on_error:
351 pj_pool_release(codec_factory.pool);
352 codec_factory.pool = NULL;
353 return status;
354}
355
356/*
357 * Unregister passthrough codecs factory from pjmedia endpoint.
358 */
359PJ_DEF(pj_status_t) pjmedia_codec_passthrough_deinit(void)
360{
361 pjmedia_codec_mgr *codec_mgr;
362 pj_status_t status;
363
364 if (codec_factory.pool == NULL) {
365 /* Already deinitialized */
366 return PJ_SUCCESS;
367 }
368
369 pj_mutex_lock(codec_factory.mutex);
370
371 /* Get the codec manager. */
372 codec_mgr = pjmedia_endpt_get_codec_mgr(codec_factory.endpt);
373 if (!codec_mgr) {
374 pj_pool_release(codec_factory.pool);
375 codec_factory.pool = NULL;
376 return PJ_EINVALIDOP;
377 }
378
379 /* Unregister passthrough codecs factory. */
380 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
381 &codec_factory.base);
382
383 /* Destroy mutex. */
384 pj_mutex_destroy(codec_factory.mutex);
385
386 /* Destroy pool. */
387 pj_pool_release(codec_factory.pool);
388 codec_factory.pool = NULL;
389
390 return status;
391}
392
393/*
394 * Check if factory can allocate the specified codec.
395 */
396static pj_status_t test_alloc( pjmedia_codec_factory *factory,
397 const pjmedia_codec_info *info )
398{
399 unsigned i;
400
401 PJ_UNUSED_ARG(factory);
402
403 /* Type MUST be audio. */
404 if (info->type != PJMEDIA_TYPE_AUDIO)
405 return PJMEDIA_CODEC_EUNSUP;
406
407 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
408 pj_str_t name = pj_str((char*)codec_desc[i].name);
409 if ((pj_stricmp(&info->encoding_name, &name) == 0) &&
410 (info->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
411 (info->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
412 (codec_desc[i].enabled))
413 {
414 return PJ_SUCCESS;
415 }
416 }
417
418 /* Unsupported, or mode is disabled. */
419 return PJMEDIA_CODEC_EUNSUP;
420}
421
422/*
423 * Generate default attribute.
424 */
425static pj_status_t default_attr ( pjmedia_codec_factory *factory,
426 const pjmedia_codec_info *id,
427 pjmedia_codec_param *attr )
428{
429 unsigned i;
430
431 PJ_ASSERT_RETURN(factory==&codec_factory.base, PJ_EINVAL);
432
433 pj_bzero(attr, sizeof(pjmedia_codec_param));
434
435 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
436 pj_str_t name = pj_str((char*)codec_desc[i].name);
437 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
438 (id->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
439 (id->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
440 (id->pt == (unsigned)codec_desc[i].pt))
441 {
442 attr->info.pt = (pj_uint8_t)id->pt;
443 attr->info.channel_cnt = codec_desc[i].channel_count;
444 attr->info.clock_rate = codec_desc[i].clock_rate;
445 attr->info.avg_bps = codec_desc[i].def_bitrate;
446 attr->info.max_bps = codec_desc[i].max_bitrate;
447 attr->info.pcm_bits_per_sample = 16;
448 attr->info.frm_ptime = (pj_uint16_t)
449 (codec_desc[i].samples_per_frame * 1000 /
450 codec_desc[i].channel_count /
451 codec_desc[i].clock_rate);
Benny Prijonof863ca32009-02-17 15:19:45 +0000452 attr->info.fmt_id = codec_desc[i].fmt_id;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000453
454 /* Default flags. */
455 attr->setting.frm_per_pkt = codec_desc[i].frm_per_pkt;
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000456 attr->setting.plc = codec_desc[i].plc;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000457 attr->setting.penh= 0;
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000458 attr->setting.vad = codec_desc[i].vad;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000459 attr->setting.cng = attr->setting.vad;
460 attr->setting.dec_fmtp = codec_desc[i].dec_fmtp;
461
462 if (attr->setting.vad == 0) {
463#if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
464 if (id->pt == PJMEDIA_RTP_PT_G729) {
465 /* Signal G729 Annex B is being disabled */
466 attr->setting.dec_fmtp.cnt = 1;
467 pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb");
468 pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no");
469 }
470#endif
471 }
472
473 return PJ_SUCCESS;
474 }
475 }
476
477 return PJMEDIA_CODEC_EUNSUP;
478}
479
480/*
481 * Enum codecs supported by this factory.
482 */
483static pj_status_t enum_codecs( pjmedia_codec_factory *factory,
484 unsigned *count,
485 pjmedia_codec_info codecs[])
486{
487 unsigned max;
488 unsigned i;
489
490 PJ_UNUSED_ARG(factory);
491 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
492
493 max = *count;
494
495 for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(codec_desc) && *count < max; ++i)
496 {
497 if (!codec_desc[i].enabled)
498 continue;
499
500 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
501 codecs[*count].encoding_name = pj_str((char*)codec_desc[i].name);
502 codecs[*count].pt = codec_desc[i].pt;
503 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
504 codecs[*count].clock_rate = codec_desc[i].clock_rate;
505 codecs[*count].channel_cnt = codec_desc[i].channel_count;
506
507 ++*count;
508 }
509
510 return PJ_SUCCESS;
511}
512
513/*
514 * Allocate a new codec instance.
515 */
516static pj_status_t alloc_codec( pjmedia_codec_factory *factory,
517 const pjmedia_codec_info *id,
518 pjmedia_codec **p_codec)
519{
520 codec_private_t *codec_data;
521 pjmedia_codec *codec;
522 int idx;
523 pj_pool_t *pool;
524 unsigned i;
525
526 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
527 PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
528
529 pj_mutex_lock(codec_factory.mutex);
530
531 /* Find codec's index */
532 idx = -1;
533 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
534 pj_str_t name = pj_str((char*)codec_desc[i].name);
535 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
536 (id->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
537 (id->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
538 (codec_desc[i].enabled))
539 {
540 idx = i;
541 break;
542 }
543 }
544 if (idx == -1) {
545 *p_codec = NULL;
546 return PJMEDIA_CODEC_EUNSUP;
547 }
548
549 /* Create pool for codec instance */
550 pool = pjmedia_endpt_create_pool(codec_factory.endpt, "passthroughcodec",
551 512, 512);
552 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
553 codec->op = &codec_op;
554 codec->factory = factory;
555 codec->codec_data = PJ_POOL_ZALLOC_T(pool, codec_private_t);
556 codec_data = (codec_private_t*) codec->codec_data;
557 codec_data->pool = pool;
558 codec_data->codec_idx = idx;
559
560 pj_mutex_unlock(codec_factory.mutex);
561
562 *p_codec = codec;
563 return PJ_SUCCESS;
564}
565
566/*
567 * Free codec.
568 */
569static pj_status_t dealloc_codec( pjmedia_codec_factory *factory,
570 pjmedia_codec *codec )
571{
572 codec_private_t *codec_data;
573
574 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
575 PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
576
577 /* Close codec, if it's not closed. */
578 codec_data = (codec_private_t*) codec->codec_data;
579 codec_close(codec);
580
581 pj_pool_release(codec_data->pool);
582
583 return PJ_SUCCESS;
584}
585
586/*
587 * Init codec.
588 */
589static pj_status_t codec_init( pjmedia_codec *codec,
590 pj_pool_t *pool )
591{
592 PJ_UNUSED_ARG(codec);
593 PJ_UNUSED_ARG(pool);
594 return PJ_SUCCESS;
595}
596
597/*
598 * Open codec.
599 */
600static pj_status_t codec_open( pjmedia_codec *codec,
601 pjmedia_codec_param *attr )
602{
603 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
604 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
605 pj_pool_t *pool;
606 int i, j;
607
608 pool = codec_data->pool;
609
610 /* Get bitstream size */
611 i = attr->info.avg_bps * desc->samples_per_frame;
612 j = desc->clock_rate << 3;
613 codec_data->avg_frame_size = (pj_uint16_t)(i / j);
614 if (i % j) ++codec_data->avg_frame_size;
615
616#if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
617 /* Init AMR settings */
618 if (desc->pt == PJMEDIA_RTP_PT_AMR || desc->pt == PJMEDIA_RTP_PT_AMRWB) {
619 amr_settings_t *s;
620 pj_uint8_t octet_align = 0;
621 const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11};
622
623 /* Fetch octet-align setting. It should be fine to fetch only
624 * the decoder, since encoder & decoder must use the same setting
625 * (RFC 4867 section 8.3.1).
626 */
627 for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
628 if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name,
629 &STR_FMTP_OCTET_ALIGN) == 0)
630 {
631 octet_align=(pj_uint8_t)
632 (pj_strtoul(&attr->setting.dec_fmtp.param[i].val));
633 break;
634 }
635 }
636
637 s = PJ_POOL_ZALLOC_T(pool, amr_settings_t);
638 codec_data->codec_setting = s;
639
640 s->enc_mode = pjmedia_codec_amr_get_mode(desc->def_bitrate);
641 if (s->enc_mode < 0)
642 return PJMEDIA_CODEC_EINMODE;
643
Benny Prijonob7dfc9c2009-02-13 11:53:12 +0000644 s->enc_setting.amr_nb = (pj_uint8_t)(desc->pt == PJMEDIA_RTP_PT_AMR);
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000645 s->enc_setting.octet_aligned = octet_align;
646 s->enc_setting.reorder = PJ_FALSE; /* Note this! passthrough codec
647 doesn't do sensitivity bits
648 reordering */
649 s->enc_setting.cmr = 15;
650
Benny Prijonob7dfc9c2009-02-13 11:53:12 +0000651 s->dec_setting.amr_nb = (pj_uint8_t)(desc->pt == PJMEDIA_RTP_PT_AMR);
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000652 s->dec_setting.octet_aligned = octet_align;
653 s->dec_setting.reorder = PJ_FALSE; /* Note this! passthrough codec
654 doesn't do sensitivity bits
655 reordering */
656 }
657#endif
658
659 return PJ_SUCCESS;
660}
661
662/*
663 * Close codec.
664 */
665static pj_status_t codec_close( pjmedia_codec *codec )
666{
667 PJ_UNUSED_ARG(codec);
668
669 return PJ_SUCCESS;
670}
671
672
673/*
674 * Modify codec settings.
675 */
676static pj_status_t codec_modify( pjmedia_codec *codec,
677 const pjmedia_codec_param *attr )
678{
679 /* Not supported yet. */
680 PJ_UNUSED_ARG(codec);
681 PJ_UNUSED_ARG(attr);
682
683 return PJ_ENOTSUP;
684}
685
686/*
687 * Get frames in the packet.
688 */
689static pj_status_t codec_parse( pjmedia_codec *codec,
690 void *pkt,
691 pj_size_t pkt_size,
692 const pj_timestamp *ts,
693 unsigned *frame_cnt,
694 pjmedia_frame frames[])
695{
696 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
697 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
698 unsigned count = 0;
699
700 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
701
702 if (desc->parse != NULL) {
703 return desc->parse(codec_data, pkt, pkt_size, ts, frame_cnt, frames);
704 }
705
706 while (pkt_size >= codec_data->avg_frame_size && count < *frame_cnt) {
707 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
708 frames[count].buf = pkt;
709 frames[count].size = codec_data->avg_frame_size;
710 frames[count].timestamp.u64 = ts->u64 + count*desc->samples_per_frame;
711
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000712 pkt = (pj_uint8_t*)pkt + codec_data->avg_frame_size;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000713 pkt_size -= codec_data->avg_frame_size;
714
715 ++count;
716 }
717
718 if (pkt_size && count < *frame_cnt) {
719 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
720 frames[count].buf = pkt;
721 frames[count].size = pkt_size;
722 frames[count].timestamp.u64 = ts->u64 + count*desc->samples_per_frame;
723 ++count;
724 }
725
726 *frame_cnt = count;
727 return PJ_SUCCESS;
728}
729
730/*
731 * Encode frames.
732 */
733static pj_status_t codec_encode( pjmedia_codec *codec,
734 const struct pjmedia_frame *input,
735 unsigned output_buf_len,
736 struct pjmedia_frame *output)
737{
738 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
739 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
740 const pjmedia_frame_ext *input_ = (const pjmedia_frame_ext*) input;
741
742 pj_assert(input && input->type == PJMEDIA_FRAME_TYPE_EXTENDED);
743
744 if (desc->pack != NULL) {
745 desc->pack(codec_data, input_, output_buf_len, output);
746 } else {
747 if (input_->subframe_cnt == 0) {
748 /* DTX */
749 output->buf = NULL;
750 output->size = 0;
751 output->type = PJMEDIA_FRAME_TYPE_NONE;
752 } else {
753 unsigned i;
754 pj_uint8_t *p = output->buf;
755
756 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
757 output->size = 0;
758
759 for (i = 0; i < input_->subframe_cnt; ++i) {
760 pjmedia_frame_ext_subframe *sf;
761 unsigned sf_len;
762
763 sf = pjmedia_frame_ext_get_subframe(input_, i);
764 pj_assert(sf);
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000765
766 sf_len = (sf->bitlen + 7) >> 3;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000767
768 pj_memcpy(p, sf->data, sf_len);
769 p += sf_len;
770 output->size += sf_len;
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000771
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000772 /* If there is SID or DTX frame, break the loop. */
773 if (desc->pt == PJMEDIA_RTP_PT_G729 &&
774 sf_len < codec_data->avg_frame_size)
775 {
776 break;
777 }
Nanang Izzuddind5c54ab2009-02-10 04:43:16 +0000778
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000779 }
780 }
781 }
782
783 return PJ_SUCCESS;
784}
785
786/*
787 * Decode frame.
788 */
789static pj_status_t codec_decode( pjmedia_codec *codec,
790 const struct pjmedia_frame *input,
791 unsigned output_buf_len,
792 struct pjmedia_frame *output)
793{
794 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
795 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
796 pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output;
797
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000798 pj_assert(input);
Benny Prijonob7dfc9c2009-02-13 11:53:12 +0000799 PJ_UNUSED_ARG(output_buf_len);
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000800
801#if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
802 /* Need to rearrange the AMR bitstream, since the bitstream may not be
803 * started from bit 0 or may need to be reordered from sensitivity order
804 * into encoder bits order.
805 */
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000806 if (desc->pt == PJMEDIA_RTP_PT_AMR || desc->pt == PJMEDIA_RTP_PT_AMRWB) {
807 pjmedia_frame input_;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000808 pjmedia_codec_amr_pack_setting *setting;
809
810 setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting;
811
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000812 input_ = *input;
813 pjmedia_codec_amr_predecode(input, setting, &input_);
814
815 pjmedia_frame_ext_append_subframe(output_, input_.buf,
816 (pj_uint16_t)(input_.size << 3),
817 (pj_uint16_t)desc->samples_per_frame);
818
819 return PJ_SUCCESS;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000820 }
821#endif
Nanang Izzuddinbfa860b2009-02-09 10:39:58 +0000822
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000823 pjmedia_frame_ext_append_subframe(output_, input->buf,
824 (pj_uint16_t)(input->size << 3),
825 (pj_uint16_t)desc->samples_per_frame);
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000826
827 return PJ_SUCCESS;
828}
829
830/*
831 * Recover lost frame.
832 */
833static pj_status_t codec_recover( pjmedia_codec *codec,
834 unsigned output_buf_len,
835 struct pjmedia_frame *output)
836{
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000837 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
838 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
839 pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output;
840
841 PJ_UNUSED_ARG(output_buf_len);
842
843 pjmedia_frame_ext_append_subframe(output_, NULL, 0,
844 (pj_uint16_t)desc->samples_per_frame);
845
846 return PJ_SUCCESS;
Nanang Izzuddin8cdba462009-01-29 20:06:28 +0000847}
848
849#endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */
850