stream: implement get_protocols method for stream_lavf

Previously, all stream protocols were a static list in mpv. This is okay
for own builtin stuff, but for protocols that depend on ffmpeg it's not
so great. Support for certain protocols may or may not be enabled in a
user's ffmpeg and the protocol list that mpv generates should ideally
match this. Fix this by implementing a get_protocols method for
stream_lavf that will have different results depending on the ffmpeg mpv
is built against. We keep the safe and unsafe protocols separation. The
former is essentially a whitelist. Any protocol that is found in ffmpeg
but is not in the safe whitelist is considered unsafe. In the stream
list, ffmpeg is moved to the bottom so any possible protocols that are
added in the future don't automatically take precedence over any builtin
mpv ones.
This commit is contained in:
Dudemanguy 2024-05-15 19:22:32 -05:00
parent 406301f23d
commit f921b64ed7
5 changed files with 136 additions and 25 deletions

View File

@ -271,6 +271,18 @@ char **mp_get_lavf_demuxers(void)
return list; return list;
} }
char **mp_get_lavf_protocols(void)
{
char **list = NULL;
int num = 0;
void *opaque = NULL;
const char *name;
while ((name = avio_enum_protocols(&opaque, 0)))
MP_TARRAY_APPEND(NULL, list, num, talloc_strdup(list, name));
MP_TARRAY_APPEND(NULL, list, num, NULL);
return list;
}
int mp_codec_to_av_codec_id(const char *codec) int mp_codec_to_av_codec_id(const char *codec)
{ {
int id = AV_CODEC_ID_NONE; int id = AV_CODEC_ID_NONE;

View File

@ -42,6 +42,7 @@ void mp_set_avcodec_threads(struct mp_log *l, AVCodecContext *avctx, int threads
void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type); void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type);
void mp_add_lavc_encoders(struct mp_decoder_list *list); void mp_add_lavc_encoders(struct mp_decoder_list *list);
char **mp_get_lavf_demuxers(void); char **mp_get_lavf_demuxers(void);
char **mp_get_lavf_protocols(void);
int mp_codec_to_av_codec_id(const char *codec); int mp_codec_to_av_codec_id(const char *codec);
const char *mp_codec_from_av_codec_id(int codec_id); const char *mp_codec_from_av_codec_id(int codec_id);
bool mp_codec_is_lossless(const char *codec); bool mp_codec_is_lossless(const char *codec);

View File

@ -66,8 +66,6 @@ static const stream_info_t *const stream_list[] = {
#if HAVE_CDDA #if HAVE_CDDA
&stream_info_cdda, &stream_info_cdda,
#endif #endif
&stream_info_ffmpeg,
&stream_info_ffmpeg_unsafe,
&stream_info_avdevice, &stream_info_avdevice,
#if HAVE_DVBIN #if HAVE_DVBIN
&stream_info_dvb, &stream_info_dvb,
@ -92,6 +90,8 @@ static const stream_info_t *const stream_list[] = {
&stream_info_slice, &stream_info_slice,
&stream_info_fd, &stream_info_fd,
&stream_info_cb, &stream_info_cb,
&stream_info_ffmpeg,
&stream_info_ffmpeg_unsafe,
}; };
// Because of guarantees documented on STREAM_BUFFER_SIZE. // Because of guarantees documented on STREAM_BUFFER_SIZE.
@ -325,12 +325,17 @@ static int stream_create_instance(const stream_info_t *sinfo,
if (!sinfo->local_fs) if (!sinfo->local_fs)
return STREAM_NO_MATCH; return STREAM_NO_MATCH;
} else { } else {
for (int n = 0; sinfo->protocols && sinfo->protocols[n]; n++) { char **get_protocols = sinfo->get_protocols ? sinfo->get_protocols() : NULL;
path = match_proto(url, sinfo->protocols[n]); char **protocols = get_protocols ? get_protocols : (char **)sinfo->protocols;
for (int n = 0; protocols && protocols[n]; n++) {
path = match_proto(url, protocols[n]);
if (path) if (path)
break; break;
} }
talloc_free(get_protocols);
if (!path) if (!path)
return STREAM_NO_MATCH; return STREAM_NO_MATCH;
} }
@ -864,16 +869,17 @@ char **stream_get_proto_list(void)
for (int i = 0; i < MP_ARRAY_SIZE(stream_list); i++) { for (int i = 0; i < MP_ARRAY_SIZE(stream_list); i++) {
const stream_info_t *stream_info = stream_list[i]; const stream_info_t *stream_info = stream_list[i];
if (!stream_info->protocols) char **get_protocols = stream_info->get_protocols ? stream_info->get_protocols() : NULL;
continue; char **protocols = get_protocols ? get_protocols : (char **)stream_info->protocols;
for (int j = 0; stream_info->protocols[j]; j++) { for (int j = 0; protocols && protocols[j]; j++) {
if (*stream_info->protocols[j] == '\0') if (*protocols[j] == '\0')
continue; continue;
MP_TARRAY_APPEND(NULL, list, num, MP_TARRAY_APPEND(NULL, list, num, talloc_strdup(list, protocols[j]));
talloc_strdup(NULL, stream_info->protocols[j]));
} }
talloc_free(get_protocols);
} }
MP_TARRAY_APPEND(NULL, list, num, NULL); MP_TARRAY_APPEND(NULL, list, num, NULL);
return list; return list;
@ -888,7 +894,6 @@ void stream_print_proto_list(struct mp_log *log)
for (int i = 0; list[i]; i++) { for (int i = 0; list[i]; i++) {
mp_info(log, " %s://\n", list[i]); mp_info(log, " %s://\n", list[i]);
count++; count++;
talloc_free(list[i]);
} }
talloc_free(list); talloc_free(list);
mp_info(log, "\nTotal: %d protocols\n", count); mp_info(log, "\nTotal: %d protocols\n", count);
@ -899,10 +904,19 @@ bool stream_has_proto(const char *proto)
for (int i = 0; i < MP_ARRAY_SIZE(stream_list); i++) { for (int i = 0; i < MP_ARRAY_SIZE(stream_list); i++) {
const stream_info_t *stream_info = stream_list[i]; const stream_info_t *stream_info = stream_list[i];
for (int j = 0; stream_info->protocols && stream_info->protocols[j]; j++) { bool match = false;
if (strcmp(stream_info->protocols[j], proto) == 0) char **get_protocols = stream_info->get_protocols ? stream_info->get_protocols() : NULL;
return true; char **protocols = get_protocols ? get_protocols : (char **)stream_info->protocols;
for (int j = 0; protocols && protocols[j]; j++) {
if (strcmp(protocols[j], proto) == 0) {
match = true;
break;
}
} }
talloc_free(get_protocols);
return match;
} }
return false; return false;

View File

@ -114,6 +114,8 @@ typedef struct stream_info_st {
// Alternative to open(). Only either open() or open2() can be set. // Alternative to open(). Only either open() or open2() can be set.
int (*open2)(struct stream *st, const struct stream_open_args *args); int (*open2)(struct stream *st, const struct stream_open_args *args);
const char *const *protocols; const char *const *protocols;
// Alternative to protocols. For stream_lavf.
char **(*get_protocols)(void);
bool can_write; // correctly checks for READ/WRITE modes bool can_write; // correctly checks for READ/WRITE modes
bool local_fs; // supports STREAM_LOCAL_FS_ONLY bool local_fs; // supports STREAM_LOCAL_FS_ONLY
int stream_origin; // 0 or set of STREAM_ORIGIN_*; if 0, the same origin int stream_origin; // 0 or set of STREAM_ORIGIN_*; if 0, the same origin

View File

@ -238,6 +238,96 @@ void mp_setup_av_network_options(AVDictionary **dict, const char *target_fmt,
talloc_free(temp); talloc_free(temp);
} }
#define PROTO(...) (const char *[]){__VA_ARGS__, NULL}
// List of safe protocols and their aliases
static const char **safe_protos[] = {
PROTO("data"),
PROTO("gopher"),
PROTO("gophers"),
PROTO("http", "dav", "webdav"),
PROTO("httpproxy"),
PROTO("https", "davs", "webdavs"),
PROTO("ipfs"),
PROTO("ipns"),
PROTO("mmsh", "mms", "mmshttp"),
PROTO("mmst"),
PROTO("rist"),
PROTO("rtmp"),
PROTO("rtmpe"),
PROTO("rtmps"),
PROTO("rtmpt"),
PROTO("rtmpte"),
PROTO("rtmpts"),
PROTO("rtp"),
PROTO("srt"),
PROTO("srtp"),
NULL,
};
static char **get_safe_protocols(void)
{
int num = 0;
char **protocols = NULL;
char **ffmpeg_demuxers = mp_get_lavf_demuxers();
char **ffmpeg_protos = mp_get_lavf_protocols();
for (int i = 0; ffmpeg_protos[i]; i++) {
for (int j = 0; safe_protos[j]; j++) {
if (strcmp(ffmpeg_protos[i], safe_protos[j][0]) != 0)
continue;
for (int k = 0; safe_protos[j][k]; k++)
MP_TARRAY_APPEND(NULL, protocols, num, talloc_strdup(protocols, safe_protos[j][k]));
break;
}
}
// rtsp is a demuxer not protocol in ffmpeg so it is handled separately
for (int i = 0; ffmpeg_demuxers[i]; i++) {
if (strcmp("rtsp", ffmpeg_demuxers[i]) == 0) {
MP_TARRAY_APPEND(NULL, protocols, num, talloc_strdup(protocols, "rtsp"));
MP_TARRAY_APPEND(NULL, protocols, num, talloc_strdup(protocols, "rtsps"));
break;
}
}
MP_TARRAY_APPEND(NULL, protocols, num, NULL);
talloc_free(ffmpeg_demuxers);
talloc_free(ffmpeg_protos);
return protocols;
}
static char **get_unsafe_protocols(void)
{
int num = 0;
char **protocols = NULL;
char **safe_protocols = get_safe_protocols();
char **ffmpeg_protos = mp_get_lavf_protocols();
for (int i = 0; ffmpeg_protos[i]; i++) {
bool safe_protocol = false;
for (int j = 0; safe_protocols[j]; j++) {
if (strcmp(ffmpeg_protos[i], safe_protocols[j]) == 0) {
safe_protocol = true;
break;
}
}
if (!safe_protocol)
MP_TARRAY_APPEND(NULL, protocols, num, talloc_strdup(protocols, ffmpeg_protos[i]));
}
MP_TARRAY_APPEND(NULL, protocols, num, talloc_strdup(protocols, "ffmpeg"));
MP_TARRAY_APPEND(NULL, protocols, num, talloc_strdup(protocols, "lavf"));
MP_TARRAY_APPEND(NULL, protocols, num, NULL);
talloc_free(ffmpeg_protos);
talloc_free(safe_protocols);
return protocols;
}
// Escape http URLs with unescaped, invalid characters in them. // Escape http URLs with unescaped, invalid characters in them.
// libavformat's http protocol does not do this, and a patch to add this // libavformat's http protocol does not do this, and a patch to add this
// in a 100% safe case (spaces only) was rejected. // in a 100% safe case (spaces only) was rejected.
@ -431,12 +521,7 @@ done:
const stream_info_t stream_info_ffmpeg = { const stream_info_t stream_info_ffmpeg = {
.name = "ffmpeg", .name = "ffmpeg",
.open = open_f, .open = open_f,
.protocols = (const char *const[]){ .get_protocols = get_safe_protocols,
"rtmp", "rtsp", "rtsps", "http", "https", "mms", "mmst", "mmsh", "mmshttp",
"rtp", "httpproxy", "rtmpe", "rtmps", "rtmpt", "rtmpte", "rtmpts", "srt",
"rist", "srtp", "gopher", "gophers", "data", "ipfs", "ipns", "dav",
"davs", "webdav", "webdavs",
NULL },
.can_write = true, .can_write = true,
.stream_origin = STREAM_ORIGIN_NET, .stream_origin = STREAM_ORIGIN_NET,
}; };
@ -448,10 +533,7 @@ const stream_info_t stream_info_ffmpeg = {
const stream_info_t stream_info_ffmpeg_unsafe = { const stream_info_t stream_info_ffmpeg_unsafe = {
.name = "ffmpeg", .name = "ffmpeg",
.open = open_f, .open = open_f,
.protocols = (const char *const[]){ .get_protocols = get_unsafe_protocols,
"lavf", "ffmpeg", "udp", "ftp", "tcp", "tls", "unix", "sftp", "md5",
"concat", "smb",
NULL },
.stream_origin = STREAM_ORIGIN_UNSAFE, .stream_origin = STREAM_ORIGIN_UNSAFE,
.can_write = true, .can_write = true,
}; };