dtchat: add the call state machine (T6.4)

The call now has IDLE, RINGING, IN_CALL and ENDED with dc_call_ring/accept/hangup/tick: RING and ACCEPT cross the wire as sealed control packets, an incoming RING during a call is answered BUSY, an unanswered ring ends after 30 s (dc_call_tick), and audio/PTT are refused outside IN_CALL. An authenticated packet also updates the peer address, so an incoming call is answered on the address it came from (no manual peer needed). test_media_call: audio refused before answer, ring seen on the other side, accept on both, PTT and ten audio frames in the call, hangup ending both sides and stopping audio; standalone and ASan/UBSan clean. Also widened the media packet type range to DC_MEDIA_HANGUP (the PTT fix was incomplete). In-tree build pending: make mcde-linux MCDE_DIRS=install is still running.
This commit is contained in:
lotherk committed 2026-11-17 07:58:06 +00:00
1 parent 51e884563a
commit 5179969bc9
4 files changed
+164 -10

No files matched your search

+21 -1
View File
@@ -368,7 +368,11 @@ enum {
DC_MEDIA_VIDEO = 2,
DC_MEDIA_PING = 3,
DC_MEDIA_BYE = 4,
DC_MEDIA_PTT = 5
DC_MEDIA_PTT = 5,
DC_MEDIA_RING = 6,
DC_MEDIA_ACCEPT = 7,
DC_MEDIA_BUSY = 8,
DC_MEDIA_HANGUP = 9
};
typedef struct {
@@ -438,12 +442,28 @@ int dc_jitter_get(DcJitter *jitter, uint32_t *ts, unsigned char *out,
*/
typedef struct DcMediaCall DcMediaCall;
typedef enum {
DC_CALL_IDLE = 0,
DC_CALL_RINGING,
DC_CALL_IN_CALL,
DC_CALL_ENDED
} DcCallState;
DcMediaCall *dc_call_new(const char *peer_host, int peer_port,
const DcMediaSend *send, const DcMediaRecv *recv, char *err,
size_t errlen);
int dc_call_fd(const DcMediaCall *call);
int dc_call_local_port(DcMediaCall *call);
int dc_call_talking(const DcMediaCall *call);
/* ring, answer, hang up and the state; a peer that rings during a call is
* told BUSY, an unanswered ring ends after DC_CALL_RING_MS */
#define DC_CALL_RING_MS 30000
DcCallState dc_call_state(const DcMediaCall *call);
int dc_call_ring(DcMediaCall *call);
int dc_call_accept(DcMediaCall *call);
int dc_call_hangup(DcMediaCall *call);
/* call from the loop: ends an unanswered ring */
void dc_call_tick(DcMediaCall *call);
int dc_call_send_ptt(DcMediaCall *call, int on);
int dc_call_drain(DcMediaCall *call);
int dc_call_audio_in(DcMediaCall *call, const int16_t *pcm);
+115 -5
View File
@@ -58,7 +58,9 @@ struct DcMediaCall {
DcJitter *jitter;
uint32_t frames;
int talking;
uint32_t last_in;
long long last_in;
DcCallState state;
long long state_since;
};
DcJitter *
@@ -226,6 +228,8 @@ dc_call_new(const char *peer_host, int peer_port, const DcMediaSend *send,
call->jitter = dc_jitter_new(3, JITTER_SLOTS);
call->snd = *send;
call->rcv = *recv;
call->state = DC_CALL_IDLE;
call->state_since = dc_loop_now_ms();
if (call->fd < 0 || call->enc == NULL || call->dec == NULL ||
call->jitter == NULL) {
if (err != NULL && errlen > 0 && err[0] == '\0')
@@ -275,7 +279,7 @@ dc_call_audio_in(DcMediaCall *call, const int16_t *pcm)
int n;
size_t len;
if (call == NULL || pcm == NULL)
if (call == NULL || pcm == NULL || call->state != DC_CALL_IN_CALL)
return -1;
n = dc_opus_enc_frame(call->enc, pcm, payload, sizeof(payload));
if (n <= 0)
@@ -299,7 +303,7 @@ dc_call_send_ptt(DcMediaCall *call, int on)
unsigned char payload = (unsigned char)(on ? 1 : 0);
size_t len;
if (call == NULL)
if (call == NULL || call->state != DC_CALL_IN_CALL)
return -1;
call->talking = on ? 1 : 0;
len = dc_media_seal(&call->snd, DC_MEDIA_PTT, call->frames *
@@ -312,6 +316,87 @@ dc_call_send_ptt(DcMediaCall *call, int on)
return 0;
}
/* one control packet: no payload */
static int
send_control(DcMediaCall *call, int type)
{
unsigned char packet[64];
size_t len;
len = dc_media_seal(&call->snd, type,
call->frames * (uint32_t)DC_AUDIO_FRAME_MS, NULL, 0, packet,
sizeof(packet));
if (len == 0)
return -1;
if (sendto(call->fd, packet, len, 0, (struct sockaddr *)&call->peer,
sizeof(call->peer)) != (ssize_t)len)
return -1;
return 0;
}
static void
set_state(DcMediaCall *call, DcCallState state)
{
call->state = state;
call->state_since = dc_loop_now_ms();
}
DcCallState
dc_call_state(const DcMediaCall *call)
{
return call != NULL ? call->state : DC_CALL_IDLE;
}
int
dc_call_ring(DcMediaCall *call)
{
if (call == NULL || call->state == DC_CALL_IN_CALL ||
call->state == DC_CALL_ENDED)
return -1;
if (send_control(call, DC_MEDIA_RING) != 0)
return -1;
set_state(call, DC_CALL_RINGING);
return 0;
}
int
dc_call_accept(DcMediaCall *call)
{
if (call == NULL ||
(call->state != DC_CALL_RINGING && call->state != DC_CALL_IDLE))
return -1;
if (send_control(call, DC_MEDIA_ACCEPT) != 0)
return -1;
set_state(call, DC_CALL_IN_CALL);
return 0;
}
int
dc_call_hangup(DcMediaCall *call)
{
if (call == NULL || call->state == DC_CALL_ENDED)
return -1;
(void)send_control(call, DC_MEDIA_HANGUP);
set_state(call, DC_CALL_ENDED);
call->talking = 0;
return 0;
}
void
dc_call_tick(DcMediaCall *call)
{
long long now;
if (call == NULL)
return;
now = dc_loop_now_ms();
if (call->state == DC_CALL_RINGING &&
now - call->state_since > DC_CALL_RING_MS) {
(void)send_control(call, DC_MEDIA_HANGUP);
set_state(call, DC_CALL_ENDED);
}
}
int
dc_call_drain(DcMediaCall *call)
{
@@ -322,7 +407,10 @@ dc_call_drain(DcMediaCall *call)
if (call == NULL)
return -1;
for (;;) {
ssize_t n = recv(call->fd, packet, sizeof(packet), 0);
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
ssize_t n = recvfrom(call->fd, packet, sizeof(packet), 0,
(struct sockaddr *)&from, &fromlen);
int type = 0;
uint32_t ts = 0;
size_t len = 0;
@@ -337,12 +425,34 @@ dc_call_drain(DcMediaCall *call)
if (dc_media_open(&call->rcv, packet, (size_t)n, &type, &ts,
plain, &len) != 1)
continue;
call->last_in = (uint32_t)time(NULL);
/* an authenticated packet says where the peer is: answer an
* incoming call on the address it came from */
call->peer = from;
call->last_in = dc_loop_now_ms();
if (type == DC_MEDIA_PTT) {
if (len == 1)
call->talking = plain[0] ? 1 : 0;
continue;
}
if (type == DC_MEDIA_RING) {
if (call->state == DC_CALL_IN_CALL)
(void)send_control(call, DC_MEDIA_BUSY);
else if (call->state == DC_CALL_IDLE ||
call->state == DC_CALL_ENDED)
set_state(call, DC_CALL_RINGING);
continue;
}
if (type == DC_MEDIA_ACCEPT) {
if (call->state == DC_CALL_RINGING ||
call->state == DC_CALL_IDLE)
set_state(call, DC_CALL_IN_CALL);
continue;
}
if (type == DC_MEDIA_BUSY || type == DC_MEDIA_HANGUP) {
set_state(call, DC_CALL_ENDED);
call->talking = 0;
continue;
}
if (type == DC_MEDIA_BYE)
continue;
if (type != DC_MEDIA_AUDIO)
+2 -2
View File
@@ -211,7 +211,7 @@ dc_media_seal(DcMediaSend *send, int type, uint32_t ts, const void *plain,
if (send == NULL || out == NULL ||
(len > 0 && plain == NULL) || len > DC_MEDIA_MAX_PAYLOAD)
return 0;
if (type < DC_MEDIA_AUDIO || type > DC_MEDIA_PTT)
if (type < DC_MEDIA_AUDIO || type > DC_MEDIA_HANGUP)
return 0;
need = HDR_LEN + len + DC_MEDIA_TAG_LEN;
if (outlen < need)
@@ -271,7 +271,7 @@ dc_media_open(DcMediaRecv *recv, const unsigned char *packet, size_t len,
return -1;
if (packet[0] != DC_MEDIA_VERSION || packet[2] != 0 || packet[3] != 0)
return -1;
if (packet[1] < DC_MEDIA_AUDIO || packet[1] > DC_MEDIA_PTT)
if (packet[1] < DC_MEDIA_AUDIO || packet[1] > DC_MEDIA_HANGUP)
return -1;
seq = get_u32(packet + 4);
paylen = len - HDR_LEN - DC_MEDIA_TAG_LEN;
+26 -2
View File
@@ -180,10 +180,10 @@ test_call(void)
CHECK(dc_media_keypair(pub_b, priv_b) == 0);
CHECK(dc_media_keys_agree(priv_a, pub_b, 1, &ka) == 0);
CHECK(dc_media_keys_agree(priv_b, pub_a, 0, &kb) == 0);
dc_media_send_init(&snd, ka.send);
dc_media_send_init(&snd, kb.send);
dc_media_recv_init(&rcv, kb.recv);
/* B first: it needs its port for A. B never sends in this test. */
/* B first: it needs its port for A; its peer is set by the RING. */
b = dc_call_new("127.0.0.1", 9, NULL, NULL, err, sizeof(err));
CHECK(b == NULL); /* NULL keys are refused */
b = dc_call_new("127.0.0.1", 9, &snd, &rcv, err, sizeof(err));
@@ -209,6 +209,22 @@ test_call(void)
CHECK(dc_call_fd(a) != dc_call_fd(b));
CHECK(dc_call_talking(a) == 0);
/* no audio before the call is answered */
fill_sine(pcm, 0);
CHECK(dc_call_audio_in(a, pcm) < 0);
/* ring, see it on the other side, accept, both in the call */
CHECK(dc_call_state(a) == DC_CALL_IDLE);
CHECK(dc_call_ring(a) == 0);
CHECK(dc_call_state(a) == DC_CALL_RINGING);
(void)dc_call_drain(b);
CHECK(dc_call_state(b) == DC_CALL_RINGING);
CHECK(dc_call_audio_in(a, pcm) < 0);
CHECK(dc_call_accept(b) == 0);
(void)dc_call_drain(a);
CHECK(dc_call_state(a) == DC_CALL_IN_CALL);
CHECK(dc_call_state(b) == DC_CALL_IN_CALL);
/* push-to-talk reaches the other side */
CHECK(dc_call_send_ptt(a, 1) == 0);
(void)dc_call_drain(b);
@@ -248,6 +264,14 @@ test_call(void)
CHECK(dc_call_drain(b) == 0);
CHECK(dc_call_audio_out(b, pcm) == 0);
/* hanging up ends the call on both sides and stops the audio */
CHECK(dc_call_hangup(a) == 0);
CHECK(dc_call_state(a) == DC_CALL_ENDED);
(void)dc_call_drain(b);
CHECK(dc_call_state(b) == DC_CALL_ENDED);
fill_sine(pcm, 0);
CHECK(dc_call_audio_in(a, pcm) < 0);
dc_call_free(a);
dc_call_free(b);
}