struct iax_event {
int etype; /* Type of event */
int subclass; /* Subclass data (event specific) */
- unsigned int ts; /* Timestamp */
+ time_in_ms_t ts; /* Timestamp */
struct iax_session *session; /* Applicable session */
int datalen; /* Length of raw data */
struct iax_ies ies; /* IE's for IAX2 frames */
extern int iax_get_fd(void);
/* Find out how many milliseconds until the next scheduled event */
-extern int iax_time_to_next_event(void);
+extern time_in_ms_t iax_time_to_next_event(void);
/* Generate a new IAX session */
extern struct iax_session *iax_session_new(void);
extern int iax_setup_transfer(struct iax_session *s0, struct iax_session *s1);
struct iax_netstat {
- int jitter;
+ time_in_ms_t jitter;
int losspct;
int losscnt;
int packets;
- int delay;
+ time_in_ms_t delay;
int dropped;
int ooo;
};
/* fills in rtt, and an iax_netstat structure for each of local/remote directions of call */
-extern int iax_get_netstats(struct iax_session *s, int *rtt, struct iax_netstat *local, struct iax_netstat *remote);
+extern int iax_get_netstats(struct iax_session *s, time_in_ms_t *rtt, struct iax_netstat *local, struct iax_netstat *remote);
extern void iax_set_private(struct iax_session *s, void *pvt);
#include <arpa/inet.h>
#include <time.h>
-#endif
-#ifdef NEWJB
-#include "jitterbuf.h"
#endif
-typedef long long time_in_ms_t;
-
+#include "iax2.h"
#include "iax-client.h"
#include "md5.h"
+#ifdef NEWJB
+#include "jitterbuf.h"
+#endif
+
/*
work around jitter-buffer shrinking in asterisk:
channels/chan_iax2.c:schedule_delivery() shrinks jitter buffer by 2.
/* Per session capability */
int capability;
/* Last received timestamp */
- unsigned int last_ts;
+ time_in_ms_t last_ts;
/* Last transmitted timestamp */
- unsigned int lastsent;
+ time_in_ms_t lastsent;
/* Last transmitted voice timestamp */
unsigned int lastvoicets;
/* Next predicted voice ts */
- unsigned int nextpred;
+ time_in_ms_t nextpred;
/* True if the last voice we transmitted was not silence/CNG */
int notsilenttx;
/* Our last measured ping time */
- unsigned int pingtime;
+ time_in_ms_t pingtime;
/* Address of peer */
struct sockaddr_in peeraddr;
/* Our call number */
return (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) || (sin1->sin_port != sin2->sin_port);
}
-static int iax_sched_add(struct iax_event *event, struct iax_frame *frame, sched_func func, void *arg, int ms)
+static int iax_sched_add(struct iax_event *event, struct iax_frame *frame, sched_func func, void *arg, time_in_ms_t ms)
{
/* Schedule event to be delivered to the client
}
-int iax_time_to_next_event(void)
+time_in_ms_t iax_time_to_next_event(void)
{
struct iax_sched *cur = schedq;
- int min = 999999999;
+ time_in_ms_t minimum = 999999999;
/* If there are no pending events, we don't need to timeout */
if (!cur)
return -1;
while(cur) {
- if (cur->when < min) {
- min = cur->when;
+ if (cur->when < minimum) {
+ minimum = cur->when;
}
cur = cur->next;
}
- if (min <= 0) {
+ if (minimum <= 0) {
return -1;
}
- return (int) (min - current_time_in_ms());
+ return minimum - current_time_in_ms();
}
struct iax_session *iax_session_new(void)
return 0;
}
-int iax_get_netstats(struct iax_session *session, int *rtt, struct iax_netstat *local, struct iax_netstat *remote) {
+int iax_get_netstats(struct iax_session *session, time_in_ms_t *rtt, struct iax_netstat *local, struct iax_netstat *remote) {
if(!iax_session_valid(session)) return -1;
return 0;
}
-static int calc_timestamp(struct iax_session *session, unsigned int ts, struct ast_frame *f)
+static time_in_ms_t calc_timestamp(struct iax_session *session, time_in_ms_t ts, struct ast_frame *f)
{
- unsigned int ms;
+ time_in_ms_t ms;
time_in_ms_t time_in_ms;
int voice = 0;
int genuine = 0;
/* If we haven't most recently sent silence, and we're
* close in time, use predicted time */
- if(session->notsilenttx && abs(ms - session->nextpred) <= 240) {
+ if(session->notsilenttx && iax_abs(ms - session->nextpred) <= 240) {
/* Adjust our txcore, keeping voice and non-voice
* synchronized */
session->offset += (int)(ms - session->nextpred)/10;
* time. Also, round ms to the next multiple of
* frame size (so our silent periods are multiples
* of frame size too) */
- int diff = ms % (f->samples / 8);
+ time_in_ms_t diff = ms % (f->samples / 8);
if(diff)
ms += f->samples/8 - diff;
session->nextpred = ms;
if (genuine) {
if (ms <= session->lastsent)
ms = session->lastsent + 3;
- } else if (abs(ms - session->lastsent) <= 240) {
+ } else if (iax_abs(ms - session->lastsent) <= 240) {
ms = session->lastsent + 3;
}
return power | IAX_FLAG_SC_LOG;
}
-static int iax_send(struct iax_session *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
+static int iax_send(struct iax_session *pvt, struct ast_frame *f, time_in_ms_t ts, int seqno, int now, int transfer, int final)
{
/* Queue a packet for delivery on a given private structure. Use "ts" for
timestamp, or calculate if ts is 0. Send immediately without retransmission
struct iax_frame *fr;
int res;
int sendmini=0;
- unsigned int lastsent;
- unsigned int fts;
+ time_in_ms_t lastsent;
+ time_in_ms_t fts;
if (!pvt) {
IAXERROR "No private structure for packet?\n");
fr->iseqno = pvt->iseqno;
fh = (struct ast_iax2_full_hdr *)(((char *)fr->af.data) - sizeof(struct ast_iax2_full_hdr));
fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
- fh->ts = htonl(fr->ts);
+ fh->ts = htonl((u_long)(fr->ts));
fh->oseqno = fr->oseqno;
if (transfer) {
fh->iseqno = 0;
/* Mini frame will do */
mh = (struct ast_iax2_mini_hdr *)(((char *)fr->af.data) - sizeof(struct ast_iax2_mini_hdr));
mh->callno = htons(fr->callno);
- mh->ts = htons(fr->ts & 0xFFFF);
+ mh->ts = htons((u_short)(fr->ts & 0xFFFF));
fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
fr->data = mh;
fr->retries = -1;
}
#endif
-static int __send_command(struct iax_session *i, char type, int command, unsigned int ts, unsigned char *data, int datalen, int seqno,
+static int __send_command(struct iax_session *i, char type, int command, time_in_ms_t ts, unsigned char *data, int datalen, int seqno,
int now, int transfer, int final, int samples)
{
struct ast_frame f;
return iax_send(i, &f, ts, seqno, now, transfer, final);
}
-static int send_command(struct iax_session *i, char type, int command, unsigned int ts, unsigned char *data, int datalen, int seqno)
+static int send_command(struct iax_session *i, char type, int command, time_in_ms_t ts, unsigned char *data, int datalen, int seqno)
{
return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 0, 0);
}
}
}
-static int iax_send_lagrp(struct iax_session *session, unsigned int ts);
-static int iax_send_pong(struct iax_session *session, unsigned int ts);
+static int iax_send_lagrp(struct iax_session *session, time_in_ms_t ts);
+static int iax_send_pong(struct iax_session *session, time_in_ms_t ts);
static struct iax_event *handle_event(struct iax_event *event)
{
return send_command(session, AST_FRAME_HTML, AST_HTML_LINKREJECT, 0, NULL, 0, -1);
}
-static int iax_send_pong(struct iax_session *session, unsigned int ts)
+static int iax_send_pong(struct iax_session *session, time_in_ms_t ts)
{
struct iax_ie_data ied;
jb_info stats;
jb_getinfo(session->jb, &stats);
- iax_ie_append_int(&ied,IAX_IE_RR_JITTER, stats.jitter);
+ iax_ie_append_int(&ied,IAX_IE_RR_JITTER, (int)stats.jitter);
/* XXX: should be short-term loss pct.. */
if(stats.frames_in == 0) stats.frames_in = 1;
iax_ie_append_int(&ied,IAX_IE_RR_LOSS,
return;
}
-static int iax_send_lagrp(struct iax_session *session, unsigned int ts)
+static int iax_send_lagrp(struct iax_session *session, time_in_ms_t ts)
{
return send_command(session, AST_FRAME_IAX, IAX_COMMAND_LAGRP, ts, NULL, 0, -1);
}
return res;
}
-static int calc_rxstamp(struct iax_session *session)
+static time_in_ms_t calc_rxstamp(struct iax_session *session)
{
time_in_ms_t time_in_ms;
/* From chan_iax2/steve davies: need to get permission from steve or digium, I guess */
static time_in_ms_t unwrap_timestamp(time_in_ms_t ts, time_in_ms_t last)
{
- int x;
+ time_in_ms_t x;
if ( (ts & 0xFFFF0000) == (last & 0xFFFF0000) ) {
x = ts - last;
#endif
-static struct iax_event *schedule_delivery(struct iax_event *e, unsigned int ts, int updatehistory)
+static struct iax_event *schedule_delivery(struct iax_event *e, time_in_ms_t ts, int updatehistory)
{
/*
* This is the core of the IAX jitterbuffer delivery mechanism:
struct iax_sched *sch;
unsigned int ts;
int subclass = uncompress_subclass(fh->csub);
- int nowts;
+ time_in_ms_t nowts;
int updatehistory = 1;
ts = ntohl(fh->ts);
/* don't run last_ts backwards; i.e. for retransmits and the like */
int datalen)
{
struct iax_event *e;
- unsigned int ts;
+ time_in_ms_t ts;
int updatehistory = 1;
e = (struct iax_event *)malloc(sizeof(struct iax_event) + datalen);
if (e) {
if (blocking) {
/* Block until there is data if desired */
fd_set fds;
- int nextEventTime;
+ time_in_ms_t nextEventTime;
FD_ZERO(&fds);
FD_SET(netfd, &fds);
{
struct timeval nextEvent;
- nextEvent.tv_sec = nextEventTime / 1000;
- nextEvent.tv_usec = (nextEventTime % 1000) * 1000;
+ nextEvent.tv_sec = (long)(nextEventTime / 1000);
+ nextEvent.tv_usec = (long)((nextEventTime % 1000) * 1000);
select(netfd + 1, &fds, NULL, NULL, &nextEvent);
}
/* How many retries so far? */
int retries;
/* Outgoing relative timestamp (ms) */
- unsigned int ts;
+ time_in_ms_t ts;
/* How long to wait before retrying */
- int retrytime;
+ time_in_ms_t retrytime;
/* Are we received out of order? */
int outoforder;
/* Have we been sent at all yet? */
#ifndef _IAX2_H
#define _IAX2_H
+typedef long long time_in_ms_t;
+#define iax_abs(x) ((x) >= 0 ? (x) : -(x))
+
/* Max version of IAX protocol we support */
#define IAX_PROTO_VERSION 2
* Copyright on this file is disclaimed to Digium for inclusion in Asterisk
*/
+#include "iax2.h"
#include "jitterbuf.h"
#include <stdio.h>
#include <stdlib.h>
}
#endif
-static int history_put(jitterbuf *jb, long ts, long now, long ms)
+static int history_put(jitterbuf *jb, time_in_ms_t ts, time_in_ms_t now, long ms)
{
- long delay = now - (ts - jb->info.resync_offset);
- long threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
- long kicked;
+ time_in_ms_t delay = now - (ts - jb->info.resync_offset);
+ time_in_ms_t threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
+ time_in_ms_t kicked;
/* don't add special/negative times to history */
if (ts <= 0)
/* check for drastic change in delay */
if (jb->info.conf.resync_threshold != -1) {
- if (abs(delay - jb->info.last_delay) > threshold) {
+ if (iax_abs(delay - jb->info.last_delay) > threshold) {
jb->info.cnt_delay_discont++;
if (jb->info.cnt_delay_discont > 3) {
/* resync the jitterbuffer */
i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0;
for(;i<jb->hist_ptr;i++) {
- long toins = jb->history[i % JB_HISTORY_SZ];
+ time_in_ms_t toins = jb->history[i % JB_HISTORY_SZ];
/* if the maxbuf should get this */
if(toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) {
static void history_get(jitterbuf *jb)
{
- long max, min, jitter;
+ time_in_ms_t max, min, jitter;
int index;
int count;
}
/* returns 1 if frame was inserted into head of queue, 0 otherwise */
-static int queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
+static int queue_put(jitterbuf *jb, void *data, int type, long ms, time_in_ms_t ts)
{
jb_frame *frame;
jb_frame *p;
int head = 0;
- long resync_ts = ts - jb->info.resync_offset;
+ time_in_ms_t resync_ts = ts - jb->info.resync_offset;
frame = jb->free;
if(frame) {
return head;
}
-static long queue_next(jitterbuf *jb)
+static time_in_ms_t queue_next(jitterbuf *jb)
{
if(jb->frames) return jb->frames->ts;
else return -1;
}
-static long queue_last(jitterbuf *jb)
+static time_in_ms_t queue_last(jitterbuf *jb)
{
if(jb->frames) return jb->frames->prev->ts;
else return -1;
}
-static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
+static jb_frame *_queue_get(jitterbuf *jb, time_in_ms_t ts, int all)
{
jb_frame *frame;
frame = jb->frames;
return NULL;
}
-static jb_frame *queue_get(jitterbuf *jb, long ts)
+static jb_frame *queue_get(jitterbuf *jb, time_in_ms_t ts)
{
return _queue_get(jb,ts,0);
}
}
#endif
-int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now)
+int jb_put(jitterbuf *jb, void *data, int type, long ms, time_in_ms_t ts, time_in_ms_t now)
{
jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
}
-static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
+static int _jb_get(jitterbuf *jb, jb_frame *frameout, time_in_ms_t now, long interpl)
{
jb_frame *frame;
- long diff;
+ time_in_ms_t diff;
/*if((now - jb_next(jb)) > 2 * jb->info.last_voice_ms) jb_warn("SCHED: %ld", (now - jb_next(jb))); */
/* get jitter info */
}
}
-long jb_next(jitterbuf *jb)
+time_in_ms_t jb_next(jitterbuf *jb)
{
if(jb->info.silence_begin_ts) {
- long next = queue_next(jb);
+ time_in_ms_t next = queue_next(jb);
if(next > 0) {
/* shrink during silence */
if (jb->info.target - jb->info.current < -JB_TARGET_EXTRA)
}
}
-int jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
+int jb_get(jitterbuf *jb, jb_frame *frameout, time_in_ms_t now, long interpl)
{
int ret = _jb_get(jb,frameout,now,interpl);
#if 0
long frames_dropped; /* number of frames dropped (shrinkage) */
long frames_ooo; /* number of frames received out-of-order */
long frames_cur; /* number of frames presently in jb, awaiting delivery.*/
- long jitter; /* jitter measured within current history interval*/
- long min; /* minimum lateness within current history interval */
- long current; /* the present jitterbuffer adjustment */
- long target; /* the target jitterbuffer adjustment */
+ time_in_ms_t jitter; /* jitter measured within current history interval*/
+ time_in_ms_t min; /* minimum lateness within current history interval */
+ time_in_ms_t current; /* the present jitterbuffer adjustment */
+ time_in_ms_t target; /* the target jitterbuffer adjustment */
long losspct; /* recent lost frame percentage (* 1000) */
- long next_voice_ts; /* the ts of the next frame to be read from the jb - in receiver's time */
+ time_in_ms_t next_voice_ts; /* the ts of the next frame to be read from the jb - in receiver's time */
long last_voice_ms; /* the duration of the last voice frame */
- long silence_begin_ts; /* the time of the last CNG frame, when in silence */
- long last_adjustment; /* the time of the last adjustment */
- long last_delay; /* the last now added to history */
+ time_in_ms_t silence_begin_ts; /* the time of the last CNG frame, when in silence */
+ time_in_ms_t last_adjustment; /* the time of the last adjustment */
+ time_in_ms_t last_delay; /* the last now added to history */
long cnt_delay_discont; /* the count of discontinuous delays */
- long resync_offset; /* the amount to offset ts to support resyncs */
+ time_in_ms_t resync_offset; /* the amount to offset ts to support resyncs */
long cnt_contig_interp; /* the number of contiguous interp frames returned */
} jb_info;
typedef struct jb_frame {
void *data; /* the frame data */
- long ts; /* the relative delivery time expected */
+ time_in_ms_t ts; /* the relative delivery time expected */
long ms; /* the time covered by this frame, in sec/8000 */
int type; /* the type of frame */
struct jb_frame *next, *prev;
jb_info info;
/* history */
- long history[JB_HISTORY_SZ]; /* history */
+ time_in_ms_t history[JB_HISTORY_SZ]; /* history */
int hist_ptr; /* points to index in history for next entry */
- long hist_maxbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the max delays (highest first) */
- long hist_minbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the min delays (lowest first) */
+ time_in_ms_t hist_maxbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the max delays (highest first) */
+ time_in_ms_t hist_minbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the min delays (lowest first) */
int hist_maxbuf_valid; /* are the "maxbuf"/minbuf valid? */
* JB_DROP: Drop this frame immediately
* JB_SCHED: Frame added. Call jb_next() to get a new time for the next frame
*/
-extern int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now);
+extern int jb_put(jitterbuf *jb, void *data, int type, long ms, time_in_ms_t ts, time_in_ms_t now);
/* get a frame for time now (receiver's time) return value is one of
* JB_OK: You've got frame!
* JB_INTERP: Please interpolate an interpl-length frame for this time (either we need to grow, or there was a lost frame)
* JB_EMPTY: The jb is empty.
*/
-extern int jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl);
+extern int jb_get(jitterbuf *jb, jb_frame *frame, time_in_ms_t now, long interpl);
/* unconditionally get frames from jitterbuf until empty */
extern int jb_getall(jitterbuf *jb, jb_frame *frameout);
/* when is the next frame due out, in receiver's time (0=EMPTY)
* This value may change as frames are added (esp non-audio frames) */
-extern long jb_next(jitterbuf *jb);
+extern time_in_ms_t jb_next(jitterbuf *jb);
/* get jitterbuf info: only "statistics" may be valid */
extern int jb_getinfo(jitterbuf *jb, jb_info *stats);