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 *);
120 /* Everything is read-only when timer is armed. */
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->size = (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->size, 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;
227 es_format_Init (&fmt, VIDEO_ES, chroma);
228 fmt.video.i_chroma = chroma;
229 fmt.video.i_bits_per_pixel = bpp;
230 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
231 fmt.video.i_frame_rate = 1000 * rate;
232 fmt.video.i_frame_rate_base = 1000;
233 fmt.video.i_visible_width = fmt.video.i_width = width;
234 fmt.video.i_visible_height = fmt.video.i_height = height;
236 sys->es = es_out_Add (demux->out, &fmt);
238 /* Initializes demux */
239 if (vlc_timer_create (&sys->timer, Demux, demux))
241 vlc_timer_schedule (sys->timer, false, 1, sys->interval);
244 demux->pf_demux = NULL;
245 demux->pf_control = Control;
258 static void Close (vlc_object_t *obj)
260 demux_t *demux = (demux_t *)obj;
261 demux_sys_t *sys = demux->p_sys;
263 vlc_timer_destroy (sys->timer);
269 static void map_detach (demux_sys_t *sys)
271 munmap ((void *)sys->addr, sys->size);
274 #ifdef HAVE_SYS_SHM_H
275 static void sysv_detach (demux_sys_t *sys)
281 static void no_detach (demux_sys_t *sys)
289 static int Control (demux_t *demux, int query, va_list args)
291 demux_sys_t *sys = demux->p_sys;
295 case DEMUX_GET_POSITION:
297 float *v = va_arg (args, float *);
302 case DEMUX_GET_LENGTH:
305 int64_t *v = va_arg (args, int64_t *);
310 case DEMUX_GET_PTS_DELAY:
312 int64_t *v = va_arg (args, int64_t *);
313 *v = INT64_C(1000) * var_GetInteger (demux, "live-caching");
317 case DEMUX_CAN_PAUSE:
319 bool *v = (bool *)va_arg (args, bool *);
324 case DEMUX_SET_PAUSE_STATE:
326 bool pausing = va_arg (args, int);
328 vlc_timer_schedule (sys->timer, false, !pausing, sys->interval);
332 case DEMUX_CAN_CONTROL_PACE:
333 case DEMUX_CAN_CONTROL_RATE:
336 bool *v = (bool *)va_arg (args, bool *);
347 * Processing callback
349 static void Demux (void *data)
351 demux_t *demux = data;
352 demux_sys_t *sys = demux->p_sys;
355 block_t *block = block_Alloc (sys->length);
358 memcpy (block->p_buffer, sys->addr, sys->length);
359 block->i_pts = block->i_dts = mdate ();
362 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
363 es_out_Send (demux->out, sys->es, block);