]> git.sesse.net Git - vlc/blob - modules/stream_out/duplicate.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / stream_out / duplicate.c
1 /*****************************************************************************
2  * duplicate.c: duplicate stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@via.ecp.fr>
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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_sout.h>
35 #include <vlc_block.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int      Open    ( vlc_object_t * );
41 static void     Close   ( vlc_object_t * );
42
43 vlc_module_begin ()
44     set_description( N_("Duplicate stream output") )
45     set_capability( "sout stream", 50 )
46     add_shortcut( "duplicate" )
47     add_shortcut( "dup" )
48     set_category( CAT_SOUT )
49     set_subcategory( SUBCAT_SOUT_STREAM )
50     set_callbacks( Open, Close )
51 vlc_module_end ()
52
53
54 /*****************************************************************************
55  * Exported prototypes
56  *****************************************************************************/
57 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
58 static int               Del ( sout_stream_t *, sout_stream_id_t * );
59 static int               Send( sout_stream_t *, sout_stream_id_t *,
60                                block_t* );
61
62 struct sout_stream_sys_t
63 {
64     int             i_nb_streams;
65     sout_stream_t   **pp_streams;
66
67     int             i_nb_select;
68     char            **ppsz_select;
69 };
70
71 struct sout_stream_id_t
72 {
73     int                 i_nb_ids;
74     void                **pp_ids;
75 };
76
77 static bool ESSelected( es_format_t *fmt, char *psz_select );
78
79 /*****************************************************************************
80  * Open:
81  *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
83 {
84     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
85     sout_stream_sys_t *p_sys;
86     config_chain_t        *p_cfg;
87
88     msg_Dbg( p_stream, "creating 'duplicate'" );
89
90     p_sys = malloc( sizeof( sout_stream_sys_t ) );
91     if( !p_sys )
92         return VLC_ENOMEM;
93
94     TAB_INIT( p_sys->i_nb_streams, p_sys->pp_streams );
95     TAB_INIT( p_sys->i_nb_select, p_sys->ppsz_select );
96
97     for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
98     {
99         if( !strncmp( p_cfg->psz_name, "dst", strlen( "dst" ) ) )
100         {
101             sout_stream_t *s;
102
103             msg_Dbg( p_stream, " * adding `%s'", p_cfg->psz_value );
104             s = sout_StreamNew( p_stream->p_sout, p_cfg->psz_value );
105
106             if( s )
107             {
108                 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, s );
109                 TAB_APPEND( p_sys->i_nb_select,  p_sys->ppsz_select, NULL );
110             }
111         }
112         else if( !strncmp( p_cfg->psz_name, "select", strlen( "select" ) ) )
113         {
114             char *psz = p_cfg->psz_value;
115             if( p_sys->i_nb_select > 0 && psz && *psz )
116             {
117                 char **ppsz_select = &p_sys->ppsz_select[p_sys->i_nb_select - 1];
118
119                 if( *ppsz_select )
120                 {
121                     msg_Err( p_stream, " * ignore selection `%s' (it already has `%s')",
122                              psz, *ppsz_select );
123                 }
124                 else
125                 {
126                     msg_Dbg( p_stream, " * apply selection `%s'", psz );
127                     *ppsz_select = strdup( psz );
128                 }
129             }
130         }
131         else
132         {
133             msg_Err( p_stream, " * ignore unknown option `%s'", p_cfg->psz_name );
134         }
135     }
136
137     if( p_sys->i_nb_streams == 0 )
138     {
139         msg_Err( p_stream, "no destination given" );
140         free( p_sys );
141
142         return VLC_EGENERIC;
143     }
144
145     p_stream->pf_add    = Add;
146     p_stream->pf_del    = Del;
147     p_stream->pf_send   = Send;
148
149     p_stream->p_sys     = p_sys;
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Close:
156  *****************************************************************************/
157 static void Close( vlc_object_t * p_this )
158 {
159     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
160     sout_stream_sys_t *p_sys = p_stream->p_sys;
161
162     int i;
163
164     msg_Dbg( p_stream, "closing a duplication" );
165     for( i = 0; i < p_sys->i_nb_streams; i++ )
166     {
167         sout_StreamDelete( p_sys->pp_streams[i] );
168         free( p_sys->ppsz_select[i] );
169     }
170     free( p_sys->pp_streams );
171     free( p_sys->ppsz_select );
172
173     free( p_sys );
174 }
175
176 /*****************************************************************************
177  * Add:
178  *****************************************************************************/
179 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
180 {
181     sout_stream_sys_t *p_sys = p_stream->p_sys;
182     sout_stream_id_t  *id;
183     int i_stream, i_valid_streams = 0;
184
185     id = malloc( sizeof( sout_stream_id_t ) );
186     if( !id )
187         return NULL;
188
189     TAB_INIT( id->i_nb_ids, id->pp_ids );
190
191     msg_Dbg( p_stream, "duplicated a new stream codec=%4.4s (es=%d group=%d)",
192              (char*)&p_fmt->i_codec, p_fmt->i_id, p_fmt->i_group );
193
194     for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
195     {
196         void *id_new = NULL;
197
198         if( ESSelected( p_fmt, p_sys->ppsz_select[i_stream] ) )
199         {
200             sout_stream_t *out = p_sys->pp_streams[i_stream];
201
202             id_new = (void*)sout_StreamIdAdd( out, p_fmt );
203             if( id_new )
204             {
205                 msg_Dbg( p_stream, "    - added for output %d", i_stream );
206                 i_valid_streams++;
207             }
208             else
209             {
210                 msg_Dbg( p_stream, "    - failed for output %d", i_stream );
211             }
212         }
213         else
214         {
215             msg_Dbg( p_stream, "    - ignored for output %d", i_stream );
216         }
217
218         /* Append failed attempts as well to keep track of which pp_id
219          * belongs to which duplicated stream */
220         TAB_APPEND( id->i_nb_ids, id->pp_ids, id_new );
221     }
222
223     if( i_valid_streams <= 0 )
224     {
225         Del( p_stream, id );
226         return NULL;
227     }
228
229     return id;
230 }
231
232 /*****************************************************************************
233  * Del:
234  *****************************************************************************/
235 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
236 {
237     sout_stream_sys_t *p_sys = p_stream->p_sys;
238     int               i_stream;
239
240     for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
241     {
242         if( id->pp_ids[i_stream] )
243         {
244             sout_stream_t *out = p_sys->pp_streams[i_stream];
245             sout_StreamIdDel( out, id->pp_ids[i_stream] );
246         }
247     }
248
249     free( id->pp_ids );
250     free( id );
251     return VLC_SUCCESS;
252 }
253
254 /*****************************************************************************
255  * Send:
256  *****************************************************************************/
257 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
258                  block_t *p_buffer )
259 {
260     sout_stream_sys_t *p_sys = p_stream->p_sys;
261     sout_stream_t     *p_dup_stream;
262     int               i_stream;
263
264     /* Loop through the linked list of buffers */
265     while( p_buffer )
266     {
267         block_t *p_next = p_buffer->p_next;
268
269         p_buffer->p_next = NULL;
270
271         for( i_stream = 0; i_stream < p_sys->i_nb_streams - 1; i_stream++ )
272         {
273             p_dup_stream = p_sys->pp_streams[i_stream];
274
275             if( id->pp_ids[i_stream] )
276             {
277                 block_t *p_dup = block_Duplicate( p_buffer );
278
279                 if( p_dup )
280                     sout_StreamIdSend( p_dup_stream, id->pp_ids[i_stream], p_dup );
281             }
282         }
283
284         if( i_stream < p_sys->i_nb_streams && id->pp_ids[i_stream] )
285         {
286             p_dup_stream = p_sys->pp_streams[i_stream];
287             sout_StreamIdSend( p_dup_stream, id->pp_ids[i_stream], p_buffer );
288         }
289         else
290         {
291             block_Release( p_buffer );
292         }
293
294         p_buffer = p_next;
295     }
296     return VLC_SUCCESS;
297 }
298
299 /*****************************************************************************
300  * Divers
301  *****************************************************************************/
302 static bool NumInRange( char *psz_range, int i_num )
303 {
304     char *psz = strchr( psz_range, '-' );
305     char *end;
306     int  i_start, i_stop;
307
308     if( psz )
309     {
310         i_start = strtol( psz_range, &end, 0 );
311         if( end == psz_range ) i_start = i_num;
312
313         i_stop  = strtol( psz,       &end, 0 );
314         if( end == psz_range ) i_stop = i_num;
315     }
316     else
317     {
318         i_start = i_stop = strtol( psz_range, NULL, 0 );
319     }
320
321     return i_start <= i_num && i_num <= i_stop ? true : false;
322 }
323
324 static bool ESSelected( es_format_t *fmt, char *psz_select )
325 {
326     char  *psz_dup;
327     char  *psz;
328
329     /* We have tri-state variable : no tested (-1), failed(0), succeed(1) */
330     int i_cat = -1;
331     int i_es  = -1;
332     int i_prgm= -1;
333
334     /* If empty all es are selected */
335     if( psz_select == NULL || *psz_select == '\0' )
336     {
337         return true;
338     }
339     psz_dup = strdup( psz_select );
340     if( !psz_dup )
341         return false;
342     psz = psz_dup;
343
344     /* If non empty, parse the selection:
345      * We have selection[,selection[,..]] where following selection are recognized:
346      *      (no(-))audio
347      *      (no(-))spu
348      *      (no(-))video
349      *      (no(-))es=[start]-[end] or es=num
350      *      (no(-))prgm=[start]-[end] or prgm=num (program works too)
351      *      if a negative test failed we exit directly
352      */
353     while( psz && *psz )
354     {
355         char *p;
356
357         /* Skip space */
358         while( *psz == ' ' || *psz == '\t' ) psz++;
359
360         /* Search end */
361         p = strchr( psz, ',' );
362         if( p == psz )
363         {
364             /* Empty */
365             psz = p + 1;
366             continue;
367         }
368         if( p )
369         {
370             *p++ = '\0';
371         }
372
373         if( !strncmp( psz, "no-audio", strlen( "no-audio" ) ) ||
374             !strncmp( psz, "noaudio", strlen( "noaudio" ) ) )
375         {
376             if( i_cat == -1 )
377             {
378                 i_cat = fmt->i_cat != AUDIO_ES ? 1 : 0;
379             }
380         }
381         else if( !strncmp( psz, "no-video", strlen( "no-video" ) ) ||
382                  !strncmp( psz, "novideo", strlen( "novideo" ) ) )
383         {
384             if( i_cat == -1 )
385             {
386                 i_cat = fmt->i_cat != VIDEO_ES ? 1 : 0;
387             }
388         }
389         else if( !strncmp( psz, "no-spu", strlen( "no-spu" ) ) ||
390                  !strncmp( psz, "nospu", strlen( "nospu" ) ) )
391         {
392             if( i_cat == -1 )
393             {
394                 i_cat = fmt->i_cat != SPU_ES ? 1 : 0;
395             }
396         }
397         else if( !strncmp( psz, "audio", strlen( "audio" ) ) )
398         {
399             if( i_cat == -1 )
400             {
401                 i_cat = fmt->i_cat == AUDIO_ES ? 1 : 0;
402             }
403         }
404         else if( !strncmp( psz, "video", strlen( "video" ) ) )
405         {
406             if( i_cat == -1 )
407             {
408                 i_cat = fmt->i_cat == VIDEO_ES ? 1 : 0;
409             }
410         }
411         else if( !strncmp( psz, "spu", strlen( "spu" ) ) )
412         {
413             if( i_cat == -1 )
414             {
415                 i_cat = fmt->i_cat == SPU_ES ? 1 : 0;
416             }
417         }
418         else if( strchr( psz, '=' ) != NULL )
419         {
420             char *psz_arg = strchr( psz, '=' );
421             *psz_arg++ = '\0';
422
423             if( !strcmp( psz, "no-es" ) || !strcmp( psz, "noes" ) )
424             {
425                 if( i_es == -1 )
426                 {
427                     i_es = NumInRange( psz_arg, fmt->i_id ) ? 0 : -1;
428                 }
429             }
430             else if( !strcmp( psz, "es" ) )
431             {
432                 if( i_es == -1 )
433                 {
434                     i_es = NumInRange( psz_arg, fmt->i_id) ? 1 : -1;
435                 }
436             }
437             else if( !strcmp( psz, "no-prgm" ) || !strcmp( psz, "noprgm" ) ||
438                       !strcmp( psz, "no-program" ) || !strcmp( psz, "noprogram" ) )
439             {
440                 if( fmt->i_group >= 0 && i_prgm == -1 )
441                 {
442                     i_prgm = NumInRange( psz_arg, fmt->i_group ) ? 0 : -1;
443                 }
444             }
445             else if( !strcmp( psz, "prgm" ) || !strcmp( psz, "program" ) )
446             {
447                 if( fmt->i_group >= 0 && i_prgm == -1 )
448                 {
449                     i_prgm = NumInRange( psz_arg, fmt->i_group ) ? 1 : -1;
450                 }
451             }
452         }
453         else
454         {
455             fprintf( stderr, "unknown args (%s)\n", psz );
456         }
457         /* Next */
458         psz = p;
459     }
460
461     free( psz_dup );
462
463     if( i_cat == 1 || i_es == 1 || i_prgm == 1 )
464     {
465         return true;
466     }
467     return false;
468 }