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