aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/vlc_extractor.c
blob: e90b3ea4365043a4b6d0d4505160fbf414f79e92 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
     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 "platform.h"
#include "extractor.h"
#include <vlc/vlc.h>
#include <signal.h>

/**
 * Function to help VLC open a custom bitstream input media.
 *
 * The same media item can be opened multiple times. Each time, this callback
 * is invoked. It should allocate and initialize any instance-specific
 * resources, then store them in *datap. The instance resources can be freed
 * in the @ref libvlc_media_close_cb callback.
 *
 * @param opaque our `struct EXTRACTOR_ExtractContext`
 * @param[out] datap storage space for a private data pointer
 * @param[out] sizep byte length of the bitstream or UINT64_MAX if unknown
 *
 * @note For convenience, *datap is initially NULL and *sizep is initially 0.
 *
 * @return 0 on success, non-zero on error. In case of failure, the other
 * callbacks will not be invoked and any value stored in *datap and *sizep is
 * discarded.
 */
static int
open_cb (void *opaque,
         void **datap,
         uint64_t *sizep)
{
  struct EXTRACTOR_ExtractContext *ec = opaque;

  *datap = ec;
  *sizep = ec->get_size (ec->cls);
  if (UINT64_MAX == *sizep)
  {
    fprintf (stderr,
             "Open failed!\n");
    return 1;
  }
  fprintf (stderr,
           "Open returns %llu file size!\n",
           (unsigned long long) *sizep);
  return 0;
}


/**
 * Function to help VLC read data from a custom bitstream input media.
 *
 * @param opaque our `struct EXTRACTOR_ExtractContext`
 * @param buf start address of the buffer to read data into
 * @param len bytes length of the buffer
 * @return strictly positive number of bytes read, 0 on end-of-stream,
 *         or -1 on non-recoverable error
 *
 * @note If no data is immediately available, then the callback should sleep.
 * @warning The application is responsible for avoiding deadlock situations.
 * In particular, the callback should return an error if playback is stopped;
 * if it does not return, then libvlc_media_player_stop() will never return.
 */
static ssize_t
read_cb (void *opaque,
         unsigned char *buf,
         size_t len)
{
  struct EXTRACTOR_ExtractContext *ec = opaque;
  void *data;
  ssize_t ret;

  ret = ec->read (ec->cls,
                  &data,
                  len);
  if (-1 == ret)
  {
    fprintf (stderr,
             "Read failed!\n");
    return -1;
  }
  memcpy (buf,
          data,
          ret);
  fprintf (stderr,
           "Read %u bytes!\n",
           (unsigned int) ret);
  return ret;
}


/**
 * Allow VLC to seek a custom bitstream input media.
 *
 * @param opaque our `struct EXTRACTOR_ExtractContext`
 * @param offset absolute byte offset to seek to
 * @return 0 on success, -1 on error.
 */
static int
seek_cb (void *opaque,
         uint64_t offset)
{
  struct EXTRACTOR_ExtractContext *ec = opaque;

  fprintf (stderr,
           "Seek to %llu!\n",
           (unsigned long long) offset);
  if (offset > INT64_MAX)
  {
    fprintf (stderr,
             "Excessive seek, impossible with LE!\n");
    return -1;
  }
  if (-1 ==
      ec->seek (ec->cls,
                offset,
                SEEK_SET))
  {
    fprintf (stderr,
             "Seek failed!\n");
    return -1;
  }
  return 0;
}


/**
 * Callback prototype to close a custom bitstream input media.
 *
 * @param opaque our `struct EXTRACTOR_ExtractContext`
 */
static void
close_cb (void *opaque)
{
  /* intentionally empty */
  fprintf (stderr,
           "Close called\n");
}


static void
extract (struct EXTRACTOR_ExtractContext *ec,
         libvlc_media_t *media)
{
  struct
  {
    enum libvlc_meta_t vt;
    enum EXTRACTOR_MetaType mt;
  } map[] = {
    { libvlc_meta_Title,
      EXTRACTOR_METATYPE_TITLE },
    { libvlc_meta_Artist,
      EXTRACTOR_METATYPE_ARTIST },
    { libvlc_meta_Genre,
      EXTRACTOR_METATYPE_GENRE },
    { libvlc_meta_Copyright,
      EXTRACTOR_METATYPE_COPYRIGHT },
    { libvlc_meta_Album,
      EXTRACTOR_METATYPE_ALBUM },
    { libvlc_meta_TrackNumber,
      EXTRACTOR_METATYPE_TRACK_NUMBER },
    { libvlc_meta_Description,
      EXTRACTOR_METATYPE_DESCRIPTION },
    { libvlc_meta_Rating,
      EXTRACTOR_METATYPE_RATING },
    { libvlc_meta_Date,
      EXTRACTOR_METATYPE_CREATION_TIME },
    { libvlc_meta_Setting,
      EXTRACTOR_METATYPE_UNKNOWN },
    { libvlc_meta_URL,
      EXTRACTOR_METATYPE_URL },
    { libvlc_meta_Language,
      EXTRACTOR_METATYPE_LANGUAGE },
    { libvlc_meta_NowPlaying,
      EXTRACTOR_METATYPE_UNKNOWN },
    { libvlc_meta_Publisher,
      EXTRACTOR_METATYPE_PUBLISHER },
    { libvlc_meta_EncodedBy,
      EXTRACTOR_METATYPE_ENCODED_BY },
    { libvlc_meta_ArtworkURL,
      EXTRACTOR_METATYPE_URL },
    { libvlc_meta_TrackID,
      EXTRACTOR_METATYPE_TRACK_NUMBER },
    { libvlc_meta_TrackTotal,
      EXTRACTOR_METATYPE_UNKNOWN },
    { libvlc_meta_Director,
      EXTRACTOR_METATYPE_MOVIE_DIRECTOR },
    { libvlc_meta_Season,
      EXTRACTOR_METATYPE_SHOW_SEASON_NUMBER },
    { libvlc_meta_Episode,
      EXTRACTOR_METATYPE_SHOW_EPISODE_NUMBER },
    { libvlc_meta_ShowName,
      EXTRACTOR_METATYPE_SHOW_NAME },
    { libvlc_meta_Actors,
      EXTRACTOR_METATYPE_PERFORMER },
    { libvlc_meta_AlbumArtist,
      EXTRACTOR_METATYPE_ARTIST },
    { libvlc_meta_DiscNumber,
      EXTRACTOR_METATYPE_DISC_NUMBER },
    { libvlc_meta_DiscTotal,
      EXTRACTOR_METATYPE_UNKNOWN },
    { 0, 0 }
  };

  for (unsigned int i = 0;
       EXTRACTOR_METATYPE_RESERVED != map[i].mt;
       i++)
  {
    char *meta;

    fprintf (stderr,
             ".");
    meta = libvlc_media_get_meta (media,
                                  map[i].vt);
    if (NULL == meta)
      continue;
    ec->proc (ec->cls,
              "vlc",
              map[i].mt,
              EXTRACTOR_METAFORMAT_UTF8, /* ??? */
              "text/plain",
              meta,
              strlen (meta) + 1);
    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");
  }
}


/**
 * Extract information using libvlc
 *
 * @param ec extraction context
 */
void
EXTRACTOR_vlc_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  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);
  }

  vlc = libvlc_new (0, NULL);
  if (NULL == vlc)
    return;
  media = libvlc_media_new_callbacks (vlc,
                                      &open_cb,
                                      &read_cb,
                                      &seek_cb,
                                      &close_cb,
                                      ec);
  if (NULL == media)
  {
    libvlc_release (vlc);
    return;
  }

  em = libvlc_media_event_manager (media);
  libvlc_event_attach (em,
                       libvlc_MediaParsedChanged,
                       &media_ready,
                       ec);
  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 (1);
  extract (ec,
           media);
  libvlc_media_release (media);
  libvlc_release (vlc);
}


/* end of vlc_extractor.c */