ratox

FIFO based tox client
git clone git://git.2f30.org/ratox
Log | Files | Refs | README | LICENSE

commit b10ae96fd0736aafa547d7fdee1be9cd4f5fd941
parent 81bf766c09d5fd0d255960825471e0962203d3fe
Author: sin <sin@2f30.org>
Date:   Wed,  8 Oct 2014 14:03:29 +0100

Rework state management for audio calls

No need to try to keep local state, just use toxav_get_call_state().
We now can auto-accept calls by pre-attaching on the call_out FIFO.

Diffstat:
Mratox.c | 74+++++++++++++++++++++++++-------------------------------------------------
1 file changed, 25 insertions(+), 49 deletions(-)

diff --git a/ratox.c b/ratox.c @@ -160,7 +160,6 @@ struct transfer { struct call { int num; - ToxAvCallState state; uint8_t *frame; uint8_t payload[RTP_PAYLOAD_SIZE]; ssize_t n; @@ -222,8 +221,7 @@ static void cbreqtimeout(void *, int32_t, void *); static void cbpeertimeout(void *, int32_t, void *); static void cbcalltypechange(void *, int32_t, void *); static void cbcalldata(ToxAv *, int32_t, int16_t *, int, void *); -static void cancelrxcall(struct friend *, char *); -static void canceltxcall(struct friend *, char *); +static void cancelcall(struct friend *, char *); static void sendfriendcalldata(struct friend *); static void cbconnstatus(Tox *, int32_t, uint8_t, void *); static void cbfriendmessage(Tox *, int32_t, const uint8_t *, uint16_t, void *); @@ -383,8 +381,6 @@ cbcallinvite(void *av, int32_t cnum, void *udata) ftruncate(f->fd[FCALL_PENDING], 0); lseek(f->fd[FCALL_PENDING], 0, SEEK_SET); dprintf(f->fd[FCALL_PENDING], "1\n"); - - f->av.state = av_CallStarting; } static void @@ -401,8 +397,6 @@ cbcallstarted(void *av, int32_t cnum, void *udata) printout(": %s : Rx AV > Started\n", f->name); toxav_prepare_transmission(toxav, cnum, av_jbufdc, av_VADd, 0); - - f->av.state = av_CallActive; } static void @@ -416,9 +410,7 @@ cbcallended(void *av, int32_t cnum, void *udata) if (!f) return; - cancelrxcall(f, "Ended"); - canceltxcall(f, "Ended"); - toxav_kill_transmission(toxav, cnum); + cancelcall(f, "Ended"); } static void @@ -432,9 +424,7 @@ cbcallcancelled(void *av, int32_t cnum, void *udata) if (!f) return; - cancelrxcall(f, "Cancelled"); - canceltxcall(f, "Cancelled"); - toxav_kill_transmission(toxav, cnum); + cancelcall(f, "Cancelled"); } static void @@ -448,9 +438,7 @@ cbcallrejected(void *av, int32_t cnum, void *udata) if (!f) return; - canceltxcall(f, "Rejected"); - cancelrxcall(f, "Rejected"); - toxav_kill_transmission(toxav, cnum); + cancelcall(f, "Rejected"); } static void @@ -469,7 +457,6 @@ preparetxcall(struct friend *f) f->av.incompleteframe = 0; f->av.lastsent.tv_sec = 0; f->av.lastsent.tv_nsec = 0; - f->av.state = av_CallActive; } static void @@ -499,9 +486,7 @@ cbcallending(void *av, int32_t cnum, void *udata) if (!f) return; - cancelrxcall(f, "Ending"); - canceltxcall(f, "Ending"); - toxav_kill_transmission(toxav, cnum); + cancelcall(f, "Ending"); } static void @@ -514,9 +499,7 @@ cbreqtimeout(void *av, int32_t cnum, void *udata) break; if (!f) return; - cancelrxcall(f, "Request timeout"); - canceltxcall(f, "Request timeout"); - toxav_kill_transmission(toxav, cnum); + cancelcall(f, "Request timeout"); } static void @@ -529,9 +512,8 @@ cbpeertimeout(void *av, int32_t cnum, void *udata) break; if (!f) return; - cancelrxcall(f, "Peer timeout"); - canceltxcall(f, "Peer timeout"); - toxav_kill_transmission(toxav, cnum); + toxav_stop_call(toxav, cnum); + cancelcall(f, "Peer timeout"); } static void @@ -575,11 +557,15 @@ cbcalldata(ToxAv *av, int32_t cnum, int16_t *data, int len, void *udata) } static void -cancelrxcall(struct friend *f, char *action) +cancelcall(struct friend *f, char *action) { - printout(": %s : Rx AV > %s\n", f->name, action); - f->av.state = av_CallNonExistant; + printout(": %s : Rx/Tx AV > %s\n", f->name, action); + + if (f->av.num != -1) + toxav_kill_transmission(toxav, f->av.num); f->av.num = -1; + + /* Cancel Tx side of the call */ if (f->fd[FCALL_OUT] != -1) { close(f->fd[FCALL_OUT]); f->fd[FCALL_OUT] = -1; @@ -587,14 +573,8 @@ cancelrxcall(struct friend *f, char *action) ftruncate(f->fd[FCALL_PENDING], 0); lseek(f->fd[FCALL_PENDING], 0, SEEK_SET); dprintf(f->fd[FCALL_PENDING], "0\n"); -} -static void -canceltxcall(struct friend *f, char *action) -{ - printout(": %s : Tx AV > %s\n", f->name, action); - f->av.state = av_CallNonExistant; - f->av.num = -1; + /* Cancel Rx side of the call */ free(f->av.frame); f->av.frame = NULL; fiforeset(f->dirfd, &f->fd[FCALL_IN], ffiles[FCALL_IN]); @@ -614,7 +594,6 @@ sendfriendcalldata(struct friend *f) framesize * sizeof(int16_t) - f->av.incompleteframe * f->av.n); if (n == 0) { toxav_hangup(toxav, f->av.num); - f->av.state = av_CallNonExistant; return; } else if (n == -1) { return; @@ -1392,7 +1371,6 @@ friendcreate(int32_t frnum) ftruncate(f->fd[FCALL_PENDING], 0); dprintf(f->fd[FCALL_PENDING], "0\n"); - f->av.state = av_CallNonExistant; f->av.num = -1; TAILQ_INSERT_TAIL(&friendhead, f, entry); @@ -1407,11 +1385,8 @@ frienddestroy(struct friend *f) canceltxtransfer(f); cancelrxtransfer(f); - if (f->av.state != av_CallNonExistant) { - cancelrxcall(f, "Destroying"); - canceltxcall(f, "Destroying"); - toxav_kill_transmission(toxav, f->av.num); - } + if (toxav_get_call_state(toxav, f->av.num) != av_CallNonExistant) + cancelcall(f, "Destroying"); for (i = 0; i < LEN(ffiles); i++) { if (f->dirfd != -1) { unlinkat(f->dirfd, ffiles[i].name, 0); @@ -1678,8 +1653,8 @@ loop(void) if (f->fd[FFILE_IN] > fdmax) fdmax = f->fd[FFILE_IN]; } - if (f->av.state == av_CallNonExistant || - f->av.state == av_CallActive) { + if (f->av.num == -1 || + toxav_get_call_state(toxav, f->av.num) == av_CallActive) { FD_SET(f->fd[FCALL_IN], &rfds); if (f->fd[FCALL_IN] > fdmax) fdmax = f->fd[FCALL_IN]; @@ -1763,12 +1738,14 @@ loop(void) if (r < 0) { if (errno != ENXIO) eprintf("openat %s:", ffiles[FCALL_OUT].name); + continue; } else { f->fd[FCALL_OUT] = r; - if (f->av.state == av_CallStarting) - toxav_answer(toxav, f->av.num, &toxavconfig); } } + if (f->av.num != -1) + if (toxav_get_call_state(toxav, f->av.num) == av_CallStarting) + toxav_answer(toxav, f->av.num, &toxavconfig); } if (n == 0) @@ -1827,10 +1804,9 @@ loop(void) } } if (FD_ISSET(f->fd[FCALL_IN], &rfds)) { - switch (f->av.state) { + switch (toxav_get_call_state(toxav, f->av.num)) { case av_CallNonExistant: toxav_call(toxav, &f->av.num, f->num, &toxavconfig, RINGINGDELAY); - f->av.state = av_CallInviting; printout(": %s : Tx AV > Inviting\n", f->name); break; case av_CallActive: