3 * @brief Shared memory frame buffer capture module for VLC media player
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 ****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_demux.h>
39 #include <vlc_plugin.h>
41 #define FPS_TEXT N_("Frame rate")
42 #define FPS_LONGTEXT N_( \
43 "How many times the screen content should be refreshed per second.")
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")
49 #define WIDTH_TEXT N_("Frame buffer width")
50 #define WIDTH_LONGTEXT N_( \
51 "Pixel width of the frame buffer (ignored for XWD file)")
53 #define HEIGHT_TEXT N_("Frame buffer height")
54 #define HEIGHT_LONGTEXT N_( \
55 "Pixel height of the frame buffer (ignored for XWD file)")
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).")
62 #define FILE_TEXT N_("Frame buffer file")
63 #define FILE_LONGTEXT N_( \
64 "Path of the memory mapped file of the frame buffer")
66 static int Open (vlc_object_t *);
67 static void Close (vlc_object_t *);
69 static const int depths[] = {
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"),
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)
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)
93 add_integer ("shm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT, false)
94 change_integer_range (0, 65535)
96 add_integer ("shm-height", 480, HEIGHT_TEXT, HEIGHT_LONGTEXT, false)
97 change_integer_range (0, 65535)
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)
105 #ifdef HAVE_SYS_SHM_H
106 add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
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 *);
119 static void no_detach (demux_sys_t *);
123 /* Everything is read-only when timer is armed. */
135 void (*detach) (demux_sys_t *);
138 static int Open (vlc_object_t *obj)
140 demux_t *demux = (demux_t *)obj;
141 demux_sys_t *sys = malloc (sizeof (*sys));
142 if (unlikely(sys == NULL))
144 sys->detach = no_detach;
147 uint16_t width = 0, height = 0;
149 switch (var_InheritInteger (demux, "shm-depth"))
152 chroma = VLC_CODEC_RGB32; bpp = 32;
155 chroma = VLC_CODEC_RGB24; bpp = 24;
158 chroma = VLC_CODEC_RGB16; bpp = 16;
161 chroma = VLC_CODEC_RGB15; bpp = 16;
164 chroma = VLC_CODEC_RGB8; bpp = 8;
167 chroma = VLC_CODEC_XWD; bpp = 0;
174 width = var_InheritInteger (demux, "shm-width");
175 height = var_InheritInteger (demux, "shm-height");
178 static void (*Demux) (void *);
180 char *path = var_InheritString (demux, "shm-file");
183 sys->fd = vlc_open (path, O_RDONLY);
185 msg_Err (demux, "cannot open file %s: %m", path);
190 sys->detach = CloseFile;
195 #ifdef HAVE_SYS_SHM_H
196 sys->mem.length = width * height * (bpp >> 3);
197 if (sys->mem.length == 0)
200 int id = var_InheritInteger (demux, "shm-id");
201 if (id == IPC_PRIVATE)
203 void *mem = shmat (id, NULL, SHM_RDONLY);
205 if (mem == (const void *)(-1))
207 msg_Err (demux, "cannot attach segment %d: %m", id);
211 sys->detach = CloseIPC;
218 /* Initializes format */
219 float rate = var_InheritFloat (obj, "shm-fps");
223 mtime_t interval = llroundf((float)CLOCK_FREQ / rate);
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;
237 sys->es = es_out_Add (demux->out, &fmt);
239 /* Initializes demux */
240 if (vlc_timer_create (&sys->timer, Demux, demux))
242 vlc_timer_schedule (sys->timer, false, 1, interval);
245 demux->pf_demux = NULL;
246 demux->pf_control = Control;
259 static void Close (vlc_object_t *obj)
261 demux_t *demux = (demux_t *)obj;
262 demux_sys_t *sys = demux->p_sys;
264 vlc_timer_destroy (sys->timer);
269 static void no_detach (demux_sys_t *sys)
277 static int Control (demux_t *demux, int query, va_list args)
281 case DEMUX_GET_POSITION:
283 float *v = va_arg (args, float *);
288 case DEMUX_GET_LENGTH:
291 int64_t *v = va_arg (args, int64_t *);
296 case DEMUX_GET_PTS_DELAY:
298 int64_t *v = va_arg (args, int64_t *);
299 *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
303 case DEMUX_CAN_PAUSE:
304 case DEMUX_CAN_CONTROL_PACE:
305 case DEMUX_CAN_CONTROL_RATE:
308 bool *v = va_arg (args, bool *);
313 case DEMUX_SET_PAUSE_STATE:
314 return VLC_SUCCESS; /* should not happen */
321 * Processing callback
323 static void DemuxFile (void *data)
325 demux_t *demux = data;
326 demux_sys_t *sys = demux->p_sys;
329 block_t *block = block_File (sys->fd);
332 block->i_pts = block->i_dts = mdate ();
335 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
336 es_out_Send (demux->out, sys->es, block);
339 static void CloseFile (demux_sys_t *sys)
344 #ifdef HAVE_SYS_SHM_H
345 static void DemuxIPC (void *data)
347 demux_t *demux = data;
348 demux_sys_t *sys = demux->p_sys;
351 block_t *block = block_Alloc (sys->mem.length);
354 memcpy (block->p_buffer, sys->mem.addr, sys->mem.length);
355 block->i_pts = block->i_dts = mdate ();
358 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
359 es_out_Send (demux->out, sys->es, block);
362 static void CloseIPC (demux_sys_t *sys)
364 shmdt (sys->mem.addr);