Factorize record format parsing

Convert either the filename extension or the explicit record format
to a sc_record_format using the same function.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
This commit is contained in:
Romain Vimont 2023-05-07 12:18:27 +02:00
parent 8c650e53cd
commit be86e14e05

View file

@ -1479,18 +1479,27 @@ sc_parse_shortcut_mods(const char *s, struct sc_shortcut_mods *mods) {
} }
#endif #endif
static enum sc_record_format
get_record_format(const char *name) {
if (!strcmp(name, "mp4")) {
return SC_RECORD_FORMAT_MP4;
}
if (!strcmp(name, "mkv")) {
return SC_RECORD_FORMAT_MKV;
}
return 0;
}
static bool static bool
parse_record_format(const char *optarg, enum sc_record_format *format) { parse_record_format(const char *optarg, enum sc_record_format *format) {
if (!strcmp(optarg, "mp4")) { enum sc_record_format fmt = get_record_format(optarg);
*format = SC_RECORD_FORMAT_MP4; if (!fmt) {
return true;
}
if (!strcmp(optarg, "mkv")) {
*format = SC_RECORD_FORMAT_MKV;
return true;
}
LOGE("Unsupported format: %s (expected mp4 or mkv)", optarg); LOGE("Unsupported format: %s (expected mp4 or mkv)", optarg);
return false; return false;
}
*format = fmt;
return true;
} }
static bool static bool
@ -1510,18 +1519,13 @@ parse_port(const char *optarg, uint16_t *port) {
static enum sc_record_format static enum sc_record_format
guess_record_format(const char *filename) { guess_record_format(const char *filename) {
size_t len = strlen(filename); const char *dot = strrchr(filename, '.');
if (len < 4) { if (!dot) {
return 0; return 0;
} }
const char *ext = &filename[len - 4];
if (!strcmp(ext, ".mp4")) { const char *ext = dot + 1;
return SC_RECORD_FORMAT_MP4; return get_record_format(ext);
}
if (!strcmp(ext, ".mkv")) {
return SC_RECORD_FORMAT_MKV;
}
return 0;
} }
static bool static bool