]> git.sesse.net Git - vlc/blob - modules/access/imem.c
Removed duplicated call in imem.
[vlc] / modules / access / imem.c
1 /*****************************************************************************
2  * imem.c : Memory input for VLC
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir _AT_ videolan _DOT org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <limits.h>
32 #include <math.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
38
39 /*****************************************************************************
40  * Module descriptior
41  *****************************************************************************/
42 static int  Open (vlc_object_t *);
43 static void Close(vlc_object_t *);
44
45 #define CACHING_TEXT N_("Caching value in ms")
46 #define CACHING_LONGTEXT N_(\
47     "Caching value for imem streams. This " \
48     "value should be set in milliseconds.")
49
50 #define ID_TEXT N_("ID")
51 #define ID_LONGTEXT N_(\
52     "Set the ID of the elementary stream")
53
54 #define GROUP_TEXT N_("Group")
55 #define GROUP_LONGTEXT N_(\
56     "Set the group of the elementary stream")
57
58 #define CAT_TEXT N_("Category")
59 #define CAT_LONGTEXT N_(\
60     "Set the category of the elementary stream")
61 static const int cat_values[] = {
62     0, 1, 2, 3
63 };
64 static const char *cat_texts[] = {
65     N_("Unknown"), N_("Audio"), N_("Video"), N_("Subtitle")
66 };
67
68 #define CODEC_TEXT N_("Codec")
69 #define CODEC_LONGTEXT N_(\
70     "Set the codec of the elementary stream")
71
72 #define LANGUAGE_TEXT N_("Language")
73 #define LANGUAGE_LONGTEXT N_(\
74     "Language of the elementary stream as described by ISO639")
75
76 #define SAMPLERATE_TEXT N_("Sample rate")
77 #define SAMPLERATE_LONGTEXT N_(\
78     "Sample rate of an audio elementary stream")
79
80 #define CHANNELS_TEXT N_("Channels count")
81 #define CHANNELS_LONGTEXT N_(\
82     "Channels count of an audio elementary stream")
83
84 #define WIDTH_TEXT N_("Width")
85 #define WIDTH_LONGTEXT N_("Width of video or subtitle elementary streams")
86
87 #define HEIGHT_TEXT N_("Height")
88 #define HEIGHT_LONGTEXT N_("Height of video or subtitle elementary streams")
89
90 #define DAR_TEXT N_("Display aspect ratio")
91 #define DAR_LONGTEXT N_(\
92     "Display aspect ratio of a video elementary stream")
93
94 #define FPS_TEXT N_("Frame rate")
95 #define FPS_LONGTEXT N_(\
96     "Frame rate of a video elementary stream")
97
98 #define COOKIE_TEXT N_("Callback cookie string")
99 #define COOKIE_LONGTEXT N_(\
100     "Text identifier for the callback functions")
101
102 #define DATA_TEXT N_("Callback data")
103 #define DATA_LONGTEXT N_(\
104     "Data for the get and release functions")
105
106 #define GET_TEXT N_("Get function")
107 #define GET_LONGTEXT N_(\
108     "Address of the get callback function")
109
110 #define RELEASE_TEXT N_("Release function")
111 #define RELEASE_LONGTEXT N_(\
112     "Address of the release callback function")
113
114 vlc_module_begin()
115     set_shortname(N_("Memory input"))
116     set_description(N_("Memory input"))
117     set_category(CAT_INPUT)
118     set_subcategory(SUBCAT_INPUT_ACCESS)
119
120     add_integer("imem-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true)
121         change_private()
122     add_string ("imem-get", "0", NULL, GET_TEXT, GET_LONGTEXT, true)
123         change_volatile()
124     add_string ("imem-release", "0", NULL, RELEASE_TEXT, RELEASE_LONGTEXT, true)
125         change_volatile()
126     add_string ("imem-cookie", NULL, NULL, COOKIE_TEXT, COOKIE_LONGTEXT, true)
127         change_volatile()
128         change_safe()
129     add_string ("imem-data", "0", NULL, DATA_TEXT, DATA_LONGTEXT, true)
130         change_volatile()
131
132     add_integer("imem-id", -1, NULL, ID_TEXT, ID_LONGTEXT, true)
133         change_private()
134         change_safe()
135     add_integer("imem-group", 0, NULL, GROUP_TEXT, GROUP_LONGTEXT, true)
136         change_private()
137         change_safe()
138     add_integer("imem-cat", 0, NULL, CAT_TEXT, CAT_LONGTEXT, true)
139         change_integer_list(cat_values, cat_texts, NULL)
140         change_private()
141         change_safe()
142     add_string ("imem-codec", NULL, NULL, CODEC_TEXT, CODEC_LONGTEXT, true)
143         change_private()
144         change_safe()
145     add_string( "imem-language", NULL, NULL, LANGUAGE_TEXT, LANGUAGE_LONGTEXT, false)
146         change_private()
147         change_safe()
148
149     add_integer("imem-samplerate", 0, NULL, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, true)
150         change_private()
151         change_safe()
152     add_integer("imem-channels", 0, NULL, CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
153         change_private()
154         change_safe()
155
156     add_integer("imem-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true)
157         change_private()
158         change_safe()
159     add_integer("imem-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
160         change_private()
161         change_safe()
162     add_string ("imem-dar", NULL, NULL, DAR_TEXT, DAR_LONGTEXT, true)
163         change_private()
164         change_safe()
165     add_string ("imem-fps", NULL, NULL, FPS_TEXT, FPS_LONGTEXT, true)
166         change_private()
167         change_safe()
168
169     add_shortcut("imem")
170     set_capability("access_demux", 0)
171     set_callbacks(Open, Close)
172 vlc_module_end()
173
174 /*****************************************************************************
175  * Exported API
176  *****************************************************************************/
177
178 /* The clock origin for the DTS and PTS is assumed to be 0.
179  * A negative value means unknown.
180  *
181  * TODO define flags
182  */
183 typedef int  (*imem_get_t)(void *data, const char *cookie,
184                            int64_t *dts, int64_t *pts, unsigned *flags,
185                            size_t *, void **);
186 typedef void (*imem_release_t)(void *data, const char *cookie, size_t, void *);
187
188 /*****************************************************************************
189  * Local prototypes
190  *****************************************************************************/
191
192 /* */
193 static int Demux(demux_t *);
194 static int Control(demux_t *, int, va_list);
195
196 /* */
197 struct demux_sys_t {
198     struct {
199         imem_get_t      get;
200         imem_release_t  release;
201         void           *data;
202         char           *cookie;
203     } source;
204
205     es_out_id_t  *es;
206
207     mtime_t      pts_delay;
208
209     mtime_t      dts;
210
211     mtime_t      deadline;
212 };
213
214 static void ParseMRL(demux_t *);
215 static int var_CreateGetRational(demux_t *,
216                                  unsigned *num, unsigned *den,
217                                  const char *var);
218
219 /**
220  * It opens an imem access_demux
221  */
222 static int Open(vlc_object_t *object)
223 {
224     demux_t     *demux = (demux_t*)object;
225     char *tmp;
226
227         /* */
228     demux_sys_t *sys = calloc(1, sizeof(*sys));
229         if (!sys)
230                 return VLC_ENOMEM;
231
232     /* Read the user functions */
233     tmp = var_CreateGetString(demux, "imem-get");
234     if (tmp)
235         sys->source.get = (imem_get_t)(intptr_t)strtoll(tmp, NULL, 0);
236     free(tmp);
237
238     tmp = var_CreateGetString(demux, "imem-release");
239     if (tmp)
240         sys->source.release = (imem_release_t)(intptr_t)strtoll(tmp, NULL, 0);
241     free(tmp);
242
243     if (!sys->source.get || !sys->source.release) {
244         msg_Err(demux, "Invalid get/release function pointers");
245         free(sys);
246         return VLC_EGENERIC;
247     }
248
249     tmp = var_CreateGetString(demux, "imem-data");
250     if (tmp)
251         sys->source.data = (void *)(uintptr_t)strtoull(tmp, NULL, 0);
252     free(tmp);
253
254     /* Now we can parse the MRL (get/release must not be parsed to avoid
255      * security risks) */
256     if (*demux->psz_path)
257         ParseMRL(demux);
258
259     sys->source.cookie = var_InheritString(demux, "imem-cookie");
260
261     msg_Dbg(demux, "Using get(%p), release(%p), data(%p), cookie(%s)",
262             sys->source.get, sys->source.release, sys->source.data,
263             sys->source.cookie ? sys->source.cookie : "(null)");
264
265         /* ES format */
266     es_format_t fmt;
267         es_format_Init(&fmt, UNKNOWN_ES, 0);
268
269     fmt.i_id = var_CreateGetInteger(demux, "imem-id");
270     fmt.i_group = var_CreateGetInteger(demux, "imem-group");
271
272     tmp = var_CreateGetString(demux, "imem-codec");
273     if (tmp)
274         fmt.i_codec = vlc_fourcc_GetCodecFromString(UNKNOWN_ES, tmp);
275     free(tmp);
276
277     switch (var_CreateGetInteger(demux, "imem-cat")) {
278     case 1: {
279         fmt.i_cat = AUDIO_ES;
280         fmt.audio.i_channels = var_CreateGetInteger(demux, "imem-channels");
281         fmt.audio.i_rate = var_CreateGetInteger(demux, "imem-samplerate");
282
283         msg_Dbg(demux, "Audio %4.4s %d channels %d Hz",
284                 (const char *)&fmt.i_codec,
285                 fmt.audio.i_channels, fmt.audio.i_rate);
286         break;
287     }
288     case 2: {
289         fmt.i_cat = VIDEO_ES;
290         fmt.video.i_width  = var_CreateGetInteger(demux, "imem-width");
291         fmt.video.i_height = var_CreateGetInteger(demux, "imem-height");
292         unsigned num, den;
293         if (!var_CreateGetRational(demux, &num, &den, "imem-dar") && num > 0 && den > 0) {
294             if (fmt.video.i_width > 0 && fmt.video.i_height > 0) {
295                 fmt.video.i_sar_num = num * fmt.video.i_height;
296                 fmt.video.i_sar_den = den * fmt.video.i_width;
297             }
298         }
299         if (!var_CreateGetRational(demux, &num, &den, "imem-fps") && num > 0 && den > 0) {
300             fmt.video.i_frame_rate      = num;
301             fmt.video.i_frame_rate_base = den;
302         }
303
304         msg_Dbg(demux, "Video %4.4s %dx%d  SAR %d:%d frame rate %u/%u",
305                 (const char *)&fmt.i_codec,
306                 fmt.video.i_width, fmt.video.i_height,
307                 fmt.video.i_sar_num, fmt.video.i_sar_den,
308                 fmt.video.i_frame_rate, fmt.video.i_frame_rate_base);
309         break;
310     }
311     case 3: {
312         fmt.i_cat = SPU_ES;
313         fmt.subs.spu.i_original_frame_width =
314             var_CreateGetInteger(demux, "imem-width");
315         fmt.subs.spu.i_original_frame_height =
316             var_CreateGetInteger(demux, "imem-height");
317
318         msg_Dbg(demux, "Subtitle %4.4s",
319                 (const char *)&fmt.i_codec);
320         break;
321     }
322     default:
323         msg_Err(demux, "Invalid ES category");
324         es_format_Clean(&fmt);
325         free(sys);
326         return VLC_EGENERIC;
327     }
328
329     fmt.psz_language = var_CreateGetString(demux, "imem-language");
330
331     /* */
332         sys->es = es_out_Add(demux->out, &fmt);
333     es_format_Clean(&fmt);
334
335     if (!sys->es) {
336         free(sys->source.cookie);
337         free(sys);
338         return VLC_EGENERIC;
339     }
340
341     /* */
342     sys->pts_delay = var_CreateGetInteger(demux, "imem-caching") * INT64_C(1000);
343     sys->dts       = 0;
344     sys->deadline  = VLC_TS_INVALID;
345
346     /* Set up demux */
347     demux->pf_control = Control;
348     demux->pf_demux   = Demux;
349     demux->p_sys      = sys;
350
351     demux->info.i_update = 0;
352     demux->info.i_title = 0;
353     demux->info.i_seekpoint = 0;
354     return VLC_SUCCESS;
355 }
356
357 /**
358  * It closes an imem access_demux
359  */
360 static void Close(vlc_object_t *object)
361 {
362     demux_t     *demux = (demux_t *)object;
363     demux_sys_t *sys = demux->p_sys;
364
365     free(sys->source.cookie);
366     free(sys);
367 }
368
369 /**
370  * It controls imem
371  */
372 static int Control(demux_t *demux, int i_query, va_list args)
373 {
374     demux_sys_t *sys = demux->p_sys;
375
376     switch (i_query)
377     {
378     case DEMUX_CAN_PAUSE:
379     case DEMUX_CAN_CONTROL_PACE: {
380         bool *b = va_arg(args, bool *);
381         *b = true;
382         return VLC_SUCCESS;
383     }
384     case DEMUX_SET_PAUSE_STATE:
385         return VLC_SUCCESS;
386
387     case DEMUX_GET_PTS_DELAY: {
388         int64_t *delay = va_arg(args, int64_t *);
389         *delay = sys->pts_delay;
390         return VLC_SUCCESS;
391     }
392     case DEMUX_GET_POSITION: {
393         double *position = va_arg(args, double *);
394         *position = 0.0;
395         return VLC_SUCCESS;
396     }
397     case DEMUX_GET_TIME: {
398         int64_t *t = va_arg(args, int64_t *);
399         *t = sys->dts;
400         return VLC_SUCCESS;
401     }
402     case DEMUX_GET_LENGTH: {
403         int64_t *l = va_arg(args, int64_t *);
404         *l = 0;
405         return VLC_SUCCESS;
406     }
407     case DEMUX_SET_NEXT_DEMUX_TIME:
408         sys->deadline = va_arg(args, int64_t);
409         return VLC_SUCCESS;
410
411     /* */
412     case DEMUX_CAN_SEEK:
413     case DEMUX_SET_POSITION:
414     case DEMUX_SET_TIME:
415     default:
416         return VLC_EGENERIC;
417     }
418
419     return VLC_EGENERIC;
420 }
421
422 /**
423  * It retreives data using the get() callback, sends them to es_out
424  * and the release it using the release() callback.
425  */
426 static int Demux(demux_t *demux)
427 {
428     demux_sys_t *sys = demux->p_sys;
429
430     if (sys->deadline == VLC_TS_INVALID)
431         sys->deadline = sys->dts + 1;
432
433     for (;;) {
434         if (sys->deadline <= sys->dts)
435             break;
436
437         /* */
438         int64_t dts, pts;
439         unsigned flags;
440         size_t buffer_size;
441         void   *buffer;
442
443         if (sys->source.get(sys->source.data, sys->source.cookie,
444                             &dts, &pts, &flags, &buffer_size, &buffer))
445             return 0;
446
447         if (dts < 0)
448             dts = pts;
449
450         if (buffer_size > 0) {
451             block_t *block = block_New(demux, buffer_size);
452             if (block) {
453                 block->i_dts = dts >= 0 ? (1 + dts) : VLC_TS_INVALID;
454                 block->i_pts = pts >= 0 ? (1 + pts) : VLC_TS_INVALID;
455                 memcpy(block->p_buffer, buffer, buffer_size);
456
457                 es_out_Control(demux->out, ES_OUT_SET_PCR, block->i_dts);
458                 es_out_Send(demux->out, sys->es, block);
459             }
460         }
461
462         sys->dts = dts;
463
464         sys->source.release(sys->source.data, sys->source.cookie,
465                             buffer_size, buffer);
466     }
467     sys->deadline = VLC_TS_INVALID;
468     return 1;
469 }
470
471 /**
472  * It parses a rational number (it also accepts basic float number).
473  *
474  * It returns an error if the rational number cannot be parsed (0/0 is valid).
475  */
476 static int var_CreateGetRational(demux_t *demux,
477                                  unsigned *num, unsigned *den,
478                                  const char *var)
479 {
480     /* */
481     *num = 0;
482     *den = 0;
483
484     /* */
485     char *tmp = var_CreateGetString(demux, var);
486     if (!tmp)
487         goto error;
488
489     char *next;
490     unsigned n = strtol(tmp,  &next, 0);
491     unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
492
493     if (*next == '.') {
494         /* Interpret as a float number */
495         double r = us_atof(tmp);
496         double c = ceil(r);
497         if (c >= UINT_MAX)
498             goto error;
499         unsigned m = c;
500         if (m > 0) {
501             d = UINT_MAX / m;
502             n = r * d;
503         } else {
504             n = 0;
505             d = 0;
506         }
507     }
508
509     if (n > 0 && d > 0)
510         vlc_ureduce(num, den, n, d, 0);
511
512     free(tmp);
513     return VLC_SUCCESS;
514
515 error:
516     free(tmp);
517     return VLC_EGENERIC;
518 }
519
520 /**
521  * Parse the MRL and extract configuration from it.
522  *
523  * Syntax: option1=value1[:option2=value2[...]]
524  *
525  * XXX get and release are not supported on purpose.
526  */
527 static void ParseMRL(demux_t *demux)
528 {
529     static const struct {
530         const char *name;
531         int        type;
532     } options[] = {
533         { "caching",    VLC_VAR_INTEGER },
534         { "id",         VLC_VAR_INTEGER },
535         { "group",      VLC_VAR_INTEGER },
536         { "cat",        VLC_VAR_INTEGER },
537         { "samplerate", VLC_VAR_INTEGER },
538         { "channels",   VLC_VAR_INTEGER },
539         { "width",      VLC_VAR_INTEGER },
540         { "height",     VLC_VAR_INTEGER },
541         { "cookie",     VLC_VAR_STRING },
542         { "codec",      VLC_VAR_STRING },
543         { "language",   VLC_VAR_STRING },
544         { "dar",        VLC_VAR_STRING },
545         { "fps",        VLC_VAR_STRING },
546         { NULL, -1 }
547     };
548
549     char *dup = strdup(demux->psz_path);
550     if (!dup)
551         return;
552     char *current = dup;
553
554     while (current) {
555         char *next = strchr(current, ':');
556         if (next)
557             *next++ = '\0';
558
559         char *option = current;
560         char *value = strchr(current, '=');
561         if (value) {
562             *value++ = '\0';
563             msg_Dbg(demux, "option '%s' value '%s'", option, value);
564         } else {
565             msg_Dbg(demux, "option '%s' without value (unsupported)", option);
566         }
567
568         char *name;
569         if (asprintf(&name, "imem-%s", option) < 0)
570             name = NULL;
571         for (unsigned i = 0; name && options[i].name; i++) {
572             if (strcmp(options[i].name, option))
573                 continue;
574             /* */
575             var_Create(demux, name, options[i].type | VLC_VAR_DOINHERIT);
576             if (options[i].type == VLC_VAR_INTEGER && value) {
577                 var_SetInteger(demux, name, strtol(value, NULL, 0));
578             } else if (options[i].type == VLC_VAR_STRING && value) {
579                 var_SetString(demux, name, value);
580             }
581             break;
582         }
583         free(name);
584
585         /* */
586         current = next;
587     }
588     free(dup);
589 }