1 /**
2  * @file core.h  Internal API
3  *
4  * Copyright (C) 2010 Creytiv.com
5  */
6 
7 
8 #include <limits.h>
9 
10 
11 /* max bytes in pathname */
12 #if defined (PATH_MAX)
13 #define FS_PATH_MAX PATH_MAX
14 #elif defined (_POSIX_PATH_MAX)
15 #define FS_PATH_MAX _POSIX_PATH_MAX
16 #else
17 #define FS_PATH_MAX 512
18 #endif
19 
20 
21 /**
22  * RFC 3551:
23  *
24  *    0 -  95  Static payload types
25  *   96 - 127  Dynamic payload types
26  */
27 enum {
28 	PT_CN       = 13,
29 	PT_STAT_MIN = 0,
30 	PT_STAT_MAX = 95,
31 	PT_DYN_MIN  = 96,
32 	PT_DYN_MAX  = 127
33 };
34 
35 
36 /** Media constants */
37 enum {
38 	AUDIO_BANDWIDTH = 128000,  /**< Bandwidth for audio in bits/s      */
39 	VIDEO_SRATE     =  90000,  /**< Sampling rate for video            */
40 };
41 
42 
43 /* forward declarations */
44 struct stream_param;
45 
46 
47 /*
48  * Account
49  */
50 
51 
52 struct account {
53 	char *buf;                   /**< Buffer for the SIP address         */
54 	struct sip_addr laddr;       /**< Decoded SIP address                */
55 	struct uri luri;             /**< Decoded AOR uri                    */
56 	char *dispname;              /**< Display name                       */
57 	char *aor;                   /**< Local SIP uri                      */
58 
59 	/* parameters: */
60 	enum answermode answermode;  /**< Answermode for incoming calls      */
61 	struct le acv[8];            /**< List elements for aucodecl         */
62 	struct list aucodecl;        /**< List of preferred audio-codecs     */
63 	char *auth_user;             /**< Authentication username            */
64 	char *auth_pass;             /**< Authentication password            */
65 	char *mnatid;                /**< Media NAT handling                 */
66 	char *mencid;                /**< Media encryption type              */
67 	const struct mnat *mnat;     /**< MNAT module                        */
68 	const struct menc *menc;     /**< MENC module                        */
69 	char *outboundv[2];          /**< Optional SIP outbound proxies      */
70 	uint32_t ptime;              /**< Configured packet time in [ms]     */
71 	uint32_t regint;             /**< Registration interval in [seconds] */
72 	uint32_t pubint;             /**< Publication interval in [seconds]  */
73 	char *regq;                  /**< Registration Q-value               */
74 	char *rtpkeep;               /**< RTP Keepalive mechanism            */
75 	char *sipnat;                /**< SIP Nat mechanism                  */
76 	char *stun_user;             /**< STUN Username                      */
77 	char *stun_pass;             /**< STUN Password                      */
78 	char *stun_host;             /**< STUN Hostname                      */
79 	uint16_t stun_port;          /**< STUN Port number                   */
80 	struct le vcv[4];            /**< List elements for vidcodecl        */
81 	struct list vidcodecl;       /**< List of preferred video-codecs     */
82 };
83 
84 
85 /*
86  * Audio Player
87  */
88 
89 struct auplay_st {
90 	struct auplay *ap;
91 };
92 
93 struct auplay {
94 	struct le        le;
95 	const char      *name;
96 	auplay_alloc_h  *alloch;
97 };
98 
99 
100 /*
101  * Audio Source
102  */
103 
104 struct ausrc_st {
105 	const struct ausrc *as;
106 };
107 
108 struct ausrc {
109 	struct le        le;
110 	const char      *name;
111 	ausrc_alloc_h   *alloch;
112 };
113 
114 
115 /*
116  * Audio Stream
117  */
118 
119 struct audio;
120 
121 typedef void (audio_event_h)(int key, bool end, void *arg);
122 typedef void (audio_err_h)(int err, const char *str, void *arg);
123 
124 int audio_alloc(struct audio **ap, const struct stream_param *stream_prm,
125 		const struct config *cfg,
126 		struct call *call, struct sdp_session *sdp_sess, int label,
127 		const struct mnat *mnat, struct mnat_sess *mnat_sess,
128 		const struct menc *menc, struct menc_sess *menc_sess,
129 		uint32_t ptime, const struct list *aucodecl, bool offerer,
130 		audio_event_h *eventh, audio_err_h *errh, void *arg);
131 int  audio_start(struct audio *a);
132 void audio_stop(struct audio *a);
133 int  audio_encoder_set(struct audio *a, const struct aucodec *ac,
134 		       int pt_tx, const char *params);
135 int  audio_decoder_set(struct audio *a, const struct aucodec *ac,
136 		       int pt_rx, const char *params);
137 int  audio_send_digit(struct audio *a, char key);
138 void audio_sdp_attr_decode(struct audio *a);
139 int  audio_print_rtpstat(struct re_printf *pf, const struct audio *au);
140 
141 
142 /*
143  * BFCP
144  */
145 
146 struct bfcp;
147 int bfcp_alloc(struct bfcp **bfcpp, struct sdp_session *sdp_sess,
148 	       const char *proto, bool offerer,
149 	       const struct mnat *mnat, struct mnat_sess *mnat_sess);
150 int bfcp_start(struct bfcp *bfcp);
151 
152 
153 /*
154  * Call Control
155  */
156 
157 enum {
158 	CALL_LINENUM_MIN  =   1,
159 	CALL_LINENUM_MAX  = 256
160 };
161 
162 struct call;
163 
164 /** Call parameters */
165 struct call_prm {
166 	struct sa laddr;
167 	enum vidmode vidmode;
168 	int af;
169 	bool use_rtp;
170 };
171 
172 int  call_alloc(struct call **callp, const struct config *cfg,
173 		struct list *lst,
174 		const char *local_name, const char *local_uri,
175 		struct account *acc, struct ua *ua, const struct call_prm *prm,
176 		const struct sip_msg *msg, struct call *xcall,
177 		struct dnsc *dnsc,
178 		call_event_h *eh, void *arg);
179 int  call_connect(struct call *call, const struct pl *paddr);
180 int  call_accept(struct call *call, struct sipsess_sock *sess_sock,
181 		 const struct sip_msg *msg);
182 int  call_hangup(struct call *call, uint16_t scode, const char *reason);
183 int  call_progress(struct call *call);
184 int  call_answer(struct call *call, uint16_t scode);
185 int  call_sdp_get(const struct call *call, struct mbuf **descp, bool offer);
186 int  call_jbuf_stat(struct re_printf *pf, const struct call *call);
187 int  call_info(struct re_printf *pf, const struct call *call);
188 int  call_reset_transp(struct call *call, const struct sa *laddr);
189 int  call_notify_sipfrag(struct call *call, uint16_t scode,
190 			 const char *reason, ...);
191 int  call_af(const struct call *call);
192 void call_set_xrtpstat(struct call *call);
193 struct account *call_account(const struct call *call);
194 
195 
196 /*
197  * Conf
198  */
199 
200 int conf_get_range(const struct conf *conf, const char *name,
201 		   struct range *rng);
202 int conf_get_csv(const struct conf *conf, const char *name,
203 		 char *str1, size_t sz1, char *str2, size_t sz2);
204 
205 
206 /*
207  * Media control
208  */
209 
210 int mctrl_handle_media_control(struct pl *body, bool *pfu);
211 
212 
213 /*
214  * Media NAT traversal
215  */
216 
217 struct mnat {
218 	struct le le;
219 	const char *id;
220 	const char *ftag;
221 	mnat_sess_h *sessh;
222 	mnat_media_h *mediah;
223 	mnat_update_h *updateh;
224 };
225 
226 const struct mnat *mnat_find(const struct list *mnatl, const char *id);
227 
228 
229 /*
230  * Metric
231  */
232 
233 struct metric {
234 	/* internal stuff: */
235 	struct tmr tmr;
236 	uint64_t ts_start;
237 	bool started;
238 
239 	/* counters: */
240 	uint32_t n_packets;
241 	uint32_t n_bytes;
242 	uint32_t n_err;
243 
244 	/* bitrate calculation */
245 	uint32_t cur_bitrate;
246 	uint64_t ts_last;
247 	uint32_t n_bytes_last;
248 };
249 
250 void     metric_init(struct metric *metric);
251 void     metric_reset(struct metric *metric);
252 void     metric_add_packet(struct metric *metric, size_t packetsize);
253 double   metric_avg_bitrate(const struct metric *metric);
254 
255 
256 /*
257  * Module
258  */
259 
260 int module_init(const struct conf *conf);
261 void module_app_unload(void);
262 
263 
264 /*
265  * Register client
266  */
267 
268 struct reg;
269 
270 int  reg_add(struct list *lst, struct ua *ua, int regid);
271 int  reg_register(struct reg *reg, const char *reg_uri,
272 		    const char *params, uint32_t regint, const char *outbound);
273 void reg_unregister(struct reg *reg);
274 bool reg_isok(const struct reg *reg);
275 int  reg_debug(struct re_printf *pf, const struct reg *reg);
276 int  reg_status(struct re_printf *pf, const struct reg *reg);
277 
278 
279 /*
280  * RTP Header Extensions
281  */
282 
283 #define RTPEXT_HDR_SIZE        4
284 #define RTPEXT_TYPE_MAGIC 0xbede
285 
286 enum {
287 	RTPEXT_ID_MIN  =  1,
288 	RTPEXT_ID_MAX  = 14,
289 };
290 
291 enum {
292 	RTPEXT_LEN_MIN =  1,
293 	RTPEXT_LEN_MAX = 16,
294 };
295 
296 struct rtpext {
297 	unsigned id:4;
298 	unsigned len:4;
299 	uint8_t data[RTPEXT_LEN_MAX];
300 };
301 
302 
303 int rtpext_hdr_encode(struct mbuf *mb, size_t num_bytes);
304 int rtpext_encode(struct mbuf *mb, unsigned id, unsigned len,
305 		  const uint8_t *data);
306 int rtpext_decode(struct rtpext *ext, struct mbuf *mb);
307 
308 
309 /*
310  * RTP keepalive
311  */
312 
313 struct rtpkeep;
314 
315 int  rtpkeep_alloc(struct rtpkeep **rkp, const char *method, int proto,
316 		   struct rtp_sock *rtp, struct sdp_media *sdp);
317 void rtpkeep_refresh(struct rtpkeep *rk, uint32_t ts);
318 
319 
320 /*
321  * SDP
322  */
323 
324 int sdp_decode_multipart(const struct pl *ctype_prm, struct mbuf *mb);
325 const struct sdp_format *sdp_media_format_cycle(struct sdp_media *m);
326 
327 
328 /*
329  * Stream
330  */
331 
332 struct rtp_header;
333 
334 enum {STREAM_PRESZ = 4+12}; /* same as RTP_HEADER_SIZE */
335 
336 typedef void (stream_rtp_h)(const struct rtp_header *hdr,
337 			    struct rtpext *extv, size_t extc,
338 			    struct mbuf *mb, void *arg);
339 typedef void (stream_rtcp_h)(struct rtcp_msg *msg, void *arg);
340 
341 typedef void (stream_error_h)(struct stream *strm, int err, void *arg);
342 
343 /** Common parameters for media stream */
344 struct stream_param {
345 	bool use_rtp;
346 };
347 
348 /** Defines a generic media stream */
349 struct stream {
350 	struct le le;            /**< Linked list element                   */
351 	struct config_avt cfg;   /**< Stream configuration                  */
352 	struct call *call;       /**< Ref. to call object                   */
353 	struct sdp_media *sdp;   /**< SDP Media line                        */
354 	struct rtp_sock *rtp;    /**< RTP Socket                            */
355 	struct rtpkeep *rtpkeep; /**< RTP Keepalive                         */
356 	struct rtcp_stats rtcp_stats;/**< RTCP statistics                   */
357 	struct jbuf *jbuf;       /**< Jitter Buffer for incoming RTP        */
358 	struct mnat_media *mns;  /**< Media NAT traversal state             */
359 	const struct menc *menc; /**< Media encryption module               */
360 	struct menc_sess *mencs; /**< Media encryption session state        */
361 	struct menc_media *mes;  /**< Media Encryption media state          */
362 	struct metric metric_tx; /**< Metrics for transmit                  */
363 	struct metric metric_rx; /**< Metrics for receiving                 */
364 	char *cname;             /**< RTCP Canonical end-point identifier   */
365 	uint32_t ssrc_rx;        /**< Incoming syncronizing source          */
366 	uint32_t pseq;           /**< Sequence number for incoming RTP      */
367 	int pt_enc;              /**< Payload type for encoding             */
368 	bool rtcp;               /**< Enable RTCP                           */
369 	bool rtcp_mux;           /**< RTP/RTCP multiplex supported by peer  */
370 	bool jbuf_started;       /**< True if jitter-buffer was started     */
371 	stream_rtp_h *rtph;      /**< Stream RTP handler                    */
372 	stream_rtcp_h *rtcph;    /**< Stream RTCP handler                   */
373 	void *arg;               /**< Handler argument                      */
374 	stream_error_h *errorh;  /**< Stream error handler                  */
375 	void *errorh_arg;        /**< Error handler argument                */
376 	struct tmr tmr_rtp;      /**< Timer for detecting RTP timeout       */
377 	uint64_t ts_last;        /**< Timestamp of last received RTP pkt    */
378 	bool terminated;         /**< Stream is terminated flag             */
379 	uint32_t rtp_timeout_ms; /**< RTP Timeout value in [ms]             */
380 	bool rtp_estab;          /**< True if RTP stream is established     */
381 };
382 
383 int  stream_alloc(struct stream **sp, const struct stream_param *prm,
384 		  const struct config_avt *cfg,
385 		  struct call *call, struct sdp_session *sdp_sess,
386 		  const char *name, int label,
387 		  const struct mnat *mnat, struct mnat_sess *mnat_sess,
388 		  const struct menc *menc, struct menc_sess *menc_sess,
389 		  const char *cname,
390 		  stream_rtp_h *rtph, stream_rtcp_h *rtcph, void *arg);
391 struct sdp_media *stream_sdpmedia(const struct stream *s);
392 int  stream_send(struct stream *s, bool ext, bool marker, int pt, uint32_t ts,
393 		 struct mbuf *mb);
394 void stream_update(struct stream *s);
395 void stream_update_encoder(struct stream *s, int pt_enc);
396 int  stream_jbuf_stat(struct re_printf *pf, const struct stream *s);
397 void stream_hold(struct stream *s, bool hold);
398 void stream_set_srate(struct stream *s, uint32_t srate_tx, uint32_t srate_rx);
399 void stream_send_fir(struct stream *s, bool pli);
400 void stream_reset(struct stream *s);
401 void stream_set_bw(struct stream *s, uint32_t bps);
402 void stream_set_error_handler(struct stream *strm,
403 			      stream_error_h *errorh, void *arg);
404 int  stream_debug(struct re_printf *pf, const struct stream *s);
405 int  stream_print(struct re_printf *pf, const struct stream *s);
406 void stream_enable_rtp_timeout(struct stream *strm, uint32_t timeout_ms);
407 
408 
409 /*
410  * User-Agent
411  */
412 
413 struct ua;
414 
415 void         ua_event(struct ua *ua, enum ua_event ev, struct call *call,
416 		      const char *fmt, ...);
417 void         ua_printf(const struct ua *ua, const char *fmt, ...);
418 
419 struct tls  *uag_tls(void);
420 const char  *uag_allowed_methods(void);
421 
422 
423 /*
424  * Video Display
425  */
426 
427 struct vidisp {
428 	struct le        le;
429 	const char      *name;
430 	vidisp_alloc_h  *alloch;
431 	vidisp_update_h *updateh;
432 	vidisp_disp_h   *disph;
433 	vidisp_hide_h   *hideh;
434 };
435 
436 struct vidisp *vidisp_get(struct vidisp_st *st);
437 
438 
439 /*
440  * Video Source
441  */
442 
443 struct vidsrc {
444 	struct le         le;
445 	const char       *name;
446 	vidsrc_alloc_h   *alloch;
447 	vidsrc_update_h  *updateh;
448 };
449 
450 struct vidsrc *vidsrc_get(struct vidsrc_st *st);
451 
452 
453 /*
454  * Video Stream
455  */
456 
457 struct video;
458 
459 typedef void (video_err_h)(int err, const char *str, void *arg);
460 
461 int  video_alloc(struct video **vp, const struct stream_param *stream_prm,
462 		 const struct config *cfg,
463 		 struct call *call, struct sdp_session *sdp_sess, int label,
464 		 const struct mnat *mnat, struct mnat_sess *mnat_sess,
465 		 const struct menc *menc, struct menc_sess *menc_sess,
466 		 const char *content, const struct list *vidcodecl,
467 		 video_err_h *errh, void *arg);
468 int  video_start(struct video *v, const char *peer);
469 void video_stop(struct video *v);
470 bool video_is_started(const struct video *v);
471 int  video_encoder_set(struct video *v, struct vidcodec *vc,
472 		       int pt_tx, const char *params);
473 int  video_decoder_set(struct video *v, struct vidcodec *vc, int pt_rx,
474 		       const char *fmtp);
475 void video_update_picture(struct video *v);
476 void video_sdp_attr_decode(struct video *v);
477 int  video_print(struct re_printf *pf, const struct video *v);
478 
479 
480 /*
481  * Timestamp helpers
482  */
483 
484 
485 /**
486  * This struct is used to keep track of timestamps for
487  * incoming RTP packets.
488  */
489 struct timestamp_recv {
490 	uint32_t first;
491 	uint32_t last;
492 	bool is_set;
493 	unsigned num_wraps;
494 };
495 
496 
calc_extended_timestamp(uint32_t num_wraps,uint32_t ts)497 static inline uint64_t calc_extended_timestamp(uint32_t num_wraps, uint32_t ts)
498 {
499 	uint64_t ext_ts;
500 
501 	ext_ts  = (uint64_t)num_wraps * 0x100000000ULL;
502 	ext_ts += (uint64_t)ts;
503 
504 	return ext_ts;
505 }
506 
507 
timestamp_duration(const struct timestamp_recv * ts)508 static inline uint64_t timestamp_duration(const struct timestamp_recv *ts)
509 {
510 	uint64_t last_ext;
511 
512 	if (!ts || !ts->is_set)
513 		return 0;
514 
515 	last_ext = calc_extended_timestamp(ts->num_wraps, ts->last);
516 
517 	return last_ext - ts->first;
518 }
519 
520 
521 /*
522  *  -1  backwards wrap-around
523  *   0  no wrap-around
524  *   1  forward wrap-around
525  */
timestamp_wrap(uint32_t ts_new,uint32_t ts_old)526 static inline int timestamp_wrap(uint32_t ts_new, uint32_t ts_old)
527 {
528 	int32_t delta;
529 
530 	if (ts_new < ts_old) {
531 
532 		delta = (int32_t)ts_new - (int32_t)ts_old;
533 
534 		if (delta > 0)
535 			return 1;
536 	}
537 	else if ((int32_t)(ts_old - ts_new) > 0) {
538 
539 		return -1;
540 	}
541 
542 	return 0;
543 }
544 
545 
546 /*
547  * Timer
548  */
549 
550 uint64_t tmr_jiffies_usec(void);
551