Skip to content

Commit 63af7fb

Browse files
committed
Reduce latency by 1 frame
To packetize the H.264 raw stream, av_parser_parse2() (called by av_read_frame()) knows that it has received a full frame only after it has received some data for the next frame. As a consequence, the client always waited until the next frame before sending the current frame to the decoder! On the device side, we know packets boundaries. To reduce latency, make the device always transmit the "frame meta" to packetize the stream manually (it was already implemented to send PTS, but only enabled on recording). On the client side, replace av_read_frame() by manual packetizing and parsing. <https://stackoverflow.com/questions/50682518/replacing-av-read-frame-to-reduce-delay> <https://trac.ffmpeg.org/ticket/3354>
1 parent a90ccbd commit 63af7fb

File tree

6 files changed

+174
-181
lines changed

6 files changed

+174
-181
lines changed

app/src/recorder.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,21 @@ recorder_rescale_packet(struct recorder *recorder, AVPacket *packet) {
166166
bool
167167
recorder_write(struct recorder *recorder, AVPacket *packet) {
168168
if (!recorder->header_written) {
169+
if (packet->pts != AV_NOPTS_VALUE) {
170+
LOGE("The first packet is not a config packet");
171+
return false;
172+
}
169173
bool ok = recorder_write_header(recorder, packet);
170174
if (!ok) {
171175
return false;
172176
}
173177
recorder->header_written = true;
178+
return true;
179+
}
180+
181+
if (packet->pts == AV_NOPTS_VALUE) {
182+
// ignore config packets
183+
return true;
174184
}
175185

176186
recorder_rescale_packet(recorder, packet);

app/src/scrcpy.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ scrcpy(const struct scrcpy_options *options) {
277277
.local_port = options->port,
278278
.max_size = options->max_size,
279279
.bit_rate = options->bit_rate,
280-
.send_frame_meta = record,
281280
.control = options->control,
282281
};
283282
if (!server_start(&server, options->serial, &params)) {

app/src/server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ execute_server(struct server *server, const struct server_params *params) {
130130
bit_rate_string,
131131
server->tunnel_forward ? "true" : "false",
132132
params->crop ? params->crop : "-",
133-
params->send_frame_meta ? "true" : "false",
133+
"true", // always send frame meta (packet boundaries + timestamp)
134134
params->control ? "true" : "false",
135135
};
136136
return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0]));

app/src/server.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ struct server_params {
3434
uint16_t local_port;
3535
uint16_t max_size;
3636
uint32_t bit_rate;
37-
bool send_frame_meta;
3837
bool control;
3938
};
4039

0 commit comments

Comments
 (0)