Files
mcde/programs/dtchat/ui_call.c
T
lotherk ea31073ce0 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.
2026-11-17 08:49:25 +00:00

422 lines
11 KiB
C

/*
* ui_call.c -- the call window: video, push-to-talk, hang up
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The video area keeps the newest I420 frame; on every expose (and when a new
* frame arrives) it is scaled bilinearly into a rectangle that keeps the
* aspect ratio, converted to 0x00RRGGBB with the BT.601 matrix and written
* with one XPutImage. The pixels live in one buffer, so a frame is composed
* before it reaches the server (no tearing between planes). Push-to-talk is
* a toggle that calls dc_call_send_ptt(); hang up calls
* dc_call_hangup() and tells the owner.
*/
#define _POSIX_C_SOURCE 200809L
#include "ui_call.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <Xm/DialogS.h>
#include <Xm/DrawingA.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/ToggleB.h>
#include <Xm/Xm.h>
#define VIDEO_W 480
#define VIDEO_H 270
struct DtCallWindow {
Widget shell;
Widget form;
Widget area;
Widget status;
Widget talk;
Widget answer;
Widget hangup;
int incoming;
DcMediaCall *call;
DtCallHangupFn fn;
void *arg;
char *peer_name;
unsigned char *frame; /* W x H I420 */
int fw;
int fh;
int has_frame;
int talking;
int closed;
uint32_t *pixels; /* VIDEO_W x VIDEO_H, 0x00RRGGBB */
XImage *image;
int dirty;
XtIntervalId size_timer;
};
static void
render(DtCallWindow *w)
{
int dw = VIDEO_W;
int dh = VIDEO_H;
int vw = w->fw;
int vh = w->fh;
int tw, th, ox, oy;
unsigned char *scaled;
size_t i;
if (!w->has_frame || w->pixels == NULL || w->area == NULL ||
!XtIsRealized(w->area))
return;
/* fit the frame into the area, bars stay black */
tw = dw;
th = vw > 0 ? (int)((long)dw * vh / vw) : dh;
if (th > dh) {
th = dh;
tw = vh > 0 ? (int)((long)dh * vw / vh) : dw;
}
if (tw < 1)
tw = 1;
if (th < 1)
th = 1;
ox = (dw - tw) / 2;
oy = (dh - th) / 2;
for (i = 0; i < (size_t)dw * dh; i++)
w->pixels[i] = 0;
scaled = malloc((size_t)tw * th * 3 / 2);
if (scaled == NULL)
return;
dc_i420_scale(w->frame, vw, vh, scaled, tw, th);
{
uint32_t *rgb = malloc((size_t)tw * th * sizeof(uint32_t));
int y;
if (rgb == NULL) {
free(scaled);
return;
}
dc_i420_to_xrgb(scaled, tw, th, rgb, tw);
for (y = 0; y < th; y++)
memcpy(w->pixels + (size_t)(oy + y) * dw + ox,
rgb + (size_t)y * tw, (size_t)tw * sizeof(uint32_t));
free(rgb);
}
free(scaled);
if (w->image == NULL) {
Display *dpy = XtDisplay(w->area);
int screen = DefaultScreen(dpy);
/* 0x00RRGGBB words match the RGB masks of the usual TrueColor
* Xvfb/X.Org visuals */
w->image = XCreateImage(dpy, DefaultVisual(dpy, screen),
(unsigned)DefaultDepth(dpy, screen), ZPixmap, 0,
(char *)w->pixels, (unsigned)dw, (unsigned)dh, 32, 0);
}
if (w->image != NULL) {
w->image->data = (char *)w->pixels;
XPutImage(XtDisplay(w->area), XtWindow(w->area),
DefaultGC(XtDisplay(w->area), DefaultScreen(XtDisplay(w->area))),
w->image, 0, 0, 0, 0, (unsigned)dw, (unsigned)dh);
XFlush(XtDisplay(w->area));
}
w->dirty = 0;
}
static void
area_expose(Widget widget, XtPointer client, XEvent *event, Boolean *cont)
{
DtCallWindow *w = client;
(void)widget;
(void)event;
(void)cont;
render(w);
}
static void
talk_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
int on = XmToggleButtonGetState(widget) ? 1 : 0;
(void)call;
w->talking = on;
if (w->call != NULL && dc_call_send_ptt(w->call, on) != 0) {
dtchat_call_window_set_status(w, "cannot send the talk state");
return;
}
dtchat_call_window_set_status(w, on ? "talking" :
"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)
{
DtCallWindow *w = client;
(void)widget;
(void)call;
if (w->closed)
return;
w->closed = 1;
if (w->call != NULL)
(void)dc_call_hangup(w->call);
if (w->fn != NULL)
w->fn(w->arg);
}
static void
destroyed_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
(void)widget;
(void)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)
{
XmString x = XmStringCreateLocalized((char *)text);
XtVaSetValues(widget, XmNlabelString, x, NULL);
XmStringFree(x);
}
DtCallWindow *
dtchat_call_window_create(Widget parent, DcMediaCall *call,
const char *peer_name, DtCallHangupFn hangup, void *arg)
{
DtCallWindow *w;
Widget form, buttons, close;
char title[256];
XmString x;
if (parent == NULL)
return NULL;
w = calloc(1, sizeof(*w));
if (w == NULL)
return NULL;
w->call = call;
w->fn = hangup;
w->arg = arg;
w->peer_name = strdup(peer_name != NULL ? peer_name : "call");
w->frame = calloc(1, (size_t)DC_VIDEO_W * DC_VIDEO_H * 3 / 2);
w->pixels = calloc((size_t)VIDEO_W * VIDEO_H, sizeof(uint32_t));
if (w->frame == NULL || w->pixels == NULL || w->peer_name == NULL) {
dtchat_call_window_destroy(w);
return NULL;
}
w->fw = DC_VIDEO_W;
w->fh = DC_VIDEO_H;
form = XmCreateFormDialog(parent, "callWindow", NULL, 0);
if (form == NULL) {
dtchat_call_window_destroy(w);
return NULL;
}
w->form = form;
w->shell = XtParent(form);
snprintf(title, sizeof(title), "Call - %s",
peer_name != NULL ? peer_name : "peer");
XtVaSetValues(w->shell, XmNtitle, title, XmNwidth, 500,
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_WIDGET, XmNtopWidget, buttons,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNwidth, VIDEO_W, XmNheight, VIDEO_H,
XmNmarginWidth, 0, XmNmarginHeight, 0, NULL);
XtAddEventHandler(w->area, ExposureMask, False, area_expose, w);
w->status = XmCreateLabel(form, "status", NULL, 0);
x = XmStringCreateLocalized("connecting");
XtVaSetValues(w->status, XmNlabelString, x,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, w->area,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmarginLeft, 6, XmNmarginTop, 4, NULL);
XmStringFree(x);
XtManageChild(w->status);
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);
w->size_timer = XtAppAddTimeOut(
XtWidgetToApplicationContext(form), 50, size_cb, w);
return w;
}
void
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;
w->shell = NULL;
XtDestroyWidget(shell);
}
if (w->image != NULL) {
w->image->data = NULL;
XDestroyImage(w->image);
w->image = NULL;
}
free(w->pixels);
free(w->frame);
free(w->peer_name);
free(w);
}
Widget
dtchat_call_window_shell(const DtCallWindow *w)
{
return w != NULL ? w->shell : NULL;
}
void
dtchat_call_window_set_frame(DtCallWindow *w, const unsigned char *i420, int fw,
int fh)
{
size_t size;
if (w == NULL || i420 == NULL || fw <= 0 || fh <= 0)
return;
if (fw != w->fw || fh != w->fh) {
size = (size_t)fw * fh * 3 / 2;
{
unsigned char *frame = realloc(w->frame, size);
if (frame == NULL)
return;
w->frame = frame;
}
w->fw = fw;
w->fh = fh;
}
memcpy(w->frame, i420, (size_t)fw * fh * 3 / 2);
w->has_frame = 1;
w->dirty = 1;
render(w);
}
void
dtchat_call_window_set_status(DtCallWindow *w, const char *text)
{
if (w == NULL)
return;
if (w->status != NULL)
set_label(w->status, text != NULL ? text : "");
}
int
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)
{
if (w == NULL || w->talk == NULL)
return;
XmToggleButtonSetState(w->talk, on ? True : False, False);
w->talking = on ? 1 : 0;
}