]> git.sesse.net Git - vlc/blob - modules/stream_out/gather.c
Do not insert TTL in c= line for IPv6 - this was invalid
[vlc] / modules / stream_out / gather.c
1 /*****************************************************************************
2  * gather.c: gathering stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: 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
29 #include <vlc/vlc.h>
30 #include <vlc_input.h>
31 #include <vlc_sout.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int      Open    ( vlc_object_t * );
37 static void     Close   ( vlc_object_t * );
38
39 vlc_module_begin();
40     set_description( _("Gathering stream output") );
41     set_capability( "sout stream", 50 );
42     add_shortcut( "gather" );
43     set_callbacks( Open, Close );
44 vlc_module_end();
45
46 /*****************************************************************************
47  * Exported prototypes
48  *****************************************************************************/
49 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
50 static int               Del ( sout_stream_t *, sout_stream_id_t * );
51 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
52
53 struct sout_stream_id_t
54 {
55     vlc_bool_t    b_used;
56
57     es_format_t fmt;
58     void          *id;
59 };
60
61 struct sout_stream_sys_t
62 {
63     sout_stream_t   *p_out;
64
65     int              i_id;
66     sout_stream_id_t **id;
67 };
68
69 /*****************************************************************************
70  * Open:
71  *****************************************************************************/
72 static int Open( vlc_object_t *p_this )
73 {
74     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
75     sout_stream_sys_t *p_sys;
76
77     p_stream->p_sys = p_sys = malloc( sizeof( sout_stream_sys_t ) );
78     p_sys->p_out    = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
79     if( p_sys->p_out == NULL )
80     {
81         free( p_sys );
82         return VLC_EGENERIC;
83     }
84     p_stream->pf_add    = Add;
85     p_stream->pf_del    = Del;
86     p_stream->pf_send   = Send;
87
88     TAB_INIT( p_sys->i_id, p_sys->id );
89
90     return VLC_SUCCESS;
91 }
92
93 /*****************************************************************************
94  * Close:
95  *****************************************************************************/
96 static void Close( vlc_object_t * p_this )
97 {
98     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
99     sout_stream_sys_t *p_sys = p_stream->p_sys;
100     int i;
101
102     for( i = 0; i < p_sys->i_id; i++ )
103     {
104         sout_stream_id_t *id = p_sys->id[i];
105
106         sout_StreamIdDel( p_sys->p_out, id->id );
107         es_format_Clean( &id->fmt );
108         free( id );
109     }
110     TAB_CLEAN( p_sys->i_id, p_sys->id );
111
112     sout_StreamDelete( p_sys->p_out );
113     free( p_sys );
114 }
115
116 /*****************************************************************************
117  * Add:
118  *****************************************************************************/
119 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
120 {
121     sout_stream_sys_t *p_sys = p_stream->p_sys;
122     sout_stream_id_t  *id;
123     int i;
124
125     /* search a compatible output */
126     for( i = 0; i < p_sys->i_id; i++ )
127     {
128         id = p_sys->id[i];
129         if( id->b_used )
130             continue;
131
132         if( id->fmt.i_cat != p_fmt->i_cat || id->fmt.i_codec != p_fmt->i_codec )
133             continue;
134
135         if( id->fmt.i_cat == AUDIO_ES )
136         {
137             audio_format_t *p_a = &id->fmt.audio;
138             if( p_a->i_rate != p_fmt->audio.i_rate ||
139                 p_a->i_channels != p_fmt->audio.i_channels ||
140                 p_a->i_blockalign != p_fmt->audio.i_blockalign )
141                 continue;
142         }
143         else if( id->fmt.i_cat == VIDEO_ES )
144         {
145             video_format_t *p_v = &id->fmt.video;
146             if( p_v->i_width != p_fmt->video.i_width ||
147                 p_v->i_height != p_fmt->video.i_height )
148                 continue;
149         }
150
151         /* */
152         msg_Dbg( p_stream, "reusing already opened output" );
153         id->b_used = VLC_TRUE;
154         return id;
155     }
156
157     /* destroy all outputs from the same category */
158     for( i = 0; i < p_sys->i_id; i++ )
159     {
160         id = p_sys->id[i];
161         if( !id->b_used && id->fmt.i_cat == p_fmt->i_cat )
162         {
163             TAB_REMOVE( p_sys->i_id, p_sys->id, id );
164             sout_StreamIdDel( p_sys->p_out, id->id );
165             es_format_Clean( &id->fmt );
166             free( id );
167
168             i = 0;
169             continue;
170         }
171     }
172
173     msg_Dbg( p_stream, "creating new output" );
174     id = malloc( sizeof( sout_stream_id_t ) );
175     es_format_Copy( &id->fmt, p_fmt );
176     id->b_used           = VLC_TRUE;
177     id->id               = sout_StreamIdAdd( p_sys->p_out, &id->fmt );
178     if( id->id == NULL )
179     {
180         free( id );
181         return NULL;
182     }
183     TAB_APPEND( p_sys->i_id, p_sys->id, id );
184
185     return id;
186 }
187
188 /*****************************************************************************
189  * Del:
190  *****************************************************************************/
191 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
192 {
193     id->b_used = VLC_FALSE;
194     return VLC_SUCCESS;
195 }
196
197 /*****************************************************************************
198  * Send:
199  *****************************************************************************/
200 static int Send( sout_stream_t *p_stream,
201                  sout_stream_id_t *id, block_t *p_buffer )
202 {
203     sout_stream_sys_t *p_sys = p_stream->p_sys;
204
205     return sout_StreamIdSend( p_sys->p_out, id->id, p_buffer );
206 }
207