Skip to content

Commit

Permalink
Allocate AVPacket for recorder
Browse files Browse the repository at this point in the history
From FFmpeg/doc/APIchanges:

    2021-03-17 - f7db77bd87 - lavc 58.133.100 - codec.h
      Deprecated av_init_packet(). Once removed, sizeof(AVPacket) will
      no longer be a part of the public ABI.

Refs #2302 <#2302>
  • Loading branch information
rom1v committed Jun 14, 2021
1 parent 318b6a5 commit 4af317d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 16 additions & 11 deletions app/src/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ record_packet_new(const AVPacket *packet) {
return NULL;
}

// av_packet_ref() does not initialize all fields in old FFmpeg versions
// See <https://github.com/Genymobile/scrcpy/issues/707>
av_init_packet(&rec->packet);
rec->packet = av_packet_alloc();
if (!rec->packet) {
free(rec);
return NULL;
}

if (av_packet_ref(&rec->packet, packet)) {
if (av_packet_ref(rec->packet, packet)) {
av_packet_free(&rec->packet);
free(rec);
return NULL;
}
Expand All @@ -48,7 +51,8 @@ record_packet_new(const AVPacket *packet) {

static void
record_packet_delete(struct record_packet *rec) {
av_packet_unref(&rec->packet);
av_packet_unref(rec->packet);
av_packet_free(&rec->packet);
free(rec);
}

Expand Down Expand Up @@ -144,8 +148,8 @@ run_recorder(void *data) {
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);
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
Expand All @@ -172,13 +176,14 @@ run_recorder(void *data) {
}

// config packets have no PTS, we must ignore them
if (rec->packet.pts != AV_NOPTS_VALUE
&& previous->packet.pts != AV_NOPTS_VALUE) {
if (rec->packet->pts != AV_NOPTS_VALUE
&& previous->packet->pts != AV_NOPTS_VALUE) {
// we now know the duration of the previous packet
previous->packet.duration = rec->packet.pts - previous->packet.pts;
previous->packet->duration =
rec->packet->pts - previous->packet->pts;
}

bool ok = recorder_write(recorder, &previous->packet);
bool ok = recorder_write(recorder, previous->packet);
record_packet_delete(previous);
if (!ok) {
LOGE("Could not record packet");
Expand Down
2 changes: 1 addition & 1 deletion app/src/recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "util/thread.h"

struct record_packet {
AVPacket packet;
AVPacket *packet;
struct record_packet *next;
};

Expand Down

0 comments on commit 4af317d

Please sign in to comment.