Browse Source

Some streamer changes.

master
Jeroen Vreeken 3 years ago
parent
commit
c6e8a9e52c
4 changed files with 151 additions and 73 deletions
  1. +1
    -1
      dml_stream_client.c
  2. +17
    -11
      dml_stream_client_simple.c
  3. +1
    -0
      dml_streamer.c
  4. +132
    -61
      isom.c

+ 1
- 1
dml_stream_client.c View File

@ -79,7 +79,7 @@ int main(int argc, char **argv)
dml_str_id(req_id, req_id_str);
dss = dml_stream_client_simple_create(server, req_id, NULL, data_cb, true);
dss = dml_stream_client_simple_create(server, req_id, NULL, data_cb, false);
if (!dss) {
printf("Could not create stream\n");
return -1;


+ 17
- 11
dml_stream_client_simple.c View File

@ -133,7 +133,7 @@ static void rx_packet(struct dml_connection *dc, void *arg,
fprintf(stderr, "Parse certificate\n");
if (dml_packet_parse_certificate(data, len, cid, &cert, &size))
break;
fprintf(stderr, "verify %d\n", dss->verify);
if (!dss->verify || !dml_crypto_cert_add_verify(cert, size, cid)) {
fprintf(stderr, "Request header\n");
dml_packet_send_req_header(dc, dss->req_id);
@ -149,28 +149,34 @@ static void rx_packet(struct dml_connection *dc, void *arg,
size_t header_size;
struct dml_stream *ds;
struct dml_crypto_key *dk;
bool send_connect = false;
if (dml_packet_parse_header(data, len, hid, sig, &header, &header_size))
break;
if (!dss->verify) {
dss->data_cb(dss->arg, header, header_size);
} else if ((ds = dml_stream_by_id(hid))) {
if ((dk = dml_stream_crypto_get(ds))) {
if ((ds = dml_stream_by_id(hid))) {
if (!dss->verify) {
send_connect = true;
} else if ((dk = dml_stream_crypto_get(ds))) {
bool verified = dml_crypto_verify(header, header_size, sig, dk);
if (verified) {
dss->data_cb(dss->arg, header, header_size);
dss->header_written = true;
dml_stream_data_id_set(ds, DML_PACKET_DATA);
dml_packet_send_connect(dc, dss->req_id, DML_PACKET_DATA);
fprintf(stderr, "Send connect\n");
send_connect = true;
} else {
fprintf(stderr, "Failed to verify header signature (%zd bytes)\n", header_size);
}
}
}
if (send_connect) {
dss->data_cb(dss->arg, header, header_size);
dss->header_written = true;
dml_stream_data_id_set(ds, DML_PACKET_DATA);
dml_packet_send_connect(dc, dss->req_id, DML_PACKET_DATA);
fprintf(stderr, "Send connect\n");
}
free(header);
break;


+ 1
- 0
dml_streamer.c View File

@ -167,6 +167,7 @@ void send_data(void *data, size_t size)
timestamp = (uint64_t)ts.tv_sec << 16;
timestamp |= prev_ctr;
printf("timestamp: 0x%016llx\n", timestamp);
dml_packet_send_data(dml_con, packet_id, data, size, timestamp, dk);
}


+ 132
- 61
isom.c View File

@ -15,7 +15,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#undef TESTMAIN
#define TESTMAIN
#include <string.h>
#include <stdlib.h>
@ -27,80 +27,151 @@
#include "isom.h"
struct fileparse {
uint8_t box_header[8];
size_t box_header_pos;
uint8_t *box;
size_t cur_size;
size_t box_size;
bool header_done;
ssize_t (*data_cb)(void *data, size_t size);
int (*trigger_cb)(enum fileparse_trigger trig);
};
char *subbox[] = {
"moov",
"trak",
"mdia",
"minf",
"stbl",
"moof",
"traf",
};
char *removebox[] = {
"tfdt",
};
char *dumpbox[] = {
"mvhd",
"mfhd",
"stts",
"tfdt",
// "trun",
};
void printbox(void *box, size_t *box_size, int level)
{
uint8_t *cbox = box;
uint8_t *type = &cbox[4];
char levelstr[level*2+1];
memset(levelstr, ' ', level*2);
levelstr[level*2] = 0;
printf("%sbox: %c%c%c%c %zd\n", levelstr,
type[0], type[1], type[2], type[3], *box_size);
int i;
bool has_subbox = false;
for (i = 0; i < sizeof(subbox)/sizeof(subbox[0]); i++) {
if (!memcmp(subbox[i], type, 4)) {
has_subbox = true;
}
}
bool is_removebox = false;
for (i = 0; i < sizeof(removebox)/sizeof(removebox[0]); i++) {
if (!memcmp(removebox[i], type, 4)) {
is_removebox = true;
}
}
bool is_dumpbox = false;
for (i = 0; i < sizeof(dumpbox)/sizeof(dumpbox[0]); i++) {
if (!memcmp(dumpbox[i], type, 4)) {
is_dumpbox = true;
}
}
if (is_dumpbox) {
printf("%s ", levelstr);
for (i = 0; i < *box_size; i++) {
if (i == 8)
printf(" ");
printf("%02x", cbox[i]);
}
printf("\n");
}
if (is_removebox) {
*box_size = 0;
return;
}
size_t pos;
if (has_subbox) for (pos = 8; pos < *box_size; ) {
size_t subbox_size =
(cbox[pos + 0] << 24) |
(cbox[pos + 1] << 16) |
(cbox[pos + 2] << 8) |
(cbox[pos + 3] << 0);
size_t org_size = subbox_size;
printbox(&cbox[pos], &subbox_size, level+1);
if (subbox_size < org_size) {
size_t removed = org_size - subbox_size;
memmove(&cbox[pos+subbox_size], &cbox[pos+removed], *box_size - (pos + removed));
printf("%sremoved %zd, %zd <- %zd (%zd) %zd\n", levelstr, removed, pos, pos+removed, *box_size - (pos+removed), subbox_size);
*box_size -= removed;
}
pos += subbox_size;
}
printf("%s%zd\n", levelstr, *box_size);
cbox[0] = (*box_size >> 24) & 0xff;
cbox[1] = (*box_size >> 16) & 0xff;
cbox[2] = (*box_size >> 8) & 0xff;
cbox[3] = (*box_size >> 0) & 0xff;
}
int isom_parse(struct fileparse *isom, void *buffer, size_t size)
{
uint8_t *cbuffer = buffer;
char *cbuffer = buffer;
while (size) {
if (isom->box_header_pos < 8) {
isom->box_header[isom->box_header_pos] = cbuffer[0];
isom->box_header_pos++;
if (isom->box_header_pos == 8) {
if (isom->cur_size < 4) {
size_t copy = 4 - isom->cur_size;
if (size < copy) {
copy = size;
}
memcpy(&isom->box[isom->cur_size], cbuffer, copy);
cbuffer += copy;
size -= copy;
isom->cur_size += copy;
if (isom->cur_size == 4) {
isom->box_size =
(isom->box_header[0] << 24) |
(isom->box_header[1] << 16) |
(isom->box_header[2] << 8) |
(isom->box_header[3] << 0);
if (0) printf("box: %zd: %02x%02x%02x%02x %c%c%c%c\n", isom->box_size,
isom->box_header[4],
isom->box_header[5],
isom->box_header[6],
isom->box_header[7],
isom->box_header[4],
isom->box_header[5],
isom->box_header[6],
isom->box_header[7]);
if (isom->box_size >= 8) {
isom->box_size -= 8;
}
if (isom->box_size == 0) {
isom->box_header_pos = 0;
}
if (isom->box_header[4] == 'm' &&
isom->box_header[5] == 'o' &&
isom->box_header[6] == 'o' &&
isom->box_header[7] == 'f') {
if (isom->header_done) {
isom->trigger_cb(FILEPARSE_TRIGGER_PACKET_COMPLETE);
} else {
isom->header_done = true;
isom->trigger_cb(FILEPARSE_TRIGGER_HEADER_COMPLETE);
}
}
isom->data_cb(isom->box_header, 8);
(isom->box[0] << 24) |
(isom->box[1] << 16) |
(isom->box[2] << 8) |
(isom->box[3] << 0);
isom->box = realloc(isom->box, isom->box_size);
}
cbuffer++;
size--;
} else {
size_t data_size = size;
if (data_size > isom->box_size) {
data_size = isom->box_size;
}
isom->box_size -= data_size;
size -= data_size;
isom->data_cb(cbuffer, data_size);
cbuffer += data_size;
if (isom->box_size == 0) {
isom->box_header_pos = 0;
size_t copy = isom->box_size - isom->cur_size;
if (size < copy) {
copy = size;
}
memcpy(&isom->box[isom->cur_size], cbuffer, copy);
cbuffer += copy;
size -= copy;
isom->cur_size += copy;
}
if (isom->cur_size == isom->box_size) {
printbox(isom->box, &isom->box_size, 0);
write(2, isom->box, isom->box_size);
isom->cur_size = 0;
}
}
@ -117,6 +188,7 @@ struct fileparse *isom_create(
isom = calloc(sizeof(struct fileparse), 1);
if (!isom)
goto err;
isom->box = malloc(4);
*parse = isom_parse;
isom->data_cb = data_cb;
@ -151,7 +223,6 @@ int main(int argc, char **argv)
do {
r = read(0, buffer, 1000);
if (r > 0) {
printf("read: %zd\n", r);
isom_parse(isom, buffer, r);
}
} while (r > 0);


Loading…
Cancel
Save