Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## Asterisk developer's documentation

# frame.h

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (C) 1999 - 2005, Digium, Inc.
    00005  *
    00006  * Mark Spencer <markster@digium.com>
    00007  *
    00008  * See http://www.asterisk.org for more information about
    00009  * the Asterisk project. Please do not directly contact
    00010  * any of the maintainers of this project for assistance;
    00011  * the project provides a web site, mailing lists and IRC
    00012  * channels for your use.
    00013  *
    00014  * This program is free software, distributed under the terms of
    00015  * the GNU General Public License Version 2. See the LICENSE file
    00016  * at the top of the source tree.
    00017  */
    00018
    00019 /*! \file
    00020  * \brief Asterisk internal frame definitions.
    00021  * \arg For an explanation of frames, see \ref Def_Frame
    00022  * \arg Frames are send of Asterisk channels, see \ref Def_Channel
    00023  */
    00024
    00025 #ifndef _ASTERISK_FRAME_H
    00026 #define _ASTERISK_FRAME_H
    00027
    00028 #if defined(__cplusplus) || defined(c_plusplus)
    00029 extern "C" {
    00030 #endif
    00031
    00032 #include <sys/types.h>
    00033 #include <sys/time.h>
    00034 #include "asterisk/endian.h"
    00035
    00036 struct ast_codec_pref {
    00037    char order[32];
    00038 };
    00039
    00040 /*! \page Def_Frame AST Multimedia and signalling frames
    00041    \section Def_AstFrame What is an ast_frame ?
    00042    A frame of data read used to communicate between
    00043    between channels and applications.
    00044    Frames are divided into frame types and subclasses.
    00045
    00046    \par Frame types
    00047    \arg \b VOICE: Voice data, subclass is codec (AST_FORMAT_*)
    00048    \arg \b VIDEO: Video data, subclass is codec (AST_FORMAT_*)
    00049    \arg \b DTMF:  A DTMF digit, subclass is the digit
    00050    \arg \b IMAGE: Image transport, mostly used in IAX
    00051    \arg \b TEXT:  Text messages
    00052    \arg \b HTML:  URL's and web pages
    00053    \arg \b T38:   T38 Fax transport frames
    00054    \arg \b IAX:   Private frame type for the IAX protocol
    00055    \arg \b CNG:   Comfort noice frames
    00056    \arg \b CONTROL:  A control frame, subclass defined as AST_CONTROL_
    00057    \arg \b NULL:  Empty, useless frame
    00058
    00059    \par Files
    00060    \arg frame.h   Definitions
    00061    \arg frame.c   Function library
    00062    \arg \ref Def_Channel Asterisk channels
    00063    \section Def_ControlFrame Control Frames
    00064    Control frames send signalling information between channels
    00065    and devices. They are prefixed with AST_CONTROL_, like AST_CONTROL_FRAME_HANGUP
    00066    \arg \b HANGUP The other end has hungup
    00067    \arg \b RING   Local ring
    00068    \arg \b RINGING   The other end is ringing
    00069    \arg \b ANSWER The other end has answered
    00070    \arg \b BUSY   Remote end is busy
    00071    \arg \b TAKEOFFHOOK  Make it go off hook (what's "it" ? )
    00072    \arg \b OFFHOOK   Line is off hook
    00073    \arg \b CONGESTION   Congestion (circuit is busy, not available)
    00074    \arg \b FLASH  Other end sends flash hook
    00075    \arg \b WINK   Other end sends wink
    00076    \arg \b OPTION Send low-level option
    00077    \arg \b RADIO_KEY Key radio (see app_rpt.c)
    00078    \arg \b RADIO_UNKEY  Un-key radio (see app_rpt.c)
    00079    \arg \b PROGRESS  Other end indicates call progress
    00080    \arg \b PROCEEDING   Indicates proceeding
    00081    \arg \b HOLD   Call is placed on hold
    00082    \arg \b UNHOLD Call is back from hold
    00083    \arg \b VIDUPDATE Video update requested
    00084
    00085 */
    00086
    00087 /*! \brief Data structure associated with a single frame of data
    00088  */
    00089 struct ast_frame {
    00090    /*! Kind of frame */
    00091    int frametype;
    00092    /*! Subclass, frame dependent */
    00093    int subclass;
    00094    /*! Length of data */
    00095    int datalen;
    00096    /*! Number of 8khz samples in this frame */
    00097    int samples;
    00098    /*! Was the data malloc'd?  i.e. should we free it when we discard the frame? */
    00099    int mallocd;
    00100    /*! How many bytes exist _before_ "data" that can be used if needed */
    00101    int offset;
    00102    /*! Optional source of frame for debugging */
    00103    const char *src;
    00104    /*! Pointer to actual data */
    00105    void *data;
    00106    /*! Global delivery time */
    00107    struct timeval delivery;
    00108    /*! Next/Prev for linking stand alone frames */
    00109    struct ast_frame *prev;
    00110    /*! Next/Prev for linking stand alone frames */
    00111    struct ast_frame *next;
    00112 };
    00113
    00114 #define AST_FRIENDLY_OFFSET   64 /*! It's polite for a a new frame to
    00115                  have this number of bytes for additional
    00116                  headers.  */
    00117 #define AST_MIN_OFFSET     32 /*! Make sure we keep at least this much handy */
    00118
    00119 /*! Need the header be free'd? */
    00120 #define AST_MALLOCD_HDR    (1 << 0)
    00121 /*! Need the data be free'd? */
    00122 #define AST_MALLOCD_DATA   (1 << 1)
    00123 /*! Need the source be free'd? (haha!) */
    00124 #define AST_MALLOCD_SRC    (1 << 2)
    00125
    00126 /* Frame types */
    00127 /*! A DTMF digit, subclass is the digit */
    00128 #define AST_FRAME_DTMF     1
    00129 /*! Voice data, subclass is AST_FORMAT_* */
    00130 #define AST_FRAME_VOICE    2
    00131 /*! Video frame, maybe?? :) */
    00132 #define AST_FRAME_VIDEO    3
    00133 /*! A control frame, subclass is AST_CONTROL_* */
    00134 #define AST_FRAME_CONTROL  4
    00135 /*! An empty, useless frame */
    00136 #define AST_FRAME_NULL     5
    00137 /*! Inter Asterisk Exchange private frame type */
    00138 #define AST_FRAME_IAX      6
    00139 /*! Text messages */
    00140 #define AST_FRAME_TEXT     7
    00141 /*! Image Frames */
    00142 #define AST_FRAME_IMAGE    8
    00143 /*! HTML Frame */
    00144 #define AST_FRAME_HTML     9
    00145 /*! Comfort Noise frame (subclass is level of CNG in -dBov),
    00146     body may include zero or more 8-bit quantization coefficients */
    00147 #define AST_FRAME_CNG      10
    00148 /*! T.38 Fax-over-IP data stream */
    00149 #define AST_FRAME_T38      11
    00150
    00151 /* HTML subclasses */
    00152 /*! Sending a URL */
    00153 #define AST_HTML_URL    1
    00154 /*! Data frame */
    00155 #define AST_HTML_DATA      2
    00156 /*! Beginning frame */
    00157 #define AST_HTML_BEGIN     4
    00158 /*! End frame */
    00159 #define AST_HTML_END    8
    00160 /*! Load is complete */
    00161 #define AST_HTML_LDCOMPLETE   16
    00162 /*! Peer is unable to support HTML */
    00163 #define AST_HTML_NOSUPPORT 17
    00164 /*! Send URL, and track */
    00165 #define AST_HTML_LINKURL   18
    00166 /*! No more HTML linkage */
    00167 #define AST_HTML_UNLINK    19
    00168 /*! Reject link request */
    00169 #define AST_HTML_LINKREJECT   20
    00170
    00171 /* Data formats for capabilities and frames alike */
    00172 /*! G.723.1 compression */
    00173 #define AST_FORMAT_G723_1  (1 << 0)
    00174 /*! GSM compression */
    00175 #define AST_FORMAT_GSM     (1 << 1)
    00176 /*! Raw mu-law data (G.711) */
    00177 #define AST_FORMAT_ULAW    (1 << 2)
    00178 /*! Raw A-law data (G.711) */
    00179 #define AST_FORMAT_ALAW    (1 << 3)
    00180 /*! ADPCM (G.726, 32kbps) */
    00181 #define AST_FORMAT_G726    (1 << 4)
    00182 /*! ADPCM (IMA) */
    00183 #define AST_FORMAT_ADPCM   (1 << 5)
    00184 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
    00185 #define AST_FORMAT_SLINEAR (1 << 6)
    00186 /*! LPC10, 180 samples/frame */
    00187 #define AST_FORMAT_LPC10   (1 << 7)
    00188 /*! G.729A audio */
    00189 #define AST_FORMAT_G729A   (1 << 8)
    00190 /*! SpeeX Free Compression */
    00191 #define AST_FORMAT_SPEEX   (1 << 9)
    00192 /*! iLBC Free Compression */
    00193 #define AST_FORMAT_ILBC    (1 << 10)
    00194 /*! Maximum audio format */
    00195 #define AST_FORMAT_MAX_AUDIO  (1 << 15)
    00196 /*! JPEG Images */
    00197 #define AST_FORMAT_JPEG    (1 << 16)
    00198 /*! PNG Images */
    00199 #define AST_FORMAT_PNG     (1 << 17)
    00200 /*! H.261 Video */
    00201 #define AST_FORMAT_H261    (1 << 18)
    00202 /*! H.263 Video */
    00203 #define AST_FORMAT_H263    (1 << 19)
    00204 /*! H.263+ Video */
    00205 #define AST_FORMAT_H263_PLUS  (1 << 20)
    00206 /*! Maximum video format */
    00207 #define AST_FORMAT_MAX_VIDEO  (1 << 24)
    00208
    00209 /* Control frame types */
    00210 /*! Other end has hungup */
    00211 #define AST_CONTROL_HANGUP    1
    00212 /*! Local ring */
    00213 #define AST_CONTROL_RING      2
    00214 /*! Remote end is ringing */
    00215 #define AST_CONTROL_RINGING      3
    00216 /*! Remote end has answered */
    00217 #define AST_CONTROL_ANSWER    4
    00218 /*! Remote end is busy */
    00219 #define AST_CONTROL_BUSY      5
    00220 /*! Make it go off hook */
    00221 #define AST_CONTROL_TAKEOFFHOOK     6
    00222 /*! Line is off hook */
    00223 #define AST_CONTROL_OFFHOOK      7
    00224 /*! Congestion (circuits busy) */
    00225 #define AST_CONTROL_CONGESTION      8
    00226 /*! Flash hook */
    00227 #define AST_CONTROL_FLASH     9
    00228 /*! Wink */
    00229 #define AST_CONTROL_WINK      10
    00230 /*! Set a low-level option */
    00231 #define AST_CONTROL_OPTION    11
    00232 /*! Key Radio */
    00233 #define  AST_CONTROL_RADIO_KEY      12
    00234 /*! Un-Key Radio */
    00235 #define  AST_CONTROL_RADIO_UNKEY    13
    00236 /*! Indicate PROGRESS */
    00237 #define AST_CONTROL_PROGRESS            14
    00238 /*! Indicate CALL PROCEEDING */
    00239 #define AST_CONTROL_PROCEEDING      15
    00240 /*! Indicate call is placed on hold */
    00241 #define AST_CONTROL_HOLD         16
    00242 /*! Indicate call is left from hold */
    00243 #define AST_CONTROL_UNHOLD       17
    00244 /*! Indicate video frame update */
    00245 #define AST_CONTROL_VIDUPDATE    18
    00246
    00247 #define AST_SMOOTHER_FLAG_G729      (1 << 0)
    00248
    00249 /* Option identifiers and flags */
    00250 #define AST_OPTION_FLAG_REQUEST     0
    00251 #define AST_OPTION_FLAG_ACCEPT      1
    00252 #define AST_OPTION_FLAG_REJECT      2
    00253 #define AST_OPTION_FLAG_QUERY    4
    00254 #define AST_OPTION_FLAG_ANSWER      5
    00255 #define AST_OPTION_FLAG_WTF      6
    00256
    00257 /*! Verify touchtones by muting audio transmission
    00258    (and reception) and verify the tone is still present */
    00259 #define AST_OPTION_TONE_VERIFY      1
    00260
    00261 /*! Put a compatible channel into TDD (TTY for the hearing-impared) mode */
    00262 #define  AST_OPTION_TDD       2
    00263
    00264 /*! Relax the parameters for DTMF reception (mainly for radio use) */
    00265 #define  AST_OPTION_RELAXDTMF    3
    00266
    00267 /*! Set (or clear) Audio (Not-Clear) Mode */
    00268 #define  AST_OPTION_AUDIO_MODE      4
    00269
    00270 /*! Set channel transmit gain
    00271  * Option data is a single signed char
    00272    representing number of decibels (dB)
    00273    to set gain to (on top of any gain
    00274    specified in channel driver)
    00275 */
    00276 #define AST_OPTION_TXGAIN     5
    00277
    00278 /*! Set channel receive gain
    00279  * Option data is a single signed char
    00280    representing number of decibels (dB)
    00281    to set gain to (on top of any gain
    00282    specified in channel driver)
    00283 */
    00284 #define AST_OPTION_RXGAIN     6
    00285
    00286 struct ast_option_header {
    00287    /* Always keep in network byte order */
    00288 #if __BYTE_ORDER == __BIG_ENDIAN
    00289         u_int16_t flag:3;
    00290         u_int16_t option:13;
    00291 #else
    00292 #if __BYTE_ORDER == __LITTLE_ENDIAN
    00293         u_int16_t option:13;
    00294         u_int16_t flag:3;
    00295 #else
    00296 #error Byte order not defined
    00297 #endif
    00298 #endif
    00299       u_int8_t data[0];
    00300 };
    00301
    00302 /*! \brief  Requests a frame to be allocated
    00303  *
    00304  * \param source
    00305  * Request a frame be allocated.  source is an optional source of the frame,
    00306  * len is the requested length, or "0" if the caller will supply the buffer
    00307  */
    00308 #if 0 /* Unimplemented */
    00309 struct ast_frame *ast_fralloc(char *source, int len);
    00310 #endif
    00311
    00312 /*!  \brief Frees a frame
    00313  * \param fr Frame to free
    00314  * Free a frame, and the memory it used if applicable
    00315  * \return no return.
    00316  */
    00317 void ast_frfree(struct ast_frame *fr);
    00318
    00319 /*! \brief Copies a frame
    00320  * \param fr frame to act upon
    00321  * Take a frame, and if it's not been malloc'd, make a malloc'd copy
    00322  * and if the data hasn't been malloced then make the
    00323  * data malloc'd.  If you need to store frames, say for queueing, then
    00324  * you should call this function.
    00325  * \return Returns a frame on success, NULL on error
    00326  */
    00327 struct ast_frame *ast_frisolate(struct ast_frame *fr);
    00328
    00329 /*! \brief Copies a frame
    00330  * \param fr frame to copy
    00331  * Dupliates a frame -- should only rarely be used, typically frisolate is good enough
    00332  * \return Returns a frame on success, NULL on error
    00333  */
    00334 struct ast_frame *ast_frdup(struct ast_frame *fr);
    00335
    00336 /*! \brief Reads a frame from an fd
    00337  * Read a frame from a stream or packet fd, as written by fd_write
    00338  * \param fd an opened fd to read from
    00339  * \return returns a frame on success, NULL on error
    00340  */
    00341 struct ast_frame *ast_fr_fdread(int fd);
    00342
    00343 /*! Writes a frame to an fd
    00344  * Write a frame to an fd
    00345  * \param fd Which fd to write to
    00346  * \param frame frame to write to the fd
    00347  * \return Returns 0 on success, -1 on failure
    00348  */
    00349 int ast_fr_fdwrite(int fd, struct ast_frame *frame);
    00350
    00351 /*! \brief Sends a hangup to an fd
    00352  * Send a hangup (NULL equivalent) on an fd
    00353  * \param fd fd to write to
    00354  * \return Returns 0 on success, -1 on failure
    00355  */
    00356 int ast_fr_fdhangup(int fd);
    00357
    00358 void ast_swapcopy_samples(void *dst, const void *src, int samples);
    00359
    00360 /* Helpers for byteswapping native samples to/from
    00361    little-endian and big-endian. */
    00362 #if __BYTE_ORDER == __LITTLE_ENDIAN
    00363 #define ast_frame_byteswap_le(fr) do { ; } while(0)
    00364 #define ast_frame_byteswap_be(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0)
    00365 #else
    00366 #define ast_frame_byteswap_le(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0)
    00367 #define ast_frame_byteswap_be(fr) do { ; } while(0)
    00368 #endif
    00369
    00370
    00371 /*! \brief Get the name of a format
    00372  * \param format id of format
    00373  * \return A static string containing the name of the format or "UNKN" if unknown.
    00374  */
    00375 extern char* ast_getformatname(int format);
    00376
    00377 /*! \brief Get the names of a set of formats
    00378  * \param buf a buffer for the output string
    00379  * \param size size of buf (bytes)
    00380  * \param format the format (combined IDs of codecs)
    00381  * Prints a list of readable codec names corresponding to "format".
    00382  * ex: for format=AST_FORMAT_GSM|AST_FORMAT_SPEEX|AST_FORMAT_ILBC it will return "0x602 (GSM|SPEEX|ILBC)"
    00383  * \return The return value is buf.
    00384  */
    00385 extern char* ast_getformatname_multiple(char *buf, size_t size, int format);
    00386
    00387 /*!
    00388  * \brief Gets a format from a name.
    00389  * \param name string of format
    00390  * \return This returns the form of the format in binary on success, 0 on error.
    00391  */
    00392 extern int ast_getformatbyname(char *name);
    00393
    00394 /*! \brief Get a name from a format
    00395  * Gets a name from a format
    00396  * \param codec codec number (1,2,4,8,16,etc.)
    00397  * \return This returns a static string identifying the format on success, 0 on error.
    00398  */
    00399 extern char *ast_codec2str(int codec);
    00400
    00401 struct ast_smoother;
    00402
    00403 extern struct ast_format_list *ast_get_format_list_index(int index);
    00404 extern struct ast_format_list *ast_get_format_list(size_t *size);
    00405 extern struct ast_smoother *ast_smoother_new(int bytes);
    00406 extern void ast_smoother_set_flags(struct ast_smoother *smoother, int flags);
    00407 extern int ast_smoother_get_flags(struct ast_smoother *smoother);
    00408 extern void ast_smoother_free(struct ast_smoother *s);
    00409 extern void ast_smoother_reset(struct ast_smoother *s, int bytes);
    00410 extern int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap);
    00411 extern struct ast_frame *ast_smoother_read(struct ast_smoother *s);
    00412 #define ast_smoother_feed(s,f) __ast_smoother_feed(s, f, 0)
    00413 #if __BYTE_ORDER == __LITTLE_ENDIAN
    00414 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 1)
    00415 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 0)
    00416 #else
    00417 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 0)
    00418 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 1)
    00419 #endif
    00420
    00421 extern void ast_frame_dump(char *name, struct ast_frame *f, char *prefix);
    00422
    00423 /*! \brief Initialize a codec preference to "no preference" */
    00424 extern void ast_codec_pref_init(struct ast_codec_pref *pref);
    00425
    00426 /*! \brief Codec located at  a particular place in the preference index */
    00427 extern int ast_codec_pref_index(struct ast_codec_pref *pref, int index);
    00428
    00429 /*! \brief Remove a codec from a preference list */
    00430 extern void ast_codec_pref_remove(struct ast_codec_pref *pref, int format);
    00431
    00432 /*! \brief Append a codec to a preference list, removing it first if it was already there */
    00433 extern int ast_codec_pref_append(struct ast_codec_pref *pref, int format);
    00434
    00435 /*! \brief Select the best format according to preference list from supplied options.
    00436    If "find_best" is non-zero then if nothing is found, the "Best" format of
    00437    the format list is selected, otherwise 0 is returned. */
    00438 extern int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best);
    00439
    00440 /*! \brief Parse an "allow" or "deny" line and update the mask and pref if provided */
    00441 extern void ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing);
    00442
    00443 /*! \brief Dump codec preference list into a string */
    00444 extern int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size);
    00445
    00446 /*! \brief Shift a codec preference list up or down 65 bytes so that it becomes an ASCII string */
    00447 extern void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right);
    00448
    00449 /*! \brief Returns the number of samples contained in the frame */
    00450 extern int ast_codec_get_samples(struct ast_frame *f);
    00451
    00452 /*! \brief Returns the number of bytes for the number of samples of the given format */
    00453 extern int ast_codec_get_len(int format, int samples);
    00454
    00455 /*! \brief Gets duration in ms of interpolation frame for a format */
    00456 static inline int ast_codec_interp_len(int format)
    00457 {
    00458    return (format == AST_FORMAT_ILBC) ? 30 : 20;
    00459 }
    00460
    00461 /*!
    00462   \brief Adjusts the volume of the audio samples contained in a frame.
    00463   \param f The frame containing the samples (must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR)
    00464   \param adjustment The number of dB to adjust up or down.
    00465   \return 0 for success, non-zero for an error
    00466  */
    00467 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment);
    00468
    00469 /*!
    00470   \brief Sums two frames of audio samples.
    00471   \param f1 The first frame (which will contain the result)
    00472   \param f2 The second frame
    00473   \return 0 for success, non-zero for an error
    00474
    00475   The frames must be AST_FRAME_VOICE and must contain AST_FORMAT_SLINEAR samples,
    00476   and must contain the same number of samples.
    00477  */
    00478 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2);
    00479
    00480 #if defined(__cplusplus) || defined(c_plusplus)
    00481 }
    00482 #endif
    00483
    00484 #endif /* _ASTERISK_FRAME_H */