The call window gains a Camera button that cycles the video source (/dev/video0..3, then the test pattern) through a hook in localui; the status line names the new backend (e.g. video source: v4l2:/dev/video0), and the button is insensitive in --call-demo. The in-call status now says 'in the call - LAN only, no relay', the note the plan asks for. Verified live between two UIs under Xvfb: the Camera button switched the sender from camera to pattern and back, the status line showed the backend, and video kept flowing; screenshots build/mcde-shots/dtchat-t76-camera.png, dtchat-t76b.png, dtchat-t76-video-off.png, dtchat-t76-full.png. Video off, full screen and resize-aware rendering were committed in 4e967b8a3c.
1418 lines
38 KiB
C
1418 lines
38 KiB
C
/*
|
|
* localui.c -- the local peer manager behind the UI (T5.8)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* Starts the listener, one reconnecting link per --peer, the pin store and the
|
|
* transfers. Peers appear in the Nearby section (DcPeers); their messages
|
|
* become conversations of the "local" account; the file hooks use the dialogs
|
|
* of ui_file.c. An unknown pin shows the 6-digit SAS: only an Accept stores
|
|
* the pin, a Reject (or a changed pin) blocks the peer.
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "localui.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/Xm.h>
|
|
|
|
#include "ui_file.h"
|
|
#include "ui_call.h"
|
|
#include "ui_text.h"
|
|
|
|
typedef struct Peer Peer;
|
|
struct Peer {
|
|
DtLocal *local;
|
|
char id[128];
|
|
char name[128];
|
|
char pin[128];
|
|
DcLocalConn *conn; /* accepted connection, or the link's conn */
|
|
DcLocalPeer *link;
|
|
DcFileSend *send;
|
|
char send_id[64];
|
|
DcFileRecv *recv;
|
|
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;
|
|
/* video: the camera (or test pattern), the VP8 encoder and the frame
|
|
* being sent; the encoder is created with the call */
|
|
DcCapture *capture;
|
|
DcVpxEnc *venc;
|
|
unsigned char *vframe;
|
|
unsigned char *vpacket;
|
|
uint16_t video_id;
|
|
unsigned int video_in;
|
|
unsigned int video_out;
|
|
long long video_last;
|
|
DcCallState call_state;
|
|
unsigned int call_video;
|
|
unsigned int audio_in;
|
|
unsigned int audio_out;
|
|
Peer *next;
|
|
};
|
|
|
|
struct DtLocal {
|
|
DtChatUi *ui;
|
|
DcModel *model;
|
|
DcPeers *peers;
|
|
DcLoop *loop;
|
|
DcIdentity *identity;
|
|
DcPins *pins;
|
|
DcLocalServer *server;
|
|
char *config;
|
|
char *downloads;
|
|
Peer *state;
|
|
XtAppContext app;
|
|
XtIntervalId timer;
|
|
int seq;
|
|
int port;
|
|
int call_debug;
|
|
int auto_answer;
|
|
int test_trust;
|
|
};
|
|
|
|
typedef struct {
|
|
DtLocal *local;
|
|
Peer *peer;
|
|
DcJson *offer;
|
|
char id[64];
|
|
Widget shell;
|
|
} OfferCtx;
|
|
|
|
typedef struct {
|
|
DtLocal *local;
|
|
Peer *peer;
|
|
char id[128];
|
|
char pin[128];
|
|
char name[128];
|
|
Widget shell;
|
|
} PairCtx;
|
|
|
|
static void
|
|
status(DtLocal *local, const char *text)
|
|
{
|
|
dtchat_ui_set_status(local->ui, text);
|
|
}
|
|
|
|
/* A destroyed dialog leaves a hole under Xvfb (no window manager). */
|
|
static void
|
|
repaint_ui(DtLocal *local)
|
|
{
|
|
Widget shell = dtchat_ui_shell(local->ui);
|
|
|
|
if (shell != NULL && XtIsRealized(shell)) {
|
|
XClearArea(XtDisplay(shell), XtWindow(shell), 0, 0, 0, 0,
|
|
True);
|
|
XFlush(XtDisplay(shell));
|
|
}
|
|
}
|
|
|
|
static Peer *
|
|
peer_find_id(DtLocal *local, const char *id)
|
|
{
|
|
Peer *p;
|
|
|
|
for (p = local->state; p != NULL; p = p->next)
|
|
if (strcmp(p->id, id) == 0)
|
|
return p;
|
|
return NULL;
|
|
}
|
|
|
|
static Peer *
|
|
peer_find_conn(DtLocal *local, DcLocalConn *conn)
|
|
{
|
|
Peer *p;
|
|
|
|
for (p = local->state; p != NULL; p = p->next)
|
|
if (p->conn == conn)
|
|
return p;
|
|
return NULL;
|
|
}
|
|
|
|
static Peer *
|
|
peer_get(DtLocal *local, const char *id, const char *pin)
|
|
{
|
|
Peer *p = peer_find_id(local, id);
|
|
|
|
if (p != NULL)
|
|
return p;
|
|
p = calloc(1, sizeof(*p));
|
|
if (p == NULL)
|
|
return NULL;
|
|
p->local = local;
|
|
snprintf(p->id, sizeof(p->id), "%s", id);
|
|
snprintf(p->name, sizeof(p->name), "%.12s", id);
|
|
if (pin != NULL)
|
|
snprintf(p->pin, sizeof(p->pin), "%s", pin);
|
|
p->next = local->state;
|
|
local->state = p;
|
|
return p;
|
|
}
|
|
|
|
static void
|
|
announce(DtLocal *local, Peer *p, int online)
|
|
{
|
|
char txt[512];
|
|
DcPeer *dp;
|
|
|
|
snprintf(txt, sizeof(txt), "v=1;id=%s;name=%s;pin=%s", p->id, p->name,
|
|
p->pin);
|
|
(void)dc_peers_announce(local->peers, txt, "", 0);
|
|
dp = dc_peers_find(local->peers, p->id);
|
|
if (dp != NULL) {
|
|
dc_peer_set_blocked(dp, p->blocked);
|
|
if (!online)
|
|
(void)dc_peers_offline(local->peers, p->id);
|
|
}
|
|
dtchat_ui_set_peers(local->ui, local->peers);
|
|
}
|
|
|
|
static void
|
|
ensure_conv(DtLocal *local, Peer *p)
|
|
{
|
|
(void)dc_model_add_account(local->model, "local", "local",
|
|
dc_text(TX_LOCAL_PEERS, "Local peers"));
|
|
(void)dc_model_add_conv(local->model, "local", p->id, DC_CONV_DIRECT,
|
|
p->name);
|
|
}
|
|
|
|
static void
|
|
add_message(DtLocal *local, Peer *p, const char *from, const char *text,
|
|
int flags)
|
|
{
|
|
DcConv *conv;
|
|
|
|
ensure_conv(local, p);
|
|
conv = dc_model_find_conv(local->model, "local", p->id);
|
|
if (conv == NULL)
|
|
return;
|
|
(void)dc_conv_add_message(conv, from, text, (long long)time(NULL),
|
|
flags);
|
|
dtchat_ui_message_added(local->ui, "local", p->id);
|
|
}
|
|
|
|
/* ------------------------------- pairing -------------------------------- */
|
|
|
|
static void
|
|
pair_accept(PairCtx *ctx)
|
|
{
|
|
Peer *p;
|
|
|
|
if (dc_pins_confirm(ctx->local->pins, ctx->id, ctx->pin) != 0)
|
|
return;
|
|
(void)dc_pins_save(ctx->local->pins, ctx->local->config);
|
|
p = peer_find_id(ctx->local, ctx->id);
|
|
if (p == NULL)
|
|
p = peer_get(ctx->local, ctx->id, ctx->pin);
|
|
if (p != NULL) {
|
|
snprintf(p->name, sizeof(p->name), "%s",
|
|
ctx->name[0] != '\0' ? ctx->name : p->name);
|
|
snprintf(p->pin, sizeof(p->pin), "%s", ctx->pin);
|
|
p->blocked = 0;
|
|
p->paired = 1;
|
|
announce(ctx->local, p, 1);
|
|
}
|
|
status(ctx->local, dc_text(TX_PEER_PAIRED, "peer paired"));
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
pair_reject(PairCtx *ctx)
|
|
{
|
|
Peer *p;
|
|
|
|
(void)dc_pins_set_blocked(ctx->local->pins, ctx->id, 1);
|
|
(void)dc_pins_save(ctx->local->pins, ctx->local->config);
|
|
p = peer_find_id(ctx->local, ctx->id);
|
|
if (p != NULL) {
|
|
p->blocked = 1;
|
|
if (p->link != NULL) {
|
|
dc_local_peer_stop(p->link);
|
|
p->conn = NULL;
|
|
} else if (p->conn != NULL) {
|
|
dc_local_conn_close(p->conn);
|
|
p->conn = NULL;
|
|
}
|
|
announce(ctx->local, p, 0);
|
|
}
|
|
status(ctx->local, dc_text(TX_PEER_BLOCKED, "peer blocked"));
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
pair_accept_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
pair_accept(client);
|
|
}
|
|
|
|
static void
|
|
pair_reject_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
pair_reject(client);
|
|
}
|
|
|
|
static void
|
|
pair_dialog(DtLocal *local, const char *name, const char *id,
|
|
const char *pin)
|
|
{
|
|
PairCtx *ctx = calloc(1, sizeof(*ctx));
|
|
Widget form, label, buttons, accept, reject;
|
|
char code_line[128];
|
|
char *code;
|
|
|
|
if (ctx == NULL)
|
|
return;
|
|
ctx->local = local;
|
|
snprintf(ctx->id, sizeof(ctx->id), "%s", id);
|
|
snprintf(ctx->pin, sizeof(ctx->pin), "%s", pin);
|
|
snprintf(ctx->name, sizeof(ctx->name), "%s", name);
|
|
code = dc_pair_sas(dc_identity_pin(local->identity), pin);
|
|
snprintf(code_line, sizeof(code_line), "%s", code != NULL ? code :
|
|
"------");
|
|
free(code);
|
|
form = XmCreateFormDialog(dtchat_ui_shell(local->ui), "pair", NULL, 0);
|
|
if (form == NULL) {
|
|
free(ctx);
|
|
return;
|
|
}
|
|
ctx->shell = XtParent(form);
|
|
XtVaSetValues(ctx->shell, XmNtitle,
|
|
(char *)dc_text(TX_PAIR_PEER, "Pair peer"), XmNwidth, 380,
|
|
XmNheight, 210, XmNdeleteResponse, XmDESTROY, NULL);
|
|
label = XmCreateLabel(form, "who", NULL, 0);
|
|
{
|
|
char line[512];
|
|
XmString x;
|
|
|
|
snprintf(line, sizeof(line),
|
|
dc_text(TX_WANTS_PAIR, "%s wants to pair. Compare this "
|
|
"code on both machines:"), name);
|
|
x = XmStringCreateLocalized(line);
|
|
XtVaSetValues(label, XmNlabelString, x,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNmarginTop, 10, XmNmarginLeft, 8,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtManageChild(label);
|
|
{
|
|
Widget big = XmCreateLabel(form, "code", NULL, 0);
|
|
XmString x = XmStringCreateLocalized(code_line);
|
|
|
|
XtVaSetValues(big, XmNlabelString, x,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, label,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopOffset, 14, XmNmarginTop, 6, NULL);
|
|
XmStringFree(x);
|
|
XtManageChild(big);
|
|
}
|
|
buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass,
|
|
form, XmNorientation, XmHORIZONTAL, XmNspacing, 8,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNmarginHeight, 8, NULL);
|
|
reject = XmCreatePushButton(buttons, "reject", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(TX_REJECT,
|
|
"Reject"));
|
|
|
|
XtVaSetValues(reject, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(reject, XmNactivateCallback, pair_reject_cb, ctx);
|
|
XtManageChild(reject);
|
|
accept = XmCreatePushButton(buttons, "accept", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(
|
|
TX_CODES_MATCH, "Codes match"));
|
|
|
|
XtVaSetValues(accept, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(accept, XmNactivateCallback, pair_accept_cb, ctx);
|
|
XtManageChild(accept);
|
|
XtManageChild(form);
|
|
XtVaSetValues(ctx->shell, XmNwidth, 380, XmNheight, 210, NULL);
|
|
XtPopup(ctx->shell, XtGrabNone);
|
|
}
|
|
|
|
/* ----------------------------- file receive ----------------------------- */
|
|
|
|
static void
|
|
offer_accept(OfferCtx *ctx)
|
|
{
|
|
char err[256];
|
|
const char *id = dc_json_string(dc_json_get(ctx->offer, "id"));
|
|
DcJson *acc;
|
|
|
|
ctx->peer->recv = dc_file_recv_new(ctx->local->downloads, ctx->offer,
|
|
err, sizeof(err));
|
|
if (ctx->peer->recv == NULL) {
|
|
status(ctx->local, err);
|
|
} else {
|
|
snprintf(ctx->peer->recv_id, sizeof(ctx->peer->recv_id), "%s",
|
|
id != NULL ? id : ctx->id);
|
|
acc = dc_json_new_object();
|
|
(void)dc_json_object_set(acc, "type",
|
|
dc_json_new_string("file-accept"));
|
|
(void)dc_json_object_set(acc, "id",
|
|
dc_json_new_string(ctx->peer->recv_id));
|
|
(void)dc_local_conn_send_json(ctx->peer->conn, acc);
|
|
dc_json_free(acc);
|
|
status(ctx->local, dc_text(TX_RECEIVING, "receiving file"));
|
|
}
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
dc_json_free(ctx->offer);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
offer_reject(OfferCtx *ctx)
|
|
{
|
|
DcJson *rej = dc_json_new_object();
|
|
const char *id = dc_json_string(dc_json_get(ctx->offer, "id"));
|
|
|
|
(void)dc_json_object_set(rej, "type",
|
|
dc_json_new_string("file-reject"));
|
|
(void)dc_json_object_set(rej, "id",
|
|
dc_json_new_string(id != NULL ? id : ""));
|
|
(void)dc_json_object_set(rej, "reason",
|
|
dc_json_new_string("rejected"));
|
|
if (ctx->peer->conn != NULL)
|
|
(void)dc_local_conn_send_json(ctx->peer->conn, rej);
|
|
dc_json_free(rej);
|
|
XtDestroyWidget(ctx->shell);
|
|
repaint_ui(ctx->local);
|
|
dc_json_free(ctx->offer);
|
|
free(ctx);
|
|
}
|
|
|
|
static void
|
|
offer_accept_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
offer_accept(client);
|
|
}
|
|
|
|
static void
|
|
offer_reject_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)call;
|
|
offer_reject(client);
|
|
}
|
|
|
|
static void
|
|
file_offer(DtLocal *local, Peer *peer, const DcJson *json)
|
|
{
|
|
const char *name = dc_json_string(dc_json_get(json, "name"));
|
|
long long size = dc_json_int(dc_json_get(json, "size"));
|
|
OfferCtx *ctx = calloc(1, sizeof(*ctx));
|
|
const char *id = dc_json_string(dc_json_get(json, "id"));
|
|
Widget form, label, buttons, accept, reject;
|
|
char line[1024];
|
|
char human[64];
|
|
|
|
if (ctx == NULL)
|
|
return;
|
|
ctx->local = local;
|
|
ctx->peer = peer;
|
|
/* keep the fields dc_file_recv_new() needs */
|
|
ctx->offer = dc_json_new_object();
|
|
(void)dc_json_object_set(ctx->offer, "type",
|
|
dc_json_new_string("file-offer"));
|
|
(void)dc_json_object_set(ctx->offer, "id",
|
|
dc_json_new_string(id != NULL ? id : ""));
|
|
(void)dc_json_object_set(ctx->offer, "name",
|
|
dc_json_new_string(name != NULL ? name : "file"));
|
|
(void)dc_json_object_set(ctx->offer, "size", dc_json_new_int(size));
|
|
(void)dc_json_object_set(ctx->offer, "sha256",
|
|
dc_json_new_string(dc_json_string(dc_json_get(json, "sha256"))));
|
|
if (ctx->offer == NULL) {
|
|
free(ctx);
|
|
return;
|
|
}
|
|
if (size >= 1024 * 1024)
|
|
snprintf(human, sizeof(human), "%.1f MB",
|
|
(double)size / (1024.0 * 1024));
|
|
else
|
|
snprintf(human, sizeof(human), "%lld bytes", size);
|
|
form = XmCreateFormDialog(dtchat_ui_shell(local->ui), "offer", NULL,
|
|
0);
|
|
if (form == NULL) {
|
|
dc_json_free(ctx->offer);
|
|
free(ctx);
|
|
return;
|
|
}
|
|
ctx->shell = XtParent(form);
|
|
XtVaSetValues(ctx->shell, XmNtitle,
|
|
(char *)dc_text(TX_INCOMING_FILE, "Incoming file"), XmNwidth, 420,
|
|
XmNheight, 180, NULL);
|
|
snprintf(line, sizeof(line),
|
|
dc_text(TX_OFFERS, "%s offers %s (%s)"), peer->name,
|
|
name != NULL ? name : "a file", human);
|
|
label = XmCreateLabel(form, "name", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized(line);
|
|
|
|
XtVaSetValues(label, XmNlabelString, x,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNmarginTop, 12, XmNmarginLeft, 8,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtManageChild(label);
|
|
buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass,
|
|
form, XmNorientation, XmHORIZONTAL, XmNspacing, 8,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, XmNmarginHeight, 8, NULL);
|
|
reject = XmCreatePushButton(buttons, "reject", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(TX_REJECT,
|
|
"Reject"));
|
|
|
|
XtVaSetValues(reject, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(reject, XmNactivateCallback, offer_reject_cb, ctx);
|
|
XtManageChild(reject);
|
|
accept = XmCreatePushButton(buttons, "accept", NULL, 0);
|
|
{
|
|
XmString x = XmStringCreateLocalized((char *)dc_text(TX_ACCEPT,
|
|
"Accept"));
|
|
|
|
XtVaSetValues(accept, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
XtAddCallback(accept, XmNactivateCallback, offer_accept_cb, ctx);
|
|
XtManageChild(accept);
|
|
XtManageChild(form);
|
|
XtVaSetValues(ctx->shell, XmNwidth, 420, XmNheight, 180, NULL);
|
|
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;
|
|
}
|
|
dc_capture_close(peer->capture);
|
|
peer->capture = NULL;
|
|
dc_vpx_enc_free(peer->venc);
|
|
peer->venc = NULL;
|
|
free(peer->vframe);
|
|
peer->vframe = NULL;
|
|
free(peer->vpacket);
|
|
peer->vpacket = NULL;
|
|
peer->video_in = 0;
|
|
peer->video_out = 0;
|
|
peer->video_id = 0;
|
|
mcde_sys_pcm_close(peer->play);
|
|
peer->play = NULL;
|
|
mcde_sys_pcm_close(peer->cap);
|
|
peer->cap = NULL;
|
|
peer->call_state = DC_CALL_ENDED;
|
|
}
|
|
|
|
/* the Camera button: the next source (the camera nodes, then the pattern) */
|
|
static const char *
|
|
call_camera_cycle(void *arg)
|
|
{
|
|
Peer *peer = arg;
|
|
static const char *const sources[] = {
|
|
"/dev/video0", "/dev/video1", "/dev/video2", "/dev/video3",
|
|
"pattern"
|
|
};
|
|
int cur = -1;
|
|
int k;
|
|
char err[256] = "";
|
|
|
|
if (peer == NULL || peer->call == NULL)
|
|
return NULL;
|
|
if (peer->capture != NULL) {
|
|
const char *b = dc_capture_backend(peer->capture);
|
|
unsigned int i;
|
|
|
|
if (strcmp(b, "test-pattern") == 0) {
|
|
cur = 4;
|
|
} else {
|
|
for (i = 0; i < 4; i++)
|
|
if (strcmp(b, sources[i]) == 0) {
|
|
cur = (int)i;
|
|
break;
|
|
}
|
|
}
|
|
if (strncmp(b, "v4l2:", 5) == 0) {
|
|
int n = -1;
|
|
|
|
if (sscanf(b, "v4l2:/dev/video%d", &n) == 1 &&
|
|
n >= 0 && n <= 3)
|
|
cur = n;
|
|
}
|
|
}
|
|
for (k = 1; k <= 5; k++) {
|
|
int idx = (cur + k + 5) % 5;
|
|
DcCapture *cap = dc_capture_open(DC_VIDEO_W, DC_VIDEO_H,
|
|
DC_VIDEO_FPS, sources[idx], err, sizeof(err));
|
|
|
|
if (cap == NULL)
|
|
continue;
|
|
dc_capture_close(peer->capture);
|
|
peer->capture = cap;
|
|
if (getenv("DTCHAT_CALL_DEBUG") != NULL)
|
|
fprintf(stderr, "call %s: camera %s\n", peer->name,
|
|
dc_capture_backend(cap));
|
|
return dc_capture_backend(cap);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
/* the camera (or the pattern of a test) and the encoder, once in the call */
|
|
static void
|
|
call_video_start(DtLocal *local, Peer *peer)
|
|
{
|
|
char err[256] = "";
|
|
const char *device = getenv("DTCHAT_TEST_VIDEO") != NULL ? "pattern" :
|
|
"auto";
|
|
|
|
if (peer->venc != NULL || peer->capture != NULL)
|
|
return;
|
|
peer->venc = dc_vpx_enc_new(DC_VIDEO_W, DC_VIDEO_H, DC_VIDEO_FPS,
|
|
DC_VIDEO_KBPS, err, sizeof(err));
|
|
peer->capture = dc_capture_open(DC_VIDEO_W, DC_VIDEO_H, DC_VIDEO_FPS,
|
|
device, err, sizeof(err));
|
|
peer->vframe = malloc((size_t)DC_VIDEO_W * DC_VIDEO_H * 3 / 2);
|
|
peer->vpacket = malloc(DC_VIDEO_FRAME_MAX);
|
|
if (peer->venc == NULL || peer->capture == NULL ||
|
|
peer->vframe == NULL || peer->vpacket == NULL) {
|
|
call_debug(local, peer, "no video");
|
|
dc_capture_close(peer->capture);
|
|
peer->capture = NULL;
|
|
dc_vpx_enc_free(peer->venc);
|
|
peer->venc = NULL;
|
|
return;
|
|
}
|
|
peer->video_last = dc_loop_now_ms() - 1000 / DC_VIDEO_FPS;
|
|
call_debug(local, peer, dc_capture_backend(peer->capture));
|
|
}
|
|
|
|
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);
|
|
dtchat_call_window_set_camera_hook(peer->call_window,
|
|
call_camera_cycle, 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
|
|
handle_line(DtLocal *local, Peer *peer, DcLocalConn *conn, const DcJson *json)
|
|
{
|
|
const char *type = dc_json_string(dc_json_get(json, "type"));
|
|
const char *id = dc_json_string(dc_json_get(json, "id"));
|
|
|
|
if (type == NULL || peer == NULL)
|
|
return;
|
|
if (!peer->paired &&
|
|
(strcmp(type, "file-offer") == 0 ||
|
|
strcmp(type, "file-data") == 0 ||
|
|
strcmp(type, "file-end") == 0 ||
|
|
strcmp(type, "file-cancel") == 0 ||
|
|
strcmp(type, "file-accept") == 0 ||
|
|
strcmp(type, "file-done") == 0)) {
|
|
if (strcmp(type, "file-offer") == 0) {
|
|
DcJson *rej = dc_json_new_object();
|
|
|
|
(void)dc_json_object_set(rej, "type",
|
|
dc_json_new_string("file-reject"));
|
|
(void)dc_json_object_set(rej, "id",
|
|
dc_json_new_string(dc_json_string(
|
|
dc_json_get(json, "id"))));
|
|
(void)dc_json_object_set(rej, "reason",
|
|
dc_json_new_string("pairing required"));
|
|
if (conn != NULL)
|
|
(void)dc_local_conn_send_json(conn, rej);
|
|
dc_json_free(rej);
|
|
status(local, "file offer refused: peer is not paired");
|
|
}
|
|
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"));
|
|
|
|
add_message(local, peer, peer->name, text != NULL ? text : "", 0);
|
|
if (conn != NULL)
|
|
(void)dc_local_send_msg_ack(conn, dc_json_string(
|
|
dc_json_get(json, "client_id")));
|
|
} else if (strcmp(type, "file-offer") == 0) {
|
|
file_offer(local, peer, json);
|
|
} else if (strcmp(type, "file-data") == 0) {
|
|
if (peer->recv != NULL && id != NULL &&
|
|
strcmp(id, peer->recv_id) == 0)
|
|
(void)dc_file_recv_data(peer->recv,
|
|
dc_json_string(dc_json_get(json, "data")),
|
|
NULL, 0);
|
|
} else if (strcmp(type, "file-end") == 0) {
|
|
if (peer->recv != NULL) {
|
|
char err[256];
|
|
int ok = dc_file_recv_end(peer->recv, err,
|
|
sizeof(err)) == 0;
|
|
DcJson *done = dc_json_new_object();
|
|
char line[512];
|
|
|
|
(void)dc_json_object_set(done, "type",
|
|
dc_json_new_string("file-done"));
|
|
(void)dc_json_object_set(done, "id",
|
|
dc_json_new_string(peer->recv_id));
|
|
(void)dc_json_object_set(done, "ok",
|
|
dc_json_new_bool(ok));
|
|
if (conn != NULL)
|
|
(void)dc_local_conn_send_json(conn, done);
|
|
dc_json_free(done);
|
|
if (ok) {
|
|
snprintf(line, sizeof(line),
|
|
dc_text(TX_SAVED_TO, "saved to %s"),
|
|
dc_file_recv_path(peer->recv));
|
|
status(local, line);
|
|
} else {
|
|
status(local,
|
|
dc_text(TX_FILE_CHECK_FAILED,
|
|
"file check failed"));
|
|
}
|
|
dc_file_recv_cancel(peer->recv);
|
|
peer->recv = NULL;
|
|
}
|
|
} else if (strcmp(type, "file-cancel") == 0) {
|
|
if (peer->recv != NULL) {
|
|
dc_file_recv_cancel(peer->recv);
|
|
peer->recv = NULL;
|
|
}
|
|
if (peer->send != NULL) {
|
|
dc_file_send_free(peer->send);
|
|
peer->send = NULL;
|
|
}
|
|
} else if (strcmp(type, "file-accept") == 0) {
|
|
if (peer->send != NULL && id != NULL &&
|
|
strcmp(id, peer->send_id) == 0)
|
|
dc_file_send_start(peer->send);
|
|
} else if (strcmp(type, "file-done") == 0) {
|
|
if (peer->send != NULL) {
|
|
dc_file_send_free(peer->send);
|
|
peer->send = NULL;
|
|
status(local, dc_text(TX_FILE_SENT, "file sent"));
|
|
}
|
|
} else if (strcmp(type, "typing") == 0) {
|
|
status(local, dc_json_bool(dc_json_get(json, "on")) ?
|
|
"peer is typing" : "peer stopped typing");
|
|
}
|
|
}
|
|
|
|
/* ------------------------------- callbacks ------------------------------ */
|
|
|
|
static void
|
|
server_hello(DcLocalConn *conn, const DcJson *hello, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
const char *id = dc_json_string(dc_json_get(hello, "id"));
|
|
const char *pin = dc_json_string(dc_json_get(hello, "pin"));
|
|
Peer *peer;
|
|
int known;
|
|
|
|
if (id == NULL || pin == NULL)
|
|
return;
|
|
known = dc_pins_check(local->pins, id, pin);
|
|
peer = peer_get(local, id, pin);
|
|
if (peer == NULL)
|
|
return;
|
|
peer->conn = conn;
|
|
snprintf(peer->pin, sizeof(peer->pin), "%s", pin);
|
|
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 {
|
|
peer->blocked = 1;
|
|
(void)dc_pins_set_blocked(local->pins, id, 1);
|
|
(void)dc_pins_save(local->pins, local->config);
|
|
announce(local, peer, 0);
|
|
dc_local_conn_close(conn);
|
|
peer->conn = NULL;
|
|
}
|
|
}
|
|
|
|
static void
|
|
server_line(DcLocalConn *conn, const DcJson *json, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
|
|
handle_line(local, peer_find_conn(local, conn), conn, json);
|
|
}
|
|
|
|
static void
|
|
server_closed(DcLocalConn *conn, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
Peer *peer = peer_find_conn(local, conn);
|
|
|
|
if (peer != NULL) {
|
|
peer->conn = NULL;
|
|
announce(local, peer, 0);
|
|
}
|
|
}
|
|
|
|
static void
|
|
link_line(DcLocalPeer *link, const DcJson *json, void *arg)
|
|
{
|
|
Peer *peer = arg;
|
|
|
|
handle_line(peer->local, peer, dc_local_peer_conn(link), json);
|
|
}
|
|
|
|
static void
|
|
link_state(DcLocalPeer *link, int online, void *arg)
|
|
{
|
|
Peer *peer = arg;
|
|
DtLocal *local = peer->local;
|
|
DcLocalConn *conn = dc_local_peer_conn(link);
|
|
const char *id = conn != NULL ? dc_local_conn_peer_id(conn) : NULL;
|
|
const char *pin = conn != NULL ? dc_local_conn_peer_pin(conn) : NULL;
|
|
int known;
|
|
|
|
if (!online) {
|
|
peer->conn = NULL;
|
|
announce(local, peer, 0);
|
|
return;
|
|
}
|
|
if (id == NULL || pin == NULL)
|
|
return;
|
|
snprintf(peer->id, sizeof(peer->id), "%s", id);
|
|
snprintf(peer->pin, sizeof(peer->pin), "%s", pin);
|
|
peer->conn = conn;
|
|
known = dc_pins_check(local->pins, id, pin);
|
|
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 {
|
|
peer->blocked = 1;
|
|
(void)dc_pins_set_blocked(local->pins, id, 1);
|
|
(void)dc_pins_save(local->pins, local->config);
|
|
announce(local, peer, 0);
|
|
dc_local_peer_stop(link);
|
|
peer->conn = NULL;
|
|
}
|
|
}
|
|
|
|
/* ---------------------------- send hooks -------------------------------- */
|
|
|
|
static int
|
|
local_send(void *arg, const char *conv, const char *text)
|
|
{
|
|
DtLocal *local = arg;
|
|
Peer *peer = peer_find_id(local, conv);
|
|
|
|
if (peer == NULL || peer->conn == NULL)
|
|
return -1;
|
|
if (dc_local_send_msg(peer->conn, text, NULL) != 0)
|
|
return -1;
|
|
add_message(local, peer, "me", text, DTCHAT_MSG_SELF);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
local_file_send(const char *peer_id, const char *path, void *arg)
|
|
{
|
|
DtLocal *local = arg;
|
|
Peer *peer = peer_find_id(local, peer_id);
|
|
char err[256];
|
|
|
|
if (peer == NULL || peer->conn == NULL) {
|
|
status(local, "peer is offline");
|
|
return;
|
|
}
|
|
if (!peer->paired) {
|
|
status(local, dc_text(TX_PEER_NOT_PAIRED,
|
|
"peer is not paired"));
|
|
return;
|
|
}
|
|
snprintf(peer->send_id, sizeof(peer->send_id), "f%d", ++local->seq);
|
|
peer->send = dc_file_send_new(peer->conn, peer->send_id, path, err,
|
|
sizeof(err));
|
|
if (peer->send == NULL) {
|
|
status(local, err);
|
|
return;
|
|
}
|
|
status(local, "file offered");
|
|
}
|
|
|
|
/* ------------------------------- the timer ------------------------------ */
|
|
|
|
static void
|
|
pump_cb(XtPointer client, XtIntervalId *id)
|
|
{
|
|
DtLocal *local = client;
|
|
Peer *peer;
|
|
|
|
(void)id;
|
|
(void)dc_loop_run(local->loop, 0);
|
|
for (peer = local->state; peer != NULL; peer = peer->next) {
|
|
int rc;
|
|
|
|
if (peer->send == NULL)
|
|
continue;
|
|
rc = dc_file_send_step(peer->send);
|
|
if (rc == 1) {
|
|
dc_file_send_free(peer->send);
|
|
peer->send = NULL;
|
|
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)) {
|
|
/* the pump runs every 40 ms and a frame is 20 ms: take as
|
|
* many as the device has, or the sound falls behind real
|
|
* time (and the audio clock with it) */
|
|
int sent;
|
|
|
|
for (sent = 0; sent < 4 &&
|
|
mcde_sys_pcm_wait(peer->cap, 0) == 1; sent++) {
|
|
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)
|
|
break;
|
|
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);
|
|
}
|
|
}
|
|
if (peer->capture != NULL && peer->venc != NULL &&
|
|
dc_call_state(peer->call) == DC_CALL_IN_CALL) {
|
|
long long now = dc_loop_now_ms();
|
|
|
|
if (dc_call_wants_keyframe(peer->call))
|
|
(void)dc_call_request_keyframe(peer->call);
|
|
if (dc_call_take_keyframe_request(peer->call))
|
|
dc_vpx_enc_keyframe(peer->venc);
|
|
if (now - peer->video_last >= 1000 / DC_VIDEO_FPS &&
|
|
dc_capture_frame(peer->capture, peer->vframe,
|
|
0) == 1) {
|
|
size_t vlen = 0;
|
|
|
|
peer->video_last = now;
|
|
if (dc_vpx_enc_frame(peer->venc,
|
|
peer->vframe, peer->vpacket,
|
|
DC_VIDEO_FRAME_MAX, &vlen) == 1 &&
|
|
vlen > 0) {
|
|
int kf = (peer->vpacket[0] & 1) == 0;
|
|
|
|
if (dc_call_send_video(peer->call,
|
|
peer->video_id++, peer->vpacket,
|
|
vlen, kf) == 0) {
|
|
peer->video_out++;
|
|
if (local->call_debug &&
|
|
peer->video_out % 30 == 0)
|
|
fprintf(stderr,
|
|
"call %s: video out %u\n",
|
|
peer->name,
|
|
peer->video_out);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
video = malloc(DC_VIDEO_I420_LEN);
|
|
if (video != NULL) {
|
|
while (dc_call_video_out(peer->call, video,
|
|
DC_VIDEO_I420_LEN, &vlen, &keyframe,
|
|
&frame_id) == 1) {
|
|
dtchat_call_window_set_frame(
|
|
peer->call_window, video,
|
|
DC_VIDEO_W, DC_VIDEO_H);
|
|
peer->call_video++;
|
|
peer->video_in++;
|
|
if (local->call_debug &&
|
|
peer->video_in % 30 == 0)
|
|
fprintf(stderr,
|
|
"call %s: video in %u\n",
|
|
peer->name, peer->video_in);
|
|
}
|
|
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 - LAN only, no relay");
|
|
call_debug(local, peer, "in call");
|
|
call_video_start(local, peer);
|
|
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);
|
|
}
|
|
|
|
/* ------------------------------- start/stop ----------------------------- */
|
|
|
|
static int
|
|
split_host_port(const char *text, char *host, size_t hostlen, int *port)
|
|
{
|
|
const char *colon = strrchr(text, ':');
|
|
char *end = NULL;
|
|
long value;
|
|
|
|
if (colon == NULL) {
|
|
snprintf(host, hostlen, "127.0.0.1");
|
|
value = strtol(text, &end, 10);
|
|
} else {
|
|
size_t len = (size_t)(colon - text);
|
|
|
|
if (len >= hostlen)
|
|
return -1;
|
|
memcpy(host, text, len);
|
|
host[len] = '\0';
|
|
if (host[0] == '\0')
|
|
snprintf(host, hostlen, "127.0.0.1");
|
|
value = strtol(colon + 1, &end, 10);
|
|
}
|
|
if (end == NULL || *end != '\0' || value < 0 || value > 65535)
|
|
return -1;
|
|
*port = (int)value;
|
|
return 0;
|
|
}
|
|
|
|
DtLocal *
|
|
dtchat_local_start(DtChatUi *ui, DcModel *model, DcPeers *peers,
|
|
const char *config, const char *listen_text, const char *const *peer_text,
|
|
int npeers, const char *downloads)
|
|
{
|
|
DtLocal *local;
|
|
char host[128] = "127.0.0.1";
|
|
int port = 0;
|
|
int i;
|
|
|
|
if (ui == NULL || model == NULL || peers == NULL || config == NULL)
|
|
return NULL;
|
|
local = calloc(1, sizeof(*local));
|
|
if (local == NULL)
|
|
return NULL;
|
|
local->ui = ui;
|
|
local->model = model;
|
|
local->peers = peers;
|
|
local->app = XtWidgetToApplicationContext(dtchat_ui_shell(ui));
|
|
local->identity = dc_identity_load_or_create(config);
|
|
local->pins = dc_pins_load(config);
|
|
if (local->identity == NULL || local->pins == NULL) {
|
|
status(local, "local: cannot load the identity");
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
local->config = strdup(config);
|
|
local->downloads = strdup(downloads != NULL ? downloads : config);
|
|
if (local->config == NULL || local->downloads == NULL) {
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
local->loop = dc_loop_new();
|
|
if (local->loop == NULL) {
|
|
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) {
|
|
status(local, "local: bad --listen");
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
local->server = dc_local_server_new(local->loop,
|
|
local->identity, host, port, NULL, NULL, server_hello,
|
|
server_line, server_closed, local);
|
|
if (local->server == NULL) {
|
|
status(local, "local: cannot listen");
|
|
dtchat_local_stop(local);
|
|
return NULL;
|
|
}
|
|
local->port = dc_local_server_port(local->server);
|
|
}
|
|
for (i = 0; i < npeers; i++) {
|
|
Peer *peer;
|
|
char phost[128];
|
|
int pport;
|
|
|
|
if (split_host_port(peer_text[i], phost, sizeof(phost),
|
|
&pport) != 0 || pport <= 0)
|
|
continue;
|
|
peer = peer_get(local, peer_text[i], "");
|
|
if (peer == NULL)
|
|
continue;
|
|
if (peer->link != NULL)
|
|
continue;
|
|
peer->link = dc_local_peer_new(local->loop, local->identity,
|
|
phost, pport, NULL, link_line, link_state, peer);
|
|
if (peer->link != NULL)
|
|
(void)dc_local_peer_start(peer->link);
|
|
}
|
|
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];
|
|
|
|
snprintf(line, sizeof(line),
|
|
"local: listening on port %d, %d peer link(s)",
|
|
local->server != NULL ?
|
|
dc_local_server_port(local->server) : 0, npeers);
|
|
status(local, line);
|
|
}
|
|
local->timer = XtAppAddTimeOut(local->app, 40, pump_cb, local);
|
|
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)
|
|
{
|
|
Peer *peer;
|
|
|
|
if (local == NULL)
|
|
return;
|
|
if (local->timer != 0) {
|
|
(void)XtRemoveTimeOut(local->timer);
|
|
local->timer = 0;
|
|
}
|
|
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)
|
|
dc_file_recv_cancel(peer->recv);
|
|
if (peer->link != NULL)
|
|
dc_local_peer_free(peer->link);
|
|
free(peer);
|
|
}
|
|
dc_local_server_free(local->server);
|
|
if (local->loop != NULL)
|
|
dc_loop_free(local->loop);
|
|
dc_pins_free(local->pins);
|
|
dc_identity_free(local->identity);
|
|
free(local->config);
|
|
free(local->downloads);
|
|
free(local);
|
|
}
|