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.
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)
32 #include <sys/types.h>
34 #include "asterisk/endian.h"
35 #include "asterisk/linkedlists.h"
37 struct ast_codec_pref {
41 /*! \page Def_Frame AST Multimedia and signalling frames
42 \section Def_AstFrame What is an ast_frame ?
43 A frame of data read used to communicate between
44 between channels and applications.
45 Frames are divided into frame types and subclasses.
48 \arg \b VOICE: Voice data, subclass is codec (AST_FORMAT_*)
49 \arg \b VIDEO: Video data, subclass is codec (AST_FORMAT_*)
50 \arg \b DTMF: A DTMF digit, subclass is the digit
51 \arg \b IMAGE: Image transport, mostly used in IAX
52 \arg \b TEXT: Text messages
53 \arg \b HTML: URL's and web pages
54 \arg \b MODEM: Modulated data encodings, such as T.38 and V.150
55 \arg \b IAX: Private frame type for the IAX protocol
56 \arg \b CNG: Comfort noice frames
57 \arg \b CONTROL: A control frame, subclass defined as AST_CONTROL_
58 \arg \b NULL: Empty, useless frame
61 \arg frame.h Definitions
62 \arg frame.c Function library
63 \arg \ref Def_Channel Asterisk channels
64 \section Def_ControlFrame Control Frames
65 Control frames send signalling information between channels
66 and devices. They are prefixed with AST_CONTROL_, like AST_CONTROL_FRAME_HANGUP
67 \arg \b HANGUP The other end has hungup
68 \arg \b RING Local ring
69 \arg \b RINGING The other end is ringing
70 \arg \b ANSWER The other end has answered
71 \arg \b BUSY Remote end is busy
72 \arg \b TAKEOFFHOOK Make it go off hook (what's "it" ? )
73 \arg \b OFFHOOK Line is off hook
74 \arg \b CONGESTION Congestion (circuit is busy, not available)
75 \arg \b FLASH Other end sends flash hook
76 \arg \b WINK Other end sends wink
77 \arg \b OPTION Send low-level option
78 \arg \b RADIO_KEY Key radio (see app_rpt.c)
79 \arg \b RADIO_UNKEY Un-key radio (see app_rpt.c)
80 \arg \b PROGRESS Other end indicates call progress
81 \arg \b PROCEEDING Indicates proceeding
82 \arg \b HOLD Call is placed on hold
83 \arg \b UNHOLD Call is back from hold
84 \arg \b VIDUPDATE Video update requested
91 * \note It is important that the values of each frame type are never changed,
92 * because it will break backwards compatability with older versions.
95 /*! DTMF end event, subclass is the digit */
96 AST_FRAME_DTMF_END = 1,
97 /*! Voice data, subclass is AST_FORMAT_* */
99 /*! Video frame, maybe?? :) */
101 /*! A control frame, subclass is AST_CONTROL_* */
103 /*! An empty, useless frame */
105 /*! Inter Asterisk Exchange private frame type */
113 /*! Comfort Noise frame (subclass is level of CNG in -dBov),
114 body may include zero or more 8-bit quantization coefficients */
116 /*! Modem-over-IP data streams */
118 /*! DTMF begin event, subclass is the digit */
119 AST_FRAME_DTMF_BEGIN,
121 #define AST_FRAME_DTMF AST_FRAME_DTMF_END
123 /*! \brief Data structure associated with a single frame of data
127 enum ast_frame_type frametype;
128 /*! Subclass, frame dependent */
130 /*! Length of data */
132 /*! Number of 8khz samples in this frame */
134 /*! Was the data malloc'd? i.e. should we free it when we discard the frame? */
136 /*! The number of bytes allocated for a malloc'd frame header */
137 size_t mallocd_hdr_len;
138 /*! How many bytes exist _before_ "data" that can be used if needed */
140 /*! Optional source of frame for debugging */
142 /*! Pointer to actual data */
144 /*! Global delivery time */
145 struct timeval delivery;
146 /*! For placing in a linked list */
147 AST_LIST_ENTRY(ast_frame) frame_list;
148 /*! Timing data flag */
150 /*! Timestamp in milliseconds */
152 /*! Length in milliseconds */
154 /*! Sequence number */
159 * Set the various field of a frame to point to a buffer.
160 * Typically you set the base address of the buffer, the offset as
161 * AST_FRIENDLY_OFFSET, and the datalen as the amount of bytes queued.
162 * The remaining things (to be done manually) is set the number of
163 * samples, which cannot be derived from the datalen unless you know
164 * the number of bits per sample.
166 #define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen) \
168 (fr)->data = (char *)_base + (_ofs); \
169 (fr)->offset = (_ofs); \
170 (fr)->datalen = (_datalen); \
173 /*! Queueing a null frame is fairly common, so we declare a global null frame object
174 for this purpose instead of having to declare one on the stack */
175 extern struct ast_frame ast_null_frame;
177 #define AST_FRIENDLY_OFFSET 64 /*! It's polite for a a new frame to
178 have this number of bytes for additional
180 #define AST_MIN_OFFSET 32 /*! Make sure we keep at least this much handy */
182 /*! Need the header be free'd? */
183 #define AST_MALLOCD_HDR (1 << 0)
184 /*! Need the data be free'd? */
185 #define AST_MALLOCD_DATA (1 << 1)
186 /*! Need the source be free'd? (haha!) */
187 #define AST_MALLOCD_SRC (1 << 2)
189 /* MODEM subclasses */
190 /*! T.38 Fax-over-IP */
191 #define AST_MODEM_T38 1
192 /*! V.150 Modem-over-IP */
193 #define AST_MODEM_V150 2
195 /* HTML subclasses */
197 #define AST_HTML_URL 1
199 #define AST_HTML_DATA 2
200 /*! Beginning frame */
201 #define AST_HTML_BEGIN 4
203 #define AST_HTML_END 8
204 /*! Load is complete */
205 #define AST_HTML_LDCOMPLETE 16
206 /*! Peer is unable to support HTML */
207 #define AST_HTML_NOSUPPORT 17
208 /*! Send URL, and track */
209 #define AST_HTML_LINKURL 18
210 /*! No more HTML linkage */
211 #define AST_HTML_UNLINK 19
212 /*! Reject link request */
213 #define AST_HTML_LINKREJECT 20
215 /* Data formats for capabilities and frames alike */
216 /*! G.723.1 compression */
217 #define AST_FORMAT_G723_1 (1 << 0)
218 /*! GSM compression */
219 #define AST_FORMAT_GSM (1 << 1)
220 /*! Raw mu-law data (G.711) */
221 #define AST_FORMAT_ULAW (1 << 2)
222 /*! Raw A-law data (G.711) */
223 #define AST_FORMAT_ALAW (1 << 3)
224 /*! ADPCM (G.726, 32kbps, AAL2 codeword packing) */
225 #define AST_FORMAT_G726_AAL2 (1 << 4)
227 #define AST_FORMAT_ADPCM (1 << 5)
228 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
229 #define AST_FORMAT_SLINEAR (1 << 6)
230 /*! LPC10, 180 samples/frame */
231 #define AST_FORMAT_LPC10 (1 << 7)
233 #define AST_FORMAT_G729A (1 << 8)
234 /*! SpeeX Free Compression */
235 #define AST_FORMAT_SPEEX (1 << 9)
236 /*! iLBC Free Compression */
237 #define AST_FORMAT_ILBC (1 << 10)
238 /*! ADPCM (G.726, 32kbps, RFC3551 codeword packing) */
239 #define AST_FORMAT_G726 (1 << 11)
240 /*! Maximum audio format */
241 #define AST_FORMAT_MAX_AUDIO (1 << 15)
242 /*! Maximum audio mask */
243 #define AST_FORMAT_AUDIO_MASK ((1 << 16)-1)
245 #define AST_FORMAT_JPEG (1 << 16)
247 #define AST_FORMAT_PNG (1 << 17)
249 #define AST_FORMAT_H261 (1 << 18)
251 #define AST_FORMAT_H263 (1 << 19)
253 #define AST_FORMAT_H263_PLUS (1 << 20)
255 #define AST_FORMAT_H264 (1 << 21)
256 /*! Maximum video format */
257 #define AST_FORMAT_MAX_VIDEO (1 << 24)
258 #define AST_FORMAT_VIDEO_MASK (((1 << 25)-1) & ~(AST_FORMAT_AUDIO_MASK))
260 enum ast_control_frame_type {
261 AST_CONTROL_HANGUP = 1, /*!< Other end has hungup */
262 AST_CONTROL_RING = 2, /*!< Local ring */
263 AST_CONTROL_RINGING = 3, /*!< Remote end is ringing */
264 AST_CONTROL_ANSWER = 4, /*!< Remote end has answered */
265 AST_CONTROL_BUSY = 5, /*!< Remote end is busy */
266 AST_CONTROL_TAKEOFFHOOK = 6, /*!< Make it go off hook */
267 AST_CONTROL_OFFHOOK = 7, /*!< Line is off hook */
268 AST_CONTROL_CONGESTION = 8, /*!< Congestion (circuits busy) */
269 AST_CONTROL_FLASH = 9, /*!< Flash hook */
270 AST_CONTROL_WINK = 10, /*!< Wink */
271 AST_CONTROL_OPTION = 11, /*!< Set a low-level option */
272 AST_CONTROL_RADIO_KEY = 12, /*!< Key Radio */
273 AST_CONTROL_RADIO_UNKEY = 13, /*!< Un-Key Radio */
274 AST_CONTROL_PROGRESS = 14, /*!< Indicate PROGRESS */
275 AST_CONTROL_PROCEEDING = 15, /*!< Indicate CALL PROCEEDING */
276 AST_CONTROL_HOLD = 16, /*!< Indicate call is placed on hold */
277 AST_CONTROL_UNHOLD = 17, /*!< Indicate call is left from hold */
278 AST_CONTROL_VIDUPDATE = 18, /*!< Indicate video frame update */
281 #define AST_SMOOTHER_FLAG_G729 (1 << 0)
283 /* Option identifiers and flags */
284 #define AST_OPTION_FLAG_REQUEST 0
285 #define AST_OPTION_FLAG_ACCEPT 1
286 #define AST_OPTION_FLAG_REJECT 2
287 #define AST_OPTION_FLAG_QUERY 4
288 #define AST_OPTION_FLAG_ANSWER 5
289 #define AST_OPTION_FLAG_WTF 6
291 /*! Verify touchtones by muting audio transmission
292 (and reception) and verify the tone is still present */
293 #define AST_OPTION_TONE_VERIFY 1
295 /*! Put a compatible channel into TDD (TTY for the hearing-impared) mode */
296 #define AST_OPTION_TDD 2
298 /*! Relax the parameters for DTMF reception (mainly for radio use) */
299 #define AST_OPTION_RELAXDTMF 3
301 /*! Set (or clear) Audio (Not-Clear) Mode */
302 #define AST_OPTION_AUDIO_MODE 4
304 /*! Set channel transmit gain
305 * Option data is a single signed char
306 representing number of decibels (dB)
307 to set gain to (on top of any gain
308 specified in channel driver)
310 #define AST_OPTION_TXGAIN 5
312 /*! Set channel receive gain
313 * Option data is a single signed char
314 representing number of decibels (dB)
315 to set gain to (on top of any gain
316 specified in channel driver)
318 #define AST_OPTION_RXGAIN 6
320 /* set channel into "Operator Services" mode */
321 #define AST_OPTION_OPRMODE 7
323 /*! Explicitly enable or disable echo cancelation for the given channel */
324 #define AST_OPTION_ECHOCAN 8
327 struct ast_channel *peer;
331 struct ast_option_header {
332 /* Always keep in network byte order */
333 #if __BYTE_ORDER == __BIG_ENDIAN
337 #if __BYTE_ORDER == __LITTLE_ENDIAN
341 #error Byte order not defined
347 /*! \brief Requests a frame to be allocated
350 * Request a frame be allocated. source is an optional source of the frame,
351 * len is the requested length, or "0" if the caller will supply the buffer
353 #if 0 /* Unimplemented */
354 struct ast_frame *ast_fralloc(char *source, int len);
357 /*! \brief Frees a frame
358 * \param fr Frame to free
359 * Free a frame, and the memory it used if applicable
362 void ast_frfree(struct ast_frame *fr);
364 /*! \brief Makes a frame independent of any static storage
365 * \param fr frame to act upon
366 * Take a frame, and if it's not been malloc'd, make a malloc'd copy
367 * and if the data hasn't been malloced then make the
368 * data malloc'd. If you need to store frames, say for queueing, then
369 * you should call this function.
370 * \return Returns a frame on success, NULL on error
372 struct ast_frame *ast_frisolate(struct ast_frame *fr);
374 /*! \brief Copies a frame
375 * \param fr frame to copy
376 * Duplicates a frame -- should only rarely be used, typically frisolate is good enough
377 * \return Returns a frame on success, NULL on error
379 struct ast_frame *ast_frdup(const struct ast_frame *fr);
381 /*! \brief Reads a frame from an fd
382 * Read a frame from a stream or packet fd, as written by fd_write
383 * \param fd an opened fd to read from
384 * \return returns a frame on success, NULL on error
386 struct ast_frame *ast_fr_fdread(int fd);
388 /*! Writes a frame to an fd
389 * Write a frame to an fd
390 * \param fd Which fd to write to
391 * \param frame frame to write to the fd
392 * \return Returns 0 on success, -1 on failure
394 int ast_fr_fdwrite(int fd, struct ast_frame *frame);
396 /*! \brief Sends a hangup to an fd
397 * Send a hangup (NULL equivalent) on an fd
398 * \param fd fd to write to
399 * \return Returns 0 on success, -1 on failure
401 int ast_fr_fdhangup(int fd);
403 void ast_swapcopy_samples(void *dst, const void *src, int samples);
405 /* Helpers for byteswapping native samples to/from
406 little-endian and big-endian. */
407 #if __BYTE_ORDER == __LITTLE_ENDIAN
408 #define ast_frame_byteswap_le(fr) do { ; } while(0)
409 #define ast_frame_byteswap_be(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0)
411 #define ast_frame_byteswap_le(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0)
412 #define ast_frame_byteswap_be(fr) do { ; } while(0)
416 /*! \brief Get the name of a format
417 * \param format id of format
418 * \return A static string containing the name of the format or "UNKN" if unknown.
420 char* ast_getformatname(int format);
422 /*! \brief Get the names of a set of formats
423 * \param buf a buffer for the output string
424 * \param size size of buf (bytes)
425 * \param format the format (combined IDs of codecs)
426 * Prints a list of readable codec names corresponding to "format".
427 * ex: for format=AST_FORMAT_GSM|AST_FORMAT_SPEEX|AST_FORMAT_ILBC it will return "0x602 (GSM|SPEEX|ILBC)"
428 * \return The return value is buf.
430 char* ast_getformatname_multiple(char *buf, size_t size, int format);
433 * \brief Gets a format from a name.
434 * \param name string of format
435 * \return This returns the form of the format in binary on success, 0 on error.
437 int ast_getformatbyname(const char *name);
439 /*! \brief Get a name from a format
440 * Gets a name from a format
441 * \param codec codec number (1,2,4,8,16,etc.)
442 * \return This returns a static string identifying the format on success, 0 on error.
444 char *ast_codec2str(int codec);
448 struct ast_format_list *ast_get_format_list_index(int index);
449 struct ast_format_list *ast_get_format_list(size_t *size);
450 struct ast_smoother *ast_smoother_new(int bytes);
451 void ast_smoother_set_flags(struct ast_smoother *smoother, int flags);
452 int ast_smoother_get_flags(struct ast_smoother *smoother);
453 void ast_smoother_free(struct ast_smoother *s);
454 void ast_smoother_reset(struct ast_smoother *s, int bytes);
455 int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap);
456 struct ast_frame *ast_smoother_read(struct ast_smoother *s);
457 #define ast_smoother_feed(s,f) __ast_smoother_feed(s, f, 0)
458 #if __BYTE_ORDER == __LITTLE_ENDIAN
459 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 1)
460 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 0)
462 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 0)
463 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 1)
466 void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix);
468 /*! \par AudioCodecPref Audio Codec Preferences
469 In order to negotiate audio codecs in the order they are configured
470 in <channel>.conf for a device, we set up codec preference lists
471 in addition to the codec capabilities setting. The capabilities
472 setting is a bitmask of audio and video codecs with no internal
473 order. This will reflect the offer given to the other side, where
474 the prefered codecs will be added to the top of the list in the
475 order indicated by the "allow" lines in the device configuration.
477 Video codecs are not included in the preference lists since they
478 can't be transcoded and we just have to pick whatever is supported
481 /*! \brief Initialize an audio codec preference to "no preference" See \ref AudioCodecPref */
482 void ast_codec_pref_init(struct ast_codec_pref *pref);
484 /*! \brief Codec located at a particular place in the preference index See \ref AudioCodecPref */
485 int ast_codec_pref_index(struct ast_codec_pref *pref, int index);
487 /*! \brief Remove audio a codec from a preference list */
488 void ast_codec_pref_remove(struct ast_codec_pref *pref, int format);
490 /*! \brief Append a audio codec to a preference list, removing it first if it was already there
492 int ast_codec_pref_append(struct ast_codec_pref *pref, int format);
494 /*! \brief Select the best audio format according to preference list from supplied options.
495 If "find_best" is non-zero then if nothing is found, the "Best" format of
496 the format list is selected, otherwise 0 is returned. */
497 int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best);
499 /*! \brief Parse an "allow" or "deny" line in a channel or device configuration
500 and update the capabilities mask and pref if provided.
501 Video codecs are not added to codec preference lists, since we can not transcode
503 void ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing);
505 /*! \brief Dump audio codec preference list into a string */
506 int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size);
508 /*! \brief Shift an audio codec preference list up or down 65 bytes so that it becomes an ASCII string */
509 void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right);
511 /*! \brief Returns the number of samples contained in the frame */
512 int ast_codec_get_samples(struct ast_frame *f);
514 /*! \brief Returns the number of bytes for the number of samples of the given format */
515 int ast_codec_get_len(int format, int samples);
517 /*! \brief Appends a frame to the end of a list of frames, truncating the maximum length of the list */
518 struct ast_frame *ast_frame_enqueue(struct ast_frame *head, struct ast_frame *f, int maxlen, int dupe);
521 /*! \brief Gets duration in ms of interpolation frame for a format */
522 static inline int ast_codec_interp_len(int format)
524 return (format == AST_FORMAT_ILBC) ? 30 : 20;
528 \brief Adjusts the volume of the audio samples contained in a frame.
529 \param f The frame containing the samples (must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR)
530 \param adjustment The number of dB to adjust up or down.
531 \return 0 for success, non-zero for an error
533 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment);
536 \brief Sums two frames of audio samples.
537 \param f1 The first frame (which will contain the result)
538 \param f2 The second frame
539 \return 0 for success, non-zero for an error
541 The frames must be AST_FRAME_VOICE and must contain AST_FORMAT_SLINEAR samples,
542 and must contain the same number of samples.
544 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2);
546 #if defined(__cplusplus) || defined(c_plusplus)
550 #endif /* _ASTERISK_FRAME_H */