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