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 ****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_demux.h>
38 #include <vlc_plugin.h>
40 #define FPS_TEXT N_("Frame rate")
41 #define FPS_LONGTEXT N_( \
42 "How many times the screen content should be refreshed per second.")
44 #define WIDTH_TEXT N_("Frame buffer width")
45 #define WIDTH_LONGTEXT N_( \
46 "Pixel width of the frame buffer")
48 #define HEIGHT_TEXT N_("Frame buffer height")
49 #define HEIGHT_LONGTEXT N_( \
50 "Pixel height of the frame buffer")
52 #define DEPTH_TEXT N_("Frame buffer depth")
53 #define DEPTH_LONGTEXT N_( \
54 "Pixel depth of the frame buffer")
56 #define ID_TEXT N_("Frame buffer segment ID")
57 #define ID_LONGTEXT N_( \
58 "System V shared memory segment ID of the frame buffer " \
59 "(this is ignored if --shm-file is specified).")
61 #define FILE_TEXT N_("Frame buffer file")
62 #define FILE_LONGTEXT N_( \
63 "Path of the memory mapped file of the frame buffer")
65 static int Open (vlc_object_t *);
66 static void Close (vlc_object_t *);
68 static const int depths[] = {
72 static const char *const depth_texts[] = {
73 N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
80 set_shortname (N_("Framebuffer input"))
81 set_description (N_("Shared memory framebuffer"))
82 set_category (CAT_INPUT)
83 set_subcategory (SUBCAT_INPUT_ACCESS)
84 set_capability ("access_demux", 0)
85 set_callbacks (Open, Close)
87 add_float ("shm-fps", 10.0, FPS_TEXT, FPS_LONGTEXT, true)
88 add_integer ("shm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT, false)
89 change_integer_range (0, 65535)
91 add_integer ("shm-height", 480, HEIGHT_TEXT, HEIGHT_LONGTEXT, false)
92 change_integer_range (0, 65535)
94 add_integer ("shm-depth", 32, DEPTH_TEXT, DEPTH_LONGTEXT, true)
95 change_integer_list (depths, depth_texts)
98 /* We need to "trust" the memory segment. If it were shrunk while we copy
99 * its content our process may crash - or worse. So we pass the shared
100 * memory location via an unsafe variable rather than the URL. */
101 add_string ("shm-file", NULL, FILE_TEXT, FILE_LONGTEXT, false)
103 #ifdef HAVE_SYS_SHM_H
104 add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
110 static void Demux (void *);
111 static int Control (demux_t *, int, va_list);
112 static void map_detach (demux_sys_t *);
113 #ifdef HAVE_SYS_SHM_H
114 static void sysv_detach (demux_sys_t *);
116 static void no_detach (demux_sys_t *);
123 mtime_t pts, interval;
124 /* pts is protected by the lock. The rest is read-only. */
127 void (*detach) (demux_sys_t *);
130 static int Open (vlc_object_t *obj)
132 demux_t *demux = (demux_t *)obj;
134 long pagesize = sysconf (_SC_PAGE_SIZE);
138 demux_sys_t *sys = malloc (sizeof (*sys));
139 if (unlikely(sys == NULL))
141 sys->detach = no_detach;
143 uint16_t width = var_InheritInteger (demux, "shm-width");
144 uint16_t height = var_InheritInteger (demux, "shm-height");
147 switch (var_InheritInteger (demux, "shm-depth"))
150 chroma = VLC_CODEC_RGB32; bpp = 32;
153 chroma = VLC_CODEC_RGB24; bpp = 24;
156 chroma = VLC_CODEC_RGB16; bpp = 16;
159 chroma = VLC_CODEC_RGB15; bpp = 16;
162 chroma = VLC_CODEC_RGB8; bpp = 8;
168 sys->length = width * height * (bpp >> 3);
169 if (sys->length == 0)
172 sys->length = (sys->length + pagesize) & ~pagesize; /* pad */
174 char *path = var_InheritString (demux, "shm-file");
177 int fd = vlc_open (path, O_RDONLY);
180 msg_Err (demux, "cannot open file %s: %m", path);
185 void *mem = mmap (NULL, sys->length, PROT_READ, MAP_SHARED, fd, 0);
187 if (mem == MAP_FAILED)
189 msg_Err (demux, "cannot map file %s: %m", path);
195 sys->detach = map_detach;
199 #ifdef HAVE_SYS_SHM_H
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 = sysv_detach;
217 /* Initializes format */
218 float rate = var_InheritFloat (obj, "shm-fps");
222 sys->interval = (float)CLOCK_FREQ / rate;
225 sys->pts = VLC_TS_INVALID;
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 vlc_mutex_init (&sys->lock);
241 if (vlc_timer_create (&sys->timer, Demux, demux))
243 vlc_timer_schedule (sys->timer, false, 1, sys->interval);
246 demux->pf_demux = NULL;
247 demux->pf_control = Control;
260 static void Close (vlc_object_t *obj)
262 demux_t *demux = (demux_t *)obj;
263 demux_sys_t *sys = demux->p_sys;
265 vlc_timer_destroy (sys->timer);
266 vlc_mutex_destroy (&sys->lock);
272 static void map_detach (demux_sys_t *sys)
274 munmap ((void *)sys->addr, sys->length);
277 #ifdef HAVE_SYS_SHM_H
278 static void sysv_detach (demux_sys_t *sys)
284 static void no_detach (demux_sys_t *sys)
292 static int Control (demux_t *demux, int query, va_list args)
294 demux_sys_t *sys = demux->p_sys;
298 case DEMUX_GET_POSITION:
300 float *v = va_arg (args, float *);
305 case DEMUX_GET_LENGTH:
308 int64_t *v = va_arg (args, int64_t *);
313 case DEMUX_GET_PTS_DELAY:
315 int64_t *v = va_arg (args, int64_t *);
316 *v = INT64_C(1000) * var_GetInteger (demux, "live-caching");
320 case DEMUX_CAN_PAUSE:
322 bool *v = (bool *)va_arg (args, bool *);
327 case DEMUX_SET_PAUSE_STATE:
329 bool pausing = va_arg (args, int);
333 vlc_mutex_lock (&sys->lock);
334 sys->pts = VLC_TS_INVALID;
335 es_out_Control (demux->out, ES_OUT_RESET_PCR);
336 vlc_mutex_unlock (&sys->lock);
338 vlc_timer_schedule (sys->timer, false,
339 pausing ? 0 : 1, sys->interval);
343 case DEMUX_CAN_CONTROL_PACE:
344 case DEMUX_CAN_CONTROL_RATE:
347 bool *v = (bool *)va_arg (args, bool *);
358 * Processing callback
360 static void Demux (void *data)
362 demux_t *demux = data;
363 demux_sys_t *sys = demux->p_sys;
366 block_t *block = block_Alloc (sys->length);
370 vlc_memcpy (block->p_buffer, sys->addr, sys->length);
373 vlc_mutex_lock (&sys->lock);
374 if (sys->pts == VLC_TS_INVALID)
376 block->i_pts = block->i_dts = sys->pts;
378 es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
379 es_out_Send (demux->out, sys->es, block);
380 sys->pts += sys->interval;
381 vlc_mutex_unlock (&sys->lock);