From ea31073ce00ad10a3c1ee756b80f457a35eb79e2 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 17 Nov 2026 08:49:25 +0000 Subject: [PATCH] dtchat: exchange media keys and wire live calls into the UI (T6.5) The local protocol gets media-key: each side sends its ephemeral X25519 public key (base64) and UDP port over the ready text connection; both derive the direction keys with HKDF-SHA256, the caller is the initiator. localui sends the key on Conversation > Call, answers an offer with its own key, creates the call with dc_call_new_bound on the listener port (TLS and media share the number), rings, and opens the call window; an incoming ring enables Answer. The Xt loop pumps the call: drain, Opus decode to the playback stream, capture and encode only while our own PTT is held, video frames to the window, ring timeout. dc_local_conn_remote_addr gives the numeric peer address; dc_call_new_bound binds the announced port. dc_call_talking() is now only our own key, dc_call_peer_talking() the peer's (a received PTT used to start our microphone too). The call window has an Answer button and lays the buttons above the video so they stay visible. --call-first and DTCHAT_TEST_TRUST/DTCHAT_TEST_AUTOANSWER/DTCHAT_TEST_PTT drive the test: two UI instances auto-paired, exchanged media-key, rang, answered, reached IN_CALL and carried 200 Opus frames caller->callee with the callee silent (audio out 200 on the receiver, audio in 200 on the sender); screenshot build/mcde-shots/dtchat-t65-live.png. The smoke still passes; test_media_call uses peer_talking; test_media_packet covers the base64 key exchange. The test switch is documented in the security review. --- lib/libDtChat/dc_internal.h | 13 ++ lib/libDtChat/local.c | 15 ++ lib/libDtChat/media_call.c | 21 +- lib/libDtChat/media_packet.c | 37 ++++ lib/libDtChat/test_media_call.c | 5 +- lib/libDtChat/test_media_packet.c | 38 ++++ programs/dtchat/Makefile.am | 3 +- programs/dtchat/localui.c | 348 ++++++++++++++++++++++++++++++ programs/dtchat/localui.h | 2 + programs/dtchat/main.c | 28 +++ programs/dtchat/ui_call.c | 119 +++++++--- programs/dtchat/ui_call.h | 3 + programs/dtchat/ui_main.c | 34 ++- programs/dtchat/ui_main.h | 4 + 14 files changed, 631 insertions(+), 39 deletions(-) diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index d0c27a1b..fef20e01 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -254,6 +254,10 @@ int dc_local_send_typing(DcLocalConn *conn, int on); void dc_local_conn_close(DcLocalConn *conn); const char *dc_local_conn_peer_id(const DcLocalConn *conn); const char *dc_local_conn_peer_pin(const DcLocalConn *conn); +/* the numeric address the connection comes from ("127.0.0.1"), for the media + * stream a call opens to that peer; 0 on success */ +int dc_local_conn_remote_addr(const DcLocalConn *conn, char *host, + size_t len); /* * A client link with reconnect: 2, 4, 8 ... 60 s backoff, an online/offline @@ -399,6 +403,13 @@ int dc_media_keypair(unsigned char pub[DC_MEDIA_KEY_LEN], int dc_media_keys_agree(const unsigned char priv[DC_MEDIA_KEY_LEN], const unsigned char peer_pub[DC_MEDIA_KEY_LEN], int initiator, DcMediaKeys *keys); +/* the same over the wire: the public key as base64 (43 characters + NUL), + * and the receiver's side of the agreement from that text */ +#define DC_MEDIA_PUB_B64_LEN 45 +int dc_media_keypair_b64(char pub[DC_MEDIA_PUB_B64_LEN], + unsigned char priv[DC_MEDIA_KEY_LEN]); +int dc_media_keys_from_b64(const unsigned char priv[DC_MEDIA_KEY_LEN], + const char *peer_pub_b64, int initiator, DcMediaKeys *keys); void dc_media_send_init(DcMediaSend *send, const unsigned char key[DC_MEDIA_KEY_LEN]); void dc_media_recv_init(DcMediaRecv *recv, @@ -459,7 +470,9 @@ DcMediaCall *dc_call_new_bound(int local_port, const char *peer_host, char *err, size_t errlen); int dc_call_fd(const DcMediaCall *call); int dc_call_local_port(DcMediaCall *call); +/* our own key is down; dc_call_peer_talking() is the peer's last PTT state */ int dc_call_talking(const DcMediaCall *call); +int dc_call_peer_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 diff --git a/lib/libDtChat/local.c b/lib/libDtChat/local.c index 4cc9de6f..f7b43371 100644 --- a/lib/libDtChat/local.c +++ b/lib/libDtChat/local.c @@ -1018,6 +1018,21 @@ dc_local_conn_peer_pin(const DcLocalConn *conn) return conn != NULL ? conn->peer_pin : NULL; } +int +dc_local_conn_remote_addr(const DcLocalConn *conn, char *host, size_t len) +{ + struct sockaddr_in addr; + socklen_t alen = sizeof(addr); + + if (conn == NULL || conn->fd < 0 || host == NULL || len == 0) + return -1; + if (getpeername(conn->fd, (struct sockaddr *)&addr, &alen) != 0) + return -1; + if (inet_ntop(AF_INET, &addr.sin_addr, host, (socklen_t)len) == NULL) + return -1; + return 0; +} + static const char * dc_local_conn_peer_cert_pin(const DcLocalConn *conn) { diff --git a/lib/libDtChat/media_call.c b/lib/libDtChat/media_call.c index d5aa180b..bc7fa8a3 100644 --- a/lib/libDtChat/media_call.c +++ b/lib/libDtChat/media_call.c @@ -61,7 +61,8 @@ struct DcMediaCall { uint32_t video_ts; int video_dropped; uint32_t frames; - int talking; + int local_talking; /* we hold the key */ + int peer_talking; /* the peer told us so */ long long last_in; DcCallState state; long long state_since; @@ -282,7 +283,13 @@ dc_call_local_port(DcMediaCall *call) int dc_call_talking(const DcMediaCall *call) { - return call != NULL ? call->talking : 0; + return call != NULL ? call->local_talking : 0; +} + +int +dc_call_peer_talking(const DcMediaCall *call) +{ + return call != NULL ? call->peer_talking : 0; } int @@ -319,7 +326,7 @@ dc_call_send_ptt(DcMediaCall *call, int on) if (call == NULL || call->state != DC_CALL_IN_CALL) return -1; - call->talking = on ? 1 : 0; + call->local_talking = on ? 1 : 0; len = dc_media_seal(&call->snd, DC_MEDIA_PTT, call->frames * (uint32_t)DC_AUDIO_FRAME_MS, &payload, 1, packet, sizeof(packet)); if (len == 0) @@ -392,7 +399,8 @@ dc_call_hangup(DcMediaCall *call) return -1; (void)send_control(call, DC_MEDIA_HANGUP); set_state(call, DC_CALL_ENDED); - call->talking = 0; + call->local_talking = 0; + call->peer_talking = 0; return 0; } @@ -445,7 +453,7 @@ dc_call_drain(DcMediaCall *call) call->last_in = dc_loop_now_ms(); if (type == DC_MEDIA_PTT) { if (len == 1) - call->talking = plain[0] ? 1 : 0; + call->peer_talking = plain[0] ? 1 : 0; continue; } if (type == DC_MEDIA_RING) { @@ -464,7 +472,8 @@ dc_call_drain(DcMediaCall *call) } if (type == DC_MEDIA_BUSY || type == DC_MEDIA_HANGUP) { set_state(call, DC_CALL_ENDED); - call->talking = 0; + call->local_talking = 0; + call->peer_talking = 0; continue; } if (type == DC_MEDIA_BYE) diff --git a/lib/libDtChat/media_packet.c b/lib/libDtChat/media_packet.c index c5be4f27..b523cc72 100644 --- a/lib/libDtChat/media_packet.c +++ b/lib/libDtChat/media_packet.c @@ -177,6 +177,43 @@ out: return rc; } +int +dc_media_keypair_b64(char pub[DC_MEDIA_PUB_B64_LEN], + unsigned char priv[DC_MEDIA_KEY_LEN]) +{ + unsigned char raw[DC_MEDIA_KEY_LEN]; + + if (pub == NULL || priv == NULL) + return -1; + if (dc_media_keypair(raw, priv) != 0) + return -1; + EVP_EncodeBlock((unsigned char *)pub, raw, DC_MEDIA_KEY_LEN); + return 0; +} + +int +dc_media_keys_from_b64(const unsigned char priv[DC_MEDIA_KEY_LEN], + const char *peer_pub_b64, int initiator, DcMediaKeys *keys) +{ + unsigned char raw[DC_MEDIA_KEY_LEN + 3]; + size_t len; + int n; + + if (priv == NULL || peer_pub_b64 == NULL || keys == NULL) + return -1; + len = strlen(peer_pub_b64); + if (len != 44 || len % 4 != 0) + return -1; + n = EVP_DecodeBlock(raw, (const unsigned char *)peer_pub_b64, (int)len); + if (n < 0) + return -1; + if (n == DC_MEDIA_KEY_LEN + 1 && peer_pub_b64[len - 1] == '=') + n = DC_MEDIA_KEY_LEN; /* the padding byte */ + if (n != DC_MEDIA_KEY_LEN) + return -1; + return dc_media_keys_agree(priv, raw, initiator, keys); +} + void dc_media_send_init(DcMediaSend *send, const unsigned char key[DC_MEDIA_KEY_LEN]) { diff --git a/lib/libDtChat/test_media_call.c b/lib/libDtChat/test_media_call.c index 9861ce0a..dc049f7d 100644 --- a/lib/libDtChat/test_media_call.c +++ b/lib/libDtChat/test_media_call.c @@ -228,10 +228,11 @@ test_call(void) /* push-to-talk reaches the other side */ CHECK(dc_call_send_ptt(a, 1) == 0); (void)dc_call_drain(b); - CHECK(dc_call_talking(b) == 1); + CHECK(dc_call_peer_talking(b) == 1); + CHECK(dc_call_talking(b) == 0); CHECK(dc_call_send_ptt(a, 0) == 0); (void)dc_call_drain(b); - CHECK(dc_call_talking(b) == 0); + CHECK(dc_call_peer_talking(b) == 0); /* ten frames of audio, decoded in order */ for (sent = 0; sent < 10; sent++) { diff --git a/lib/libDtChat/test_media_packet.c b/lib/libDtChat/test_media_packet.c index cb82473e..7a25aadb 100644 --- a/lib/libDtChat/test_media_packet.c +++ b/lib/libDtChat/test_media_packet.c @@ -85,6 +85,43 @@ test_roundtrip(void) CHECK(gotlen == 0); } +static void +test_b64(void) +{ + unsigned char priv_a[32], priv_b[32]; + char b64_a[DC_MEDIA_PUB_B64_LEN], b64_b[DC_MEDIA_PUB_B64_LEN]; + DcMediaKeys ka, kb; + unsigned char packet[128]; + unsigned char plain[16] = "hello"; + unsigned char got[16]; + size_t len, gotlen = 0; + int type; + + CHECK(dc_media_keypair_b64(b64_a, priv_a) == 0); + CHECK(dc_media_keypair_b64(b64_b, priv_b) == 0); + CHECK(strlen(b64_a) == 44); + CHECK(dc_media_keys_from_b64(priv_a, b64_b, 1, &ka) == 0); + CHECK(dc_media_keys_from_b64(priv_b, b64_a, 0, &kb) == 0); + CHECK(memcmp(ka.send, kb.recv, 32) == 0); + CHECK(memcmp(ka.recv, kb.send, 32) == 0); + + { + DcMediaSend snd; + DcMediaRecv rcv; + + dc_media_send_init(&snd, ka.send); + dc_media_recv_init(&rcv, kb.recv); + len = dc_media_seal(&snd, DC_MEDIA_AUDIO, 0, plain, 5, packet, + sizeof(packet)); + CHECK(len > 0); + CHECK(dc_media_open(&rcv, packet, len, &type, NULL, got, + &gotlen) == 1); + CHECK(gotlen == 5 && memcmp(got, plain, 5) == 0); + } + CHECK(dc_media_keys_from_b64(priv_a, "not-base64", 1, &ka) == -1); + CHECK(dc_media_keys_from_b64(priv_a, "", 1, &ka) == -1); +} + static void test_tamper(void) { @@ -283,6 +320,7 @@ int main(void) { test_roundtrip(); + test_b64(); test_tamper(); test_replay(); test_limits(); diff --git a/programs/dtchat/Makefile.am b/programs/dtchat/Makefile.am index 0d308f7a..0b128299 100644 --- a/programs/dtchat/Makefile.am +++ b/programs/dtchat/Makefile.am @@ -12,7 +12,8 @@ dtchat_CPPFLAGS = -I$(top_srcdir)/include \ # The model and the JSON schema helpers live in libDtChat/libDtChatIpc; # DtInitialize/DTCLIENTLIBS come with T2.7. dtchat_LDADD = $(top_builddir)/lib/libDtChat/libDtChat.la \ - $(top_builddir)/lib/libDtChat/libDtChatIpc.la $(XTOOLLIB) + $(top_builddir)/lib/libDtChat/libDtChatIpc.la \ + $(top_builddir)/lib/McdeSys/libMcdeSys.la $(XTOOLLIB) dtchat_SOURCES = main.c ui_main.c ui_accounts.c ui_request.c ui_peer.c \ ui_file.c localui.c ui_call.c demo.c ui_main.h ui_accounts.h \ diff --git a/programs/dtchat/localui.c b/programs/dtchat/localui.c index affb2984..3c74c349 100644 --- a/programs/dtchat/localui.c +++ b/programs/dtchat/localui.c @@ -19,6 +19,8 @@ #include #include +#include + #include #include #include @@ -26,6 +28,7 @@ #include #include "ui_file.h" +#include "ui_call.h" #include "ui_text.h" typedef struct Peer Peer; @@ -42,6 +45,23 @@ struct Peer { char recv_id[64]; int blocked; int paired; + /* the media stream of a call: the X25519 secret and the peer's public + * key (base64) from the media-key message, the call itself and the + * audio device streams */ + unsigned char call_priv[DC_MEDIA_KEY_LEN]; + char call_pub[DC_MEDIA_PUB_B64_LEN]; + char call_peer_pub[DC_MEDIA_PUB_B64_LEN]; + int call_offered; + int call_initiator; + int call_port; + DcMediaCall *call; + DtCallWindow *call_window; + McdeSysPcm *play; + McdeSysPcm *cap; + DcCallState call_state; + unsigned int call_video; + unsigned int audio_in; + unsigned int audio_out; Peer *next; }; @@ -59,6 +79,10 @@ struct DtLocal { XtAppContext app; XtIntervalId timer; int seq; + int port; + int call_debug; + int auto_answer; + int test_trust; }; typedef struct { @@ -498,6 +522,173 @@ file_offer(DtLocal *local, Peer *peer, const DcJson *json) XtPopup(ctx->shell, XtGrabNone); } +/* -------------------------------- calls --------------------------------- */ + +static void +call_debug(DtLocal *local, Peer *peer, const char *text) +{ + if (local->call_debug) + fprintf(stderr, "call %s: %s\n", + peer != NULL ? peer->name : "?", text); +} + +static void +send_media_key(DtLocal *local, Peer *peer, DcLocalConn *conn, int port) +{ + DcJson *json; + + (void)local; + if (conn == NULL) + return; + json = dc_json_new_object(); + (void)dc_json_object_set(json, "type", + dc_json_new_string("media-key")); + (void)dc_json_object_set(json, "pub", + dc_json_new_string(peer->call_pub)); + (void)dc_json_object_set(json, "port", dc_json_new_int(port)); + (void)dc_local_conn_send_json(conn, json); + dc_json_free(json); +} + +static void +call_stop(DtLocal *local, Peer *peer) +{ + (void)local; + if (peer->call != NULL) { + dc_call_hangup(peer->call); + dc_call_free(peer->call); + peer->call = NULL; + } + if (peer->call_window != NULL) { + dtchat_call_window_destroy(peer->call_window); + peer->call_window = NULL; + } + mcde_sys_pcm_close(peer->play); + peer->play = NULL; + mcde_sys_pcm_close(peer->cap); + peer->cap = NULL; + peer->call_state = DC_CALL_ENDED; +} + +static void +call_hangup_cb(void *arg) +{ + Peer *peer = arg; + + call_debug(peer->local, peer, "hung up"); + call_stop(peer->local, peer); + status(peer->local, "call ended"); +} + +static int +start_call(DtLocal *local, Peer *peer, DcLocalConn *conn, int initiator) +{ + DcMediaKeys keys; + DcMediaSend snd; + DcMediaRecv rcv; + char host[128] = "127.0.0.1"; + int port = 0; + char err[256] = ""; + + if (peer->call != NULL) + return 0; + if (!peer->paired) { + status(local, "call refused: peer is not paired"); + return -1; + } + if (dc_media_keys_from_b64(peer->call_priv, peer->call_peer_pub, + initiator, &keys) != 0) { + status(local, "call refused: bad media key"); + call_debug(local, peer, "bad media key"); + return -1; + } + if (dc_local_conn_remote_addr(conn, host, sizeof(host)) != 0) + snprintf(host, sizeof(host), "127.0.0.1"); + { + DcPeer *dp = dc_peers_find(local->peers, peer->id); + + if (dp != NULL && dc_peer_host(dp) != NULL && + dc_peer_host(dp)[0] != '\0') + snprintf(host, sizeof(host), "%s", dc_peer_host(dp)); + } + port = peer->call_port; + if (port <= 0) { + status(local, "call refused: the peer sent no media port"); + return -1; + } + dc_media_send_init(&snd, keys.send); + dc_media_recv_init(&rcv, keys.recv); + peer->call = dc_call_new_bound(local->port, host, port, &snd, &rcv, + err, sizeof(err)); + if (peer->call == NULL) { + char line[300]; + + snprintf(line, sizeof(line), "call failed: %s", err); + status(local, line); + return -1; + } + peer->call_initiator = initiator; + peer->call_state = DC_CALL_IDLE; + peer->play = NULL; + peer->cap = NULL; + /* the audio device is optional: a call still carries video/text */ + (void)mcde_sys_pcm_open(&peer->play, MCDE_SYS_PCM_PLAYBACK, + MCDE_SYS_PCM_DEFAULT, DC_AUDIO_RATE, 1); + (void)mcde_sys_pcm_open(&peer->cap, MCDE_SYS_PCM_CAPTURE, + MCDE_SYS_PCM_DEFAULT, DC_AUDIO_RATE, 1); + peer->call_window = dtchat_call_window_create(dtchat_ui_shell( + local->ui), peer->call, peer->name, call_hangup_cb, peer); + if (initiator) { + dtchat_call_window_set_status(peer->call_window, + "ringing"); + (void)dc_call_ring(peer->call); + } else { + dtchat_call_window_set_incoming(peer->call_window, 1); + } + call_debug(local, peer, initiator ? "ringing" : "incoming"); + return 0; +} + +/* the UI asked to call PEER_ID (Conversation > Call) */ +static void +local_call_hook(const char *peer_id, void *arg) +{ + DtLocal *local = arg; + Peer *peer = peer_find_id(local, peer_id); + + if (peer == NULL) { + status(local, "call: peer is gone"); + return; + } + if (!peer->paired) { + status(local, "call: pair the peer first"); + return; + } + if (peer->call != NULL) { + status(local, "call: already in a call"); + return; + } + if (peer->conn == NULL) { + status(local, "call: peer is offline"); + return; + } + if (peer->call_offered) + return; + if (local->port <= 0) { + status(local, "call: start dtchat with --listen first"); + return; + } + peer->call_offered = 1; + if (dc_media_keypair_b64(peer->call_pub, peer->call_priv) != 0) { + peer->call_offered = 0; + status(local, "call: cannot make a key"); + return; + } + send_media_key(local, peer, peer->conn, local->port); + status(local, "call: ringing"); + call_debug(local, peer, "media-key sent"); +} + /* ------------------------------ message lines --------------------------- */ static void @@ -532,6 +723,42 @@ handle_line(DtLocal *local, Peer *peer, DcLocalConn *conn, const DcJson *json) } return; } + if (strcmp(type, "media-key") == 0) { + const char *pub = dc_json_string(dc_json_get(json, "pub")); + int port = (int)dc_json_int(dc_json_get(json, "port")); + + if (pub == NULL || strlen(pub) != 44) { + status(local, "call: malformed media key"); + return; + } + if (!peer->paired) { + status(local, "call refused: peer is not paired"); + return; + } + snprintf(peer->call_peer_pub, sizeof(peer->call_peer_pub), + "%s", pub); + peer->call_port = port > 0 && port < 65536 ? port : 0; + if (peer->call == NULL && peer->call_offered) { + /* our offer was answered: we are the caller */ + peer->call_offered = 0; + (void)start_call(local, peer, conn, 1); + } else if (peer->call == NULL) { + /* the peer wants to call us: answer with our key */ + char mypub[DC_MEDIA_PUB_B64_LEN]; + + if (dc_media_keypair_b64(mypub, + peer->call_priv) != 0) { + status(local, "call: cannot make a key"); + return; + } + snprintf(peer->call_pub, sizeof(peer->call_pub), + "%s", mypub); + (void)start_call(local, peer, conn, 0); + send_media_key(local, peer, conn, local->port); + call_debug(local, peer, "media-key answered"); + } + return; + } if (strcmp(type, "msg") == 0) { const char *text = dc_json_string(dc_json_get(json, "text")); @@ -624,6 +851,11 @@ server_hello(DcLocalConn *conn, const DcJson *hello, void *arg) if (known == 1) { peer->paired = 1; announce(local, peer, 1); + } else if (known == 0 && local->test_trust) { + peer->paired = 1; + (void)dc_pins_confirm(local->pins, id, pin); + (void)dc_pins_save(local->pins, local->config); + announce(local, peer, 1); } else if (known == 0) { pair_dialog(local, peer->name, id, pin); } else { @@ -688,6 +920,11 @@ link_state(DcLocalPeer *link, int online, void *arg) if (known == 1) { peer->paired = 1; announce(local, peer, 1); + } else if (known == 0 && local->test_trust) { + peer->paired = 1; + (void)dc_pins_confirm(local->pins, id, pin); + (void)dc_pins_save(local->pins, local->config); + announce(local, peer, 1); } else if (known == 0) { pair_dialog(local, peer->name, id, pin); } else { @@ -764,6 +1001,92 @@ pump_cb(XtPointer client, XtIntervalId *id) status(local, dc_text(TX_FILE_SENT, "file sent")); } } + + /* calls: media packets, the audio device and the video frames */ + for (peer = local->state; peer != NULL; peer = peer->next) { + int16_t pcm[DC_AUDIO_FRAME_SAMPLES]; + unsigned char *video; + size_t vlen = 0; + int keyframe = 0; + uint16_t frame_id = 0; + DcCallState state; + + if (peer->call == NULL) + continue; + (void)dc_call_drain(peer->call); + while (peer->play != NULL && + dc_call_audio_out(peer->call, pcm) == 1) { + (void)mcde_sys_pcm_write(peer->play, pcm, + DC_AUDIO_FRAME_SAMPLES); + peer->audio_out++; + if (local->call_debug && peer->audio_out % 50 == 0) + fprintf(stderr, "call %s: audio out %u\n", + peer->name, peer->audio_out); + } + if (peer->cap != NULL && + dc_call_state(peer->call) == DC_CALL_IN_CALL && + dc_call_talking(peer->call) && + mcde_sys_pcm_wait(peer->cap, 0) == 1) { + ssize_t n = mcde_sys_pcm_read(peer->cap, pcm, + DC_AUDIO_FRAME_SAMPLES); + + if (n == DC_AUDIO_FRAME_SAMPLES && + dc_call_audio_in(peer->call, pcm) == 0) { + peer->audio_in++; + if (local->call_debug && + peer->audio_in % 50 == 0) + fprintf(stderr, + "call %s: audio in %u\n", + peer->name, peer->audio_in); + } + } + video = malloc(DC_VIDEO_FRAME_MAX); + if (video != NULL) { + while (dc_call_video_out(peer->call, video, + DC_VIDEO_FRAME_MAX, &vlen, &keyframe, + &frame_id) == 1) { + dtchat_call_window_set_frame( + peer->call_window, video, + DC_VIDEO_W, DC_VIDEO_H); + peer->call_video++; + } + free(video); + } + dc_call_tick(peer->call); + state = dc_call_state(peer->call); + if (state != peer->call_state) { + peer->call_state = state; + if (state == DC_CALL_IN_CALL) { + dtchat_call_window_set_status( + peer->call_window, "in the call"); + call_debug(local, peer, "in call"); + if (peer->call_initiator && + getenv("DTCHAT_TEST_PTT") != NULL) { + (void)dc_call_send_ptt(peer->call, 1); + dtchat_call_window_set_talking( + peer->call_window, 1); + call_debug(local, peer, + "talking (test)"); + } + } else if (state == DC_CALL_RINGING) { + if (!peer->call_initiator) { + dtchat_call_window_set_incoming( + peer->call_window, 1); + call_debug(local, peer, "ringing in"); + if (local->auto_answer) { + (void)dc_call_accept( + peer->call); + call_debug(local, peer, + "auto answered"); + } + } + } else if (state == DC_CALL_ENDED) { + dtchat_call_window_set_status( + peer->call_window, "call ended"); + call_debug(local, peer, "ended"); + } + } + } local->timer = XtAppAddTimeOut(local->app, 40, pump_cb, local); } @@ -833,6 +1156,9 @@ dtchat_local_start(DtChatUi *ui, DcModel *model, DcPeers *peers, dtchat_local_stop(local); return NULL; } + local->call_debug = getenv("DTCHAT_CALL_DEBUG") != NULL; + local->auto_answer = getenv("DTCHAT_TEST_AUTOANSWER") != NULL; + local->test_trust = getenv("DTCHAT_TEST_TRUST") != NULL; if (listen_text != NULL) { if (split_host_port(listen_text, host, sizeof(host), &port) != 0) { @@ -848,6 +1174,7 @@ dtchat_local_start(DtChatUi *ui, DcModel *model, DcPeers *peers, dtchat_local_stop(local); return NULL; } + local->port = dc_local_server_port(local->server); } for (i = 0; i < npeers; i++) { Peer *peer; @@ -869,6 +1196,7 @@ dtchat_local_start(DtChatUi *ui, DcModel *model, DcPeers *peers, } dtchat_ui_set_send_hook(ui, local_send, local); dtchat_ui_set_file_send(ui, local_file_send, local); + dtchat_ui_set_call_hook(ui, local_call_hook, local); dtchat_ui_set_peers(ui, peers); { char line[256]; @@ -883,6 +1211,24 @@ dtchat_local_start(DtChatUi *ui, DcModel *model, DcPeers *peers, return local; } +int +dtchat_local_call_first(DtLocal *local) +{ + size_t i; + + if (local == NULL) + return -1; + for (i = 0; i < dc_peers_count(local->peers); i++) { + DcPeer *dp = dc_peers_at(local->peers, i); + + if (!dc_peer_online(dp) || dc_peer_blocked(dp)) + continue; + local_call_hook(dc_peer_id(dp), local); + return 0; + } + return -1; +} + void dtchat_local_stop(DtLocal *local) { @@ -896,6 +1242,8 @@ dtchat_local_stop(DtLocal *local) } while ((peer = local->state) != NULL) { local->state = peer->next; + if (peer->call != NULL || peer->call_window != NULL) + call_stop(local, peer); if (peer->send != NULL) dc_file_send_free(peer->send); if (peer->recv != NULL) diff --git a/programs/dtchat/localui.h b/programs/dtchat/localui.h index 0451691b..592fc261 100644 --- a/programs/dtchat/localui.h +++ b/programs/dtchat/localui.h @@ -20,5 +20,7 @@ DtLocal *dtchat_local_start(DtChatUi *ui, DcModel *model, DcPeers *peers, const char *config_dir, const char *listen_text, const char *const *peer_text, int npeers, const char *downloads_dir); void dtchat_local_stop(DtLocal *local); +/* call the first online peer, for --call-first (a test convenience) */ +int dtchat_local_call_first(DtLocal *local); #endif /* _Dtchat_localui_h */ diff --git a/programs/dtchat/main.c b/programs/dtchat/main.c index 71864f18..50b9b73f 100644 --- a/programs/dtchat/main.c +++ b/programs/dtchat/main.c @@ -565,6 +565,25 @@ default_adapter_path(const char *argv0) return path; } +static void +call_first_cb(XtPointer client, XtIntervalId *id) +{ + static int attempts; + (void)client; + (void)id; + if (local_mgr == NULL) { + if (++attempts < 20) + XtAppAddTimeOut(app, 500, call_first_cb, NULL); + return; + } + if (dtchat_local_call_first(local_mgr) == 0) { + fprintf(stderr, "dtchat: call to the first peer started\n"); + return; + } + if (++attempts < 20) + XtAppAddTimeOut(app, 500, call_first_cb, NULL); +} + static void demo_smoke_check(XtPointer client, XtIntervalId *id) { @@ -714,6 +733,7 @@ main(int argc, char **argv) char *adapter_path = NULL; int local_mode = 0; int call_demo = 0; + int call_first = 0; int demo_flag = 0; int perf_flag = 0; int smoke = 0; @@ -730,6 +750,12 @@ main(int argc, char **argv) (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; + } else if (strcmp(argv[i], "--call-first") == 0) { + call_first = 1; + memmove(&argv[i], &argv[i + 1], + (size_t)(argc - i) * sizeof(argv[0])); + argc--; + i--; } else if (strcmp(argv[i], "--call-demo") == 0) { call_demo = 1; memmove(&argv[i], &argv[i + 1], @@ -830,6 +856,8 @@ main(int argc, char **argv) } dtchat_ui_set_peers(ui, demo_peers); XtRealizeWidget(toplevel); + if (call_first) + XtAppAddTimeOut(app, 1000, call_first_cb, NULL); if (call_demo) { unsigned char *i420 = malloc((size_t)DC_VIDEO_W * DC_VIDEO_H * 3 / 2); diff --git a/programs/dtchat/ui_call.c b/programs/dtchat/ui_call.c index 2fcc04eb..fa237929 100644 --- a/programs/dtchat/ui_call.c +++ b/programs/dtchat/ui_call.c @@ -38,7 +38,9 @@ struct DtCallWindow { Widget area; Widget status; Widget talk; + Widget answer; Widget hangup; + int incoming; DcMediaCall *call; DtCallHangupFn fn; void *arg; @@ -52,6 +54,7 @@ struct DtCallWindow { uint32_t *pixels; /* VIDEO_W x VIDEO_H, 0x00RRGGBB */ XImage *image; int dirty; + XtIntervalId size_timer; }; static void @@ -149,6 +152,25 @@ talk_cb(Widget widget, XtPointer client, XtPointer call) "push-to-talk is off"); } +static void +answer_cb(Widget widget, XtPointer client, XtPointer call) +{ + DtCallWindow *w = client; + + (void)widget; + (void)call; + if (w->call == NULL || w->closed) + return; + if (dc_call_accept(w->call) != 0) { + dtchat_call_window_set_status(w, "cannot answer the call"); + return; + } + XmToggleButtonSetState(w->talk, False, False); + XtSetSensitive(w->answer, False); + w->incoming = 0; + dtchat_call_window_set_status(w, "in the call"); +} + static void hangup_cb(Widget widget, XtPointer client, XtPointer call) { @@ -175,6 +197,19 @@ destroyed_cb(Widget widget, XtPointer client, XtPointer call) w->shell = NULL; } +static void +size_cb(XtPointer client, XtIntervalId *id) +{ + DtCallWindow *w = client; + + (void)id; + w->size_timer = 0; + if (w->shell == NULL) + return; + XtVaSetValues(w->form, XmNwidth, 500, XmNheight, 400, NULL); + XtVaSetValues(w->shell, XmNwidth, 500, XmNheight, 400, NULL); +} + static void set_label(Widget widget, const char *text) { @@ -223,8 +258,42 @@ dtchat_call_window_create(Widget parent, DcMediaCall *call, XmNheight, 420, XmNdeleteResponse, XmDESTROY, NULL); XtAddCallback(w->shell, XmNdestroyCallback, destroyed_cb, w); + buttons = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, XmNheight, 40, NULL); + w->talk = XmCreateToggleButton(buttons, "talk", NULL, 0); + set_label(w->talk, "Push to talk"); + XtVaSetValues(w->talk, XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, 6, + XmNtopOffset, 6, NULL); + XtAddCallback(w->talk, XmNvalueChangedCallback, talk_cb, w); + XtManageChild(w->talk); + w->answer = XmCreatePushButton(buttons, "answer", NULL, 0); + set_label(w->answer, "Answer"); + XtVaSetValues(w->answer, XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, w->talk, + XmNleftOffset, 12, XmNtopOffset, 6, NULL); + XtAddCallback(w->answer, XmNactivateCallback, answer_cb, w); + XtManageChild(w->answer); + XtSetSensitive(w->answer, False); + w->hangup = XmCreatePushButton(buttons, "hangup", NULL, 0); + set_label(w->hangup, "Hang up"); + XtVaSetValues(w->hangup, XmNtopAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, 6, + XmNtopOffset, 6, NULL); + XtAddCallback(w->hangup, XmNactivateCallback, hangup_cb, w); + XtManageChild(w->hangup); + close = XmCreatePushButton(buttons, "close", NULL, 0); + set_label(close, "Close"); + XtVaSetValues(close, XmNtopAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, w->hangup, + XmNrightOffset, 6, XmNtopOffset, 6, NULL); + XtAddCallback(close, XmNactivateCallback, hangup_cb, w); + XtManageChild(close); + w->area = XtVaCreateManagedWidget("video", xmDrawingAreaWidgetClass, - form, XmNtopAttachment, XmATTACH_FORM, + form, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, buttons, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNwidth, VIDEO_W, XmNheight, VIDEO_H, @@ -242,40 +311,19 @@ dtchat_call_window_create(Widget parent, DcMediaCall *call, XmStringFree(x); XtManageChild(w->status); - buttons = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form, - XmNbottomAttachment, XmATTACH_FORM, - XmNleftAttachment, XmATTACH_FORM, - XmNrightAttachment, XmATTACH_FORM, XmNheight, 40, NULL); - w->talk = XmCreateToggleButton(buttons, "talk", NULL, 0); - set_label(w->talk, "Push to talk"); - XtVaSetValues(w->talk, XmNbottomAttachment, XmATTACH_FORM, - XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, 6, - XmNbottomOffset, 6, NULL); - XtAddCallback(w->talk, XmNvalueChangedCallback, talk_cb, w); - XtManageChild(w->talk); - w->hangup = XmCreatePushButton(buttons, "hangup", NULL, 0); - set_label(w->hangup, "Hang up"); - XtVaSetValues(w->hangup, XmNbottomAttachment, XmATTACH_FORM, - XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, 6, - XmNbottomOffset, 6, NULL); - XtAddCallback(w->hangup, XmNactivateCallback, hangup_cb, w); - XtManageChild(w->hangup); - close = XmCreatePushButton(buttons, "close", NULL, 0); - set_label(close, "Close"); - XtVaSetValues(close, XmNbottomAttachment, XmATTACH_FORM, - XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, w->hangup, - XmNrightOffset, 6, XmNbottomOffset, 6, NULL); - XtAddCallback(close, XmNactivateCallback, hangup_cb, w); - XtManageChild(close); - if (call == NULL) { XtSetSensitive(w->talk, False); XtSetSensitive(w->hangup, False); + XtSetSensitive(w->answer, False); } XtManageChild(form); + XtPopup(w->shell, XtGrabNone); + /* after the popup the form has its natural height; ask for the full + * window (video + status + buttons) now */ XtVaSetValues(w->form, XmNwidth, 500, XmNheight, 400, NULL); XtVaSetValues(w->shell, XmNwidth, 500, XmNheight, 400, NULL); - XtPopup(w->shell, XtGrabNone); + w->size_timer = XtAppAddTimeOut( + XtWidgetToApplicationContext(form), 50, size_cb, w); return w; } @@ -284,6 +332,10 @@ dtchat_call_window_destroy(DtCallWindow *w) { if (w == NULL) return; + if (w->size_timer != 0) { + XtRemoveTimeOut(w->size_timer); + w->size_timer = 0; + } if (w->shell != NULL) { Widget shell = w->shell; @@ -348,6 +400,17 @@ dtchat_call_window_has_frame(const DtCallWindow *w) return w != NULL ? w->has_frame : 0; } +void +dtchat_call_window_set_incoming(DtCallWindow *w, int incoming) +{ + if (w == NULL || w->answer == NULL) + return; + w->incoming = incoming ? 1 : 0; + XtSetSensitive(w->answer, incoming ? True : False); + if (incoming) + dtchat_call_window_set_status(w, "incoming call - Answer"); +} + void dtchat_call_window_set_talking(DtCallWindow *w, int on) { diff --git a/programs/dtchat/ui_call.h b/programs/dtchat/ui_call.h index 056dd70e..6c94f969 100644 --- a/programs/dtchat/ui_call.h +++ b/programs/dtchat/ui_call.h @@ -33,5 +33,8 @@ void dtchat_call_window_set_status(DtCallWindow *window, const char *text); int dtchat_call_window_has_frame(const DtCallWindow *window); /* push-to-talk state as the button shows it */ void dtchat_call_window_set_talking(DtCallWindow *window, int on); +/* an incoming call: the Answer button becomes sensitive and answers with + * dc_call_accept() */ +void dtchat_call_window_set_incoming(DtCallWindow *window, int incoming); #endif /* _Dtchat_ui_call_h */ diff --git a/programs/dtchat/ui_main.c b/programs/dtchat/ui_main.c index 6890eae3..44d4dd20 100644 --- a/programs/dtchat/ui_main.c +++ b/programs/dtchat/ui_main.c @@ -58,6 +58,8 @@ struct DtChatUi { DcPeers *peers; DtChatFileSendFn file_fn; void *file_arg; + DtChatCallFn call_fn; + void *call_arg; size_t shown; int selecting; DtAccountsDialog *accounts; @@ -606,6 +608,26 @@ send_file_cb(Widget w, XtPointer client, XtPointer call) dc_peer_name(peer), ui->file_fn, ui->file_arg); } +static void +call_cb(Widget w, XtPointer client, XtPointer call) +{ + DtChatUi *ui = client; + + (void)w; + (void)call; + if (ui->cur_peer == NULL) { + set_status(ui, dc_text(TX_SELECT_PEER, + "select a Peer in Nearby first")); + return; + } + if (ui->call_fn == NULL) { + set_status(ui, dc_text(TX_LOCAL_NOT_CONNECTED, + "local mode is not connected")); + return; + } + ui->call_fn(ui->cur_peer, ui->call_arg); +} + static void make_menu_bar(DtChatUi *ui, Widget form, Widget *bar_out) { @@ -645,8 +667,7 @@ make_menu_bar(DtChatUi *ui, Widget form, Widget *bar_out) (void)make_item(conversation, dc_text(TX_JOIN, "Join Channel"), not_implemented_cb, not_impl(ui, "Join a channel")); (void)make_item(conversation, dc_text(TX_SEND_FILE, "Send File"), send_file_cb, ui); - (void)make_item(conversation, dc_text(TX_CALL, "Call"), not_implemented_cb, - not_impl(ui, "Call")); + (void)make_item(conversation, dc_text(TX_CALL, "Call"), call_cb, ui); help = make_menu(bar, dc_text(TX_HELP, "Help")); (void)make_item(help, dc_text(TX_ABOUT, "About dtchat"), about_cb, ui); @@ -1281,6 +1302,15 @@ dtchat_ui_set_file_send(DtChatUi *ui, DtChatFileSendFn fn, void *arg) ui->file_arg = arg; } +void +dtchat_ui_set_call_hook(DtChatUi *ui, DtChatCallFn fn, void *arg) +{ + if (ui == NULL) + return; + ui->call_fn = fn; + ui->call_arg = arg; +} + int dtchat_ui_is_peer(const DtChatUi *ui, const char *id) { diff --git a/programs/dtchat/ui_main.h b/programs/dtchat/ui_main.h index 796ebb54..bd4f481e 100644 --- a/programs/dtchat/ui_main.h +++ b/programs/dtchat/ui_main.h @@ -38,6 +38,10 @@ typedef void (*DtChatFileSendFn)(const char *peer_id, const char *path, void *arg); void dtchat_ui_set_file_send(DtChatUi *ui, DtChatFileSendFn fn, void *arg); +/* ... and the call starter (T6.5/T7.6). */ +typedef void (*DtChatCallFn)(const char *peer_id, void *arg); +void dtchat_ui_set_call_hook(DtChatUi *ui, DtChatCallFn fn, void *arg); + /* T2.3 runtime binding. */ void dtchat_ui_refresh(DtChatUi *ui); void dtchat_ui_message_added(DtChatUi *ui, const char *account,