From 8a802568c1ab2ddcb45d9a9dd8799c8c3fdd5ab3 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 17 Nov 2026 08:35:29 +0000 Subject: [PATCH] dtchat: add the call window with video and push-to-talk (T7.6, T7.2 XImage part) ui_call.c: a Motif window for one call with a 480x270 video area, status line, Push to talk (a toggle wired to dc_call_send_ptt), Hang up (dc_call_hangup + owner callback) and Close. The newest I420 frame is scaled bilinearly to an aspect-preserving rectangle, converted with dc_i420_to_xrgb, composed into one 0x00RRGGBB buffer (black bars around it) and blitted with one XPutImage on expose or a new frame; no per-plane drawing. dc_call_new_bound() binds the UDP socket to the announced port, so a peer's service port serves TLS and media. --call-demo opens the window with a synthetic gradient frame; --smoke-test creates the window, sets a frame and checks it is there. Standalone build: smoke PASS, screenshot build/mcde-shots/dtchat-t76.png (video, status, Push to talk/Hang up/Close). A live call still needs the media-key exchange in the local text protocol (next: T6.5); camera capture is T7.1. --- lib/libDtChat/dc_internal.h | 5 + lib/libDtChat/media_call.c | 15 +- programs/dtchat/Makefile.am | 4 +- programs/dtchat/main.c | 81 ++++++++ programs/dtchat/ui_call.c | 358 ++++++++++++++++++++++++++++++++++++ programs/dtchat/ui_call.h | 37 ++++ 6 files changed, 495 insertions(+), 5 deletions(-) create mode 100644 programs/dtchat/ui_call.c create mode 100644 programs/dtchat/ui_call.h diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index b631c360..d0c27a1b 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -452,6 +452,11 @@ typedef enum { DcMediaCall *dc_call_new(const char *peer_host, int peer_port, const DcMediaSend *send, const DcMediaRecv *recv, char *err, size_t errlen); +/* same, but the UDP socket is bound to LOCAL_PORT (0: ephemeral); a peer's + * announced port is used for both its TLS listener and its media socket */ +DcMediaCall *dc_call_new_bound(int local_port, 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); diff --git a/lib/libDtChat/media_call.c b/lib/libDtChat/media_call.c index d044265b..d5aa180b 100644 --- a/lib/libDtChat/media_call.c +++ b/lib/libDtChat/media_call.c @@ -184,8 +184,9 @@ set_nonblock(int fd) } DcMediaCall * -dc_call_new(const char *peer_host, int peer_port, const DcMediaSend *send, - const DcMediaRecv *recv, char *err, size_t errlen) +dc_call_new_bound(int local_port, const char *peer_host, int peer_port, + const DcMediaSend *send, const DcMediaRecv *recv, char *err, + size_t errlen) { DcMediaCall *call; struct addrinfo hints; @@ -222,7 +223,7 @@ dc_call_new(const char *peer_host, int peer_port, const DcMediaSend *send, memset(&self, 0, sizeof(self)); self.sin_family = AF_INET; self.sin_addr.s_addr = htonl(INADDR_ANY); - self.sin_port = 0; + self.sin_port = htons((uint16_t)local_port); if (bind(call->fd, (struct sockaddr *)&self, sizeof(self)) != 0) (void) close(call->fd), call->fd = -1; @@ -251,6 +252,14 @@ dc_call_new(const char *peer_host, int peer_port, const DcMediaSend *send, return call; } +DcMediaCall * +dc_call_new(const char *peer_host, int peer_port, const DcMediaSend *send, + const DcMediaRecv *recv, char *err, size_t errlen) +{ + return dc_call_new_bound(0, peer_host, peer_port, send, recv, err, + errlen); +} + int dc_call_fd(const DcMediaCall *call) { diff --git a/programs/dtchat/Makefile.am b/programs/dtchat/Makefile.am index f0502968..0d308f7a 100644 --- a/programs/dtchat/Makefile.am +++ b/programs/dtchat/Makefile.am @@ -15,8 +15,8 @@ dtchat_LDADD = $(top_builddir)/lib/libDtChat/libDtChat.la \ $(top_builddir)/lib/libDtChat/libDtChatIpc.la $(XTOOLLIB) dtchat_SOURCES = main.c ui_main.c ui_accounts.c ui_request.c ui_peer.c \ - ui_file.c localui.c demo.c ui_main.h ui_accounts.h ui_request.h \ - ui_peer.h ui_file.h localui.h demo.h + ui_file.c localui.c ui_call.c demo.c ui_main.h ui_accounts.h \ + ui_request.h ui_peer.h ui_file.h localui.h ui_call.h demo.h dtchat_demo_SOURCES = demo_adapter.c diff --git a/programs/dtchat/main.c b/programs/dtchat/main.c index 254f9331..71864f18 100644 --- a/programs/dtchat/main.c +++ b/programs/dtchat/main.c @@ -26,12 +26,14 @@ #include "ui_peer.h" #include "ui_file.h" #include "localui.h" +#include "ui_call.h" static XtAppContext app; static Widget toplevel; static DcModel *demo_model; static DcPeers *demo_peers; static DtLocal *local_mgr; +static DtCallWindow *demo_call; static struct timespec perf_start; static int smoke_failed; static const char *demo_kind; @@ -327,6 +329,60 @@ make_demo_model(void) return model; } +static void +demo_video_frame(unsigned char *i420, int n) +{ + int x, y; + int left = (n * 5) % (DC_VIDEO_W - 80); + int top = (n * 3) % (DC_VIDEO_H - 60); + unsigned char *u = i420 + (size_t)DC_VIDEO_W * DC_VIDEO_H; + unsigned char *v = u + (size_t)(DC_VIDEO_W / 2) * (DC_VIDEO_H / 2); + int cw = DC_VIDEO_W / 2; + int ch = DC_VIDEO_H / 2; + + for (y = 0; y < DC_VIDEO_H; y++) + for (x = 0; x < DC_VIDEO_W; x++) { + int inside = x >= left && x < left + 80 && + y >= top && y < top + 60; + + i420[(size_t)y * DC_VIDEO_W + x] = + (unsigned char)(inside ? 235 : + (16 + (x / 3 + y / 2 + n * 6) % 220)); + } + for (y = 0; y < ch; y++) + for (x = 0; x < cw; x++) { + u[(size_t)y * cw + x] = (unsigned char)(96 + + (x * 3 / 4 + n) % 64); + v[(size_t)y * cw + x] = (unsigned char)(160 - + (y * 3 / 4 + n) % 64); + } +} + +static int +call_window_checks(DtChatUi *ui) +{ + unsigned char *i420 = malloc((size_t)DC_VIDEO_W * DC_VIDEO_H * 3 / 2); + + if (i420 == NULL) + return 0; + demo_call = dtchat_call_window_create(dtchat_ui_shell(ui), NULL, + "demo peer", NULL, NULL); + if (demo_call == NULL) { + free(i420); + return 0; + } + demo_video_frame(i420, 0); + dtchat_call_window_set_frame(demo_call, i420, DC_VIDEO_W, + DC_VIDEO_H); + XSync(XtDisplay(dtchat_ui_shell(ui)), False); + free(i420); + if (!dtchat_call_window_has_frame(demo_call)) + return 0; + dtchat_call_window_destroy(demo_call); + demo_call = NULL; + return 1; +} + static int smoke_ui_checks(DtChatUi *ui) { @@ -431,6 +487,8 @@ smoke_ui_checks(DtChatUi *ui) return 0; dtchat_file_offer_dialog_destroy(offer); } + if (!call_window_checks(ui)) + return 0; return 1; } @@ -655,6 +713,7 @@ main(int argc, char **argv) int npeers = 0; char *adapter_path = NULL; int local_mode = 0; + int call_demo = 0; int demo_flag = 0; int perf_flag = 0; int smoke = 0; @@ -671,6 +730,12 @@ main(int argc, char **argv) (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], + (size_t)(argc - i) * sizeof(argv[0])); + argc--; + i--; } else if (strcmp(argv[i], "--perf-test") == 0) { perf_flag = 1; memmove(&argv[i], &argv[i + 1], @@ -765,6 +830,21 @@ main(int argc, char **argv) } dtchat_ui_set_peers(ui, demo_peers); XtRealizeWidget(toplevel); + if (call_demo) { + unsigned char *i420 = malloc((size_t)DC_VIDEO_W * + DC_VIDEO_H * 3 / 2); + + XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); + if (i420 != NULL) { + demo_video_frame(i420, 0); + demo_call = dtchat_call_window_create(toplevel, + NULL, "alice", NULL, NULL); + if (demo_call != NULL) + dtchat_call_window_set_frame(demo_call, + i420, DC_VIDEO_W, DC_VIDEO_H); + free(i420); + } + } if (perf_flag) { perf_report(ui); dtchat_ui_destroy(ui); @@ -832,6 +912,7 @@ main(int argc, char **argv) XtAppAddTimeOut(app, 700, demo_request_cb, (XtPointer)ui); } XtAppMainLoop(app); + dtchat_call_window_destroy(demo_call); dtchat_local_stop(local_mgr); dtchat_demo_stop(echo); dtchat_ui_destroy(ui); diff --git a/programs/dtchat/ui_call.c b/programs/dtchat/ui_call.c new file mode 100644 index 00000000..2fcc04eb --- /dev/null +++ b/programs/dtchat/ui_call.c @@ -0,0 +1,358 @@ +/* + * 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 +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define VIDEO_W 480 +#define VIDEO_H 270 + +struct DtCallWindow { + Widget shell; + Widget form; + Widget area; + Widget status; + Widget talk; + Widget hangup; + 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; +}; + +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 +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 +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); + + w->area = XtVaCreateManagedWidget("video", xmDrawingAreaWidgetClass, + form, XmNtopAttachment, XmATTACH_FORM, + 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); + + 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); + } + XtManageChild(form); + XtVaSetValues(w->form, XmNwidth, 500, XmNheight, 400, NULL); + XtVaSetValues(w->shell, XmNwidth, 500, XmNheight, 400, NULL); + XtPopup(w->shell, XtGrabNone); + return w; +} + +void +dtchat_call_window_destroy(DtCallWindow *w) +{ + if (w == NULL) + return; + 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_talking(DtCallWindow *w, int on) +{ + if (w == NULL || w->talk == NULL) + return; + XmToggleButtonSetState(w->talk, on ? True : False, False); + w->talking = on ? 1 : 0; +} diff --git a/programs/dtchat/ui_call.h b/programs/dtchat/ui_call.h new file mode 100644 index 00000000..056dd70e --- /dev/null +++ b/programs/dtchat/ui_call.h @@ -0,0 +1,37 @@ +/* + * ui_call.h -- the call window + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + */ +#ifndef _Dtchat_ui_call_h +#define _Dtchat_ui_call_h + +#include + +#include "dc_internal.h" + +typedef struct DtCallWindow DtCallWindow; + +/* called when the person hangs up (button or window close) */ +typedef void (*DtCallHangupFn)(void *arg); + +/* + * A window for one call: the video area (I420 frames, scaled bilinearly and + * converted to XRGB), push-to-talk and hang-up. CALL may be NULL for the + * demo, then PTT and hang-up are insensitive and only frames are shown. The + * window does not own CALL. + */ +DtCallWindow *dtchat_call_window_create(Widget parent, DcMediaCall *call, + const char *peer_name, DtCallHangupFn hangup, void *arg); +void dtchat_call_window_destroy(DtCallWindow *window); +Widget dtchat_call_window_shell(const DtCallWindow *window); + +/* the newest I420 frame (W x H) to show */ +void dtchat_call_window_set_frame(DtCallWindow *window, + const unsigned char *i420, int w, int h); +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); + +#endif /* _Dtchat_ui_call_h */