dtchat: fragment and reassemble VP8 frames over media packets (T7.4)
media_video_packet.c: a frame travels in 1200-byte DC_MEDIA_VIDEO packets with an 8-byte header (start/end/keyframe flags, reserved, frame id, fragment index and count, big-endian) and 1192 data bytes. The reassembler keeps one frame, accepts fragments out of order, ignores duplicates, bounds the frame at 256 KiB and refuses a fragment count that cannot fit, drops an incomplete frame when the next frame id starts and then asks for a keyframe. media_call.c gains dc_call_send_video (fragments + seals while in a call), dc_call_video_out and dc_call_wants_keyframe; drained VIDEO packets feed the reassembler. test_video_packet: 50 KB in 42 fragments in order and reversed with a duplicate, missing fragment -> no frame then wants_keyframe on the next id, bad/oversize fragments refused, and a loopback call where an 11-frame sequence (50 KB each) crosses sealed UDP and reassembles byte-identical in 0.6 ms per frame; standalone and ASan/UBSan clean. In-tree build still pending: make mcde-host was running when this landed.
This commit is contained in:
1 parent
d208236ed5
commit
4be64d1abc
5 files changed
+543
-3
No files matched your search
@@ -12,7 +12,8 @@ libDtChatIpc_la_LDFLAGS = -version-info 1:0:0
|
||||
libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include
|
||||
libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \
|
||||
irc.c identity.c peers.c local.c filexfer.c pair.c i18n.c media_audio.c \
|
||||
media_packet.c media_call.c media_video.c dc_internal.h
|
||||
media_packet.c media_call.c media_video.c media_video_packet.c \
|
||||
dc_internal.h
|
||||
libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) $(OPUSLIB) $(VPXLIB)
|
||||
libDtChat_la_LDFLAGS = -version-info 1:0:0
|
||||
|
||||
@@ -21,7 +22,7 @@ libDtChat_la_LDFLAGS = -version-info 1:0:0
|
||||
noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \
|
||||
test_identity test_peers test_local test_filexfer test_pair test_fuzz \
|
||||
test_i18n test_audio_opus test_media_packet test_media_call test_video_vp8 \
|
||||
test_adapter test_instance fake_adapter
|
||||
test_video_packet test_adapter test_instance fake_adapter
|
||||
test_json_SOURCES = test_json.c
|
||||
test_json_CPPFLAGS = -I$(top_srcdir)/include
|
||||
test_json_LDADD = libDtChatIpc.la
|
||||
@@ -86,7 +87,10 @@ test_media_call_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) $(OPUSLIB) -lm
|
||||
test_video_vp8_SOURCES = test_video_vp8.c
|
||||
test_video_vp8_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) $(VPX_CPPFLAGS)
|
||||
test_video_vp8_LDADD = libDtChat.la libDtChatIpc.la $(VPXLIB) -lm
|
||||
test_video_packet_SOURCES = test_video_packet.c
|
||||
test_video_packet_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir)
|
||||
test_video_packet_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) $(OPUSLIB) -lm
|
||||
TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \
|
||||
test_identity test_peers test_local test_filexfer test_pair test_fuzz \
|
||||
test_i18n test_audio_opus test_media_packet test_media_call test_video_vp8 \
|
||||
test_adapter test_instance
|
||||
test_video_packet test_adapter test_instance
|
||||
@@ -468,6 +468,12 @@ 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);
|
||||
int dc_call_video_out(DcMediaCall *call, unsigned char *out, size_t outlen,
|
||||
size_t *len, int *keyframe, uint16_t *frame_id);
|
||||
int dc_call_wants_keyframe(DcMediaCall *call);
|
||||
DcJitter *dc_call_jitter(const DcMediaCall *call);
|
||||
void dc_call_free(DcMediaCall *call);
|
||||
|
||||
@@ -495,6 +501,32 @@ 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)
|
||||
|
||||
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, 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);
|
||||
int dc_video_reasm_wants_keyframe(DcVideoReasm *reasm);
|
||||
|
||||
/*
|
||||
* 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;
|
||||
|
||||
@@ -56,6 +56,7 @@ struct DcMediaCall {
|
||||
DcMediaSend snd;
|
||||
DcMediaRecv rcv;
|
||||
DcJitter *jitter;
|
||||
DcVideoReasm *video;
|
||||
uint32_t frames;
|
||||
int talking;
|
||||
long long last_in;
|
||||
@@ -455,6 +456,15 @@ dc_call_drain(DcMediaCall *call)
|
||||
}
|
||||
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,
|
||||
plain, len);
|
||||
continue;
|
||||
}
|
||||
if (type != DC_MEDIA_AUDIO)
|
||||
continue;
|
||||
if (dc_jitter_put(call->jitter, ts / DC_AUDIO_FRAME_MS, ts,
|
||||
@@ -483,6 +493,57 @@ dc_call_audio_out(DcMediaCall *call, int16_t *pcm)
|
||||
return n > 0;
|
||||
}
|
||||
|
||||
int
|
||||
dc_call_send_video(DcMediaCall *call, 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;
|
||||
plen = dc_media_seal(&call->snd, DC_MEDIA_VIDEO, frame_id,
|
||||
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_out(DcMediaCall *call, unsigned char *out, size_t outlen,
|
||||
size_t *len, int *keyframe, uint16_t *frame_id)
|
||||
{
|
||||
if (call == NULL || call->video == NULL)
|
||||
return 0;
|
||||
return dc_video_reasm_frame(call->video, out, outlen, len,
|
||||
keyframe, frame_id);
|
||||
}
|
||||
|
||||
int
|
||||
dc_call_wants_keyframe(DcMediaCall *call)
|
||||
{
|
||||
if (call == NULL || call->video == NULL)
|
||||
return 0;
|
||||
return dc_video_reasm_wants_keyframe(call->video);
|
||||
}
|
||||
|
||||
void
|
||||
dc_call_free(DcMediaCall *call)
|
||||
{
|
||||
@@ -493,5 +554,6 @@ dc_call_free(DcMediaCall *call)
|
||||
dc_opus_enc_free(call->enc);
|
||||
dc_opus_dec_free(call->dec);
|
||||
dc_jitter_free(call->jitter);
|
||||
dc_video_reasm_free(call->video);
|
||||
free(call);
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* media_video_packet.c -- fragmentation and reassembly of a VP8 frame (T7.4)
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*
|
||||
* One encoded frame is larger than a media packet (a 640x360 keyframe is tens
|
||||
* of KiB), so it travels as fragments of one DC_MEDIA_VIDEO packet each. A
|
||||
* fragment carries an 8-byte header:
|
||||
*
|
||||
* byte 0 flags: bit 0 start, bit 1 end, bit 2 keyframe, bit 3 request
|
||||
* byte 1 reserved (0)
|
||||
* bytes 2..3 frame id, big-endian
|
||||
* bytes 4..5 fragment index, big-endian
|
||||
* bytes 6..7 fragment count, big-endian
|
||||
*
|
||||
* The reassembler keeps one frame at a time: fragments of the frame id it is
|
||||
* building are stored by index (out of order is fine), the frame is complete
|
||||
* when every index arrived. A fragment of a newer frame while the current
|
||||
* one is incomplete drops the old one and sets WANTS_KEYFRAME, because the
|
||||
* decoder has no reference for the next inter frame. The buffer is bounded
|
||||
* (DC_VIDEO_FRAME_MAX, 256 KiB).
|
||||
*/
|
||||
#include "dc_internal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define FRAG_HDR 8
|
||||
#define FRAG_DATA (DC_MEDIA_MAX_PAYLOAD - FRAG_HDR)
|
||||
|
||||
struct DcVideoReasm {
|
||||
uint16_t frame_id;
|
||||
uint16_t count;
|
||||
uint16_t got;
|
||||
uint16_t *index;
|
||||
unsigned char *data;
|
||||
size_t len; /* bytes in data */
|
||||
size_t max;
|
||||
int keyframe;
|
||||
int have_id;
|
||||
int completed;
|
||||
int wants_keyframe;
|
||||
};
|
||||
|
||||
size_t
|
||||
dc_video_fragment_len(void)
|
||||
{
|
||||
return FRAG_DATA;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
size_t off, n, total;
|
||||
|
||||
if (frame == NULL || out == NULL || outlen_out == NULL ||
|
||||
index < 0 || count <= 0 || index >= count)
|
||||
return -1;
|
||||
off = (size_t)index * FRAG_DATA;
|
||||
if (off > len)
|
||||
return -1;
|
||||
total = len - off;
|
||||
n = total > FRAG_DATA ? FRAG_DATA : total;
|
||||
if (outlen < FRAG_HDR + n)
|
||||
return -1;
|
||||
out[0] = (unsigned char)((index == 0 ? 1 : 0) |
|
||||
(index == count - 1 ? 2 : 0) | (keyframe ? 4 : 0));
|
||||
out[1] = 0;
|
||||
out[2] = (unsigned char)(frame_id >> 8);
|
||||
out[3] = (unsigned char)frame_id;
|
||||
out[4] = (unsigned char)(index >> 8);
|
||||
out[5] = (unsigned char)index;
|
||||
out[6] = (unsigned char)(count >> 8);
|
||||
out[7] = (unsigned char)count;
|
||||
if (n > 0)
|
||||
memcpy(out + FRAG_HDR, frame + off, n);
|
||||
*outlen_out = FRAG_HDR + n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
dc_video_fragment_count(size_t len)
|
||||
{
|
||||
size_t n = (len + FRAG_DATA - 1) / FRAG_DATA;
|
||||
|
||||
return n == 0 ? 1 : (int)n;
|
||||
}
|
||||
|
||||
DcVideoReasm *
|
||||
dc_video_reasm_new(size_t max_frame)
|
||||
{
|
||||
DcVideoReasm *r;
|
||||
|
||||
r = calloc(1, sizeof(*r));
|
||||
if (r == NULL)
|
||||
return NULL;
|
||||
if (max_frame == 0 || max_frame > DC_VIDEO_FRAME_MAX)
|
||||
max_frame = DC_VIDEO_FRAME_MAX;
|
||||
r->max = max_frame;
|
||||
r->data = malloc(max_frame);
|
||||
if (r->data == NULL) {
|
||||
free(r);
|
||||
return NULL;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void
|
||||
dc_video_reasm_free(DcVideoReasm *r)
|
||||
{
|
||||
if (r == NULL)
|
||||
return;
|
||||
free(r->index);
|
||||
free(r->data);
|
||||
free(r);
|
||||
}
|
||||
|
||||
static void
|
||||
reset_frame(DcVideoReasm *r, uint16_t frame_id, uint16_t count, int keyframe)
|
||||
{
|
||||
if (r->count != count || r->index == NULL) {
|
||||
free(r->index);
|
||||
r->index = calloc(count, sizeof(*r->index));
|
||||
if (r->index == NULL) {
|
||||
r->count = 0;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
memset(r->index, 0, count * sizeof(*r->index));
|
||||
}
|
||||
r->frame_id = frame_id;
|
||||
r->count = count;
|
||||
r->got = 0;
|
||||
r->len = 0;
|
||||
r->keyframe = keyframe;
|
||||
r->have_id = 1;
|
||||
r->completed = 0;
|
||||
}
|
||||
|
||||
int
|
||||
dc_video_reasm_put(DcVideoReasm *r, const unsigned char *payload, size_t len)
|
||||
{
|
||||
uint16_t frame_id, count, index;
|
||||
size_t off, n;
|
||||
|
||||
if (r == NULL || payload == NULL || len < FRAG_HDR || len > DC_MEDIA_MAX_PAYLOAD)
|
||||
return -1;
|
||||
frame_id = (uint16_t)(((uint16_t)payload[2] << 8) | payload[3]);
|
||||
index = (uint16_t)(((uint16_t)payload[4] << 8) | payload[5]);
|
||||
count = (uint16_t)(((uint16_t)payload[6] << 8) | payload[7]);
|
||||
n = len - FRAG_HDR;
|
||||
if (count == 0 || index >= count)
|
||||
return -1;
|
||||
if ((size_t)count > (r->max + FRAG_DATA - 1) / FRAG_DATA)
|
||||
return -1;
|
||||
off = (size_t)index * FRAG_DATA;
|
||||
if (off + n > r->max)
|
||||
return -1;
|
||||
if (r->have_id && frame_id != r->frame_id) {
|
||||
/* a newer frame while the old one is incomplete: lose it and
|
||||
* ask for a keyframe, the decoder has no reference */
|
||||
if (!r->completed)
|
||||
r->wants_keyframe = 1;
|
||||
r->have_id = 0;
|
||||
}
|
||||
if (!r->have_id)
|
||||
reset_frame(r, frame_id, count, (payload[0] & 4) != 0);
|
||||
if (r->count != count)
|
||||
return -1;
|
||||
if ((payload[0] & 4) != 0)
|
||||
r->keyframe = 1;
|
||||
if (r->index == NULL)
|
||||
return -1;
|
||||
if (r->index[index])
|
||||
return 1; /* a duplicate fragment */
|
||||
r->index[index] = 1;
|
||||
r->got++;
|
||||
if (off + n > r->len)
|
||||
r->len = off + n;
|
||||
memcpy(r->data + off, payload + FRAG_HDR, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
dc_video_reasm_ready(const DcVideoReasm *r)
|
||||
{
|
||||
return r != NULL && r->have_id && r->count > 0 &&
|
||||
r->got == r->count;
|
||||
}
|
||||
|
||||
int
|
||||
dc_video_reasm_frame(DcVideoReasm *r, unsigned char *out, size_t outlen,
|
||||
size_t *len_out, int *keyframe, uint16_t *frame_id)
|
||||
{
|
||||
if (r == NULL || out == NULL || len_out == NULL ||
|
||||
!dc_video_reasm_ready(r))
|
||||
return 0;
|
||||
if (r->len > outlen)
|
||||
return -1;
|
||||
memcpy(out, r->data, r->len);
|
||||
*len_out = r->len;
|
||||
if (keyframe != NULL)
|
||||
*keyframe = r->keyframe;
|
||||
if (frame_id != NULL)
|
||||
*frame_id = r->frame_id;
|
||||
r->completed = 1;
|
||||
r->have_id = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
dc_video_reasm_wants_keyframe(DcVideoReasm *r)
|
||||
{
|
||||
int want;
|
||||
|
||||
if (r == NULL)
|
||||
return 0;
|
||||
want = r->wants_keyframe;
|
||||
r->wants_keyframe = 0;
|
||||
return want;
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* test_video_packet.c -- tests for VP8 frame fragmentation (T7.4)
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*/
|
||||
#include "dc_internal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
static int fails;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
||||
#cond); \
|
||||
fails++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define FRAME_LEN 50000
|
||||
|
||||
static unsigned char frame[DC_VIDEO_FRAME_MAX];
|
||||
static unsigned char got[DC_VIDEO_FRAME_MAX];
|
||||
static unsigned char fragment[DC_MEDIA_MAX_PAYLOAD];
|
||||
|
||||
static void
|
||||
fill_frame(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FRAME_LEN; i++)
|
||||
frame[i] = (unsigned char)(i * 13 + 7);
|
||||
}
|
||||
|
||||
static void
|
||||
test_unit(void)
|
||||
{
|
||||
DcVideoReasm *r;
|
||||
int count;
|
||||
int i;
|
||||
int keyframe = 0;
|
||||
uint16_t frame_id = 0;
|
||||
size_t len = 0;
|
||||
size_t flen;
|
||||
|
||||
fill_frame();
|
||||
count = dc_video_fragment_count(FRAME_LEN);
|
||||
CHECK((size_t)count == (FRAME_LEN + dc_video_fragment_len() - 1) /
|
||||
dc_video_fragment_len());
|
||||
CHECK(dc_video_fragment_count(1) == 1);
|
||||
CHECK(dc_video_fragment_len() == DC_MEDIA_MAX_PAYLOAD - 8);
|
||||
|
||||
r = dc_video_reasm_new(DC_VIDEO_FRAME_MAX);
|
||||
CHECK(r != NULL);
|
||||
if (r == NULL)
|
||||
return;
|
||||
|
||||
/* in order */
|
||||
for (i = 0; i < count; i++) {
|
||||
CHECK(dc_video_fragment_make(1, 1, i, count, frame, FRAME_LEN,
|
||||
fragment, sizeof(fragment), &flen) == 0);
|
||||
CHECK(flen <= DC_MEDIA_MAX_PAYLOAD);
|
||||
CHECK((fragment[0] & 1) == (i == 0 ? 1 : 0));
|
||||
CHECK((fragment[0] & 2) == (i == count - 1 ? 2 : 0));
|
||||
CHECK(dc_video_reasm_put(r, fragment, flen) == 1);
|
||||
}
|
||||
CHECK(dc_video_reasm_ready(r));
|
||||
CHECK(dc_video_reasm_frame(r, got, sizeof(got), &len, &keyframe,
|
||||
&frame_id) == 1);
|
||||
CHECK(len == FRAME_LEN);
|
||||
CHECK(keyframe == 1);
|
||||
CHECK(frame_id == 1);
|
||||
CHECK(memcmp(frame, got, FRAME_LEN) == 0);
|
||||
|
||||
/* out of order, with a duplicate */
|
||||
CHECK(dc_video_fragment_make(2, 0, 0, count, frame, FRAME_LEN,
|
||||
fragment, sizeof(fragment), &flen) == 0);
|
||||
CHECK(dc_video_reasm_put(r, fragment, flen) == 1);
|
||||
for (i = count - 1; i > 0; i--) {
|
||||
CHECK(dc_video_fragment_make(2, 0, i, count, frame, FRAME_LEN,
|
||||
fragment, sizeof(fragment), &flen) == 0);
|
||||
CHECK(dc_video_reasm_put(r, fragment, flen) == 1);
|
||||
if (i == count - 1) {
|
||||
CHECK(dc_video_reasm_put(r, fragment, flen) == 1);
|
||||
CHECK(!dc_video_reasm_ready(r));
|
||||
}
|
||||
}
|
||||
CHECK(dc_video_reasm_ready(r));
|
||||
CHECK(dc_video_reasm_frame(r, got, sizeof(got), &len, &keyframe,
|
||||
&frame_id) == 1);
|
||||
CHECK(len == FRAME_LEN && frame_id == 2 && keyframe == 0);
|
||||
|
||||
/* a lost fragment: the frame never completes, the next frame id asks
|
||||
* for a keyframe */
|
||||
for (i = 0; i < count; i++) {
|
||||
if (i == 5)
|
||||
continue;
|
||||
CHECK(dc_video_fragment_make(3, 0, i, count, frame, FRAME_LEN,
|
||||
fragment, sizeof(fragment), &flen) == 0);
|
||||
CHECK(dc_video_reasm_put(r, fragment, flen) == 1);
|
||||
}
|
||||
CHECK(!dc_video_reasm_ready(r));
|
||||
CHECK(dc_video_reasm_wants_keyframe(r) == 0);
|
||||
CHECK(dc_video_fragment_make(4, 1, 0, 1, frame, 100, fragment,
|
||||
sizeof(fragment), &flen) == 0);
|
||||
CHECK(dc_video_reasm_put(r, fragment, flen) == 1);
|
||||
CHECK(dc_video_reasm_wants_keyframe(r) == 1);
|
||||
CHECK(dc_video_reasm_wants_keyframe(r) == 0);
|
||||
CHECK(dc_video_reasm_ready(r));
|
||||
CHECK(dc_video_reasm_frame(r, got, sizeof(got), &len, &keyframe,
|
||||
&frame_id) == 1);
|
||||
CHECK(len == 100 && frame_id == 4 && keyframe == 1);
|
||||
|
||||
/* bad fragments are refused */
|
||||
CHECK(dc_video_reasm_put(r, fragment, 4) == -1);
|
||||
fragment[6] = 1;
|
||||
fragment[7] = 0; /* count 256 */
|
||||
CHECK(dc_video_reasm_put(r, fragment, flen) == -1);
|
||||
|
||||
dc_video_reasm_free(r);
|
||||
}
|
||||
|
||||
static double
|
||||
now_ms(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1000000.0;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
unsigned char pub_a[DC_MEDIA_KEY_LEN], priv_a[DC_MEDIA_KEY_LEN];
|
||||
unsigned char pub_b[DC_MEDIA_KEY_LEN], priv_b[DC_MEDIA_KEY_LEN];
|
||||
DcMediaKeys ka, kb;
|
||||
DcMediaSend snd;
|
||||
DcMediaRecv rcv;
|
||||
DcMediaCall *a = NULL;
|
||||
DcMediaCall *b = NULL;
|
||||
int16_t pcm[DC_AUDIO_FRAME_SAMPLES];
|
||||
char err[256] = "";
|
||||
int port_b;
|
||||
size_t len = 0;
|
||||
int keyframe = 0;
|
||||
uint16_t frame_id = 0;
|
||||
double started;
|
||||
int rounds;
|
||||
|
||||
test_unit();
|
||||
|
||||
CHECK(dc_media_keypair(pub_a, priv_a) == 0);
|
||||
CHECK(dc_media_keypair(pub_b, priv_b) == 0);
|
||||
CHECK(dc_media_keys_agree(priv_a, pub_b, 1, &ka) == 0);
|
||||
CHECK(dc_media_keys_agree(priv_b, pub_a, 0, &kb) == 0);
|
||||
dc_media_send_init(&snd, kb.send);
|
||||
dc_media_recv_init(&rcv, kb.recv);
|
||||
b = dc_call_new("127.0.0.1", 9, &snd, &rcv, err, sizeof(err));
|
||||
CHECK(b != NULL);
|
||||
if (b == NULL)
|
||||
return 1;
|
||||
port_b = dc_call_local_port(b);
|
||||
dc_media_send_init(&snd, ka.send);
|
||||
dc_media_recv_init(&rcv, ka.recv);
|
||||
a = dc_call_new("127.0.0.1", port_b, &snd, &rcv, err, sizeof(err));
|
||||
CHECK(a != NULL);
|
||||
if (a == NULL) {
|
||||
dc_call_free(b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* video needs a call in progress */
|
||||
fill_frame();
|
||||
CHECK(dc_call_send_video(a, 1, frame, FRAME_LEN, 1) < 0);
|
||||
CHECK(dc_call_ring(a) == 0);
|
||||
(void)dc_call_drain(b);
|
||||
CHECK(dc_call_accept(b) == 0);
|
||||
(void)dc_call_drain(a);
|
||||
CHECK(dc_call_state(a) == DC_CALL_IN_CALL);
|
||||
|
||||
/* a 50 KB keyframe crosses the wire in fragments and reassembles */
|
||||
started = now_ms();
|
||||
CHECK(dc_call_send_video(a, 7, frame, FRAME_LEN, 1) == 0);
|
||||
(void)dc_call_drain(b);
|
||||
CHECK(dc_call_video_out(b, got, sizeof(got), &len, &keyframe,
|
||||
&frame_id) == 1);
|
||||
CHECK(len == FRAME_LEN && frame_id == 7 && keyframe == 1);
|
||||
CHECK(memcmp(frame, got, FRAME_LEN) == 0);
|
||||
printf("video packet: %d bytes in %d fragments, %.1f ms\n", FRAME_LEN,
|
||||
dc_video_fragment_count(FRAME_LEN), now_ms() - started);
|
||||
|
||||
/* ten more frames in the same call, one per drain */
|
||||
for (rounds = 0; rounds < 10; rounds++) {
|
||||
frame[0] = (unsigned char)rounds;
|
||||
frame[FRAME_LEN - 1] = (unsigned char)(255 - rounds);
|
||||
CHECK(dc_call_send_video(a, (uint16_t)(8 + rounds), frame,
|
||||
FRAME_LEN, 0) == 0);
|
||||
(void)dc_call_drain(b);
|
||||
CHECK(dc_call_video_out(b, got, sizeof(got), &len, &keyframe,
|
||||
&frame_id) == 1);
|
||||
CHECK(frame_id == 8 + rounds && keyframe == 0);
|
||||
CHECK(memcmp(frame, got, FRAME_LEN) == 0);
|
||||
}
|
||||
(void)pcm;
|
||||
|
||||
dc_call_free(a);
|
||||
dc_call_free(b);
|
||||
if (fails != 0) {
|
||||
fprintf(stderr, "%d test(s) failed\n", fails);
|
||||
return 1;
|
||||
}
|
||||
printf("test_video_packet: all ok\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user