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