2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, 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.
21 * \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
23 * RTP is defined in RFC 3550.
26 #ifndef _ASTERISK_RTP_H
27 #define _ASTERISK_RTP_H
29 #include "asterisk/network.h"
31 #include "asterisk/frame.h"
32 #include "asterisk/io.h"
33 #include "asterisk/sched.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/linkedlists.h"
37 #if defined(__cplusplus) || defined(c_plusplus)
41 /* Codes for RTP-specific data - not defined by our AST_FORMAT codes */
43 #define AST_RTP_DTMF (1 << 0)
44 /*! 'Comfort Noise' (RFC3389) */
45 #define AST_RTP_CN (1 << 1)
46 /*! DTMF (Cisco Proprietary) */
47 #define AST_RTP_CISCO_DTMF (1 << 2)
48 /*! Maximum RTP-specific code */
49 #define AST_RTP_MAX AST_RTP_CISCO_DTMF
51 /*! Maxmum number of payload defintions for a RTP session */
52 #define MAX_RTP_PT 256
54 #define FLAG_3389_WARNING (1 << 0)
56 enum ast_rtp_options {
57 AST_RTP_OPT_G726_NONSTANDARD = (1 << 0),
60 enum ast_rtp_get_result {
61 /*! Failed to find the RTP structure */
62 AST_RTP_GET_FAILED = 0,
63 /*! RTP structure exists but true native bridge can not occur so try partial */
65 /*! RTP structure exists and native bridge can occur */
71 /*! \brief This is the structure that binds a channel (SIP/Jingle/H.323) to the RTP subsystem
73 struct ast_rtp_protocol {
74 /*! Get RTP struct, or NULL if unwilling to transfer */
75 enum ast_rtp_get_result (* const get_rtp_info)(struct ast_channel *chan, struct ast_rtp **rtp);
76 /*! Get RTP struct, or NULL if unwilling to transfer */
77 enum ast_rtp_get_result (* const get_vrtp_info)(struct ast_channel *chan, struct ast_rtp **rtp);
78 /*! Get RTP struct, or NULL if unwilling to transfer */
79 enum ast_rtp_get_result (* const get_trtp_info)(struct ast_channel *chan, struct ast_rtp **rtp);
81 int (* const set_rtp_peer)(struct ast_channel *chan, struct ast_rtp *peer, struct ast_rtp *vpeer, struct ast_rtp *tpeer, int codecs, int nat_active);
82 int (* const get_codec)(struct ast_channel *chan);
83 const char * const type;
84 AST_LIST_ENTRY(ast_rtp_protocol) list;
87 /*! \brief RTCP quality report storage */
88 struct ast_rtp_quality {
89 unsigned int local_ssrc; /*!< Our SSRC */
90 unsigned int local_lostpackets; /*!< Our lost packets */
91 double local_jitter; /*!< Our calculated jitter */
92 unsigned int local_count; /*!< Number of received packets */
93 unsigned int remote_ssrc; /*!< Their SSRC */
94 unsigned int remote_lostpackets; /*!< Their lost packets */
95 double remote_jitter; /*!< Their reported jitter */
96 unsigned int remote_count; /*!< Number of transmitted packets */
97 double rtt; /*!< Round trip time */
100 /*! RTP callback structure */
101 typedef int (*ast_rtp_callback)(struct ast_rtp *rtp, struct ast_frame *f, void *data);
104 * \brief Get the amount of space required to hold an RTP session
105 * \return number of bytes required
107 size_t ast_rtp_alloc_size(void);
110 * \brief Initializate a RTP session.
115 * \param callbackmode
116 * \returns A representation (structure) of an RTP session.
118 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode);
121 * \brief Initializate a RTP session using an in_addr structure.
123 * This fuction gets called by ast_rtp_new().
128 * \param callbackmode
130 * \returns A representation (structure) of an RTP session.
132 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr in);
134 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them);
136 /* Copies from rtp to them and returns 1 if there was a change or 0 if it was already the same */
137 int ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them);
139 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us);
141 struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp);
143 /*! Destroy RTP session */
144 void ast_rtp_destroy(struct ast_rtp *rtp);
146 void ast_rtp_reset(struct ast_rtp *rtp);
148 /*! Stop RTP session, do not destroy structure */
149 void ast_rtp_stop(struct ast_rtp *rtp);
151 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback);
153 void ast_rtp_set_data(struct ast_rtp *rtp, void *data);
155 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *f);
157 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp);
159 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp);
161 int ast_rtp_fd(struct ast_rtp *rtp);
163 int ast_rtcp_fd(struct ast_rtp *rtp);
165 int ast_rtp_senddigit_begin(struct ast_rtp *rtp, char digit);
167 int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit);
169 int ast_rtp_sendcng(struct ast_rtp *rtp, int level);
171 int ast_rtp_setqos(struct ast_rtp *rtp, int tos, int cos);
173 /*! \brief Setting RTP payload types from lines in a SDP description: */
174 void ast_rtp_pt_clear(struct ast_rtp* rtp);
175 /*! \brief Set payload types to defaults */
176 void ast_rtp_pt_default(struct ast_rtp* rtp);
178 /*! \brief Copy payload types between RTP structures */
179 void ast_rtp_pt_copy(struct ast_rtp *dest, struct ast_rtp *src);
181 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt);
182 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
183 char *mimeType, char *mimeSubtype,
184 enum ast_rtp_options options);
186 /*! \brief Mapping between RTP payload format codes and Asterisk codes: */
187 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt);
188 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code);
190 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
191 int* astFormats, int* nonAstFormats);
193 /*! \brief Mapping an Asterisk code into a MIME subtype (string): */
194 const char *ast_rtp_lookup_mime_subtype(int isAstFormat, int code,
195 enum ast_rtp_options options);
197 /*! \brief Build a string of MIME subtype names from a capability list */
198 char *ast_rtp_lookup_mime_multiple(char *buf, size_t size, const int capability,
199 const int isAstFormat, enum ast_rtp_options options);
201 void ast_rtp_setnat(struct ast_rtp *rtp, int nat);
203 int ast_rtp_getnat(struct ast_rtp *rtp);
205 /*! \brief Indicate whether this RTP session is carrying DTMF or not */
206 void ast_rtp_setdtmf(struct ast_rtp *rtp, int dtmf);
208 /*! \brief Compensate for devices that send RFC2833 packets all at once */
209 void ast_rtp_setdtmfcompensate(struct ast_rtp *rtp, int compensate);
211 /*! \brief Enable STUN capability */
212 void ast_rtp_setstun(struct ast_rtp *rtp, int stun_enable);
214 /*! \brief Generic STUN request
215 * send a generic stun request to the server specified.
216 * \param s the socket used to send the request
217 * \param dst the address of the STUN server
218 * \param username if non null, add the username in the request
219 * \param answer if non null, the function waits for a response and
220 * puts here the externally visible address.
221 * \return 0 on success, other values on error.
222 * The interface it may change in the future.
224 int ast_stun_request(int s, struct sockaddr_in *dst,
225 const char *username, struct sockaddr_in *answer);
227 /*! \brief Send STUN request for an RTP socket
228 * Deprecated, this is just a wrapper for ast_rtp_stun_request()
230 void ast_rtp_stun_request(struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username);
232 /*! \brief The RTP bridge.
233 \arg \ref AstRTPbridge
235 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
237 /*! \brief Register an RTP channel client */
238 int ast_rtp_proto_register(struct ast_rtp_protocol *proto);
240 /*! \brief Unregister an RTP channel client */
241 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto);
243 int ast_rtp_make_compatible(struct ast_channel *dest, struct ast_channel *src, int media);
245 /*! \brief If possible, create an early bridge directly between the devices without
246 having to send a re-invite later */
247 int ast_rtp_early_bridge(struct ast_channel *c0, struct ast_channel *c1);
249 /*! \brief Return RTCP quality string */
250 char *ast_rtp_get_quality(struct ast_rtp *rtp, struct ast_rtp_quality *qual);
252 /*! \brief Send an H.261 fast update request. Some devices need this rather than the XML message in SIP */
253 int ast_rtcp_send_h261fur(void *data);
255 void ast_rtp_init(void); /*! Initialize RTP subsystem */
256 int ast_rtp_reload(void); /*! reload rtp configuration */
257 void ast_rtp_new_init(struct ast_rtp *rtp);
259 /*! Set codec preference */
260 int ast_rtp_codec_setpref(struct ast_rtp *rtp, struct ast_codec_pref *prefs);
262 /*! Get codec preference */
263 struct ast_codec_pref *ast_rtp_codec_getpref(struct ast_rtp *rtp);
265 /*! get format from predefined dynamic payload format */
266 int ast_rtp_codec_getformat(int pt);
268 /*! \brief Set rtp timeout */
269 void ast_rtp_set_rtptimeout(struct ast_rtp *rtp, int timeout);
270 /*! \brief Set rtp hold timeout */
271 void ast_rtp_set_rtpholdtimeout(struct ast_rtp *rtp, int timeout);
272 /*! \brief set RTP keepalive interval */
273 void ast_rtp_set_rtpkeepalive(struct ast_rtp *rtp, int period);
274 /*! \brief Get RTP keepalive interval */
275 int ast_rtp_get_rtpkeepalive(struct ast_rtp *rtp);
276 /*! \brief Get rtp hold timeout */
277 int ast_rtp_get_rtpholdtimeout(struct ast_rtp *rtp);
278 /*! \brief Get rtp timeout */
279 int ast_rtp_get_rtptimeout(struct ast_rtp *rtp);
280 /* \brief Put RTP timeout timers on hold during another transaction, like T.38 */
281 void ast_rtp_set_rtptimers_onhold(struct ast_rtp *rtp);
283 #if defined(__cplusplus) || defined(c_plusplus)
287 #endif /* _ASTERISK_RTP_H */