~ubuntu-branches/ubuntu/raring/sflphone/raring

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjmedia/include/pjmedia/types.h

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: types.h 3553 2011-05-05 06:14:19Z nanang $ */
 
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_TYPES_H__
 
21
#define __PJMEDIA_TYPES_H__
 
22
 
 
23
/**
 
24
 * @file pjmedia/types.h Basic Types
 
25
 * @brief Basic PJMEDIA types.
 
26
 */
 
27
 
 
28
#include <pjmedia/config.h>
 
29
#include <pj/sock.h>        /* pjmedia_sock_info        */
 
30
#include <pj/string.h>      /* pj_memcpy(), pj_memset() */
 
31
 
 
32
/**
 
33
 * @defgroup PJMEDIA_PORT Media Ports Framework
 
34
 * @brief Extensible framework for media terminations
 
35
 */
 
36
 
 
37
 
 
38
/**
 
39
 * @defgroup PJMEDIA_FRAME_OP Audio Manipulation Algorithms
 
40
 * @brief Algorithms to manipulate audio frames
 
41
 */
 
42
 
 
43
/**
 
44
 * @defgroup PJMEDIA_TYPES Basic Types
 
45
 * @ingroup PJMEDIA_BASE
 
46
 * @brief Basic PJMEDIA types and operations.
 
47
 * @{
 
48
 */
 
49
 
 
50
/**
 
51
 * Top most media type.
 
52
 */
 
53
typedef enum pjmedia_type
 
54
{
 
55
    /** No type. */
 
56
    PJMEDIA_TYPE_NONE = 0,
 
57
 
 
58
    /** The media is audio */
 
59
    PJMEDIA_TYPE_AUDIO = 1,
 
60
 
 
61
    /** The media is video. */
 
62
    PJMEDIA_TYPE_VIDEO = 2,
 
63
 
 
64
    /** Unknown media type, in this case the name will be specified in
 
65
     *  encoding_name.
 
66
     */
 
67
    PJMEDIA_TYPE_UNKNOWN = 3,
 
68
 
 
69
    /** The media is application. */
 
70
    PJMEDIA_TYPE_APPLICATION = 4
 
71
 
 
72
} pjmedia_type;
 
73
 
 
74
 
 
75
/**
 
76
 * Media transport protocol.
 
77
 */
 
78
typedef enum pjmedia_tp_proto
 
79
{
 
80
    /** No transport type */
 
81
    PJMEDIA_TP_PROTO_NONE = 0,
 
82
 
 
83
    /** RTP using A/V profile */
 
84
    PJMEDIA_TP_PROTO_RTP_AVP,
 
85
 
 
86
    /** Secure RTP */
 
87
    PJMEDIA_TP_PROTO_RTP_SAVP,
 
88
 
 
89
    /** Unknown */
 
90
    PJMEDIA_TP_PROTO_UNKNOWN
 
91
 
 
92
} pjmedia_tp_proto;
 
93
 
 
94
 
 
95
/**
 
96
 * Media direction.
 
97
 */
 
98
typedef enum pjmedia_dir
 
99
{
 
100
    /** None */
 
101
    PJMEDIA_DIR_NONE = 0,
 
102
 
 
103
    /** Encoding (outgoing to network) stream */
 
104
    PJMEDIA_DIR_ENCODING = 1,
 
105
 
 
106
    /** Decoding (incoming from network) stream. */
 
107
    PJMEDIA_DIR_DECODING = 2,
 
108
 
 
109
    /** Incoming and outgoing stream. */
 
110
    PJMEDIA_DIR_ENCODING_DECODING = 3
 
111
 
 
112
} pjmedia_dir;
 
113
 
 
114
 
 
115
 
 
116
/* Alternate names for media direction: */
 
117
 
 
118
/**
 
119
 * Direction is capturing audio frames.
 
120
 */
 
121
#define PJMEDIA_DIR_CAPTURE     PJMEDIA_DIR_ENCODING
 
122
 
 
123
/**
 
124
 * Direction is playback of audio frames.
 
125
 */
 
126
#define PJMEDIA_DIR_PLAYBACK    PJMEDIA_DIR_DECODING
 
127
 
 
128
/**
 
129
 * Direction is both capture and playback.
 
130
 */
 
131
#define PJMEDIA_DIR_CAPTURE_PLAYBACK    PJMEDIA_DIR_ENCODING_DECODING
 
132
 
 
133
 
 
134
/**
 
135
 * Create 32bit port signature from ASCII characters.
 
136
 */
 
137
#define PJMEDIA_PORT_SIGNATURE(a,b,c,d)     \
 
138
            (a<<24 | b<<16 | c<<8 | d)
 
139
 
 
140
 
 
141
/**
 
142
 * Opaque declaration of media endpoint.
 
143
 */
 
144
typedef struct pjmedia_endpt pjmedia_endpt;
 
145
 
 
146
 
 
147
/*
 
148
 * Forward declaration for stream (needed by transport).
 
149
 */
 
150
typedef struct pjmedia_stream pjmedia_stream;
 
151
 
 
152
 
 
153
/**
 
154
 * Media socket info is used to describe the underlying sockets
 
155
 * to be used as media transport.
 
156
 */
 
157
typedef struct pjmedia_sock_info
 
158
{
 
159
    /** The RTP socket handle */
 
160
    pj_sock_t       rtp_sock;
 
161
 
 
162
    /** Address to be advertised as the local address for the RTP
 
163
     *  socket, which does not need to be equal as the bound
 
164
     *  address (for example, this address can be the address resolved
 
165
     *  with STUN).
 
166
     */
 
167
    pj_sockaddr     rtp_addr_name;
 
168
 
 
169
    /** The RTCP socket handle. */
 
170
    pj_sock_t       rtcp_sock;
 
171
 
 
172
    /** Address to be advertised as the local address for the RTCP
 
173
     *  socket, which does not need to be equal as the bound
 
174
     *  address (for example, this address can be the address resolved
 
175
     *  with STUN).
 
176
     */
 
177
    pj_sockaddr     rtcp_addr_name;
 
178
 
 
179
} pjmedia_sock_info;
 
180
 
 
181
 
 
182
/**
 
183
 * Macro for packing format.
 
184
 */
 
185
#define PJMEDIA_FORMAT_PACK(C1, C2, C3, C4) ( C4<<24 | C3<<16 | C2<<8 | C1 )
 
186
 
 
187
/**
 
188
 * This enumeration describes format ID. 
 
189
 */
 
190
typedef enum pjmedia_format_id
 
191
{
 
192
    /**
 
193
     * 16bit linear
 
194
     */
 
195
    PJMEDIA_FORMAT_L16      = 0,
 
196
    
 
197
    /**
 
198
     * Alias for PJMEDIA_FORMAT_L16
 
199
     */
 
200
    PJMEDIA_FORMAT_PCM      = PJMEDIA_FORMAT_L16,
 
201
 
 
202
    /**
 
203
     * G.711 ALAW
 
204
     */
 
205
    PJMEDIA_FORMAT_PCMA     = PJMEDIA_FORMAT_PACK('A', 'L', 'A', 'W'),
 
206
 
 
207
    /**
 
208
     * Alias for PJMEDIA_FORMAT_PCMA
 
209
     */
 
210
    PJMEDIA_FORMAT_ALAW     = PJMEDIA_FORMAT_PCMA,
 
211
 
 
212
    /**
 
213
     * G.711 ULAW
 
214
     */
 
215
    PJMEDIA_FORMAT_PCMU     = PJMEDIA_FORMAT_PACK('u', 'L', 'A', 'W'),
 
216
 
 
217
    /**
 
218
     * Aliaw for PJMEDIA_FORMAT_PCMU
 
219
     */
 
220
    PJMEDIA_FORMAT_ULAW     = PJMEDIA_FORMAT_PCMU,
 
221
 
 
222
    /**
 
223
     * AMR narrowband
 
224
     */
 
225
    PJMEDIA_FORMAT_AMR      = PJMEDIA_FORMAT_PACK(' ', 'A', 'M', 'R'),
 
226
 
 
227
    /**
 
228
     * ITU G.729
 
229
     */
 
230
    PJMEDIA_FORMAT_G729     = PJMEDIA_FORMAT_PACK('G', '7', '2', '9'),
 
231
 
 
232
    /**
 
233
     * Internet Low Bit-Rate Codec (ILBC)
 
234
     */
 
235
    PJMEDIA_FORMAT_ILBC     = PJMEDIA_FORMAT_PACK('I', 'L', 'B', 'C')
 
236
 
 
237
} pjmedia_format_id;
 
238
 
 
239
 
 
240
/**
 
241
 * Media format information.
 
242
 */
 
243
typedef struct pjmedia_format
 
244
{
 
245
    /** Format ID */
 
246
    pjmedia_format_id   id;
 
247
 
 
248
    /** Bitrate. */
 
249
    pj_uint32_t         bitrate;
 
250
 
 
251
    /** Flag to indicate whether VAD is enabled */
 
252
    pj_bool_t           vad;
 
253
 
 
254
} pjmedia_format;
 
255
 
 
256
 
 
257
 
 
258
/**
 
259
 * This is a general purpose function set PCM samples to zero.
 
260
 * Since this function is needed by many parts of the library,
 
261
 * by putting this functionality in one place, it enables some.
 
262
 * clever people to optimize this function.
 
263
 *
 
264
 * @param samples       The 16bit PCM samples.
 
265
 * @param count         Number of samples.
 
266
 */
 
267
PJ_INLINE(void) pjmedia_zero_samples(pj_int16_t *samples, unsigned count)
 
268
{
 
269
#if 1
 
270
    pj_bzero(samples, (count<<1));
 
271
#elif 0
 
272
    unsigned i;
 
273
    for (i=0; i<count; ++i) samples[i] = 0;
 
274
#else
 
275
    unsigned i;
 
276
    count >>= 1;
 
277
    for (i=0; i<count; ++i) ((pj_int32_t*)samples)[i] = (pj_int32_t)0;
 
278
#endif
 
279
}
 
280
 
 
281
 
 
282
/**
 
283
 * This is a general purpose function to copy samples from/to buffers with
 
284
 * equal size. Since this function is needed by many parts of the library,
 
285
 * by putting this functionality in one place, it enables some.
 
286
 * clever people to optimize this function.
 
287
 */
 
288
PJ_INLINE(void) pjmedia_copy_samples(pj_int16_t *dst, const pj_int16_t *src,
 
289
                                     unsigned count)
 
290
{
 
291
#if 1
 
292
    pj_memcpy(dst, src, (count<<1));
 
293
#elif 0
 
294
    unsigned i;
 
295
    for (i=0; i<count; ++i) dst[i] = src[i];
 
296
#else
 
297
    unsigned i;
 
298
    count >>= 1;
 
299
    for (i=0; i<count; ++i)
 
300
        ((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i];
 
301
#endif
 
302
}
 
303
 
 
304
 
 
305
/**
 
306
 * This is a general purpose function to copy samples from/to buffers with
 
307
 * equal size. Since this function is needed by many parts of the library,
 
308
 * by putting this functionality in one place, it enables some.
 
309
 * clever people to optimize this function.
 
310
 */
 
311
PJ_INLINE(void) pjmedia_move_samples(pj_int16_t *dst, const pj_int16_t *src,
 
312
                                     unsigned count)
 
313
{
 
314
#if 1
 
315
    pj_memmove(dst, src, (count<<1));
 
316
#elif 0
 
317
    unsigned i;
 
318
    for (i=0; i<count; ++i) dst[i] = src[i];
 
319
#else
 
320
    unsigned i;
 
321
    count >>= 1;
 
322
    for (i=0; i<count; ++i)
 
323
        ((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i];
 
324
#endif
 
325
}
 
326
 
 
327
/** 
 
328
 * Types of media frame. 
 
329
 */
 
330
typedef enum pjmedia_frame_type
 
331
{
 
332
    PJMEDIA_FRAME_TYPE_NONE,        /**< No frame.              */
 
333
    PJMEDIA_FRAME_TYPE_AUDIO,       /**< Normal audio frame.    */
 
334
    PJMEDIA_FRAME_TYPE_EXTENDED     /**< Extended audio frame.  */
 
335
 
 
336
} pjmedia_frame_type;
 
337
 
 
338
 
 
339
/** 
 
340
 * This structure describes a media frame. 
 
341
 */
 
342
typedef struct pjmedia_frame
 
343
{
 
344
    pjmedia_frame_type   type;      /**< Frame type.                        */
 
345
    void                *buf;       /**< Pointer to buffer.                 */
 
346
    pj_size_t            size;      /**< Frame size in bytes.               */
 
347
    pj_timestamp         timestamp; /**< Frame timestamp.                   */
 
348
    pj_uint32_t          bit_info;  /**< Bit info of the frame, sample case:
 
349
                                         a frame may not exactly start and end
 
350
                                         at the octet boundary, so this field 
 
351
                                         may be used for specifying start & 
 
352
                                         end bit offset.                    */
 
353
} pjmedia_frame;
 
354
 
 
355
 
 
356
/**
 
357
 * The pjmedia_frame_ext is used to carry a more complex audio frames than
 
358
 * the typical PCM audio frames, and it is signaled by setting the "type"
 
359
 * field of a pjmedia_frame to PJMEDIA_FRAME_TYPE_EXTENDED. With this set,
 
360
 * application may typecast pjmedia_frame to pjmedia_frame_ext.
 
361
 *
 
362
 * This structure may contain more than one audio frames, which subsequently
 
363
 * will be called subframes in this structure. The subframes section
 
364
 * immediately follows the end of this structure, and each subframe is
 
365
 * represented by pjmedia_frame_ext_subframe structure. Every next
 
366
 * subframe immediately follows the previous subframe, and all subframes
 
367
 * are byte-aligned although its payload may not be byte-aligned.
 
368
 */
 
369
 
 
370
#pragma pack(1)
 
371
typedef struct pjmedia_frame_ext {
 
372
    pjmedia_frame   base;           /**< Base frame info */
 
373
    pj_uint16_t     samples_cnt;    /**< Number of samples in this frame */
 
374
    pj_uint16_t     subframe_cnt;   /**< Number of (sub)frames in this frame */
 
375
 
 
376
    /* Zero or more (sub)frames follows immediately after this,
 
377
     * each will be represented by pjmedia_frame_ext_subframe
 
378
     */
 
379
} pjmedia_frame_ext;
 
380
#pragma pack()
 
381
 
 
382
/**
 
383
 * This structure represents the individual subframes in the
 
384
 * pjmedia_frame_ext structure.
 
385
 */
 
386
#pragma pack(1)
 
387
typedef struct pjmedia_frame_ext_subframe {
 
388
    pj_uint16_t     bitlen;         /**< Number of bits in the data */
 
389
    pj_uint8_t      data[1];        /**< Start of encoded data */
 
390
} pjmedia_frame_ext_subframe;
 
391
 
 
392
#pragma pack()
 
393
 
 
394
 
 
395
/**
 
396
 * Append one subframe to #pjmedia_frame_ext.
 
397
 *
 
398
 * @param frm               The #pjmedia_frame_ext.
 
399
 * @param src               Subframe data.
 
400
 * @param bitlen            Lenght of subframe, in bits.
 
401
 * @param samples_cnt       Number of audio samples in subframe.
 
402
 */
 
403
PJ_INLINE(void) pjmedia_frame_ext_append_subframe(pjmedia_frame_ext *frm,
 
404
                                                  const void *src,
 
405
                                                  unsigned bitlen,
 
406
                                                  unsigned samples_cnt)
 
407
{
 
408
    pjmedia_frame_ext_subframe *fsub;
 
409
    pj_uint8_t *p;
 
410
    unsigned i;
 
411
 
 
412
    p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext);
 
413
    for (i = 0; i < frm->subframe_cnt; ++i) {
 
414
        fsub = (pjmedia_frame_ext_subframe*) p;
 
415
        p += sizeof(fsub->bitlen) + ((fsub->bitlen+7) >> 3);
 
416
    }
 
417
 
 
418
    fsub = (pjmedia_frame_ext_subframe*) p;
 
419
    fsub->bitlen = (pj_uint16_t)bitlen;
 
420
    if (bitlen)
 
421
        pj_memcpy(fsub->data, src, (bitlen+7) >> 3);
 
422
 
 
423
    frm->subframe_cnt++;
 
424
    frm->samples_cnt = (pj_uint16_t)(frm->samples_cnt + samples_cnt);
 
425
}
 
426
 
 
427
/**
 
428
 * Get a subframe from #pjmedia_frame_ext.
 
429
 *
 
430
 * @param frm               The #pjmedia_frame_ext.
 
431
 * @param n                 Subframe index, zero based.
 
432
 *
 
433
 * @return                  The n-th subframe, or NULL if n is out-of-range.
 
434
 */
 
435
PJ_INLINE(pjmedia_frame_ext_subframe*) 
 
436
pjmedia_frame_ext_get_subframe(const pjmedia_frame_ext *frm, unsigned n)
 
437
{
 
438
    pjmedia_frame_ext_subframe *sf = NULL;
 
439
 
 
440
    if (n < frm->subframe_cnt) {
 
441
        pj_uint8_t *p;
 
442
        unsigned i;
 
443
 
 
444
        p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext);
 
445
        for (i = 0; i < n; ++i) {       
 
446
            sf = (pjmedia_frame_ext_subframe*) p;
 
447
            p += sizeof(sf->bitlen) + ((sf->bitlen+7) >> 3);
 
448
        }
 
449
        
 
450
        sf = (pjmedia_frame_ext_subframe*) p;
 
451
    }
 
452
 
 
453
    return sf;
 
454
}
 
455
        
 
456
/**
 
457
 * Extract all frame payload to the specified buffer. 
 
458
 *
 
459
 * @param frm               The frame.
 
460
 * @param dst               Destination buffer.
 
461
 * @param maxlen            Maximum size to copy (i.e. the size of the
 
462
 *                          destination buffer).
 
463
 *
 
464
 * @return                  Total size of payload copied.
 
465
 */
 
466
PJ_INLINE(unsigned) 
 
467
pjmedia_frame_ext_copy_payload(const pjmedia_frame_ext *frm,
 
468
                               void *dst, 
 
469
                               unsigned maxlen)
 
470
{
 
471
    unsigned i, copied=0;
 
472
    for (i=0; i<frm->subframe_cnt; ++i) {
 
473
        pjmedia_frame_ext_subframe *sf;
 
474
        unsigned sz;
 
475
 
 
476
        sf = pjmedia_frame_ext_get_subframe(frm, i);
 
477
        if (!sf)
 
478
            continue;
 
479
 
 
480
        sz = ((sf->bitlen + 7) >> 3);
 
481
        if (sz + copied > maxlen)
 
482
            break;
 
483
 
 
484
        pj_memcpy(((pj_uint8_t*)dst) + copied, sf->data, sz);
 
485
        copied += sz;
 
486
    }
 
487
    return copied;
 
488
}
 
489
 
 
490
 
 
491
/**
 
492
 * Pop out first n subframes from #pjmedia_frame_ext.
 
493
 *
 
494
 * @param frm               The #pjmedia_frame_ext.
 
495
 * @param n                 Number of first subframes to be popped out.
 
496
 *
 
497
 * @return                  PJ_SUCCESS when successful.
 
498
 */
 
499
PJ_INLINE(pj_status_t) 
 
500
pjmedia_frame_ext_pop_subframes(pjmedia_frame_ext *frm, unsigned n)
 
501
{
 
502
    pjmedia_frame_ext_subframe *sf;
 
503
    pj_uint8_t *move_src;
 
504
    unsigned move_len;
 
505
 
 
506
    if (frm->subframe_cnt <= n) {
 
507
        frm->subframe_cnt = 0;
 
508
        frm->samples_cnt = 0;
 
509
        return PJ_SUCCESS;
 
510
    }
 
511
 
 
512
    move_src = (pj_uint8_t*)pjmedia_frame_ext_get_subframe(frm, n);
 
513
    sf = pjmedia_frame_ext_get_subframe(frm, frm->subframe_cnt-1);
 
514
    move_len = (pj_uint8_t*)sf - move_src + sizeof(sf->bitlen) + 
 
515
               ((sf->bitlen+7) >> 3);
 
516
    pj_memmove((pj_uint8_t*)frm+sizeof(pjmedia_frame_ext), 
 
517
               move_src, move_len);
 
518
            
 
519
    frm->samples_cnt = (pj_uint16_t)
 
520
                   (frm->samples_cnt - n*frm->samples_cnt/frm->subframe_cnt);
 
521
    frm->subframe_cnt = (pj_uint16_t) (frm->subframe_cnt - n);
 
522
 
 
523
    return PJ_SUCCESS;
 
524
}
 
525
 
 
526
 
 
527
/**
 
528
 * @}
 
529
 */
 
530
 
 
531
 
 
532
#endif  /* __PJMEDIA_TYPES_H__ */
 
533