aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/test-vlc.c
blob: 6bef89346ca48275c2eff4fa13188f12d61d5740 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
     This file is part of libextractor.
     Copyright (C) 2021 Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.

NOTE: This plugin is not yet working. Somehow libvlc never calls any of the IO callbacks.

*/
/**
 * @file plugins/vlc_extractor.c
 * @brief plugin to extract metadata using libvlc
 * @author Christian Grothoff
 */
#include <vlc/vlc.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>


static void
extract (void *ptr,
         libvlc_media_t *media)
{
  struct
  {
    enum libvlc_meta_t vt;
  } map[] = {
    { libvlc_meta_Title, },
    { libvlc_meta_Artist, },
    { libvlc_meta_Genre, },
    { libvlc_meta_Copyright, },
    { libvlc_meta_Album, },
    { libvlc_meta_TrackNumber, },
    { libvlc_meta_Description, },
    { libvlc_meta_Rating, },
    { libvlc_meta_Date, },
    { libvlc_meta_Setting, },
    { libvlc_meta_URL, },
    { libvlc_meta_Language, },
    { libvlc_meta_NowPlaying, },
    { libvlc_meta_Publisher, },
    { libvlc_meta_EncodedBy, },
    { libvlc_meta_ArtworkURL, },
    { libvlc_meta_TrackID, },
    { libvlc_meta_TrackTotal, },
    { libvlc_meta_Director, },
    { libvlc_meta_Season, },
    { libvlc_meta_Episode, },
    { libvlc_meta_ShowName, },
    { libvlc_meta_Actors, },
    { libvlc_meta_AlbumArtist, },
    { libvlc_meta_DiscNumber, },
    { libvlc_meta_DiscTotal, },
    { -1 }
  };

  for (unsigned int i = 0;
       -1 != map[i].vt;
       i++)
  {
    char *meta;

    fprintf (stderr,
             ".");
    meta = libvlc_media_get_meta (media,
                                  map[i].vt);
    if (NULL == meta)
      continue;
    fprintf (stderr,
             "Found %d: %s\n",
             map[i].vt,
             meta);
    free (meta);
  }
}


static void
media_ready (const struct libvlc_event_t *p_event,
             void *p_data)
{
  fprintf (stderr,
           "media status: %d, %d\n",
           p_event->type == libvlc_MediaParsedChanged,
           p_event->u.media_parsed_changed.new_status);
  if (p_event->u.media_parsed_changed.new_status ==
      libvlc_media_parsed_status_done)
  {
    fprintf (stderr,
             "media ready\n");
  }
}


static void
my_logger (void *data,
           int level,
           const libvlc_log_t *ctx,
           const char *fmt,
           va_list args)
{
  vfprintf (stderr,
            fmt,
            args);
  fprintf (stderr, "\n");
}


/**
 * Extract information using libvlc
 *
 * @param ec extraction context
 */
void
main(int argc,
     char **argv)
{
  libvlc_instance_t *vlc;
  libvlc_media_t *media;
  libvlc_event_manager_t *em;

  {
    sigset_t set;

    signal (SIGCHLD, SIG_DFL);
    sigemptyset (&set);
    sigaddset (&set, SIGPIPE);
    pthread_sigmask (SIG_BLOCK, &set, NULL);
  }

  {
    const char *argv[] = {
      "-v",
      "3",
      NULL
    };
    vlc = libvlc_new (2, argv);
  }
  if (NULL == vlc)
    return;
  libvlc_log_set (vlc,
                  &my_logger,
                  NULL);
  media = libvlc_media_new_path (vlc,
                                 argv[1]);
  if (NULL == media)
  {
    fprintf (stderr,
             "Opening path `%s' failed!\n",
             argv[1]);
    libvlc_release (vlc);
    return;
  }

  em = libvlc_media_event_manager (media);
  libvlc_event_attach (em,
                       libvlc_MediaParsedChanged,
                       &media_ready,
                       NULL);
  fprintf (stderr,
           "Triggering parser\n");
  {
    int status;

    status = libvlc_media_parse_with_options (media,
                                              libvlc_media_fetch_local
                                              | libvlc_media_parse_network
                                              | libvlc_media_fetch_network,
                                              30000); /* 30s timeout */
    fprintf (stderr,
             "Status: %d\n",
             status);
  }
  fprintf (stderr,
           "Sleeping\n");
  sleep (2);
  extract (NULL,
           media);
  libvlc_media_release (media);
  libvlc_release (vlc);
}


/* end of vlc_extractor.c */