]> git.sesse.net Git - vlc/blob - modules/access/shm.c
DTV: read TS feed old-style from the monolithic DVR
[vlc] / modules / access / shm.c
1 /**
2  * @file shm.c
3  * @brief Shared memory frame buffer capture module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2011 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdarg.h>
28 #include <fcntl.h>
29 #include <sys/ipc.h>
30 #include <sys/shm.h>
31 #include <sys/mman.h>
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35 #include <vlc_fs.h>
36 #include <vlc_plugin.h>
37
38 #define CACHING_TEXT N_("Caching value in ms")
39 #define CACHING_LONGTEXT N_( \
40     "Caching value for frame buffer capture. " \
41     "This value should be set in milliseconds.")
42
43 #define FPS_TEXT N_("Frame rate")
44 #define FPS_LONGTEXT N_( \
45     "How many times the screen content should be refreshed per second.")
46
47 #define WIDTH_TEXT N_("Frame buffer width")
48 #define WIDTH_LONGTEXT N_( \
49     "Pixel width of the frame buffer")
50
51 #define HEIGHT_TEXT N_("Frame buffer height")
52 #define HEIGHT_LONGTEXT N_( \
53     "Pixel height of the frame buffer")
54
55 #define DEPTH_TEXT N_("Frame buffer depth")
56 #define DEPTH_LONGTEXT N_( \
57     "Pixel depth of the frame buffer")
58
59 #define ID_TEXT N_("Frame buffer segment ID")
60 #define ID_LONGTEXT N_( \
61     "System V shared memory segment ID of the frame buffer " \
62     "(this is ignored if --shm-file is specified).")
63
64 #define FILE_TEXT N_("Frame buffer file")
65 #define FILE_LONGTEXT N_( \
66     "Path of the memory mapped file of the frame buffer")
67
68 static int  Open (vlc_object_t *);
69 static void Close (vlc_object_t *);
70
71 static const int depths[] = {
72     8, 15, 16, 24, 32,
73 };
74
75 static const char *const depth_texts[] = {
76     N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
77 };
78
79 /*
80  * Module descriptor
81  */
82 vlc_module_begin ()
83     set_shortname (N_("Framebuffer input"))
84     set_description (N_("Shared memory framebuffer"))
85     set_category (CAT_INPUT)
86     set_subcategory (SUBCAT_INPUT_ACCESS)
87     set_capability ("access_demux", 0)
88     set_callbacks (Open, Close)
89
90     add_integer ("shm-caching", DEFAULT_PTS_DELAY * 1000 / CLOCK_FREQ,
91                  CACHING_TEXT, CACHING_LONGTEXT, true)
92     add_float ("shm-fps", 10.0, FPS_TEXT, FPS_LONGTEXT, true)
93     add_integer ("shm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT, false)
94         change_integer_range (0, 65535)
95         change_safe ()
96     add_integer ("shm-height", 480, HEIGHT_TEXT, HEIGHT_LONGTEXT, false)
97         change_integer_range (0, 65535)
98         change_safe ()
99     add_integer ("shm-depth", 32, DEPTH_TEXT, DEPTH_LONGTEXT, true)
100         change_integer_list (depths, depth_texts)
101         change_safe ()
102
103     /* We need to "trust" the memory segment. If it were shrunk while we copy
104      * its content our process may crash - or worse. So we pass the shared
105      * memory location via an unsafe variable rather than the URL. */
106     add_string ("shm-file", NULL, FILE_TEXT, FILE_LONGTEXT, false)
107         change_volatile ()
108     add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
109         change_volatile ()
110
111     add_shortcut ("shm")
112 vlc_module_end ()
113
114 static void Demux (void *);
115 static int Control (demux_t *, int, va_list);
116 static void map_detach (demux_sys_t *);
117 static void sysv_detach (demux_sys_t *);
118 static void no_detach (demux_sys_t *);
119
120 struct demux_sys_t
121 {
122     const void  *addr;
123     size_t       length;
124     es_out_id_t *es;
125     mtime_t      pts, interval;
126     /* pts is protected by the lock. The rest is read-only. */
127     vlc_mutex_t  lock;
128     vlc_timer_t  timer;
129     void (*detach) (demux_sys_t *);
130 };
131
132 static int Open (vlc_object_t *obj)
133 {
134     demux_t *demux = (demux_t *)obj;
135
136     long pagesize = sysconf (_SC_PAGE_SIZE);
137     if (pagesize == -1)
138         return VLC_EGENERIC;
139     
140     demux_sys_t *sys = malloc (sizeof (*sys));
141     if (unlikely(sys == NULL))
142         return VLC_ENOMEM;
143     sys->detach = no_detach;
144
145     uint16_t width = var_InheritInteger (demux, "shm-width");
146     uint16_t height = var_InheritInteger (demux, "shm-height");
147     uint32_t chroma;
148     uint8_t bpp;
149     switch (var_InheritInteger (demux, "shm-depth"))
150     {
151         case 32:
152             chroma = VLC_CODEC_RGB32; bpp = 32;
153             break;
154         case 24:
155             chroma = VLC_CODEC_RGB24; bpp = 24;
156             break;
157         case 16:
158             chroma = VLC_CODEC_RGB16; bpp = 16;
159             break;
160         case 15:
161             chroma = VLC_CODEC_RGB15; bpp = 16;
162             break;
163         case 8:
164             chroma = VLC_CODEC_RGB8; bpp = 8;
165             break;
166         default:
167             goto error;
168     }
169
170     sys->length = width * height * (bpp >> 3);
171     if (sys->length == 0)
172         goto error;
173     pagesize--;
174     sys->length = (sys->length + pagesize) & ~pagesize; /* pad */
175
176     char *path = var_InheritString (demux, "shm-file");
177     if (path != NULL)
178     {
179         int fd = vlc_open (path, O_RDONLY);
180         if (fd == -1)
181         {
182             msg_Err (demux, "cannot open file %s: %m", path);
183             free (path);
184             goto error;
185         }
186
187         void *mem = mmap (NULL, sys->length, PROT_READ, MAP_SHARED, fd, 0);
188         close (fd);
189         if (mem == MAP_FAILED)
190         {
191             msg_Err (demux, "cannot map file %s: %m", path);
192             free (path);
193             goto error;
194         }
195         free (path);
196         sys->addr = mem;
197         sys->detach = map_detach;
198     }
199     else
200     {
201         int id = var_InheritInteger (demux, "shm-id");
202         if (id == IPC_PRIVATE)
203             goto error;
204         void *mem = shmat (id, NULL, SHM_RDONLY);
205
206         if (mem == (const void *)(-1))
207         {
208             msg_Err (demux, "cannot attach segment %d: %m", id);
209             goto error;
210         }
211         sys->addr = mem;
212         sys->detach = sysv_detach;
213     }
214
215     /* Initializes format */
216     float rate = var_InheritFloat (obj, "shm-fps");
217     if (rate <= 0.)
218         goto error;
219
220     sys->interval = (float)CLOCK_FREQ / rate;
221     if (!sys->interval)
222         goto error;
223     sys->pts = VLC_TS_INVALID;
224
225     es_format_t fmt;
226     es_format_Init (&fmt, VIDEO_ES, chroma);
227     fmt.video.i_chroma = chroma;
228     fmt.video.i_bits_per_pixel = bpp;
229     fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
230     fmt.video.i_frame_rate = 1000 * rate;
231     fmt.video.i_frame_rate_base = 1000;
232     fmt.video.i_visible_width = fmt.video.i_width = width;
233     fmt.video.i_visible_height = fmt.video.i_height = height;
234
235     sys->es = es_out_Add (demux->out, &fmt);
236
237     /* Initializes demux */
238     var_Create (obj, "shm-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
239
240     vlc_mutex_init (&sys->lock);
241     if (vlc_timer_create (&sys->timer, Demux, demux))
242         goto error;
243     vlc_timer_schedule (sys->timer, false, 1, sys->interval);
244
245     demux->p_sys = sys;
246     demux->pf_demux   = NULL;
247     demux->pf_control = Control;
248     return VLC_SUCCESS;
249
250 error:
251     sys->detach (sys);
252     free (sys);
253     return VLC_EGENERIC;
254 }
255
256
257 /**
258  * Releases resources
259  */
260 static void Close (vlc_object_t *obj)
261 {
262     demux_t *demux = (demux_t *)obj;
263     demux_sys_t *sys = demux->p_sys;
264
265     vlc_timer_destroy (sys->timer);
266     vlc_mutex_destroy (&sys->lock);
267     sys->detach (sys);
268     free (sys);
269 }
270
271
272 static void map_detach (demux_sys_t *sys)
273 {
274     munmap ((void *)sys->addr, sys->length);
275 }
276
277
278 static void sysv_detach (demux_sys_t *sys)
279 {
280     shmdt (sys->addr);
281 }
282
283 static void no_detach (demux_sys_t *sys)
284 {
285     (void) sys;
286 }
287
288 /**
289  * Control callback
290  */
291 static int Control (demux_t *demux, int query, va_list args)
292 {
293     demux_sys_t *sys = demux->p_sys;
294
295     switch (query)
296     {
297         case DEMUX_GET_POSITION:
298         {
299             float *v = va_arg (args, float *);
300             *v = 0.;
301             return VLC_SUCCESS;
302         }
303
304         case DEMUX_GET_LENGTH:
305         case DEMUX_GET_TIME:
306         {
307             int64_t *v = va_arg (args, int64_t *);
308             *v = 0;
309             return VLC_SUCCESS;
310         }
311
312         case DEMUX_GET_PTS_DELAY:
313         {
314             int64_t *v = va_arg (args, int64_t *);
315             *v = var_GetInteger (demux, "shm-caching") * UINT64_C(1000);
316             return VLC_SUCCESS;
317         }
318
319         case DEMUX_CAN_PAUSE:
320         {
321             bool *v = (bool *)va_arg (args, bool *);
322             *v = true;
323             return VLC_SUCCESS;
324         }
325
326         case DEMUX_SET_PAUSE_STATE:
327         {
328             bool pausing = va_arg (args, int);
329
330             if (!pausing)
331             {
332                 vlc_mutex_lock (&sys->lock);
333                 sys->pts = VLC_TS_INVALID;
334                 es_out_Control (demux->out, ES_OUT_RESET_PCR);
335                 vlc_mutex_unlock (&sys->lock);
336             }
337             vlc_timer_schedule (sys->timer, false,
338                                 pausing ? 0 : 1, sys->interval);
339             return VLC_SUCCESS;
340         }
341
342         case DEMUX_CAN_CONTROL_PACE:
343         case DEMUX_CAN_CONTROL_RATE:
344         case DEMUX_CAN_SEEK:
345         {
346             bool *v = (bool *)va_arg (args, bool *);
347             *v = false;
348             return VLC_SUCCESS;
349         }
350     }
351
352     return VLC_EGENERIC;
353 }
354
355
356 /**
357  * Processing callback
358  */
359 static void Demux (void *data)
360 {
361     demux_t *demux = data;
362     demux_sys_t *sys = demux->p_sys;
363
364     /* Copy frame */
365     block_t *block = block_Alloc (sys->length);
366     if (block == NULL)
367         return;
368
369     vlc_memcpy (block->p_buffer, sys->addr, sys->length);
370
371     /* Send block */
372     vlc_mutex_lock (&sys->lock);
373     if (sys->pts == VLC_TS_INVALID)
374         sys->pts = mdate ();
375     block->i_pts = block->i_dts = sys->pts;
376
377     es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
378     es_out_Send (demux->out, sys->es, block);
379     sys->pts += sys->interval;
380     vlc_mutex_unlock (&sys->lock);
381 }