@@ -87,6 +87,7 @@ recorder_init(struct recorder *recorder,
87
87
recorder -> format = format ;
88
88
recorder -> declared_frame_size = declared_frame_size ;
89
89
recorder -> header_written = false;
90
+ recorder -> previous = NULL ;
90
91
91
92
return true;
92
93
}
@@ -257,6 +258,19 @@ run_recorder(void *data) {
257
258
258
259
if (recorder -> stopped && queue_is_empty (& recorder -> queue )) {
259
260
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
+ }
260
274
break ;
261
275
}
262
276
@@ -265,8 +279,20 @@ run_recorder(void *data) {
265
279
266
280
mutex_unlock (recorder -> mutex );
267
281
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 );
270
296
if (!ok ) {
271
297
LOGE ("Could not record packet" );
272
298
0 commit comments