]> git.sesse.net Git - vlc/blob - modules/access/imem.c
962683177ff24fc2c3c8f0dace1b9fb9ca308fa0
[vlc] / modules / access / imem.c
1 /*****************************************************************************
2  * imem.c : Memory input for VLC
3  *****************************************************************************
4  * Copyright (C) 2009-2010 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_access.h>
37 #include <vlc_demux.h>
38 #include <vlc_charset.h>
39
40 /*****************************************************************************
41  * Module descriptior
42  *****************************************************************************/
43 static int  OpenAccess (vlc_object_t *);
44 static void CloseAccess(vlc_object_t *);
45
46 static int  OpenDemux (vlc_object_t *);
47 static void CloseDemux(vlc_object_t *);
48
49 #define CACHING_TEXT N_("Caching value in ms")
50 #define CACHING_LONGTEXT N_(\
51     "Caching value for imem streams. This " \
52     "value should be set in milliseconds.")
53
54 #define ID_TEXT N_("ID")
55 #define ID_LONGTEXT N_(\
56     "Set the ID of the elementary stream")
57
58 #define GROUP_TEXT N_("Group")
59 #define GROUP_LONGTEXT N_(\
60     "Set the group of the elementary stream")
61
62 #define CAT_TEXT N_("Category")
63 #define CAT_LONGTEXT N_(\
64     "Set the category of the elementary stream")
65 static const int cat_values[] = {
66     0, 1, 2, 3, 4,
67 };
68 static const char *cat_texts[] = {
69     N_("Unknown"), N_("Audio"), N_("Video"), N_("Subtitle"), N_("Data")
70 };
71
72 #define CODEC_TEXT N_("Codec")
73 #define CODEC_LONGTEXT N_(\
74     "Set the codec of the elementary stream")
75
76 #define LANGUAGE_TEXT N_("Language")
77 #define LANGUAGE_LONGTEXT N_(\
78     "Language of the elementary stream as described by ISO639")
79
80 #define SAMPLERATE_TEXT N_("Sample rate")
81 #define SAMPLERATE_LONGTEXT N_(\
82     "Sample rate of an audio elementary stream")
83
84 #define CHANNELS_TEXT N_("Channels count")
85 #define CHANNELS_LONGTEXT N_(\
86     "Channels count of an audio elementary stream")
87
88 #define WIDTH_TEXT N_("Width")
89 #define WIDTH_LONGTEXT N_("Width of video or subtitle elementary streams")
90
91 #define HEIGHT_TEXT N_("Height")
92 #define HEIGHT_LONGTEXT N_("Height of video or subtitle elementary streams")
93
94 #define DAR_TEXT N_("Display aspect ratio")
95 #define DAR_LONGTEXT N_(\
96     "Display aspect ratio of a video elementary stream")
97
98 #define FPS_TEXT N_("Frame rate")
99 #define FPS_LONGTEXT N_(\
100     "Frame rate of a video elementary stream")
101
102 #define COOKIE_TEXT N_("Callback cookie string")
103 #define COOKIE_LONGTEXT N_(\
104     "Text identifier for the callback functions")
105
106 #define DATA_TEXT N_("Callback data")
107 #define DATA_LONGTEXT N_(\
108     "Data for the get and release functions")
109
110 #define GET_TEXT N_("Get function")
111 #define GET_LONGTEXT N_(\
112     "Address of the get callback function")
113
114 #define RELEASE_TEXT N_("Release function")
115 #define RELEASE_LONGTEXT N_(\
116     "Address of the release callback function")
117
118 vlc_module_begin()
119     set_shortname(N_("Memory input"))
120     set_description(N_("Memory input"))
121     set_category(CAT_INPUT)
122     set_subcategory(SUBCAT_INPUT_ACCESS)
123
124     add_integer("imem-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true)
125         change_private()
126     add_string ("imem-get", "0", NULL, GET_TEXT, GET_LONGTEXT, true)
127         change_volatile()
128     add_string ("imem-release", "0", NULL, RELEASE_TEXT, RELEASE_LONGTEXT, true)
129         change_volatile()
130     add_string ("imem-cookie", NULL, NULL, COOKIE_TEXT, COOKIE_LONGTEXT, true)
131         change_volatile()
132         change_safe()
133     add_string ("imem-data", "0", NULL, DATA_TEXT, DATA_LONGTEXT, true)
134         change_volatile()
135
136     add_integer("imem-id", -1, NULL, ID_TEXT, ID_LONGTEXT, true)
137         change_private()
138         change_safe()
139     add_integer("imem-group", 0, NULL, GROUP_TEXT, GROUP_LONGTEXT, true)
140         change_private()
141         change_safe()
142     add_integer("imem-cat", 0, NULL, CAT_TEXT, CAT_LONGTEXT, true)
143         change_integer_list(cat_values, cat_texts, NULL)
144         change_private()
145         change_safe()
146     add_string ("imem-codec", NULL, NULL, CODEC_TEXT, CODEC_LONGTEXT, true)
147         change_private()
148         change_safe()
149     add_string( "imem-language", NULL, NULL, LANGUAGE_TEXT, LANGUAGE_LONGTEXT, false)
150         change_private()
151         change_safe()
152
153     add_integer("imem-samplerate", 0, NULL, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, true)
154         change_private()
155         change_safe()
156     add_integer("imem-channels", 0, NULL, CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
157         change_private()
158         change_safe()
159
160     add_integer("imem-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true)
161         change_private()
162         change_safe()
163     add_integer("imem-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
164         change_private()
165         change_safe()
166     add_string ("imem-dar", NULL, NULL, DAR_TEXT, DAR_LONGTEXT, true)
167         change_private()
168         change_safe()
169     add_string ("imem-fps", NULL, NULL, FPS_TEXT, FPS_LONGTEXT, true)
170         change_private()
171         change_safe()
172
173     add_shortcut("imem")
174     set_capability("access_demux", 0)
175     set_callbacks(OpenDemux, CloseDemux)
176
177     add_submodule()
178         add_shortcut("imem")
179         set_capability("access", 0)
180         set_callbacks(OpenAccess, CloseAccess)
181 vlc_module_end()
182
183 /*****************************************************************************
184  * Exported API
185  *****************************************************************************/
186
187 /* The clock origin for the DTS and PTS is assumed to be 0.
188  * A negative value means unknown.
189  *
190  * TODO define flags
191  */
192 typedef int  (*imem_get_t)(void *data, const char *cookie,
193                            int64_t *dts, int64_t *pts, unsigned *flags,
194                            size_t *, void **);
195 typedef void (*imem_release_t)(void *data, const char *cookie, size_t, void *);
196
197 /*****************************************************************************
198  * Local prototypes
199  *****************************************************************************/
200
201 /* */
202 static block_t *Block(access_t *);
203 static int ControlAccess(access_t *, int, va_list);
204
205 static int Demux(demux_t *);
206 static int ControlDemux(demux_t *, int, va_list);
207
208 /* */
209 typedef struct {
210     struct {
211         imem_get_t      get;
212         imem_release_t  release;
213         void           *data;
214         char           *cookie;
215     } source;
216
217     es_out_id_t  *es;
218
219     mtime_t      pts_delay;
220
221     mtime_t      dts;
222
223     mtime_t      deadline;
224 } imem_sys_t;
225
226 static void ParseMRL(vlc_object_t *, const char *);
227
228 /**
229  * It closes the common part of the access and access_demux
230  */
231 static void CloseCommon(imem_sys_t *sys)
232 {
233     free(sys->source.cookie);
234     free(sys);
235 }
236
237 /**
238  * It initializes the common part for imem access/access_demux.
239  */
240 static int OpenCommon(vlc_object_t *object, imem_sys_t **sys_ptr, const char *psz_path)
241 {
242     char *tmp;
243
244     /* */
245     imem_sys_t *sys = calloc(1, sizeof(*sys));
246     if (!sys)
247         return VLC_ENOMEM;
248
249     /* Read the user functions */
250     tmp = var_InheritString(object, "imem-get");
251     if (tmp)
252         sys->source.get = (imem_get_t)(intptr_t)strtoll(tmp, NULL, 0);
253     free(tmp);
254
255     tmp = var_InheritString(object, "imem-release");
256     if (tmp)
257         sys->source.release = (imem_release_t)(intptr_t)strtoll(tmp, NULL, 0);
258     free(tmp);
259
260     if (!sys->source.get || !sys->source.release) {
261         msg_Err(object, "Invalid get/release function pointers");
262         free(sys);
263         return VLC_EGENERIC;
264     }
265
266     tmp = var_InheritString(object, "imem-data");
267     if (tmp)
268         sys->source.data = (void *)(uintptr_t)strtoull(tmp, NULL, 0);
269     free(tmp);
270
271     /* Now we can parse the MRL (get/release must not be parsed to avoid
272      * security risks) */
273     if (*psz_path)
274         ParseMRL(object, psz_path);
275
276     sys->source.cookie = var_InheritString(object, "imem-cookie");
277
278     msg_Dbg(object, "Using get(%p), release(%p), data(%p), cookie(%s)",
279             sys->source.get, sys->source.release, sys->source.data,
280             sys->source.cookie ? sys->source.cookie : "(null)");
281
282     /* */
283     sys->pts_delay = var_InheritInteger(object, "imem-caching") * INT64_C(1000);
284     sys->dts       = 0;
285     sys->deadline  = VLC_TS_INVALID;
286
287     *sys_ptr = sys;
288     return VLC_SUCCESS;
289 }
290
291 /**
292  * It opens an imem access.
293  */
294 static int OpenAccess(vlc_object_t *object)
295 {
296     access_t   *access = (access_t *)object;
297     imem_sys_t *sys;
298
299     if (OpenCommon(object, &sys, access->psz_location))
300         return VLC_EGENERIC;
301
302     if (var_InheritInteger(object, "imem-cat") != 4) {
303         CloseCommon(sys);
304         return VLC_EGENERIC;
305     }
306
307     /* */
308     access_InitFields(access);
309     access->pf_control = ControlAccess;
310     access->pf_read    = NULL;
311     access->pf_block   = Block;
312     access->pf_seek    = NULL;
313     access->p_sys      = (access_sys_t*)sys;
314
315     return VLC_SUCCESS;
316 }
317
318 /**
319  * It closes an imem access
320  */
321 static void CloseAccess(vlc_object_t *object)
322 {
323     access_t *access = (access_t *)object;
324
325     CloseCommon((imem_sys_t*)access->p_sys);
326 }
327
328 /**
329  * It controls an imem access
330  */
331 static int ControlAccess(access_t *access, int i_query, va_list args)
332 {
333     imem_sys_t *sys = (imem_sys_t*)access->p_sys;
334
335     switch (i_query)
336     {
337     case ACCESS_CAN_SEEK:
338     case ACCESS_CAN_FASTSEEK: {
339         bool *b = va_arg( args, bool* );
340         *b = false;
341         return VLC_SUCCESS;
342     }
343     case ACCESS_CAN_PAUSE:
344     case ACCESS_CAN_CONTROL_PACE: {
345         bool *b = va_arg( args, bool* );
346         *b = true;
347         return VLC_SUCCESS;
348     }
349     case ACCESS_GET_PTS_DELAY: {
350         int64_t *delay = va_arg(args, int64_t *);
351         *delay = sys->pts_delay;
352         return VLC_SUCCESS;
353     }
354     case ACCESS_SET_PAUSE_STATE:
355         return VLC_SUCCESS;
356
357     case ACCESS_GET_TITLE_INFO:
358     case ACCESS_SET_TITLE:
359     case ACCESS_SET_SEEKPOINT:
360     case ACCESS_SET_PRIVATE_ID_STATE:
361     case ACCESS_GET_META:
362     case ACCESS_GET_PRIVATE_ID_STATE:
363     case ACCESS_GET_CONTENT_TYPE:
364     default:
365         return VLC_EGENERIC;
366     }
367 }
368
369 /**
370  * It retreives data using the get() callback, copies them,
371  * and then release them using the release() callback.
372  */
373 static block_t *Block(access_t *access)
374 {
375     imem_sys_t *sys = (imem_sys_t*)access->p_sys;
376
377     unsigned flags;
378     size_t buffer_size;
379     void   *buffer;
380
381     if (sys->source.get(sys->source.data, sys->source.cookie,
382                         NULL, NULL, &flags, &buffer_size, &buffer)) {
383         access->info.b_eof = true;
384         return NULL;
385     }
386
387     block_t *block = NULL;
388     if (buffer_size > 0) {
389         block = block_New(access, buffer_size);
390         if (block)
391             memcpy(block->p_buffer, buffer, buffer_size);
392     }
393
394     sys->source.release(sys->source.data, sys->source.cookie,
395                         buffer_size, buffer);
396     return block;
397 }
398
399 /**
400  * It opens an imem access_demux.
401  */
402 static int OpenDemux(vlc_object_t *object)
403 {
404     demux_t    *demux = (demux_t *)object;
405     imem_sys_t *sys;
406
407     if (OpenCommon(object, &sys, demux->psz_location))
408         return VLC_EGENERIC;
409
410     /* ES format */
411     es_format_t fmt;
412     es_format_Init(&fmt, UNKNOWN_ES, 0);
413
414     fmt.i_id = var_InheritInteger(object, "imem-id");
415     fmt.i_group = var_InheritInteger(object, "imem-group");
416
417     char *tmp = var_InheritString(object, "imem-codec");
418     if (tmp)
419         fmt.i_codec = vlc_fourcc_GetCodecFromString(UNKNOWN_ES, tmp);
420     free(tmp);
421
422     const int cat = var_InheritInteger(object, "imem-cat");
423     switch (cat) {
424     case 1: {
425         fmt.i_cat = AUDIO_ES;
426         fmt.audio.i_channels = var_InheritInteger(object, "imem-channels");
427         fmt.audio.i_rate = var_InheritInteger(object, "imem-samplerate");
428
429         msg_Dbg(object, "Audio %4.4s %d channels %d Hz",
430                 (const char *)&fmt.i_codec,
431                 fmt.audio.i_channels, fmt.audio.i_rate);
432         break;
433     }
434     case 2: {
435         fmt.i_cat = VIDEO_ES;
436         fmt.video.i_width  = var_InheritInteger(object, "imem-width");
437         fmt.video.i_height = var_InheritInteger(object, "imem-height");
438         unsigned num, den;
439         if (!var_InheritURational(object, &num, &den, "imem-dar") && num > 0 && den > 0) {
440             if (fmt.video.i_width > 0 && fmt.video.i_height > 0) {
441                 fmt.video.i_sar_num = num * fmt.video.i_height;
442                 fmt.video.i_sar_den = den * fmt.video.i_width;
443             }
444         }
445         if (!var_InheritURational(object, &num, &den, "imem-fps") && num > 0 && den > 0) {
446             fmt.video.i_frame_rate      = num;
447             fmt.video.i_frame_rate_base = den;
448         }
449
450         msg_Dbg(object, "Video %4.4s %dx%d  SAR %d:%d frame rate %u/%u",
451                 (const char *)&fmt.i_codec,
452                 fmt.video.i_width, fmt.video.i_height,
453                 fmt.video.i_sar_num, fmt.video.i_sar_den,
454                 fmt.video.i_frame_rate, fmt.video.i_frame_rate_base);
455         break;
456     }
457     case 3: {
458         fmt.i_cat = SPU_ES;
459         fmt.subs.spu.i_original_frame_width =
460             var_InheritInteger(object, "imem-width");
461         fmt.subs.spu.i_original_frame_height =
462             var_InheritInteger(object, "imem-height");
463
464         msg_Dbg(object, "Subtitle %4.4s",
465                 (const char *)&fmt.i_codec);
466         break;
467     }
468     default:
469         if (cat != 4)
470             msg_Err(object, "Invalid ES category");
471         es_format_Clean(&fmt);
472         CloseCommon(sys);
473         return VLC_EGENERIC;
474     }
475
476     fmt.psz_language = var_InheritString(object, "imem-language");
477
478     sys->es = es_out_Add(demux->out, &fmt);
479     es_format_Clean(&fmt);
480
481     if (!sys->es) {
482         CloseCommon(sys);
483         return VLC_EGENERIC;
484     }
485
486     /* */
487     demux->pf_control = ControlDemux;
488     demux->pf_demux   = Demux;
489     demux->p_sys      = (demux_sys_t*)sys;
490
491     demux->info.i_update = 0;
492     demux->info.i_title = 0;
493     demux->info.i_seekpoint = 0;
494     return VLC_SUCCESS;
495 }
496
497 /**
498  * It closes an imem access_demux
499  */
500 static void CloseDemux(vlc_object_t *object)
501 {
502     demux_t *demux = (demux_t *)object;
503
504     CloseCommon((imem_sys_t*)demux->p_sys);
505 }
506
507 /**
508  * It controls an imem access_demux
509  */
510 static int ControlDemux(demux_t *demux, int i_query, va_list args)
511 {
512     imem_sys_t *sys = (imem_sys_t*)demux->p_sys;
513
514     switch (i_query)
515     {
516     case DEMUX_CAN_PAUSE:
517     case DEMUX_CAN_CONTROL_PACE: {
518         bool *b = va_arg(args, bool *);
519         *b = true;
520         return VLC_SUCCESS;
521     }
522     case DEMUX_SET_PAUSE_STATE:
523         return VLC_SUCCESS;
524
525     case DEMUX_GET_PTS_DELAY: {
526         int64_t *delay = va_arg(args, int64_t *);
527         *delay = sys->pts_delay;
528         return VLC_SUCCESS;
529     }
530     case DEMUX_GET_POSITION: {
531         double *position = va_arg(args, double *);
532         *position = 0.0;
533         return VLC_SUCCESS;
534     }
535     case DEMUX_GET_TIME: {
536         int64_t *t = va_arg(args, int64_t *);
537         *t = sys->dts;
538         return VLC_SUCCESS;
539     }
540     case DEMUX_GET_LENGTH: {
541         int64_t *l = va_arg(args, int64_t *);
542         *l = 0;
543         return VLC_SUCCESS;
544     }
545     case DEMUX_SET_NEXT_DEMUX_TIME:
546         sys->deadline = va_arg(args, int64_t);
547         return VLC_SUCCESS;
548
549     /* */
550     case DEMUX_CAN_SEEK:
551     case DEMUX_SET_POSITION:
552     case DEMUX_SET_TIME:
553     default:
554         return VLC_EGENERIC;
555     }
556
557     return VLC_EGENERIC;
558 }
559
560 /**
561  * It retreives data using the get() callback, sends them to es_out
562  * and the release it using the release() callback.
563  */
564 static int Demux(demux_t *demux)
565 {
566     imem_sys_t *sys = (imem_sys_t*)demux->p_sys;
567
568     if (sys->deadline == VLC_TS_INVALID)
569         sys->deadline = sys->dts + 1;
570
571     for (;;) {
572         if (sys->deadline <= sys->dts)
573             break;
574
575         /* */
576         int64_t dts, pts;
577         unsigned flags;
578         size_t buffer_size;
579         void   *buffer;
580
581         if (sys->source.get(sys->source.data, sys->source.cookie,
582                             &dts, &pts, &flags, &buffer_size, &buffer))
583             return 0;
584
585         if (dts < 0)
586             dts = pts;
587
588         if (buffer_size > 0) {
589             block_t *block = block_New(demux, buffer_size);
590             if (block) {
591                 block->i_dts = dts >= 0 ? (1 + dts) : VLC_TS_INVALID;
592                 block->i_pts = pts >= 0 ? (1 + pts) : VLC_TS_INVALID;
593                 memcpy(block->p_buffer, buffer, buffer_size);
594
595                 es_out_Control(demux->out, ES_OUT_SET_PCR, block->i_dts);
596                 es_out_Send(demux->out, sys->es, block);
597             }
598         }
599
600         sys->dts = dts;
601
602         sys->source.release(sys->source.data, sys->source.cookie,
603                             buffer_size, buffer);
604     }
605     sys->deadline = VLC_TS_INVALID;
606     return 1;
607 }
608
609 /**
610  * Parse the MRL and extract configuration from it.
611  *
612  * Syntax: option1=value1[:option2=value2[...]]
613  *
614  * XXX get and release are not supported on purpose.
615  */
616 static void ParseMRL(vlc_object_t *object, const char *psz_path)
617 {
618     static const struct {
619         const char *name;
620         int        type;
621     } options[] = {
622         { "caching",    VLC_VAR_INTEGER },
623         { "id",         VLC_VAR_INTEGER },
624         { "group",      VLC_VAR_INTEGER },
625         { "cat",        VLC_VAR_INTEGER },
626         { "samplerate", VLC_VAR_INTEGER },
627         { "channels",   VLC_VAR_INTEGER },
628         { "width",      VLC_VAR_INTEGER },
629         { "height",     VLC_VAR_INTEGER },
630         { "cookie",     VLC_VAR_STRING },
631         { "codec",      VLC_VAR_STRING },
632         { "language",   VLC_VAR_STRING },
633         { "dar",        VLC_VAR_STRING },
634         { "fps",        VLC_VAR_STRING },
635         { NULL, -1 }
636     };
637
638     char *dup = strdup(psz_path);
639     if (!dup)
640         return;
641     char *current = dup;
642
643     while (current) {
644         char *next = strchr(current, ':');
645         if (next)
646             *next++ = '\0';
647
648         char *option = current;
649         char *value = strchr(current, '=');
650         if (value) {
651             *value++ = '\0';
652             msg_Dbg(object, "option '%s' value '%s'", option, value);
653         } else {
654             msg_Dbg(object, "option '%s' without value (unsupported)", option);
655         }
656
657         char *name;
658         if (asprintf(&name, "imem-%s", option) < 0)
659             name = NULL;
660         for (unsigned i = 0; name && options[i].name; i++) {
661             if (strcmp(options[i].name, option))
662                 continue;
663             /* */
664             var_Create(object, name, options[i].type | VLC_VAR_DOINHERIT);
665             if (options[i].type == VLC_VAR_INTEGER && value) {
666                 var_SetInteger(object, name, strtol(value, NULL, 0));
667             } else if (options[i].type == VLC_VAR_STRING && value) {
668                 var_SetString(object, name, value);
669             }
670             break;
671         }
672         free(name);
673
674         /* */
675         current = next;
676     }
677     free(dup);
678 }