Skip to content

Commit

Permalink
Reduce latency by 1 frame
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rom1v committed Jul 29, 2019
1 parent 3b69463 commit 0f0ccf4
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 182 deletions.
10 changes: 10 additions & 0 deletions app/src/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,21 @@ recorder_rescale_packet(struct recorder *recorder, AVPacket *packet) {
bool
recorder_write(struct recorder *recorder, AVPacket *packet) {
if (!recorder->header_written) {
if (packet->pts != AV_NOPTS_VALUE) {
LOGE("The first packet is not a config packet");
return false;
}
bool ok = recorder_write_header(recorder, packet);
if (!ok) {
return false;
}
recorder->header_written = true;
return true;
}

if (packet->pts == AV_NOPTS_VALUE) {
// ignore config packets
return true;
}

recorder_rescale_packet(recorder, packet);
Expand Down
1 change: 0 additions & 1 deletion app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ scrcpy(const struct scrcpy_options *options) {
.local_port = options->port,
.max_size = options->max_size,
.bit_rate = options->bit_rate,
.send_frame_meta = record,
.control = options->control,
};
if (!server_start(&server, options->serial, &params)) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ execute_server(struct server *server, const struct server_params *params) {
bit_rate_string,
server->tunnel_forward ? "true" : "false",
params->crop ? params->crop : "-",
params->send_frame_meta ? "true" : "false",
"true", // always send frame headers
params->control ? "true" : "false",
};
return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
Expand Down
1 change: 0 additions & 1 deletion app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct server_params {
uint16_t local_port;
uint16_t max_size;
uint32_t bit_rate;
bool send_frame_meta;
bool control;
};

Expand Down
Loading

0 comments on commit 0f0ccf4

Please sign in to comment.