add passthrough and file format support for G.722 16KHz audio (issue #5084, original...
[asterisk-bristuff.git] / include / asterisk / frame.h
blobfa1c1289128b926d767419ae1233498576664403
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
20 * \brief Asterisk internal frame definitions.
21 * \arg For an explanation of frames, see \ref Def_Frame
22 * \arg Frames are send of Asterisk channels, see \ref Def_Channel
25 #ifndef _ASTERISK_FRAME_H
26 #define _ASTERISK_FRAME_H
28 #if defined(__cplusplus) || defined(c_plusplus)
29 extern "C" {
30 #endif
32 #include <sys/types.h>
33 #include <sys/time.h>
35 #include "asterisk/compiler.h"
36 #include "asterisk/endian.h"
37 #include "asterisk/linkedlists.h"
39 struct ast_codec_pref {
40 char order[32];
41 char framing[32];
44 /*! \page Def_Frame AST Multimedia and signalling frames
45 \section Def_AstFrame What is an ast_frame ?
46 A frame of data read used to communicate between
47 between channels and applications.
48 Frames are divided into frame types and subclasses.
50 \par Frame types
51 \arg \b VOICE: Voice data, subclass is codec (AST_FORMAT_*)
52 \arg \b VIDEO: Video data, subclass is codec (AST_FORMAT_*)
53 \arg \b DTMF: A DTMF digit, subclass is the digit
54 \arg \b IMAGE: Image transport, mostly used in IAX
55 \arg \b TEXT: Text messages
56 \arg \b HTML: URL's and web pages
57 \arg \b MODEM: Modulated data encodings, such as T.38 and V.150
58 \arg \b IAX: Private frame type for the IAX protocol
59 \arg \b CNG: Comfort noice frames
60 \arg \b CONTROL: A control frame, subclass defined as AST_CONTROL_
61 \arg \b NULL: Empty, useless frame
63 \par Files
64 \arg frame.h Definitions
65 \arg frame.c Function library
66 \arg \ref Def_Channel Asterisk channels
67 \section Def_ControlFrame Control Frames
68 Control frames send signalling information between channels
69 and devices. They are prefixed with AST_CONTROL_, like AST_CONTROL_FRAME_HANGUP
70 \arg \b HANGUP The other end has hungup
71 \arg \b RING Local ring
72 \arg \b RINGING The other end is ringing
73 \arg \b ANSWER The other end has answered
74 \arg \b BUSY Remote end is busy
75 \arg \b TAKEOFFHOOK Make it go off hook (what's "it" ? )
76 \arg \b OFFHOOK Line is off hook
77 \arg \b CONGESTION Congestion (circuit is busy, not available)
78 \arg \b FLASH Other end sends flash hook
79 \arg \b WINK Other end sends wink
80 \arg \b OPTION Send low-level option
81 \arg \b RADIO_KEY Key radio (see app_rpt.c)
82 \arg \b RADIO_UNKEY Un-key radio (see app_rpt.c)
83 \arg \b PROGRESS Other end indicates call progress
84 \arg \b PROCEEDING Indicates proceeding
85 \arg \b HOLD Call is placed on hold
86 \arg \b UNHOLD Call is back from hold
87 \arg \b VIDUPDATE Video update requested
91 /*!
92 * \brief Frame types
94 * \note It is important that the values of each frame type are never changed,
95 * because it will break backwards compatability with older versions.
97 enum ast_frame_type {
98 /*! DTMF end event, subclass is the digit */
99 AST_FRAME_DTMF_END = 1,
100 /*! Voice data, subclass is AST_FORMAT_* */
101 AST_FRAME_VOICE,
102 /*! Video frame, maybe?? :) */
103 AST_FRAME_VIDEO,
104 /*! A control frame, subclass is AST_CONTROL_* */
105 AST_FRAME_CONTROL,
106 /*! An empty, useless frame */
107 AST_FRAME_NULL,
108 /*! Inter Asterisk Exchange private frame type */
109 AST_FRAME_IAX,
110 /*! Text messages */
111 AST_FRAME_TEXT,
112 /*! Image Frames */
113 AST_FRAME_IMAGE,
114 /*! HTML Frame */
115 AST_FRAME_HTML,
116 /*! Comfort Noise frame (subclass is level of CNG in -dBov),
117 body may include zero or more 8-bit quantization coefficients */
118 AST_FRAME_CNG,
119 /*! Modem-over-IP data streams */
120 AST_FRAME_MODEM,
121 /*! DTMF begin event, subclass is the digit */
122 AST_FRAME_DTMF_BEGIN,
124 #define AST_FRAME_DTMF AST_FRAME_DTMF_END
126 /*! \brief Data structure associated with a single frame of data
128 struct ast_frame {
129 /*! Kind of frame */
130 enum ast_frame_type frametype;
131 /*! Subclass, frame dependent */
132 int subclass;
133 /*! Length of data */
134 int datalen;
135 /*! Number of 8khz samples in this frame */
136 int samples;
137 /*! Was the data malloc'd? i.e. should we free it when we discard the frame? */
138 int mallocd;
139 /*! The number of bytes allocated for a malloc'd frame header */
140 size_t mallocd_hdr_len;
141 /*! How many bytes exist _before_ "data" that can be used if needed */
142 int offset;
143 /*! Optional source of frame for debugging */
144 const char *src;
145 /*! Pointer to actual data */
146 void *data;
147 /*! Global delivery time */
148 struct timeval delivery;
149 /*! For placing in a linked list */
150 AST_LIST_ENTRY(ast_frame) frame_list;
151 /*! Timing data flag */
152 int has_timing_info;
153 /*! Timestamp in milliseconds */
154 long ts;
155 /*! Length in milliseconds */
156 long len;
157 /*! Sequence number */
158 int seqno;
162 * Set the various field of a frame to point to a buffer.
163 * Typically you set the base address of the buffer, the offset as
164 * AST_FRIENDLY_OFFSET, and the datalen as the amount of bytes queued.
165 * The remaining things (to be done manually) is set the number of
166 * samples, which cannot be derived from the datalen unless you know
167 * the number of bits per sample.
169 #define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen) \
171 (fr)->data = (char *)_base + (_ofs); \
172 (fr)->offset = (_ofs); \
173 (fr)->datalen = (_datalen); \
176 /*! Queueing a null frame is fairly common, so we declare a global null frame object
177 for this purpose instead of having to declare one on the stack */
178 extern struct ast_frame ast_null_frame;
180 #define AST_FRIENDLY_OFFSET 64 /*! It's polite for a a new frame to
181 have this number of bytes for additional
182 headers. */
183 #define AST_MIN_OFFSET 32 /*! Make sure we keep at least this much handy */
185 /*! Need the header be free'd? */
186 #define AST_MALLOCD_HDR (1 << 0)
187 /*! Need the data be free'd? */
188 #define AST_MALLOCD_DATA (1 << 1)
189 /*! Need the source be free'd? (haha!) */
190 #define AST_MALLOCD_SRC (1 << 2)
192 /* MODEM subclasses */
193 /*! T.38 Fax-over-IP */
194 #define AST_MODEM_T38 1
195 /*! V.150 Modem-over-IP */
196 #define AST_MODEM_V150 2
198 /* HTML subclasses */
199 /*! Sending a URL */
200 #define AST_HTML_URL 1
201 /*! Data frame */
202 #define AST_HTML_DATA 2
203 /*! Beginning frame */
204 #define AST_HTML_BEGIN 4
205 /*! End frame */
206 #define AST_HTML_END 8
207 /*! Load is complete */
208 #define AST_HTML_LDCOMPLETE 16
209 /*! Peer is unable to support HTML */
210 #define AST_HTML_NOSUPPORT 17
211 /*! Send URL, and track */
212 #define AST_HTML_LINKURL 18
213 /*! No more HTML linkage */
214 #define AST_HTML_UNLINK 19
215 /*! Reject link request */
216 #define AST_HTML_LINKREJECT 20
218 /* Data formats for capabilities and frames alike */
219 /*! G.723.1 compression */
220 #define AST_FORMAT_G723_1 (1 << 0)
221 /*! GSM compression */
222 #define AST_FORMAT_GSM (1 << 1)
223 /*! Raw mu-law data (G.711) */
224 #define AST_FORMAT_ULAW (1 << 2)
225 /*! Raw A-law data (G.711) */
226 #define AST_FORMAT_ALAW (1 << 3)
227 /*! ADPCM (G.726, 32kbps, AAL2 codeword packing) */
228 #define AST_FORMAT_G726_AAL2 (1 << 4)
229 /*! ADPCM (IMA) */
230 #define AST_FORMAT_ADPCM (1 << 5)
231 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
232 #define AST_FORMAT_SLINEAR (1 << 6)
233 /*! LPC10, 180 samples/frame */
234 #define AST_FORMAT_LPC10 (1 << 7)
235 /*! G.729A audio */
236 #define AST_FORMAT_G729A (1 << 8)
237 /*! SpeeX Free Compression */
238 #define AST_FORMAT_SPEEX (1 << 9)
239 /*! iLBC Free Compression */
240 #define AST_FORMAT_ILBC (1 << 10)
241 /*! ADPCM (G.726, 32kbps, RFC3551 codeword packing) */
242 #define AST_FORMAT_G726 (1 << 11)
243 /*! G.722 */
244 #define AST_FORMAT_G722 (1 << 12)
245 /*! Maximum audio format */
246 #define AST_FORMAT_MAX_AUDIO (1 << 15)
247 /*! Maximum audio mask */
248 #define AST_FORMAT_AUDIO_MASK ((1 << 16)-1)
249 /*! JPEG Images */
250 #define AST_FORMAT_JPEG (1 << 16)
251 /*! PNG Images */
252 #define AST_FORMAT_PNG (1 << 17)
253 /*! H.261 Video */
254 #define AST_FORMAT_H261 (1 << 18)
255 /*! H.263 Video */
256 #define AST_FORMAT_H263 (1 << 19)
257 /*! H.263+ Video */
258 #define AST_FORMAT_H263_PLUS (1 << 20)
259 /*! H.264 Video */
260 #define AST_FORMAT_H264 (1 << 21)
261 /*! Maximum video format */
262 #define AST_FORMAT_MAX_VIDEO (1 << 24)
263 #define AST_FORMAT_VIDEO_MASK (((1 << 25)-1) & ~(AST_FORMAT_AUDIO_MASK))
265 enum ast_control_frame_type {
266 AST_CONTROL_HANGUP = 1, /*!< Other end has hungup */
267 AST_CONTROL_RING = 2, /*!< Local ring */
268 AST_CONTROL_RINGING = 3, /*!< Remote end is ringing */
269 AST_CONTROL_ANSWER = 4, /*!< Remote end has answered */
270 AST_CONTROL_BUSY = 5, /*!< Remote end is busy */
271 AST_CONTROL_TAKEOFFHOOK = 6, /*!< Make it go off hook */
272 AST_CONTROL_OFFHOOK = 7, /*!< Line is off hook */
273 AST_CONTROL_CONGESTION = 8, /*!< Congestion (circuits busy) */
274 AST_CONTROL_FLASH = 9, /*!< Flash hook */
275 AST_CONTROL_WINK = 10, /*!< Wink */
276 AST_CONTROL_OPTION = 11, /*!< Set a low-level option */
277 AST_CONTROL_RADIO_KEY = 12, /*!< Key Radio */
278 AST_CONTROL_RADIO_UNKEY = 13, /*!< Un-Key Radio */
279 AST_CONTROL_PROGRESS = 14, /*!< Indicate PROGRESS */
280 AST_CONTROL_PROCEEDING = 15, /*!< Indicate CALL PROCEEDING */
281 AST_CONTROL_HOLD = 16, /*!< Indicate call is placed on hold */
282 AST_CONTROL_UNHOLD = 17, /*!< Indicate call is left from hold */
283 AST_CONTROL_VIDUPDATE = 18, /*!< Indicate video frame update */
286 #define AST_SMOOTHER_FLAG_G729 (1 << 0)
287 #define AST_SMOOTHER_FLAG_BE (1 << 1)
289 /* Option identifiers and flags */
290 #define AST_OPTION_FLAG_REQUEST 0
291 #define AST_OPTION_FLAG_ACCEPT 1
292 #define AST_OPTION_FLAG_REJECT 2
293 #define AST_OPTION_FLAG_QUERY 4
294 #define AST_OPTION_FLAG_ANSWER 5
295 #define AST_OPTION_FLAG_WTF 6
297 /*! Verify touchtones by muting audio transmission
298 (and reception) and verify the tone is still present */
299 #define AST_OPTION_TONE_VERIFY 1
301 /*! Put a compatible channel into TDD (TTY for the hearing-impared) mode */
302 #define AST_OPTION_TDD 2
304 /*! Relax the parameters for DTMF reception (mainly for radio use) */
305 #define AST_OPTION_RELAXDTMF 3
307 /*! Set (or clear) Audio (Not-Clear) Mode */
308 #define AST_OPTION_AUDIO_MODE 4
310 /*! Set channel transmit gain
311 * Option data is a single signed char
312 representing number of decibels (dB)
313 to set gain to (on top of any gain
314 specified in channel driver)
316 #define AST_OPTION_TXGAIN 5
318 /*! Set channel receive gain
319 * Option data is a single signed char
320 representing number of decibels (dB)
321 to set gain to (on top of any gain
322 specified in channel driver)
324 #define AST_OPTION_RXGAIN 6
326 /* set channel into "Operator Services" mode */
327 #define AST_OPTION_OPRMODE 7
329 /*! Explicitly enable or disable echo cancelation for the given channel */
330 #define AST_OPTION_ECHOCAN 8
332 struct oprmode {
333 struct ast_channel *peer;
334 int mode;
337 struct ast_option_header {
338 /* Always keep in network byte order */
339 #if __BYTE_ORDER == __BIG_ENDIAN
340 uint16_t flag:3;
341 uint16_t option:13;
342 #else
343 #if __BYTE_ORDER == __LITTLE_ENDIAN
344 uint16_t option:13;
345 uint16_t flag:3;
346 #else
347 #error Byte order not defined
348 #endif
349 #endif
350 uint8_t data[0];
354 /*! \brief Definition of supported media formats (codecs) */
355 struct ast_format_list {
356 int visible; /*!< Can we see this entry */
357 int bits; /*!< bitmask value */
358 char *name; /*!< short name */
359 char *desc; /*!< Description */
360 int fr_len; /*!< Single frame length in bytes */
361 int min_ms; /*!< Min value */
362 int max_ms; /*!< Max value */
363 int inc_ms; /*!< Increment */
364 int def_ms; /*!< Default value */
365 unsigned int flags; /*!< Smoother flags */
366 int cur_ms; /*!< Current value */
370 /*! \brief Requests a frame to be allocated
372 * \param source
373 * Request a frame be allocated. source is an optional source of the frame,
374 * len is the requested length, or "0" if the caller will supply the buffer
376 #if 0 /* Unimplemented */
377 struct ast_frame *ast_fralloc(char *source, int len);
378 #endif
380 /*!
381 * \brief Frees a frame
383 * \param fr Frame to free
384 * \param cache Whether to consider this frame for frame caching
386 void ast_frame_free(struct ast_frame *fr, int cache);
388 static void force_inline ast_frfree(struct ast_frame *fr)
390 ast_frame_free(fr, 1);
393 /*! \brief Makes a frame independent of any static storage
394 * \param fr frame to act upon
395 * Take a frame, and if it's not been malloc'd, make a malloc'd copy
396 * and if the data hasn't been malloced then make the
397 * data malloc'd. If you need to store frames, say for queueing, then
398 * you should call this function.
399 * \return Returns a frame on success, NULL on error
401 struct ast_frame *ast_frisolate(struct ast_frame *fr);
403 /*! \brief Copies a frame
404 * \param fr frame to copy
405 * Duplicates a frame -- should only rarely be used, typically frisolate is good enough
406 * \return Returns a frame on success, NULL on error
408 struct ast_frame *ast_frdup(const struct ast_frame *fr);
410 void ast_swapcopy_samples(void *dst, const void *src, int samples);
412 /* Helpers for byteswapping native samples to/from
413 little-endian and big-endian. */
414 #if __BYTE_ORDER == __LITTLE_ENDIAN
415 #define ast_frame_byteswap_le(fr) do { ; } while(0)
416 #define ast_frame_byteswap_be(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0)
417 #else
418 #define ast_frame_byteswap_le(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0)
419 #define ast_frame_byteswap_be(fr) do { ; } while(0)
420 #endif
423 /*! \brief Get the name of a format
424 * \param format id of format
425 * \return A static string containing the name of the format or "UNKN" if unknown.
427 char* ast_getformatname(int format);
429 /*! \brief Get the names of a set of formats
430 * \param buf a buffer for the output string
431 * \param size size of buf (bytes)
432 * \param format the format (combined IDs of codecs)
433 * Prints a list of readable codec names corresponding to "format".
434 * ex: for format=AST_FORMAT_GSM|AST_FORMAT_SPEEX|AST_FORMAT_ILBC it will return "0x602 (GSM|SPEEX|ILBC)"
435 * \return The return value is buf.
437 char* ast_getformatname_multiple(char *buf, size_t size, int format);
440 * \brief Gets a format from a name.
441 * \param name string of format
442 * \return This returns the form of the format in binary on success, 0 on error.
444 int ast_getformatbyname(const char *name);
446 /*! \brief Get a name from a format
447 * Gets a name from a format
448 * \param codec codec number (1,2,4,8,16,etc.)
449 * \return This returns a static string identifying the format on success, 0 on error.
451 char *ast_codec2str(int codec);
453 struct ast_smoother;
455 struct ast_format_list *ast_get_format_list_index(int index);
456 struct ast_format_list *ast_get_format_list(size_t *size);
457 struct ast_smoother *ast_smoother_new(int bytes);
458 void ast_smoother_set_flags(struct ast_smoother *smoother, int flags);
459 int ast_smoother_get_flags(struct ast_smoother *smoother);
460 int ast_smoother_test_flag(struct ast_smoother *s, int flag);
461 void ast_smoother_free(struct ast_smoother *s);
462 void ast_smoother_reset(struct ast_smoother *s, int bytes);
463 int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap);
464 struct ast_frame *ast_smoother_read(struct ast_smoother *s);
465 #define ast_smoother_feed(s,f) __ast_smoother_feed(s, f, 0)
466 #if __BYTE_ORDER == __LITTLE_ENDIAN
467 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 1)
468 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 0)
469 #else
470 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 0)
471 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 1)
472 #endif
474 void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix);
476 /*! \par AudioCodecPref Audio Codec Preferences
477 In order to negotiate audio codecs in the order they are configured
478 in <channel>.conf for a device, we set up codec preference lists
479 in addition to the codec capabilities setting. The capabilities
480 setting is a bitmask of audio and video codecs with no internal
481 order. This will reflect the offer given to the other side, where
482 the prefered codecs will be added to the top of the list in the
483 order indicated by the "allow" lines in the device configuration.
485 Video codecs are not included in the preference lists since they
486 can't be transcoded and we just have to pick whatever is supported
489 /*! \brief Initialize an audio codec preference to "no preference" See \ref AudioCodecPref */
490 void ast_codec_pref_init(struct ast_codec_pref *pref);
492 /*! \brief Codec located at a particular place in the preference index See \ref AudioCodecPref */
493 int ast_codec_pref_index(struct ast_codec_pref *pref, int index);
495 /*! \brief Remove audio a codec from a preference list */
496 void ast_codec_pref_remove(struct ast_codec_pref *pref, int format);
498 /*! \brief Append a audio codec to a preference list, removing it first if it was already there
500 int ast_codec_pref_append(struct ast_codec_pref *pref, int format);
502 /*! \brief Select the best audio format according to preference list from supplied options.
503 If "find_best" is non-zero then if nothing is found, the "Best" format of
504 the format list is selected, otherwise 0 is returned. */
505 int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best);
507 /*! \brief Set packet size for codec
509 int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems);
511 /*! \brief Get packet size for codec
513 struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int format);
515 /*! \brief Parse an "allow" or "deny" line in a channel or device configuration
516 and update the capabilities mask and pref if provided.
517 Video codecs are not added to codec preference lists, since we can not transcode
519 void ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing);
521 /*! \brief Dump audio codec preference list into a string */
522 int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size);
524 /*! \brief Shift an audio codec preference list up or down 65 bytes so that it becomes an ASCII string */
525 void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right);
527 /*! \brief Returns the number of samples contained in the frame */
528 int ast_codec_get_samples(struct ast_frame *f);
530 /*! \brief Returns the number of bytes for the number of samples of the given format */
531 int ast_codec_get_len(int format, int samples);
533 /*! \brief Appends a frame to the end of a list of frames, truncating the maximum length of the list */
534 struct ast_frame *ast_frame_enqueue(struct ast_frame *head, struct ast_frame *f, int maxlen, int dupe);
537 /*! \brief Gets duration in ms of interpolation frame for a format */
538 static inline int ast_codec_interp_len(int format)
540 return (format == AST_FORMAT_ILBC) ? 30 : 20;
544 \brief Adjusts the volume of the audio samples contained in a frame.
545 \param f The frame containing the samples (must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR)
546 \param adjustment The number of dB to adjust up or down.
547 \return 0 for success, non-zero for an error
549 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment);
552 \brief Sums two frames of audio samples.
553 \param f1 The first frame (which will contain the result)
554 \param f2 The second frame
555 \return 0 for success, non-zero for an error
557 The frames must be AST_FRAME_VOICE and must contain AST_FORMAT_SLINEAR samples,
558 and must contain the same number of samples.
560 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2);
562 #if defined(__cplusplus) || defined(c_plusplus)
564 #endif
566 #endif /* _ASTERISK_FRAME_H */