]> git.sesse.net Git - vlc/blob - modules/video_filter/rss.c
free stuff and add mutex (will be needed when RSS feed get updated)
[vlc] / modules / video_filter / rss.c
1 /*****************************************************************************
2  * rss.c : rss feed display video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "vlc_filter.h"
34 #include "vlc_block.h"
35 #include "osd.h"
36
37 #include "vlc_block.h"
38 #include "vlc_stream.h"
39 #include "vlc_xml.h"
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  CreateFilter ( vlc_object_t * );
45 static void DestroyFilter( vlc_object_t * );
46 static subpicture_t *Filter( filter_t *, mtime_t );
47
48 static int FetchRSS( filter_t * );
49 static void FreeRSS( filter_t * );
50
51 static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
52                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
53                0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
54                0x00000080, 0x000000FF, 0x0000FFFF};
55 static char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"),
56                N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
57                N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
58                N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
59                N_("Aqua") };
60
61 /*****************************************************************************
62  * filter_sys_t: rss filter descriptor
63  *****************************************************************************/
64
65 struct rss_item_t
66 {
67     char *psz_title;
68     char *psz_description;
69     char *psz_link;
70 };
71
72 struct rss_feed_t
73 {
74     char *psz_title;
75     char *psz_description;
76     char *psz_link;
77
78     int i_items;
79     struct rss_item_t *p_items;
80 };
81
82 struct filter_sys_t
83 {
84     vlc_mutex_t lock;
85     vlc_mutex_t *p_lock;
86
87     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
88     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
89     int i_speed;
90     int i_length;
91
92     char *psz_marquee;    /* marquee string */
93
94     int  i_font_color, i_font_opacity, i_font_size; /* font control */
95
96     mtime_t last_date;
97
98     //vlc_bool_t b_need_update;
99
100     char *psz_urls;
101     int i_feeds;
102     struct rss_feed_t *p_feeds;
103
104     int i_cur_feed;
105     int i_cur_item;
106     int i_cur_char;
107 };
108
109 #define MSG_TEXT N_("RSS feed URLs")
110 #define MSG_LONGTEXT N_("RSS feed comma seperated URLs")
111 #define SPEED_TEXT N_("RSS feed speed")
112 #define SPEED_LONGTEXT N_("RSS feed speed (bigger is slower)")
113 #define LENGTH_TEXT N_("RSS feed max number of chars displayed")
114 #define LENGTH_LONGTEXT N_("RSS feed max number of chars displayed")
115
116 #define POSX_TEXT N_("X offset, from left")
117 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
118 #define POSY_TEXT N_("Y offset, from the top")
119 #define POSY_LONGTEXT N_("Y offset, down from the top" )
120 #define OPACITY_TEXT N_("Opacity")
121 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
122     "overlay text. 0 = transparent, 255 = totally opaque. " )
123 #define SIZE_TEXT N_("Font size, pixels")
124 #define SIZE_LONGTEXT N_("Specify the font size, in pixels, " \
125     "with -1 = use freetype-fontsize" )
126
127 #define COLOR_TEXT N_("Text Default Color")
128 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, hexadecimal. " \
129     "#000000 = all colors off, " \
130     "0xFF0000 = just Red, 0xFFFFFF = all color on [White]" )
131
132 #define POS_TEXT N_("Marquee position")
133 #define POS_LONGTEXT N_( \
134   "You can enforce the marquee position on the video " \
135   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
136   "also use combinations of these values by adding them).")
137
138 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
139 static char *ppsz_pos_descriptions[] =
140      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
141      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
142
143 /*****************************************************************************
144  * Module descriptor
145  *****************************************************************************/
146 vlc_module_begin();
147     set_capability( "sub filter", 0 );
148     set_shortname( N_("RSS" ));
149     set_callbacks( CreateFilter, DestroyFilter );
150     set_category( CAT_VIDEO );
151     set_subcategory( SUBCAT_VIDEO_SUBPIC );
152     add_string( "rss-urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
153
154     set_section( N_("Position"), NULL );
155     add_integer( "rss-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
156     add_integer( "rss-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
157     add_integer( "rss-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
158
159     set_section( N_("Font"), NULL );
160     /* 5 sets the default to top [1] left [4] */
161     change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
162     add_integer_with_range( "rss-opacity", 255, 0, 255, NULL,
163         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
164     add_integer( "rss-color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT, VLC_TRUE );
165         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
166     add_integer( "rss-size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE );
167
168     set_section( N_("Misc"), NULL );
169     add_integer( "rss-speed", 100000, NULL, SPEED_TEXT, SPEED_LONGTEXT,
170                  VLC_FALSE );
171     add_integer( "rss-length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT,
172                  VLC_FALSE );
173
174     set_description( _("RSS feed display sub filter") );
175     add_shortcut( "rss" );
176 vlc_module_end();
177
178 /*****************************************************************************
179  * CreateFilter: allocates RSS video filter
180  *****************************************************************************/
181 static int CreateFilter( vlc_object_t *p_this )
182 {
183     filter_t *p_filter = (filter_t *)p_this;
184     filter_sys_t *p_sys;
185     int i_feed;
186
187     /* Allocate structure */
188     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
189     if( p_sys == NULL )
190     {
191         msg_Err( p_filter, "out of memory" );
192         return VLC_ENOMEM;
193     }
194
195     vlc_mutex_init( p_filter, &p_sys->lock );
196     vlc_mutex_lock( &p_sys->lock );
197
198     p_sys->psz_urls = var_CreateGetString( p_filter, "rss-urls" );
199     p_sys->i_cur_feed = 0;
200     p_sys->i_cur_item = 0;
201     p_sys->i_cur_char = 0;
202     p_sys->i_feeds = 0;
203     p_sys->p_feeds = NULL;
204     p_sys->i_speed = var_CreateGetInteger( p_filter, "rss-speed" );
205     p_sys->i_length = var_CreateGetInteger( p_filter, "rss-length" );
206     p_sys->psz_marquee = (char *)malloc( p_sys->i_length );
207
208     p_sys->i_xoff = var_CreateGetInteger( p_filter, "rss-x" );
209     p_sys->i_yoff = var_CreateGetInteger( p_filter, "rss-y" );
210     p_sys->i_pos = var_CreateGetInteger( p_filter, "rss-position" );
211     var_Create( p_filter, "rss-opacity", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
212     p_sys->i_font_opacity = var_CreateGetInteger( p_filter, "rss-opacity" );
213     p_sys->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
214     p_sys->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
215
216     if( FetchRSS( p_filter ) )
217     {
218         msg_Err( p_filter, "failed while fetching RSS ... too bad" );
219         vlc_mutex_unlock( &p_sys->lock );
220         return VLC_EGENERIC;
221     }
222
223     if( p_sys->i_feeds == 0 )
224     {
225         vlc_mutex_unlock( &p_sys->lock );
226         return VLC_EGENERIC;
227     }
228     for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
229         if( p_sys->p_feeds[i_feed].i_items == 0 )
230         {
231             vlc_mutex_unlock( &p_sys->lock );
232             return VLC_EGENERIC;
233         }
234
235     /* Misc init */
236     p_filter->pf_sub_filter = Filter;
237     p_sys->last_date = (mtime_t)0;
238
239     vlc_mutex_unlock( &p_sys->lock );
240
241     return VLC_SUCCESS;
242 }
243 /*****************************************************************************
244  * DestroyFilter: destroy RSS video filter
245  *****************************************************************************/
246 static void DestroyFilter( vlc_object_t *p_this )
247 {
248     filter_t *p_filter = (filter_t *)p_this;
249     filter_sys_t *p_sys = p_filter->p_sys;
250
251     vlc_mutex_lock( &p_sys->lock );
252
253     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
254     free( p_sys->psz_urls );
255     FreeRSS( p_filter );
256     vlc_mutex_unlock( &p_sys->lock );
257     vlc_mutex_destroy( &p_sys->lock );
258     free( p_sys );
259
260     /* Delete the RSS variables */
261     var_Destroy( p_filter, "rss-urls" );
262     var_Destroy( p_filter, "rss-speed" );
263     var_Destroy( p_filter, "rss-length" );
264     var_Destroy( p_filter, "rss-x" );
265     var_Destroy( p_filter, "rss-y" );
266     var_Destroy( p_filter, "rss-position" );
267     var_Destroy( p_filter, "rss-color");
268     var_Destroy( p_filter, "rss-opacity");
269     var_Destroy( p_filter, "rss-size");
270 }
271
272 /****************************************************************************
273  * Filter: the whole thing
274  ****************************************************************************
275  * This function outputs subpictures at regular time intervals.
276  ****************************************************************************/
277 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
278 {
279     filter_sys_t *p_sys = p_filter->p_sys;
280     subpicture_t *p_spu;
281     video_format_t fmt;
282
283     int i_feed, i_item;
284
285     vlc_mutex_lock( &p_sys->lock );
286
287     /* wait more for the 1st char */
288     if( p_sys->last_date + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 ) * p_sys->i_speed > date )
289     {
290         vlc_mutex_unlock( &p_sys->lock );
291         return NULL;
292     }
293
294     p_sys->last_date = date;
295     p_sys->i_cur_char++;
296     if( p_sys->p_feeds[p_sys->i_cur_feed].p_items[p_sys->i_cur_item].psz_title[p_sys->i_cur_char] == 0 )
297     {
298         p_sys->i_cur_char = 0;
299         p_sys->i_cur_item++;
300         if( p_sys->i_cur_item >= p_sys->p_feeds[p_sys->i_cur_feed].i_items )
301         {
302             p_sys->i_cur_item = 0;
303             p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
304         }
305     }
306
307     p_spu = p_filter->pf_sub_buffer_new( p_filter );
308     if( !p_spu )
309     {
310         vlc_mutex_unlock( &p_sys->lock );
311         return NULL;
312     }
313
314     memset( &fmt, 0, sizeof(video_format_t) );
315     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
316     fmt.i_aspect = 0;
317     fmt.i_width = 0;
318     fmt.i_height = 0;
319     fmt.i_x_offset = 0;
320     fmt.i_y_offset = 0;
321     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
322     if( !p_spu->p_region )
323     {
324         p_filter->pf_sub_buffer_del( p_filter, p_spu );
325         vlc_mutex_unlock( &p_sys->lock );
326         return NULL;
327     }
328
329     i_item = p_sys->i_cur_item;
330     i_feed = p_sys->i_cur_feed;
331     snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s", p_sys->p_feeds[i_feed].psz_title, p_sys->p_feeds[i_feed].p_items[i_item].psz_title+p_sys->i_cur_char );
332     while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
333     {
334         i_item++;
335         if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
336         snprintf( strchr( p_sys->psz_marquee, 0 ), p_sys->i_length - strlen( p_sys->psz_marquee ), " - %s", p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
337     }
338
339     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
340     p_spu->i_start = date;
341     p_spu->i_stop  = 0;
342     p_spu->b_ephemer = VLC_TRUE;
343
344     /*  where to locate the string: */
345     if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
346     {   /* set to one of the 9 relative locations */
347         p_spu->i_flags = p_sys->i_pos;
348         p_spu->i_x = 0;
349         p_spu->i_y = 0;
350         p_spu->b_absolute = VLC_FALSE;
351     }
352     else
353     {   /*  set to an absolute xy, referenced to upper left corner */
354         p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
355         p_spu->i_x = p_sys->i_xoff;
356         p_spu->i_y = p_sys->i_yoff;
357         p_spu->b_absolute = VLC_TRUE;
358     }
359     p_spu->i_height = 1;
360     p_spu->p_region->i_text_color = p_sys->i_font_color;
361     p_spu->p_region->i_text_alpha = 255 - p_sys->i_font_opacity;
362     p_spu->p_region->i_text_size = p_sys->i_font_size;
363
364     vlc_mutex_unlock( &p_sys->lock );
365     return p_spu;
366 }
367
368 /****************************************************************************
369  * RSS related functions
370  ****************************************************************************
371  * You should always lock the p_filter mutex before using any of these
372  * functions
373  ***************************************************************************/
374
375 /****************************************************************************
376  * FetchRSS
377  ***************************************************************************/
378 static int FetchRSS( filter_t *p_filter)
379 {
380     filter_sys_t *p_sys = p_filter->p_sys;
381
382     stream_t *p_stream = NULL;
383     xml_t *p_xml = NULL;
384     xml_reader_t *p_xml_reader = NULL;
385
386     char *psz_eltname = NULL;
387     char *psz_eltvalue = NULL;
388     char *psz_feed = NULL;
389     char *psz_buffer = NULL;
390     char *psz_buffer_2 = NULL;
391
392     int i_feed;
393     int i_item;
394     int i_is_item;
395     int i_int;
396
397     FreeRSS( p_filter );
398     p_sys->i_feeds = 1;
399     i_int = 0;
400     while( p_sys->psz_urls[i_int] != 0 )
401         if( p_sys->psz_urls[i_int++] == ',' )
402             p_sys->i_feeds++;
403     p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
404                                 * sizeof( struct rss_feed_t ) );
405
406     p_xml = xml_Create( p_filter );
407     if( !p_xml )
408     {
409         msg_Err( p_filter, "Failed to open XML parser" );
410         return 1;
411     }
412
413     psz_buffer = strdup( p_sys->psz_urls );
414     psz_buffer_2 = psz_buffer; /* keep track so we can free it */
415     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
416     {
417         struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
418
419         if( psz_buffer == NULL ) break;
420         if( psz_buffer[0] == 0 ) psz_buffer++;
421         psz_feed = psz_buffer;
422         psz_buffer = strchr( psz_buffer, ',' );
423         if( psz_buffer != NULL ) psz_buffer[0] = 0;
424
425         p_feed->psz_title = NULL;
426         p_feed->psz_description = NULL;
427         p_feed->psz_link = NULL;
428         p_feed->i_items = 0;
429         p_feed->p_items = NULL;
430
431         msg_Dbg( p_filter, "Opening %s RSS feed ...", psz_feed );
432
433         p_stream = stream_UrlNew( p_filter, psz_feed );
434         if( !p_stream )
435         {
436             msg_Err( p_filter, "Failed to open %s for reading", psz_feed );
437             return 1;
438         }
439
440         p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
441         if( !p_xml_reader )
442         {
443             msg_Err( p_filter, "Failed to open %s for parsing", psz_feed );
444             return 1;
445         }
446
447         i_item = 0;
448         i_is_item = VLC_FALSE;
449
450         while( xml_ReaderRead( p_xml_reader ) == 1 )
451         {
452             switch( xml_ReaderNodeType( p_xml_reader ) )
453             {
454                 // Error
455                 case -1:
456                     return 1;
457
458                 case XML_READER_STARTELEM:
459                     if( psz_eltname ) free( psz_eltname );
460                     psz_eltname = xml_ReaderName( p_xml_reader );
461                     if( !psz_eltname )
462                     {
463                         return 1;
464                     }
465                     msg_Dbg( p_filter, "element name : %s", psz_eltname );
466                     if( !strcmp( psz_eltname, "item" ) )
467                     {
468                         i_is_item = VLC_TRUE;
469                         p_feed->i_items++;
470                         p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
471                     }
472                     break;
473
474                 case XML_READER_ENDELEM:
475                     if( psz_eltname ) free( psz_eltname );
476                     psz_eltname = xml_ReaderName( p_xml_reader );
477                     if( !psz_eltname )
478                     {
479                         return 1;
480                     }
481                     msg_Dbg( p_filter, "element end : %s", psz_eltname );
482                     if( !strcmp( psz_eltname, "item" ) )
483                     {
484                         i_is_item = VLC_FALSE;
485                         i_item++;
486                     }
487                     free( psz_eltname ); psz_eltname = NULL;
488                     break;
489
490                 case XML_READER_TEXT:
491                     psz_eltvalue = xml_ReaderValue( p_xml_reader );
492                     if( !psz_eltvalue )
493                     {
494                         return 1;
495                     }
496                     msg_Dbg( p_filter, "  text : %s", psz_eltvalue );
497                     if( i_is_item == VLC_FALSE )
498                     {
499                         if( !strcmp( psz_eltname, "title" ) )
500                         {
501                             p_feed->psz_title = psz_eltvalue;
502                         }
503                         else if( !strcmp( psz_eltname, "link" ) )
504                         {
505                             p_feed->psz_link = psz_eltvalue;
506                         }
507                         else if( !strcmp( psz_eltname, "description" ) )
508                         {
509                             p_feed->psz_description = psz_eltvalue;
510                         }
511                         else
512                         {
513                             free( psz_eltvalue );
514                         }
515                     }
516                     else
517                     {
518                         struct rss_item_t *p_item;
519                         p_item = p_feed->p_items+i_item;
520                         if( !strcmp( psz_eltname, "title" ) )
521                         {
522                             p_item->psz_title = psz_eltvalue;
523                         }
524                         else if( !strcmp( psz_eltname, "link" ) )
525                         {
526                             p_item->psz_link = psz_eltvalue;
527                         }
528                         else if( !strcmp( psz_eltname, "description" ) )
529                         {
530                             p_item->psz_description = psz_eltvalue;
531                         }
532                         else
533                         {
534                             free( psz_eltvalue );
535                         }
536                     }
537                     break;
538             }
539         }
540
541         if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
542         if( p_stream ) stream_Delete( p_stream );
543     }
544     free( psz_buffer_2 );
545     if( p_xml ) xml_Delete( p_xml );
546
547     return 0;
548 }
549
550 /****************************************************************************
551  * FreeRSS
552  ***************************************************************************/
553 static void FreeRSS( filter_t *p_filter)
554 {
555     filter_sys_t *p_sys = p_filter->p_sys;
556
557     struct rss_item_t *p_item;
558     struct rss_feed_t *p_feed;
559
560     int i_feed;
561     int i_item;
562
563     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
564     {
565         p_feed = p_sys->p_feeds+i_feed;
566         for( i_item = 0; i_item < p_feed->i_items; i_item++ )
567         {
568             p_item = p_feed->p_items+i_item;
569             free( p_item->psz_title );
570             free( p_item->psz_link );
571             free( p_item->psz_description );
572         }
573         free( p_feed->p_items );
574         free( p_feed->psz_title);
575         free( p_feed->psz_link );
576         free( p_feed->psz_description );
577     }
578     free( p_sys->p_feeds );
579     p_sys->i_feeds = 0;
580 }