1
/* $Id: types.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#ifndef __PJMEDIA_TYPES_H__
21
#define __PJMEDIA_TYPES_H__
24
* @file pjmedia/types.h Basic Types
25
* @brief Basic PJMEDIA types.
28
#include <pjmedia/config.h>
29
#include <pj/sock.h> /* pjmedia_sock_info */
30
#include <pj/string.h> /* pj_memcpy(), pj_memset() */
33
* @defgroup PJMEDIA_PORT Media Ports Framework
34
* @brief Extensible framework for media terminations
39
* @defgroup PJMEDIA_FRAME_OP Audio Manipulation Algorithms
40
* @brief Algorithms to manipulate audio frames
44
* @defgroup PJMEDIA_TYPES Basic Types
45
* @ingroup PJMEDIA_BASE
46
* @brief Basic PJMEDIA types and operations.
51
* Top most media type.
53
typedef enum pjmedia_type
56
PJMEDIA_TYPE_NONE = 0,
58
/** The media is audio */
59
PJMEDIA_TYPE_AUDIO = 1,
61
/** The media is video. */
62
PJMEDIA_TYPE_VIDEO = 2,
64
/** Unknown media type, in this case the name will be specified in
67
PJMEDIA_TYPE_UNKNOWN = 3,
69
/** The media is application. */
70
PJMEDIA_TYPE_APPLICATION = 4
76
* Media transport protocol.
78
typedef enum pjmedia_tp_proto
80
/** No transport type */
81
PJMEDIA_TP_PROTO_NONE = 0,
83
/** RTP using A/V profile */
84
PJMEDIA_TP_PROTO_RTP_AVP,
87
PJMEDIA_TP_PROTO_RTP_SAVP,
90
PJMEDIA_TP_PROTO_UNKNOWN
98
typedef enum pjmedia_dir
101
PJMEDIA_DIR_NONE = 0,
103
/** Encoding (outgoing to network) stream */
104
PJMEDIA_DIR_ENCODING = 1,
106
/** Decoding (incoming from network) stream. */
107
PJMEDIA_DIR_DECODING = 2,
109
/** Incoming and outgoing stream. */
110
PJMEDIA_DIR_ENCODING_DECODING = 3
116
/* Alternate names for media direction: */
119
* Direction is capturing audio frames.
121
#define PJMEDIA_DIR_CAPTURE PJMEDIA_DIR_ENCODING
124
* Direction is playback of audio frames.
126
#define PJMEDIA_DIR_PLAYBACK PJMEDIA_DIR_DECODING
129
* Direction is both capture and playback.
131
#define PJMEDIA_DIR_CAPTURE_PLAYBACK PJMEDIA_DIR_ENCODING_DECODING
135
* Create 32bit port signature from ASCII characters.
137
#define PJMEDIA_PORT_SIGNATURE(a,b,c,d) \
138
(a<<24 | b<<16 | c<<8 | d)
142
* Opaque declaration of media endpoint.
144
typedef struct pjmedia_endpt pjmedia_endpt;
148
* Forward declaration for stream (needed by transport).
150
typedef struct pjmedia_stream pjmedia_stream;
154
* Media socket info is used to describe the underlying sockets
155
* to be used as media transport.
157
typedef struct pjmedia_sock_info
159
/** The RTP socket handle */
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
167
pj_sockaddr rtp_addr_name;
169
/** The RTCP socket handle. */
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
177
pj_sockaddr rtcp_addr_name;
183
* Macro for packing format.
185
#define PJMEDIA_FORMAT_PACK(C1, C2, C3, C4) ( C4<<24 | C3<<16 | C2<<8 | C1 )
188
* This enumeration describes format ID.
190
typedef enum pjmedia_format_id
195
PJMEDIA_FORMAT_L16 = 0,
198
* Alias for PJMEDIA_FORMAT_L16
200
PJMEDIA_FORMAT_PCM = PJMEDIA_FORMAT_L16,
205
PJMEDIA_FORMAT_PCMA = PJMEDIA_FORMAT_PACK('A', 'L', 'A', 'W'),
208
* Alias for PJMEDIA_FORMAT_PCMA
210
PJMEDIA_FORMAT_ALAW = PJMEDIA_FORMAT_PCMA,
215
PJMEDIA_FORMAT_PCMU = PJMEDIA_FORMAT_PACK('u', 'L', 'A', 'W'),
218
* Aliaw for PJMEDIA_FORMAT_PCMU
220
PJMEDIA_FORMAT_ULAW = PJMEDIA_FORMAT_PCMU,
225
PJMEDIA_FORMAT_AMR = PJMEDIA_FORMAT_PACK(' ', 'A', 'M', 'R'),
230
PJMEDIA_FORMAT_G729 = PJMEDIA_FORMAT_PACK('G', '7', '2', '9'),
233
* Internet Low Bit-Rate Codec (ILBC)
235
PJMEDIA_FORMAT_ILBC = PJMEDIA_FORMAT_PACK('I', 'L', 'B', 'C')
241
* Media format information.
243
typedef struct pjmedia_format
246
pjmedia_format_id id;
251
/** Flag to indicate whether VAD is enabled */
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.
264
* @param samples The 16bit PCM samples.
265
* @param count Number of samples.
267
PJ_INLINE(void) pjmedia_zero_samples(pj_int16_t *samples, unsigned count)
270
pj_bzero(samples, (count<<1));
273
for (i=0; i<count; ++i) samples[i] = 0;
277
for (i=0; i<count; ++i) ((pj_int32_t*)samples)[i] = (pj_int32_t)0;
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.
288
PJ_INLINE(void) pjmedia_copy_samples(pj_int16_t *dst, const pj_int16_t *src,
292
pj_memcpy(dst, src, (count<<1));
295
for (i=0; i<count; ++i) dst[i] = src[i];
299
for (i=0; i<count; ++i)
300
((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i];
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.
311
PJ_INLINE(void) pjmedia_move_samples(pj_int16_t *dst, const pj_int16_t *src,
315
pj_memmove(dst, src, (count<<1));
318
for (i=0; i<count; ++i) dst[i] = src[i];
322
for (i=0; i<count; ++i)
323
((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i];
328
* Types of media frame.
330
typedef enum pjmedia_frame_type
332
PJMEDIA_FRAME_TYPE_NONE, /**< No frame. */
333
PJMEDIA_FRAME_TYPE_AUDIO, /**< Normal audio frame. */
334
PJMEDIA_FRAME_TYPE_EXTENDED /**< Extended audio frame. */
336
} pjmedia_frame_type;
340
* This structure describes a media frame.
342
typedef struct pjmedia_frame
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 &
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.
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.
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 */
376
/* Zero or more (sub)frames follows immediately after this,
377
* each will be represented by pjmedia_frame_ext_subframe
383
* This structure represents the individual subframes in the
384
* pjmedia_frame_ext structure.
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;
396
* Append one subframe to #pjmedia_frame_ext.
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.
403
PJ_INLINE(void) pjmedia_frame_ext_append_subframe(pjmedia_frame_ext *frm,
406
unsigned samples_cnt)
408
pjmedia_frame_ext_subframe *fsub;
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);
418
fsub = (pjmedia_frame_ext_subframe*) p;
419
fsub->bitlen = (pj_uint16_t)bitlen;
421
pj_memcpy(fsub->data, src, (bitlen+7) >> 3);
424
frm->samples_cnt = (pj_uint16_t)(frm->samples_cnt + samples_cnt);
428
* Get a subframe from #pjmedia_frame_ext.
430
* @param frm The #pjmedia_frame_ext.
431
* @param n Subframe index, zero based.
433
* @return The n-th subframe, or NULL if n is out-of-range.
435
PJ_INLINE(pjmedia_frame_ext_subframe*)
436
pjmedia_frame_ext_get_subframe(const pjmedia_frame_ext *frm, unsigned n)
438
pjmedia_frame_ext_subframe *sf = NULL;
440
if (n < frm->subframe_cnt) {
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);
450
sf = (pjmedia_frame_ext_subframe*) p;
457
* Extract all frame payload to the specified buffer.
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).
464
* @return Total size of payload copied.
467
pjmedia_frame_ext_copy_payload(const pjmedia_frame_ext *frm,
471
unsigned i, copied=0;
472
for (i=0; i<frm->subframe_cnt; ++i) {
473
pjmedia_frame_ext_subframe *sf;
476
sf = pjmedia_frame_ext_get_subframe(frm, i);
480
sz = ((sf->bitlen + 7) >> 3);
481
if (sz + copied > maxlen)
484
pj_memcpy(((pj_uint8_t*)dst) + copied, sf->data, sz);
492
* Pop out first n subframes from #pjmedia_frame_ext.
494
* @param frm The #pjmedia_frame_ext.
495
* @param n Number of first subframes to be popped out.
497
* @return PJ_SUCCESS when successful.
499
PJ_INLINE(pj_status_t)
500
pjmedia_frame_ext_pop_subframes(pjmedia_frame_ext *frm, unsigned n)
502
pjmedia_frame_ext_subframe *sf;
503
pj_uint8_t *move_src;
506
if (frm->subframe_cnt <= n) {
507
frm->subframe_cnt = 0;
508
frm->samples_cnt = 0;
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),
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);
532
#endif /* __PJMEDIA_TYPES_H__ */