Contribs: update dvbpsi to 1.3.2
[vlc.git] / modules / access / shm.c
blob1d192662047fad2dcfe2d127ccf23add5648dd87
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
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.
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.
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 ****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <stdarg.h>
28 #include <math.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #ifdef HAVE_SYS_SHM_H
33 # include <sys/ipc.h>
34 # include <sys/shm.h>
35 #endif
37 #include <vlc_common.h>
38 #include <vlc_demux.h>
39 #include <vlc_fs.h>
40 #include <vlc_plugin.h>
42 #define FPS_TEXT N_("Frame rate")
43 #define FPS_LONGTEXT N_( \
44 "How many times the screen content should be refreshed per second.")
46 #define DEPTH_TEXT N_("Frame buffer depth")
47 #define DEPTH_LONGTEXT N_( \
48 "Pixel depth of the frame buffer, or zero for XWD file")
50 #define WIDTH_TEXT N_("Frame buffer width")
51 #define WIDTH_LONGTEXT N_( \
52 "Pixel width of the frame buffer (ignored for XWD file)")
54 #define HEIGHT_TEXT N_("Frame buffer height")
55 #define HEIGHT_LONGTEXT N_( \
56 "Pixel height of the frame buffer (ignored for XWD file)")
58 #define ID_TEXT N_("Frame buffer segment ID")
59 #define ID_LONGTEXT N_( \
60 "System V shared memory segment ID of the frame buffer " \
61 "(this is ignored if --shm-file is specified).")
63 #define FILE_TEXT N_("Frame buffer file")
64 #define FILE_LONGTEXT N_( \
65 "Path of the memory mapped file of the frame buffer")
67 static int Open (vlc_object_t *);
68 static void Close (vlc_object_t *);
70 static const int depths[] = {
71 0, 8, 15, 16, 24, 32,
74 static const char *const depth_texts[] = {
75 N_("XWD file (autodetect)"),
76 N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
80 * Module descriptor
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", 0)
88 set_callbacks (Open, Close)
90 add_float ("shm-fps", 10.0, FPS_TEXT, FPS_LONGTEXT, true)
91 add_integer ("shm-depth", 0, DEPTH_TEXT, DEPTH_LONGTEXT, true)
92 change_integer_list (depths, depth_texts)
93 change_safe ()
94 add_integer ("shm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT, false)
95 change_integer_range (0, 65535)
96 change_safe ()
97 add_integer ("shm-height", 480, HEIGHT_TEXT, HEIGHT_LONGTEXT, false)
98 change_integer_range (0, 65535)
99 change_safe ()
101 /* We need to "trust" the memory segment. If it were shrunk while we copy
102 * its content our process may crash - or worse. So we pass the shared
103 * memory location via an unsafe variable rather than the URL. */
104 add_string ("shm-file", NULL, FILE_TEXT, FILE_LONGTEXT, false)
105 change_volatile ()
106 #ifdef HAVE_SYS_SHM_H
107 add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
108 change_volatile ()
109 #endif
110 add_shortcut ("shm")
111 vlc_module_end ()
113 typedef struct demux_sys_t demux_sys_t;
115 static int Control (demux_t *, int, va_list);
116 static void DemuxFile (void *);
117 static void CloseFile (demux_sys_t *);
118 #ifdef HAVE_SYS_SHM_H
119 static void DemuxIPC (void *);
120 static void CloseIPC (demux_sys_t *);
121 #endif
123 struct demux_sys_t
125 /* Everything is read-only when timer is armed. */
126 union
128 int fd;
129 struct
131 const void *addr;
132 size_t length;
133 } mem;
135 es_out_id_t *es;
136 vlc_timer_t timer;
137 void (*detach) (demux_sys_t *);
140 static int Open (vlc_object_t *obj)
142 demux_t *demux = (demux_t *)obj;
143 if (demux->out == NULL)
144 return VLC_EGENERIC;
146 demux_sys_t *sys = vlc_obj_malloc(obj, sizeof (*sys));
147 if (unlikely(sys == NULL))
148 return VLC_ENOMEM;
150 uint32_t chroma;
151 uint16_t width = 0, height = 0;
152 uint8_t bpp;
153 switch (var_InheritInteger (demux, "shm-depth"))
155 case 32:
156 chroma = VLC_CODEC_RGB32; bpp = 32;
157 break;
158 case 24:
159 chroma = VLC_CODEC_RGB24; bpp = 24;
160 break;
161 case 16:
162 chroma = VLC_CODEC_RGB16; bpp = 16;
163 break;
164 case 15:
165 chroma = VLC_CODEC_RGB15; bpp = 16;
166 break;
167 case 8:
168 chroma = VLC_CODEC_RGB8; bpp = 8;
169 break;
170 case 0:
171 chroma = VLC_CODEC_XWD; bpp = 0;
172 break;
173 default:
174 return VLC_EGENERIC;
176 if (bpp != 0)
178 width = var_InheritInteger (demux, "shm-width");
179 height = var_InheritInteger (demux, "shm-height");
182 static void (*Demux) (void *);
184 char *path = var_InheritString (demux, "shm-file");
185 if (path != NULL)
187 sys->fd = vlc_open (path, O_RDONLY);
188 if (sys->fd == -1)
189 msg_Err (demux, "cannot open file %s: %s", path,
190 vlc_strerror_c(errno));
191 free (path);
192 if (sys->fd == -1)
193 return VLC_EGENERIC;
195 sys->detach = CloseFile;
196 Demux = DemuxFile;
198 else
200 #ifdef HAVE_SYS_SHM_H
201 sys->mem.length = width * height * (bpp >> 3);
202 if (sys->mem.length == 0)
203 return VLC_EGENERIC;
205 int id = var_InheritInteger (demux, "shm-id");
206 if (id == IPC_PRIVATE)
207 return VLC_EGENERIC;
208 void *mem = shmat (id, NULL, SHM_RDONLY);
210 if (mem == (const void *)(-1))
212 msg_Err (demux, "cannot attach segment %d: %s", id,
213 vlc_strerror_c(errno));
214 return VLC_EGENERIC;
216 sys->mem.addr = mem;
217 sys->detach = CloseIPC;
218 Demux = DemuxIPC;
219 #else
220 goto error;
221 #endif
224 /* Initializes format */
225 float rate = var_InheritFloat (obj, "shm-fps");
226 if (rate <= 0.f)
227 goto error;
229 mtime_t interval = llroundf((float)CLOCK_FREQ / rate);
230 if (!interval)
231 goto error;
233 es_format_t fmt;
234 es_format_Init (&fmt, VIDEO_ES, chroma);
235 fmt.video.i_chroma = chroma;
236 fmt.video.i_bits_per_pixel = bpp;
237 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
238 fmt.video.i_frame_rate = 1000 * rate;
239 fmt.video.i_frame_rate_base = 1000;
240 fmt.video.i_visible_width = fmt.video.i_width = width;
241 fmt.video.i_visible_height = fmt.video.i_height = height;
243 sys->es = es_out_Add (demux->out, &fmt);
245 /* Initializes demux */
246 if (vlc_timer_create (&sys->timer, Demux, demux))
247 goto error;
248 vlc_timer_schedule (sys->timer, false, 1, interval);
250 demux->p_sys = sys;
251 demux->pf_demux = NULL;
252 demux->pf_control = Control;
253 return VLC_SUCCESS;
255 error:
256 sys->detach (sys);
257 return VLC_EGENERIC;
262 * Releases resources
264 static void Close (vlc_object_t *obj)
266 demux_t *demux = (demux_t *)obj;
267 demux_sys_t *sys = demux->p_sys;
269 vlc_timer_destroy (sys->timer);
270 sys->detach (sys);
274 * Control callback
276 static int Control (demux_t *demux, int query, va_list args)
278 switch (query)
280 case DEMUX_GET_POSITION:
282 float *v = va_arg (args, float *);
283 *v = 0.;
284 return VLC_SUCCESS;
287 case DEMUX_GET_LENGTH:
288 case DEMUX_GET_TIME:
290 int64_t *v = va_arg (args, int64_t *);
291 *v = 0;
292 return VLC_SUCCESS;
295 case DEMUX_GET_PTS_DELAY:
297 int64_t *v = va_arg (args, int64_t *);
298 *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
299 return VLC_SUCCESS;
302 case DEMUX_CAN_PAUSE:
303 case DEMUX_CAN_CONTROL_PACE:
304 case DEMUX_CAN_CONTROL_RATE:
305 case DEMUX_CAN_SEEK:
307 bool *v = va_arg (args, bool *);
308 *v = false;
309 return VLC_SUCCESS;
312 case DEMUX_SET_PAUSE_STATE:
313 return VLC_SUCCESS; /* should not happen */
316 return VLC_EGENERIC;
320 * Processing callback
322 static void DemuxFile (void *data)
324 demux_t *demux = data;
325 demux_sys_t *sys = demux->p_sys;
327 /* Copy frame */
328 block_t *block = block_File(sys->fd, true);
329 if (block == NULL)
330 return;
331 block->i_pts = block->i_dts = mdate ();
333 /* Send block */
334 es_out_SetPCR(demux->out, block->i_pts);
335 es_out_Send (demux->out, sys->es, block);
338 static void CloseFile (demux_sys_t *sys)
340 vlc_close (sys->fd);
343 #ifdef HAVE_SYS_SHM_H
344 static void DemuxIPC (void *data)
346 demux_t *demux = data;
347 demux_sys_t *sys = demux->p_sys;
349 /* Copy frame */
350 block_t *block = block_Alloc (sys->mem.length);
351 if (block == NULL)
352 return;
353 memcpy (block->p_buffer, sys->mem.addr, sys->mem.length);
354 block->i_pts = block->i_dts = mdate ();
356 /* Send block */
357 es_out_SetPCR(demux->out, block->i_pts);
358 es_out_Send (demux->out, sys->es, block);
361 static void CloseIPC (demux_sys_t *sys)
363 shmdt (sys->mem.addr);
365 #endif