]> git.sesse.net Git - vlc/blob - modules/video_filter/osdmenu.c
Send an empty subpicture to remove the OSD menu on the client application when "menu...
[vlc] / modules / video_filter / osdmenu.c
1 /*****************************************************************************
2  * osdmenu.c: osd filter module
3  *****************************************************************************
4  * Copyright (C) 2004-2005 M2X
5  * $Id: osdmenu.c 11131 2005-05-23 11:04:07Z hartman $
6  *
7  * Authors: Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implid warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include <vlc_filter.h>
33 #include <vlc_video.h>
34
35 #include <osd.h>
36 #include <vlc_osd.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41
42 /* FIXME: Future extension make the definition file in XML format. */
43 #define OSD_FILE_TEXT N_("OSD menu configuration file")
44 #define OSD_FILE_LONGTEXT N_( \
45     "An OSD menu configuration file that menu actions with button images" )
46
47 #define OSD_PATH_TEXT N_("Path to OSD menu images")
48 #define OSD_PATH_LONGTEXT N_( \
49     "Specify another path to the OSD menu images. This will override the path as defined in the " \
50     "OSD configuration file." )
51
52 #define POSX_TEXT N_("X coordinate of the OSD menu")
53 #define POSX_LONGTEXT N_("You can move the OSD menu by left-clicking on it." )
54
55 #define POSY_TEXT N_("Y coordinate of the OSD menu")
56 #define POSY_LONGTEXT N_("You can move the OSD menu by left-clicking on it." )
57
58 #define POS_TEXT N_("OSD menu position")
59 #define POS_LONGTEXT N_( \
60   "You can enforce the OSD menu position on the video " \
61   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
62   "also use combinations of these values).")
63
64 #define TIMEOUT_TEXT N_("Timeout of OSD menu")
65 #define TIMEOUT_LONGTEXT N_( \
66     "OSD menu pictures get a default timeout of 15 seconds added to their remaining time." \
67     "This will ensure that they are at least the specified time visible.")
68     
69 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
70 static char *ppsz_pos_descriptions[] =
71 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
72   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
73
74 /* subfilter functions */
75 static int  CreateFilter ( vlc_object_t * );
76 static void DestroyFilter( vlc_object_t * );
77 static subpicture_t *Filter( filter_t *, mtime_t );
78 static int OSDMenuUpdateEvent( vlc_object_t *, char const *,
79                     vlc_value_t, vlc_value_t, void * );                    
80 static int OSDMenuVisibleEvent( vlc_object_t *, char const *,
81                     vlc_value_t, vlc_value_t, void * );
82
83 #define OSD_CFG "osdmenu-"
84
85 #if defined( WIN32 ) || defined( UNDER_CE )
86 #define OSD_DEFAULT_CFG "osdmenu/default.cfg"
87 #else
88 #define OSD_DEFAULT_CFG "share/osdmenu/default.cfg"
89 #endif
90
91 vlc_module_begin();
92     add_integer( OSD_CFG "x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
93     add_integer( OSD_CFG "y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
94     add_integer( OSD_CFG "position", 8, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
95         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
96     add_string( OSD_CFG "file", OSD_DEFAULT_CFG, NULL, OSD_FILE_TEXT, OSD_FILE_LONGTEXT, VLC_FALSE );
97     add_string( OSD_CFG "file-path", NULL, NULL, OSD_PATH_TEXT, OSD_PATH_LONGTEXT, VLC_FALSE );
98     add_integer( OSD_CFG "timeout", 0, NULL, TIMEOUT_TEXT, TIMEOUT_LONGTEXT, VLC_FALSE );
99
100     set_capability( "sub filter", 100 );
101     set_description( N_("On Screen Display menu subfilter") );
102     set_shortname( N_("OSD menu") );
103     add_shortcut( "osdmenu" );
104     set_category( CAT_VIDEO );
105     set_subcategory( SUBCAT_VIDEO_SUBPIC );
106     set_callbacks( CreateFilter, DestroyFilter );
107 vlc_module_end();
108
109 /*****************************************************************************
110  * Sub filter code
111  *****************************************************************************/
112
113 /*****************************************************************************
114  * Local prototypes
115  *****************************************************************************/
116 struct filter_sys_t
117 {
118     vlc_mutex_t  lock;
119
120     int          position;      /* relative positioning of SPU images */
121     mtime_t      i_last_date;   /* last mdate SPU object has been sent to SPU subsytem */
122     int          i_timeout;     /* duration SPU object is valid on the video output in seconds */
123     
124     vlc_bool_t   b_absolute;    /* do we use absolute positioning or relative? */
125     vlc_bool_t   b_update;      /* Update OSD Menu by sending SPU objects */
126     vlc_bool_t   b_visible;     /* OSD Menu is visible */
127     
128     char        *psz_file;      /* OSD Menu configuration file */
129     osd_menu_t  *p_menu;        /* pointer to OSD Menu object */
130 };
131
132 /*****************************************************************************
133  * CreateFilter: Create the filter and open the definition file
134  *****************************************************************************/
135 static int CreateFilter ( vlc_object_t *p_this )
136 {
137     filter_t *p_filter = (filter_t *)p_this;
138     vlc_value_t val;
139     int posx, posy;
140
141     p_filter->p_sys = (filter_sys_t *) malloc( sizeof( filter_sys_t ) );
142     if( !p_filter->p_sys )
143     {
144         msg_Err( p_filter, "out of memory" );
145         return VLC_ENOMEM;
146     }        
147     
148     /* Populating struct */
149     p_filter->p_sys->p_menu = NULL;
150     p_filter->p_sys->psz_file = NULL;
151     
152     vlc_mutex_init( p_filter, &p_filter->p_sys->lock );
153
154     p_filter->p_sys->psz_file = config_GetPsz( p_filter, OSD_CFG "file" );
155     if( p_filter->p_sys->psz_file == NULL || *p_filter->p_sys->psz_file == '\0' ) 
156     {
157         msg_Err( p_filter, "unable to get filename" );
158         goto error;
159     }
160
161     var_Create( p_this, OSD_CFG "position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
162     var_Get( p_this, OSD_CFG "position", &val );
163     p_filter->p_sys->position = val.i_int;
164     var_Create( p_this, OSD_CFG "x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
165     var_Get( p_this, OSD_CFG "x", &val );
166     posx = val.i_int;
167     var_Create( p_this, OSD_CFG "y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
168     var_Get( p_this, OSD_CFG "y", &val );
169     posy = val.i_int;
170     var_Create( p_this, OSD_CFG "timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
171     var_Get( p_this, OSD_CFG "timeout", &val );
172     p_filter->p_sys->i_timeout = val.i_int; /* in seconds */
173
174     /* Load the osd menu subsystem */
175     p_filter->p_sys->p_menu = osd_MenuCreate( p_this, p_filter->p_sys->psz_file );
176     if( p_filter->p_sys->p_menu == NULL )
177         goto error;
178     
179     /* Check if menu position was overridden */
180     p_filter->p_sys->b_absolute = VLC_TRUE;
181     
182     if( posx < 0 || posy < 0)
183     {
184         p_filter->p_sys->b_absolute = VLC_FALSE;
185         p_filter->p_sys->p_menu->i_x = 0;
186         p_filter->p_sys->p_menu->i_y = 0;
187     }
188     else if( posx >= 0 || posy >= 0 )
189     {
190         p_filter->p_sys->p_menu->i_x = posx;
191         p_filter->p_sys->p_menu->i_y = posy;
192     }
193     else if( p_filter->p_sys->p_menu->i_x < 0 || p_filter->p_sys->p_menu->i_y < 0 )
194     {
195         p_filter->p_sys->b_absolute = VLC_FALSE;
196         p_filter->p_sys->p_menu->i_x = 0;
197         p_filter->p_sys->p_menu->i_y = 0;
198     }
199     
200     /* Set up p_filter */
201     p_filter->p_sys->i_last_date = mdate();
202     
203     /* Keep track of OSD Events */
204     p_filter->p_sys->b_update = VLC_FALSE;
205     p_filter->p_sys->b_visible = VLC_FALSE;
206     
207     var_AddCallback( p_filter->p_sys->p_menu, "osd-menu-update", OSDMenuUpdateEvent, p_filter );        
208     var_AddCallback( p_filter->p_sys->p_menu, "osd-menu-visible", OSDMenuVisibleEvent, p_filter );        
209
210     /* Attach subpicture filter callback */
211     p_filter->pf_sub_filter = Filter;
212     
213     es_format_Init( &p_filter->fmt_out, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
214     p_filter->fmt_out.i_priority = 0;
215     
216     msg_Dbg( p_filter, "successfully loaded osdmenu filter" );    
217     return VLC_SUCCESS;
218     
219 error:
220     msg_Err( p_filter, "osdmenu filter discarded" );
221     vlc_mutex_destroy( &p_filter->p_sys->lock );
222     if( p_filter->p_sys->p_menu )
223     {
224         osd_MenuDelete( p_this, p_filter->p_sys->p_menu );
225         p_filter->p_sys->p_menu = NULL;
226     }
227     if( p_filter->p_sys->psz_file ) free( p_filter->p_sys->psz_file );
228     if( p_filter->p_sys ) free( p_filter->p_sys );
229     return VLC_EGENERIC;    
230 }
231
232 /*****************************************************************************
233  * DestroyFilter: Make a clean exit of this plugin
234  *****************************************************************************/
235 static void DestroyFilter( vlc_object_t *p_this )
236 {
237     filter_t     *p_filter = (filter_t*)p_this;
238     filter_sys_t *p_sys = p_filter->p_sys;
239
240     var_Destroy( p_this, OSD_CFG "file" );
241     var_Destroy( p_this, OSD_CFG "x" );
242     var_Destroy( p_this, OSD_CFG "y" );
243     var_Destroy( p_this, OSD_CFG "position" );
244     var_Destroy( p_this, OSD_CFG "timeout" );
245     
246     var_DelCallback( p_sys->p_menu, "osd-menu-update", OSDMenuUpdateEvent, p_filter );        
247     var_DelCallback( p_sys->p_menu, "osd-menu-visible", OSDMenuVisibleEvent, p_filter );        
248
249     osd_MenuDelete( p_filter, p_sys->p_menu );
250     
251     vlc_mutex_destroy( &p_filter->p_sys->lock );    
252     if( p_sys->psz_file) free( p_sys->psz_file );    
253     if( p_sys ) free( p_sys );   
254      
255     msg_Dbg( p_filter, "osdmenu filter destroyed" );    
256 }
257
258 /*****************************************************************************
259  * OSDMenuEvent: callback for OSD Menu events
260  *****************************************************************************/
261 static int OSDMenuVisibleEvent( vlc_object_t *p_this, char const *psz_var,
262                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
263 {
264     filter_t *p_filter = (filter_t *) p_data;
265     
266     p_filter->p_sys->b_visible = VLC_TRUE;        
267     return VLC_SUCCESS;
268 }
269
270 static int OSDMenuUpdateEvent( vlc_object_t *p_this, char const *psz_var,
271                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
272 {
273     filter_t *p_filter = (filter_t *) p_data;
274     
275     p_filter->p_sys->b_update = VLC_TRUE;        
276     return VLC_SUCCESS;
277 }
278
279 #if 0
280 /*****************************************************************************
281  * create_text_region : compose a text region SPU
282  *****************************************************************************/
283 static subpicture_region_t *create_text_region( filter_t *p_filter, subpicture_t *p_spu, 
284     int i_width, int i_height, const char *psz_text )
285 {
286     subpicture_region_t *p_region;
287     video_format_t       fmt;    
288     
289     /* Create new SPU region */
290     memset( &fmt, 0, sizeof(video_format_t) );
291     fmt.i_chroma = VLC_FOURCC( 'T','E','X','T' );
292     fmt.i_aspect = VOUT_ASPECT_FACTOR;
293     fmt.i_sar_num = fmt.i_sar_den = 1;
294     fmt.i_width = fmt.i_visible_width = i_width;
295     fmt.i_height = fmt.i_visible_height = i_height;
296     fmt.i_x_offset = fmt.i_y_offset = 0;
297     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
298     if( !p_region )
299     {
300         msg_Err( p_filter, "cannot allocate another SPU region" );
301         return NULL;
302     }
303     p_region->psz_text = strdup( psz_text );
304     p_region->i_x = 0; 
305     p_region->i_y = 40;
306 #if 1    
307     msg_Dbg( p_filter, "SPU text region position (%d,%d) (%d,%d) [%s]", 
308         p_region->i_x, p_region->i_y, 
309         p_region->fmt.i_width, p_region->fmt.i_height, p_region->psz_text );
310 #endif
311     return p_region;                                
312 }    
313 #endif
314
315 static void osdmenu_RegionPictureRelease( picture_t *p_pic )
316 {
317     if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
318 }
319 /*****************************************************************************
320  * create_picture_region : compose a picture region SPU
321  *****************************************************************************/
322 static subpicture_region_t *create_picture_region( filter_t *p_filter, subpicture_t *p_spu,
323     int i_width, int i_height, picture_t *p_pic )
324 {
325     subpicture_region_t *p_region;
326     video_format_t       fmt;    
327
328     if( !p_spu ) return NULL;
329     
330     /* Create new SPU region */
331     memset( &fmt, 0, sizeof(video_format_t) );
332     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
333     fmt.i_aspect = VOUT_ASPECT_FACTOR;
334     fmt.i_sar_num = fmt.i_sar_den = 1;
335     fmt.i_width = fmt.i_visible_width = i_width;
336     fmt.i_height = fmt.i_visible_height = i_height;
337     fmt.i_x_offset = fmt.i_y_offset = 0;
338     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
339     if( !p_region )
340     {
341         msg_Err( p_filter, "cannot allocate SPU region" );
342         p_filter->pf_sub_buffer_del( p_filter, p_spu );
343         return NULL;
344     }
345     if( p_pic )
346         vout_CopyPicture( p_filter, &p_region->picture, p_pic );
347     else
348     {
349         picture_t *dest_pic = NULL;
350
351         /* Create an empty subpicture */
352         dest_pic = (picture_t*) malloc( sizeof( picture_t ) );
353         if( vout_AllocatePicture( p_filter, dest_pic,
354                             fmt.i_chroma,
355                             fmt.i_width,
356                             fmt.i_height,
357                             fmt.i_aspect )
358             != VLC_SUCCESS )
359         {
360             free( dest_pic );
361             return NULL;
362         }
363         dest_pic->pf_release = osdmenu_RegionPictureRelease;
364         vout_CopyPicture( p_filter, &p_region->picture, dest_pic );
365         dest_pic->pf_release( dest_pic );
366     }
367     p_region->i_x = 0;
368     p_region->i_y = 0;
369
370 #if 0
371     msg_Dbg( p_filter, "SPU picture region position (%d,%d) (%d,%d) [%p]", 
372         p_region->i_x, p_region->i_y, 
373         p_region->fmt.i_width, p_region->fmt.i_height, p_pic );
374 #endif
375     return p_region;
376 }
377
378 /****************************************************************************
379  * Filter: the whole thing
380  ****************************************************************************
381  * This function outputs subpictures at regular time intervals.
382  ****************************************************************************/
383 static subpicture_t *Filter( filter_t *p_filter, mtime_t i_date )
384 {
385     filter_sys_t *p_sys = p_filter->p_sys;    
386     subpicture_t *p_spu;
387     subpicture_region_t *p_region;
388     
389     if( !p_filter->p_sys->b_update &&
390         (p_sys->i_last_date + (mtime_t)(p_sys->i_timeout * 1000000) < i_date) )
391             return NULL;
392
393     p_filter->p_sys->i_last_date = i_date;
394     p_filter->p_sys->b_update = VLC_FALSE; 
395     
396     /* Allocate the subpicture internal data. */
397     p_spu = p_filter->pf_sub_buffer_new( p_filter );
398     if( !p_spu ) return NULL;
399     
400     p_spu->b_absolute = p_sys->b_absolute;    
401     p_spu->i_start = p_sys->i_last_date = i_date; 
402     p_spu->i_stop = (p_sys->i_timeout == 0) ? 0 : i_date + (mtime_t)(p_sys->i_timeout * 1000000);
403     p_spu->b_ephemer = VLC_TRUE;
404     p_spu->b_fade = VLC_TRUE;    
405     p_spu->i_flags = p_sys->position;
406     p_filter->p_sys->b_update = VLC_FALSE; 
407     
408     /* Send an empty subpicture to clear the display
409      * when OSD menu should be hidden and menu picture is not allocated.
410      */
411     if( !p_filter->p_sys->p_menu->p_state->p_pic ||
412         ( p_filter->p_sys->b_visible == VLC_FALSE ) )
413     {
414         /* Create new spu regions and allocate an empty picture in it. */    
415         p_region = create_picture_region( p_filter, p_spu, 
416             p_filter->p_sys->p_menu->p_state->i_width,
417             p_filter->p_sys->p_menu->p_state->i_height, 
418             NULL );
419         /* proper positioning of OSD menu image */
420         p_spu->i_x = p_filter->p_sys->p_menu->p_state->i_x;
421         p_spu->i_y = p_filter->p_sys->p_menu->p_state->i_y;
422         
423         p_spu->p_region = p_region;
424         msg_Dbg( p_filter, "sending empty subpicture." );
425         return p_spu;
426     }
427     
428     /* Create new spu regions */    
429     p_region = create_picture_region( p_filter, p_spu, 
430         p_filter->p_sys->p_menu->p_state->i_width,
431         p_filter->p_sys->p_menu->p_state->i_height, 
432         p_filter->p_sys->p_menu->p_state->p_pic );
433 #if 0
434     p_region->p_next = create_text_region( p_filter, p_spu, 
435         p_filter->p_sys->p_menu->p_state->i_width, p_filter->p_sys->p_menu->p_state->i_height,         
436         p_filter->p_sys->p_menu->p_state->p_visible->psz_action );
437 #endif
438     
439     /* proper positioning of OSD menu image */
440     p_spu->i_x = p_filter->p_sys->p_menu->p_state->i_x;
441     p_spu->i_y = p_filter->p_sys->p_menu->p_state->i_y;
442     
443     p_spu->p_region = p_region;
444     return p_spu;
445 }