Video frames are timestamped by wall clock now, so test_video_packet can no longer make the old frame-count timestamps by sending quickly. dc_call_send_video_at() takes the timestamp as an argument (dc_call_send_video passes the elapsed time since the call began), and the test sends 0/66/132 ms after 200 ms of decoded audio: the first two are dropped, the third renders with offset -68 ms and two drops. Standalone and in-tree (43 ok/0 bad) green.
705 lines
17 KiB
C
705 lines
17 KiB
C
/*
|
|
* media_call.c -- dtchat's audio call: UDP, jitter buffer, push-to-talk
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* A call is one UDP socket, the two direction keys of T6.3 and an Opus
|
|
* encoder/decoder. The caller pumps it: dc_call_drain() reads and authenticates
|
|
* every packet that arrived, dc_call_audio_out() takes one frame from the
|
|
* jitter buffer (a lost one becomes concealment), dc_call_audio_in() encodes
|
|
* and sends one 20 ms frame while the person holds the key. The jitter
|
|
* buffer starts after DEPTH frames (default 3 = 60 ms), keeps an order for
|
|
* reordered packets and forgets anything that is already late. Push-to-talk
|
|
* is a state of the call that the peer is told about (DC_MEDIA_PTT); the
|
|
* caller stops sending audio when it is off.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <arpa/inet.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#define JITTER_SLOTS 32
|
|
|
|
typedef struct {
|
|
uint32_t seq;
|
|
uint32_t ts;
|
|
unsigned char data[DC_MEDIA_MAX_PAYLOAD];
|
|
size_t len;
|
|
int used;
|
|
} JitterSlot;
|
|
|
|
struct DcJitter {
|
|
size_t depth;
|
|
uint32_t next;
|
|
uint32_t highest;
|
|
int started;
|
|
size_t buffered;
|
|
JitterSlot slot[JITTER_SLOTS];
|
|
};
|
|
|
|
struct DcMediaCall {
|
|
int fd;
|
|
struct sockaddr_in peer;
|
|
DcOpusEnc *enc;
|
|
DcOpusDec *dec;
|
|
DcMediaSend snd;
|
|
DcMediaRecv rcv;
|
|
DcJitter *jitter;
|
|
DcVideoReasm *video;
|
|
DcVpxDec *vdec;
|
|
unsigned char *vpkt;
|
|
size_t vpkt_max;
|
|
DcSync sync;
|
|
long long video_start;
|
|
int video_dropped;
|
|
int peer_wants_keyframe;
|
|
int dbg_types;
|
|
int dbg_bad;
|
|
uint32_t frames;
|
|
int local_talking; /* we hold the key */
|
|
int peer_talking; /* the peer told us so */
|
|
long long last_in;
|
|
DcCallState state;
|
|
long long state_since;
|
|
};
|
|
|
|
DcJitter *
|
|
dc_jitter_new(size_t depth, size_t slots)
|
|
{
|
|
DcJitter *j;
|
|
|
|
if (slots == 0 || slots > JITTER_SLOTS)
|
|
slots = JITTER_SLOTS;
|
|
if (depth == 0)
|
|
depth = 1;
|
|
if (depth > slots)
|
|
depth = slots;
|
|
j = calloc(1, sizeof(*j));
|
|
if (j == NULL)
|
|
return NULL;
|
|
j->depth = depth;
|
|
return j;
|
|
}
|
|
|
|
void
|
|
dc_jitter_free(DcJitter *j)
|
|
{
|
|
free(j);
|
|
}
|
|
|
|
int
|
|
dc_jitter_put(DcJitter *j, uint32_t seq, uint32_t ts, const void *data,
|
|
size_t len)
|
|
{
|
|
JitterSlot *s;
|
|
|
|
if (j == NULL || len > DC_MEDIA_MAX_PAYLOAD || (len > 0 && data == NULL))
|
|
return -1;
|
|
if (j->started && seq < j->next)
|
|
return 0; /* too late, the frame is behind us */
|
|
s = &j->slot[seq % JITTER_SLOTS];
|
|
if (s->used && s->seq == seq)
|
|
return 0; /* a duplicate */
|
|
if (!s->used)
|
|
j->buffered++;
|
|
s->used = 1;
|
|
s->seq = seq;
|
|
s->ts = ts;
|
|
s->len = len;
|
|
if (len > 0)
|
|
memcpy(s->data, data, len);
|
|
if (seq > j->highest)
|
|
j->highest = seq;
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
dc_jitter_get(DcJitter *j, uint32_t *ts, unsigned char *out, size_t outlen,
|
|
size_t *len)
|
|
{
|
|
JitterSlot *s;
|
|
uint32_t a, b;
|
|
|
|
if (j == NULL || out == NULL || len == NULL)
|
|
return 0;
|
|
if (!j->started) {
|
|
/* until the first DEPTH frames are here, wait */
|
|
if (j->buffered < j->depth)
|
|
return 0;
|
|
j->started = 1;
|
|
j->next = j->highest + 1 - (uint32_t)j->buffered;
|
|
/* the lowest sequence present is the start, not highest-n */
|
|
for (a = 0; a < JITTER_SLOTS; a++) {
|
|
if (j->slot[a].used && j->slot[a].seq < j->next)
|
|
j->next = j->slot[a].seq;
|
|
}
|
|
}
|
|
s = &j->slot[j->next % JITTER_SLOTS];
|
|
if (s->used && s->seq == j->next) {
|
|
if (s->len > 0 && s->len <= outlen)
|
|
memcpy(out, s->data, s->len);
|
|
*len = s->len <= outlen ? s->len : 0;
|
|
if (ts != NULL)
|
|
*ts = s->ts;
|
|
s->used = 0;
|
|
j->buffered--;
|
|
j->next++;
|
|
return 1;
|
|
}
|
|
/* a gap: is a later frame already DEPTH ahead? then it is lost */
|
|
a = j->highest - j->next;
|
|
if (j->highest >= j->next && a + 1 >= j->depth) {
|
|
*len = 0; /* concealment */
|
|
if (ts != NULL)
|
|
*ts = 0;
|
|
b = j->next % JITTER_SLOTS;
|
|
if (j->slot[b].used) {
|
|
j->slot[b].used = 0;
|
|
j->buffered--;
|
|
}
|
|
j->next++;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
DcJitter *
|
|
dc_call_jitter(const DcMediaCall *call)
|
|
{
|
|
return call != NULL ? call->jitter : NULL;
|
|
}
|
|
|
|
static int
|
|
set_nonblock(int fd)
|
|
{
|
|
int fl = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (fl < 0)
|
|
return -1;
|
|
return fcntl(fd, F_SETFL, fl | O_NONBLOCK);
|
|
}
|
|
|
|
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)
|
|
{
|
|
DcMediaCall *call;
|
|
struct addrinfo hints;
|
|
struct addrinfo *res = NULL;
|
|
char service[16];
|
|
|
|
if (err != NULL && errlen > 0)
|
|
err[0] = '\0';
|
|
if (peer_host == NULL || peer_port <= 0 || send == NULL || recv == NULL) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "bad call arguments");
|
|
return NULL;
|
|
}
|
|
memset(&hints, 0, sizeof(hints));
|
|
hints.ai_family = AF_INET;
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
snprintf(service, sizeof(service), "%d", peer_port);
|
|
if (getaddrinfo(peer_host, service, &hints, &res) != 0) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "cannot resolve %s", peer_host);
|
|
return NULL;
|
|
}
|
|
call = calloc(1, sizeof(*call));
|
|
if (call == NULL) {
|
|
freeaddrinfo(res);
|
|
return NULL;
|
|
}
|
|
memcpy(&call->peer, res->ai_addr, sizeof(call->peer));
|
|
freeaddrinfo(res);
|
|
call->fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
if (call->fd >= 0) {
|
|
struct sockaddr_in self;
|
|
|
|
memset(&self, 0, sizeof(self));
|
|
self.sin_family = AF_INET;
|
|
self.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
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;
|
|
}
|
|
call->enc = dc_opus_enc_new(24000, err, errlen);
|
|
call->dec = dc_opus_dec_new(err, errlen);
|
|
call->jitter = dc_jitter_new(3, JITTER_SLOTS);
|
|
call->vpkt_max = DC_VIDEO_FRAME_MAX;
|
|
call->vpkt = malloc(call->vpkt_max);
|
|
call->snd = *send;
|
|
call->rcv = *recv;
|
|
call->state = DC_CALL_IDLE;
|
|
call->state_since = dc_loop_now_ms();
|
|
call->video_start = call->state_since;
|
|
dc_sync_init(&call->sync);
|
|
if (call->fd < 0 || call->enc == NULL || call->dec == NULL ||
|
|
call->jitter == NULL || call->vpkt == NULL) {
|
|
if (err != NULL && errlen > 0 && err[0] == '\0')
|
|
snprintf(err, errlen, "cannot set up the call");
|
|
dc_call_free(call);
|
|
return NULL;
|
|
}
|
|
if (set_nonblock(call->fd) != 0) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "cannot set the socket");
|
|
dc_call_free(call);
|
|
return NULL;
|
|
}
|
|
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)
|
|
{
|
|
return call != NULL ? call->fd : -1;
|
|
}
|
|
|
|
int
|
|
dc_call_local_port(DcMediaCall *call)
|
|
{
|
|
struct sockaddr_in addr;
|
|
socklen_t len = sizeof(addr);
|
|
|
|
if (call == NULL)
|
|
return 0;
|
|
if (getsockname(call->fd, (struct sockaddr *)&addr, &len) != 0)
|
|
return 0;
|
|
return ntohs(addr.sin_port);
|
|
}
|
|
|
|
int
|
|
dc_call_talking(const DcMediaCall *call)
|
|
{
|
|
return call != NULL ? call->local_talking : 0;
|
|
}
|
|
|
|
int
|
|
dc_call_peer_talking(const DcMediaCall *call)
|
|
{
|
|
return call != NULL ? call->peer_talking : 0;
|
|
}
|
|
|
|
int
|
|
dc_call_audio_in(DcMediaCall *call, const int16_t *pcm)
|
|
{
|
|
unsigned char payload[512];
|
|
unsigned char packet[600];
|
|
int n;
|
|
size_t len;
|
|
|
|
if (call == NULL || pcm == NULL || call->state != DC_CALL_IN_CALL)
|
|
return -1;
|
|
n = dc_opus_enc_frame(call->enc, pcm, payload, sizeof(payload));
|
|
if (n <= 0)
|
|
return -1;
|
|
len = dc_media_seal(&call->snd, DC_MEDIA_AUDIO,
|
|
call->frames * (uint32_t)DC_AUDIO_FRAME_MS, payload, (size_t)n,
|
|
packet, sizeof(packet));
|
|
if (len == 0)
|
|
return -1;
|
|
if (sendto(call->fd, packet, len, 0, (struct sockaddr *)&call->peer,
|
|
sizeof(call->peer)) != (ssize_t)len)
|
|
return -1;
|
|
call->frames++;
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
dc_call_send_ptt(DcMediaCall *call, int on)
|
|
{
|
|
unsigned char packet[64];
|
|
unsigned char payload = (unsigned char)(on ? 1 : 0);
|
|
size_t len;
|
|
|
|
if (call == NULL || call->state != DC_CALL_IN_CALL)
|
|
return -1;
|
|
call->local_talking = on ? 1 : 0;
|
|
len = dc_media_seal(&call->snd, DC_MEDIA_PTT, call->frames *
|
|
(uint32_t)DC_AUDIO_FRAME_MS, &payload, 1, packet, sizeof(packet));
|
|
if (len == 0)
|
|
return -1;
|
|
if (sendto(call->fd, packet, len, 0, (struct sockaddr *)&call->peer,
|
|
sizeof(call->peer)) != (ssize_t)len)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
/* one control packet: no payload */
|
|
static int
|
|
send_control(DcMediaCall *call, int type)
|
|
{
|
|
unsigned char packet[64];
|
|
size_t len;
|
|
|
|
len = dc_media_seal(&call->snd, type,
|
|
call->frames * (uint32_t)DC_AUDIO_FRAME_MS, NULL, 0, packet,
|
|
sizeof(packet));
|
|
if (len == 0)
|
|
return -1;
|
|
if (sendto(call->fd, packet, len, 0, (struct sockaddr *)&call->peer,
|
|
sizeof(call->peer)) != (ssize_t)len)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
set_state(DcMediaCall *call, DcCallState state)
|
|
{
|
|
call->state = state;
|
|
call->state_since = dc_loop_now_ms();
|
|
}
|
|
|
|
DcCallState
|
|
dc_call_state(const DcMediaCall *call)
|
|
{
|
|
return call != NULL ? call->state : DC_CALL_IDLE;
|
|
}
|
|
|
|
int
|
|
dc_call_ring(DcMediaCall *call)
|
|
{
|
|
if (call == NULL || call->state == DC_CALL_IN_CALL ||
|
|
call->state == DC_CALL_ENDED)
|
|
return -1;
|
|
if (send_control(call, DC_MEDIA_RING) != 0)
|
|
return -1;
|
|
set_state(call, DC_CALL_RINGING);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
dc_call_accept(DcMediaCall *call)
|
|
{
|
|
if (call == NULL ||
|
|
(call->state != DC_CALL_RINGING && call->state != DC_CALL_IDLE))
|
|
return -1;
|
|
if (send_control(call, DC_MEDIA_ACCEPT) != 0)
|
|
return -1;
|
|
set_state(call, DC_CALL_IN_CALL);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
dc_call_hangup(DcMediaCall *call)
|
|
{
|
|
if (call == NULL || call->state == DC_CALL_ENDED)
|
|
return -1;
|
|
(void)send_control(call, DC_MEDIA_HANGUP);
|
|
set_state(call, DC_CALL_ENDED);
|
|
call->local_talking = 0;
|
|
call->peer_talking = 0;
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
dc_call_tick(DcMediaCall *call)
|
|
{
|
|
long long now;
|
|
|
|
if (call == NULL)
|
|
return;
|
|
now = dc_loop_now_ms();
|
|
if (call->state == DC_CALL_RINGING &&
|
|
now - call->state_since > DC_CALL_RING_MS) {
|
|
(void)send_control(call, DC_MEDIA_HANGUP);
|
|
set_state(call, DC_CALL_ENDED);
|
|
}
|
|
}
|
|
|
|
int
|
|
dc_call_drain(DcMediaCall *call)
|
|
{
|
|
unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64];
|
|
unsigned char plain[DC_MEDIA_MAX_PAYLOAD];
|
|
int count = 0;
|
|
|
|
if (call == NULL)
|
|
return -1;
|
|
for (;;) {
|
|
struct sockaddr_in from;
|
|
socklen_t fromlen = sizeof(from);
|
|
ssize_t n = recvfrom(call->fd, packet, sizeof(packet), 0,
|
|
(struct sockaddr *)&from, &fromlen);
|
|
int type = 0;
|
|
uint32_t ts = 0;
|
|
size_t len = 0;
|
|
|
|
if (n < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
break;
|
|
return count;
|
|
}
|
|
if (dc_media_open(&call->rcv, packet, (size_t)n, &type, &ts,
|
|
plain, &len) != 1) {
|
|
if (getenv("DTCHAT_CALL_DEBUG") != NULL &&
|
|
call->dbg_bad < 10) {
|
|
fprintf(stderr, "drain: bad packet len=%zd\n", n);
|
|
call->dbg_bad++;
|
|
}
|
|
continue;
|
|
}
|
|
if (getenv("DTCHAT_CALL_DEBUG") != NULL && call->dbg_types < 12) {
|
|
fprintf(stderr, "drain: type=%d len=%zu\n", type, len);
|
|
call->dbg_types++;
|
|
}
|
|
/* an authenticated packet says where the peer is: answer an
|
|
* incoming call on the address it came from */
|
|
call->peer = from;
|
|
call->last_in = dc_loop_now_ms();
|
|
if (type == DC_MEDIA_PTT) {
|
|
if (len == 1)
|
|
call->peer_talking = plain[0] ? 1 : 0;
|
|
continue;
|
|
}
|
|
if (type == DC_MEDIA_RING) {
|
|
if (call->state == DC_CALL_IN_CALL)
|
|
(void)send_control(call, DC_MEDIA_BUSY);
|
|
else if (call->state == DC_CALL_IDLE ||
|
|
call->state == DC_CALL_ENDED)
|
|
set_state(call, DC_CALL_RINGING);
|
|
continue;
|
|
}
|
|
if (type == DC_MEDIA_ACCEPT) {
|
|
if (call->state == DC_CALL_RINGING ||
|
|
call->state == DC_CALL_IDLE)
|
|
set_state(call, DC_CALL_IN_CALL);
|
|
continue;
|
|
}
|
|
if (type == DC_MEDIA_KFREQ) {
|
|
call->peer_wants_keyframe = 1;
|
|
continue;
|
|
}
|
|
if (type == DC_MEDIA_BUSY || type == DC_MEDIA_HANGUP) {
|
|
set_state(call, DC_CALL_ENDED);
|
|
call->local_talking = 0;
|
|
call->peer_talking = 0;
|
|
continue;
|
|
}
|
|
if (type == DC_MEDIA_BYE)
|
|
continue;
|
|
if (type == DC_MEDIA_VIDEO) {
|
|
if (call->video == NULL)
|
|
call->video = dc_video_reasm_new(
|
|
DC_VIDEO_FRAME_MAX);
|
|
if (call->video != NULL)
|
|
(void)dc_video_reasm_put(call->video, ts,
|
|
plain, len);
|
|
continue;
|
|
}
|
|
if (type != DC_MEDIA_AUDIO)
|
|
continue;
|
|
if (dc_jitter_put(call->jitter, ts / DC_AUDIO_FRAME_MS, ts,
|
|
plain, len) == 1)
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
int
|
|
dc_call_audio_out(DcMediaCall *call, int16_t *pcm)
|
|
{
|
|
unsigned char payload[DC_MEDIA_MAX_PAYLOAD];
|
|
uint32_t ts = 0;
|
|
size_t len = 0;
|
|
int n;
|
|
|
|
if (call == NULL || pcm == NULL)
|
|
return 0;
|
|
if (dc_jitter_get(call->jitter, &ts, payload, sizeof(payload),
|
|
&len) != 1)
|
|
return 0;
|
|
if (len == 0) {
|
|
n = dc_opus_dec_frame(call->dec, NULL, 0, pcm);
|
|
if (n > 0)
|
|
dc_sync_audio(&call->sync, 1);
|
|
return n > 0;
|
|
}
|
|
n = dc_opus_dec_frame(call->dec, payload, len, pcm);
|
|
if (n > 0)
|
|
dc_sync_audio(&call->sync, 1);
|
|
return n > 0;
|
|
}
|
|
|
|
int
|
|
dc_call_send_video(DcMediaCall *call, uint16_t frame_id,
|
|
const unsigned char *frame, size_t len, int keyframe)
|
|
{
|
|
if (call == NULL)
|
|
return -1;
|
|
return dc_call_send_video_at(call,
|
|
(uint32_t)(dc_loop_now_ms() - call->video_start), frame_id, frame,
|
|
len, keyframe);
|
|
}
|
|
|
|
int
|
|
dc_call_send_video_at(DcMediaCall *call, uint32_t ts, uint16_t frame_id,
|
|
const unsigned char *frame, size_t len, int keyframe)
|
|
{
|
|
unsigned char fragment[DC_MEDIA_MAX_PAYLOAD];
|
|
unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64];
|
|
int count;
|
|
int i;
|
|
|
|
if (call == NULL || frame == NULL ||
|
|
call->state != DC_CALL_IN_CALL || len == 0 ||
|
|
len > DC_VIDEO_FRAME_MAX)
|
|
return -1;
|
|
count = dc_video_fragment_count(len);
|
|
for (i = 0; i < count; i++) {
|
|
size_t flen = 0;
|
|
size_t plen;
|
|
|
|
if (dc_video_fragment_make(frame_id, keyframe, i, count,
|
|
frame, len, fragment, sizeof(fragment), &flen) != 0)
|
|
return -1;
|
|
/* the receiver synchronises against its audio clock, so the
|
|
* timestamp is when the frame is sent, not a frame count: a pump
|
|
* that runs slower than the nominal frame rate must not make the
|
|
* stream drift behind the sound */
|
|
plen = dc_media_seal(&call->snd, DC_MEDIA_VIDEO, ts, fragment,
|
|
flen, packet, sizeof(packet));
|
|
if (plen == 0)
|
|
return -1;
|
|
if (sendto(call->fd, packet, plen, 0,
|
|
(struct sockaddr *)&call->peer,
|
|
sizeof(call->peer)) != (ssize_t)plen)
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
dc_call_video_packet(DcMediaCall *call, unsigned char *out, size_t outlen,
|
|
size_t *len, int *keyframe, uint16_t *frame_id)
|
|
{
|
|
uint32_t ts = 0;
|
|
int rc;
|
|
|
|
if (call == NULL || call->video == NULL)
|
|
return 0;
|
|
rc = dc_video_reasm_frame(call->video, out, outlen, len, keyframe,
|
|
frame_id, &ts);
|
|
if (rc != 1)
|
|
return rc;
|
|
if (getenv("DTCHAT_CALL_DEBUG") != NULL)
|
|
fprintf(stderr, "video reasm: len=%zu ts=%u audio=%lld\n",
|
|
*len, ts, call->sync.audio_ms);
|
|
if (!dc_sync_video(&call->sync, ts)) {
|
|
call->video_dropped++;
|
|
if (getenv("DTCHAT_CALL_DEBUG") != NULL)
|
|
fprintf(stderr, "video dropped: ts=%u audio=%lld\n",
|
|
ts, call->sync.audio_ms);
|
|
return -1; /* too late, the audio has moved on */
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
dc_call_video_out(DcMediaCall *call, unsigned char *out, size_t outlen,
|
|
size_t *len, int *keyframe, uint16_t *frame_id)
|
|
{
|
|
size_t plen = 0;
|
|
int rc;
|
|
|
|
if (call == NULL || out == NULL || len == NULL)
|
|
return 0;
|
|
if (outlen < (size_t)DC_VIDEO_I420_LEN)
|
|
return -1;
|
|
rc = dc_call_video_packet(call, call->vpkt, call->vpkt_max, &plen,
|
|
keyframe, frame_id);
|
|
if (rc != 1)
|
|
return rc;
|
|
if (call->vdec == NULL)
|
|
call->vdec = dc_vpx_dec_new(DC_VIDEO_W, DC_VIDEO_H, NULL, 0);
|
|
if (call->vdec == NULL)
|
|
return 0;
|
|
rc = dc_vpx_dec_frame(call->vdec, call->vpkt, plen, out);
|
|
if (getenv("DTCHAT_CALL_DEBUG") != NULL)
|
|
fprintf(stderr, "video decode: plen=%zu rc=%d key=%d\n", plen, rc,
|
|
*keyframe);
|
|
if (rc != 1)
|
|
return 0; /* not decodable yet (no keyframe), try the next */
|
|
*len = (size_t)DC_VIDEO_I420_LEN;
|
|
return 1;
|
|
}
|
|
|
|
long long
|
|
dc_call_sync_offset(const DcMediaCall *call)
|
|
{
|
|
return call != NULL ? dc_sync_offset(&call->sync) : 0;
|
|
}
|
|
|
|
int
|
|
dc_call_video_dropped(const DcMediaCall *call)
|
|
{
|
|
return call != NULL ? call->video_dropped : 0;
|
|
}
|
|
|
|
int
|
|
dc_call_wants_keyframe(DcMediaCall *call)
|
|
{
|
|
if (call == NULL || call->video == NULL)
|
|
return 0;
|
|
return dc_video_reasm_wants_keyframe(call->video);
|
|
}
|
|
|
|
int
|
|
dc_call_request_keyframe(DcMediaCall *call)
|
|
{
|
|
if (call == NULL || call->state != DC_CALL_IN_CALL)
|
|
return -1;
|
|
return send_control(call, DC_MEDIA_KFREQ);
|
|
}
|
|
|
|
int
|
|
dc_call_take_keyframe_request(DcMediaCall *call)
|
|
{
|
|
int want;
|
|
|
|
if (call == NULL)
|
|
return 0;
|
|
want = call->peer_wants_keyframe;
|
|
call->peer_wants_keyframe = 0;
|
|
return want;
|
|
}
|
|
|
|
void
|
|
dc_call_free(DcMediaCall *call)
|
|
{
|
|
if (call == NULL)
|
|
return;
|
|
if (call->fd >= 0)
|
|
close(call->fd);
|
|
dc_opus_enc_free(call->enc);
|
|
dc_opus_dec_free(call->dec);
|
|
dc_jitter_free(call->jitter);
|
|
dc_video_reasm_free(call->video);
|
|
dc_vpx_dec_free(call->vdec);
|
|
free(call->vpkt);
|
|
free(call);
|
|
}
|