PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/daemon/libs/pjproject/pjmedia/include/pjmedia/types.h

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