Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/passthrough.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjmedia-codec/passthrough.h>
20 #include <pjmedia-codec/amr_sdp_match.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/math.h>
28 #include <pj/pool.h>
29 #include <pj/string.h>
30 #include <pj/os.h>
31
32 /*
33 * Only build this file if PJMEDIA_HAS_PASSTHROUGH_CODECS != 0
34 */
35 #if defined(PJMEDIA_HAS_PASSTHROUGH_CODECS) && PJMEDIA_HAS_PASSTHROUGH_CODECS!=0
36
37 #define THIS_FILE "passthrough.c"
38
39
40 /* Prototypes for passthrough codecs factory */
41 static pj_status_t test_alloc( pjmedia_codec_factory *factory,
42 const pjmedia_codec_info *id );
43 static pj_status_t default_attr( pjmedia_codec_factory *factory,
44 const pjmedia_codec_info *id,
45 pjmedia_codec_param *attr );
46 static pj_status_t enum_codecs( pjmedia_codec_factory *factory,
47 unsigned *count,
48 pjmedia_codec_info codecs[]);
49 static pj_status_t alloc_codec( pjmedia_codec_factory *factory,
50 const pjmedia_codec_info *id,
51 pjmedia_codec **p_codec);
52 static pj_status_t dealloc_codec( pjmedia_codec_factory *factory,
53 pjmedia_codec *codec );
54
55 /* Prototypes for passthrough codecs implementation. */
56 static pj_status_t codec_init( pjmedia_codec *codec,
57 pj_pool_t *pool );
58 static pj_status_t codec_open( pjmedia_codec *codec,
59 pjmedia_codec_param *attr );
60 static pj_status_t codec_close( pjmedia_codec *codec );
61 static pj_status_t codec_modify(pjmedia_codec *codec,
62 const pjmedia_codec_param *attr );
63 static pj_status_t codec_parse( pjmedia_codec *codec,
64 void *pkt,
65 pj_size_t pkt_size,
66 const pj_timestamp *ts,
67 unsigned *frame_cnt,
68 pjmedia_frame frames[]);
69 static pj_status_t codec_encode( pjmedia_codec *codec,
70 const struct pjmedia_frame *input,
71 unsigned output_buf_len,
72 struct pjmedia_frame *output);
73 static pj_status_t codec_decode( pjmedia_codec *codec,
74 const struct pjmedia_frame *input,
75 unsigned output_buf_len,
76 struct pjmedia_frame *output);
77 static pj_status_t codec_recover( pjmedia_codec *codec,
78 unsigned output_buf_len,
79 struct pjmedia_frame *output);
80
81 /* Definition for passthrough codecs operations. */
82 static pjmedia_codec_op codec_op =
83 {
84 &codec_init,
85 &codec_open,
86 &codec_close,
87 &codec_modify,
88 &codec_parse,
89 &codec_encode,
90 &codec_decode,
91 &codec_recover
92 };
93
94 /* Definition for passthrough codecs factory operations. */
95 static pjmedia_codec_factory_op codec_factory_op =
96 {
97 &test_alloc,
98 &default_attr,
99 &enum_codecs,
100 &alloc_codec,
101 &dealloc_codec,
102 &pjmedia_codec_passthrough_deinit
103 };
104
105 /* Passthrough codecs factory */
106 static struct codec_factory {
107 pjmedia_codec_factory base;
108 pjmedia_endpt *endpt;
109 pj_pool_t *pool;
110 pj_mutex_t *mutex;
111 } codec_factory;
112
113 /* Passthrough codecs private data. */
114 typedef struct codec_private {
115 pj_pool_t *pool; /**< Pool for each instance. */
116 int codec_idx; /**< Codec index. */
117 void *codec_setting; /**< Specific codec setting. */
118 pj_uint16_t avg_frame_size; /**< Average of frame size. */
119 unsigned samples_per_frame; /**< Samples per frame, for iLBC
120 this can be 240 or 160, can
121 only be known after codec is
122 opened. */
123 } codec_private_t;
124
125
126
127 /* CUSTOM CALLBACKS */
128
129 /* Parse frames from a packet. Default behaviour of frame parsing is
130 * just separating frames based on calculating frame length derived
131 * from bitrate. Implement this callback when the default behaviour is
132 * unapplicable.
133 */
134 typedef pj_status_t (*parse_cb)(codec_private_t *codec_data, void *pkt,
135 pj_size_t pkt_size, const pj_timestamp *ts,
136 unsigned *frame_cnt, pjmedia_frame frames[]);
137
138 /* Pack frames into a packet. Default behaviour of packing frames is
139 * just stacking the frames with octet aligned without adding any
140 * payload header. Implement this callback when the default behaviour is
141 * unapplicable.
142 */
143 typedef pj_status_t (*pack_cb)(codec_private_t *codec_data,
144 const struct pjmedia_frame_ext *input,
145 unsigned output_buf_len,
146 struct pjmedia_frame *output);
147
148
149 /* Custom callback implementations. */
150 static pj_status_t parse_amr( codec_private_t *codec_data, void *pkt,
151 pj_size_t pkt_size, const pj_timestamp *ts,
152 unsigned *frame_cnt, pjmedia_frame frames[]);
153 static pj_status_t pack_amr ( codec_private_t *codec_data,
154 const struct pjmedia_frame_ext *input,
155 unsigned output_buf_len,
156 struct pjmedia_frame *output);
157
158
159 /* Passthrough codec implementation descriptions. */
160 static struct codec_desc {
161 int enabled; /* Is this codec enabled? */
162 const char *name; /* Codec name. */
163 pj_uint8_t pt; /* Payload type. */
164 pjmedia_format_id fmt_id; /* Source format. */
165 unsigned clock_rate; /* Codec's clock rate. */
166 unsigned channel_count; /* Codec's channel count. */
167 unsigned samples_per_frame; /* Codec's samples count. */
168 unsigned def_bitrate; /* Default bitrate of this codec. */
169 unsigned max_bitrate; /* Maximum bitrate of this codec. */
170 pj_uint8_t frm_per_pkt; /* Default num of frames per packet.*/
171 pj_uint8_t vad; /* VAD enabled/disabled. */
172 pj_uint8_t plc; /* PLC enabled/disabled. */
173 parse_cb parse; /* Callback to parse bitstream. */
174 pack_cb pack; /* Callback to pack bitstream. */
175 pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */
176 }
177
178 codec_desc[] =
179 {
180 # if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
181 {1, "AMR", PJMEDIA_RTP_PT_AMR, PJMEDIA_FORMAT_AMR,
182 8000, 1, 160,
183 7400, 12200, 2, 1, 1,
184 &parse_amr, &pack_amr
185 /*, {1, {{{"octet-align", 11}, {"1", 1}}} } */
186 },
187 # endif
188
189 # if PJMEDIA_HAS_PASSTHROUGH_CODEC_G722
190 {1, "G722", PJMEDIA_RTP_PT_G722, PJMEDIA_FORMAT_G722,
191 16000, 1, 160,
192 64000, 64000, 2, 1, 1
193 },
194 # endif
195
196 # if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
197 {1, "G729", PJMEDIA_RTP_PT_G729, PJMEDIA_FORMAT_G729,
198 8000, 1, 80,
199 8000, 8000, 2, 1, 1
200 },
201 # endif
202
203 # if PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC
204 {1, "iLBC", PJMEDIA_RTP_PT_ILBC, PJMEDIA_FORMAT_ILBC,
205 8000, 1, 240,
206 13333, 15200, 1, 1, 1,
207 NULL, NULL,
208 {1, {{{"mode", 4}, {"30", 2}}} }
209 },
210 # endif
211
212 # if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU
213 {1, "PCMU", PJMEDIA_RTP_PT_PCMU, PJMEDIA_FORMAT_PCMU,
214 8000, 1, 80,
215 64000, 64000, 2, 1, 1
216 },
217 # endif
218
219 # if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA
220 {1, "PCMA", PJMEDIA_RTP_PT_PCMA, PJMEDIA_FORMAT_PCMA,
221 8000, 1, 80,
222 64000, 64000, 2, 1, 1
223 },
224 # endif
225 };
226
227
228 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
229
230 #include <pjmedia-codec/amr_helper.h>
231
232 typedef struct amr_settings_t {
233 pjmedia_codec_amr_pack_setting enc_setting;
234 pjmedia_codec_amr_pack_setting dec_setting;
235 pj_int8_t enc_mode;
236 } amr_settings_t;
237
238
239 /* Pack AMR payload */
240 static pj_status_t pack_amr ( codec_private_t *codec_data,
241 const struct pjmedia_frame_ext *input,
242 unsigned output_buf_len,
243 struct pjmedia_frame *output)
244 {
245 enum {MAX_FRAMES_PER_PACKET = PJMEDIA_MAX_FRAME_DURATION_MS / 20};
246
247 pjmedia_frame frames[MAX_FRAMES_PER_PACKET];
248 amr_settings_t* setting = (amr_settings_t*)codec_data->codec_setting;
249 pjmedia_codec_amr_pack_setting *enc_setting = &setting->enc_setting;
250 pj_uint8_t SID_FT;
251 unsigned i;
252
253 pj_assert(input->subframe_cnt <= MAX_FRAMES_PER_PACKET);
254
255 SID_FT = (pj_uint8_t)(enc_setting->amr_nb? 8 : 9);
256
257 /* Get frames */
258 for (i = 0; i < input->subframe_cnt; ++i) {
259 pjmedia_frame_ext_subframe *sf;
260 pjmedia_codec_amr_bit_info *info;
261 unsigned len;
262
263 sf = pjmedia_frame_ext_get_subframe(input, i);
264 len = (sf->bitlen + 7) >> 3;
265
266 info = (pjmedia_codec_amr_bit_info*) &frames[i].bit_info;
267 pj_bzero(info, sizeof(*info));
268
269 if (len == 0) {
270 /* DTX */
271 info->frame_type = 15;
272 } else {
273 info->frame_type = pjmedia_codec_amr_get_mode2(enc_setting->amr_nb,
274 len);
275 }
276 info->good_quality = 1;
277 info->mode = setting->enc_mode;
278 if (info->frame_type == SID_FT)
279 info->STI = (sf->data[4] >> 4) & 1;
280
281 frames[i].buf = sf->data;
282 frames[i].size = len;
283 }
284
285 output->size = output_buf_len;
286
287 return pjmedia_codec_amr_pack(frames, input->subframe_cnt, enc_setting,
288 output->buf, &output->size);
289 }
290
291
292 /* Parse AMR payload into frames. */
293 static pj_status_t parse_amr(codec_private_t *codec_data, void *pkt,
294 pj_size_t pkt_size, const pj_timestamp *ts,
295 unsigned *frame_cnt, pjmedia_frame frames[])
296 {
297 amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting;
298 pjmedia_codec_amr_pack_setting *setting;
299 pj_status_t status;
300 pj_uint8_t cmr;
301
302 setting = &s->dec_setting;
303
304 status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames,
305 frame_cnt, &cmr);
306 if (status != PJ_SUCCESS)
307 return status;
308
309 // CMR is not supported for now.
310 /* Check Change Mode Request. */
311 //if ((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) {
312 // s->enc_mode = cmr;
313 //}
314
315 return PJ_SUCCESS;
316 }
317
318 #endif /* PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR */
319
320
321 /*
322 * Initialize and register passthrough codec factory to pjmedia endpoint.
323 */
324 PJ_DEF(pj_status_t) pjmedia_codec_passthrough_init( pjmedia_endpt *endpt )
325 {
326 pjmedia_codec_mgr *codec_mgr;
327 pj_str_t codec_name;
328 pj_status_t status;
329
330 if (codec_factory.pool != NULL) {
331 /* Already initialized. */
332 return PJ_EEXISTS;
333 }
334
335 /* Create passthrough codec factory. */
336 codec_factory.base.op = &codec_factory_op;
337 codec_factory.base.factory_data = NULL;
338 codec_factory.endpt = endpt;
339
340 codec_factory.pool = pjmedia_endpt_create_pool(endpt, "Passthrough codecs",
341 4000, 4000);
342 if (!codec_factory.pool)
343 return PJ_ENOMEM;
344
345 /* Create mutex. */
346 status = pj_mutex_create_simple(codec_factory.pool, "Passthrough codecs",
347 &codec_factory.mutex);
348 if (status != PJ_SUCCESS)
349 goto on_error;
350
351 /* Get the codec manager. */
352 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
353 if (!codec_mgr) {
354 status = PJ_EINVALIDOP;
355 goto on_error;
356 }
357
358 /* Register format match callback. */
359 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
360 pj_cstr(&codec_name, "AMR");
361 status = pjmedia_sdp_neg_register_fmt_match_cb(
362 &codec_name,
363 &pjmedia_codec_amr_match_sdp);
364 if (status != PJ_SUCCESS)
365 goto on_error;
366 #endif
367
368 /* Suppress compile warning */
369 PJ_UNUSED_ARG(codec_name);
370
371 /* Register codec factory to endpoint. */
372 status = pjmedia_codec_mgr_register_factory(codec_mgr,
373 &codec_factory.base);
374 if (status != PJ_SUCCESS)
375 goto on_error;
376
377 /* Done. */
378 return PJ_SUCCESS;
379
380 on_error:
381 pj_pool_release(codec_factory.pool);
382 codec_factory.pool = NULL;
383 return status;
384 }
385
386 /*
387 * Initialize and register passthrough codec factory to pjmedia endpoint.
388 */
389 PJ_DEF(pj_status_t) pjmedia_codec_passthrough_init2(
390 pjmedia_endpt *endpt,
391 const pjmedia_codec_passthrough_setting *setting)
392 {
393 if (codec_factory.pool != NULL) {
394 /* Already initialized. */
395 return PJ_EEXISTS;
396 }
397
398 if (setting != NULL) {
399 unsigned i;
400
401 /* Enable/disable codecs based on the specified encoding formats */
402 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
403 pj_bool_t enabled = PJ_FALSE;
404 unsigned j;
405
406 for (j = 0; j < setting->fmt_cnt && !enabled; ++j) {
407 if ((pj_uint32_t)codec_desc[i].fmt_id == setting->fmts[j].id)
408 enabled = PJ_TRUE;
409 }
410
411 codec_desc[i].enabled = enabled;
412 }
413
414 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC
415 /* Update iLBC codec description based on default mode setting. */
416 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
417 if (codec_desc[i].enabled &&
418 codec_desc[i].fmt_id == PJMEDIA_FORMAT_ILBC)
419 {
420 codec_desc[i].samples_per_frame =
421 (setting->ilbc_mode == 20? 160 : 240);
422 codec_desc[i].def_bitrate =
423 (setting->ilbc_mode == 20? 15200 : 13333);
424 pj_strset2(&codec_desc[i].dec_fmtp.param[0].val,
425 (setting->ilbc_mode == 20? "20" : "30"));
426 break;
427 }
428 }
429 #endif
430 }
431
432 return pjmedia_codec_passthrough_init(endpt);
433 }
434
435 /*
436 * Unregister passthrough codecs factory from pjmedia endpoint.
437 */
438 PJ_DEF(pj_status_t) pjmedia_codec_passthrough_deinit(void)
439 {
440 pjmedia_codec_mgr *codec_mgr;
441 unsigned i;
442 pj_status_t status;
443
444 if (codec_factory.pool == NULL) {
445 /* Already deinitialized */
446 return PJ_SUCCESS;
447 }
448
449 pj_mutex_lock(codec_factory.mutex);
450
451 /* Get the codec manager. */
452 codec_mgr = pjmedia_endpt_get_codec_mgr(codec_factory.endpt);
453 if (!codec_mgr) {
454 pj_pool_release(codec_factory.pool);
455 codec_factory.pool = NULL;
456 pj_mutex_unlock(codec_factory.mutex);
457 return PJ_EINVALIDOP;
458 }
459
460 /* Release the factory mutex before calling unregister to avoid lock
461 * order inversion with codec manager mutex.
462 */
463 pj_mutex_unlock(codec_factory.mutex);
464
465 /* Unregister passthrough codecs factory. */
466 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
467 &codec_factory.base);
468
469 /* Destroy mutex. */
470 pj_mutex_destroy(codec_factory.mutex);
471 codec_factory.mutex = NULL;
472
473 /* Destroy pool. */
474 pj_pool_release(codec_factory.pool);
475 codec_factory.pool = NULL;
476
477 /* Re-enable all codecs in the codec_desc. */
478 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
479 codec_desc[i].enabled = PJ_TRUE;
480 }
481
482 return status;
483 }
484
485 /*
486 * Check if factory can allocate the specified codec.
487 */
488 static pj_status_t test_alloc( pjmedia_codec_factory *factory,
489 const pjmedia_codec_info *info )
490 {
491 unsigned i;
492
493 PJ_UNUSED_ARG(factory);
494
495 /* Type MUST be audio. */
496 if (info->type != PJMEDIA_TYPE_AUDIO)
497 return PJMEDIA_CODEC_EUNSUP;
498
499 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
500 pj_str_t name = pj_str((char*)codec_desc[i].name);
501 if ((pj_stricmp(&info->encoding_name, &name) == 0) &&
502 (info->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
503 (info->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
504 (codec_desc[i].enabled))
505 {
506 return PJ_SUCCESS;
507 }
508 }
509
510 /* Unsupported, or mode is disabled. */
511 return PJMEDIA_CODEC_EUNSUP;
512 }
513
514 /*
515 * Generate default attribute.
516 */
517 static pj_status_t default_attr ( pjmedia_codec_factory *factory,
518 const pjmedia_codec_info *id,
519 pjmedia_codec_param *attr )
520 {
521 unsigned i;
522
523 PJ_ASSERT_RETURN(factory==&codec_factory.base, PJ_EINVAL);
524
525 pj_bzero(attr, sizeof(pjmedia_codec_param));
526
527 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
528 pj_str_t name = pj_str((char*)codec_desc[i].name);
529 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
530 (id->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
531 (id->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
532 (id->pt == (unsigned)codec_desc[i].pt))
533 {
534 attr->info.pt = (pj_uint8_t)id->pt;
535 attr->info.channel_cnt = codec_desc[i].channel_count;
536 attr->info.clock_rate = codec_desc[i].clock_rate;
537 attr->info.avg_bps = codec_desc[i].def_bitrate;
538 attr->info.max_bps = codec_desc[i].max_bitrate;
539 attr->info.pcm_bits_per_sample = 16;
540 attr->info.frm_ptime = (pj_uint16_t)
541 (codec_desc[i].samples_per_frame * 1000 /
542 codec_desc[i].channel_count /
543 codec_desc[i].clock_rate);
544 attr->info.fmt_id = codec_desc[i].fmt_id;
545
546 /* Default flags. */
547 attr->setting.frm_per_pkt = codec_desc[i].frm_per_pkt;
548 attr->setting.plc = codec_desc[i].plc;
549 attr->setting.penh= 0;
550 attr->setting.vad = codec_desc[i].vad;
551 attr->setting.cng = attr->setting.vad;
552 attr->setting.dec_fmtp = codec_desc[i].dec_fmtp;
553
554 if (attr->setting.vad == 0) {
555 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
556 if (id->pt == PJMEDIA_RTP_PT_G729) {
557 /* Signal G729 Annex B is being disabled */
558 attr->setting.dec_fmtp.cnt = 1;
559 pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb");
560 pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no");
561 }
562 #endif
563 }
564
565 return PJ_SUCCESS;
566 }
567 }
568
569 return PJMEDIA_CODEC_EUNSUP;
570 }
571
572 /*
573 * Enum codecs supported by this factory.
574 */
575 static pj_status_t enum_codecs( pjmedia_codec_factory *factory,
576 unsigned *count,
577 pjmedia_codec_info codecs[])
578 {
579 unsigned max;
580 unsigned i;
581
582 PJ_UNUSED_ARG(factory);
583 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
584
585 max = *count;
586
587 for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(codec_desc) && *count < max; ++i)
588 {
589 if (!codec_desc[i].enabled)
590 continue;
591
592 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
593 codecs[*count].encoding_name = pj_str((char*)codec_desc[i].name);
594 codecs[*count].pt = codec_desc[i].pt;
595 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
596 codecs[*count].clock_rate = codec_desc[i].clock_rate;
597 codecs[*count].channel_cnt = codec_desc[i].channel_count;
598
599 ++*count;
600 }
601
602 return PJ_SUCCESS;
603 }
604
605 /*
606 * Allocate a new codec instance.
607 */
608 static pj_status_t alloc_codec( pjmedia_codec_factory *factory,
609 const pjmedia_codec_info *id,
610 pjmedia_codec **p_codec)
611 {
612 codec_private_t *codec_data;
613 pjmedia_codec *codec;
614 int idx;
615 pj_pool_t *pool;
616 unsigned i;
617
618 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
619 PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
620
621 pj_mutex_lock(codec_factory.mutex);
622
623 /* Find codec's index */
624 idx = -1;
625 for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
626 pj_str_t name = pj_str((char*)codec_desc[i].name);
627 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
628 (id->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
629 (id->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
630 (codec_desc[i].enabled))
631 {
632 idx = i;
633 break;
634 }
635 }
636 if (idx == -1) {
637 *p_codec = NULL;
638 pj_mutex_unlock(codec_factory.mutex);
639 return PJMEDIA_CODEC_EUNSUP;
640 }
641
642 /* Create pool for codec instance */
643 pool = pjmedia_endpt_create_pool(codec_factory.endpt, "passthroughcodec",
644 512, 512);
645 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
646 codec->op = &codec_op;
647 codec->factory = factory;
648 codec->codec_data = PJ_POOL_ZALLOC_T(pool, codec_private_t);
649 codec_data = (codec_private_t*) codec->codec_data;
650 codec_data->pool = pool;
651 codec_data->codec_idx = idx;
652
653 pj_mutex_unlock(codec_factory.mutex);
654
655 *p_codec = codec;
656 return PJ_SUCCESS;
657 }
658
659 /*
660 * Free codec.
661 */
662 static pj_status_t dealloc_codec( pjmedia_codec_factory *factory,
663 pjmedia_codec *codec )
664 {
665 codec_private_t *codec_data;
666
667 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
668 PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
669
670 /* Close codec, if it's not closed. */
671 codec_data = (codec_private_t*) codec->codec_data;
672 codec_close(codec);
673
674 pj_pool_release(codec_data->pool);
675
676 return PJ_SUCCESS;
677 }
678
679 /*
680 * Init codec.
681 */
682 static pj_status_t codec_init( pjmedia_codec *codec,
683 pj_pool_t *pool )
684 {
685 PJ_UNUSED_ARG(codec);
686 PJ_UNUSED_ARG(pool);
687 return PJ_SUCCESS;
688 }
689
690 /*
691 * Open codec.
692 */
693 static pj_status_t codec_open( pjmedia_codec *codec,
694 pjmedia_codec_param *attr )
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 int i, j;
699
700 /* Cache samples per frame value */
701 codec_data->samples_per_frame = desc->samples_per_frame;
702
703 /* Calculate bitstream size */
704 i = attr->info.avg_bps * codec_data->samples_per_frame;
705 j = desc->clock_rate << 3;
706 codec_data->avg_frame_size = (pj_uint16_t)(i / j);
707 if (i % j) ++codec_data->avg_frame_size;
708
709 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
710 /* Init AMR settings */
711 if (desc->pt == PJMEDIA_RTP_PT_AMR || desc->pt == PJMEDIA_RTP_PT_AMRWB) {
712 amr_settings_t *s;
713 pj_uint8_t octet_align = 0;
714 pj_int8_t enc_mode;
715
716 enc_mode = pjmedia_codec_amr_get_mode(attr->info.avg_bps);
717 pj_assert(enc_mode >= 0 && enc_mode <= 8);
718
719 for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
720 const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11};
721
722 /* Fetch octet-align setting. It should be fine to fetch only
723 * the decoder, since encoder & decoder must use the same setting
724 * (RFC 4867 section 8.3.1).
725 */
726 if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name,
727 &STR_FMTP_OCTET_ALIGN) == 0)
728 {
729 octet_align=(pj_uint8_t)
730 (pj_strtoul(&attr->setting.dec_fmtp.param[i].val));
731 break;
732 }
733 }
734
735 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
736 const pj_str_t STR_FMTP_MODE_SET = {"mode-set", 8};
737
738 /* mode-set, encoding mode is chosen based on local default mode
739 * setting:
740 * - if local default mode is included in the mode-set, use it
741 * - otherwise, find the closest mode to local default mode;
742 * if there are two closest modes, prefer to use the higher
743 * one, e.g: local default mode is 4, the mode-set param
744 * contains '2,3,5,6', then 5 will be chosen.
745 */
746 if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name,
747 &STR_FMTP_MODE_SET) == 0)
748 {
749 const char *p;
750 pj_size_t l;
751 pj_int8_t diff = 99;
752
753 p = pj_strbuf(&attr->setting.enc_fmtp.param[i].val);
754 l = pj_strlen(&attr->setting.enc_fmtp.param[i].val);
755
756 while (l--) {
757 if ((desc->pt==PJMEDIA_RTP_PT_AMR && *p>='0' && *p<='7') ||
758 (desc->pt==PJMEDIA_RTP_PT_AMRWB && *p>='0' && *p<='8'))
759 {
760 pj_int8_t tmp = (pj_int8_t)(*p - '0' - enc_mode);
761
762 if (PJ_ABS(diff) > PJ_ABS(tmp) ||
763 (PJ_ABS(diff) == PJ_ABS(tmp) && tmp > diff))
764 {
765 diff = tmp;
766 if (diff == 0) break;
767 }
768 }
769 ++p;
770 }
771
772 if (diff == 99)
773 return PJMEDIA_CODEC_EFAILED;
774
775 enc_mode = (pj_int8_t)(enc_mode + diff);
776
777 break;
778 }
779 }
780
781 s = PJ_POOL_ZALLOC_T(codec_data->pool, amr_settings_t);
782 codec_data->codec_setting = s;
783
784 s->enc_mode = enc_mode;
785 if (s->enc_mode < 0)
786 return PJMEDIA_CODEC_EINMODE;
787
788 s->enc_setting.amr_nb = (pj_uint8_t)(desc->pt == PJMEDIA_RTP_PT_AMR);
789 s->enc_setting.octet_aligned = octet_align;
790 s->enc_setting.reorder = PJ_FALSE; /* Note this! passthrough codec
791 doesn't do sensitivity bits
792 reordering */
793 s->enc_setting.cmr = 15;
794
795 s->dec_setting.amr_nb = (pj_uint8_t)(desc->pt == PJMEDIA_RTP_PT_AMR);
796 s->dec_setting.octet_aligned = octet_align;
797 s->dec_setting.reorder = PJ_FALSE; /* Note this! passthrough codec
798 doesn't do sensitivity bits
799 reordering */
800
801 /* Return back bitrate info to application */
802 attr->info.avg_bps = s->enc_setting.amr_nb?
803 pjmedia_codec_amrnb_bitrates[s->enc_mode]:
804 pjmedia_codec_amrwb_bitrates[s->enc_mode];
805 }
806 #endif
807
808 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC
809 /* Init iLBC settings */
810 if (desc->pt == PJMEDIA_RTP_PT_ILBC)
811 {
812 enum { DEFAULT_MODE = 30 };
813 static pj_str_t STR_MODE = {"mode", 4};
814 pj_uint16_t dec_fmtp_mode = DEFAULT_MODE,
815 enc_fmtp_mode = DEFAULT_MODE;
816
817 /* Get decoder mode */
818 for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
819 if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name, &STR_MODE) == 0)
820 {
821 dec_fmtp_mode = (pj_uint16_t)
822 pj_strtoul(&attr->setting.dec_fmtp.param[i].val);
823 break;
824 }
825 }
826
827 /* Decoder mode must be set */
828 PJ_ASSERT_RETURN(dec_fmtp_mode == 20 || dec_fmtp_mode == 30,
829 PJMEDIA_CODEC_EINMODE);
830
831 /* Get encoder mode */
832 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
833 if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name, &STR_MODE) == 0)
834 {
835 enc_fmtp_mode = (pj_uint16_t)
836 pj_strtoul(&attr->setting.enc_fmtp.param[i].val);
837 break;
838 }
839 }
840
841 PJ_ASSERT_RETURN(enc_fmtp_mode==20 || enc_fmtp_mode==30,
842 PJMEDIA_CODEC_EINMODE);
843
844 /* Both sides of a bi-directional session MUST use the same "mode" value.
845 * In this point, possible values are only 20 or 30, so when encoder and
846 * decoder modes are not same, just use the default mode, it is 30.
847 */
848 if (enc_fmtp_mode != dec_fmtp_mode) {
849 enc_fmtp_mode = dec_fmtp_mode = DEFAULT_MODE;
850 PJ_LOG(4,(codec_data->pool->obj_name,
851 "Normalized iLBC encoder and decoder modes to %d",
852 DEFAULT_MODE));
853 }
854
855 /* Update some attributes based on negotiated mode. */
856 attr->info.avg_bps = (dec_fmtp_mode == 30? 13333 : 15200);
857 attr->info.frm_ptime = dec_fmtp_mode;
858
859 /* Override average frame size */
860 codec_data->avg_frame_size = (dec_fmtp_mode == 30? 50 : 38);
861
862 /* Override samples per frame */
863 codec_data->samples_per_frame = (dec_fmtp_mode == 30? 240 : 160);
864 }
865 #endif
866
867 return PJ_SUCCESS;
868 }
869
870 /*
871 * Close codec.
872 */
873 static pj_status_t codec_close( pjmedia_codec *codec )
874 {
875 PJ_UNUSED_ARG(codec);
876
877 return PJ_SUCCESS;
878 }
879
880
881 /*
882 * Modify codec settings.
883 */
884 static pj_status_t codec_modify( pjmedia_codec *codec,
885 const pjmedia_codec_param *attr )
886 {
887 /* Not supported yet. */
888 PJ_UNUSED_ARG(codec);
889 PJ_UNUSED_ARG(attr);
890
891 return PJ_ENOTSUP;
892 }
893
894 /*
895 * Get frames in the packet.
896 */
897 static pj_status_t codec_parse( pjmedia_codec *codec,
898 void *pkt,
899 pj_size_t pkt_size,
900 const pj_timestamp *ts,
901 unsigned *frame_cnt,
902 pjmedia_frame frames[])
903 {
904 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
905 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
906 unsigned count = 0;
907
908 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
909
910 if (desc->parse != NULL) {
911 return desc->parse(codec_data, pkt, pkt_size, ts, frame_cnt, frames);
912 }
913
914 while (pkt_size >= codec_data->avg_frame_size && count < *frame_cnt) {
915 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
916 frames[count].buf = pkt;
917 frames[count].size = codec_data->avg_frame_size;
918 frames[count].timestamp.u64 = ts->u64 +
919 count * codec_data->samples_per_frame;
920
921 pkt = (pj_uint8_t*)pkt + codec_data->avg_frame_size;
922 pkt_size -= codec_data->avg_frame_size;
923
924 ++count;
925 }
926
927 if (pkt_size && count < *frame_cnt) {
928 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
929 frames[count].buf = pkt;
930 frames[count].size = pkt_size;
931 frames[count].timestamp.u64 = ts->u64 +
932 count * codec_data->samples_per_frame;
933 ++count;
934 }
935
936 *frame_cnt = count;
937 return PJ_SUCCESS;
938 }
939
940 /*
941 * Encode frames.
942 */
943 static pj_status_t codec_encode( pjmedia_codec *codec,
944 const struct pjmedia_frame *input,
945 unsigned output_buf_len,
946 struct pjmedia_frame *output)
947 {
948 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
949 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
950 const pjmedia_frame_ext *input_ = (const pjmedia_frame_ext*) input;
951
952 pj_assert(input && input->type == PJMEDIA_FRAME_TYPE_EXTENDED);
953
954 if (desc->pack != NULL) {
955 desc->pack(codec_data, input_, output_buf_len, output);
956 } else {
957 if (input_->subframe_cnt == 0) {
958 /* DTX */
959 output->buf = NULL;
960 output->size = 0;
961 output->type = PJMEDIA_FRAME_TYPE_NONE;
962 } else {
963 unsigned i;
964 pj_uint8_t *p = output->buf;
965
966 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
967 output->size = 0;
968
969 for (i = 0; i < input_->subframe_cnt; ++i) {
970 pjmedia_frame_ext_subframe *sf;
971 unsigned sf_len;
972
973 sf = pjmedia_frame_ext_get_subframe(input_, i);
974 pj_assert(sf);
975
976 sf_len = (sf->bitlen + 7) >> 3;
977
978 pj_memcpy(p, sf->data, sf_len);
979 p += sf_len;
980 output->size += sf_len;
981
982 /* If there is SID or DTX frame, break the loop. */
983 if (desc->pt == PJMEDIA_RTP_PT_G729 &&
984 sf_len < codec_data->avg_frame_size)
985 {
986 break;
987 }
988
989 }
990 }
991 }
992
993 output->timestamp = input->timestamp;
994
995 return PJ_SUCCESS;
996 }
997
998 /*
999 * Decode frame.
1000 */
1001 static pj_status_t codec_decode( pjmedia_codec *codec,
1002 const struct pjmedia_frame *input,
1003 unsigned output_buf_len,
1004 struct pjmedia_frame *output)
1005 {
1006 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
1007 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
1008 struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
1009 #endif
1010 pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output;
1011
1012 pj_assert(input);
1013 PJ_UNUSED_ARG(output_buf_len);
1014
1015 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
1016 /* Need to rearrange the AMR bitstream, since the bitstream may not be
1017 * started from bit 0 or may need to be reordered from sensitivity order
1018 * into encoder bits order.
1019 */
1020 if (desc->pt == PJMEDIA_RTP_PT_AMR || desc->pt == PJMEDIA_RTP_PT_AMRWB) {
1021 pjmedia_frame input_;
1022 pjmedia_codec_amr_pack_setting *setting;
1023
1024 setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting;
1025
1026 input_ = *input;
1027 pjmedia_codec_amr_predecode(input, setting, &input_);
1028
1029 pjmedia_frame_ext_append_subframe(output_, input_.buf,
1030 (pj_uint16_t)(input_.size << 3),
1031 (pj_uint16_t)codec_data->samples_per_frame);
1032 output->timestamp = input->timestamp;
1033
1034 return PJ_SUCCESS;
1035 }
1036 #endif
1037
1038 pjmedia_frame_ext_append_subframe(output_, input->buf,
1039 (pj_uint16_t)(input->size << 3),
1040 (pj_uint16_t)codec_data->samples_per_frame);
1041 output->timestamp = input->timestamp;
1042
1043 return PJ_SUCCESS;
1044 }
1045
1046 /*
1047 * Recover lost frame.
1048 */
1049 static pj_status_t codec_recover( pjmedia_codec *codec,
1050 unsigned output_buf_len,
1051 struct pjmedia_frame *output)
1052 {
1053 codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
1054 pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output;
1055
1056 PJ_UNUSED_ARG(output_buf_len);
1057
1058 pjmedia_frame_ext_append_subframe(output_, NULL, 0,
1059 (pj_uint16_t)codec_data->samples_per_frame);
1060
1061 return PJ_SUCCESS;
1062 }
1063
1064 #endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */
1065