1 /*****************************************************************************
2 * dynamicoverlay.c : dynamic overlay plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
7 * Author: Søren Bøg <avacore@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
44 #include "dynamicoverlay.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Create( vlc_object_t * );
50 static void Destroy( vlc_object_t * );
51 static subpicture_t *Filter( filter_t *, mtime_t );
53 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
54 vlc_value_t oldval, vlc_value_t newval,
57 /*****************************************************************************
59 *****************************************************************************/
61 #define INPUT_TEXT N_("Input FIFO")
62 #define INPUT_LONGTEXT N_("FIFO which will be read for commands")
64 #define OUTPUT_TEXT N_("Output FIFO")
65 #define OUTPUT_LONGTEXT N_("FIFO which will be written to for responses")
68 set_description( N_("Dynamic video overlay") )
69 set_shortname( N_("Overlay" ))
70 set_category( CAT_VIDEO )
71 set_subcategory( SUBCAT_VIDEO_VFILTER )
72 set_capability( "sub source", 0 )
74 add_loadfile( "overlay-input", NULL, INPUT_TEXT, INPUT_LONGTEXT,
76 /* Note: add_loadfile as O_WRONLY w/o O_CREAT, i.e. FIFO must exist */
77 add_loadfile( "overlay-output", NULL, OUTPUT_TEXT, OUTPUT_LONGTEXT,
80 add_shortcut( "overlay" )
81 set_callbacks( Create, Destroy )
84 static const char *const ppsz_filter_options[] = {
85 "input", "output", NULL
88 /*****************************************************************************
89 * Create: allocates adjust video thread output method
90 *****************************************************************************
91 * This function allocates and initializes a adjust vout method.
92 *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
95 filter_t *p_filter = (filter_t *)p_this;
98 /* Allocate structure */
99 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
100 if( p_filter->p_sys == NULL )
102 p_sys = p_filter->p_sys;
104 BufferInit( &p_sys->input );
105 BufferInit( &p_sys->output );
106 QueueInit( &p_sys->atomic );
107 QueueInit( &p_sys->pending );
108 QueueInit( &p_sys->processed );
109 do_ListInit( &p_sys->overlays );
111 p_sys->i_inputfd = -1;
112 p_sys->i_outputfd = -1;
113 p_sys->b_updated = true;
114 p_sys->b_atomic = false;
115 vlc_mutex_init( &p_sys->lock );
117 p_filter->pf_sub_source = Filter;
119 config_ChainParse( p_filter, "overlay-", ppsz_filter_options,
122 p_sys->psz_inputfile = var_CreateGetStringCommand( p_filter,
124 p_sys->psz_outputfile = var_CreateGetStringCommand( p_filter,
127 var_AddCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
128 var_AddCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
130 RegisterCommand( p_filter );
134 /*****************************************************************************
135 * Destroy: destroy adjust video thread output method
136 *****************************************************************************
137 * Terminate an output method created by adjustCreateOutputMethod
138 *****************************************************************************/
139 static void Destroy( vlc_object_t *p_this )
141 filter_t *p_filter = (filter_t *)p_this;
142 filter_sys_t *p_sys = p_filter->p_sys;
144 BufferDestroy( &p_sys->input );
145 BufferDestroy( &p_sys->output );
146 QueueDestroy( &p_sys->atomic );
147 QueueDestroy( &p_sys->pending );
148 QueueDestroy( &p_sys->processed );
149 do_ListDestroy( &p_sys->overlays );
150 UnregisterCommand( p_filter );
152 var_DelCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
153 var_DelCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
155 vlc_mutex_destroy( &p_sys->lock );
156 free( p_sys->psz_inputfile );
157 free( p_sys->psz_outputfile );
161 /*****************************************************************************
162 * Render: displays previously rendered output
163 *****************************************************************************
164 * This function send the currently rendered image to adjust modified image,
165 * waits until it is displayed and switch the two rendering buffers, preparing
167 *****************************************************************************/
168 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
170 filter_sys_t *p_sys = p_filter->p_sys;
172 /* We might need to open these at any time. */
173 vlc_mutex_lock( &p_sys->lock );
174 if( p_sys->i_inputfd == -1 )
176 p_sys->i_inputfd = vlc_open( p_sys->psz_inputfile, O_RDONLY | O_NONBLOCK );
177 if( p_sys->i_inputfd == -1 )
179 msg_Warn( p_filter, "Failed to grab input file: %s (%m)",
180 p_sys->psz_inputfile );
184 msg_Info( p_filter, "Grabbed input file: %s",
185 p_sys->psz_inputfile );
189 if( p_sys->i_outputfd == -1 )
191 p_sys->i_outputfd = vlc_open( p_sys->psz_outputfile,
192 O_WRONLY | O_NONBLOCK );
193 if( p_sys->i_outputfd == -1 )
197 msg_Warn( p_filter, "Failed to grab output file: %s (%m)",
198 p_sys->psz_outputfile );
203 msg_Info( p_filter, "Grabbed output file: %s",
204 p_sys->psz_outputfile );
207 vlc_mutex_unlock( &p_sys->lock );
209 /* Read any waiting commands */
210 if( p_sys->i_inputfd != -1 )
213 ssize_t i_len = read( p_sys->i_inputfd, p_buffer, 1024 );
216 /* We hit an error */
217 if( errno != EAGAIN )
219 msg_Warn( p_filter, "Error on input file: %m" );
220 close( p_sys->i_inputfd );
221 p_sys->i_inputfd = -1;
224 else if( i_len == 0 )
226 /* We hit the end-of-file */
230 BufferAdd( &p_sys->input, p_buffer, i_len );
234 /* Parse any complete commands */
236 while( ( p_end = memchr( p_sys->input.p_begin, '\n',
237 p_sys->input.i_length ) ) )
239 commanddesc_t *p_cur = NULL;
240 bool b_found = false;
244 p_cmd = BufferGetToken( &p_sys->input );
246 msg_Info( p_filter, "Search command: %s", p_cmd );
247 for( i_index = 0; i_index < p_sys->i_commands; i_index++ )
249 p_cur = p_sys->pp_commands[i_index];
250 if( !strncmp( p_cur->psz_command, p_cmd, strlen(p_cur->psz_command) ) )
252 p_cmd[strlen(p_cur->psz_command)] = '\0';
260 /* No matching command */
261 msg_Err( p_filter, "Got invalid command: %s", p_cmd );
262 BufferPrintf( &p_sys->output, "FAILURE: %d Invalid Command\n", VLC_EGENERIC );
266 msg_Info( p_filter, "Got valid command: %s", p_cmd );
268 command_t *p_cmddesc = malloc( sizeof( command_t ) );
272 p_cmd = p_cmd + strlen(p_cur->psz_command) +1;
273 p_cmddesc->p_command = p_cur;
274 p_cmddesc->p_command->pf_parser( p_cmd, p_end,
275 &p_cmddesc->params );
277 if( p_cmddesc->p_command->b_atomic && p_sys->b_atomic )
278 QueueEnqueue( &p_sys->atomic, p_cmddesc );
280 QueueEnqueue( &p_sys->pending, p_cmddesc );
283 BufferDel( &p_sys->input, p_end - p_sys->input.p_begin + 1 );
286 /* Process any pending commands */
287 command_t *p_command = NULL;
288 while( (p_command = QueueDequeue( &p_sys->pending )) )
290 p_command->i_status =
291 p_command->p_command->pf_execute( p_filter, &p_command->params,
292 &p_command->results );
293 QueueEnqueue( &p_sys->processed, p_command );
296 /* Output any processed commands */
297 while( (p_command = QueueDequeue( &p_sys->processed )) )
299 if( p_command->i_status == VLC_SUCCESS )
301 const char *psz_success = "SUCCESS:";
302 const char *psz_nl = "\n";
303 BufferAdd( &p_sys->output, psz_success, 8 );
304 p_command->p_command->pf_unparse( &p_command->results,
306 BufferAdd( &p_sys->output, psz_nl, 1 );
310 BufferPrintf( &p_sys->output, "FAILURE: %d\n",
311 p_command->i_status );
315 /* Try emptying the output buffer */
316 if( p_sys->i_outputfd != -1 )
318 ssize_t i_len = write( p_sys->i_outputfd, p_sys->output.p_begin,
319 p_sys->output.i_length );
322 /* We hit an error */
323 if( errno != EAGAIN )
325 msg_Warn( p_filter, "Error on output file: %m" );
326 close( p_sys->i_outputfd );
327 p_sys->i_outputfd = -1;
332 BufferDel( &p_sys->output, i_len );
336 if( !p_sys->b_updated )
339 subpicture_t *p_spu = NULL;
340 overlay_t *p_overlay = NULL;
342 p_spu = p_filter->pf_sub_buffer_new( p_filter );
345 msg_Err( p_filter, "cannot allocate subpicture" );
349 p_spu->b_absolute = true;
350 p_spu->i_start = date;
352 p_spu->b_ephemer = true;
354 subpicture_region_t **pp_region = &p_spu->p_region;
355 while( (p_overlay = ListWalk( &p_sys->overlays )) )
357 subpicture_region_t *p_region;
359 *pp_region = p_region = subpicture_region_New( &p_overlay->format );
363 msg_Dbg( p_filter, "Displaying overlay: %4.4s, %d, %d, %d",
364 (char*)&p_overlay->format.i_chroma, p_overlay->i_x, p_overlay->i_y,
365 p_overlay->i_alpha );
367 if( p_overlay->format.i_chroma == VLC_CODEC_TEXT )
369 p_region->psz_text = strdup( p_overlay->data.p_text );
370 p_region->p_style = text_style_Duplicate( p_overlay->p_fontstyle );
374 /* FIXME the copy is probably not needed anymore */
375 picture_Copy( p_region->p_picture, p_overlay->data.p_pic );
377 p_region->i_x = p_overlay->i_x;
378 p_region->i_y = p_overlay->i_y;
379 p_region->i_align = SUBPICTURE_ALIGN_LEFT | SUBPICTURE_ALIGN_TOP;
380 p_region->i_alpha = p_overlay->i_alpha;
381 pp_region = &p_region->p_next;
384 p_sys->b_updated = false;
388 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
389 vlc_value_t oldval, vlc_value_t newval,
392 filter_sys_t *p_sys = (filter_sys_t *)p_data;
393 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
395 vlc_mutex_lock( &p_sys->lock );
396 if( !strncmp( psz_var, "overlay-input", 13 ) )
398 free( p_sys->psz_inputfile );
399 p_sys->psz_inputfile = strdup( newval.psz_string );
401 else if( !strncmp( psz_var, "overlay-output", 14 ) )
403 free( p_sys->psz_outputfile );
404 p_sys->psz_outputfile = strdup( newval.psz_string );
406 vlc_mutex_unlock( &p_sys->lock );