]> git.sesse.net Git - vlc/blob - modules/video_filter/dynamicoverlay/dynamicoverlay.c
Missing #include <errno.h>
[vlc] / modules / video_filter / dynamicoverlay / dynamicoverlay.c
1 /*****************************************************************************
2  * dynamicoverlay.c : dynamic overlay plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Søren Bøg <avacore@videolan.org>
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 implied 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
34 #include <vlc_vout.h>
35 #include <vlc_filter.h>
36 #include <vlc_osd.h>
37
38 #include <ctype.h>
39 #include <fcntl.h>
40 #include <errno.h>
41
42 #include "dynamicoverlay.h"
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int Create( vlc_object_t * );
48 static void Destroy( vlc_object_t * );
49 static subpicture_t *Filter( filter_t *, mtime_t );
50
51 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
52                            vlc_value_t oldval, vlc_value_t newval,
53                            void *p_data );
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58
59 #define INPUT_TEXT N_("Input FIFO")
60 #define INPUT_LONGTEXT N_("FIFO which will be read for commands")
61
62 #define OUTPUT_TEXT N_("Output FIFO")
63 #define OUTPUT_LONGTEXT N_("FIFO which will be written to for responses")
64
65 vlc_module_begin ()
66     set_description( N_("Dynamic video overlay") )
67     set_shortname( N_("Overlay" ))
68     set_category( CAT_VIDEO )
69     set_subcategory( SUBCAT_VIDEO_VFILTER )
70     set_capability( "sub filter", 0 )
71
72     add_file( "overlay-input", NULL, NULL, INPUT_TEXT, INPUT_LONGTEXT,
73               false )
74     add_file( "overlay-output", NULL, NULL, OUTPUT_TEXT, OUTPUT_LONGTEXT,
75               false )
76
77     add_shortcut( "overlay" )
78     set_callbacks( Create, Destroy )
79 vlc_module_end ()
80
81 static const char *const ppsz_filter_options[] = {
82     "input", "output", NULL
83 };
84
85 /*****************************************************************************
86  * Create: allocates adjust video thread output method
87  *****************************************************************************
88  * This function allocates and initializes a adjust vout method.
89  *****************************************************************************/
90 static int Create( vlc_object_t *p_this )
91 {
92     filter_t *p_filter = (filter_t *)p_this;
93     filter_sys_t *p_sys;
94
95     /* Allocate structure */
96     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
97     if( p_filter->p_sys == NULL )
98         return VLC_ENOMEM;
99     p_sys = p_filter->p_sys;
100
101     BufferInit( &p_sys->input );
102     BufferInit( &p_sys->output );
103     QueueInit( &p_sys->atomic );
104     QueueInit( &p_sys->pending );
105     QueueInit( &p_sys->processed );
106     ListInit( &p_sys->overlays );
107
108     p_sys->i_inputfd = -1;
109     p_sys->i_outputfd = -1;
110     p_sys->b_updated = true;
111     p_sys->b_atomic = false;
112     vlc_mutex_init( &p_sys->lock );
113
114     p_filter->pf_sub_filter = Filter;
115
116     config_ChainParse( p_filter, "overlay-", ppsz_filter_options,
117                        p_filter->p_cfg );
118
119     p_sys->psz_inputfile = var_CreateGetStringCommand( p_filter,
120                                                        "overlay-input" );
121     p_sys->psz_outputfile = var_CreateGetStringCommand( p_filter,
122                                                         "overlay-output" );
123
124     var_AddCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
125     var_AddCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
126
127     RegisterCommand( p_filter );
128     return VLC_SUCCESS;
129 }
130
131 /*****************************************************************************
132  * Destroy: destroy adjust video thread output method
133  *****************************************************************************
134  * Terminate an output method created by adjustCreateOutputMethod
135  *****************************************************************************/
136 static void Destroy( vlc_object_t *p_this )
137 {
138     filter_t *p_filter = (filter_t *)p_this;
139     filter_sys_t *p_sys = p_filter->p_sys;
140
141     BufferDestroy( &p_sys->input );
142     BufferDestroy( &p_sys->output );
143     QueueDestroy( &p_sys->atomic );
144     QueueDestroy( &p_sys->pending );
145     QueueDestroy( &p_sys->processed );
146     ListDestroy( &p_sys->overlays );
147     UnregisterCommand( p_filter );
148
149     var_DelCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
150     var_DelCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
151
152     vlc_mutex_destroy( &p_sys->lock );
153     free( p_sys->psz_inputfile );
154     free( p_sys->psz_outputfile );
155     free( p_sys );
156 }
157
158 /*****************************************************************************
159  * Render: displays previously rendered output
160  *****************************************************************************
161  * This function send the currently rendered image to adjust modified image,
162  * waits until it is displayed and switch the two rendering buffers, preparing
163  * next frame.
164  *****************************************************************************/
165 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
166 {
167     filter_sys_t *p_sys = p_filter->p_sys;
168
169     /* We might need to open these at any time. */
170     vlc_mutex_lock( &p_sys->lock );
171     if( p_sys->i_inputfd == -1 )
172     {
173         p_sys->i_inputfd = open( p_sys->psz_inputfile, O_RDONLY | O_NONBLOCK );
174         if( p_sys->i_inputfd == -1 )
175         {
176             msg_Warn( p_filter, "Failed to grab input file: %s (%s)",
177                       p_sys->psz_inputfile, strerror( errno ) );
178         }
179         else
180         {
181             msg_Info( p_filter, "Grabbed input file: %s",
182                       p_sys->psz_inputfile );
183         }
184     }
185
186     if( p_sys->i_outputfd == -1 )
187     {
188         p_sys->i_outputfd = open( p_sys->psz_outputfile,
189                                   O_WRONLY | O_NONBLOCK );
190         if( p_sys->i_outputfd == -1 )
191         {
192             if( errno != ENXIO )
193             {
194                 msg_Warn( p_filter, "Failed to grab output file: %s (%s)",
195                           p_sys->psz_outputfile, strerror( errno ) );
196             }
197         }
198         else
199         {
200             msg_Info( p_filter, "Grabbed output file: %s",
201                       p_sys->psz_outputfile );
202         }
203     }
204     vlc_mutex_unlock( &p_sys->lock );
205
206     /* Read any waiting commands */
207     if( p_sys->i_inputfd != -1 )
208     {
209         char p_buffer[1024];
210         ssize_t i_len = read( p_sys->i_inputfd, p_buffer, 1024 );
211         if( i_len == -1 )
212         {
213             /* We hit an error */
214             if( errno != EAGAIN )
215             {
216                 msg_Warn( p_filter, "Error on input file: %s",
217                           strerror( errno ) );
218                 close( p_sys->i_inputfd );
219                 p_sys->i_inputfd = -1;
220             }
221         }
222         else if( i_len == 0 )
223         {
224             /* We hit the end-of-file */
225         }
226         else
227         {
228             BufferAdd( &p_sys->input, p_buffer, i_len );
229         }
230     }
231
232     /* Parse any complete commands */
233     char *p_end, *p_cmd;
234     while( ( p_end = memchr( p_sys->input.p_begin, '\n',
235                              p_sys->input.i_length ) ) )
236     {
237         commanddesc_t *p_cur = NULL;
238         bool b_found = false;
239         size_t i_index = 0;
240
241         *p_end = '\0';
242         p_cmd = BufferGetToken( &p_sys->input );
243
244         msg_Info( p_filter, "Search command: %s", p_cmd );
245         for( i_index = 0; i_index < p_sys->i_commands; i_index++ )
246         {
247             p_cur = p_sys->pp_commands[i_index];
248             if( !strncmp( p_cur->psz_command, p_cmd, strlen(p_cur->psz_command) ) )
249             {
250                 p_cmd[strlen(p_cur->psz_command)] = '\0';
251                 b_found = true;
252                 break;
253             }
254         }
255
256         if( !b_found )
257         {
258             /* No matching command */
259             msg_Err( p_filter, "Got invalid command: %s", p_cmd );
260             BufferPrintf( &p_sys->output, "FAILURE: %d Invalid Command\n", VLC_EGENERIC );
261         }
262         else
263         {
264             msg_Info( p_filter, "Got valid command: %s", p_cmd );
265
266             command_t *p_cmddesc = malloc( sizeof( command_t ) );
267             if( !p_cmddesc )
268                 return NULL;
269
270             p_cmd = p_cmd + strlen(p_cur->psz_command) +1;
271             p_cmddesc->p_command = p_cur;
272             p_cmddesc->p_command->pf_parser( p_cmd, p_end,
273                                              &p_cmddesc->params );
274
275             if( ( p_cmddesc->p_command->b_atomic == true ) &&
276                 ( p_sys->b_atomic == true ) )
277                 QueueEnqueue( &p_sys->atomic, p_cmddesc );
278             else
279                 QueueEnqueue( &p_sys->pending, p_cmddesc );
280         }
281
282         BufferDel( &p_sys->input, p_end - p_sys->input.p_begin + 1 );
283     }
284
285     /* Process any pending commands */
286     command_t *p_command = NULL;
287     while( (p_command = QueueDequeue( &p_sys->pending )) )
288     {
289         p_command->i_status =
290             p_command->p_command->pf_execute( p_filter, &p_command->params,
291                                               &p_command->results );
292         QueueEnqueue( &p_sys->processed, p_command );
293     }
294
295     /* Output any processed commands */
296     while( (p_command = QueueDequeue( &p_sys->processed )) )
297     {
298         if( p_command->i_status == VLC_SUCCESS )
299         {
300             const char *psz_success = "SUCCESS:";
301             const char *psz_nl = "\n";
302             BufferAdd( &p_sys->output, psz_success, 8 );
303             p_command->p_command->pf_unparse( &p_command->results,
304                                               &p_sys->output );
305             BufferAdd( &p_sys->output, psz_nl, 1 );
306         }
307         else
308         {
309             BufferPrintf( &p_sys->output, "FAILURE: %d\n",
310                           p_command->i_status );
311         }
312     }
313
314     /* Try emptying the output buffer */
315     if( p_sys->i_outputfd != -1 )
316     {
317         ssize_t i_len = write( p_sys->i_outputfd, p_sys->output.p_begin,
318                               p_sys->output.i_length );
319         if( i_len == -1 )
320         {
321             /* We hit an error */
322             if( errno != EAGAIN )
323             {
324                 msg_Warn( p_filter, "Error on output file: %s",
325                           strerror( errno ) );
326                 close( p_sys->i_outputfd );
327                 p_sys->i_outputfd = -1;
328             }
329         }
330         else
331         {
332             BufferDel( &p_sys->output, i_len );
333         }
334     }
335
336     if( p_sys->b_updated == false )
337         return NULL;
338
339     subpicture_t *p_spu = NULL;
340     overlay_t *p_overlay = NULL;
341
342     p_spu = p_filter->pf_sub_buffer_new( p_filter );
343     if( !p_spu )
344     {
345         msg_Err( p_filter, "cannot allocate subpicture" );
346         return NULL;
347     }
348
349     p_spu->b_absolute = true;
350     p_spu->i_start = date;
351     p_spu->i_stop = 0;
352     p_spu->b_ephemer = true;
353
354     subpicture_region_t **pp_region = &p_spu->p_region;
355     while( (p_overlay = ListWalk( &p_sys->overlays )) )
356     {
357         subpicture_region_t *p_region;
358
359         *pp_region = p_region = subpicture_region_New( &p_overlay->format );
360         if( !p_region )
361             break;
362
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 );
366
367         if( p_overlay->format.i_chroma == VLC_CODEC_TEXT )
368         {
369             p_region->psz_text = strdup( p_overlay->data.p_text );
370             p_region->p_style = text_style_Duplicate( p_overlay->p_fontstyle );
371         }
372         else
373         {
374             /* FIXME the copy is probably not needed anymore */
375             picture_Copy( p_region->p_picture, p_overlay->data.p_pic );
376         }
377         p_region->i_x = p_overlay->i_x;
378         p_region->i_y = p_overlay->i_y;
379         p_region->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
380         p_region->i_alpha = p_overlay->i_alpha;
381         pp_region = &p_region->p_next;
382     }
383
384     p_sys->b_updated = false;
385     return p_spu;
386 }
387
388 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
389                            vlc_value_t oldval, vlc_value_t newval,
390                            void *p_data )
391 {
392     filter_sys_t *p_sys = (filter_sys_t *)p_data;
393     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
394
395     vlc_mutex_lock( &p_sys->lock );
396     if( !strncmp( psz_var, "overlay-input", 13 ) )
397     {
398         free( p_sys->psz_inputfile );
399         p_sys->psz_inputfile = strdup( newval.psz_string );
400     }
401     else if( !strncmp( psz_var, "overlay-output", 14 ) )
402     {
403         free( p_sys->psz_outputfile );
404         p_sys->psz_outputfile = strdup( newval.psz_string );
405     }
406     vlc_mutex_unlock( &p_sys->lock );
407
408     return VLC_EGENERIC;
409 }