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