]> git.sesse.net Git - vlc/blob - modules/access/shm.c
SRTP: remove dedicated gcrypt initialization, use VLC one
[vlc] / modules / access / shm.c
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
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
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>
34
35 #include <vlc_common.h>
36 #include <vlc_demux.h>
37 #include <vlc_fs.h>
38 #include <vlc_plugin.h>
39
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.")
44
45 #define FPS_TEXT N_("Frame rate")
46 #define FPS_LONGTEXT N_( \
47     "How many times the screen content should be refreshed per second.")
48
49 #define WIDTH_TEXT N_("Frame buffer width")
50 #define WIDTH_LONGTEXT N_( \
51     "Pixel width of the frame buffer")
52
53 #define HEIGHT_TEXT N_("Frame buffer height")
54 #define HEIGHT_LONGTEXT N_( \
55     "Pixel height of the frame buffer")
56
57 #define DEPTH_TEXT N_("Frame buffer depth")
58 #define DEPTH_LONGTEXT N_( \
59     "Pixel depth of the frame buffer")
60
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).")
65
66 #define FILE_TEXT N_("Frame buffer file")
67 #define FILE_LONGTEXT N_( \
68     "Path of the memory mapped file of the frame buffer")
69
70 static int  Open (vlc_object_t *);
71 static void Close (vlc_object_t *);
72
73 static const int depths[] = {
74     8, 15, 16, 24, 32,
75 };
76
77 static const char *const depth_texts[] = {
78     N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
79 };
80
81 /*
82  * Module descriptor
83  */
84 vlc_module_begin ()
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)
91
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)
97         change_safe ()
98     add_integer ("shm-height", 480, HEIGHT_TEXT, HEIGHT_LONGTEXT, false)
99         change_integer_range (0, 65535)
100         change_safe ()
101     add_integer ("shm-depth", 32, DEPTH_TEXT, DEPTH_LONGTEXT, true)
102         change_integer_list (depths, depth_texts)
103         change_safe ()
104
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)
109         change_volatile ()
110 #ifdef HAVE_SYS_SHM_H
111     add_integer ("shm-id", (int64_t)IPC_PRIVATE, ID_TEXT, ID_LONGTEXT, false)
112         change_volatile ()
113 #endif
114     add_shortcut ("shm")
115 vlc_module_end ()
116
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 *);
122 #endif
123 static void no_detach (demux_sys_t *);
124
125 struct demux_sys_t
126 {
127     const void  *addr;
128     size_t       length;
129     es_out_id_t *es;
130     mtime_t      pts, interval;
131     /* pts is protected by the lock. The rest is read-only. */
132     vlc_mutex_t  lock;
133     vlc_timer_t  timer;
134     void (*detach) (demux_sys_t *);
135 };
136
137 static int Open (vlc_object_t *obj)
138 {
139     demux_t *demux = (demux_t *)obj;
140
141     long pagesize = sysconf (_SC_PAGE_SIZE);
142     if (pagesize == -1)
143         return VLC_EGENERIC;
144     
145     demux_sys_t *sys = malloc (sizeof (*sys));
146     if (unlikely(sys == NULL))
147         return VLC_ENOMEM;
148     sys->detach = no_detach;
149
150     uint16_t width = var_InheritInteger (demux, "shm-width");
151     uint16_t height = var_InheritInteger (demux, "shm-height");
152     uint32_t chroma;
153     uint8_t bpp;
154     switch (var_InheritInteger (demux, "shm-depth"))
155     {
156         case 32:
157             chroma = VLC_CODEC_RGB32; bpp = 32;
158             break;
159         case 24:
160             chroma = VLC_CODEC_RGB24; bpp = 24;
161             break;
162         case 16:
163             chroma = VLC_CODEC_RGB16; bpp = 16;
164             break;
165         case 15:
166             chroma = VLC_CODEC_RGB15; bpp = 16;
167             break;
168         case 8:
169             chroma = VLC_CODEC_RGB8; bpp = 8;
170             break;
171         default:
172             goto error;
173     }
174
175     sys->length = width * height * (bpp >> 3);
176     if (sys->length == 0)
177         goto error;
178     pagesize--;
179     sys->length = (sys->length + pagesize) & ~pagesize; /* pad */
180
181     char *path = var_InheritString (demux, "shm-file");
182     if (path != NULL)
183     {
184         int fd = vlc_open (path, O_RDONLY);
185         if (fd == -1)
186         {
187             msg_Err (demux, "cannot open file %s: %m", path);
188             free (path);
189             goto error;
190         }
191
192         void *mem = mmap (NULL, sys->length, PROT_READ, MAP_SHARED, fd, 0);
193         close (fd);
194         if (mem == MAP_FAILED)
195         {
196             msg_Err (demux, "cannot map file %s: %m", path);
197             free (path);
198             goto error;
199         }
200         free (path);
201         sys->addr = mem;
202         sys->detach = map_detach;
203     }
204     else
205     {
206 #ifdef HAVE_SYS_SHM_H
207         int id = var_InheritInteger (demux, "shm-id");
208         if (id == IPC_PRIVATE)
209             goto error;
210         void *mem = shmat (id, NULL, SHM_RDONLY);
211
212         if (mem == (const void *)(-1))
213         {
214             msg_Err (demux, "cannot attach segment %d: %m", id);
215             goto error;
216         }
217         sys->addr = mem;
218         sys->detach = sysv_detach;
219 #else
220         goto error;
221 #endif
222     }
223
224     /* Initializes format */
225     float rate = var_InheritFloat (obj, "shm-fps");
226     if (rate <= 0.)
227         goto error;
228
229     sys->interval = (float)CLOCK_FREQ / rate;
230     if (!sys->interval)
231         goto error;
232     sys->pts = VLC_TS_INVALID;
233
234     es_format_t fmt;
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;
243
244     sys->es = es_out_Add (demux->out, &fmt);
245
246     /* Initializes demux */
247     var_Create (obj, "shm-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
248
249     vlc_mutex_init (&sys->lock);
250     if (vlc_timer_create (&sys->timer, Demux, demux))
251         goto error;
252     vlc_timer_schedule (sys->timer, false, 1, sys->interval);
253
254     demux->p_sys = sys;
255     demux->pf_demux   = NULL;
256     demux->pf_control = Control;
257     return VLC_SUCCESS;
258
259 error:
260     sys->detach (sys);
261     free (sys);
262     return VLC_EGENERIC;
263 }
264
265
266 /**
267  * Releases resources
268  */
269 static void Close (vlc_object_t *obj)
270 {
271     demux_t *demux = (demux_t *)obj;
272     demux_sys_t *sys = demux->p_sys;
273
274     vlc_timer_destroy (sys->timer);
275     vlc_mutex_destroy (&sys->lock);
276     sys->detach (sys);
277     free (sys);
278 }
279
280
281 static void map_detach (demux_sys_t *sys)
282 {
283     munmap ((void *)sys->addr, sys->length);
284 }
285
286 #ifdef HAVE_SYS_SHM_H
287 static void sysv_detach (demux_sys_t *sys)
288 {
289     shmdt (sys->addr);
290 }
291 #endif
292
293 static void no_detach (demux_sys_t *sys)
294 {
295     (void) sys;
296 }
297
298 /**
299  * Control callback
300  */
301 static int Control (demux_t *demux, int query, va_list args)
302 {
303     demux_sys_t *sys = demux->p_sys;
304
305     switch (query)
306     {
307         case DEMUX_GET_POSITION:
308         {
309             float *v = va_arg (args, float *);
310             *v = 0.;
311             return VLC_SUCCESS;
312         }
313
314         case DEMUX_GET_LENGTH:
315         case DEMUX_GET_TIME:
316         {
317             int64_t *v = va_arg (args, int64_t *);
318             *v = 0;
319             return VLC_SUCCESS;
320         }
321
322         case DEMUX_GET_PTS_DELAY:
323         {
324             int64_t *v = va_arg (args, int64_t *);
325             *v = var_GetInteger (demux, "shm-caching") * UINT64_C(1000);
326             return VLC_SUCCESS;
327         }
328
329         case DEMUX_CAN_PAUSE:
330         {
331             bool *v = (bool *)va_arg (args, bool *);
332             *v = true;
333             return VLC_SUCCESS;
334         }
335
336         case DEMUX_SET_PAUSE_STATE:
337         {
338             bool pausing = va_arg (args, int);
339
340             if (!pausing)
341             {
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);
346             }
347             vlc_timer_schedule (sys->timer, false,
348                                 pausing ? 0 : 1, sys->interval);
349             return VLC_SUCCESS;
350         }
351
352         case DEMUX_CAN_CONTROL_PACE:
353         case DEMUX_CAN_CONTROL_RATE:
354         case DEMUX_CAN_SEEK:
355         {
356             bool *v = (bool *)va_arg (args, bool *);
357             *v = false;
358             return VLC_SUCCESS;
359         }
360     }
361
362     return VLC_EGENERIC;
363 }
364
365
366 /**
367  * Processing callback
368  */
369 static void Demux (void *data)
370 {
371     demux_t *demux = data;
372     demux_sys_t *sys = demux->p_sys;
373
374     /* Copy frame */
375     block_t *block = block_Alloc (sys->length);
376     if (block == NULL)
377         return;
378
379     vlc_memcpy (block->p_buffer, sys->addr, sys->length);
380
381     /* Send block */
382     vlc_mutex_lock (&sys->lock);
383     if (sys->pts == VLC_TS_INVALID)
384         sys->pts = mdate ();
385     block->i_pts = block->i_dts = sys->pts;
386
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);
391 }