Skip to content

Commit 1fd0f92

Browse files
committed
Merge branch 'notsecure-split-video'
2 parents 139cfa7 + 6441351 commit 1fd0f92

File tree

3 files changed

+97
-20
lines changed

3 files changed

+97
-20
lines changed

toxav/msi.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ typedef struct _MSIMessage {
116116
static struct _Callbacks {
117117
MSICallback function;
118118
void *data;
119-
} callbacks[11] = {0};
119+
} callbacks[11] = {{0}};
120120

121121
inline__ void invoke_callback(int32_t call_index, MSICallbackID id)
122122
{
@@ -736,13 +736,7 @@ int send_message ( MSISession *session, MSICall *call, MSIMessage *msg, uint32_t
736736
*/
737737
int call_id_bigger( const uint8_t *first, const uint8_t *second)
738738
{
739-
int i = 0;
740-
741-
for (; i < CALL_ID_LEN; i ++) {
742-
743-
if ( first[i] != second[i] )
744-
return first[i] > second [i] ? 0 : 1;
745-
}
739+
return (memcmp(first, second, CALL_ID_LEN) < 0);
746740
}
747741

748742

toxav/toxav.c

Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
/* Assume 60 fps*/
4141
#define MAX_ENCODE_TIME_US ((1000 / 60) * 1000)
4242

43+
#define MAX_VIDEOFRAME_SIZE 0x40000 /* 256KiB */
44+
#define VIDEOFRAME_PIECE_SIZE 0x500 /* 1.25 KiB*/
45+
#define VIDEOFRAME_HEADER_SIZE 0x2
46+
4347

4448
#define inline__ inline __attribute__((always_inline))
4549

@@ -58,6 +62,10 @@ typedef struct _CallSpecific {
5862
* reuse them really.
5963
*/
6064
JitterBuffer *j_buf; /** Jitter buffer for audio */
65+
66+
uint32_t frame_limit; /* largest address written to in frame_buf for current input frame*/
67+
uint8_t frame_id, frame_outid; /* id of input and output video frame */
68+
void *frame_buf; /* buffer for split video payloads */
6169
} CallSpecific;
6270

6371

@@ -324,6 +332,17 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
324332
LOGGER_ERROR("Error while starting video RTP session!\n");
325333
return ErrorStartingVideoRtp;
326334
}
335+
336+
call->frame_limit = 0;
337+
call->frame_id = 0;
338+
call->frame_outid = 0;
339+
340+
call->frame_buf = calloc(MAX_VIDEOFRAME_SIZE, 1);
341+
342+
if (!call->frame_buf) {
343+
return ErrorInternal;
344+
}
345+
327346
}
328347

329348
if ( !(call->j_buf = create_queue(codec_settings->jbuf_capacity)) ) return ErrorInternal;
@@ -395,14 +414,51 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
395414
* @retval -1 Failure.
396415
*/
397416
inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload,
398-
uint16_t length )
417+
unsigned int length )
399418
{
400419
if (cii(call_index, av->msi_session)) return ErrorNoCall;
401420

402-
if ( av->calls[call_index].crtps[type - TypeAudio] )
403-
return rtp_send_msg ( av->calls[call_index].crtps[type - TypeAudio], av->msi_session->messenger_handle, payload,
404-
length );
405-
else return -1;
421+
#define send(data, len) rtp_send_msg(av->calls[call_index].crtps[type - TypeAudio], av->msi_session->messenger_handle, data, len)
422+
423+
if (av->calls[call_index].crtps[type - TypeAudio]) {
424+
if (type == TypeAudio) {
425+
return send(payload, length);
426+
} else {
427+
if (length == 0 || length > MAX_VIDEOFRAME_SIZE) {
428+
LOGGER_ERROR("Invalid video frame size: %u\n", length);
429+
return -1;
430+
}
431+
432+
/* number of pieces - 1*/
433+
uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE;
434+
435+
uint8_t load[3 + VIDEOFRAME_PIECE_SIZE];
436+
load[0] = av->calls[call_index].frame_outid++;
437+
load[1] = 0;
438+
439+
int i;
440+
441+
for (i = 0; i < numparts; i++) {
442+
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, VIDEOFRAME_PIECE_SIZE);
443+
payload += VIDEOFRAME_PIECE_SIZE;
444+
445+
if (send(load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) {
446+
return -1;
447+
}
448+
449+
load[1]++;
450+
}
451+
452+
/* remainder = length % VIDEOFRAME_PIECE_SIZE, VIDEOFRAME_PIECE_SIZE if = 0 */
453+
length = ((length - 1) % VIDEOFRAME_PIECE_SIZE) + 1;
454+
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, length);
455+
return send(load, VIDEOFRAME_HEADER_SIZE + length);
456+
}
457+
} else {
458+
return -1;
459+
}
460+
461+
#undef send
406462
}
407463

408464
/**
@@ -477,21 +533,47 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
477533
uint8_t packet [RTP_PAYLOAD_SIZE];
478534
CallSpecific *call = &av->calls[call_index];
479535

480-
int recved_size = 0;
536+
int recved_size;
481537

482-
do {
483-
recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet);
538+
while ((recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet)) > 0) {
539+
if (recved_size < VIDEOFRAME_HEADER_SIZE) {
540+
continue;
541+
}
542+
543+
uint8_t i = packet[0] - call->frame_id;
484544

485-
if (recved_size > 0) {
486-
int rc = vpx_codec_decode(&call->cs->v_decoder, packet, recved_size, NULL, 0);
545+
if (i == 0) {
546+
/* piece of current frame */
547+
} else if (i > 0 && i < 128) {
548+
/* recieved a piece of a frame ahead, flush current frame and start reading this new frame */
549+
int rc = vpx_codec_decode(&call->cs->v_decoder, call->frame_buf, call->frame_limit, NULL, 0);
550+
call->frame_id = packet[0];
551+
memset(call->frame_buf, 0, call->frame_limit);
552+
call->frame_limit = 0;
487553

488554
if (rc != VPX_CODEC_OK) {
489555
LOGGER_ERROR("Error decoding video: %s\n", vpx_codec_err_to_string(rc));
490556
return ErrorInternal;
491557
}
558+
} else {
559+
/* old packet, dont read */
560+
continue;
561+
}
562+
563+
if (packet[1] > (MAX_VIDEOFRAME_SIZE - VIDEOFRAME_PIECE_SIZE + 1) /
564+
VIDEOFRAME_PIECE_SIZE) { //TODO, fix this check? not sure
565+
/* packet out of buffer range */
566+
continue;
492567
}
493568

494-
} while (recved_size > 0);
569+
memcpy(call->frame_buf + packet[1] * VIDEOFRAME_PIECE_SIZE, packet + VIDEOFRAME_HEADER_SIZE,
570+
recved_size - VIDEOFRAME_HEADER_SIZE);
571+
uint32_t limit = packet[1] * VIDEOFRAME_PIECE_SIZE + recved_size - VIDEOFRAME_HEADER_SIZE;
572+
573+
if (limit > call->frame_limit) {
574+
call->frame_limit = limit;
575+
}
576+
}
495577

496578
vpx_codec_iter_t iter = NULL;
497579
vpx_image_t *img;

toxdns/toxdns.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727

2828
#include "../toxcore/Messenger.h"
29+
#include "../toxcore/logger.h"
2930
#include "toxdns.h"
3031

3132
static const char base32[32] = {"abcdefghijklmnopqrstuvwxyz012345"};
@@ -143,7 +144,7 @@ int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string
143144
}
144145

145146
if (end_len != string - old_str) {
146-
printf("tox_generate_dns3_string Fail, %u != %u\n", end_len, string - old_str);
147+
LOGGER_ERROR("tox_generate_dns3_string Fail, %u != %lu\n", end_len, string - old_str);
147148
return -1;
148149
}
149150

0 commit comments

Comments
 (0)