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 ****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
36 #include <vlc_plugin.h>
38 #define CACHING_TEXT N_("Caching value in ms")
39 #define CACHING_LONGTEXT N_( \
40 "Caching value for frame buffer capture. " \
41 "This value should be set in milliseconds.")
43 #define FPS_TEXT N_("Frame rate")
44 #define FPS_LONGTEXT N_( \
45 "How many times the screen content should be refreshed per second.")
47 #define WIDTH_TEXT N_("Frame buffer width")
48 #define WIDTH_LONGTEXT N_( \
49 "Pixel width of the frame buffer")
51 #define HEIGHT_TEXT N_("Frame buffer height")
52 #define HEIGHT_LONGTEXT N_( \
53 "Pixel height of the frame buffer")
55 #define DEPTH_TEXT N_("Frame buffer depth")
56 #define DEPTH_LONGTEXT N_( \
57 "Pixel depth of the frame buffer")
59 #define ID_TEXT N_("Frame buffer segment ID")
60 #define ID_LONGTEXT N_( \
61 "System V shared memory segment ID of the frame buffer " \
62 "(this is ignored if --shm-file is specified).")
64 #define FILE_TEXT N_("Frame buffer file")
65 #define FILE_LONGTEXT N_( \
66 "Path of the memory mapped file of the frame buffer")
68 static int Open (vlc_object_t *);
69 static void Close (vlc_object_t *);
71 static const int depths[] = {
75 static const char *const depth_texts[] = {
76 N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
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_integer ("shm-caching", DEFAULT_PTS_DELAY * 1000 / CLOCK_FREQ,
91 CACHING_TEXT, CACHING_LONGTEXT, true)
92 add_float ("shm-fps", 10.0, FPS_TEXT, FPS_LONGTEXT, true)
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)
99 add_integer ("shm-depth", 32, DEPTH_TEXT, DEPTH_LONGTEXT, true)
100 change_integer_list (depths, depth_texts)
103 /* We need to "trust" the memory segment. If it were shrunk while we copy
104 * its content our process may crash - or worse. So we pass the shared
105 * memory location via an unsafe variable rather than the URL. */
106 add_string ("shm-file", NULL, FILE_TEXT, FILE_LONGTEXT, false)
108 add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
114 static void Demux (void *);
115 static int Control (demux_t *, int, va_list);
116 static void map_detach (demux_sys_t *);
117 static void sysv_detach (demux_sys_t *);
118 static void no_detach (demux_sys_t *);
125 mtime_t pts, interval;
126 /* pts is protected by the lock. The rest is read-only. */
129 void (*detach) (demux_sys_t *);
132 static int Open (vlc_object_t *obj)
134 demux_t *demux = (demux_t *)obj;
136 long pagesize = sysconf (_SC_PAGE_SIZE);
140 demux_sys_t *sys = malloc (sizeof (*sys));
141 if (unlikely(sys == NULL))
143 sys->detach = no_detach;
145 uint16_t width = var_InheritInteger (demux, "shm-width");
146 uint16_t height = var_InheritInteger (demux, "shm-height");
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;
170 sys->length = width * height * (bpp >> 3);
171 if (sys->length == 0)
174 sys->length = (sys->length + pagesize) & ~pagesize; /* pad */
176 char *path = var_InheritString (demux, "shm-file");
179 int fd = vlc_open (path, O_RDONLY);
182 msg_Err (demux, "cannot open file %s: %m", path);
187 void *mem = mmap (NULL, sys->length, PROT_READ, MAP_SHARED, fd, 0);
189 if (mem == MAP_FAILED)
191 msg_Err (demux, "cannot map file %s: %m", path);
197 sys->detach = map_detach;
201 int id = var_InheritInteger (demux, "shm-id");
202 if (id == IPC_PRIVATE)
204 void *mem = shmat (id, NULL, SHM_RDONLY);
206 if (mem == (const void *)(-1))
208 msg_Err (demux, "cannot attach segment %d: %m", id);
212 sys->detach = sysv_detach;
215 /* Initializes format */
216 float rate = var_InheritFloat (obj, "shm-fps");
220 sys->interval = (float)CLOCK_FREQ / rate;
223 sys->pts = VLC_TS_INVALID;
226 es_format_Init (&fmt, VIDEO_ES, chroma);
227 fmt.video.i_chroma = chroma;
228 fmt.video.i_bits_per_pixel = bpp;
229 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
230 fmt.video.i_frame_rate = 1000 * rate;
231 fmt.video.i_frame_rate_base = 1000;
232 fmt.video.i_visible_width = fmt.video.i_width = width;
233 fmt.video.i_visible_height = fmt.video.i_height = height;
235 sys->es = es_out_Add (demux->out, &fmt);
237 /* Initializes demux */
238 var_Create (obj, "shm-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
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);
278 static void sysv_detach (demux_sys_t *sys)
283 static void no_detach (demux_sys_t *sys)
291 static int Control (demux_t *demux, int query, va_list args)
293 demux_sys_t *sys = demux->p_sys;
297 case DEMUX_GET_POSITION:
299 float *v = va_arg (args, float *);
304 case DEMUX_GET_LENGTH:
307 int64_t *v = va_arg (args, int64_t *);
312 case DEMUX_GET_PTS_DELAY:
314 int64_t *v = va_arg (args, int64_t *);
315 *v = var_GetInteger (demux, "shm-caching") * UINT64_C(1000);
319 case DEMUX_CAN_PAUSE:
321 bool *v = (bool *)va_arg (args, bool *);
326 case DEMUX_SET_PAUSE_STATE:
328 bool pausing = va_arg (args, int);
332 vlc_mutex_lock (&sys->lock);
333 sys->pts = VLC_TS_INVALID;
334 es_out_Control (demux->out, ES_OUT_RESET_PCR);
335 vlc_mutex_unlock (&sys->lock);
337 vlc_timer_schedule (sys->timer, false,
338 pausing ? 0 : 1, sys->interval);
342 case DEMUX_CAN_CONTROL_PACE:
343 case DEMUX_CAN_CONTROL_RATE:
346 bool *v = (bool *)va_arg (args, bool *);
357 * Processing callback
359 static void Demux (void *data)
361 demux_t *demux = data;
362 demux_sys_t *sys = demux->p_sys;
365 block_t *block = block_Alloc (sys->length);
369 vlc_memcpy (block->p_buffer, sys->addr, sys->length);
372 vlc_mutex_lock (&sys->lock);
373 if (sys->pts == VLC_TS_INVALID)
375 block->i_pts = block->i_dts = sys->pts;
377 es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
378 es_out_Send (demux->out, sys->es, block);
379 sys->pts += sys->interval;
380 vlc_mutex_unlock (&sys->lock);