]> git.sesse.net Git - vlc/blob - modules/access/shm.c
skins2: remove unneeded check
[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 program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program 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 Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, 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 <math.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #ifdef HAVE_SYS_SHM_H
32 # include <sys/ipc.h>
33 # include <sys/shm.h>
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_demux.h>
38 #include <vlc_fs.h>
39 #include <vlc_plugin.h>
40
41 #define FPS_TEXT N_("Frame rate")
42 #define FPS_LONGTEXT N_( \
43     "How many times the screen content should be refreshed per second.")
44
45 #define DEPTH_TEXT N_("Frame buffer depth")
46 #define DEPTH_LONGTEXT N_( \
47     "Pixel depth of the frame buffer, or zero for XWD file")
48
49 #define WIDTH_TEXT N_("Frame buffer width")
50 #define WIDTH_LONGTEXT N_( \
51     "Pixel width of the frame buffer (ignored for XWD file)")
52
53 #define HEIGHT_TEXT N_("Frame buffer height")
54 #define HEIGHT_LONGTEXT N_( \
55     "Pixel height of the frame buffer (ignored for XWD file)")
56
57 #define ID_TEXT N_("Frame buffer segment ID")
58 #define ID_LONGTEXT N_( \
59     "System V shared memory segment ID of the frame buffer " \
60     "(this is ignored if --shm-file is specified).")
61
62 #define FILE_TEXT N_("Frame buffer file")
63 #define FILE_LONGTEXT N_( \
64     "Path of the memory mapped file of the frame buffer")
65
66 static int  Open (vlc_object_t *);
67 static void Close (vlc_object_t *);
68
69 static const int depths[] = {
70     0, 8, 15, 16, 24, 32,
71 };
72
73 static const char *const depth_texts[] = {
74     N_("XWD file (autodetect)"),
75     N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
76 };
77
78 /*
79  * Module descriptor
80  */
81 vlc_module_begin ()
82     set_shortname (N_("Framebuffer input"))
83     set_description (N_("Shared memory framebuffer"))
84     set_category (CAT_INPUT)
85     set_subcategory (SUBCAT_INPUT_ACCESS)
86     set_capability ("access_demux", 0)
87     set_callbacks (Open, Close)
88
89     add_float ("shm-fps", 10.0, FPS_TEXT, FPS_LONGTEXT, true)
90     add_integer ("shm-depth", 0, DEPTH_TEXT, DEPTH_LONGTEXT, true)
91         change_integer_list (depths, depth_texts)
92         change_safe ()
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
100     /* We need to "trust" the memory segment. If it were shrunk while we copy
101      * its content our process may crash - or worse. So we pass the shared
102      * memory location via an unsafe variable rather than the URL. */
103     add_string ("shm-file", NULL, FILE_TEXT, FILE_LONGTEXT, false)
104         change_volatile ()
105 #ifdef HAVE_SYS_SHM_H
106     add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
107         change_volatile ()
108 #endif
109     add_shortcut ("shm")
110 vlc_module_end ()
111
112 static int Control (demux_t *, int, va_list);
113 static void DemuxFile (void *);
114 static void CloseFile (demux_sys_t *);
115 #ifdef HAVE_SYS_SHM_H
116 static void DemuxIPC (void *);
117 static void CloseIPC (demux_sys_t *);
118 #endif
119 static void no_detach (demux_sys_t *);
120
121 struct demux_sys_t
122 {
123     /* Everything is read-only when timer is armed. */
124     union
125     {
126         int fd;
127         struct
128         {
129              const void  *addr;
130              size_t       length;
131         } mem;
132     };
133     es_out_id_t *es;
134     vlc_timer_t  timer;
135     void (*detach) (demux_sys_t *);
136 };
137
138 static int Open (vlc_object_t *obj)
139 {
140     demux_t *demux = (demux_t *)obj;
141     demux_sys_t *sys = malloc (sizeof (*sys));
142     if (unlikely(sys == NULL))
143         return VLC_ENOMEM;
144     sys->detach = no_detach;
145
146     uint32_t chroma;
147     uint16_t width = 0, height = 0;
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         case 0:
167             chroma = VLC_CODEC_XWD; bpp = 0;
168             break;
169         default:
170             goto error;
171     }
172     if (bpp != 0)
173     {
174         width = var_InheritInteger (demux, "shm-width");
175         height = var_InheritInteger (demux, "shm-height");
176     }
177
178     static void (*Demux) (void *);
179
180     char *path = var_InheritString (demux, "shm-file");
181     if (path != NULL)
182     {
183         sys->fd = vlc_open (path, O_RDONLY);
184         if (sys->fd == -1)
185             msg_Err (demux, "cannot open file %s: %m", path);
186         free (path);
187         if (sys->fd == -1)
188             goto error;
189
190         sys->detach = CloseFile;
191         Demux = DemuxFile;
192     }
193     else
194     {
195 #ifdef HAVE_SYS_SHM_H
196         sys->mem.length = width * height * (bpp >> 3);
197         if (sys->mem.length == 0)
198             goto error;
199
200         int id = var_InheritInteger (demux, "shm-id");
201         if (id == IPC_PRIVATE)
202             goto error;
203         void *mem = shmat (id, NULL, SHM_RDONLY);
204
205         if (mem == (const void *)(-1))
206         {
207             msg_Err (demux, "cannot attach segment %d: %m", id);
208             goto error;
209         }
210         sys->mem.addr = mem;
211         sys->detach = CloseIPC;
212         Demux = DemuxIPC;
213 #else
214         goto error;
215 #endif
216     }
217
218     /* Initializes format */
219     float rate = var_InheritFloat (obj, "shm-fps");
220     if (rate <= 0.)
221         goto error;
222
223     mtime_t interval = llroundf((float)CLOCK_FREQ / rate);
224     if (!interval)
225         goto error;
226
227     es_format_t fmt;
228     es_format_Init (&fmt, VIDEO_ES, chroma);
229     fmt.video.i_chroma = chroma;
230     fmt.video.i_bits_per_pixel = bpp;
231     fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
232     fmt.video.i_frame_rate = 1000 * rate;
233     fmt.video.i_frame_rate_base = 1000;
234     fmt.video.i_visible_width = fmt.video.i_width = width;
235     fmt.video.i_visible_height = fmt.video.i_height = height;
236
237     sys->es = es_out_Add (demux->out, &fmt);
238
239     /* Initializes demux */
240     if (vlc_timer_create (&sys->timer, Demux, demux))
241         goto error;
242     vlc_timer_schedule (sys->timer, false, 1, interval);
243
244     demux->p_sys = sys;
245     demux->pf_demux   = NULL;
246     demux->pf_control = Control;
247     return VLC_SUCCESS;
248
249 error:
250     sys->detach (sys);
251     free (sys);
252     return VLC_EGENERIC;
253 }
254
255
256 /**
257  * Releases resources
258  */
259 static void Close (vlc_object_t *obj)
260 {
261     demux_t *demux = (demux_t *)obj;
262     demux_sys_t *sys = demux->p_sys;
263
264     vlc_timer_destroy (sys->timer);
265     sys->detach (sys);
266     free (sys);
267 }
268
269 static void no_detach (demux_sys_t *sys)
270 {
271     (void) sys;
272 }
273
274 /**
275  * Control callback
276  */
277 static int Control (demux_t *demux, int query, va_list args)
278 {
279     switch (query)
280     {
281         case DEMUX_GET_POSITION:
282         {
283             float *v = va_arg (args, float *);
284             *v = 0.;
285             return VLC_SUCCESS;
286         }
287
288         case DEMUX_GET_LENGTH:
289         case DEMUX_GET_TIME:
290         {
291             int64_t *v = va_arg (args, int64_t *);
292             *v = 0;
293             return VLC_SUCCESS;
294         }
295
296         case DEMUX_GET_PTS_DELAY:
297         {
298             int64_t *v = va_arg (args, int64_t *);
299             *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
300             return VLC_SUCCESS;
301         }
302
303         case DEMUX_CAN_PAUSE:
304         case DEMUX_CAN_CONTROL_PACE:
305         case DEMUX_CAN_CONTROL_RATE:
306         case DEMUX_CAN_SEEK:
307         {
308             bool *v = va_arg (args, bool *);
309             *v = false;
310             return VLC_SUCCESS;
311         }
312
313         case DEMUX_SET_PAUSE_STATE:
314             return VLC_SUCCESS; /* should not happen */
315     }
316
317     return VLC_EGENERIC;
318 }
319
320 /**
321  * Processing callback
322  */
323 static void DemuxFile (void *data)
324 {
325     demux_t *demux = data;
326     demux_sys_t *sys = demux->p_sys;
327
328     /* Copy frame */
329     block_t *block = block_File (sys->fd);
330     if (block == NULL)
331         return;
332     block->i_pts = block->i_dts = mdate ();
333
334     /* Send block */
335     es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
336     es_out_Send (demux->out, sys->es, block);
337 }
338
339 static void CloseFile (demux_sys_t *sys)
340 {
341     close (sys->fd);
342 }
343
344 #ifdef HAVE_SYS_SHM_H
345 static void DemuxIPC (void *data)
346 {
347     demux_t *demux = data;
348     demux_sys_t *sys = demux->p_sys;
349
350     /* Copy frame */
351     block_t *block = block_Alloc (sys->mem.length);
352     if (block == NULL)
353         return;
354     memcpy (block->p_buffer, sys->mem.addr, sys->mem.length);
355     block->i_pts = block->i_dts = mdate ();
356
357     /* Send block */
358     es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
359     es_out_Send (demux->out, sys->es, block);
360 }
361
362 static void CloseIPC (demux_sys_t *sys)
363 {
364     shmdt (sys->mem.addr);
365 }
366 #endif