Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 1 | /* $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 */ |
| 40 | static pj_status_t test_alloc( pjmedia_codec_factory *factory, |
| 41 | const pjmedia_codec_info *id ); |
| 42 | static pj_status_t default_attr( pjmedia_codec_factory *factory, |
| 43 | const pjmedia_codec_info *id, |
| 44 | pjmedia_codec_param *attr ); |
| 45 | static pj_status_t enum_codecs( pjmedia_codec_factory *factory, |
| 46 | unsigned *count, |
| 47 | pjmedia_codec_info codecs[]); |
| 48 | static pj_status_t alloc_codec( pjmedia_codec_factory *factory, |
| 49 | const pjmedia_codec_info *id, |
| 50 | pjmedia_codec **p_codec); |
| 51 | static pj_status_t dealloc_codec( pjmedia_codec_factory *factory, |
| 52 | pjmedia_codec *codec ); |
| 53 | |
| 54 | /* Prototypes for passthrough codecs implementation. */ |
| 55 | static pj_status_t codec_init( pjmedia_codec *codec, |
| 56 | pj_pool_t *pool ); |
| 57 | static pj_status_t codec_open( pjmedia_codec *codec, |
| 58 | pjmedia_codec_param *attr ); |
| 59 | static pj_status_t codec_close( pjmedia_codec *codec ); |
| 60 | static pj_status_t codec_modify(pjmedia_codec *codec, |
| 61 | const pjmedia_codec_param *attr ); |
| 62 | static 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[]); |
| 68 | static pj_status_t codec_encode( pjmedia_codec *codec, |
| 69 | const struct pjmedia_frame *input, |
| 70 | unsigned output_buf_len, |
| 71 | struct pjmedia_frame *output); |
| 72 | static pj_status_t codec_decode( pjmedia_codec *codec, |
| 73 | const struct pjmedia_frame *input, |
| 74 | unsigned output_buf_len, |
| 75 | struct pjmedia_frame *output); |
| 76 | static 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. */ |
| 81 | static 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. */ |
| 94 | static 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 */ |
| 104 | static 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. */ |
| 112 | typedef 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 | */ |
| 128 | typedef 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 | */ |
| 137 | typedef 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. */ |
| 144 | static 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[]); |
| 147 | static 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. */ |
| 154 | static struct codec_desc { |
| 155 | int enabled; /* Is this codec enabled? */ |
| 156 | const char *name; /* Codec name. */ |
| 157 | pj_uint8_t pt; /* Payload type. */ |
| 158 | pjmedia_fourcc format; /* Source format. */ |
| 159 | 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.*/ |
| 165 | parse_cb parse; /* Callback to parse bitstream. */ |
| 166 | pack_cb pack; /* Callback to pack bitstream. */ |
| 167 | pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */ |
| 168 | } |
| 169 | |
| 170 | codec_desc[] = |
| 171 | { |
| 172 | # if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 173 | {1, "AMR", PJMEDIA_RTP_PT_AMR, {PJMEDIA_FOURCC_AMR}, |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 174 | 8000, 1, 160, |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 175 | 12200, 12200, 2, |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 176 | &parse_amr, &pack_amr |
| 177 | /*, {1, {{{"octet-align", 11}, {"1", 1}}} } */ |
| 178 | }, |
| 179 | # endif |
| 180 | |
| 181 | # if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729 |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 182 | {1, "G729", PJMEDIA_RTP_PT_G729, {PJMEDIA_FOURCC_G729}, |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 183 | 8000, 1, 80, |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 184 | 8000, 8000, 2 |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 185 | }, |
| 186 | # endif |
| 187 | |
| 188 | # if PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 189 | {1, "iLBC", PJMEDIA_RTP_PT_ILBC, {PJMEDIA_FOURCC_ILBC}, |
| 190 | 8000, 1, 240, |
| 191 | 13333, 15200, 2, |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 192 | NULL, NULL, |
| 193 | {1, {{{"mode", 4}, {"30", 2}}} } |
| 194 | }, |
| 195 | # endif |
| 196 | |
| 197 | # if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 198 | {1, "PCMU", PJMEDIA_RTP_PT_PCMU, {PJMEDIA_FOURCC_G711U}, |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 199 | 8000, 1, 80, |
| 200 | 64000, 64000, 2 |
| 201 | }, |
| 202 | # endif |
| 203 | |
| 204 | # if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 205 | {1, "PCMA", PJMEDIA_RTP_PT_PCMA, {PJMEDIA_FOURCC_G711A}, |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 206 | 8000, 1, 80, |
| 207 | 64000, 64000, 2 |
| 208 | }, |
| 209 | # endif |
| 210 | }; |
| 211 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 212 | |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 213 | #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR |
| 214 | |
| 215 | #include <pjmedia-codec/amr_helper.h> |
| 216 | |
| 217 | typedef struct amr_settings_t { |
| 218 | pjmedia_codec_amr_pack_setting enc_setting; |
| 219 | pjmedia_codec_amr_pack_setting dec_setting; |
| 220 | pj_int8_t enc_mode; |
| 221 | } amr_settings_t; |
| 222 | |
| 223 | |
| 224 | /* Pack AMR payload */ |
| 225 | static pj_status_t pack_amr ( codec_private_t *codec_data, |
| 226 | const struct pjmedia_frame_ext *input, |
| 227 | unsigned output_buf_len, |
| 228 | struct pjmedia_frame *output) |
| 229 | { |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 230 | enum {MAX_FRAMES_PER_PACKET = 8}; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 231 | |
| 232 | pjmedia_frame frames[MAX_FRAMES_PER_PACKET]; |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 233 | amr_settings_t* setting = (amr_settings_t*)codec_data->codec_setting; |
| 234 | pjmedia_codec_amr_pack_setting *enc_setting = &setting->enc_setting; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 235 | pj_uint8_t SID_FT; |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 236 | unsigned i; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 237 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 238 | pj_assert(input->subframe_cnt <= MAX_FRAMES_PER_PACKET); |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 239 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 240 | SID_FT = (pj_uint8_t)(enc_setting->amr_nb? 8 : 9); |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 241 | |
| 242 | /* Get frames */ |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 243 | for (i = 0; i < input->subframe_cnt; ++i) { |
| 244 | pjmedia_frame_ext_subframe *sf; |
| 245 | pjmedia_codec_amr_bit_info *info; |
| 246 | unsigned len; |
| 247 | |
| 248 | sf = pjmedia_frame_ext_get_subframe(input, i); |
| 249 | |
| 250 | len = sf->bitlen >> 3; |
| 251 | if (sf->bitlen & 0x07) |
| 252 | ++len; |
| 253 | |
| 254 | info = (pjmedia_codec_amr_bit_info*) &frames[i].bit_info; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 255 | pj_bzero(info, sizeof(*info)); |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 256 | info->frame_type = pjmedia_codec_amr_get_mode2(enc_setting->amr_nb, |
| 257 | len); |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 258 | info->good_quality = 1; |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 259 | info->mode = setting->enc_mode; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 260 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 261 | frames[i].buf = sf->data; |
| 262 | frames[i].size = len; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 265 | output->size = output_buf_len; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 266 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 267 | return pjmedia_codec_amr_pack(frames, input->subframe_cnt, enc_setting, |
| 268 | output->buf, &output->size); |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | /* Parse AMR payload into frames. */ |
| 273 | static pj_status_t parse_amr(codec_private_t *codec_data, void *pkt, |
| 274 | pj_size_t pkt_size, const pj_timestamp *ts, |
| 275 | unsigned *frame_cnt, pjmedia_frame frames[]) |
| 276 | { |
| 277 | amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting; |
| 278 | pjmedia_codec_amr_pack_setting *setting; |
| 279 | pj_status_t status; |
| 280 | pj_uint8_t cmr; |
| 281 | |
| 282 | setting = &s->dec_setting; |
| 283 | |
| 284 | status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames, |
| 285 | frame_cnt, &cmr); |
| 286 | if (status != PJ_SUCCESS) |
| 287 | return status; |
| 288 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 289 | // CMR is not supported for now. |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 290 | /* Check Change Mode Request. */ |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 291 | //if ((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) { |
| 292 | // s->enc_mode = cmr; |
| 293 | //} |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 294 | |
| 295 | return PJ_SUCCESS; |
| 296 | } |
| 297 | |
| 298 | #endif /* PJMEDIA_HAS_PASSTROUGH_CODEC_AMR */ |
| 299 | |
| 300 | |
| 301 | /* |
| 302 | * Initialize and register passthrough codec factory to pjmedia endpoint. |
| 303 | */ |
| 304 | PJ_DEF(pj_status_t) pjmedia_codec_passthrough_init( pjmedia_endpt *endpt ) |
| 305 | { |
| 306 | pjmedia_codec_mgr *codec_mgr; |
| 307 | pj_status_t status; |
| 308 | |
| 309 | if (codec_factory.pool != NULL) { |
| 310 | /* Already initialized. */ |
| 311 | return PJ_SUCCESS; |
| 312 | } |
| 313 | |
| 314 | /* Create passthrough codec factory. */ |
| 315 | codec_factory.base.op = &codec_factory_op; |
| 316 | codec_factory.base.factory_data = NULL; |
| 317 | codec_factory.endpt = endpt; |
| 318 | |
| 319 | codec_factory.pool = pjmedia_endpt_create_pool(endpt, "Passthrough codecs", |
| 320 | 4000, 4000); |
| 321 | if (!codec_factory.pool) |
| 322 | return PJ_ENOMEM; |
| 323 | |
| 324 | /* Create mutex. */ |
| 325 | status = pj_mutex_create_simple(codec_factory.pool, "Passthrough codecs", |
| 326 | &codec_factory.mutex); |
| 327 | if (status != PJ_SUCCESS) |
| 328 | goto on_error; |
| 329 | |
| 330 | /* Get the codec manager. */ |
| 331 | codec_mgr = pjmedia_endpt_get_codec_mgr(endpt); |
| 332 | if (!codec_mgr) { |
| 333 | status = PJ_EINVALIDOP; |
| 334 | goto on_error; |
| 335 | } |
| 336 | |
| 337 | /* Register codec factory to endpoint. */ |
| 338 | status = pjmedia_codec_mgr_register_factory(codec_mgr, |
| 339 | &codec_factory.base); |
| 340 | if (status != PJ_SUCCESS) |
| 341 | goto on_error; |
| 342 | |
| 343 | /* Done. */ |
| 344 | return PJ_SUCCESS; |
| 345 | |
| 346 | on_error: |
| 347 | pj_pool_release(codec_factory.pool); |
| 348 | codec_factory.pool = NULL; |
| 349 | return status; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Unregister passthrough codecs factory from pjmedia endpoint. |
| 354 | */ |
| 355 | PJ_DEF(pj_status_t) pjmedia_codec_passthrough_deinit(void) |
| 356 | { |
| 357 | pjmedia_codec_mgr *codec_mgr; |
| 358 | pj_status_t status; |
| 359 | |
| 360 | if (codec_factory.pool == NULL) { |
| 361 | /* Already deinitialized */ |
| 362 | return PJ_SUCCESS; |
| 363 | } |
| 364 | |
| 365 | pj_mutex_lock(codec_factory.mutex); |
| 366 | |
| 367 | /* Get the codec manager. */ |
| 368 | codec_mgr = pjmedia_endpt_get_codec_mgr(codec_factory.endpt); |
| 369 | if (!codec_mgr) { |
| 370 | pj_pool_release(codec_factory.pool); |
| 371 | codec_factory.pool = NULL; |
| 372 | return PJ_EINVALIDOP; |
| 373 | } |
| 374 | |
| 375 | /* Unregister passthrough codecs factory. */ |
| 376 | status = pjmedia_codec_mgr_unregister_factory(codec_mgr, |
| 377 | &codec_factory.base); |
| 378 | |
| 379 | /* Destroy mutex. */ |
| 380 | pj_mutex_destroy(codec_factory.mutex); |
| 381 | |
| 382 | /* Destroy pool. */ |
| 383 | pj_pool_release(codec_factory.pool); |
| 384 | codec_factory.pool = NULL; |
| 385 | |
| 386 | return status; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Check if factory can allocate the specified codec. |
| 391 | */ |
| 392 | static pj_status_t test_alloc( pjmedia_codec_factory *factory, |
| 393 | const pjmedia_codec_info *info ) |
| 394 | { |
| 395 | unsigned i; |
| 396 | |
| 397 | PJ_UNUSED_ARG(factory); |
| 398 | |
| 399 | /* Type MUST be audio. */ |
| 400 | if (info->type != PJMEDIA_TYPE_AUDIO) |
| 401 | return PJMEDIA_CODEC_EUNSUP; |
| 402 | |
| 403 | for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) { |
| 404 | pj_str_t name = pj_str((char*)codec_desc[i].name); |
| 405 | if ((pj_stricmp(&info->encoding_name, &name) == 0) && |
| 406 | (info->clock_rate == (unsigned)codec_desc[i].clock_rate) && |
| 407 | (info->channel_cnt == (unsigned)codec_desc[i].channel_count) && |
| 408 | (codec_desc[i].enabled)) |
| 409 | { |
| 410 | return PJ_SUCCESS; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* Unsupported, or mode is disabled. */ |
| 415 | return PJMEDIA_CODEC_EUNSUP; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Generate default attribute. |
| 420 | */ |
| 421 | static pj_status_t default_attr ( pjmedia_codec_factory *factory, |
| 422 | const pjmedia_codec_info *id, |
| 423 | pjmedia_codec_param *attr ) |
| 424 | { |
| 425 | unsigned i; |
| 426 | |
| 427 | PJ_ASSERT_RETURN(factory==&codec_factory.base, PJ_EINVAL); |
| 428 | |
| 429 | pj_bzero(attr, sizeof(pjmedia_codec_param)); |
| 430 | |
| 431 | for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) { |
| 432 | pj_str_t name = pj_str((char*)codec_desc[i].name); |
| 433 | if ((pj_stricmp(&id->encoding_name, &name) == 0) && |
| 434 | (id->clock_rate == (unsigned)codec_desc[i].clock_rate) && |
| 435 | (id->channel_cnt == (unsigned)codec_desc[i].channel_count) && |
| 436 | (id->pt == (unsigned)codec_desc[i].pt)) |
| 437 | { |
| 438 | attr->info.pt = (pj_uint8_t)id->pt; |
| 439 | attr->info.channel_cnt = codec_desc[i].channel_count; |
| 440 | attr->info.clock_rate = codec_desc[i].clock_rate; |
| 441 | attr->info.avg_bps = codec_desc[i].def_bitrate; |
| 442 | attr->info.max_bps = codec_desc[i].max_bitrate; |
| 443 | attr->info.pcm_bits_per_sample = 16; |
| 444 | attr->info.frm_ptime = (pj_uint16_t) |
| 445 | (codec_desc[i].samples_per_frame * 1000 / |
| 446 | codec_desc[i].channel_count / |
| 447 | codec_desc[i].clock_rate); |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 448 | attr->info.format = codec_desc[i].format; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 449 | |
| 450 | /* Default flags. */ |
| 451 | attr->setting.frm_per_pkt = codec_desc[i].frm_per_pkt; |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 452 | attr->setting.plc = 0; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 453 | attr->setting.penh= 0; |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 454 | attr->setting.vad = 0; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 455 | attr->setting.cng = attr->setting.vad; |
| 456 | attr->setting.dec_fmtp = codec_desc[i].dec_fmtp; |
| 457 | |
| 458 | if (attr->setting.vad == 0) { |
| 459 | #if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729 |
| 460 | if (id->pt == PJMEDIA_RTP_PT_G729) { |
| 461 | /* Signal G729 Annex B is being disabled */ |
| 462 | attr->setting.dec_fmtp.cnt = 1; |
| 463 | pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb"); |
| 464 | pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no"); |
| 465 | } |
| 466 | #endif |
| 467 | } |
| 468 | |
| 469 | return PJ_SUCCESS; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | return PJMEDIA_CODEC_EUNSUP; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Enum codecs supported by this factory. |
| 478 | */ |
| 479 | static pj_status_t enum_codecs( pjmedia_codec_factory *factory, |
| 480 | unsigned *count, |
| 481 | pjmedia_codec_info codecs[]) |
| 482 | { |
| 483 | unsigned max; |
| 484 | unsigned i; |
| 485 | |
| 486 | PJ_UNUSED_ARG(factory); |
| 487 | PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); |
| 488 | |
| 489 | max = *count; |
| 490 | |
| 491 | for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(codec_desc) && *count < max; ++i) |
| 492 | { |
| 493 | if (!codec_desc[i].enabled) |
| 494 | continue; |
| 495 | |
| 496 | pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); |
| 497 | codecs[*count].encoding_name = pj_str((char*)codec_desc[i].name); |
| 498 | codecs[*count].pt = codec_desc[i].pt; |
| 499 | codecs[*count].type = PJMEDIA_TYPE_AUDIO; |
| 500 | codecs[*count].clock_rate = codec_desc[i].clock_rate; |
| 501 | codecs[*count].channel_cnt = codec_desc[i].channel_count; |
| 502 | |
| 503 | ++*count; |
| 504 | } |
| 505 | |
| 506 | return PJ_SUCCESS; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Allocate a new codec instance. |
| 511 | */ |
| 512 | static pj_status_t alloc_codec( pjmedia_codec_factory *factory, |
| 513 | const pjmedia_codec_info *id, |
| 514 | pjmedia_codec **p_codec) |
| 515 | { |
| 516 | codec_private_t *codec_data; |
| 517 | pjmedia_codec *codec; |
| 518 | int idx; |
| 519 | pj_pool_t *pool; |
| 520 | unsigned i; |
| 521 | |
| 522 | PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL); |
| 523 | PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL); |
| 524 | |
| 525 | pj_mutex_lock(codec_factory.mutex); |
| 526 | |
| 527 | /* Find codec's index */ |
| 528 | idx = -1; |
| 529 | for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) { |
| 530 | pj_str_t name = pj_str((char*)codec_desc[i].name); |
| 531 | if ((pj_stricmp(&id->encoding_name, &name) == 0) && |
| 532 | (id->clock_rate == (unsigned)codec_desc[i].clock_rate) && |
| 533 | (id->channel_cnt == (unsigned)codec_desc[i].channel_count) && |
| 534 | (codec_desc[i].enabled)) |
| 535 | { |
| 536 | idx = i; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | if (idx == -1) { |
| 541 | *p_codec = NULL; |
| 542 | return PJMEDIA_CODEC_EUNSUP; |
| 543 | } |
| 544 | |
| 545 | /* Create pool for codec instance */ |
| 546 | pool = pjmedia_endpt_create_pool(codec_factory.endpt, "passthroughcodec", |
| 547 | 512, 512); |
| 548 | codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec); |
| 549 | codec->op = &codec_op; |
| 550 | codec->factory = factory; |
| 551 | codec->codec_data = PJ_POOL_ZALLOC_T(pool, codec_private_t); |
| 552 | codec_data = (codec_private_t*) codec->codec_data; |
| 553 | codec_data->pool = pool; |
| 554 | codec_data->codec_idx = idx; |
| 555 | |
| 556 | pj_mutex_unlock(codec_factory.mutex); |
| 557 | |
| 558 | *p_codec = codec; |
| 559 | return PJ_SUCCESS; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Free codec. |
| 564 | */ |
| 565 | static pj_status_t dealloc_codec( pjmedia_codec_factory *factory, |
| 566 | pjmedia_codec *codec ) |
| 567 | { |
| 568 | codec_private_t *codec_data; |
| 569 | |
| 570 | PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL); |
| 571 | PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL); |
| 572 | |
| 573 | /* Close codec, if it's not closed. */ |
| 574 | codec_data = (codec_private_t*) codec->codec_data; |
| 575 | codec_close(codec); |
| 576 | |
| 577 | pj_pool_release(codec_data->pool); |
| 578 | |
| 579 | return PJ_SUCCESS; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Init codec. |
| 584 | */ |
| 585 | static pj_status_t codec_init( pjmedia_codec *codec, |
| 586 | pj_pool_t *pool ) |
| 587 | { |
| 588 | PJ_UNUSED_ARG(codec); |
| 589 | PJ_UNUSED_ARG(pool); |
| 590 | return PJ_SUCCESS; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Open codec. |
| 595 | */ |
| 596 | static pj_status_t codec_open( pjmedia_codec *codec, |
| 597 | pjmedia_codec_param *attr ) |
| 598 | { |
| 599 | codec_private_t *codec_data = (codec_private_t*) codec->codec_data; |
| 600 | struct codec_desc *desc = &codec_desc[codec_data->codec_idx]; |
| 601 | pj_pool_t *pool; |
| 602 | int i, j; |
| 603 | |
| 604 | pool = codec_data->pool; |
| 605 | |
| 606 | /* Get bitstream size */ |
| 607 | i = attr->info.avg_bps * desc->samples_per_frame; |
| 608 | j = desc->clock_rate << 3; |
| 609 | codec_data->avg_frame_size = (pj_uint16_t)(i / j); |
| 610 | if (i % j) ++codec_data->avg_frame_size; |
| 611 | |
| 612 | #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR |
| 613 | /* Init AMR settings */ |
| 614 | if (desc->pt == PJMEDIA_RTP_PT_AMR || desc->pt == PJMEDIA_RTP_PT_AMRWB) { |
| 615 | amr_settings_t *s; |
| 616 | pj_uint8_t octet_align = 0; |
| 617 | const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11}; |
| 618 | |
| 619 | /* Fetch octet-align setting. It should be fine to fetch only |
| 620 | * the decoder, since encoder & decoder must use the same setting |
| 621 | * (RFC 4867 section 8.3.1). |
| 622 | */ |
| 623 | for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) { |
| 624 | if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name, |
| 625 | &STR_FMTP_OCTET_ALIGN) == 0) |
| 626 | { |
| 627 | octet_align=(pj_uint8_t) |
| 628 | (pj_strtoul(&attr->setting.dec_fmtp.param[i].val)); |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | s = PJ_POOL_ZALLOC_T(pool, amr_settings_t); |
| 634 | codec_data->codec_setting = s; |
| 635 | |
| 636 | s->enc_mode = pjmedia_codec_amr_get_mode(desc->def_bitrate); |
| 637 | if (s->enc_mode < 0) |
| 638 | return PJMEDIA_CODEC_EINMODE; |
| 639 | |
| 640 | s->enc_setting.amr_nb = desc->pt == PJMEDIA_RTP_PT_AMR; |
| 641 | s->enc_setting.octet_aligned = octet_align; |
| 642 | s->enc_setting.reorder = PJ_FALSE; /* Note this! passthrough codec |
| 643 | doesn't do sensitivity bits |
| 644 | reordering */ |
| 645 | s->enc_setting.cmr = 15; |
| 646 | |
| 647 | s->dec_setting.amr_nb = desc->pt == PJMEDIA_RTP_PT_AMR; |
| 648 | s->dec_setting.octet_aligned = octet_align; |
| 649 | s->dec_setting.reorder = PJ_FALSE; /* Note this! passthrough codec |
| 650 | doesn't do sensitivity bits |
| 651 | reordering */ |
| 652 | } |
| 653 | #endif |
| 654 | |
| 655 | return PJ_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Close codec. |
| 660 | */ |
| 661 | static pj_status_t codec_close( pjmedia_codec *codec ) |
| 662 | { |
| 663 | PJ_UNUSED_ARG(codec); |
| 664 | |
| 665 | return PJ_SUCCESS; |
| 666 | } |
| 667 | |
| 668 | |
| 669 | /* |
| 670 | * Modify codec settings. |
| 671 | */ |
| 672 | static pj_status_t codec_modify( pjmedia_codec *codec, |
| 673 | const pjmedia_codec_param *attr ) |
| 674 | { |
| 675 | /* Not supported yet. */ |
| 676 | PJ_UNUSED_ARG(codec); |
| 677 | PJ_UNUSED_ARG(attr); |
| 678 | |
| 679 | return PJ_ENOTSUP; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Get frames in the packet. |
| 684 | */ |
| 685 | static pj_status_t codec_parse( pjmedia_codec *codec, |
| 686 | void *pkt, |
| 687 | pj_size_t pkt_size, |
| 688 | const pj_timestamp *ts, |
| 689 | unsigned *frame_cnt, |
| 690 | pjmedia_frame frames[]) |
| 691 | { |
| 692 | codec_private_t *codec_data = (codec_private_t*) codec->codec_data; |
| 693 | struct codec_desc *desc = &codec_desc[codec_data->codec_idx]; |
| 694 | unsigned count = 0; |
| 695 | |
| 696 | PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL); |
| 697 | |
| 698 | if (desc->parse != NULL) { |
| 699 | return desc->parse(codec_data, pkt, pkt_size, ts, frame_cnt, frames); |
| 700 | } |
| 701 | |
| 702 | while (pkt_size >= codec_data->avg_frame_size && count < *frame_cnt) { |
| 703 | frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 704 | frames[count].buf = pkt; |
| 705 | frames[count].size = codec_data->avg_frame_size; |
| 706 | frames[count].timestamp.u64 = ts->u64 + count*desc->samples_per_frame; |
| 707 | |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 708 | pkt = (pj_uint8_t*)pkt + codec_data->avg_frame_size; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 709 | pkt_size -= codec_data->avg_frame_size; |
| 710 | |
| 711 | ++count; |
| 712 | } |
| 713 | |
| 714 | if (pkt_size && count < *frame_cnt) { |
| 715 | frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 716 | frames[count].buf = pkt; |
| 717 | frames[count].size = pkt_size; |
| 718 | frames[count].timestamp.u64 = ts->u64 + count*desc->samples_per_frame; |
| 719 | ++count; |
| 720 | } |
| 721 | |
| 722 | *frame_cnt = count; |
| 723 | return PJ_SUCCESS; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * Encode frames. |
| 728 | */ |
| 729 | static pj_status_t codec_encode( pjmedia_codec *codec, |
| 730 | const struct pjmedia_frame *input, |
| 731 | unsigned output_buf_len, |
| 732 | struct pjmedia_frame *output) |
| 733 | { |
| 734 | codec_private_t *codec_data = (codec_private_t*) codec->codec_data; |
| 735 | struct codec_desc *desc = &codec_desc[codec_data->codec_idx]; |
| 736 | const pjmedia_frame_ext *input_ = (const pjmedia_frame_ext*) input; |
| 737 | |
| 738 | pj_assert(input && input->type == PJMEDIA_FRAME_TYPE_EXTENDED); |
| 739 | |
| 740 | if (desc->pack != NULL) { |
| 741 | desc->pack(codec_data, input_, output_buf_len, output); |
| 742 | } else { |
| 743 | if (input_->subframe_cnt == 0) { |
| 744 | /* DTX */ |
| 745 | output->buf = NULL; |
| 746 | output->size = 0; |
| 747 | output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 748 | } else { |
| 749 | unsigned i; |
| 750 | pj_uint8_t *p = output->buf; |
| 751 | |
| 752 | output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 753 | output->size = 0; |
| 754 | |
| 755 | for (i = 0; i < input_->subframe_cnt; ++i) { |
| 756 | pjmedia_frame_ext_subframe *sf; |
| 757 | unsigned sf_len; |
| 758 | |
| 759 | sf = pjmedia_frame_ext_get_subframe(input_, i); |
| 760 | pj_assert(sf); |
| 761 | |
| 762 | sf_len = sf->bitlen >> 3; |
| 763 | if (sf->bitlen & 0x07) |
| 764 | ++sf_len; |
| 765 | |
| 766 | pj_memcpy(p, sf->data, sf_len); |
| 767 | p += sf_len; |
| 768 | output->size += sf_len; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return PJ_SUCCESS; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Decode frame. |
| 778 | */ |
| 779 | static pj_status_t codec_decode( pjmedia_codec *codec, |
| 780 | const struct pjmedia_frame *input, |
| 781 | unsigned output_buf_len, |
| 782 | struct pjmedia_frame *output) |
| 783 | { |
| 784 | codec_private_t *codec_data = (codec_private_t*) codec->codec_data; |
| 785 | struct codec_desc *desc = &codec_desc[codec_data->codec_idx]; |
| 786 | pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output; |
| 787 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 788 | pj_assert(input); |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 789 | |
| 790 | #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR |
| 791 | /* Need to rearrange the AMR bitstream, since the bitstream may not be |
| 792 | * started from bit 0 or may need to be reordered from sensitivity order |
| 793 | * into encoder bits order. |
| 794 | */ |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 795 | if (desc->pt == PJMEDIA_RTP_PT_AMR || desc->pt == PJMEDIA_RTP_PT_AMRWB) { |
| 796 | pjmedia_frame input_; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 797 | pjmedia_codec_amr_pack_setting *setting; |
| 798 | |
| 799 | setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting; |
| 800 | |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 801 | input_ = *input; |
| 802 | pjmedia_codec_amr_predecode(input, setting, &input_); |
| 803 | |
| 804 | pjmedia_frame_ext_append_subframe(output_, input_.buf, |
| 805 | (pj_uint16_t)(input_.size << 3), |
| 806 | (pj_uint16_t)desc->samples_per_frame); |
| 807 | |
| 808 | return PJ_SUCCESS; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 809 | } |
| 810 | #endif |
Nanang Izzuddin | bfa860b | 2009-02-09 10:39:58 +0000 | [diff] [blame^] | 811 | |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 812 | pjmedia_frame_ext_append_subframe(output_, input->buf, |
| 813 | (pj_uint16_t)(input->size << 3), |
| 814 | (pj_uint16_t)desc->samples_per_frame); |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 815 | |
| 816 | return PJ_SUCCESS; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * Recover lost frame. |
| 821 | */ |
| 822 | static pj_status_t codec_recover( pjmedia_codec *codec, |
| 823 | unsigned output_buf_len, |
| 824 | struct pjmedia_frame *output) |
| 825 | { |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 826 | codec_private_t *codec_data = (codec_private_t*) codec->codec_data; |
| 827 | struct codec_desc *desc = &codec_desc[codec_data->codec_idx]; |
| 828 | pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output; |
| 829 | |
| 830 | PJ_UNUSED_ARG(output_buf_len); |
| 831 | |
| 832 | pjmedia_frame_ext_append_subframe(output_, NULL, 0, |
| 833 | (pj_uint16_t)desc->samples_per_frame); |
| 834 | |
| 835 | return PJ_SUCCESS; |
Nanang Izzuddin | 8cdba46 | 2009-01-29 20:06:28 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | #endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */ |
| 839 | |