qt: playlist: use item title if available
[vlc.git] / modules / stream_out / gather.c
blob15264730c51094f9cfe6a81d8b43127464f89fa1
1 /*****************************************************************************
2 * gather.c: gathering stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
34 #include <vlc_block.h>
35 #include <vlc_list.h>
37 /*****************************************************************************
38 * Module descriptor
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
43 vlc_module_begin ()
44 set_description( N_("Gathering stream output") )
45 set_capability( "sout filter", 50 )
46 add_shortcut( "gather" )
47 set_callbacks( Open, Close )
48 vlc_module_end ()
50 /*****************************************************************************
51 * Exported prototypes
52 *****************************************************************************/
53 static void *Add( sout_stream_t *, const es_format_t * );
54 static void Del( sout_stream_t *, void * );
55 static int Send( sout_stream_t *, void *, block_t * );
57 typedef struct
59 bool b_used;
60 bool b_streamswap;
62 es_format_t fmt;
63 void *id;
64 struct vlc_list node;
65 } sout_stream_id_sys_t;
67 typedef struct
69 struct vlc_list ids;
70 } sout_stream_sys_t;
72 static const struct sout_stream_operations ops = {
73 Add, Del, Send, NULL, NULL,
76 /*****************************************************************************
77 * Open:
78 *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
81 sout_stream_t *p_stream = (sout_stream_t*)p_this;
82 sout_stream_sys_t *p_sys;
84 p_stream->p_sys = p_sys = malloc( sizeof( sout_stream_sys_t ) );
85 if( p_sys == NULL )
86 return VLC_EGENERIC;
88 vlc_list_init(&p_sys->ids);
89 p_stream->ops = &ops;
90 return VLC_SUCCESS;
93 /*****************************************************************************
94 * Close:
95 *****************************************************************************/
96 static void Close( vlc_object_t * p_this )
98 sout_stream_t *p_stream = (sout_stream_t*)p_this;
99 sout_stream_sys_t *p_sys = p_stream->p_sys;
100 sout_stream_id_sys_t *id;
102 vlc_list_foreach (id, &p_sys->ids, node)
104 sout_StreamIdDel( p_stream->p_next, id->id );
105 es_format_Clean( &id->fmt );
106 free( id );
109 free( p_sys );
112 /*****************************************************************************
113 * Add:
114 *****************************************************************************/
115 static void *Add( sout_stream_t *p_stream, const es_format_t *p_fmt )
117 sout_stream_sys_t *p_sys = p_stream->p_sys;
118 sout_stream_id_sys_t *id;
120 /* search a compatible output */
121 vlc_list_foreach (id, &p_sys->ids, node)
123 if( id->b_used )
124 continue;
126 if( id->fmt.i_cat != p_fmt->i_cat || id->fmt.i_codec != p_fmt->i_codec )
127 continue;
129 if( id->fmt.i_cat == AUDIO_ES )
131 audio_format_t *p_a = &id->fmt.audio;
132 if( p_a->i_rate != p_fmt->audio.i_rate ||
133 p_a->i_channels != p_fmt->audio.i_channels ||
134 p_a->i_blockalign != p_fmt->audio.i_blockalign )
135 continue;
137 else if( id->fmt.i_cat == VIDEO_ES )
139 video_format_t *p_v = &id->fmt.video;
140 if( p_v->i_width != p_fmt->video.i_width ||
141 p_v->i_height != p_fmt->video.i_height )
142 continue;
145 /* */
146 msg_Dbg( p_stream, "reusing already opened output" );
147 id->b_used = true;
148 id->b_streamswap = true;
149 return id;
152 /* destroy all outputs from the same category */
153 vlc_list_foreach (id, &p_sys->ids, node)
154 if( !id->b_used && id->fmt.i_cat == p_fmt->i_cat )
156 sout_StreamIdDel( p_stream->p_next, id->id );
157 es_format_Clean( &id->fmt );
158 free( id );
161 msg_Dbg( p_stream, "creating new output" );
162 id = malloc( sizeof( sout_stream_id_sys_t ) );
163 if( id == NULL )
164 return NULL;
165 es_format_Copy( &id->fmt, p_fmt );
166 id->b_streamswap = false;
167 id->b_used = true;
168 id->id = sout_StreamIdAdd( p_stream->p_next, &id->fmt );
169 if( id->id == NULL )
171 free( id );
172 return NULL;
175 vlc_list_append(&id->node, &p_sys->ids);
176 return id;
179 /*****************************************************************************
180 * Del:
181 *****************************************************************************/
182 static void Del( sout_stream_t *p_stream, void *_id )
184 VLC_UNUSED(p_stream);
185 sout_stream_id_sys_t *id = (sout_stream_id_sys_t *)_id;
186 id->b_used = false;
189 /*****************************************************************************
190 * Send:
191 *****************************************************************************/
192 static int Send( sout_stream_t *p_stream, void *_id, block_t *p_buffer )
194 sout_stream_id_sys_t *id = (sout_stream_id_sys_t *)_id;
195 if ( id->b_streamswap )
197 id->b_streamswap = false;
198 p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
200 return sout_StreamIdSend( p_stream->p_next, id->id, p_buffer );