Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ static bool record_write_locked(const void *data, size_t len) {

/* Guards against NALU offsets and lengths that would read past the pack or
* underflow "length - scLen". */
static void record_feed_nalu(unsigned char *data, unsigned int len,
static bool record_feed_nalu(unsigned char *data, unsigned int len,
const hal_vidnalu *nalu, char isH265) {
unsigned int offset = nalu->offset;
unsigned int nalu_len = nalu->length;
unsigned int scLen;
const char *payload;
unsigned int payload_len;

if (offset > len || nalu_len > len - offset) return;
if (nalu_len <= 3 || offset + 3 > len) return;
if (offset > len || nalu_len > len - offset) return false;
if (nalu_len <= 3 || offset + 3 > len) return false;

scLen = (data[offset + 2] == 1) ? 3 : 4;
if (nalu_len <= scLen) return;
if (nalu_len <= scLen) return false;

payload = (const char *)data + offset + scLen;
payload_len = nalu_len - scLen;
Expand All @@ -180,13 +180,14 @@ static void record_feed_nalu(unsigned char *data, unsigned int len,
case NalUnitType_CodedSliceIdr:
case NalUnitType_CodedSliceAux:
mp4_set_slice(payload, payload_len, 1);
break;
return true;
case NalUnitType_CodedSliceNonIdr:
mp4_set_slice(payload, payload_len, 0);
break;
return true;
default:
break;
}
return false;
}

static bool record_emit_fragment_locked(void) {
Expand Down Expand Up @@ -361,8 +362,14 @@ void record_ingest_stream(hal_vidstream *stream, char isH265) {
pack_len = pack->length - pack->offset;
pack_data = pack->data + pack->offset;

bool gotSlice = false;
for (int j = 0; j < pack->naluCnt; j++)
record_feed_nalu(pack_data, pack_len, &pack->nalu[j], isH265);
if (record_feed_nalu(pack_data, pack_len, &pack->nalu[j], isH265))
gotSlice = true;

/* A pack that carries only parameter sets makes no new fragment;
* writing then would repeat the previous frame and its audio. */
if (!gotSlice) continue;

if (!record_emit_fragment_locked()) {
HAL_DANGER("record", "Stopping recording after a write failure!\n");
Expand Down
12 changes: 9 additions & 3 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) {
if (plat == HAL_PLATFORM_AK)
mp4_set_timestamp(pack->timestamp);

/* A pack that carries only parameter sets makes no new fragment;
* sending then would repeat the previous frame and its audio. */
char gotSlice = 0;
for (char j = 0; j < pack->naluCnt; j++) {
/* nalu[].offset points at the start code, which is 3 or 4
* bytes depending on the encoder; the muxer wants the payload. */
Expand All @@ -257,12 +260,15 @@ void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) {
mp4_set_vps(pack_data + pack->nalu[j].offset + scLen, pack->nalu[j].length - scLen);
else if (!anyClient)
continue;
else if (pack->nalu[j].type == NalUnitType_CodedSliceIdr || pack->nalu[j].type == NalUnitType_CodedSliceAux)
else if (pack->nalu[j].type == NalUnitType_CodedSliceIdr || pack->nalu[j].type == NalUnitType_CodedSliceAux) {
mp4_set_slice(pack_data + pack->nalu[j].offset + scLen, pack->nalu[j].length - scLen, 1);
else if (pack->nalu[j].type == NalUnitType_CodedSliceNonIdr)
gotSlice = 1;
} else if (pack->nalu[j].type == NalUnitType_CodedSliceNonIdr) {
mp4_set_slice(pack_data + pack->nalu[j].offset + scLen, pack->nalu[j].length - scLen, 0);
gotSlice = 1;
}
}
if (!anyClient) continue;
if (!anyClient || !gotSlice) continue;

static enum BufError err;
char len_buf[50];
Expand Down