]> git.sesse.net Git - vlc/blob - modules/access/shm.c
SHM: set true PTS instead of synthetic one (fixes #7579)
[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 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.
12  *
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.
17  *
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  ****************************************************************************/
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 FPS_TEXT N_("Frame rate")
41 #define FPS_LONGTEXT N_( \
42     "How many times the screen content should be refreshed per second.")
43
44 #define WIDTH_TEXT N_("Frame buffer width")
45 #define WIDTH_LONGTEXT N_( \
46     "Pixel width of the frame buffer")
47
48 #define HEIGHT_TEXT N_("Frame buffer height")
49 #define HEIGHT_LONGTEXT N_( \
50     "Pixel height of the frame buffer")
51
52 #define DEPTH_TEXT N_("Frame buffer depth")
53 #define DEPTH_LONGTEXT N_( \
54     "Pixel depth of the frame buffer")
55
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).")
60
61 #define FILE_TEXT N_("Frame buffer file")
62 #define FILE_LONGTEXT N_( \
63     "Path of the memory mapped file of the frame buffer")
64
65 static int  Open (vlc_object_t *);
66 static void Close (vlc_object_t *);
67
68 static const int depths[] = {
69     8, 15, 16, 24, 32,
70 };
71
72 static const char *const depth_texts[] = {
73     N_("8 bits"), N_("15 bits"), N_("16 bits"), N_("24 bits"), N_("32 bits"),
74 };
75
76 /*
77  * Module descriptor
78  */
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)
86
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 ()
97
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 ()
109
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 *);
117
118 struct demux_sys_t
119 {
120     /* Everything is read-only when timer is armed. */
121     const void  *addr;
122     size_t       length;
123     size_t       size;
124     es_out_id_t *es;
125     mtime_t      interval;
126     vlc_timer_t  timer;
127     void (*detach) (demux_sys_t *);
128 };
129
130 static int Open (vlc_object_t *obj)
131 {
132     demux_t *demux = (demux_t *)obj;
133
134     long pagesize = sysconf (_SC_PAGE_SIZE);
135     if (pagesize == -1)
136         return VLC_EGENERIC;
137     
138     demux_sys_t *sys = malloc (sizeof (*sys));
139     if (unlikely(sys == NULL))
140         return VLC_ENOMEM;
141     sys->detach = no_detach;
142
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"))
148     {
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;
166     }
167
168     sys->length = width * height * (bpp >> 3);
169     if (sys->length == 0)
170         goto error;
171     pagesize--;
172     sys->size = (sys->length + pagesize) & ~pagesize; /* pad */
173
174     char *path = var_InheritString (demux, "shm-file");
175     if (path != NULL)
176     {
177         int fd = vlc_open (path, O_RDONLY);
178         if (fd == -1)
179         {
180             msg_Err (demux, "cannot open file %s: %m", path);
181             free (path);
182             goto error;
183         }
184
185         void *mem = mmap (NULL, sys->size, PROT_READ, MAP_SHARED, fd, 0);
186         close (fd);
187         if (mem == MAP_FAILED)
188         {
189             msg_Err (demux, "cannot map file %s: %m", path);
190             free (path);
191             goto error;
192         }
193         free (path);
194         sys->addr = mem;
195         sys->detach = map_detach;
196     }
197     else
198     {
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);
204
205         if (mem == (const void *)(-1))
206         {
207             msg_Err (demux, "cannot attach segment %d: %m", id);
208             goto error;
209         }
210         sys->addr = mem;
211         sys->detach = sysv_detach;
212 #else
213         goto error;
214 #endif
215     }
216
217     /* Initializes format */
218     float rate = var_InheritFloat (obj, "shm-fps");
219     if (rate <= 0.)
220         goto error;
221
222     sys->interval = (float)CLOCK_FREQ / rate;
223     if (!sys->interval)
224         goto error;
225
226     es_format_t fmt;
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;
235
236     sys->es = es_out_Add (demux->out, &fmt);
237
238     /* Initializes demux */
239     if (vlc_timer_create (&sys->timer, Demux, demux))
240         goto error;
241     vlc_timer_schedule (sys->timer, false, 1, sys->interval);
242
243     demux->p_sys = sys;
244     demux->pf_demux   = NULL;
245     demux->pf_control = Control;
246     return VLC_SUCCESS;
247
248 error:
249     sys->detach (sys);
250     free (sys);
251     return VLC_EGENERIC;
252 }
253
254
255 /**
256  * Releases resources
257  */
258 static void Close (vlc_object_t *obj)
259 {
260     demux_t *demux = (demux_t *)obj;
261     demux_sys_t *sys = demux->p_sys;
262
263     vlc_timer_destroy (sys->timer);
264     sys->detach (sys);
265     free (sys);
266 }
267
268
269 static void map_detach (demux_sys_t *sys)
270 {
271     munmap ((void *)sys->addr, sys->size);
272 }
273
274 #ifdef HAVE_SYS_SHM_H
275 static void sysv_detach (demux_sys_t *sys)
276 {
277     shmdt (sys->addr);
278 }
279 #endif
280
281 static void no_detach (demux_sys_t *sys)
282 {
283     (void) sys;
284 }
285
286 /**
287  * Control callback
288  */
289 static int Control (demux_t *demux, int query, va_list args)
290 {
291     demux_sys_t *sys = demux->p_sys;
292
293     switch (query)
294     {
295         case DEMUX_GET_POSITION:
296         {
297             float *v = va_arg (args, float *);
298             *v = 0.;
299             return VLC_SUCCESS;
300         }
301
302         case DEMUX_GET_LENGTH:
303         case DEMUX_GET_TIME:
304         {
305             int64_t *v = va_arg (args, int64_t *);
306             *v = 0;
307             return VLC_SUCCESS;
308         }
309
310         case DEMUX_GET_PTS_DELAY:
311         {
312             int64_t *v = va_arg (args, int64_t *);
313             *v = INT64_C(1000) * var_GetInteger (demux, "live-caching");
314             return VLC_SUCCESS;
315         }
316
317         case DEMUX_CAN_PAUSE:
318         {
319             bool *v = (bool *)va_arg (args, bool *);
320             *v = true;
321             return VLC_SUCCESS;
322         }
323
324         case DEMUX_SET_PAUSE_STATE:
325         {
326             bool pausing = va_arg (args, int);
327
328             vlc_timer_schedule (sys->timer, false, !pausing, sys->interval);
329             return VLC_SUCCESS;
330         }
331
332         case DEMUX_CAN_CONTROL_PACE:
333         case DEMUX_CAN_CONTROL_RATE:
334         case DEMUX_CAN_SEEK:
335         {
336             bool *v = (bool *)va_arg (args, bool *);
337             *v = false;
338             return VLC_SUCCESS;
339         }
340     }
341
342     return VLC_EGENERIC;
343 }
344
345
346 /**
347  * Processing callback
348  */
349 static void Demux (void *data)
350 {
351     demux_t *demux = data;
352     demux_sys_t *sys = demux->p_sys;
353
354     /* Copy frame */
355     block_t *block = block_Alloc (sys->length);
356     if (block == NULL)
357         return;
358     memcpy (block->p_buffer, sys->addr, sys->length);
359     block->i_pts = block->i_dts = mdate ();
360
361     /* Send block */
362     es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
363     es_out_Send (demux->out, sys->es, block);
364 }