codec.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. #ifndef __PJMEDIA_CODEC_H__
  20. #define __PJMEDIA_CODEC_H__
  21. /**
  22. * @file codec.h
  23. * @brief Codec framework.
  24. */
  25. #include <pjmedia/port.h>
  26. #include <pj/errno.h>
  27. #include <pj/list.h>
  28. #include <pj/pool.h>
  29. PJ_BEGIN_DECL
  30. /**
  31. * @defgroup PJMEDIA_CODEC Codec Framework
  32. * @brief Media codec framework and management
  33. * @{
  34. *
  35. * @section codec_mgmt_sec Codec Management
  36. * @subsection codec_fact_sec Codec Manager
  37. *
  38. * The codec manager is used to manage all codec capabilities in the endpoint.
  39. * When used with media endpoint (pjmedia_endpt), application can retrieve
  40. * the codec manager instance by calling #pjmedia_endpt_get_codec_mgr().
  41. *
  42. * @subsection reg_new_codec Registering New Codec
  43. *
  44. * New codec types can be registered to PJMEDIA (or to be precise, to the
  45. * codec manager) during run-time.
  46. * To do this, application needs to initialize an instance of
  47. * codec factory (#pjmedia_codec_factory) and registers this codec factory
  48. * by calling #pjmedia_codec_mgr_register_factory().
  49. *
  50. * For codecs implemented/supported by PJMEDIA, this process is normally
  51. * concealed in an easy to use function such as #pjmedia_codec_g711_init().
  52. *
  53. * @subsection codec_factory Codec Factory
  54. *
  55. * A codec factory (#pjmedia_codec_factory) is registered to codec manager,
  56. * and it is used to create and release codec instance.
  57. *
  58. * The most important member of the codec factory is the "virtual" function
  59. * table #pjmedia_codec_factory_op, where it contains, among other thing,
  60. * pointer to functions to allocate and deallocate codec instance.
  61. *
  62. * @subsection codec_inst Codec Instance
  63. *
  64. * Application allocates codec instance by calling
  65. * #pjmedia_codec_mgr_alloc_codec().
  66. * One codec instance (#pjmedia_codec) can be used for simultaneous encoding
  67. * and decoding.
  68. *
  69. * The most important member of the codec instance is the "virtual" function
  70. * table #pjmedia_codec_op, where it holds pointer to functions to
  71. * encode/decode media frames.
  72. *
  73. * @subsection codec_ident Codec Identification
  74. *
  75. * A particular codec type in PJMEDIA can be uniquely identified by two
  76. * keys: by #pjmedia_codec_info, or by #pjmedia_codec_id string. A fully
  77. * qualified codec ID string consists of codec name, sampling rate, and
  78. * number of channels. However, application may use only first parts of
  79. * the tokens as long as it will make to codec ID unique. For example, "gsm"
  80. * is a fully qualified codec name, since it will always have 8000 clock
  81. * rate and 1 channel. Other examples of fully qualified codec ID strings
  82. * are "pcma", "speex/8000", "speex/16000", and "L16/16000/1". A codec
  83. * id "speex" (without clock rate) is not fully qualified, since it will
  84. * match the narrowband, wideband, and ultrawideband Speex codec.
  85. *
  86. * The two keys can be converted to one another, with
  87. * #pjmedia_codec_info_to_id() and #pjmedia_codec_mgr_find_codecs_by_id()
  88. * functions.
  89. *
  90. * Codec ID string is not case sensitive.
  91. *
  92. *
  93. * @section using_codec Using the Codec Framework
  94. * @subsection init_alloc_codec Allocating Codec
  95. *
  96. * Application needs to allocate one codec instance for encoding and decoding
  97. * media frames. One codec instance can be used to perform both encoding
  98. * and decoding.
  99. *
  100. * Application allocates codec by calling #pjmedia_codec_mgr_alloc_codec().
  101. * This function takes #pjmedia_codec_info argument, which is used to locate
  102. * the particular codec factory to be used to allocate the codec.
  103. *
  104. * Application can build #pjmedia_codec_info structure manually for
  105. * the specific codec, or alternatively it may get the #pjmedia_codec_info
  106. * from the codec ID string, by using #pjmedia_codec_mgr_find_codecs_by_id()
  107. * function.
  108. *
  109. * The following snippet shows an example to allocate a codec:
  110. *
  111. \code
  112. pj_str_t codec_id;
  113. pjmedia_codec_info *codec_info;
  114. unsigned count = 1;
  115. pjmedia_codec *codec;
  116. codec_id = pj_str("pcma");
  117. // Find codec info for the specified coded ID (i.e. "pcma").
  118. status = pjmedia_codec_mgr_find_codecs_by_id( codec_mgr, &codec_id,
  119. &count, &codec_info, NULL);
  120. // Allocate the codec.
  121. status = pjmedia_codec_mgr_alloc_codec( codec_mgr, codec_info, &codec );
  122. \endcode
  123. *
  124. *
  125. * @subsection opening_codec Initializing Codec
  126. *
  127. * Once codec is allocated, application needs to initialize the codec
  128. * by calling <b><tt>open</tt></b> member of the codec. This function
  129. * takes #pjmedia_codec_param as the argument, which contains the
  130. * settings for the codec.
  131. *
  132. * Application shoud use #pjmedia_codec_mgr_get_default_param() function
  133. * to initiaize #pjmedia_codec_param. The <tt>setting</tt> part of
  134. * #pjmedia_codec_param then can be tuned to suit the application's
  135. * requirements.
  136. *
  137. * The following snippet shows an example to initialize codec:
  138. *
  139. \code
  140. pjmedia_codec_param param;
  141. // Retrieve default codec param for the specified codec.
  142. pjmedia_codec_mgr_get_default_param(codec_mgr, codec_info,
  143. &param);
  144. // Application may change the "settings" part of codec param,
  145. // for example, to disable VAD
  146. param.setting.vad = 0;
  147. // Open the codec using the specified settings.
  148. codec->op->open( codec, &param );
  149. \endcode
  150. *
  151. *
  152. * @subsection enc_dec_codec Encoding and Decoding Media Frames
  153. *
  154. * Application encodes and decodes media frames by calling
  155. * <tt>encode</tt> and <tt>decode</tt> member of the codec's "virtual"
  156. * function table (#pjmedia_codec_op).
  157. *
  158. * @subsection plc_codec Concealing Lost Frames
  159. *
  160. * All codecs has Packet Lost Concealment (PLC) feature, and application
  161. * can activate the PLC to conceal lost frames by calling <tt>recover</tt>
  162. * member of the codec's "virtual" function table (#pjmedia_codec_op).
  163. *
  164. * If the codec's algorithm supports PLC, the <tt>recover</tt> function
  165. * will use the codec's PLC. Otherwise for codecs that don't have
  166. * intrinsic PLC, PJMEDIA will suply the PLC implementation from the
  167. * @ref PJMED_PLC implementation.
  168. *
  169. * @subsection close_codec Closing and Releasing the Codec
  170. *
  171. * The codec must be closed by calling <tt>close</tt> member of the codec's
  172. * operation. Then it must be released by calling
  173. * #pjmedia_codec_mgr_dealloc_codec().
  174. */
  175. /**
  176. * Standard RTP static payload types, as defined by RFC 3551.
  177. * The header file <pjmedia-codec/types.h> also declares dynamic payload
  178. * type numbers that are used by PJMEDIA when advertising the capability
  179. * for example in SDP message.
  180. */
  181. enum pjmedia_rtp_pt
  182. {
  183. PJMEDIA_RTP_PT_PCMU = 0, /**< audio PCMU */
  184. PJMEDIA_RTP_PT_G721 = 2, /**< audio G721 (old def for G726-32) */
  185. PJMEDIA_RTP_PT_GSM = 3, /**< audio GSM */
  186. PJMEDIA_RTP_PT_G723 = 4, /**< audio G723 */
  187. PJMEDIA_RTP_PT_DVI4_8K = 5, /**< audio DVI4 8KHz */
  188. PJMEDIA_RTP_PT_DVI4_16K = 6, /**< audio DVI4 16Khz */
  189. PJMEDIA_RTP_PT_LPC = 7, /**< audio LPC */
  190. PJMEDIA_RTP_PT_PCMA = 8, /**< audio PCMA */
  191. PJMEDIA_RTP_PT_G722 = 9, /**< audio G722 */
  192. PJMEDIA_RTP_PT_L16_2 = 10, /**< audio 16bit linear 44.1KHz stereo */
  193. PJMEDIA_RTP_PT_L16_1 = 11, /**< audio 16bit linear 44.1KHz mono */
  194. PJMEDIA_RTP_PT_QCELP = 12, /**< audio QCELP */
  195. PJMEDIA_RTP_PT_CN = 13, /**< audio Comfort Noise */
  196. PJMEDIA_RTP_PT_MPA = 14, /**< audio MPEG1/MPEG2 elemetr. streams */
  197. PJMEDIA_RTP_PT_G728 = 15, /**< audio G728 */
  198. PJMEDIA_RTP_PT_DVI4_11K = 16, /**< audio DVI4 11.025KHz mono */
  199. PJMEDIA_RTP_PT_DVI4_22K = 17, /**< audio DVI4 22.050KHz mono */
  200. PJMEDIA_RTP_PT_G729 = 18, /**< audio G729 */
  201. PJMEDIA_RTP_PT_CELB = 25, /**< video/comb Cell-B by Sun (RFC2029) */
  202. PJMEDIA_RTP_PT_JPEG = 26, /**< video JPEG */
  203. PJMEDIA_RTP_PT_NV = 28, /**< video NV by nv program by Xerox */
  204. PJMEDIA_RTP_PT_H261 = 31, /**< video H261 */
  205. PJMEDIA_RTP_PT_MPV = 32, /**< video MPEG1 or MPEG2 elementary */
  206. PJMEDIA_RTP_PT_MP2T = 33, /**< video MPEG2 transport */
  207. PJMEDIA_RTP_PT_H263 = 34, /**< video H263 */
  208. PJMEDIA_RTP_PT_DYNAMIC = 96 /**< start of dynamic RTP payload */
  209. };
  210. /**
  211. * Identification used to search for codec factory that supports specific
  212. * codec specification.
  213. */
  214. typedef struct pjmedia_codec_info
  215. {
  216. pjmedia_type type; /**< Media type. */
  217. unsigned pt; /**< Payload type (can be dynamic). */
  218. pj_str_t encoding_name; /**< Encoding name. */
  219. unsigned clock_rate; /**< Sampling rate. */
  220. unsigned channel_cnt; /**< Channel count. */
  221. } pjmedia_codec_info;
  222. /**
  223. * Structure of codec specific parameters which contains name=value pairs.
  224. * The codec specific parameters are to be used with SDP according to
  225. * the standards (e.g: RFC 3555) in SDP 'a=fmtp' attribute.
  226. */
  227. typedef struct pjmedia_codec_fmtp
  228. {
  229. pj_uint8_t cnt; /**< Number of parameters. */
  230. struct param {
  231. pj_str_t name; /**< Parameter name. */
  232. pj_str_t val; /**< Parameter value. */
  233. } param [PJMEDIA_CODEC_MAX_FMTP_CNT]; /**< The parameters. */
  234. } pjmedia_codec_fmtp;
  235. /**
  236. * Detailed codec attributes used in configuring a codec and in querying
  237. * the capability of codec factories. Default attributes of any codecs could
  238. * be queried using #pjmedia_codec_mgr_get_default_param() and modified
  239. * using #pjmedia_codec_mgr_set_default_param().
  240. *
  241. * Please note that codec parameter also contains SDP specific setting,
  242. * #dec_fmtp and #enc_fmtp, which may need to be set appropriately based on
  243. * the effective setting. See each codec documentation for more detail.
  244. */
  245. typedef struct pjmedia_codec_param
  246. {
  247. /**
  248. * The "info" part of codec param describes the capability of the codec,
  249. * and is recommended not to be modified unless necessary.
  250. * Note that application must be ready to handle cases when ptime
  251. * denumerators are zero, since most codecs that only support integer
  252. * ptime will leave these fields untouched.
  253. */
  254. struct {
  255. unsigned clock_rate; /**< Sampling rate in Hz */
  256. unsigned channel_cnt; /**< Channel count. */
  257. pj_uint32_t avg_bps; /**< Average bandwidth in bits/sec */
  258. pj_uint32_t max_bps; /**< Maximum bandwidth in bits/sec */
  259. unsigned max_rx_frame_size; /**< Maximum frame size */
  260. pj_uint16_t frm_ptime; /**< Decoder frame ptime in msec. */
  261. pj_uint8_t frm_ptime_denum; /**< Decoder frame ptime denum, or
  262. zero if ptime is integer. */
  263. pj_uint16_t enc_ptime; /**< Encoder ptime, or zero if it's
  264. equal to decoder ptime. */
  265. pj_uint8_t enc_ptime_denum; /**< Encoder frame ptime denum, or
  266. zero if ptime is integer. */
  267. pj_uint8_t pcm_bits_per_sample; /**< Bits/sample in the PCM side */
  268. pj_uint8_t pt; /**< Payload type. */
  269. pjmedia_format_id fmt_id; /**< Source format, it's format of
  270. encoder input and decoder
  271. output. */
  272. } info;
  273. /**
  274. * The "setting" part of codec param describes various settings to be
  275. * applied to the codec. When the codec param is retrieved from the codec
  276. * or codec factory, the values of these will be filled by the capability
  277. * of the codec. Any features that are supported by the codec (e.g. vad
  278. * or plc) will be turned on, so that application can query which
  279. * capabilities are supported by the codec. Application may change the
  280. * settings here before instantiating or modifying the codec.
  281. */
  282. struct {
  283. pj_uint8_t frm_per_pkt; /**< Number of frames per packet. */
  284. unsigned vad:1; /**< Voice Activity Detector. */
  285. unsigned cng:1; /**< Comfort Noise Generator. */
  286. unsigned penh:1; /**< Perceptual Enhancement */
  287. unsigned plc:1; /**< Packet loss concealment */
  288. unsigned reserved:1; /**< Reserved, must be zero. */
  289. pjmedia_codec_fmtp enc_fmtp;/**< Encoder's fmtp params. */
  290. pjmedia_codec_fmtp dec_fmtp;/**< Decoder's fmtp params. */
  291. unsigned packet_loss; /**< Encoder's expected pkt loss %. */
  292. unsigned complexity; /**< Encoder complexity, 0-10(max). */
  293. pj_bool_t cbr; /**< Constant bit rate? */
  294. } setting;
  295. } pjmedia_codec_param;
  296. /**
  297. * Duplicate codec parameter.
  298. *
  299. * @param pool The pool.
  300. * @param src The codec parameter to be duplicated.
  301. *
  302. * @return Duplicated codec parameter.
  303. */
  304. PJ_DECL(pjmedia_codec_param*) pjmedia_codec_param_clone(
  305. pj_pool_t *pool,
  306. const pjmedia_codec_param *src);
  307. /*
  308. * Forward declaration for pjmedia_codec.
  309. */
  310. typedef struct pjmedia_codec pjmedia_codec;
  311. /**
  312. * This structure describes codec operations. Each codec MUST implement
  313. * all of these functions.
  314. */
  315. typedef struct pjmedia_codec_op
  316. {
  317. /**
  318. * Initialize codec using the specified attribute.
  319. *
  320. * Application should call #pjmedia_codec_init() instead of
  321. * calling this function directly.
  322. *
  323. * @param codec The codec instance.
  324. * @param pool Pool to use when the codec needs to allocate
  325. * some memory.
  326. *
  327. * @return PJ_SUCCESS on success.
  328. */
  329. pj_status_t (*init)(pjmedia_codec *codec,
  330. pj_pool_t *pool );
  331. /**
  332. * Open the codec and initialize with the specified parameter.
  333. * Upon successful initialization, the codec may modify the parameter
  334. * and fills in the unspecified values (such as enc_ptime/enc_ptime_denum,
  335. * when encoder ptime is different than decoder ptime).
  336. *
  337. * Application should call #pjmedia_codec_open() instead of
  338. * calling this function directly.
  339. *
  340. * @param codec The codec instance.
  341. * @param param Codec initialization parameter.
  342. *
  343. * @return PJ_SUCCESS on success.
  344. */
  345. pj_status_t (*open)(pjmedia_codec *codec,
  346. pjmedia_codec_param *param );
  347. /**
  348. * Close and shutdown codec, releasing all resources allocated by
  349. * this codec, if any.
  350. *
  351. * Application should call #pjmedia_codec_close() instead of
  352. * calling this function directly.
  353. *
  354. * @param codec The codec instance.
  355. *
  356. * @return PJ_SUCCESS on success.
  357. */
  358. pj_status_t (*close)(pjmedia_codec *codec);
  359. /**
  360. * Modify the codec parameter after the codec is open.
  361. * Note that not all codec parameters can be modified during run-time.
  362. * Currently, only Opus codec supports changing key codec parameters
  363. * such as bitrate and bandwidth, while other codecs may only be able to
  364. * modify minor settings such as VAD or PLC.
  365. *
  366. * Application should call #pjmedia_codec_modify() instead of
  367. * calling this function directly.
  368. *
  369. * @param codec The codec instance.
  370. * @param param The new codec parameter.
  371. *
  372. * @return PJ_SUCCESS on success.
  373. */
  374. pj_status_t (*modify)(pjmedia_codec *codec,
  375. const pjmedia_codec_param *param );
  376. /**
  377. * Instruct the codec to inspect the specified payload/packet and
  378. * split the packet into individual base frames. Each output frames will
  379. * have ptime that is equal to basic frame ptime (i.e. the value of
  380. * info.frm_ptime/info.frm_ptime_denum in #pjmedia_codec_param).
  381. *
  382. * Application should call #pjmedia_codec_parse() instead of
  383. * calling this function directly.
  384. *
  385. * @param codec The codec instance
  386. * @param pkt The input packet.
  387. * @param pkt_size Size of the packet.
  388. * @param timestamp The timestamp of the first sample in the packet.
  389. * @param frame_cnt On input, specifies the maximum number of frames
  390. * in the array. On output, the codec must fill
  391. * with number of frames detected in the packet.
  392. * @param frames On output, specifies the frames that have been
  393. * detected in the packet.
  394. *
  395. * @return PJ_SUCCESS on success.
  396. */
  397. pj_status_t (*parse)( pjmedia_codec *codec,
  398. void *pkt,
  399. pj_size_t pkt_size,
  400. const pj_timestamp *timestamp,
  401. unsigned *frame_cnt,
  402. pjmedia_frame frames[]);
  403. /**
  404. * Instruct the codec to encode the specified input frame. The input
  405. * PCM samples MUST have ptime that is multiplication of base frame
  406. * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
  407. * #pjmedia_codec_param).
  408. *
  409. * Application should call #pjmedia_codec_encode() instead of
  410. * calling this function directly.
  411. *
  412. * @param codec The codec instance.
  413. * @param input The input frame.
  414. * @param out_size The length of buffer in the output frame.
  415. * @param output The output frame.
  416. *
  417. * @return PJ_SUCCESS on success;
  418. */
  419. pj_status_t (*encode)(pjmedia_codec *codec,
  420. const struct pjmedia_frame *input,
  421. unsigned out_size,
  422. struct pjmedia_frame *output);
  423. /**
  424. * Instruct the codec to decode the specified input frame. The input
  425. * frame MUST have ptime that is exactly equal to base frame
  426. * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
  427. * #pjmedia_codec_param).
  428. * Application can achieve this by parsing the packet into base
  429. * frames before decoding each frame.
  430. *
  431. * Application should call #pjmedia_codec_decode() instead of
  432. * calling this function directly.
  433. *
  434. * @param codec The codec instance.
  435. * @param input The input frame.
  436. * @param out_size The length of buffer in the output frame.
  437. * @param output The output frame.
  438. *
  439. * @return PJ_SUCCESS on success;
  440. */
  441. pj_status_t (*decode)(pjmedia_codec *codec,
  442. const struct pjmedia_frame *input,
  443. unsigned out_size,
  444. struct pjmedia_frame *output);
  445. /**
  446. * Instruct the codec to recover a missing frame.
  447. *
  448. * Application should call #pjmedia_codec_recover() instead of
  449. * calling this function directly.
  450. *
  451. * @param codec The codec instance.
  452. * @param out_size The length of buffer in the output frame.
  453. * @param output The output frame where generated signal
  454. * will be placed.
  455. *
  456. * @return PJ_SUCCESS on success;
  457. */
  458. pj_status_t (*recover)(pjmedia_codec *codec,
  459. unsigned out_size,
  460. struct pjmedia_frame *output);
  461. } pjmedia_codec_op;
  462. /*
  463. * Forward declaration for pjmedia_codec_factory.
  464. */
  465. typedef struct pjmedia_codec_factory pjmedia_codec_factory;
  466. /**
  467. * This structure describes a codec instance.
  468. */
  469. struct pjmedia_codec
  470. {
  471. /** Entries to put this codec instance in codec factory's list. */
  472. PJ_DECL_LIST_MEMBER(struct pjmedia_codec);
  473. /** Codec's private data. */
  474. void *codec_data;
  475. /** Codec factory where this codec was allocated. */
  476. pjmedia_codec_factory *factory;
  477. /** Operations to codec. */
  478. pjmedia_codec_op *op;
  479. };
  480. /**
  481. * This structure describes operations that must be supported by codec
  482. * factories.
  483. */
  484. typedef struct pjmedia_codec_factory_op
  485. {
  486. /**
  487. * Check whether the factory can create codec with the specified
  488. * codec info.
  489. *
  490. * @param factory The codec factory.
  491. * @param info The codec info.
  492. *
  493. * @return PJ_SUCCESS if this factory is able to create an
  494. * instance of codec with the specified info.
  495. */
  496. pj_status_t (*test_alloc)(pjmedia_codec_factory *factory,
  497. const pjmedia_codec_info *info );
  498. /**
  499. * Create default attributes for the specified codec ID. This function
  500. * can be called by application to get the capability of the codec.
  501. *
  502. * @param factory The codec factory.
  503. * @param info The codec info.
  504. * @param attr The attribute to be initialized.
  505. *
  506. * @return PJ_SUCCESS if success.
  507. */
  508. pj_status_t (*default_attr)(pjmedia_codec_factory *factory,
  509. const pjmedia_codec_info *info,
  510. pjmedia_codec_param *attr );
  511. /**
  512. * Enumerate supported codecs that can be created using this factory.
  513. *
  514. * @param factory The codec factory.
  515. * @param count On input, specifies the number of elements in
  516. * the array. On output, the value will be set to
  517. * the number of elements that have been initialized
  518. * by this function.
  519. * @param info The codec info array, which contents will be
  520. * initialized upon return.
  521. *
  522. * @return PJ_SUCCESS on success.
  523. */
  524. pj_status_t (*enum_info)(pjmedia_codec_factory *factory,
  525. unsigned *count,
  526. pjmedia_codec_info codecs[]);
  527. /**
  528. * Create one instance of the codec with the specified codec info.
  529. *
  530. * @param factory The codec factory.
  531. * @param info The codec info.
  532. * @param p_codec Pointer to receive the codec instance.
  533. *
  534. * @return PJ_SUCCESS on success.
  535. */
  536. pj_status_t (*alloc_codec)(pjmedia_codec_factory *factory,
  537. const pjmedia_codec_info *info,
  538. pjmedia_codec **p_codec);
  539. /**
  540. * This function is called by codec manager to return a particular
  541. * instance of codec back to the codec factory.
  542. *
  543. * @param factory The codec factory.
  544. * @param codec The codec instance to be returned.
  545. *
  546. * @return PJ_SUCCESS on success.
  547. */
  548. pj_status_t (*dealloc_codec)(pjmedia_codec_factory *factory,
  549. pjmedia_codec *codec );
  550. /**
  551. * This callback will be called to deinitialize and destroy this factory.
  552. */
  553. pj_status_t (*destroy)(void);
  554. } pjmedia_codec_factory_op;
  555. /**
  556. * Codec factory describes a module that is able to create codec with specific
  557. * capabilities. These capabilities can be queried by codec manager to create
  558. * instances of codec.
  559. */
  560. struct pjmedia_codec_factory
  561. {
  562. /** Entries to put this structure in the codec manager list. */
  563. PJ_DECL_LIST_MEMBER(struct pjmedia_codec_factory);
  564. /** The factory's private data. */
  565. void *factory_data;
  566. /** Operations to the factory. */
  567. pjmedia_codec_factory_op *op;
  568. };
  569. /**
  570. * Declare maximum codecs
  571. */
  572. #define PJMEDIA_CODEC_MGR_MAX_CODECS 32
  573. /**
  574. * Specify these values to set the codec priority, by calling
  575. * #pjmedia_codec_mgr_set_codec_priority().
  576. */
  577. typedef enum pjmedia_codec_priority
  578. {
  579. /**
  580. * This priority makes the codec the highest in the order.
  581. * The last codec specified with this priority will get the
  582. * highest place in the order, and will change the priority
  583. * of previously highest priority codec to NEXT_HIGHER.
  584. */
  585. PJMEDIA_CODEC_PRIO_HIGHEST = 255,
  586. /**
  587. * This priority will put the codec as the next codec after
  588. * codecs with this same priority.
  589. */
  590. PJMEDIA_CODEC_PRIO_NEXT_HIGHER = 254,
  591. /**
  592. * This is the initial codec priority when it is registered to
  593. * codec manager by codec factory.
  594. */
  595. PJMEDIA_CODEC_PRIO_NORMAL = 128,
  596. /**
  597. * This priority makes the codec the lowest in the order.
  598. * The last codec specified with this priority will be put
  599. * in the last place in the order.
  600. */
  601. PJMEDIA_CODEC_PRIO_LOWEST = 1,
  602. /**
  603. * This priority will prevent the codec from being listed in the
  604. * SDP created by media endpoint, thus should prevent the codec
  605. * from being used in the sessions. However, the codec will still
  606. * be listed by #pjmedia_codec_mgr_enum_codecs() and other codec
  607. * query functions.
  608. */
  609. PJMEDIA_CODEC_PRIO_DISABLED = 0
  610. } pjmedia_codec_priority;
  611. /**
  612. * Codec identification (e.g. "pcmu/8000/1").
  613. * See @ref codec_ident for more info.
  614. */
  615. typedef char pjmedia_codec_id[32];
  616. /**
  617. * Opaque declaration of default codecs parameters.
  618. */
  619. typedef struct pjmedia_codec_default_param pjmedia_codec_default_param;
  620. /**
  621. * Codec manager maintains array of these structs for each supported
  622. * codec.
  623. */
  624. struct pjmedia_codec_desc
  625. {
  626. pjmedia_codec_info info; /**< Codec info. */
  627. pjmedia_codec_id id; /**< Fully qualified name */
  628. pjmedia_codec_priority prio; /**< Priority. */
  629. pjmedia_codec_factory *factory; /**< The factory. */
  630. pjmedia_codec_default_param *param; /**< Default codecs
  631. parameters. */
  632. };
  633. /**
  634. * The declaration for codec manager. Application doesn't normally need
  635. * to see this declaration, but nevertheless this declaration is needed
  636. * by media endpoint to instantiate the codec manager.
  637. */
  638. typedef struct pjmedia_codec_mgr
  639. {
  640. /** Media endpoint instance. */
  641. pj_pool_factory *pf;
  642. /** Codec manager pool. */
  643. pj_pool_t *pool;
  644. /** Codec manager mutex. */
  645. pj_mutex_t *mutex;
  646. /** List of codec factories registered to codec manager. */
  647. pjmedia_codec_factory factory_list;
  648. /** Number of supported codecs. */
  649. unsigned codec_cnt;
  650. /** Array of codec descriptor. */
  651. struct pjmedia_codec_desc codec_desc[PJMEDIA_CODEC_MGR_MAX_CODECS];
  652. /** Number of codecs with dynamic PT. */
  653. unsigned dyn_codecs_cnt;
  654. /** Array of codec identifiers with dynamic PT. */
  655. pj_str_t dyn_codecs[PJMEDIA_CODEC_MGR_MAX_CODECS];
  656. #if defined(PJMEDIA_RTP_PT_TELEPHONE_EVENTS) && \
  657. PJMEDIA_RTP_PT_TELEPHONE_EVENTS != 0
  658. /** Number of televent clockrates. */
  659. unsigned televent_num;
  660. /** Array of televent clockrates. */
  661. unsigned televent_clockrates[8];
  662. #endif
  663. } pjmedia_codec_mgr;
  664. /**
  665. * Initialize codec manager. Normally this function is called by pjmedia
  666. * endpoint's initialization code.
  667. *
  668. * @param mgr Codec manager instance.
  669. * @param pf Pool factory instance.
  670. *
  671. * @return PJ_SUCCESS on success.
  672. */
  673. PJ_DECL(pj_status_t) pjmedia_codec_mgr_init(pjmedia_codec_mgr *mgr,
  674. pj_pool_factory *pf);
  675. /**
  676. * Destroy codec manager. Normally this function is called by pjmedia
  677. * endpoint's deinitialization code.
  678. *
  679. * @param mgr Codec manager instance.
  680. *
  681. * @return PJ_SUCCESS on success.
  682. */
  683. PJ_DECL(pj_status_t) pjmedia_codec_mgr_destroy(pjmedia_codec_mgr *mgr);
  684. /**
  685. * Register codec factory to codec manager. This will also register
  686. * all supported codecs in the factory to the codec manager.
  687. *
  688. * @param mgr The codec manager instance. Application can get the
  689. * instance by calling #pjmedia_endpt_get_codec_mgr().
  690. * @param factory The codec factory to be registered.
  691. *
  692. * @return PJ_SUCCESS on success.
  693. */
  694. PJ_DECL(pj_status_t)
  695. pjmedia_codec_mgr_register_factory( pjmedia_codec_mgr *mgr,
  696. pjmedia_codec_factory *factory);
  697. /**
  698. * Unregister codec factory from the codec manager. This will also
  699. * remove all the codecs registered by the codec factory from the
  700. * codec manager's list of supported codecs. This function should
  701. * only be called by the codec implementers and not by application.
  702. *
  703. * @param mgr The codec manager instance, use
  704. * #pjmedia_endpt_get_codec_mgr().
  705. * @param factory The codec factory to be unregistered.
  706. *
  707. * @return PJ_SUCCESS on success.
  708. */
  709. PJ_DECL(pj_status_t)
  710. pjmedia_codec_mgr_unregister_factory( pjmedia_codec_mgr *mgr,
  711. pjmedia_codec_factory *factory);
  712. /**
  713. * Enumerate all supported codecs that have been registered to the
  714. * codec manager by codec factories.
  715. *
  716. * @param mgr The codec manager instance. Application can get the
  717. * instance by calling #pjmedia_endpt_get_codec_mgr().
  718. * @param count On input, specifies the number of elements in
  719. * the array. On output, the value will be set to
  720. * the number of elements that have been initialized
  721. * by this function.
  722. * @param info The codec info array, which contents will be
  723. * initialized upon return.
  724. * @param prio Optional pointer to receive array of codec priorities.
  725. *
  726. * @return PJ_SUCCESS on success.
  727. */
  728. PJ_DECL(pj_status_t) pjmedia_codec_mgr_enum_codecs( pjmedia_codec_mgr *mgr,
  729. unsigned *count,
  730. pjmedia_codec_info info[],
  731. unsigned *prio);
  732. /**
  733. * Get codec info for the specified static payload type. Note that
  734. * this can only find codec with static payload types. This function can
  735. * be used to find codec info for a payload type inside SDP which doesn't
  736. * have the corresponding rtpmap attribute.
  737. *
  738. * @param mgr The codec manager instance. Application can get the
  739. * instance by calling #pjmedia_endpt_get_codec_mgr().
  740. * @param pt Static payload type/number.
  741. * @param inf Pointer to receive codec info.
  742. *
  743. * @return PJ_SUCCESS on success.
  744. */
  745. PJ_DECL(pj_status_t)
  746. pjmedia_codec_mgr_get_codec_info( pjmedia_codec_mgr *mgr,
  747. unsigned pt,
  748. const pjmedia_codec_info **inf);
  749. /**
  750. * Convert codec info struct into a unique codec identifier.
  751. * A codec identifier looks something like "L16/44100/2".
  752. *
  753. * @param info The codec info
  754. * @param id Buffer to put the codec info string.
  755. * @param max_len The length of the buffer.
  756. *
  757. * @return The null terminated codec info string, or NULL if
  758. * the buffer is not long enough.
  759. */
  760. PJ_DECL(char*) pjmedia_codec_info_to_id(const pjmedia_codec_info *info,
  761. char *id, unsigned max_len );
  762. /**
  763. * Find codecs by the unique codec identifier. This function will find
  764. * all codecs that match the codec identifier prefix. For example, if
  765. * "L16" is specified, then it will find "L16/8000/1", "L16/16000/1",
  766. * and so on, up to the maximum count specified in the argument.
  767. *
  768. * @param mgr The codec manager instance. Application can get the
  769. * instance by calling #pjmedia_endpt_get_codec_mgr().
  770. * @param codec_id The full codec ID or codec ID prefix. If an empty
  771. * string is given, it will match all codecs.
  772. * @param count Maximum number of codecs to find. On return, it
  773. * contains the actual number of codecs found.
  774. * @param p_info Array of pointer to codec info to be filled. This
  775. * argument may be NULL, which in this case, only
  776. * codec count will be returned.
  777. * @param prio Optional array of codec priorities.
  778. *
  779. * @return PJ_SUCCESS if at least one codec info is found.
  780. */
  781. PJ_DECL(pj_status_t)
  782. pjmedia_codec_mgr_find_codecs_by_id( pjmedia_codec_mgr *mgr,
  783. const pj_str_t *codec_id,
  784. unsigned *count,
  785. const pjmedia_codec_info *p_info[],
  786. unsigned prio[]);
  787. /**
  788. * Set codec priority. The codec priority determines the order of
  789. * the codec in the SDP created by the endpoint. If more than one codecs
  790. * are found with the same codec_id prefix, then the function sets the
  791. * priorities of all those codecs.
  792. *
  793. * @param mgr The codec manager instance. Application can get the
  794. * instance by calling #pjmedia_endpt_get_codec_mgr().
  795. * @param codec_id The full codec ID or codec ID prefix. If an empty
  796. * string is given, it will match all codecs.
  797. * @param prio Priority to be set. The priority can have any value
  798. * between 1 to 255. When the priority is set to zero,
  799. * the codec will be disabled.
  800. *
  801. * @return PJ_SUCCESS if at least one codec info is found.
  802. */
  803. PJ_DECL(pj_status_t)
  804. pjmedia_codec_mgr_set_codec_priority(pjmedia_codec_mgr *mgr,
  805. const pj_str_t *codec_id,
  806. pj_uint8_t prio);
  807. /**
  808. * Get default codec param for the specified codec info.
  809. *
  810. * @param mgr The codec manager instance. Application can get the
  811. * instance by calling #pjmedia_endpt_get_codec_mgr().
  812. * @param info The codec info, which default parameter's is being
  813. * queried.
  814. * @param param On return, will be filled with the default codec
  815. * parameter.
  816. *
  817. * @return PJ_SUCCESS on success.
  818. */
  819. PJ_DECL(pj_status_t)
  820. pjmedia_codec_mgr_get_default_param( pjmedia_codec_mgr *mgr,
  821. const pjmedia_codec_info *info,
  822. pjmedia_codec_param *param );
  823. /**
  824. * Set default codec param for the specified codec info.
  825. *
  826. * @param mgr The codec manager instance. Application can get the
  827. * instance by calling #pjmedia_endpt_get_codec_mgr().
  828. * @param info The codec info, which default parameter's is being
  829. * updated.
  830. * @param param The new default codec parameter. Set to NULL to reset
  831. * codec parameter to library default settings.
  832. *
  833. * @return PJ_SUCCESS on success.
  834. */
  835. PJ_DECL(pj_status_t)
  836. pjmedia_codec_mgr_set_default_param( pjmedia_codec_mgr *mgr,
  837. const pjmedia_codec_info *info,
  838. const pjmedia_codec_param *param );
  839. /**
  840. * Request the codec manager to allocate one instance of codec with the
  841. * specified codec info. The codec will enumerate all codec factories
  842. * until it finds factory that is able to create the specified codec.
  843. *
  844. * @param mgr The codec manager instance. Application can get the
  845. * instance by calling #pjmedia_endpt_get_codec_mgr().
  846. * @param info The information about the codec to be created.
  847. * @param p_codec Pointer to receive the codec instance.
  848. *
  849. * @return PJ_SUCCESS on success.
  850. */
  851. PJ_DECL(pj_status_t)
  852. pjmedia_codec_mgr_alloc_codec( pjmedia_codec_mgr *mgr,
  853. const pjmedia_codec_info *info,
  854. pjmedia_codec **p_codec);
  855. /**
  856. * Deallocate the specified codec instance. The codec manager will return
  857. * the instance of the codec back to its factory.
  858. *
  859. * @param mgr The codec manager instance. Application can get the
  860. * instance by calling #pjmedia_endpt_get_codec_mgr().
  861. * @param codec The codec instance.
  862. *
  863. * @return PJ_SUCESS on success.
  864. */
  865. PJ_DECL(pj_status_t) pjmedia_codec_mgr_dealloc_codec(pjmedia_codec_mgr *mgr,
  866. pjmedia_codec *codec);
  867. /**
  868. * Initialize codec using the specified attribute.
  869. *
  870. * @param codec The codec instance.
  871. * @param pool Pool to use when the codec needs to allocate some memory.
  872. *
  873. * @return PJ_SUCCESS on success.
  874. */
  875. PJ_INLINE(pj_status_t) pjmedia_codec_init( pjmedia_codec *codec,
  876. pj_pool_t *pool )
  877. {
  878. return (*codec->op->init)(codec, pool);
  879. }
  880. /**
  881. * Open the codec and initialize with the specified parameter.
  882. * Upon successful initialization, the codec may modify the parameter
  883. * and fills in the unspecified values (such as enc_ptime, when
  884. * encoder ptime is different than decoder ptime).
  885. *
  886. * @param codec The codec instance.
  887. * @param param Codec initialization parameter.
  888. *
  889. * @return PJ_SUCCESS on success.
  890. */
  891. PJ_INLINE(pj_status_t) pjmedia_codec_open( pjmedia_codec *codec,
  892. pjmedia_codec_param *param )
  893. {
  894. return (*codec->op->open)(codec, param);
  895. }
  896. /**
  897. * Close and shutdown codec, releasing all resources allocated by
  898. * this codec, if any.
  899. *
  900. * @param codec The codec instance.
  901. *
  902. * @return PJ_SUCCESS on success.
  903. */
  904. PJ_INLINE(pj_status_t) pjmedia_codec_close( pjmedia_codec *codec )
  905. {
  906. return (*codec->op->close)(codec);
  907. }
  908. /**
  909. * Modify the codec parameter after the codec is open.
  910. * Note that not all codec parameters can be modified during run-time.
  911. * Currently, only Opus codec supports changing key codec parameters
  912. * such as bitrate and bandwidth, while other codecs may only be able to
  913. * modify minor settings such as VAD or PLC.
  914. *
  915. * @param codec The codec instance.
  916. * @param param The new codec parameter.
  917. *
  918. * @return PJ_SUCCESS on success.
  919. */
  920. PJ_INLINE(pj_status_t) pjmedia_codec_modify(pjmedia_codec *codec,
  921. const pjmedia_codec_param *param)
  922. {
  923. return (*codec->op->modify)(codec, param);
  924. }
  925. /**
  926. * Instruct the codec to inspect the specified payload/packet and
  927. * split the packet into individual base frames. Each output frames will
  928. * have ptime that is equal to basic frame ptime (i.e. the value of
  929. * info.frm_ptime/info.frm_ptime_denum in #pjmedia_codec_param).
  930. *
  931. * @param codec The codec instance
  932. * @param pkt The input packet.
  933. * @param pkt_size Size of the packet.
  934. * @param timestamp The timestamp of the first sample in the packet.
  935. * @param frame_cnt On input, specifies the maximum number of frames
  936. * in the array. On output, the codec must fill
  937. * with number of frames detected in the packet.
  938. * @param frames On output, specifies the frames that have been
  939. * detected in the packet.
  940. *
  941. * @return PJ_SUCCESS on success.
  942. */
  943. PJ_INLINE(pj_status_t) pjmedia_codec_parse( pjmedia_codec *codec,
  944. void *pkt,
  945. pj_size_t pkt_size,
  946. const pj_timestamp *timestamp,
  947. unsigned *frame_cnt,
  948. pjmedia_frame frames[] )
  949. {
  950. return (*codec->op->parse)(codec, pkt, pkt_size, timestamp,
  951. frame_cnt, frames);
  952. }
  953. /**
  954. * Instruct the codec to encode the specified input frame. The input
  955. * PCM samples MUST have ptime that is multiplication of base frame
  956. * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
  957. * #pjmedia_codec_param).
  958. *
  959. * @param codec The codec instance.
  960. * @param input The input frame.
  961. * @param out_size The length of buffer in the output frame.
  962. * @param output The output frame.
  963. *
  964. * @return PJ_SUCCESS on success;
  965. */
  966. PJ_INLINE(pj_status_t) pjmedia_codec_encode(
  967. pjmedia_codec *codec,
  968. const struct pjmedia_frame *input,
  969. unsigned out_size,
  970. struct pjmedia_frame *output )
  971. {
  972. return (*codec->op->encode)(codec, input, out_size, output);
  973. }
  974. /**
  975. * Instruct the codec to decode the specified input frame. The input
  976. * frame MUST have ptime that is exactly equal to base frame
  977. * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
  978. * #pjmedia_codec_param).
  979. * Application can achieve this by parsing the packet into base
  980. * frames before decoding each frame.
  981. *
  982. * @param codec The codec instance.
  983. * @param input The input frame.
  984. * @param out_size The length of buffer in the output frame.
  985. * @param output The output frame.
  986. *
  987. * @return PJ_SUCCESS on success;
  988. */
  989. PJ_INLINE(pj_status_t) pjmedia_codec_decode(
  990. pjmedia_codec *codec,
  991. const struct pjmedia_frame *input,
  992. unsigned out_size,
  993. struct pjmedia_frame *output )
  994. {
  995. return (*codec->op->decode)(codec, input, out_size, output);
  996. }
  997. /**
  998. * Instruct the codec to recover a missing frame.
  999. *
  1000. * @param codec The codec instance.
  1001. * @param out_size The length of buffer in the output frame.
  1002. * @param output The output frame where generated signal
  1003. * will be placed.
  1004. *
  1005. * @return PJ_SUCCESS on success;
  1006. */
  1007. PJ_INLINE(pj_status_t) pjmedia_codec_recover( pjmedia_codec *codec,
  1008. unsigned out_size,
  1009. struct pjmedia_frame *output )
  1010. {
  1011. if (codec->op && codec->op->recover)
  1012. return (*codec->op->recover)(codec, out_size, output);
  1013. else
  1014. return PJ_ENOTSUP;
  1015. }
  1016. /**
  1017. * @}
  1018. */
  1019. /**
  1020. * @defgroup PJMEDIA_CODEC_CODECS Supported codecs
  1021. * @ingroup PJMEDIA_CODEC
  1022. * @brief Documentation about individual codec supported by PJMEDIA
  1023. * @{
  1024. * Please see the APIs provided by the individual codecs below.
  1025. */
  1026. /**
  1027. * @}
  1028. */
  1029. /*
  1030. * Internal functions.
  1031. */
  1032. /**
  1033. * Internal: Get the array of codec identifiers that have dynamic PT.
  1034. * Note that the array also includes telephone events.
  1035. */
  1036. pj_status_t pjmedia_codec_mgr_get_dyn_codecs(pjmedia_codec_mgr* mgr,
  1037. pj_int8_t *count,
  1038. pj_str_t dyn_codecs[]);
  1039. /* Internal: Find a certain codec string in the dynamic codecs array.
  1040. * Return the index of the array if the string is found and set
  1041. * *found == PJ_TRUE. If the string is not found, returns -1 if found
  1042. * is specified, otherwise returns the index in the array where
  1043. * the particular string could be inserted so that the array remains
  1044. * sorted, and set *found == PJ_FALSE.
  1045. */
  1046. int pjmedia_codec_mgr_find_codec(const pj_str_t dyn_codecs[],
  1047. unsigned count,
  1048. const pj_str_t *codec,
  1049. pj_bool_t *found);
  1050. /* Internal: Insert a codec ID string into the dynamic codecs array and keep
  1051. * the array sorted.
  1052. */
  1053. void pjmedia_codec_mgr_insert_codec(pj_pool_t *pool, pj_str_t dyn_codecs[],
  1054. unsigned *count, const pj_str_t *codec);
  1055. PJ_END_DECL
  1056. #endif /* __PJMEDIA_CODEC_H__ */