Skip to content

Commit 8507fea

Browse files
committed
Record a packet with its duration
Record a packet only once the following has been received, so that we can set its duration before muxing it. Fixes <#702>
1 parent 8a08ca0 commit 8507fea

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

app/src/recorder.c

+28-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ recorder_init(struct recorder *recorder,
8787
recorder->format = format;
8888
recorder->declared_frame_size = declared_frame_size;
8989
recorder->header_written = false;
90+
recorder->previous = NULL;
9091

9192
return true;
9293
}
@@ -257,6 +258,19 @@ run_recorder(void *data) {
257258

258259
if (recorder->stopped && queue_is_empty(&recorder->queue)) {
259260
mutex_unlock(recorder->mutex);
261+
struct record_packet *last = recorder->previous;
262+
if (last) {
263+
// assign an arbitrary duration to the last packet
264+
last->packet.duration = 100000;
265+
bool ok = recorder_write(recorder, &last->packet);
266+
if (!ok) {
267+
// failing to write the last frame is not very serious, no
268+
// future frame may depend on it, so the resulting file
269+
// will still be valid
270+
LOGW("Could not record last packet");
271+
}
272+
record_packet_delete(last);
273+
}
260274
break;
261275
}
262276

@@ -265,8 +279,20 @@ run_recorder(void *data) {
265279

266280
mutex_unlock(recorder->mutex);
267281

268-
bool ok = recorder_write(recorder, &rec->packet);
269-
record_packet_delete(rec);
282+
// recorder->previous is only written from this thread, no need to lock
283+
struct record_packet *previous = recorder->previous;
284+
recorder->previous = rec;
285+
286+
if (!previous) {
287+
// we just received the first packet
288+
continue;
289+
}
290+
291+
// we now know the duration of the previous packet
292+
previous->packet.duration = rec->packet.pts - previous->packet.pts;
293+
294+
bool ok = recorder_write(recorder, &previous->packet);
295+
record_packet_delete(previous);
270296
if (!ok) {
271297
LOGE("Could not record packet");
272298

app/src/recorder.h

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ struct recorder {
3434
bool stopped; // set on recorder_stop() by the stream reader
3535
bool failed; // set on packet write failure
3636
struct recorder_queue queue;
37+
38+
// we can write a packet only once we received the next one so that we can
39+
// set its duration (next_pts - current_pts)
40+
// "previous" is only accessed from the recorder thread, so it does not
41+
// need to be protected by the mutex
42+
struct record_packet *previous;
3743
};
3844

3945
bool

0 commit comments

Comments
 (0)