Coverage Report

Created: 2026-01-09 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pjsip/pjmedia/include/pjmedia/codec.h
Line
Count
Source
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
23
/**
24
 * @file codec.h
25
 * @brief Codec framework.
26
 */
27
28
#include <pjmedia/port.h>
29
#include <pj/errno.h>
30
#include <pj/list.h>
31
#include <pj/pool.h>
32
33
PJ_BEGIN_DECL
34
35
36
/**
37
 * @defgroup PJMEDIA_CODEC Codec Framework
38
 * @brief Media codec framework and management
39
 * @{
40
 *
41
 * @section codec_mgmt_sec Codec Management
42
 * @subsection codec_fact_sec Codec Manager
43
 *
44
 * The codec manager is used to manage all codec capabilities in the endpoint.
45
 * When used with media endpoint (pjmedia_endpt), application can retrieve
46
 * the codec manager instance by calling #pjmedia_endpt_get_codec_mgr().
47
 *
48
 * @subsection reg_new_codec Registering New Codec
49
 *
50
 * New codec types can be registered to PJMEDIA (or to be precise, to the 
51
 * codec manager) during run-time. 
52
 * To do this, application needs to initialize an instance of
53
 * codec factory (#pjmedia_codec_factory) and registers this codec factory
54
 * by calling #pjmedia_codec_mgr_register_factory().
55
 *
56
 * For codecs implemented/supported by PJMEDIA, this process is normally
57
 * concealed in an easy to use function such as #pjmedia_codec_g711_init().
58
 *
59
 * @subsection codec_factory Codec Factory
60
 *
61
 * A codec factory (#pjmedia_codec_factory) is registered to codec manager, 
62
 * and it is used to create and release codec instance.
63
 *
64
 * The most important member of the codec factory is the "virtual" function
65
 * table #pjmedia_codec_factory_op, where it contains, among other thing, 
66
 * pointer to functions to allocate and deallocate codec instance.
67
 *
68
 * @subsection codec_inst Codec Instance
69
 *
70
 * Application allocates codec instance by calling
71
 * #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
 */
197
enum 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
 */
235
typedef 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
 */
249
typedef 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
 */
268
typedef struct pjmedia_codec_param
269
{
270
    /**
271
     * The "info" part of codec param describes the capability of the codec,
272
     * and is recommended not to be modified unless necessary.
273
     * Note that application must be ready to handle cases when ptime
274
     * denumerators are zero, since most codecs that only support integer
275
     * ptime will leave these fields untouched.
276
     */
277
    struct {
278
       unsigned    clock_rate;          /**< Sampling rate in Hz            */
279
       unsigned    channel_cnt;         /**< Channel count.                 */
280
       pj_uint32_t avg_bps;             /**< Average bandwidth in bits/sec  */
281
       pj_uint32_t max_bps;             /**< Maximum bandwidth in bits/sec  */
282
       unsigned    max_rx_frame_size;   /**< Maximum frame size             */
283
       pj_uint16_t frm_ptime;           /**< Decoder frame ptime in msec.   */
284
       pj_uint8_t  frm_ptime_denum;     /**< Decoder frame ptime denum, or
285
                                             zero if ptime is integer.      */
286
       pj_uint16_t enc_ptime;           /**< Encoder ptime, or zero if it's
287
                                             equal to decoder ptime.        */
288
       pj_uint8_t  enc_ptime_denum;     /**< Encoder frame ptime denum, or
289
                                             zero if ptime is integer.      */
290
       pj_uint8_t  pcm_bits_per_sample; /**< Bits/sample in the PCM side    */
291
       pj_uint8_t  pt;                  /**< Payload type.                  */
292
       pjmedia_format_id fmt_id;        /**< Source format, it's format of
293
                                             encoder input and decoder 
294
                                             output.                        */
295
    } info;
296
297
    /**
298
     * The "setting" part of codec param describes various settings to be
299
     * applied to the codec. When the codec param is retrieved from the codec
300
     * or codec factory, the values of these will be filled by the capability
301
     * of the codec. Any features that are supported by the codec (e.g. vad
302
     * or plc) will be turned on, so that application can query which 
303
     * capabilities are supported by the codec. Application may change the
304
     * settings here before instantiating or modifying the codec.
305
     */
306
    struct {
307
        pj_uint8_t  frm_per_pkt;    /**< Number of frames per packet.   */
308
        unsigned    vad:1;          /**< Voice Activity Detector.       */
309
        unsigned    cng:1;          /**< Comfort Noise Generator.       */
310
        unsigned    penh:1;         /**< Perceptual Enhancement         */
311
        unsigned    plc:1;          /**< Packet loss concealment        */
312
        unsigned    reserved:1;     /**< Reserved, must be zero.        */
313
        pjmedia_codec_fmtp enc_fmtp;/**< Encoder's fmtp params.         */
314
        pjmedia_codec_fmtp dec_fmtp;/**< Decoder's fmtp params.         */
315
        unsigned   packet_loss;     /**< Encoder's expected pkt loss %. */
316
        unsigned   complexity;      /**< Encoder complexity, 0-10(max). */
317
        pj_bool_t  cbr;             /**< Constant bit rate?             */
318
    } setting;
319
} pjmedia_codec_param;
320
321
322
/**
323
 * Duplicate codec parameter.
324
 *
325
 * @param pool      The pool.
326
 * @param src       The codec parameter to be duplicated.
327
 *
328
 * @return          Duplicated codec parameter.
329
 */
330
PJ_DECL(pjmedia_codec_param*) pjmedia_codec_param_clone(
331
                                        pj_pool_t *pool, 
332
                                        const pjmedia_codec_param *src);
333
334
335
/*
336
 * Forward declaration for pjmedia_codec.
337
 */
338
typedef struct pjmedia_codec pjmedia_codec;
339
340
341
/**
342
 * This structure describes codec operations. Each codec MUST implement
343
 * all of these functions.
344
 */
345
typedef struct pjmedia_codec_op
346
{
347
    /** 
348
     * Initialize codec using the specified attribute.
349
     *
350
     * Application should call #pjmedia_codec_init() instead of 
351
     * calling this function directly.
352
     *
353
     * @param codec     The codec instance.
354
     * @param pool      Pool to use when the codec needs to allocate
355
     *                  some memory.
356
     *
357
     * @return          PJ_SUCCESS on success.
358
     */
359
    pj_status_t (*init)(pjmedia_codec *codec, 
360
                        pj_pool_t *pool );
361
362
    /** 
363
     * Open the codec and initialize with the specified parameter.
364
     * Upon successful initialization, the codec may modify the parameter
365
     * and fills in the unspecified values (such as enc_ptime/enc_ptime_denum,
366
     * when encoder ptime is different than decoder ptime).
367
     *
368
     * Application should call #pjmedia_codec_open() instead of 
369
     * calling this function directly.
370
     *
371
     * @param codec     The codec instance.
372
     * @param param     Codec initialization parameter.
373
     *
374
     * @return          PJ_SUCCESS on success.
375
     */
376
    pj_status_t (*open)(pjmedia_codec *codec, 
377
                        pjmedia_codec_param *param );
378
379
    /** 
380
     * Close and shutdown codec, releasing all resources allocated by
381
     * this codec, if any.
382
     *
383
     * Application should call #pjmedia_codec_close() instead of 
384
     * calling this function directly.
385
     *
386
     * @param codec     The codec instance.
387
     *
388
     * @return          PJ_SUCCESS on success.
389
     */
390
    pj_status_t (*close)(pjmedia_codec *codec);
391
392
    /** 
393
     * Modify the codec parameter after the codec is open.
394
     * Note that not all codec parameters can be modified during run-time.
395
     * Currently, only Opus codec supports changing key codec parameters
396
     * such as bitrate and bandwidth, while other codecs may only be able to
397
     * modify minor settings such as VAD or PLC.
398
     *
399
     * Application should call #pjmedia_codec_modify() instead of 
400
     * calling this function directly.
401
     *
402
     * @param codec     The codec instance.
403
     * @param param     The new codec parameter.
404
     *
405
     * @return          PJ_SUCCESS on success.
406
     */
407
    pj_status_t (*modify)(pjmedia_codec *codec, 
408
                          const pjmedia_codec_param *param );
409
410
    /**
411
     * Instruct the codec to inspect the specified payload/packet and
412
     * split the packet into individual base frames. Each output frames will
413
     * have ptime that is equal to basic frame ptime (i.e. the value of
414
     * info.frm_ptime/info.frm_ptime_denum in #pjmedia_codec_param).
415
     *
416
     * Application should call #pjmedia_codec_parse() instead of 
417
     * calling this function directly.
418
     *
419
     * @param codec     The codec instance
420
     * @param pkt       The input packet.
421
     * @param pkt_size  Size of the packet.
422
     * @param timestamp The timestamp of the first sample in the packet.
423
     * @param frame_cnt On input, specifies the maximum number of frames
424
     *                  in the array. On output, the codec must fill
425
     *                  with number of frames detected in the packet.
426
     * @param frames    On output, specifies the frames that have been
427
     *                  detected in the packet.
428
     *
429
     * @return          PJ_SUCCESS on success.
430
     */
431
    pj_status_t (*parse)( pjmedia_codec *codec,
432
                          void *pkt,
433
                          pj_size_t pkt_size,
434
                          const pj_timestamp *timestamp,
435
                          unsigned *frame_cnt,
436
                          pjmedia_frame frames[]);
437
438
    /** 
439
     * Instruct the codec to encode the specified input frame. The input
440
     * PCM samples MUST have ptime that is multiplication of base frame
441
     * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
442
     * #pjmedia_codec_param).
443
     *
444
     * Application should call #pjmedia_codec_encode() instead of 
445
     * calling this function directly.
446
     *
447
     * @param codec     The codec instance.
448
     * @param input     The input frame.
449
     * @param out_size  The length of buffer in the output frame.
450
     * @param output    The output frame.
451
     *
452
     * @return          PJ_SUCCESS on success;
453
     */
454
    pj_status_t (*encode)(pjmedia_codec *codec, 
455
                          const struct pjmedia_frame *input,
456
                          unsigned out_size, 
457
                          struct pjmedia_frame *output);
458
459
    /** 
460
     * Instruct the codec to decode the specified input frame. The input
461
     * frame MUST have ptime that is exactly equal to base frame
462
     * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
463
     * #pjmedia_codec_param).
464
     * Application can achieve this by parsing the packet into base
465
     * frames before decoding each frame.
466
     *
467
     * Application should call #pjmedia_codec_decode() instead of 
468
     * calling this function directly.
469
     *
470
     * @param codec     The codec instance.
471
     * @param input     The input frame.
472
     * @param out_size  The length of buffer in the output frame.
473
     * @param output    The output frame.
474
     *
475
     * @return          PJ_SUCCESS on success;
476
     */
477
    pj_status_t (*decode)(pjmedia_codec *codec, 
478
                          const struct pjmedia_frame *input,
479
                          unsigned out_size, 
480
                          struct pjmedia_frame *output);
481
482
    /**
483
     * Instruct the codec to recover a missing frame.
484
     *
485
     * Application should call #pjmedia_codec_recover() instead of 
486
     * calling this function directly.
487
     *
488
     * @param codec     The codec instance.
489
     * @param out_size  The length of buffer in the output frame.
490
     * @param output    The output frame where generated signal
491
     *                  will be placed.
492
     *
493
     * @return          PJ_SUCCESS on success;
494
     */
495
    pj_status_t (*recover)(pjmedia_codec *codec,
496
                           unsigned out_size,
497
                           struct pjmedia_frame *output);
498
} pjmedia_codec_op;
499
500
501
502
/*
503
 * Forward declaration for pjmedia_codec_factory.
504
 */
505
typedef struct pjmedia_codec_factory pjmedia_codec_factory;
506
507
508
/**
509
 * This structure describes a codec instance. 
510
 */
511
struct pjmedia_codec
512
{
513
    /** Entries to put this codec instance in codec factory's list. */
514
    PJ_DECL_LIST_MEMBER(struct pjmedia_codec);
515
516
    /** Codec's private data. */
517
    void                    *codec_data;
518
519
    /** Codec factory where this codec was allocated. */
520
    pjmedia_codec_factory   *factory;
521
522
    /** Operations to codec. */
523
    pjmedia_codec_op        *op;
524
};
525
526
527
528
/**
529
 * This structure describes operations that must be supported by codec 
530
 * factories.
531
 */
532
typedef struct pjmedia_codec_factory_op
533
{
534
    /** 
535
     * Check whether the factory can create codec with the specified 
536
     * codec info.
537
     *
538
     * @param factory   The codec factory.
539
     * @param info      The codec info.
540
     *
541
     * @return          PJ_SUCCESS if this factory is able to create an
542
     *                  instance of codec with the specified info.
543
     */
544
    pj_status_t (*test_alloc)(pjmedia_codec_factory *factory, 
545
                              const pjmedia_codec_info *info );
546
547
    /** 
548
     * Create default attributes for the specified codec ID. This function
549
     * can be called by application to get the capability of the codec.
550
     *
551
     * @param factory   The codec factory.
552
     * @param info      The codec info.
553
     * @param attr      The attribute to be initialized.
554
     *
555
     * @return          PJ_SUCCESS if success.
556
     */
557
    pj_status_t (*default_attr)(pjmedia_codec_factory *factory, 
558
                                const pjmedia_codec_info *info,
559
                                pjmedia_codec_param *attr );
560
561
    /** 
562
     * Enumerate supported codecs that can be created using this factory.
563
     * 
564
     *  @param factory  The codec factory.
565
     *  @param count    On input, specifies the number of elements in
566
     *                  the array. On output, the value will be set to
567
     *                  the number of elements that have been initialized
568
     *                  by this function.
569
     *  @param info     The codec info array, which contents will be 
570
     *                  initialized upon return.
571
     *
572
     *  @return         PJ_SUCCESS on success.
573
     */
574
    pj_status_t (*enum_info)(pjmedia_codec_factory *factory, 
575
                             unsigned *count, 
576
                             pjmedia_codec_info codecs[]);
577
578
    /** 
579
     * Create one instance of the codec with the specified codec info.
580
     *
581
     * @param factory   The codec factory.
582
     * @param info      The codec info.
583
     * @param p_codec   Pointer to receive the codec instance.
584
     *
585
     * @return          PJ_SUCCESS on success.
586
     */
587
    pj_status_t (*alloc_codec)(pjmedia_codec_factory *factory, 
588
                               const pjmedia_codec_info *info,
589
                               pjmedia_codec **p_codec);
590
591
    /** 
592
     * This function is called by codec manager to return a particular 
593
     * instance of codec back to the codec factory.
594
     *
595
     * @param factory   The codec factory.
596
     * @param codec     The codec instance to be returned.
597
     *
598
     * @return          PJ_SUCCESS on success.
599
     */
600
    pj_status_t (*dealloc_codec)(pjmedia_codec_factory *factory, 
601
                                 pjmedia_codec *codec );
602
603
    /**
604
     * This callback will be called to deinitialize and destroy this factory.
605
     */
606
    pj_status_t (*destroy)(void);
607
608
} pjmedia_codec_factory_op;
609
610
611
612
/**
613
 * Codec factory describes a module that is able to create codec with specific
614
 * capabilities. These capabilities can be queried by codec manager to create
615
 * instances of codec.
616
 */
617
struct pjmedia_codec_factory
618
{
619
    /** Entries to put this structure in the codec manager list. */
620
    PJ_DECL_LIST_MEMBER(struct pjmedia_codec_factory);
621
622
    /** The factory's private data. */
623
    void                     *factory_data;
624
625
    /** Operations to the factory. */
626
    pjmedia_codec_factory_op *op;
627
628
};
629
630
631
/**
632
 * Declare maximum codecs
633
 */
634
#define PJMEDIA_CODEC_MGR_MAX_CODECS        32
635
636
637
/**
638
 * Specify these values to set the codec priority, by calling
639
 * #pjmedia_codec_mgr_set_codec_priority().
640
 */
641
typedef enum pjmedia_codec_priority
642
{
643
    /**
644
     * This priority makes the codec the highest in the order.
645
     * The last codec specified with this priority will get the
646
     * highest place in the order, and will change the priority
647
     * of previously highest priority codec to NEXT_HIGHER.
648
     */
649
    PJMEDIA_CODEC_PRIO_HIGHEST = 255,
650
651
    /**
652
     * This priority will put the codec as the next codec after
653
     * codecs with this same priority.
654
     */
655
    PJMEDIA_CODEC_PRIO_NEXT_HIGHER = 254,
656
657
    /**
658
     * This is the initial codec priority when it is registered to
659
     * codec manager by codec factory.
660
     */
661
    PJMEDIA_CODEC_PRIO_NORMAL = 128,
662
663
    /**
664
     * This priority makes the codec the lowest in the order.
665
     * The last codec specified with this priority will be put
666
     * in the last place in the order.
667
     */
668
    PJMEDIA_CODEC_PRIO_LOWEST = 1,
669
670
    /**
671
     * This priority will prevent the codec from being listed in the
672
     * SDP created by media endpoint, thus should prevent the codec
673
     * from being used in the sessions. However, the codec will still
674
     * be listed by #pjmedia_codec_mgr_enum_codecs() and other codec
675
     * query functions.
676
     */
677
    PJMEDIA_CODEC_PRIO_DISABLED = 0
678
679
} pjmedia_codec_priority;
680
681
682
/** 
683
 * Codec identification (e.g. "pcmu/8000/1").
684
 * See @ref codec_ident for more info.
685
 */
686
typedef char pjmedia_codec_id[32];
687
688
689
/**
690
 * Opaque declaration of default codecs parameters.
691
 */
692
typedef struct pjmedia_codec_default_param pjmedia_codec_default_param;
693
694
/** 
695
 * Codec manager maintains array of these structs for each supported
696
 * codec.
697
 */
698
struct pjmedia_codec_desc
699
{
700
    pjmedia_codec_info      info;       /**< Codec info.            */
701
    pjmedia_codec_id        id;         /**< Fully qualified name   */
702
    pjmedia_codec_priority  prio;       /**< Priority.              */
703
    pjmedia_codec_factory  *factory;    /**< The factory.           */
704
    pjmedia_codec_default_param *param; /**< Default codecs 
705
                                             parameters.            */
706
};
707
708
709
/**
710
 * The declaration for codec manager. Application doesn't normally need
711
 * to see this declaration, but nevertheless this declaration is needed
712
 * by media endpoint to instantiate the codec manager.
713
 */
714
typedef struct pjmedia_codec_mgr
715
{
716
    /** Media endpoint instance. */
717
    pj_pool_factory             *pf;
718
719
    /** Codec manager pool. */
720
    pj_pool_t                   *pool;
721
722
    /** Codec manager mutex. */
723
    pj_mutex_t                  *mutex;
724
725
    /** List of codec factories registered to codec manager. */
726
    pjmedia_codec_factory        factory_list;
727
728
    /** Number of supported codecs. */
729
    unsigned                     codec_cnt;
730
731
    /** Array of codec descriptor. */
732
    struct pjmedia_codec_desc    codec_desc[PJMEDIA_CODEC_MGR_MAX_CODECS];
733
734
    /** Number of codecs with dynamic PT. */
735
    unsigned                     dyn_codecs_cnt;
736
737
    /** Array of codec identifiers with dynamic PT. */
738
    pj_str_t                     dyn_codecs[PJMEDIA_CODEC_MGR_MAX_CODECS];
739
740
#if defined(PJMEDIA_RTP_PT_TELEPHONE_EVENTS) && \
741
            PJMEDIA_RTP_PT_TELEPHONE_EVENTS != 0
742
    /** Number of televent clockrates. */
743
    unsigned                     televent_num;
744
745
    /** Array of televent clockrates. */
746
    unsigned                     televent_clockrates[8];
747
#endif
748
749
750
} pjmedia_codec_mgr;
751
752
753
754
/**
755
 * Initialize codec manager. Normally this function is called by pjmedia
756
 * endpoint's initialization code.
757
 *
758
 * @param mgr       Codec manager instance.
759
 * @param pf        Pool factory instance.
760
 *
761
 * @return          PJ_SUCCESS on success.
762
 */
763
PJ_DECL(pj_status_t) pjmedia_codec_mgr_init(pjmedia_codec_mgr *mgr, 
764
                                            pj_pool_factory *pf);
765
766
767
/**
768
 * Destroy codec manager. Normally this function is called by pjmedia
769
 * endpoint's deinitialization code.
770
 *
771
 * @param mgr       Codec manager instance.
772
 *
773
 * @return          PJ_SUCCESS on success.
774
 */
775
PJ_DECL(pj_status_t) pjmedia_codec_mgr_destroy(pjmedia_codec_mgr *mgr);
776
777
778
/** 
779
 * Register codec factory to codec manager. This will also register
780
 * all supported codecs in the factory to the codec manager.
781
 *
782
 * @param mgr       The codec manager instance. Application can get the
783
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
784
 * @param factory   The codec factory to be registered.
785
 *
786
 * @return          PJ_SUCCESS on success.
787
 */
788
PJ_DECL(pj_status_t) 
789
pjmedia_codec_mgr_register_factory( pjmedia_codec_mgr *mgr,
790
                                    pjmedia_codec_factory *factory);
791
792
/**
793
 * Unregister codec factory from the codec manager. This will also
794
 * remove all the codecs registered by the codec factory from the
795
 * codec manager's list of supported codecs. This function should
796
 * only be called by the codec implementers and not by application.
797
 *
798
 * @param mgr       The codec manager instance, use
799
 *                      #pjmedia_endpt_get_codec_mgr().
800
 * @param factory   The codec factory to be unregistered.
801
 *
802
 * @return          PJ_SUCCESS on success.
803
 */
804
PJ_DECL(pj_status_t) 
805
pjmedia_codec_mgr_unregister_factory( pjmedia_codec_mgr *mgr, 
806
                                      pjmedia_codec_factory *factory);
807
808
/**
809
 * Enumerate all supported codecs that have been registered to the
810
 * codec manager by codec factories.
811
 *
812
 * @param mgr       The codec manager instance. Application can get the
813
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
814
 * @param count     On input, specifies the number of elements in
815
 *                  the array. On output, the value will be set to
816
 *                  the number of elements that have been initialized
817
 *                  by this function.
818
 * @param info      The codec info array, which contents will be 
819
 *                  initialized upon return.
820
 * @param prio      Optional pointer to receive array of codec priorities.
821
 *
822
 * @return          PJ_SUCCESS on success.
823
 */
824
PJ_DECL(pj_status_t) pjmedia_codec_mgr_enum_codecs( pjmedia_codec_mgr *mgr, 
825
                                                    unsigned *count, 
826
                                                    pjmedia_codec_info info[],
827
                                                    unsigned *prio);
828
829
/**
830
 * Get codec info for the specified static payload type. Note that
831
 * this can only find codec with static payload types. This function can
832
 * be used to find codec info for a payload type inside SDP which doesn't
833
 * have the corresponding rtpmap attribute.
834
 *
835
 * @param mgr       The codec manager instance. Application can get the
836
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
837
 * @param pt        Static payload type/number.
838
 * @param inf       Pointer to receive codec info.
839
 *
840
 * @return          PJ_SUCCESS on success.
841
 */
842
PJ_DECL(pj_status_t) 
843
pjmedia_codec_mgr_get_codec_info( pjmedia_codec_mgr *mgr,
844
                                  unsigned pt,
845
                                  const pjmedia_codec_info **inf);
846
847
/**
848
 * Convert codec info struct into a unique codec identifier.
849
 * A codec identifier looks something like "L16/44100/2".
850
 *
851
 * @param info      The codec info
852
 * @param id        Buffer to put the codec info string.
853
 * @param max_len   The length of the buffer.
854
 *
855
 * @return          The null terminated codec info string, or NULL if
856
 *                  the buffer is not long enough.
857
 */
858
PJ_DECL(char*) pjmedia_codec_info_to_id(const pjmedia_codec_info *info,
859
                                        char *id, unsigned max_len );
860
861
862
/**
863
 * Find codecs by the unique codec identifier. This function will find
864
 * all codecs that match the codec identifier prefix. For example, if
865
 * "L16" is specified, then it will find "L16/8000/1", "L16/16000/1",
866
 * and so on, up to the maximum count specified in the argument.
867
 *
868
 * @param mgr       The codec manager instance. Application can get the
869
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
870
 * @param codec_id  The full codec ID or codec ID prefix. If an empty
871
 *                  string is given, it will match all codecs.
872
 * @param count     Maximum number of codecs to find. On return, it
873
 *                  contains the actual number of codecs found.
874
 * @param p_info    Array of pointer to codec info to be filled. This
875
 *                  argument may be NULL, which in this case, only
876
 *                  codec count will be returned.
877
 * @param prio      Optional array of codec priorities.
878
 *
879
 * @return          PJ_SUCCESS if at least one codec info is found.
880
 */
881
PJ_DECL(pj_status_t) 
882
pjmedia_codec_mgr_find_codecs_by_id( pjmedia_codec_mgr *mgr,
883
                                     const pj_str_t *codec_id,
884
                                     unsigned *count,
885
                                     const pjmedia_codec_info *p_info[],
886
                                     unsigned prio[]);
887
888
889
/**
890
 * Set codec priority. The codec priority determines the order of
891
 * the codec in the SDP created by the endpoint. If more than one codecs
892
 * are found with the same codec_id prefix, then the function sets the
893
 * priorities of all those codecs.
894
 *
895
 * @param mgr       The codec manager instance. Application can get the
896
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
897
 * @param codec_id  The full codec ID or codec ID prefix. If an empty
898
 *                  string is given, it will match all codecs.
899
 * @param prio      Priority to be set. The priority can have any value
900
 *                  between 1 to 255. When the priority is set to zero,
901
 *                  the codec will be disabled.
902
 *
903
 * @return          PJ_SUCCESS if at least one codec info is found.
904
 */
905
PJ_DECL(pj_status_t)
906
pjmedia_codec_mgr_set_codec_priority(pjmedia_codec_mgr *mgr, 
907
                                     const pj_str_t *codec_id,
908
                                     pj_uint8_t prio);
909
910
911
/**
912
 * Get default codec param for the specified codec info.
913
 *
914
 * @param mgr       The codec manager instance. Application can get the
915
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
916
 * @param info      The codec info, which default parameter's is being
917
 *                  queried.
918
 * @param param     On return, will be filled with the default codec
919
 *                  parameter.
920
 *
921
 * @return          PJ_SUCCESS on success.
922
 */
923
PJ_DECL(pj_status_t) 
924
pjmedia_codec_mgr_get_default_param( pjmedia_codec_mgr *mgr,
925
                                     const pjmedia_codec_info *info,
926
                                     pjmedia_codec_param *param );
927
928
929
/**
930
 * Set default codec param for the specified codec info.
931
 *
932
 * @param mgr       The codec manager instance. Application can get the
933
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
934
 * @param info      The codec info, which default parameter's is being
935
 *                  updated.
936
 * @param param     The new default codec parameter. Set to NULL to reset
937
 *                  codec parameter to library default settings.
938
 *
939
 * @return          PJ_SUCCESS on success.
940
 */
941
PJ_DECL(pj_status_t) 
942
pjmedia_codec_mgr_set_default_param( pjmedia_codec_mgr *mgr,
943
                                     const pjmedia_codec_info *info,
944
                                     const pjmedia_codec_param *param );
945
946
947
/**
948
 * Request the codec manager to allocate one instance of codec with the
949
 * specified codec info. The codec will enumerate all codec factories
950
 * until it finds factory that is able to create the specified codec.
951
 *
952
 * @param mgr       The codec manager instance. Application can get the
953
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
954
 * @param info      The information about the codec to be created.
955
 * @param p_codec   Pointer to receive the codec instance.
956
 *
957
 * @return          PJ_SUCCESS on success.
958
 */
959
PJ_DECL(pj_status_t) 
960
pjmedia_codec_mgr_alloc_codec( pjmedia_codec_mgr *mgr, 
961
                               const pjmedia_codec_info *info,
962
                               pjmedia_codec **p_codec);
963
964
/**
965
 * Deallocate the specified codec instance. The codec manager will return
966
 * the instance of the codec back to its factory.
967
 *
968
 * @param mgr       The codec manager instance. Application can get the
969
 *                  instance by calling #pjmedia_endpt_get_codec_mgr().
970
 * @param codec     The codec instance.
971
 *
972
 * @return          PJ_SUCESS on success.
973
 */
974
PJ_DECL(pj_status_t) pjmedia_codec_mgr_dealloc_codec(pjmedia_codec_mgr *mgr, 
975
                                                     pjmedia_codec *codec);
976
977
978
979
/** 
980
 * Initialize codec using the specified attribute.
981
 *
982
 * @param codec     The codec instance.
983
 * @param pool      Pool to use when the codec needs to allocate some memory.
984
 *
985
 * @return          PJ_SUCCESS on success.
986
 */
987
PJ_INLINE(pj_status_t) pjmedia_codec_init( pjmedia_codec *codec, 
988
                                           pj_pool_t *pool )
989
0
{
990
0
    return (*codec->op->init)(codec, pool);
991
0
}
992
993
994
/** 
995
 * Open the codec and initialize with the specified parameter.
996
 * Upon successful initialization, the codec may modify the parameter
997
 * and fills in the unspecified values (such as enc_ptime, when
998
 * encoder ptime is different than decoder ptime).
999
 *
1000
 * @param codec     The codec instance.
1001
 * @param param     Codec initialization parameter.
1002
 *
1003
 * @return          PJ_SUCCESS on success.
1004
 */
1005
PJ_INLINE(pj_status_t) pjmedia_codec_open( pjmedia_codec *codec, 
1006
                                           pjmedia_codec_param *param )
1007
0
{
1008
0
    return (*codec->op->open)(codec, param);
1009
0
}
1010
1011
1012
/** 
1013
 * Close and shutdown codec, releasing all resources allocated by
1014
 * this codec, if any.
1015
 *
1016
 * @param codec     The codec instance.
1017
 *
1018
 * @return          PJ_SUCCESS on success.
1019
 */
1020
PJ_INLINE(pj_status_t) pjmedia_codec_close( pjmedia_codec *codec )
1021
0
{
1022
0
    return (*codec->op->close)(codec);
1023
0
}
1024
1025
1026
/** 
1027
 * Modify the codec parameter after the codec is open.
1028
 * Note that not all codec parameters can be modified during run-time.
1029
 * Currently, only Opus codec supports changing key codec parameters
1030
 * such as bitrate and bandwidth, while other codecs may only be able to
1031
 * modify minor settings such as VAD or PLC.
1032
 *
1033
 * @param codec     The codec instance.
1034
 * @param param     The new codec parameter.
1035
 *
1036
 * @return          PJ_SUCCESS on success.
1037
 */
1038
PJ_INLINE(pj_status_t) pjmedia_codec_modify(pjmedia_codec *codec, 
1039
                                            const pjmedia_codec_param *param)
1040
0
{
1041
0
    return (*codec->op->modify)(codec, param);
1042
0
}
1043
1044
1045
/**
1046
 * Instruct the codec to inspect the specified payload/packet and
1047
 * split the packet into individual base frames. Each output frames will
1048
 * have ptime that is equal to basic frame ptime (i.e. the value of
1049
 * info.frm_ptime/info.frm_ptime_denum in #pjmedia_codec_param).
1050
 *
1051
 * @param codec     The codec instance
1052
 * @param pkt       The input packet.
1053
 * @param pkt_size  Size of the packet.
1054
 * @param timestamp The timestamp of the first sample in the packet.
1055
 * @param frame_cnt On input, specifies the maximum number of frames
1056
 *                  in the array. On output, the codec must fill
1057
 *                  with number of frames detected in the packet.
1058
 * @param frames    On output, specifies the frames that have been
1059
 *                  detected in the packet.
1060
 *
1061
 * @return          PJ_SUCCESS on success.
1062
 */
1063
PJ_INLINE(pj_status_t) pjmedia_codec_parse( pjmedia_codec *codec,
1064
                                            void *pkt,
1065
                                            pj_size_t pkt_size,
1066
                                            const pj_timestamp *timestamp,
1067
                                            unsigned *frame_cnt,
1068
                                            pjmedia_frame frames[] )
1069
0
{
1070
0
    return (*codec->op->parse)(codec, pkt, pkt_size, timestamp,
1071
0
                               frame_cnt, frames);
1072
0
}
1073
1074
1075
/** 
1076
 * Instruct the codec to encode the specified input frame. The input
1077
 * PCM samples MUST have ptime that is multiplication of base frame
1078
 * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
1079
 * #pjmedia_codec_param).
1080
 *
1081
 * @param codec         The codec instance.
1082
 * @param input         The input frame.
1083
 * @param out_size      The length of buffer in the output frame.
1084
 * @param output        The output frame.
1085
 *
1086
 * @return              PJ_SUCCESS on success;
1087
 */
1088
PJ_INLINE(pj_status_t) pjmedia_codec_encode( 
1089
                                        pjmedia_codec *codec, 
1090
                                        const struct pjmedia_frame *input,
1091
                                        unsigned out_size, 
1092
                                        struct pjmedia_frame *output )
1093
0
{
1094
0
    return (*codec->op->encode)(codec, input, out_size, output);
1095
0
}
1096
1097
1098
/** 
1099
 * Instruct the codec to decode the specified input frame. The input
1100
 * frame MUST have ptime that is exactly equal to base frame
1101
 * ptime (i.e. the value of info.frm_ptime/info.frm_ptime_denum in
1102
 * #pjmedia_codec_param).
1103
 * Application can achieve this by parsing the packet into base
1104
 * frames before decoding each frame.
1105
 *
1106
 * @param codec         The codec instance.
1107
 * @param input         The input frame.
1108
 * @param out_size      The length of buffer in the output frame.
1109
 * @param output        The output frame.
1110
 *
1111
 * @return              PJ_SUCCESS on success;
1112
 */
1113
PJ_INLINE(pj_status_t) pjmedia_codec_decode( 
1114
                                        pjmedia_codec *codec, 
1115
                                        const struct pjmedia_frame *input,
1116
                                        unsigned out_size, 
1117
                                        struct pjmedia_frame *output )
1118
0
{
1119
0
    return (*codec->op->decode)(codec, input, out_size, output);
1120
0
}
1121
1122
1123
/**
1124
 * Instruct the codec to recover a missing frame.
1125
 *
1126
 * @param codec         The codec instance.
1127
 * @param out_size      The length of buffer in the output frame.
1128
 * @param output        The output frame where generated signal
1129
 *                      will be placed.
1130
 *
1131
 * @return              PJ_SUCCESS on success;
1132
 */
1133
PJ_INLINE(pj_status_t) pjmedia_codec_recover( pjmedia_codec *codec,
1134
                                              unsigned out_size,
1135
                                              struct pjmedia_frame *output )
1136
0
{
1137
0
    if (codec->op && codec->op->recover)
1138
0
        return (*codec->op->recover)(codec, out_size, output);
1139
0
    else
1140
0
        return PJ_ENOTSUP;
1141
0
}
1142
1143
1144
/**
1145
 * @}
1146
 */
1147
1148
/**
1149
 * @defgroup PJMEDIA_CODEC_CODECS Supported codecs
1150
 * @ingroup PJMEDIA_CODEC
1151
 * @brief Documentation about individual codec supported by PJMEDIA
1152
 * @{
1153
 * Please see the APIs provided by the individual codecs below.
1154
 */
1155
/**
1156
 * @}
1157
 */
1158
1159
/*
1160
 * Internal functions.
1161
 */
1162
1163
/**
1164
 * Internal: Get the array of codec identifiers that have dynamic PT.
1165
 * Note that the array also includes telephone events.
1166
 */
1167
pj_status_t pjmedia_codec_mgr_get_dyn_codecs(pjmedia_codec_mgr* mgr,
1168
                                             pj_int8_t *count,
1169
                                             pj_str_t dyn_codecs[]);
1170
1171
/* Internal: Find a certain codec string in the dynamic codecs array.
1172
 * Return the index of the array if the string is found and set
1173
 * *found == PJ_TRUE. If the string is not found, returns -1 if found
1174
 * is specified, otherwise returns the index in the array where
1175
 * the particular string could be inserted so that the array remains
1176
 * sorted, and set *found == PJ_FALSE.
1177
 */
1178
int pjmedia_codec_mgr_find_codec(const pj_str_t dyn_codecs[],
1179
                                 unsigned count,
1180
                                 const pj_str_t *codec,
1181
                                 pj_bool_t *found);
1182
1183
/* Internal: Insert a codec ID string into the dynamic codecs array and keep
1184
 * the array sorted.
1185
 */
1186
void pjmedia_codec_mgr_insert_codec(pj_pool_t *pool, pj_str_t dyn_codecs[],
1187
                                    unsigned *count, const pj_str_t *codec);
1188
1189
PJ_END_DECL
1190
1191
1192
#endif  /* __PJMEDIA_CODEC_H__ */