Skip to content

Commit

Permalink
Record a packet with its duration
Browse files Browse the repository at this point in the history
Record a packet only once the following has been received, so that we
can set its duration before muxing it.

Fixes <#702>
  • Loading branch information
rom1v committed Aug 8, 2019
1 parent 8a08ca0 commit 8507fea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 28 additions & 2 deletions app/src/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ recorder_init(struct recorder *recorder,
recorder->format = format;
recorder->declared_frame_size = declared_frame_size;
recorder->header_written = false;
recorder->previous = NULL;

return true;
}
Expand Down Expand Up @@ -257,6 +258,19 @@ run_recorder(void *data) {

if (recorder->stopped && queue_is_empty(&recorder->queue)) {
mutex_unlock(recorder->mutex);
struct record_packet *last = recorder->previous;
if (last) {
// assign an arbitrary duration to the last packet
last->packet.duration = 100000;
bool ok = recorder_write(recorder, &last->packet);
if (!ok) {
// failing to write the last frame is not very serious, no
// future frame may depend on it, so the resulting file
// will still be valid
LOGW("Could not record last packet");
}
record_packet_delete(last);
}
break;
}

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

mutex_unlock(recorder->mutex);

bool ok = recorder_write(recorder, &rec->packet);
record_packet_delete(rec);
// recorder->previous is only written from this thread, no need to lock
struct record_packet *previous = recorder->previous;
recorder->previous = rec;

if (!previous) {
// we just received the first packet
continue;
}

// we now know the duration of the previous packet
previous->packet.duration = rec->packet.pts - previous->packet.pts;

bool ok = recorder_write(recorder, &previous->packet);
record_packet_delete(previous);
if (!ok) {
LOGE("Could not record packet");

Expand Down
6 changes: 6 additions & 0 deletions app/src/recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ struct recorder {
bool stopped; // set on recorder_stop() by the stream reader
bool failed; // set on packet write failure
struct recorder_queue queue;

// we can write a packet only once we received the next one so that we can
// set its duration (next_pts - current_pts)
// "previous" is only accessed from the recorder thread, so it does not
// need to be protected by the mutex
struct record_packet *previous;
};

bool
Expand Down

0 comments on commit 8507fea

Please sign in to comment.