[vlc-commits] ytdl: use FD directly instead of FILE

Rémi Denis-Courmont git at videolan.org
Wed Sep 30 20:53:12 CEST 2020


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Sep 30 21:41:04 2020 +0300| [e763b313b4be2f24e27c4358675565985937ef4a] | committer: Rémi Denis-Courmont

ytdl: use FD directly instead of FILE

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=e763b313b4be2f24e27c4358675565985937ef4a
---

 modules/demux/ytdl.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)

diff --git a/modules/demux/ytdl.c b/modules/demux/ytdl.c
index 97f4443072..63b082b4b9 100644
--- a/modules/demux/ytdl.c
+++ b/modules/demux/ytdl.c
@@ -39,7 +39,7 @@
 
 struct ytdl_json {
     struct vlc_logger *logger;
-    FILE *input;
+    int fd;
 };
 
 void json_parse_error(void *data, const char *msg)
@@ -53,24 +53,15 @@ size_t json_read(void *data, void *buf, size_t size)
 {
     struct ytdl_json *sys = data;
 
-    return fread(buf, 1, size, sys->input);
+    return read(sys->fd, buf, size);
 }
 
-static
-FILE *vlc_popen(pid_t *restrict pid, const char *argv[])
+static int ytdl_popen(pid_t *restrict pid, const char *argv[])
 {
     int fds[2];
 
     if (vlc_pipe(fds))
-        return NULL;
-
-    FILE *input = fdopen(fds[0], "rt");
-
-    if (input == NULL) {
-        vlc_close(fds[1]);
-        vlc_close(fds[0]);
-        return NULL;
-    }
+        return -1;
 
     int fdv[] = { -1, fds[1], 2, -1 };
     int val = vlc_spawn(pid, argv[0], fdv, argv);
@@ -78,12 +69,12 @@ FILE *vlc_popen(pid_t *restrict pid, const char *argv[])
     vlc_close(fds[1]);
 
     if (val) {
-        fclose(input);
-        input = NULL;
+        vlc_close(fds[0]);
         errno = val;
+        return -1;
     }
 
-    return input;
+    return fds[0];
 }
 
 struct ytdl_playlist {
@@ -91,7 +82,6 @@ struct ytdl_playlist {
     stream_t *source;
 };
 
-
 static int CompareFormats(const struct json_object *f_a,
                           const struct json_object *f_b, double pref_height)
 {
@@ -351,11 +341,14 @@ static int OpenCommon(vlc_object_t *obj)
     if (unlikely(path == NULL))
         return VLC_EGENERIC;
 
+    struct ytdl_json jsdata;
     pid_t pid;
     const char *argv[] = { path, s->psz_url, NULL };
-    FILE *input = vlc_popen(&pid, argv);
 
-    if (input == NULL) {
+    jsdata.logger = s->obj.logger;
+    jsdata.fd = ytdl_popen(&pid, argv);
+
+    if (jsdata.fd == -1) {
         msg_Dbg(obj, "cannot start %s: %s", path, vlc_strerror_c(errno));
         free(path);
         return VLC_EGENERIC;
@@ -363,11 +356,10 @@ static int OpenCommon(vlc_object_t *obj)
 
     free(path);
 
-    struct ytdl_json jsdata = { s->obj.logger, input };
     int val = json_parse(&jsdata, &sys->json);
 
     kill(pid, SIGTERM);
-    fclose(input);
+    vlc_close(jsdata.fd);
     vlc_waitpid(pid);
 
     if (val) {



More information about the vlc-commits mailing list