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_CONTROL_INBAND_INFO 42 00248 #define AST_CONTROL_DISCONNECT 43 00249 00250 #define AST_SMOOTHER_FLAG_G729 (1 << 0) 00251 00252 /* Option identifiers and flags */ 00253 #define AST_OPTION_FLAG_REQUEST 0 00254 #define AST_OPTION_FLAG_ACCEPT 1 00255 #define AST_OPTION_FLAG_REJECT 2 00256 #define AST_OPTION_FLAG_QUERY 4 00257 #define AST_OPTION_FLAG_ANSWER 5 00258 #define AST_OPTION_FLAG_WTF 6 00259 00260 /*! Verify touchtones by muting audio transmission 00261 (and reception) and verify the tone is still present */ 00262 #define AST_OPTION_TONE_VERIFY 1 00263 00264 /*! Put a compatible channel into TDD (TTY for the hearing-impared) mode */ 00265 #define AST_OPTION_TDD 2 00266 00267 /*! Relax the parameters for DTMF reception (mainly for radio use) */ 00268 #define AST_OPTION_RELAXDTMF 3 00269 00270 /*! Set (or clear) Audio (Not-Clear) Mode */ 00271 #define AST_OPTION_AUDIO_MODE 4 00272 00273 /*! Set channel transmit gain 00274 * Option data is a single signed char 00275 representing number of decibels (dB) 00276 to set gain to (on top of any gain 00277 specified in channel driver) 00278 */ 00279 #define AST_OPTION_TXGAIN 5 00280 00281 /*! Set channel receive gain 00282 * Option data is a single signed char 00283 representing number of decibels (dB) 00284 to set gain to (on top of any gain 00285 specified in channel driver) 00286 */ 00287 #define AST_OPTION_RXGAIN 6 00288 00289 struct ast_option_header { 00290 /* Always keep in network byte order */ 00291 #if __BYTE_ORDER == __BIG_ENDIAN 00292 u_int16_t flag:3; 00293 u_int16_t option:13; 00294 #else 00295 #if __BYTE_ORDER == __LITTLE_ENDIAN 00296 u_int16_t option:13; 00297 u_int16_t flag:3; 00298 #else 00299 #error Byte order not defined 00300 #endif 00301 #endif 00302 u_int8_t data[0]; 00303 }; 00304 00305 /*! \brief Requests a frame to be allocated 00306 * 00307 * \param source 00308 * Request a frame be allocated. source is an optional source of the frame, 00309 * len is the requested length, or "0" if the caller will supply the buffer 00310 */ 00311 #if 0 /* Unimplemented */ 00312 struct ast_frame *ast_fralloc(char *source, int len); 00313 #endif 00314 00315 /*! \brief Frees a frame 00316 * \param fr Frame to free 00317 * Free a frame, and the memory it used if applicable 00318 * \return no return. 00319 */ 00320 void ast_frfree(struct ast_frame *fr); 00321 00322 /*! \brief Copies a frame 00323 * \param fr frame to act upon 00324 * Take a frame, and if it's not been malloc'd, make a malloc'd copy 00325 * and if the data hasn't been malloced then make the 00326 * data malloc'd. If you need to store frames, say for queueing, then 00327 * you should call this function. 00328 * \return Returns a frame on success, NULL on error 00329 */ 00330 struct ast_frame *ast_frisolate(struct ast_frame *fr); 00331 00332 /*! \brief Copies a frame 00333 * \param fr frame to copy 00334 * Dupliates a frame -- should only rarely be used, typically frisolate is good enough 00335 * \return Returns a frame on success, NULL on error 00336 */ 00337 struct ast_frame *ast_frdup(struct ast_frame *fr); 00338 00339 /*! \brief Reads a frame from an fd 00340 * Read a frame from a stream or packet fd, as written by fd_write 00341 * \param fd an opened fd to read from 00342 * \return returns a frame on success, NULL on error 00343 */ 00344 struct ast_frame *ast_fr_fdread(int fd); 00345 00346 /*! Writes a frame to an fd 00347 * Write a frame to an fd 00348 * \param fd Which fd to write to 00349 * \param frame frame to write to the fd 00350 * \return Returns 0 on success, -1 on failure 00351 */ 00352 int ast_fr_fdwrite(int fd, struct ast_frame *frame); 00353 00354 /*! \brief Sends a hangup to an fd 00355 * Send a hangup (NULL equivalent) on an fd 00356 * \param fd fd to write to 00357 * \return Returns 0 on success, -1 on failure 00358 */ 00359 int ast_fr_fdhangup(int fd); 00360 00361 void ast_swapcopy_samples(void *dst, const void *src, int samples); 00362 00363 /* Helpers for byteswapping native samples to/from 00364 little-endian and big-endian. */ 00365 #if __BYTE_ORDER == __LITTLE_ENDIAN 00366 #define ast_frame_byteswap_le(fr) do { ; } while(0) 00367 #define ast_frame_byteswap_be(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0) 00368 #else 00369 #define ast_frame_byteswap_le(fr) do { struct ast_frame *__f = (fr); ast_swapcopy_samples(__f->data, __f->data, __f->samples); } while(0) 00370 #define ast_frame_byteswap_be(fr) do { ; } while(0) 00371 #endif 00372 00373 00374 /*! \brief Get the name of a format 00375 * \param format id of format 00376 * \return A static string containing the name of the format or "UNKN" if unknown. 00377 */ 00378 extern char* ast_getformatname(int format); 00379 00380 /*! \brief Get the names of a set of formats 00381 * \param buf a buffer for the output string 00382 * \param size size of buf (bytes) 00383 * \param format the format (combined IDs of codecs) 00384 * Prints a list of readable codec names corresponding to "format". 00385 * ex: for format=AST_FORMAT_GSM|AST_FORMAT_SPEEX|AST_FORMAT_ILBC it will return "0x602 (GSM|SPEEX|ILBC)" 00386 * \return The return value is buf. 00387 */ 00388 extern char* ast_getformatname_multiple(char *buf, size_t size, int format); 00389 00390 /*! 00391 * \brief Gets a format from a name. 00392 * \param name string of format 00393 * \return This returns the form of the format in binary on success, 0 on error. 00394 */ 00395 extern int ast_getformatbyname(char *name); 00396 00397 /*! \brief Get a name from a format 00398 * Gets a name from a format 00399 * \param codec codec number (1,2,4,8,16,etc.) 00400 * \return This returns a static string identifying the format on success, 0 on error. 00401 */ 00402 extern char *ast_codec2str(int codec); 00403 00404 struct ast_smoother; 00405 00406 extern struct ast_format_list *ast_get_format_list_index(int index); 00407 extern struct ast_format_list *ast_get_format_list(size_t *size); 00408 extern struct ast_smoother *ast_smoother_new(int bytes); 00409 extern void ast_smoother_set_flags(struct ast_smoother *smoother, int flags); 00410 extern int ast_smoother_get_flags(struct ast_smoother *smoother); 00411 extern void ast_smoother_free(struct ast_smoother *s); 00412 extern void ast_smoother_reset(struct ast_smoother *s, int bytes); 00413 extern int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap); 00414 extern struct ast_frame *ast_smoother_read(struct ast_smoother *s); 00415 #define ast_smoother_feed(s,f) __ast_smoother_feed(s, f, 0) 00416 #if __BYTE_ORDER == __LITTLE_ENDIAN 00417 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 1) 00418 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 0) 00419 #else 00420 #define ast_smoother_feed_be(s,f) __ast_smoother_feed(s, f, 0) 00421 #define ast_smoother_feed_le(s,f) __ast_smoother_feed(s, f, 1) 00422 #endif 00423 00424 extern void ast_frame_dump(char *name, struct ast_frame *f, char *prefix); 00425 00426 /*! \brief Initialize a codec preference to "no preference" */ 00427 extern void ast_codec_pref_init(struct ast_codec_pref *pref); 00428 00429 /*! \brief Codec located at a particular place in the preference index */ 00430 extern int ast_codec_pref_index(struct ast_codec_pref *pref, int index); 00431 00432 /*! \brief Remove a codec from a preference list */ 00433 extern void ast_codec_pref_remove(struct ast_codec_pref *pref, int format); 00434 00435 /*! \brief Append a codec to a preference list, removing it first if it was already there */ 00436 extern int ast_codec_pref_append(struct ast_codec_pref *pref, int format); 00437 00438 /*! \brief Select the best format according to preference list from supplied options. 00439 If "find_best" is non-zero then if nothing is found, the "Best" format of 00440 the format list is selected, otherwise 0 is returned. */ 00441 extern int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best); 00442 00443 /*! \brief Parse an "allow" or "deny" line and update the mask and pref if provided */ 00444 extern void ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing); 00445 00446 /*! \brief Dump codec preference list into a string */ 00447 extern int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size); 00448 00449 /*! \brief Shift a codec preference list up or down 65 bytes so that it becomes an ASCII string */ 00450 extern void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right); 00451 00452 /*! \brief Returns the number of samples contained in the frame */ 00453 extern int ast_codec_get_samples(struct ast_frame *f); 00454 00455 /*! \brief Returns the number of bytes for the number of samples of the given format */ 00456 extern int ast_codec_get_len(int format, int samples); 00457 00458 /*! \brief Gets duration in ms of interpolation frame for a format */ 00459 static inline int ast_codec_interp_len(int format) 00460 { 00461 return (format == AST_FORMAT_ILBC) ? 30 : 20; 00462 } 00463 00464 /*! 00465 \brief Adjusts the volume of the audio samples contained in a frame. 00466 \param f The frame containing the samples (must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR) 00467 \param adjustment The number of dB to adjust up or down. 00468 \return 0 for success, non-zero for an error 00469 */ 00470 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment); 00471 00472 /*! 00473 \brief Sums two frames of audio samples. 00474 \param f1 The first frame (which will contain the result) 00475 \param f2 The second frame 00476 \return 0 for success, non-zero for an error 00477 00478 The frames must be AST_FRAME_VOICE and must contain AST_FORMAT_SLINEAR samples, 00479 and must contain the same number of samples. 00480 */ 00481 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2); 00482 00483 #if defined(__cplusplus) || defined(c_plusplus) 00484 } 00485 #endif 00486 00487 #endif /* _ASTERISK_FRAME_H */