After the bind the adapter enables XEP-0280 carbons and unwraps sent/received forwarded messages: a message another of our resources sent becomes a normal message event with self=true and the contact as conv, a received one self=false. XEP-0085 chat states are handled before the body: composing emits typing on, paused/active/inactive/gone off. The typing command sends composing or paused to the conversation, and dc_adapter_send_typing() puts it on the core side. tests/xmpp_fake.py checks carbons were enabled, the forwarded message (self, conv bob, text), typing on/off from bob, and that a typing command reaches the server as <composing> addressed to bob. Standalone adapter: all checks ok. The UI does not send typing for adapter accounts yet (adapter account wiring is still open), which the commit documents.
729 lines
30 KiB
C
729 lines
30 KiB
C
/*
|
|
* dc_internal.h -- private types of libDtChat
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* Nothing here is installed. The public surface is include/DtChat/.
|
|
*/
|
|
#ifndef _DtChat_dc_internal_h
|
|
#define _DtChat_dc_internal_h
|
|
|
|
#include "DtChat/DtChat.h"
|
|
#include "DtChat/Ipc.h"
|
|
|
|
#include <poll.h>
|
|
#include <stdint.h>
|
|
|
|
/*
|
|
* The core's event loop: one poll(2) per run, monotonic one-shot timers and
|
|
* a wakeup pipe for the UI and for adapter threads. No Xt, no threads
|
|
* required. All functions are private to libDtChat.
|
|
*/
|
|
typedef struct DcLoop DcLoop;
|
|
|
|
typedef void (*DcLoopFdFn)(int fd, short revents, void *arg);
|
|
typedef void (*DcLoopTimerFn)(void *arg);
|
|
|
|
DcLoop *dc_loop_new(void);
|
|
void dc_loop_free(DcLoop *loop);
|
|
|
|
/* milliseconds from CLOCK_MONOTONIC; never goes backwards */
|
|
long long dc_loop_now_ms(void);
|
|
|
|
/*
|
|
* Watch FD for EVENTS (POLLIN/POLLOUT). Returns an id for
|
|
* dc_loop_del_fd(), or -1. The id is unique among fds and timers.
|
|
*/
|
|
int dc_loop_add_fd(DcLoop *loop, int fd, short events, DcLoopFdFn fn,
|
|
void *arg);
|
|
int dc_loop_del_fd(DcLoop *loop, int id);
|
|
|
|
/*
|
|
* One-shot timer: call FN(ARG) after DELAY_MS. DELAY_MS must be >= 0.
|
|
* Returns an id for dc_loop_del_timer(), or -1. Equal deadlines fire in
|
|
* the order they were added; a callback may add or delete timers.
|
|
*/
|
|
int dc_loop_add_timer(DcLoop *loop, long long delay_ms, DcLoopTimerFn fn,
|
|
void *arg);
|
|
int dc_loop_del_timer(DcLoop *loop, int id);
|
|
|
|
/* Wake a blocking dc_loop_run() from another thread/signal handler. */
|
|
int dc_loop_wake(DcLoop *loop);
|
|
|
|
/* Read end of the wakeup pipe, for XtAppAddInput() or poll(). */
|
|
int dc_loop_wakeup_fd(const DcLoop *loop);
|
|
|
|
/*
|
|
* Poll once and dispatch ready fds and due timers. timeout_ms < 0 waits
|
|
* until an fd, the wakeup pipe or the next timer; 0 does not wait.
|
|
* Returns the number of dispatched events (a wakeup counts as one), 0 on
|
|
* timeout, -1 on a poll() error other than EINTR (which returns 0).
|
|
*/
|
|
int dc_loop_run(DcLoop *loop, int timeout_ms);
|
|
|
|
/*
|
|
* The store: ~/.dt/dtchat with accounts.json, secrets.json, identity.json.
|
|
* The directory is 0700, every file 0600. Writes are atomic (write a
|
|
* temporary file, fsync, rename, fsync the directory). A file that is not
|
|
* valid JSON is renamed to NAME.bad and reported as missing/corrupt; it is
|
|
* never silently parsed or overwritten.
|
|
*/
|
|
typedef struct DcStore DcStore;
|
|
|
|
DcStore *dc_store_open(const char *dir); /* NULL: $HOME/.dt/dtchat */
|
|
void dc_store_close(DcStore *store);
|
|
const char *dc_store_dir(const DcStore *store);
|
|
|
|
/*
|
|
* Load/save one named file. NAME is a base name (no '/', no ".."). On a
|
|
* missing file dc_store_load() returns NULL and sets *MISSING to 1; on a
|
|
* corrupt file it sets *MISSING to 0, writes a message to ERR and moves the
|
|
* file to NAME.bad. ERR may be NULL.
|
|
*/
|
|
DcJson *dc_store_load(DcStore *store, const char *name, int *missing,
|
|
char *err, size_t errlen);
|
|
int dc_store_save(DcStore *store, const char *name, const DcJson *json);
|
|
|
|
/* Typed wrappers with their empty defaults. */
|
|
DcJson *dc_store_load_accounts(DcStore *store); /* {"accounts":[]} */
|
|
int dc_store_save_accounts(DcStore *store, const DcJson *json);
|
|
DcJson *dc_store_load_secrets(DcStore *store); /* {} */
|
|
int dc_store_save_secrets(DcStore *store, const DcJson *json);
|
|
DcJson *dc_store_load_identity(DcStore *store); /* no default: NULL */
|
|
int dc_store_save_identity(DcStore *store, const DcJson *json);
|
|
|
|
/*
|
|
* The chat model: accounts, conversations and a 2000-message ring buffer
|
|
* per conversation. Strings are copied; the model owns them. Message
|
|
* index 0 is the oldest one still in the ring; dc_conv_message_at()
|
|
* returns NULL past the end. Unread counts ignore our own and system
|
|
* messages.
|
|
*/
|
|
typedef struct DcModel DcModel;
|
|
typedef struct DcAccount DcAccount;
|
|
typedef struct DcConv DcConv;
|
|
|
|
typedef enum {
|
|
DC_CONV_DIRECT,
|
|
DC_CONV_CHANNEL,
|
|
DC_CONV_GROUP,
|
|
DC_CONV_NEARBY
|
|
} DcConvKind;
|
|
|
|
typedef enum {
|
|
DC_PRESENCE_OFFLINE,
|
|
DC_PRESENCE_ONLINE,
|
|
DC_PRESENCE_AWAY,
|
|
DC_PRESENCE_DND
|
|
} DcPresence;
|
|
|
|
typedef struct {
|
|
char *from;
|
|
char *text;
|
|
long long ts;
|
|
int flags; /* DTCHAT_MSG_* from DtChat.h */
|
|
} DcMessage;
|
|
|
|
DcModel *dc_model_new(void);
|
|
void dc_model_free(DcModel *model);
|
|
|
|
size_t dc_model_account_count(const DcModel *model);
|
|
DcAccount *dc_model_account_at(const DcModel *model, size_t index);
|
|
DcAccount *dc_model_find_account(const DcModel *model, const char *id);
|
|
/* Idempotent: an existing id updates protocol/label and is returned. */
|
|
DcAccount *dc_model_add_account(DcModel *model, const char *id,
|
|
const char *protocol, const char *label);
|
|
int dc_model_remove_account(DcModel *model, const char *id);
|
|
|
|
const char *dc_account_id(const DcAccount *account);
|
|
const char *dc_account_protocol(const DcAccount *account);
|
|
const char *dc_account_label(const DcAccount *account);
|
|
size_t dc_account_conv_count(const DcAccount *account);
|
|
DcConv *dc_account_conv_at(const DcAccount *account, size_t index);
|
|
|
|
DcConv *dc_model_find_conv(const DcModel *model, const char *account_id,
|
|
const char *conv_id);
|
|
/* Idempotent: an existing conversation gets the new kind/title. */
|
|
DcConv *dc_model_add_conv(DcModel *model, const char *account_id,
|
|
const char *conv_id, DcConvKind kind, const char *title);
|
|
|
|
const char *dc_conv_id(const DcConv *conv);
|
|
const char *dc_conv_title(const DcConv *conv);
|
|
const char *dc_conv_topic(const DcConv *conv);
|
|
DcConvKind dc_conv_kind(const DcConv *conv);
|
|
void dc_conv_set_topic(DcConv *conv, const char *topic);
|
|
void dc_conv_set_presence(DcConv *conv, DcPresence presence);
|
|
DcPresence dc_conv_presence(const DcConv *conv);
|
|
|
|
int dc_conv_add_message(DcConv *conv, const char *from, const char *text,
|
|
long long ts, int flags);
|
|
size_t dc_conv_message_count(const DcConv *conv);
|
|
const DcMessage *dc_conv_message_at(const DcConv *conv, size_t index);
|
|
int dc_conv_unread(const DcConv *conv);
|
|
void dc_conv_mark_read(DcConv *conv);
|
|
|
|
/*
|
|
* The local identity: one Ed25519 key, a self-signed X.509 certificate and
|
|
* the SHA-256 pin of the certificate's DER form. identity.json in DIR is
|
|
* 0600 (DIR 0700) and written atomically through the store. A corrupt file
|
|
* is moved aside and load_or_create() fails instead of silently replacing it;
|
|
* a missing one is generated and saved. The strings belong to the identity.
|
|
*/
|
|
typedef struct DcIdentity DcIdentity;
|
|
|
|
DcIdentity *dc_identity_load_or_create(const char *dir);
|
|
int dc_identity_save(const DcIdentity *id, const char *dir);
|
|
void dc_identity_free(DcIdentity *id);
|
|
const char *dc_identity_pin(const DcIdentity *id);
|
|
const char *dc_identity_public_b64(const DcIdentity *id);
|
|
const char *dc_identity_cert_pem(const DcIdentity *id);
|
|
const char *dc_identity_key_pem(const DcIdentity *id);
|
|
|
|
/*
|
|
* Local peers. One peer per identity id; dc_peers_announce() takes the
|
|
* service's TXT record ("v=1;id=...;name=...;pin=...;port=...", keys joined
|
|
* with ';'), merges it by id and marks the peer online. A service that goes
|
|
* away is dc_peers_offline(); a manual peer is not touched by that. The
|
|
* block flag belongs to the peer and survives announcements. Strings belong
|
|
* to the peer.
|
|
*/
|
|
typedef struct DcPeer DcPeer;
|
|
typedef struct DcPeers DcPeers;
|
|
|
|
DcPeers *dc_peers_new(void);
|
|
void dc_peers_free(DcPeers *peers);
|
|
size_t dc_peers_count(const DcPeers *peers);
|
|
DcPeer *dc_peers_at(const DcPeers *peers, size_t index);
|
|
DcPeer *dc_peers_find(const DcPeers *peers, const char *id);
|
|
int dc_peers_announce(DcPeers *peers, const char *txt, const char *host,
|
|
int port);
|
|
int dc_peers_offline(DcPeers *peers, const char *id);
|
|
DcPeer *dc_peers_add_manual(DcPeers *peers, const char *id, const char *name,
|
|
const char *host, int port, const char *pin);
|
|
void dc_peer_set_blocked(DcPeer *peer, int blocked);
|
|
const char *dc_peer_id(const DcPeer *peer);
|
|
const char *dc_peer_name(const DcPeer *peer);
|
|
const char *dc_peer_host(const DcPeer *peer);
|
|
const char *dc_peer_pin(const DcPeer *peer);
|
|
int dc_peer_port(const DcPeer *peer);
|
|
int dc_peer_online(const DcPeer *peer);
|
|
int dc_peer_blocked(const DcPeer *peer);
|
|
int dc_peer_manual(const DcPeer *peer);
|
|
|
|
/*
|
|
* The local peer listener. dc_local_server_new() binds HOST (NULL or
|
|
* "0.0.0.0" = all interfaces) and PORT (0 = ephemeral); the identity is the
|
|
* TLS server certificate. A peer sends {"type":"hello","id":...,"pin":...};
|
|
* ID and PIN are checked against EXPECT_ID/EXPECT_PIN (either may be NULL)
|
|
* and a hello-ack answers ok true/false. A wrong PIN closes the connection.
|
|
* FN is called once with the accepted hello (owned by the connection and
|
|
* valid only during the call). Everything runs from DcLoop.
|
|
*/
|
|
typedef struct DcLocalServer DcLocalServer;
|
|
typedef struct DcLocalConn DcLocalConn;
|
|
typedef struct DcLocalPeer DcLocalPeer;
|
|
typedef void (*DcLocalHelloFn)(DcLocalConn *conn, const DcJson *hello,
|
|
void *arg);
|
|
typedef void (*DcLocalLineFn)(DcLocalConn *conn, const DcJson *json,
|
|
void *arg);
|
|
typedef void (*DcLocalReadyFn)(DcLocalConn *conn, int ok, void *arg);
|
|
typedef void (*DcLocalCloseFn)(DcLocalConn *conn, void *arg);
|
|
typedef void (*DcLocalPeerLineFn)(DcLocalPeer *peer, const DcJson *json,
|
|
void *arg);
|
|
typedef void (*DcLocalPeerStateFn)(DcLocalPeer *peer, int online, void *arg);
|
|
|
|
DcLocalServer *dc_local_server_new(DcLoop *loop, const DcIdentity *identity,
|
|
const char *host, int port, const char *expect_id, const char *expect_pin,
|
|
DcLocalHelloFn hello, DcLocalLineFn line, DcLocalCloseFn closed,
|
|
void *arg);
|
|
int dc_local_server_port(const DcLocalServer *server);
|
|
void dc_local_server_free(DcLocalServer *server);
|
|
|
|
DcLocalConn *dc_local_connect(DcLoop *loop, const DcIdentity *identity,
|
|
const char *host, int port, const char *expect_pin,
|
|
DcLocalLineFn line, DcLocalReadyFn ready, DcLocalCloseFn closed,
|
|
void *arg);
|
|
int dc_local_conn_send_json(DcLocalConn *conn, const DcJson *json);
|
|
int dc_local_conn_busy(const DcLocalConn *conn);
|
|
int dc_local_send_presence(DcLocalConn *conn, const char *show,
|
|
const char *status);
|
|
int dc_local_send_msg(DcLocalConn *conn, const char *text,
|
|
const char *client_id);
|
|
int dc_local_send_msg_ack(DcLocalConn *conn, const char *client_id);
|
|
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
|
|
* callback and the line callback for the ready connection. Send fails while
|
|
* offline; the peer does not queue messages.
|
|
*/
|
|
DcLocalPeer *dc_local_peer_new(DcLoop *loop, const DcIdentity *identity,
|
|
const char *host, int port, const char *expect_pin,
|
|
DcLocalPeerLineFn line, DcLocalPeerStateFn state_fn, void *arg);
|
|
int dc_local_peer_start(DcLocalPeer *peer);
|
|
void dc_local_peer_stop(DcLocalPeer *peer);
|
|
void dc_local_peer_free(DcLocalPeer *peer);
|
|
int dc_local_peer_send(DcLocalPeer *peer, const DcJson *json);
|
|
int dc_local_peer_online(const DcLocalPeer *peer);
|
|
DcLocalConn *dc_local_peer_conn(const DcLocalPeer *peer);
|
|
|
|
/*
|
|
* Local file transfer. A sender offers name/size/sha256, sends 64 KiB
|
|
* chunks as base64 file-data lines and a file-end; the receiver writes
|
|
* NAME.part, enforces 512 MiB, verifies the hash and renames to a sanitized
|
|
* name (no overwrite). dc_file_send_step() sends one chunk; 0 means call
|
|
* again (the connection may be busy), 1 means the file-end is out.
|
|
*/
|
|
#define DC_FILE_MAX (512LL * 1024 * 1024)
|
|
#define DC_FILE_CHUNK (64 * 1024)
|
|
|
|
typedef struct DcFileSend DcFileSend;
|
|
typedef struct DcFileRecv DcFileRecv;
|
|
|
|
char *dc_file_safe_name(const char *name);
|
|
int dc_file_sha256(const char *path, char out[65], long long *size);
|
|
DcFileSend *dc_file_send_new(DcLocalConn *conn, const char *id,
|
|
const char *path, char *err, size_t errlen);
|
|
void dc_file_send_start(DcFileSend *send);
|
|
int dc_file_send_step(DcFileSend *send);
|
|
void dc_file_send_cancel(DcFileSend *send, const char *reason);
|
|
void dc_file_send_free(DcFileSend *send);
|
|
DcFileRecv *dc_file_recv_new(const char *dir, const DcJson *offer, char *err,
|
|
size_t errlen);
|
|
int dc_file_recv_data(DcFileRecv *recv, const char *b64, char *err,
|
|
size_t errlen);
|
|
int dc_file_recv_end(DcFileRecv *recv, char *err, size_t errlen);
|
|
void dc_file_recv_cancel(DcFileRecv *recv);
|
|
const char *dc_file_recv_path(const DcFileRecv *recv);
|
|
long long dc_file_recv_got(const DcFileRecv *recv);
|
|
|
|
/*
|
|
* Pairing and TOFU. dc_pair_sas() derives the 6-digit code from both
|
|
* certificate pins (order independent). peers.json keeps id -> pin and the
|
|
* blocked flag (0600 through the store); a changed pin answers -1 in
|
|
* dc_pins_check() and stays blocked until dc_pins_confirm() stores the new
|
|
* pin after the person compared a new SAS.
|
|
*/
|
|
char *dc_pair_sas(const char *pin_a, const char *pin_b);
|
|
|
|
typedef struct DcPins DcPins;
|
|
DcPins *dc_pins_load(const char *dir);
|
|
int dc_pins_save(DcPins *pins, const char *dir);
|
|
void dc_pins_free(DcPins *pins);
|
|
int dc_pins_check(const DcPins *pins, const char *id, const char *pin);
|
|
int dc_pins_confirm(DcPins *pins, const char *id, const char *pin);
|
|
int dc_pins_set_blocked(DcPins *pins, const char *id, int blocked);
|
|
int dc_pins_is_blocked(const DcPins *pins, const char *id);
|
|
size_t dc_pins_count(const DcPins *pins);
|
|
const char *dc_pins_id_at(const DcPins *pins, size_t index);
|
|
const char *dc_pins_pin_at(const DcPins *pins, size_t index);
|
|
|
|
/*
|
|
* Message catalog "dtchat" (set 1). The program calls setlocale() and
|
|
* dc_i18n_init() once; dc_text() returns the translation for ID or FALLBACK
|
|
* when no catalog is available. The catalog is built from dtchat.msg
|
|
* (C and each locale under programs/localized/<locale>/msg).
|
|
*/
|
|
void dc_i18n_init(void);
|
|
void dc_i18n_close(void);
|
|
const char *dc_text(int id, const char *fallback);
|
|
|
|
/*
|
|
* Audio: Opus 48 kHz mono, 20 ms frames, 24 kbit/s, in-band FEC and PLC.
|
|
* dc_opus_enc_frame() encodes exactly DC_AUDIO_FRAME_SAMPLES samples;
|
|
* dc_opus_dec_frame() with LEN 0 conceals a lost frame.
|
|
*/
|
|
#define DC_AUDIO_RATE 48000
|
|
#define DC_AUDIO_FRAME_MS 20
|
|
#define DC_AUDIO_FRAME_SAMPLES (DC_AUDIO_RATE * DC_AUDIO_FRAME_MS / 1000)
|
|
|
|
typedef struct DcOpusEnc DcOpusEnc;
|
|
typedef struct DcOpusDec DcOpusDec;
|
|
|
|
DcOpusEnc *dc_opus_enc_new(int bitrate, char *err, size_t errlen);
|
|
int dc_opus_enc_frame(DcOpusEnc *enc, const int16_t *pcm, unsigned char *out,
|
|
size_t outlen);
|
|
void dc_opus_enc_free(DcOpusEnc *enc);
|
|
DcOpusDec *dc_opus_dec_new(char *err, size_t errlen);
|
|
int dc_opus_dec_frame(DcOpusDec *dec, const unsigned char *packet, size_t len,
|
|
int16_t *pcm);
|
|
void dc_opus_dec_free(DcOpusDec *dec);
|
|
|
|
/*
|
|
* Media packets (T6.3). X25519 + HKDF-SHA256 give two 32-byte keys per
|
|
* session (send and recv, mirrored by the two sides); ChaCha20-Poly1305
|
|
* seals the payload, the 12-byte header is the additional data. The replay
|
|
* window is the 64-packet slider of RFC 3711.
|
|
*/
|
|
#define DC_MEDIA_VERSION 1
|
|
#define DC_MEDIA_KEY_LEN 32
|
|
#define DC_MEDIA_TAG_LEN 16
|
|
#define DC_MEDIA_MAX_PAYLOAD 1200
|
|
|
|
enum {
|
|
DC_MEDIA_AUDIO = 1,
|
|
DC_MEDIA_VIDEO = 2,
|
|
DC_MEDIA_PING = 3,
|
|
DC_MEDIA_BYE = 4,
|
|
DC_MEDIA_PTT = 5,
|
|
DC_MEDIA_RING = 6,
|
|
DC_MEDIA_ACCEPT = 7,
|
|
DC_MEDIA_BUSY = 8,
|
|
DC_MEDIA_HANGUP = 9,
|
|
DC_MEDIA_KFREQ = 10
|
|
};
|
|
|
|
typedef struct {
|
|
unsigned char send[DC_MEDIA_KEY_LEN];
|
|
unsigned char recv[DC_MEDIA_KEY_LEN];
|
|
} DcMediaKeys;
|
|
|
|
typedef struct {
|
|
unsigned char key[DC_MEDIA_KEY_LEN];
|
|
uint64_t seq;
|
|
} DcMediaSend;
|
|
|
|
typedef struct {
|
|
unsigned char key[DC_MEDIA_KEY_LEN];
|
|
uint64_t highest;
|
|
uint64_t mask;
|
|
int started;
|
|
} DcMediaRecv;
|
|
|
|
/* a new X25519 key pair: public in PUB, the secret in PRIV */
|
|
int dc_media_keypair(unsigned char pub[DC_MEDIA_KEY_LEN],
|
|
unsigned char priv[DC_MEDIA_KEY_LEN]);
|
|
/* the two directions of a session; INITIATOR 1 on the side that started */
|
|
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,
|
|
const unsigned char key[DC_MEDIA_KEY_LEN]);
|
|
/* the packet length, or 0 (too small a buffer, bad type, too much data) */
|
|
size_t dc_media_seal(DcMediaSend *send, int type, uint32_t ts,
|
|
const void *plain, size_t len, unsigned char *out, size_t outlen);
|
|
/* 1 and the payload/type/timestamp, or -1 (changed header or payload,
|
|
* wrong key, replay, too short) */
|
|
int dc_media_open(DcMediaRecv *recv, const unsigned char *packet, size_t len,
|
|
int *type, uint32_t *ts, void *plain, size_t *plainlen);
|
|
/* 1 when SEQ is new and inside the window (it is marked seen), else 0 */
|
|
int dc_media_replay_ok(DcMediaRecv *recv, uint32_t seq);
|
|
|
|
/*
|
|
* The jitter buffer of a call (T6.4). DEPTH frames must be buffered before
|
|
* the first frame comes out; then frames leave in sequence order, a gap that
|
|
* a later frame proves lost yields one frame of length 0 (the caller conceals
|
|
* it), duplicates and late frames are dropped, and SLOTS bounds the buffer
|
|
* (at most 32, the default).
|
|
*/
|
|
typedef struct DcJitter DcJitter;
|
|
|
|
DcJitter *dc_jitter_new(size_t depth, size_t slots);
|
|
void dc_jitter_free(DcJitter *jitter);
|
|
int dc_jitter_put(DcJitter *jitter, uint32_t seq, uint32_t ts,
|
|
const void *data, size_t len);
|
|
/* 1: one frame (LEN 0 = lost, conceal it), 0: wait for more */
|
|
int dc_jitter_get(DcJitter *jitter, uint32_t *ts, unsigned char *out,
|
|
size_t outlen, size_t *len);
|
|
|
|
/*
|
|
* An audio call: one UDP socket to PEER_HOST:PEER_PORT, the two direction
|
|
* keys, Opus 48 kHz mono and the jitter buffer. The caller pumps it from its
|
|
* loop with dc_call_fd: dc_call_drain() reads and authenticates everything
|
|
* that arrived, dc_call_audio_out() decodes one frame (0: nothing ready),
|
|
* dc_call_audio_in() encodes and sends one 20 ms frame while the person
|
|
* holds the key, and dc_call_send_ptt() tells the peer about the key. A
|
|
* packet that does not authenticate is dropped without touching the jitter
|
|
* buffer. The call owns no threads and no device.
|
|
*/
|
|
typedef struct DcMediaCall DcMediaCall;
|
|
|
|
typedef enum {
|
|
DC_CALL_IDLE = 0,
|
|
DC_CALL_RINGING,
|
|
DC_CALL_IN_CALL,
|
|
DC_CALL_ENDED
|
|
} DcCallState;
|
|
|
|
DcMediaCall *dc_call_new(const char *peer_host, int peer_port,
|
|
const DcMediaSend *send, const DcMediaRecv *recv, char *err,
|
|
size_t errlen);
|
|
/* 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);
|
|
/* 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
|
|
DcCallState dc_call_state(const DcMediaCall *call);
|
|
int dc_call_ring(DcMediaCall *call);
|
|
int dc_call_accept(DcMediaCall *call);
|
|
int dc_call_hangup(DcMediaCall *call);
|
|
/* call from the loop: ends an unanswered ring */
|
|
void dc_call_tick(DcMediaCall *call);
|
|
int dc_call_send_ptt(DcMediaCall *call, int on);
|
|
int dc_call_drain(DcMediaCall *call);
|
|
int dc_call_audio_in(DcMediaCall *call, const int16_t *pcm);
|
|
int dc_call_audio_out(DcMediaCall *call, int16_t *pcm);
|
|
/* one encoded frame out (fragmented) and the completed frames in */
|
|
int dc_call_send_video(DcMediaCall *call, uint16_t frame_id,
|
|
const unsigned char *frame, size_t len, int keyframe);
|
|
/* the same with a caller-chosen timestamp (the send path uses the elapsed
|
|
* time since the call began; a test or a recording can say when the frame
|
|
* belongs) */
|
|
int dc_call_send_video_at(DcMediaCall *call, uint32_t ts, uint16_t frame_id,
|
|
const unsigned char *frame, size_t len, int keyframe);
|
|
/* 1: a decoded I420 frame to render (DC_VIDEO_W x DC_VIDEO_H), 0: nothing
|
|
* complete or not decodable yet, -1: a frame was dropped because it was more
|
|
* than DC_SYNC_LATE_MS behind the audio (or OUT is too small) */
|
|
int dc_call_video_out(DcMediaCall *call, unsigned char *out, size_t outlen,
|
|
size_t *len, int *keyframe, uint16_t *frame_id);
|
|
/* the reassembled VP8 packet itself, as it came over the wire (for tests
|
|
* and a recording); same return values as dc_call_video_out */
|
|
int dc_call_video_packet(DcMediaCall *call, unsigned char *out, size_t outlen,
|
|
size_t *len, int *keyframe, uint16_t *frame_id);
|
|
/* the last rendered frame's video - audio offset, and the number of frames
|
|
* dropped because they were more than DC_SYNC_LATE_MS late */
|
|
long long dc_call_sync_offset(const DcMediaCall *call);
|
|
int dc_call_video_dropped(const DcMediaCall *call);
|
|
/* our reassembler lost a frame: ask the peer for a keyframe */
|
|
int dc_call_wants_keyframe(DcMediaCall *call);
|
|
int dc_call_request_keyframe(DcMediaCall *call);
|
|
/* the peer asked us for a keyframe: 1 once (the request is consumed) */
|
|
int dc_call_take_keyframe_request(DcMediaCall *call);
|
|
DcJitter *dc_call_jitter(const DcMediaCall *call);
|
|
void dc_call_free(DcMediaCall *call);
|
|
|
|
/*
|
|
* Video: VP8 through libvpx (T7.1), I420 frames, realtime, CBR, one
|
|
* keyframe every two seconds. dc_vpx_enc_frame() encodes one frame into OUT and
|
|
* sets *OUTLEN; dc_vpx_dec_frame() decodes one packet into I420 (1: a frame,
|
|
* 0: none, -1: an error). The default is 640x360 at 15 fps and 500 kbit/s.
|
|
*/
|
|
#define DC_VIDEO_W 640
|
|
#define DC_VIDEO_H 360
|
|
#define DC_VIDEO_FPS 15
|
|
#define DC_VIDEO_KBPS 500
|
|
|
|
typedef struct DcVpxEnc DcVpxEnc;
|
|
typedef struct DcVpxDec DcVpxDec;
|
|
|
|
DcVpxEnc *dc_vpx_enc_new(int width, int height, int fps, int bitrate,
|
|
char *err, size_t errlen);
|
|
/* the next frame is a keyframe (what a receiver asks for after a loss) */
|
|
void dc_vpx_enc_keyframe(DcVpxEnc *enc);
|
|
int dc_vpx_enc_frame(DcVpxEnc *enc, const uint8_t *i420, unsigned char *out,
|
|
size_t outlen, size_t *outlen_out);
|
|
void dc_vpx_enc_free(DcVpxEnc *enc);
|
|
DcVpxDec *dc_vpx_dec_new(int width, int height, char *err, size_t errlen);
|
|
int dc_vpx_dec_frame(DcVpxDec *dec, const unsigned char *data, size_t len,
|
|
uint8_t *i420);
|
|
void dc_vpx_dec_free(DcVpxDec *dec);
|
|
|
|
/*
|
|
* One VP8 frame travels as DC_MEDIA_VIDEO fragments of at most 1200 bytes
|
|
* (T7.4). A fragment is an 8-byte header (flags start/end/keyframe,
|
|
* reserved, frame id, index, count, big-endian) and up to 1192 bytes. The
|
|
* reassembler holds one frame at a time, accepts fragments out of order,
|
|
* drops an incomplete frame when the next frame id starts and then asks for a
|
|
* keyframe. The frame buffer is bounded by DC_VIDEO_FRAME_MAX.
|
|
*/
|
|
#define DC_VIDEO_FRAME_MAX (256 * 1024) /* one compressed frame */
|
|
#define DC_VIDEO_I420_LEN (DC_VIDEO_W * DC_VIDEO_H * 3 / 2)
|
|
|
|
/*
|
|
* Camera capture (T7.1): V4L2 on Linux, the test pattern elsewhere or when
|
|
* DEVICE is "pattern". DEVICE "auto" tries /dev/video0..3, NULL means the
|
|
* pattern. A camera with another size than WIDTHxHEIGHT is scaled to it, so
|
|
* a caller always gets the size it asked for. dc_capture_frame() answers 1
|
|
* with a frame, 0 when none arrived within TIMEOUT_MS, -1 on an error.
|
|
*/
|
|
typedef struct DcCapture DcCapture;
|
|
|
|
DcCapture *dc_capture_open(int width, int height, int fps,
|
|
const char *device, char *err, size_t errlen);
|
|
int dc_capture_frame(DcCapture *cap, unsigned char *i420, int timeout_ms);
|
|
const char *dc_capture_backend(const DcCapture *cap);
|
|
void dc_capture_close(DcCapture *cap);
|
|
|
|
typedef struct DcVideoReasm DcVideoReasm;
|
|
|
|
size_t dc_video_fragment_len(void);
|
|
int dc_video_fragment_count(size_t len);
|
|
int dc_video_fragment_make(uint16_t frame_id, int keyframe, int index,
|
|
int count, const unsigned char *frame, size_t len, unsigned char *out,
|
|
size_t outlen, size_t *outlen_out);
|
|
DcVideoReasm *dc_video_reasm_new(size_t max_frame);
|
|
void dc_video_reasm_free(DcVideoReasm *reasm);
|
|
int dc_video_reasm_put(DcVideoReasm *reasm, uint32_t ts,
|
|
const unsigned char *payload, size_t len);
|
|
int dc_video_reasm_ready(const DcVideoReasm *reasm);
|
|
int dc_video_reasm_frame(DcVideoReasm *reasm, unsigned char *out,
|
|
size_t outlen, size_t *len_out, int *keyframe, uint16_t *frame_id,
|
|
uint32_t *ts);
|
|
int dc_video_reasm_wants_keyframe(DcVideoReasm *reasm);
|
|
|
|
/*
|
|
* A/V sync (T7.5): the audio is the clock. dc_sync_audio() advances it by
|
|
* one 20 ms frame per played audio frame; dc_sync_video() takes a video
|
|
* timestamp in the same milliseconds and answers 0 when the frame is more
|
|
* than DC_SYNC_LATE_MS behind the audio (the caller drops it) and 1 when it
|
|
* should be rendered. The offset of the last rendered frame is video -
|
|
* audio, so the lip-sync budget is |offset| <= DC_SYNC_LATE_MS for a frame
|
|
* that was on time.
|
|
*/
|
|
#define DC_SYNC_LATE_MS 80
|
|
|
|
typedef struct {
|
|
long long audio_ms;
|
|
long long offset_ms;
|
|
int rendered;
|
|
int dropped;
|
|
} DcSync;
|
|
|
|
void dc_sync_init(DcSync *sync);
|
|
void dc_sync_audio(DcSync *sync, int frames);
|
|
int dc_sync_video(DcSync *sync, long long video_ms);
|
|
long long dc_sync_offset(const DcSync *sync);
|
|
int dc_sync_dropped(const DcSync *sync);
|
|
|
|
/*
|
|
* Rendering (T7.2): bilinear I420 scaling and the BT.601 limited-range
|
|
* conversion to 0x00RRGGBB pixels for an XImage. STRIDE_PX is in pixels
|
|
* and defaults to W.
|
|
*/
|
|
void dc_i420_scale(const unsigned char *src, int sw, int sh,
|
|
unsigned char *dst, int dw, int dh);
|
|
void dc_i420_to_xrgb(const unsigned char *i420, int w, int h, uint32_t *out,
|
|
int stride_px);
|
|
|
|
/*
|
|
* IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags,
|
|
* the prefix, the command, up to 15 parameters and the trailing text;
|
|
* dc_irc_message_free() releases them. LEN must not include CR/LF. A NUL
|
|
* inside LEN, a missing command, more than 15 parameters or a line over
|
|
* DC_IRC_MAX_LINE bytes is an error.
|
|
*/
|
|
#define DC_IRC_MAX_LINE (1024 * 1024)
|
|
|
|
typedef struct {
|
|
char *tags; /* without '@', or NULL */
|
|
char *prefix; /* without ':', or NULL */
|
|
char *command; /* never NULL after a successful parse */
|
|
char **params;
|
|
size_t nparams;
|
|
char *trailing; /* without ':', or NULL */
|
|
} DcIrcMessage;
|
|
|
|
int dc_irc_parse(const char *line, size_t len, DcIrcMessage *msg, char *err,
|
|
size_t errlen);
|
|
void dc_irc_message_free(DcIrcMessage *msg);
|
|
|
|
/*
|
|
* Split TEXT at MAX_BYTES without cutting a UTF-8 sequence. *OFFSET starts
|
|
* at 0 and is advanced past each chunk; OUT receives the chunk (NUL
|
|
* terminated, at most OUTLEN-1 bytes). Spaces and newlines at the break are
|
|
* consumed, so joining the chunks reproduces the words. Returns the chunk
|
|
* length, or 0 when TEXT is exhausted.
|
|
*/
|
|
size_t dc_irc_split_next(const char *text, size_t *offset, size_t max_bytes,
|
|
char *out, size_t outlen);
|
|
|
|
/*
|
|
* Text helpers for the UI and the adapters. All return malloc'd strings
|
|
* (free with free(3)) or NULL. dc_text_clean() removes C0 controls except
|
|
* newline/tab and normalises CR/CRLF; dc_text_irc_strip() removes the mIRC
|
|
* formatting codes but keeps UTF-8; dc_text_to_display() converts UTF-8 to
|
|
* CHARSET with iconv, falls back to ASCII//TRANSLIT and then to '?'.
|
|
*/
|
|
int dc_text_valid_utf8(const char *s);
|
|
char *dc_text_clean(const char *s);
|
|
char *dc_text_irc_strip(const char *s);
|
|
char *dc_text_to_display(const char *s, const char *charset);
|
|
|
|
/*
|
|
* Adapter processes. dc_adapter_start() forks/execs PATH with "--stdio",
|
|
* waits for its "hello" (ipc 1), sends "configure" and "connect", and then
|
|
* routes every JSON object from the adapter to FN. The event is owned by
|
|
* the adapter and valid only during the callback.
|
|
*
|
|
* If the child dies without saying "bye", the manager restarts it after
|
|
* 2, 4, 8 ... 60 seconds. dc_adapter_stop() sends "disconnect" and starts
|
|
* the 2 s shutdown; dc_adapter_free() may be called after the state is
|
|
* STOPPED (it also kills a still-running child). Settings and secrets are
|
|
* deep-copied; the caller keeps its objects.
|
|
*/
|
|
typedef struct DcAdapter DcAdapter;
|
|
|
|
typedef enum {
|
|
DC_ADAPTER_STARTING,
|
|
DC_ADAPTER_CONFIGURED,
|
|
DC_ADAPTER_ONLINE,
|
|
DC_ADAPTER_BACKOFF,
|
|
DC_ADAPTER_STOPPED
|
|
} DcAdapterState;
|
|
|
|
typedef void (*DcAdapterEventFn)(DcAdapter *adapter, const DcJson *event,
|
|
void *arg);
|
|
|
|
DcAdapter *dc_adapter_start(DcLoop *loop, const char *path,
|
|
const char *account, const DcJson *settings, const DcJson *secrets,
|
|
DcAdapterEventFn fn, void *arg);
|
|
void dc_adapter_stop(DcAdapter *adapter);
|
|
void dc_adapter_free(DcAdapter *adapter);
|
|
|
|
DcAdapterState dc_adapter_state(const DcAdapter *adapter);
|
|
int dc_adapter_restarts(const DcAdapter *adapter);
|
|
|
|
int dc_adapter_send_json(DcAdapter *adapter, const DcJson *json);
|
|
int dc_adapter_send_text(DcAdapter *adapter, const char *conv,
|
|
const char *text, const char *client_id);
|
|
/* XEP-0085 / IRC typing: tell CONV that the person is typing or stopped */
|
|
int dc_adapter_send_typing(DcAdapter *adapter, const char *conv, int on);
|
|
int dc_adapter_join(DcAdapter *adapter, const char *conv);
|
|
int dc_adapter_answer(DcAdapter *adapter, int request_id, const char *value);
|
|
|
|
/*
|
|
* One dtchat per user: DIR/run/dtchat.lock is flock()ed and
|
|
* DIR/run/dtchat.sock is the control socket. The first process is the
|
|
* primary and gets OPEN_CB for every "open" request; a second process
|
|
* gets an instance with dc_instance_is_primary()==0, calls
|
|
* dc_instance_notify() and exits. The lock is released and the socket
|
|
* removed by dc_instance_close().
|
|
*/
|
|
typedef struct DcInstance DcInstance;
|
|
typedef void (*DcInstanceOpenFn)(const char *conv, void *arg);
|
|
|
|
DcInstance *dc_instance_acquire(DcLoop *loop, const char *dir,
|
|
DcInstanceOpenFn open_cb, void *arg);
|
|
int dc_instance_is_primary(const DcInstance *instance);
|
|
const char *dc_instance_socket(const DcInstance *instance);
|
|
void dc_instance_close(DcInstance *instance);
|
|
|
|
/* Connect to a running primary and send {"cmd":"open","conv":...}. */
|
|
int dc_instance_notify(const char *dir, const char *conv);
|
|
|
|
#endif /* _DtChat_dc_internal_h */
|