passthrough.c 35 KB

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