libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

test-vlc.c (5256B)


      1 /*
      2      This file is part of libextractor.
      3      Copyright (C) 2021 Christian Grothoff
      4 
      5      libextractor is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      libextractor is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with libextractor; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19 
     20 NOTE: This plugin is not yet working. Somehow libvlc never calls any of the IO callbacks.
     21 
     22 */
     23 /**
     24  * @file plugins/vlc_extractor.c
     25  * @brief plugin to extract metadata using libvlc
     26  * @author Christian Grothoff
     27  */
     28 #include <vlc/vlc.h>
     29 #include <signal.h>
     30 #include <stdlib.h>
     31 #include <stdio.h>
     32 #include <errno.h>
     33 #include <sys/types.h>
     34 #include <sys/stat.h>
     35 #include <fcntl.h>
     36 #include <string.h>
     37 #include <unistd.h>
     38 
     39 
     40 static void
     41 extract (void *ptr,
     42          libvlc_media_t *media)
     43 {
     44   struct
     45   {
     46     enum libvlc_meta_t vt;
     47   } map[] = {
     48     { libvlc_meta_Title, },
     49     { libvlc_meta_Artist, },
     50     { libvlc_meta_Genre, },
     51     { libvlc_meta_Copyright, },
     52     { libvlc_meta_Album, },
     53     { libvlc_meta_TrackNumber, },
     54     { libvlc_meta_Description, },
     55     { libvlc_meta_Rating, },
     56     { libvlc_meta_Date, },
     57     { libvlc_meta_Setting, },
     58     { libvlc_meta_URL, },
     59     { libvlc_meta_Language, },
     60     { libvlc_meta_NowPlaying, },
     61     { libvlc_meta_Publisher, },
     62     { libvlc_meta_EncodedBy, },
     63     { libvlc_meta_ArtworkURL, },
     64     { libvlc_meta_TrackID, },
     65     { libvlc_meta_TrackTotal, },
     66     { libvlc_meta_Director, },
     67     { libvlc_meta_Season, },
     68     { libvlc_meta_Episode, },
     69     { libvlc_meta_ShowName, },
     70     { libvlc_meta_Actors, },
     71     { libvlc_meta_AlbumArtist, },
     72     { libvlc_meta_DiscNumber, },
     73     { libvlc_meta_DiscTotal, },
     74     { -1 }
     75   };
     76 
     77   for (unsigned int i = 0;
     78        -1 != map[i].vt;
     79        i++)
     80   {
     81     char *meta;
     82 
     83     fprintf (stderr,
     84              ".");
     85     meta = libvlc_media_get_meta (media,
     86                                   map[i].vt);
     87     if (NULL == meta)
     88       continue;
     89     fprintf (stderr,
     90              "Found %d: %s\n",
     91              map[i].vt,
     92              meta);
     93     free (meta);
     94   }
     95 }
     96 
     97 
     98 static void
     99 media_ready (const struct libvlc_event_t *p_event,
    100              void *p_data)
    101 {
    102   fprintf (stderr,
    103            "media status: %d, %d\n",
    104            p_event->type == libvlc_MediaParsedChanged,
    105            p_event->u.media_parsed_changed.new_status);
    106   if (p_event->u.media_parsed_changed.new_status ==
    107       libvlc_media_parsed_status_done)
    108   {
    109     fprintf (stderr,
    110              "media ready\n");
    111   }
    112 }
    113 
    114 
    115 static void
    116 my_logger (void *data,
    117            int level,
    118            const libvlc_log_t *ctx,
    119            const char *fmt,
    120            va_list args)
    121 {
    122   vfprintf (stderr,
    123             fmt,
    124             args);
    125   fprintf (stderr, "\n");
    126 }
    127 
    128 
    129 /**
    130  * Extract information using libvlc
    131  *
    132  * @param ec extraction context
    133  */
    134 void
    135 main (int argc,
    136       char **argv)
    137 {
    138   libvlc_instance_t *vlc;
    139   libvlc_media_t *media;
    140   libvlc_event_manager_t *em;
    141 
    142   {
    143     sigset_t set;
    144 
    145     signal (SIGCHLD, SIG_DFL);
    146     sigemptyset (&set);
    147     sigaddset (&set, SIGPIPE);
    148     pthread_sigmask (SIG_BLOCK, &set, NULL);
    149   }
    150 
    151   {
    152     const char *argv[] = {
    153       "-v",
    154       "3",
    155       NULL
    156     };
    157     vlc = libvlc_new (2, argv);
    158   }
    159   if (NULL == vlc)
    160     return;
    161   libvlc_log_set (vlc,
    162                   &my_logger,
    163                   NULL);
    164   if (0)
    165   {
    166     media = libvlc_media_new_path (vlc,
    167                                    argv[1]);
    168   }
    169   else
    170   {
    171     int fd = open (argv[1],
    172                    O_RDONLY);
    173     if (-1 == fd)
    174     {
    175       fprintf (stderr,
    176                "Open %s failed: %s\n",
    177                argv[1],
    178                strerror (errno));
    179       libvlc_release (vlc);
    180       return;
    181     }
    182     media = libvlc_media_new_fd (vlc,
    183                                  fd);
    184   }
    185   if (NULL == media)
    186   {
    187     fprintf (stderr,
    188              "Opening path `%s' failed!\n",
    189              argv[1]);
    190     libvlc_release (vlc);
    191     return;
    192   }
    193 
    194   em = libvlc_media_event_manager (media);
    195   libvlc_event_attach (em,
    196                        libvlc_MediaParsedChanged,
    197                        &media_ready,
    198                        NULL);
    199   fprintf (stderr,
    200            "Triggering parser\n");
    201   {
    202     int status;
    203 
    204     status = libvlc_media_parse_with_options (media,
    205                                               libvlc_media_fetch_local
    206                                               | libvlc_media_parse_network
    207                                               | libvlc_media_fetch_network,
    208                                               30000); /* 30s timeout */
    209     fprintf (stderr,
    210              "Status: %d\n",
    211              status);
    212   }
    213   fprintf (stderr,
    214            "Sleeping\n");
    215   sleep (2);
    216   extract (NULL,
    217            media);
    218   libvlc_media_release (media);
    219   libvlc_release (vlc);
    220 }
    221 
    222 
    223 /* end of vlc_extractor.c */