Skip to content

Commit

Permalink
add --video-source, --camera and --list-camera options
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Aug 13, 2023
1 parent aec4362 commit a4e7a7e
Show file tree
Hide file tree
Showing 13 changed files with 595 additions and 567 deletions.
56 changes: 56 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum {
OPT_MAX_FPS,
OPT_LOCK_VIDEO_ORIENTATION,
OPT_DISPLAY_ID,
OPT_CAMERA_ID,
OPT_ROTATION,
OPT_RENDER_DRIVER,
OPT_NO_MIPMAPS,
Expand Down Expand Up @@ -69,13 +70,15 @@ enum {
OPT_AUDIO_ENCODER,
OPT_LIST_ENCODERS,
OPT_LIST_DISPLAYS,
OPT_LIST_CAMERAS,
OPT_REQUIRE_AUDIO,
OPT_AUDIO_BUFFER,
OPT_AUDIO_OUTPUT_BUFFER,
OPT_NO_DISPLAY,
OPT_NO_VIDEO,
OPT_NO_AUDIO_PLAYBACK,
OPT_NO_VIDEO_PLAYBACK,
OPT_VIDEO_SOURCE,
OPT_AUDIO_SOURCE,
OPT_KILL_ADB_ON_CLOSE,
OPT_TIME_LIMIT,
Expand Down Expand Up @@ -181,6 +184,13 @@ static const struct sc_option options[] = {
"a higher value (10). Do not change this setting otherwise.\n"
"Default is 5.",
},
{
.longopt_id = OPT_VIDEO_SOURCE,
.longopt = "video-source",
.argdesc = "source",
.text = "Select the video source (display or camera).\n"
"Default is display.",
},
{
.shortopt = 'b',
.longopt = "video-bit-rate",
Expand Down Expand Up @@ -239,6 +249,15 @@ static const struct sc_option options[] = {
" scrcpy --list-displays\n"
"Default is 0.",
},
{
.longopt_id = OPT_CAMERA_ID,
.longopt = "camera",
.argdesc = "id",
.text = "Specify the device camera id to mirror when using --video-source camera.\n"
"The available camera ids can be listed by:\n"
" scrcpy --list-cameras\n"
"Default is the first one.",
},
{
.longopt_id = OPT_DISPLAY_BUFFER,
.longopt = "display-buffer",
Expand Down Expand Up @@ -317,6 +336,11 @@ static const struct sc_option options[] = {
.longopt = "list-displays",
.text = "List device displays.",
},
{
.longopt_id = OPT_LIST_CAMERAS,
.longopt = "list-cameras",
.text = "List device cameras.",
},
{
.longopt_id = OPT_LIST_ENCODERS,
.longopt = "list-encoders",
Expand Down Expand Up @@ -1625,6 +1649,22 @@ parse_audio_source(const char *optarg, enum sc_audio_source *source) {
return false;
}

static bool
parse_video_source(const char *optarg, enum sc_video_source *source) {
if (!strcmp(optarg, "display")) {
*source = SC_VIDEO_SOURCE_DISPLAY;
return true;
}

if (!strcmp(optarg, "camera")) {
*source = SC_VIDEO_SOURCE_CAMERA;
return true;
}

LOGE("Unsupported video source: %s (expected display or camera)", optarg);
return false;
}

static bool
parse_time_limit(const char *s, sc_tick *tick) {
long value;
Expand Down Expand Up @@ -1669,6 +1709,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
break;
case OPT_CAMERA_ID:
opts->camera_id = optarg;
break;
case 'd':
opts->select_usb = true;
break;
Expand Down Expand Up @@ -1950,6 +1993,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
case OPT_LIST_DISPLAYS:
opts->list_displays = true;
break;
case OPT_LIST_CAMERAS:
opts->list_cameras = true;
break;
case OPT_REQUIRE_AUDIO:
opts->require_audio = true;
break;
Expand All @@ -1969,6 +2015,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
break;
case OPT_VIDEO_SOURCE:
if (!parse_video_source(optarg, &opts->video_source)) {
return false;
}
break;
case OPT_KILL_ADB_ON_CLOSE:
opts->kill_adb_on_close = true;
break;
Expand Down Expand Up @@ -2124,6 +2175,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
}
}

if (opts->video_source == SC_VIDEO_SOURCE_CAMERA && opts->control) {
LOGI("--video-source camera disables control");
opts->control = false;
}

if (!opts->control) {
if (opts->turn_screen_off) {
LOGE("Could not request to turn screen off if control is disabled");
Expand Down
3 changes: 3 additions & 0 deletions app/src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const struct scrcpy_options scrcpy_options_default = {
.log_level = SC_LOG_LEVEL_INFO,
.video_codec = SC_CODEC_H264,
.audio_codec = SC_CODEC_OPUS,
.video_source = SC_VIDEO_SOURCE_DISPLAY,
.audio_source = SC_AUDIO_SOURCE_OUTPUT,
.record_format = SC_RECORD_FORMAT_AUTO,
.keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT,
Expand All @@ -39,6 +40,7 @@ const struct scrcpy_options scrcpy_options_default = {
.window_width = 0,
.window_height = 0,
.display_id = 0,
.camera_id = NULL,
.display_buffer = 0,
.audio_buffer = SC_TICK_FROM_MS(50),
.audio_output_buffer = SC_TICK_FROM_MS(5),
Expand Down Expand Up @@ -81,5 +83,6 @@ const struct scrcpy_options scrcpy_options_default = {
.require_audio = false,
.list_encoders = false,
.list_displays = false,
.list_cameras = false,
.kill_adb_on_close = false,
};
8 changes: 8 additions & 0 deletions app/src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ enum sc_audio_source {
SC_AUDIO_SOURCE_MIC,
};

enum sc_video_source {
SC_VIDEO_SOURCE_DISPLAY,
SC_VIDEO_SOURCE_CAMERA,
};

enum sc_lock_video_orientation {
SC_LOCK_VIDEO_ORIENTATION_UNLOCKED = -1,
// lock the current orientation when scrcpy starts
Expand Down Expand Up @@ -121,6 +126,7 @@ struct scrcpy_options {
enum sc_codec video_codec;
enum sc_codec audio_codec;
enum sc_audio_source audio_source;
enum sc_video_source video_source;
enum sc_record_format record_format;
enum sc_keyboard_input_mode keyboard_input_mode;
enum sc_mouse_input_mode mouse_input_mode;
Expand All @@ -139,6 +145,7 @@ struct scrcpy_options {
uint16_t window_width;
uint16_t window_height;
uint32_t display_id;
const char* camera_id;
sc_tick display_buffer;
sc_tick audio_buffer;
sc_tick audio_output_buffer;
Expand Down Expand Up @@ -181,6 +188,7 @@ struct scrcpy_options {
bool require_audio;
bool list_encoders;
bool list_displays;
bool list_cameras;
bool kill_adb_on_close;
};

Expand Down
5 changes: 4 additions & 1 deletion app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ scrcpy(struct scrcpy_options *options) {
.log_level = options->log_level,
.video_codec = options->video_codec,
.audio_codec = options->audio_codec,
.video_source = options->video_source,
.audio_source = options->audio_source,
.crop = options->crop,
.port_range = options->port_range,
Expand All @@ -363,6 +364,7 @@ scrcpy(struct scrcpy_options *options) {
.lock_video_orientation = options->lock_video_orientation,
.control = options->control,
.display_id = options->display_id,
.camera_id = options->camera_id,
.video = options->video,
.audio = options->audio,
.show_touches = options->show_touches,
Expand All @@ -381,6 +383,7 @@ scrcpy(struct scrcpy_options *options) {
.power_on = options->power_on,
.list_encoders = options->list_encoders,
.list_displays = options->list_displays,
.list_cameras = options->list_cameras,
.kill_adb_on_close = options->kill_adb_on_close,
};

Expand All @@ -399,7 +402,7 @@ scrcpy(struct scrcpy_options *options) {

server_started = true;

if (options->list_encoders || options->list_displays) {
if (options->list_encoders || options->list_displays || options->list_cameras) {
bool ok = await_for_server(NULL);
ret = ok ? SCRCPY_EXIT_SUCCESS : SCRCPY_EXIT_FAILURE;
goto end;
Expand Down
14 changes: 12 additions & 2 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ execute_server(struct sc_server *server,
ADD_PARAM("audio_codec=%s",
sc_server_get_codec_name(params->audio_codec));
}
if (params->video_source != SC_VIDEO_SOURCE_DISPLAY){
assert(params->audio_source == SC_VIDEO_SOURCE_CAMERA);
ADD_PARAM("video_source=camera");
}
if (params->audio_source != SC_AUDIO_SOURCE_OUTPUT) {
assert(params->audio_source == SC_AUDIO_SOURCE_MIC);
ADD_PARAM("audio_source=mic");
Expand All @@ -270,9 +274,12 @@ execute_server(struct sc_server *server,
// By default, control is true
ADD_PARAM("control=false");
}
if (params->display_id) {
if (params->video_source == SC_VIDEO_SOURCE_DISPLAY && params->display_id) {
ADD_PARAM("display_id=%" PRIu32, params->display_id);
}
if (params->video_source == SC_VIDEO_SOURCE_CAMERA && params->camera_id) {
ADD_PARAM("camera_id=%s", params->camera_id);
}
if (params->show_touches) {
ADD_PARAM("show_touches=true");
}
Expand Down Expand Up @@ -316,6 +323,9 @@ execute_server(struct sc_server *server,
if (params->list_displays) {
ADD_PARAM("list_displays=true");
}
if (params->list_cameras) {
ADD_PARAM("list_cameras=true");
}

#undef ADD_PARAM

Expand Down Expand Up @@ -895,7 +905,7 @@ run_server(void *data) {

// If --list-* is passed, then the server just prints the requested data
// then exits.
if (params->list_encoders || params->list_displays) {
if (params->list_encoders || params->list_displays || params->list_cameras) {
sc_pid pid = execute_server(server, params);
if (pid == SC_PROCESS_NONE) {
goto error_connection_failed;
Expand Down
3 changes: 3 additions & 0 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct sc_server_params {
enum sc_log_level log_level;
enum sc_codec video_codec;
enum sc_codec audio_codec;
enum sc_video_source video_source;
enum sc_audio_source audio_source;
const char *crop;
const char *video_codec_options;
Expand All @@ -42,6 +43,7 @@ struct sc_server_params {
int8_t lock_video_orientation;
bool control;
uint32_t display_id;
const char *camera_id;
bool video;
bool audio;
bool show_touches;
Expand All @@ -58,6 +60,7 @@ struct sc_server_params {
bool power_on;
bool list_encoders;
bool list_displays;
bool list_cameras;
bool kill_adb_on_close;
};

Expand Down
Loading

0 comments on commit a4e7a7e

Please sign in to comment.