Skip to content

Commit

Permalink
Add buffering command line options
Browse files Browse the repository at this point in the history
Add --display-buffer and --v4l2-buffer options to configure buffering
time.
  • Loading branch information
rom1v committed Jul 6, 2021
1 parent 02ecf3d commit 8f80c7d
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ For example, you could capture the video within [OBS].
[OBS]: https://obsproject.com/fr


#### Buffering

It is possible to add buffering. This increases latency but reduces jitter (see
#2464).

The option is available for display buffering:

```bash
scrcpy --display-buffer=50 # add 50 ms buffering for display
```

and V4L2 sink:

```bash
scrcpy --v4l2-buffer=500 # add 500 ms buffering for v4l2 sink
```


### Connection

#### Wireless
Expand Down
12 changes: 12 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ The list of possible display ids can be listed by "adb shell dumpsys display"

Default is 0.

.TP
.BI "\-\-display\-buffer ms
Add a buffering delay (in milliseconds) before displaying. This increases latency to compensate for jitter.

Default is 0 (no buffering).

.TP
.BI "\-\-encoder " name
Use a specific MediaCodec encoder (must be a H.264 encoder).
Expand Down Expand Up @@ -191,6 +197,12 @@ Output to v4l2loopback device.

It requires to lock the video orientation (see \fB\-\-lock\-video\-orientation\fR).

.TP
.BI "\-\-v4l2-buffer " ms
Add a buffering delay (in milliseconds) before pushing frames. This increases latency to compensate for jitter.

This option is similar to \fB\-\-display\-buffer\fR, but specific to V4L2 sink.

.TP
.BI "\-V, \-\-verbosity " value
Set the log level ("verbose", "debug", "info", "warn" or "error").
Expand Down
42 changes: 42 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ scrcpy_print_usage(const char *arg0) {
"\n"
" Default is 0.\n"
"\n"
" --display-buffer ms\n"
" Add a buffering delay (in milliseconds) before displaying.\n"
" This increases latency to compensate for jitter.\n"
"\n"
" Default is 0 (no buffering).\n"
"\n"
" --encoder name\n"
" Use a specific MediaCodec encoder (must be a H.264 encoder).\n"
"\n"
Expand Down Expand Up @@ -182,6 +188,15 @@ scrcpy_print_usage(const char *arg0) {
" It requires to lock the video orientation (see\n"
" --lock-video-orientation).\n"
"\n"
" --v4l2-buffer ms\n"
" Add a buffering delay (in milliseconds) before pushing\n"
" frames. This increases latency to compensate for jitter.\n"
"\n"
" This option is similar to --display-buffer, but specific to\n"
" V4L2 sink.\n"
"\n"
" Default is 0 (no buffering).\n"
"\n"
#endif
" -V, --verbosity value\n"
" Set the log level (verbose, debug, info, warn or error).\n"
Expand Down Expand Up @@ -392,6 +407,19 @@ parse_max_fps(const char *s, uint16_t *max_fps) {
return true;
}

static bool
parse_buffering_time(const char *s, sc_tick *tick) {
long value;
bool ok = parse_integer_arg(s, &value, false, 0, 0xFFFFFFFF,
"buffering time");
if (!ok) {
return false;
}

*tick = (sc_tick) value;
return true;
}

static bool
parse_lock_video_orientation(const char *s,
enum sc_lock_video_orientation *lock_mode) {
Expand Down Expand Up @@ -689,6 +717,8 @@ guess_record_format(const char *filename) {
#define OPT_ENCODER_NAME 1025
#define OPT_POWER_OFF_ON_CLOSE 1026
#define OPT_V4L2_SINK 1027
#define OPT_DISPLAY_BUFFER 1028
#define OPT_V4L2_BUFFER 1029

bool
scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
Expand All @@ -700,6 +730,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
{"disable-screensaver", no_argument, NULL,
OPT_DISABLE_SCREENSAVER},
{"display", required_argument, NULL, OPT_DISPLAY_ID},
{"display-buffer", required_argument, NULL, OPT_DISPLAY_BUFFER},
{"encoder", required_argument, NULL, OPT_ENCODER_NAME},
{"force-adb-forward", no_argument, NULL,
OPT_FORCE_ADB_FORWARD},
Expand Down Expand Up @@ -732,6 +763,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
{"turn-screen-off", no_argument, NULL, 'S'},
#ifdef HAVE_V4L2
{"v4l2-sink", required_argument, NULL, OPT_V4L2_SINK},
{"v4l2-buffer", required_argument, NULL, OPT_V4L2_BUFFER},
#endif
{"verbosity", required_argument, NULL, 'V'},
{"version", no_argument, NULL, 'v'},
Expand Down Expand Up @@ -917,10 +949,20 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
case OPT_POWER_OFF_ON_CLOSE:
opts->power_off_on_close = true;
break;
case OPT_DISPLAY_BUFFER:
if (!parse_buffering_time(optarg, &opts->display_buffer)) {
return false;
}
break;
#ifdef HAVE_V4L2
case OPT_V4L2_SINK:
opts->v4l2_device = optarg;
break;
case OPT_V4L2_BUFFER:
if (!parse_buffering_time(optarg, &opts->v4l2_buffer)) {
return false;
}
break;
#endif
default:
// getopt prints the error message on stderr
Expand Down
4 changes: 3 additions & 1 deletion app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ scrcpy(const struct scrcpy_options *options) {
.rotation = options->rotation,
.mipmaps = options->mipmaps,
.fullscreen = options->fullscreen,
.buffering_time = options->display_buffer,
};

if (!screen_init(&s->screen, &screen_params)) {
Expand All @@ -393,7 +394,8 @@ scrcpy(const struct scrcpy_options *options) {

#ifdef HAVE_V4L2
if (options->v4l2_device) {
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, frame_size)) {
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, frame_size,
options->v4l2_buffer)) {
goto end;
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <stddef.h>
#include <stdint.h>

#include "util/tick.h"

enum sc_log_level {
SC_LOG_LEVEL_VERBOSE,
SC_LOG_LEVEL_DEBUG,
Expand Down Expand Up @@ -78,6 +80,8 @@ struct scrcpy_options {
uint16_t window_width;
uint16_t window_height;
uint32_t display_id;
sc_tick display_buffer;
sc_tick v4l2_buffer;
bool show_touches;
bool fullscreen;
bool always_on_top;
Expand Down Expand Up @@ -126,6 +130,8 @@ struct scrcpy_options {
.window_width = 0, \
.window_height = 0, \
.display_id = 0, \
.display_buffer = 0, \
.v4l2_buffer = 0, \
.show_touches = false, \
.fullscreen = false, \
.always_on_top = false, \
Expand Down
3 changes: 2 additions & 1 deletion app/src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ screen_init(struct screen *screen, const struct screen_params *params) {
.on_new_frame = sc_video_buffer_on_new_frame,
};

bool ok = sc_video_buffer_init(&screen->vb, 0, &cbs, screen);
bool ok = sc_video_buffer_init(&screen->vb, params->buffering_time, &cbs,
screen);
if (!ok) {
LOGE("Could not initialize video buffer");
return false;
Expand Down
2 changes: 2 additions & 0 deletions app/src/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct screen_params {
bool mipmaps;

bool fullscreen;

sc_tick buffering_time;
};

// initialize screen, create window, renderer and texture (window is hidden)
Expand Down
5 changes: 3 additions & 2 deletions app/src/v4l2_sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
.on_new_frame = sc_video_buffer_on_new_frame,
};

bool ok = sc_video_buffer_init(&vs->vb, 0, &cbs, vs);
bool ok = sc_video_buffer_init(&vs->vb, vs->buffering_time, &cbs, vs);
if (!ok) {
LOGE("Could not initialize video buffer");
return false;
Expand Down Expand Up @@ -356,14 +356,15 @@ sc_v4l2_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {

bool
sc_v4l2_sink_init(struct sc_v4l2_sink *vs, const char *device_name,
struct size frame_size) {
struct size frame_size, sc_tick buffering_time) {
vs->device_name = strdup(device_name);
if (!vs->device_name) {
LOGE("Could not strdup v4l2 device name");
return false;
}

vs->frame_size = frame_size;
vs->buffering_time = buffering_time;

static const struct sc_frame_sink_ops ops = {
.open = sc_v4l2_frame_sink_open,
Expand Down
4 changes: 3 additions & 1 deletion app/src/v4l2_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "coords.h"
#include "trait/frame_sink.h"
#include "video_buffer.h"
#include "util/tick.h"

#include <libavformat/avformat.h>

Expand All @@ -18,6 +19,7 @@ struct sc_v4l2_sink {

char *device_name;
struct size frame_size;
sc_tick buffering_time;

sc_thread thread;
sc_mutex mutex;
Expand All @@ -32,7 +34,7 @@ struct sc_v4l2_sink {

bool
sc_v4l2_sink_init(struct sc_v4l2_sink *vs, const char *device_name,
struct size frame_size);
struct size frame_size, sc_tick buffering_time);

void
sc_v4l2_sink_destroy(struct sc_v4l2_sink *vs);
Expand Down

0 comments on commit 8f80c7d

Please sign in to comment.