3 * @brief Shared memory frame buffer capture module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2011 Rémi Denis-Courmont
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.
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.
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 ****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_demux.h>
38 #include <vlc_plugin.h>
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Caching value for frame buffer capture. " \
43 "This value should be set in milliseconds.")
45 #define FPS_TEXT N_("Frame rate")
46 #define FPS_LONGTEXT N_( \
47 "How many times the screen content should be refreshed per second.")
49 #define WIDTH_TEXT N_("Frame buffer width")
50 #define WIDTH_LONGTEXT N_( \
51 "Pixel width of the frame buffer")
53 #define HEIGHT_TEXT N_("Frame buffer height")
54 #define HEIGHT_LONGTEXT N_( \
55 "Pixel height of the frame buffer")
57 #define DEPTH_TEXT N_("Frame buffer depth")
58 #define DEPTH_LONGTEXT N_( \
59 "Pixel depth of the frame buffer")
61 #define ID_TEXT N_("Frame buffer segment ID")
62 #define ID_LONGTEXT N_( \
63 "System V shared memory segment ID of the frame buffer " \
64 "(this is ignored if --shm-file is specified).")
66 #define FILE_TEXT N_("Frame buffer file")
67 #define FILE_LONGTEXT N_( \
68 "Path of the memory mapped file of the frame buffer")
70 static int Open (vlc_object_t *);
71 static void Close (vlc_object_t *);
73 static const int depths[] = {
77 static const char *const depth_texts[] = {
78 N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
85 set_shortname (N_("Framebuffer input"))
86 set_description (N_("Shared memory framebuffer"))
87 set_category (CAT_INPUT)
88 set_subcategory (SUBCAT_INPUT_ACCESS)
89 set_capability ("access_demux", 0)
90 set_callbacks (Open, Close)
92 add_integer ("shm-caching", DEFAULT_PTS_DELAY * 1000 / CLOCK_FREQ,
93 CACHING_TEXT, CACHING_LONGTEXT, true)
94 add_float ("shm-fps", 10.0, FPS_TEXT, FPS_LONGTEXT, true)
95 add_integer ("shm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT, false)
96 change_integer_range (0, 65535)
98 add_integer ("shm-height", 480, HEIGHT_TEXT, HEIGHT_LONGTEXT, false)
99 change_integer_range (0, 65535)
101 add_integer ("shm-depth", 32, DEPTH_TEXT, DEPTH_LONGTEXT, true)
102 change_integer_list (depths, depth_texts)
105 /* We need to "trust" the memory segment. If it were shrunk while we copy
106 * its content our process may crash - or worse. So we pass the shared
107 * memory location via an unsafe variable rather than the URL. */
108 add_string ("shm-file", NULL, FILE_TEXT, FILE_LONGTEXT, false)
110 #ifdef HAVE_SYS_SHM_H
111 add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
117 static void Demux (void *);
118 static int Control (demux_t *, int, va_list);
119 static void map_detach (demux_sys_t *);
120 #ifdef HAVE_SYS_SHM_H
121 static void sysv_detach (demux_sys_t *);
123 static void no_detach (demux_sys_t *);
130 mtime_t pts, interval;
131 /* pts is protected by the lock. The rest is read-only. */
134 void (*detach) (demux_sys_t *);
137 static int Open (vlc_object_t *obj)
139 demux_t *demux = (demux_t *)obj;
141 long pagesize = sysconf (_SC_PAGE_SIZE);
145 demux_sys_t *sys = malloc (sizeof (*sys));
146 if (unlikely(sys == NULL))
148 sys->detach = no_detach;
150 uint16_t width = var_InheritInteger (demux, "shm-width");
151 uint16_t height = var_InheritInteger (demux, "shm-height");
154 switch (var_InheritInteger (demux, "shm-depth"))
157 chroma = VLC_CODEC_RGB32; bpp = 32;
160 chroma = VLC_CODEC_RGB24; bpp = 24;
163 chroma = VLC_CODEC_RGB16; bpp = 16;
166 chroma = VLC_CODEC_RGB15; bpp = 16;
169 chroma = VLC_CODEC_RGB8; bpp = 8;
175 sys->length = width * height * (bpp >> 3);
176 if (sys->length == 0)
179 sys->length = (sys->length + pagesize) & ~pagesize; /* pad */
181 char *path = var_InheritString (demux, "shm-file");
184 int fd = vlc_open (path, O_RDONLY);
187 msg_Err (demux, "cannot open file %s: %m", path);
192 void *mem = mmap (NULL, sys->length, PROT_READ, MAP_SHARED, fd, 0);
194 if (mem == MAP_FAILED)
196 msg_Err (demux, "cannot map file %s: %m", path);
202 sys->detach = map_detach;
206 #ifdef HAVE_SYS_SHM_H
207 int id = var_InheritInteger (demux, "shm-id");
208 if (id == IPC_PRIVATE)
210 void *mem = shmat (id, NULL, SHM_RDONLY);
212 if (mem == (const void *)(-1))
214 msg_Err (demux, "cannot attach segment %d: %m", id);
218 sys->detach = sysv_detach;
224 /* Initializes format */
225 float rate = var_InheritFloat (obj, "shm-fps");
229 sys->interval = (float)CLOCK_FREQ / rate;
232 sys->pts = VLC_TS_INVALID;
235 es_format_Init (&fmt, VIDEO_ES, chroma);
236 fmt.video.i_chroma = chroma;
237 fmt.video.i_bits_per_pixel = bpp;
238 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
239 fmt.video.i_frame_rate = 1000 * rate;
240 fmt.video.i_frame_rate_base = 1000;
241 fmt.video.i_visible_width = fmt.video.i_width = width;
242 fmt.video.i_visible_height = fmt.video.i_height = height;
244 sys->es = es_out_Add (demux->out, &fmt);
246 /* Initializes demux */
247 var_Create (obj, "shm-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
249 vlc_mutex_init (&sys->lock);
250 if (vlc_timer_create (&sys->timer, Demux, demux))
252 vlc_timer_schedule (sys->timer, false, 1, sys->interval);
255 demux->pf_demux = NULL;
256 demux->pf_control = Control;
269 static void Close (vlc_object_t *obj)
271 demux_t *demux = (demux_t *)obj;
272 demux_sys_t *sys = demux->p_sys;
274 vlc_timer_destroy (sys->timer);
275 vlc_mutex_destroy (&sys->lock);
281 static void map_detach (demux_sys_t *sys)
283 munmap ((void *)sys->addr, sys->length);
286 #ifdef HAVE_SYS_SHM_H
287 static void sysv_detach (demux_sys_t *sys)
293 static void no_detach (demux_sys_t *sys)
301 static int Control (demux_t *demux, int query, va_list args)
303 demux_sys_t *sys = demux->p_sys;
307 case DEMUX_GET_POSITION:
309 float *v = va_arg (args, float *);
314 case DEMUX_GET_LENGTH:
317 int64_t *v = va_arg (args, int64_t *);
322 case DEMUX_GET_PTS_DELAY:
324 int64_t *v = va_arg (args, int64_t *);
325 *v = var_GetInteger (demux, "shm-caching") * UINT64_C(1000);
329 case DEMUX_CAN_PAUSE:
331 bool *v = (bool *)va_arg (args, bool *);
336 case DEMUX_SET_PAUSE_STATE:
338 bool pausing = va_arg (args, int);
342 vlc_mutex_lock (&sys->lock);
343 sys->pts = VLC_TS_INVALID;
344 es_out_Control (demux->out, ES_OUT_RESET_PCR);
345 vlc_mutex_unlock (&sys->lock);
347 vlc_timer_schedule (sys->timer, false,
348 pausing ? 0 : 1, sys->interval);
352 case DEMUX_CAN_CONTROL_PACE:
353 case DEMUX_CAN_CONTROL_RATE:
356 bool *v = (bool *)va_arg (args, bool *);
367 * Processing callback
369 static void Demux (void *data)
371 demux_t *demux = data;
372 demux_sys_t *sys = demux->p_sys;
375 block_t *block = block_Alloc (sys->length);
379 vlc_memcpy (block->p_buffer, sys->addr, sys->length);
382 vlc_mutex_lock (&sys->lock);
383 if (sys->pts == VLC_TS_INVALID)
385 block->i_pts = block->i_dts = sys->pts;
387 es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
388 es_out_Send (demux->out, sys->es, block);
389 sys->pts += sys->interval;
390 vlc_mutex_unlock (&sys->lock);