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