Qt: add asserts around p_input access
[vlc.git] / modules / access / shm.c
blob24257ceec16feb381a2c7c934ba71ec97b3bd583
1 /**
2 * @file shm.c
3 * @brief Shared memory frame buffer capture module for VLC media player
4 */
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 ****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <stdarg.h>
28 #include <fcntl.h>
29 #ifdef HAVE_SYS_SHM_H
30 # include <sys/ipc.h>
31 # include <sys/shm.h>
32 #endif
33 #include <sys/mman.h>
35 #include <vlc_common.h>
36 #include <vlc_demux.h>
37 #include <vlc_fs.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[] = {
69 8, 15, 16, 24, 32,
72 static const char *const depth_texts[] = {
73 N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
77 * Module descriptor
79 vlc_module_begin ()
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)
90 change_safe ()
91 add_integer ("shm-height", 480, HEIGHT_TEXT, HEIGHT_LONGTEXT, false)
92 change_integer_range (0, 65535)
93 change_safe ()
94 add_integer ("shm-depth", 32, DEPTH_TEXT, DEPTH_LONGTEXT, true)
95 change_integer_list (depths, depth_texts)
96 change_safe ()
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)
102 change_volatile ()
103 #ifdef HAVE_SYS_SHM_H
104 add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
105 change_volatile ()
106 #endif
107 add_shortcut ("shm")
108 vlc_module_end ()
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 *);
115 #endif
116 static void no_detach (demux_sys_t *);
118 struct demux_sys_t
120 const void *addr;
121 size_t length;
122 es_out_id_t *es;
123 mtime_t pts, interval;
124 /* pts is protected by the lock. The rest is read-only. */
125 vlc_mutex_t lock;
126 vlc_timer_t timer;
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);
135 if (pagesize == -1)
136 return VLC_EGENERIC;
138 demux_sys_t *sys = malloc (sizeof (*sys));
139 if (unlikely(sys == NULL))
140 return VLC_ENOMEM;
141 sys->detach = no_detach;
143 uint16_t width = var_InheritInteger (demux, "shm-width");
144 uint16_t height = var_InheritInteger (demux, "shm-height");
145 uint32_t chroma;
146 uint8_t bpp;
147 switch (var_InheritInteger (demux, "shm-depth"))
149 case 32:
150 chroma = VLC_CODEC_RGB32; bpp = 32;
151 break;
152 case 24:
153 chroma = VLC_CODEC_RGB24; bpp = 24;
154 break;
155 case 16:
156 chroma = VLC_CODEC_RGB16; bpp = 16;
157 break;
158 case 15:
159 chroma = VLC_CODEC_RGB15; bpp = 16;
160 break;
161 case 8:
162 chroma = VLC_CODEC_RGB8; bpp = 8;
163 break;
164 default:
165 goto error;
168 sys->length = width * height * (bpp >> 3);
169 if (sys->length == 0)
170 goto error;
171 pagesize--;
172 sys->length = (sys->length + pagesize) & ~pagesize; /* pad */
174 char *path = var_InheritString (demux, "shm-file");
175 if (path != NULL)
177 int fd = vlc_open (path, O_RDONLY);
178 if (fd == -1)
180 msg_Err (demux, "cannot open file %s: %m", path);
181 free (path);
182 goto error;
185 void *mem = mmap (NULL, sys->length, PROT_READ, MAP_SHARED, fd, 0);
186 close (fd);
187 if (mem == MAP_FAILED)
189 msg_Err (demux, "cannot map file %s: %m", path);
190 free (path);
191 goto error;
193 free (path);
194 sys->addr = mem;
195 sys->detach = map_detach;
197 else
199 #ifdef HAVE_SYS_SHM_H
200 int id = var_InheritInteger (demux, "shm-id");
201 if (id == IPC_PRIVATE)
202 goto error;
203 void *mem = shmat (id, NULL, SHM_RDONLY);
205 if (mem == (const void *)(-1))
207 msg_Err (demux, "cannot attach segment %d: %m", id);
208 goto error;
210 sys->addr = mem;
211 sys->detach = sysv_detach;
212 #else
213 goto error;
214 #endif
217 /* Initializes format */
218 float rate = var_InheritFloat (obj, "shm-fps");
219 if (rate <= 0.)
220 goto error;
222 sys->interval = (float)CLOCK_FREQ / rate;
223 if (!sys->interval)
224 goto error;
225 sys->pts = VLC_TS_INVALID;
227 es_format_t fmt;
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))
242 goto error;
243 vlc_timer_schedule (sys->timer, false, 1, sys->interval);
245 demux->p_sys = sys;
246 demux->pf_demux = NULL;
247 demux->pf_control = Control;
248 return VLC_SUCCESS;
250 error:
251 sys->detach (sys);
252 free (sys);
253 return VLC_EGENERIC;
258 * Releases resources
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);
267 sys->detach (sys);
268 free (sys);
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)
280 shmdt (sys->addr);
282 #endif
284 static void no_detach (demux_sys_t *sys)
286 (void) sys;
290 * Control callback
292 static int Control (demux_t *demux, int query, va_list args)
294 demux_sys_t *sys = demux->p_sys;
296 switch (query)
298 case DEMUX_GET_POSITION:
300 float *v = va_arg (args, float *);
301 *v = 0.;
302 return VLC_SUCCESS;
305 case DEMUX_GET_LENGTH:
306 case DEMUX_GET_TIME:
308 int64_t *v = va_arg (args, int64_t *);
309 *v = 0;
310 return VLC_SUCCESS;
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");
317 return VLC_SUCCESS;
320 case DEMUX_CAN_PAUSE:
322 bool *v = (bool *)va_arg (args, bool *);
323 *v = true;
324 return VLC_SUCCESS;
327 case DEMUX_SET_PAUSE_STATE:
329 bool pausing = va_arg (args, int);
331 if (!pausing)
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);
340 return VLC_SUCCESS;
343 case DEMUX_CAN_CONTROL_PACE:
344 case DEMUX_CAN_CONTROL_RATE:
345 case DEMUX_CAN_SEEK:
347 bool *v = (bool *)va_arg (args, bool *);
348 *v = false;
349 return VLC_SUCCESS;
353 return VLC_EGENERIC;
358 * Processing callback
360 static void Demux (void *data)
362 demux_t *demux = data;
363 demux_sys_t *sys = demux->p_sys;
365 /* Copy frame */
366 block_t *block = block_Alloc (sys->length);
367 if (block == NULL)
368 return;
370 vlc_memcpy (block->p_buffer, sys->addr, sys->length);
372 /* Send block */
373 vlc_mutex_lock (&sys->lock);
374 if (sys->pts == VLC_TS_INVALID)
375 sys->pts = mdate ();
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);