A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / access / shm.c
blob7e3a536bfc17e95dd5f521d5fca24f9a9b6a6714
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_demux", 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 static int Control (demux_t *, int, va_list);
114 static void DemuxFile (void *);
115 static void CloseFile (demux_sys_t *);
116 #ifdef HAVE_SYS_SHM_H
117 static void DemuxIPC (void *);
118 static void CloseIPC (demux_sys_t *);
119 #endif
120 static void no_detach (demux_sys_t *);
122 struct demux_sys_t
124 /* Everything is read-only when timer is armed. */
125 union
127 int fd;
128 struct
130 const void *addr;
131 size_t length;
132 } mem;
134 es_out_id_t *es;
135 vlc_timer_t timer;
136 void (*detach) (demux_sys_t *);
139 static int Open (vlc_object_t *obj)
141 demux_t *demux = (demux_t *)obj;
142 demux_sys_t *sys = malloc (sizeof (*sys));
143 if (unlikely(sys == NULL))
144 return VLC_ENOMEM;
145 sys->detach = no_detach;
147 uint32_t chroma;
148 uint16_t width = 0, height = 0;
149 uint8_t bpp;
150 switch (var_InheritInteger (demux, "shm-depth"))
152 case 32:
153 chroma = VLC_CODEC_RGB32; bpp = 32;
154 break;
155 case 24:
156 chroma = VLC_CODEC_RGB24; bpp = 24;
157 break;
158 case 16:
159 chroma = VLC_CODEC_RGB16; bpp = 16;
160 break;
161 case 15:
162 chroma = VLC_CODEC_RGB15; bpp = 16;
163 break;
164 case 8:
165 chroma = VLC_CODEC_RGB8; bpp = 8;
166 break;
167 case 0:
168 chroma = VLC_CODEC_XWD; bpp = 0;
169 break;
170 default:
171 goto error;
173 if (bpp != 0)
175 width = var_InheritInteger (demux, "shm-width");
176 height = var_InheritInteger (demux, "shm-height");
179 static void (*Demux) (void *);
181 char *path = var_InheritString (demux, "shm-file");
182 if (path != NULL)
184 sys->fd = vlc_open (path, O_RDONLY);
185 if (sys->fd == -1)
186 msg_Err (demux, "cannot open file %s: %s", path,
187 vlc_strerror_c(errno));
188 free (path);
189 if (sys->fd == -1)
190 goto error;
192 sys->detach = CloseFile;
193 Demux = DemuxFile;
195 else
197 #ifdef HAVE_SYS_SHM_H
198 sys->mem.length = width * height * (bpp >> 3);
199 if (sys->mem.length == 0)
200 goto error;
202 int id = var_InheritInteger (demux, "shm-id");
203 if (id == IPC_PRIVATE)
204 goto error;
205 void *mem = shmat (id, NULL, SHM_RDONLY);
207 if (mem == (const void *)(-1))
209 msg_Err (demux, "cannot attach segment %d: %s", id,
210 vlc_strerror_c(errno));
211 goto error;
213 sys->mem.addr = mem;
214 sys->detach = CloseIPC;
215 Demux = DemuxIPC;
216 #else
217 goto error;
218 #endif
221 /* Initializes format */
222 float rate = var_InheritFloat (obj, "shm-fps");
223 if (rate <= 0.f)
224 goto error;
226 mtime_t interval = llroundf((float)CLOCK_FREQ / rate);
227 if (!interval)
228 goto error;
230 es_format_t fmt;
231 es_format_Init (&fmt, VIDEO_ES, chroma);
232 fmt.video.i_chroma = chroma;
233 fmt.video.i_bits_per_pixel = bpp;
234 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
235 fmt.video.i_frame_rate = 1000 * rate;
236 fmt.video.i_frame_rate_base = 1000;
237 fmt.video.i_visible_width = fmt.video.i_width = width;
238 fmt.video.i_visible_height = fmt.video.i_height = height;
240 sys->es = es_out_Add (demux->out, &fmt);
242 /* Initializes demux */
243 if (vlc_timer_create (&sys->timer, Demux, demux))
244 goto error;
245 vlc_timer_schedule (sys->timer, false, 1, interval);
247 demux->p_sys = sys;
248 demux->pf_demux = NULL;
249 demux->pf_control = Control;
250 return VLC_SUCCESS;
252 error:
253 sys->detach (sys);
254 free (sys);
255 return VLC_EGENERIC;
260 * Releases resources
262 static void Close (vlc_object_t *obj)
264 demux_t *demux = (demux_t *)obj;
265 demux_sys_t *sys = demux->p_sys;
267 vlc_timer_destroy (sys->timer);
268 sys->detach (sys);
269 free (sys);
272 static void no_detach (demux_sys_t *sys)
274 (void) sys;
278 * Control callback
280 static int Control (demux_t *demux, int query, va_list args)
282 switch (query)
284 case DEMUX_GET_POSITION:
286 float *v = va_arg (args, float *);
287 *v = 0.;
288 return VLC_SUCCESS;
291 case DEMUX_GET_LENGTH:
292 case DEMUX_GET_TIME:
294 int64_t *v = va_arg (args, int64_t *);
295 *v = 0;
296 return VLC_SUCCESS;
299 case DEMUX_GET_PTS_DELAY:
301 int64_t *v = va_arg (args, int64_t *);
302 *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
303 return VLC_SUCCESS;
306 case DEMUX_CAN_PAUSE:
307 case DEMUX_CAN_CONTROL_PACE:
308 case DEMUX_CAN_CONTROL_RATE:
309 case DEMUX_CAN_SEEK:
311 bool *v = va_arg (args, bool *);
312 *v = false;
313 return VLC_SUCCESS;
316 case DEMUX_SET_PAUSE_STATE:
317 return VLC_SUCCESS; /* should not happen */
320 return VLC_EGENERIC;
324 * Processing callback
326 static void DemuxFile (void *data)
328 demux_t *demux = data;
329 demux_sys_t *sys = demux->p_sys;
331 /* Copy frame */
332 block_t *block = block_File (sys->fd);
333 if (block == NULL)
334 return;
335 block->i_pts = block->i_dts = mdate ();
337 /* Send block */
338 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
339 es_out_Send (demux->out, sys->es, block);
342 static void CloseFile (demux_sys_t *sys)
344 close (sys->fd);
347 #ifdef HAVE_SYS_SHM_H
348 static void DemuxIPC (void *data)
350 demux_t *demux = data;
351 demux_sys_t *sys = demux->p_sys;
353 /* Copy frame */
354 block_t *block = block_Alloc (sys->mem.length);
355 if (block == NULL)
356 return;
357 memcpy (block->p_buffer, sys->mem.addr, sys->mem.length);
358 block->i_pts = block->i_dts = mdate ();
360 /* Send block */
361 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
362 es_out_Send (demux->out, sys->es, block);
365 static void CloseIPC (demux_sys_t *sys)
367 shmdt (sys->mem.addr);
369 #endif