* all: new sout scheme. Now a chain of module are created that can
[vlc.git] / include / stream_output.h
1 /*****************************************************************************
2  * stream_output.h : stream output module
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: stream_output.h,v 1.10 2003/04/13 20:00:20 fenrir Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Eric Petit <titer@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * sout_instance_t: stream output thread descriptor
28  *****************************************************************************/
29
30 /*
31  * i_allocated_size: size of allocated buffer
32  * p_allocated_buffer: where data has been allocated
33  *
34  * i_buffer_size: sizeof buffer from p_buffer
35  * p_buffer: where data begins
36  * i_size: size of valid data
37  *
38  */
39 #define SOUT_BUFFER_FLAGS_HEADER    0x0001
40 struct sout_buffer_t
41 {
42     size_t                  i_allocated_size;
43     byte_t                  *p_allocated_buffer;
44
45     size_t                  i_buffer_size;
46     byte_t                  *p_buffer;
47
48     size_t                  i_size;
49     mtime_t                 i_length;
50
51     mtime_t                 i_dts;
52     mtime_t                 i_pts;
53
54     uint32_t                i_flags;
55     int                     i_bitrate;
56
57     struct sout_buffer_t    *p_next;
58 };
59
60 struct sout_format_t
61 {
62     int             i_cat;
63     vlc_fourcc_t    i_fourcc;
64
65     /* audio */
66     int             i_sample_rate;
67     int             i_channels;
68     int             i_block_align;
69
70     /* video */
71     int             i_width;
72     int             i_height;
73
74     int             i_bitrate;
75     int             i_extra_data;
76     uint8_t         *p_extra_data;
77
78 };
79
80 struct sout_fifo_t
81 {
82     vlc_mutex_t         lock;                         /* fifo data lock */
83     vlc_cond_t          wait;         /* fifo data conditional variable */
84
85     int                 i_depth;
86     sout_buffer_t       *p_first;
87     sout_buffer_t       **pp_last;
88 };
89
90 typedef struct sout_stream_id_t  sout_stream_id_t;
91
92 /* for mux */
93 struct sout_input_t
94 {
95     sout_instance_t *p_sout;
96
97     sout_format_t   *p_fmt;
98     sout_fifo_t     *p_fifo;
99
100     void            *p_sys;
101 };
102
103 /* for packetizer */
104 struct sout_packetizer_input_t
105 {
106
107     sout_instance_t     *p_sout;
108
109     sout_format_t       *p_fmt;
110
111     sout_stream_id_t    *id;
112 };
113
114
115 #define SOUT_METHOD_NONE        0x00
116 #define SOUT_METHOD_FILE        0x10
117 #define SOUT_METHOD_NETWORK     0x20
118
119 typedef struct sout_access_out_sys_t   sout_access_out_sys_t;
120 struct sout_access_out_t
121 {
122     VLC_COMMON_MEMBERS
123
124     module_t                *p_module;
125
126     sout_instance_t         *p_sout;
127
128     char                    *psz_access;
129     char                    *psz_name;
130     sout_access_out_sys_t   *p_sys;
131     int                     (* pf_seek  )( sout_access_out_t *,
132                                            off_t );
133     int                     (* pf_write )( sout_access_out_t *,
134                                            sout_buffer_t * );
135 };
136 /*
137  * i_query parameter of pf_mux_capacity
138  */
139 /* SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:    p_args=NULL, p_answer=&boolean */
140 #define SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME    0x01
141 /* SOUT_MUX_CAP_GET_STREAMABLE:             p_args=NULL, p_answer=&boolean */
142 #define SOUT_MUX_CAP_GET_STREAMABLE             0x02
143 /*
144  * return error code
145  */
146 #define SOUT_MUX_CAP_ERR_OK                 0x00
147 #define SOUT_MUX_CAP_ERR_UNKNOWN            0x01
148 #define SOUT_MUX_CAP_ERR_UNIMPLEMENTED      0x02
149
150 typedef struct sout_mux_sys_t sout_mux_sys_t;
151 struct  sout_mux_t
152 {
153     VLC_COMMON_MEMBERS
154     module_t                *p_module;
155
156     sout_instance_t         *p_sout;
157
158     char                    *psz_mux;
159
160     sout_access_out_t       *p_access;
161
162     int                     i_preheader;
163     int                     (* pf_capacity)  ( sout_mux_t *,
164                                                int, void *, void *);
165     int                     (* pf_addstream )( sout_mux_t *,
166                                                sout_input_t * );
167     int                     (* pf_delstream )( sout_mux_t *,
168                                                sout_input_t * );
169     int                     (* pf_mux )      ( sout_mux_t * );
170
171
172     /* here are all inputs accepted by muxer */
173     int                     i_nb_inputs;
174     sout_input_t            **pp_inputs;
175
176
177     /* mux private */
178     sout_mux_sys_t          *p_sys;
179
180 //    /* creater private */
181 //    void                    *p_sys_owner;
182
183     /* XXX private to stream_output.c */
184     /* if muxer doesn't support adding stream at any time then we first wait
185      *  for stream then we refuse all stream and start muxing */
186     vlc_bool_t  b_add_stream_any_time;
187     vlc_bool_t  b_waiting_stream;
188     /* we wait one second after first stream added */
189     mtime_t     i_add_stream_start;
190 };
191
192
193
194 struct sout_cfg_t
195 {
196     sout_cfg_t  *p_next;
197
198     char        *psz_name;
199     char        *psz_value;
200 };
201
202 typedef struct sout_stream_sys_t sout_stream_sys_t;
203 struct sout_stream_t
204 {
205     VLC_COMMON_MEMBERS
206
207     module_t                *p_module;
208     sout_instance_t         *p_sout;
209
210     char                    *psz_name;
211     sout_cfg_t              *p_cfg;
212     char                    *psz_next;
213
214     /* add, remove a stream */
215     sout_stream_id_t *      (*pf_add) ( sout_stream_t *, sout_format_t * );
216     int                     (*pf_del) ( sout_stream_t *, sout_stream_id_t * );
217
218     /* manage a packet */
219     int                     (*pf_send)( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
220
221     /* private */
222     sout_stream_sys_t       *p_sys;
223 };
224
225 typedef struct sout_instance_sys_t sout_instance_sys_t;
226 struct sout_instance_t
227 {
228     VLC_COMMON_MEMBERS
229
230     char * psz_sout;
231     char * psz_chain;
232
233     /* muxer data */
234     int                     i_preheader;    /* max over all muxer */
235
236     vlc_mutex_t             lock;
237     sout_stream_t           *p_stream;
238
239     /* sout private */
240     sout_instance_sys_t     *p_sys;
241 };
242
243 /* some macro */
244 #define TAB_APPEND( count, tab, p )             \
245     if( (count) > 0 )                           \
246     {                                           \
247         (tab) = realloc( (tab), sizeof( void ** ) * ( (count) + 1 ) ); \
248     }                                           \
249     else                                        \
250     {                                           \
251         (tab) = malloc( sizeof( void ** ) );    \
252     }                                           \
253     (void**)(tab)[(count)] = (void*)(p);        \
254     (count)++
255
256 #define TAB_FIND( count, tab, p, index )        \
257     {                                           \
258         int _i_;                                \
259         (index) = -1;                           \
260         for( _i_ = 0; _i_ < (count); _i_++ )    \
261         {                                       \
262             if((void**)(tab)[_i_]==(void*)(p))  \
263             {                                   \
264                 (index) = _i_;                  \
265                 break;                          \
266             }                                   \
267         }                                       \
268     }
269
270 #define TAB_REMOVE( count, tab, p )             \
271     {                                           \
272         int i_index;                            \
273         TAB_FIND( count, tab, p, i_index );     \
274         if( i_index >= 0 )                      \
275         {                                       \
276             if( count > 1 )                     \
277             {                                   \
278                 memmove( ((void**)tab + i_index),    \
279                          ((void**)tab + i_index+1),  \
280                          ( (count) - i_index - 1 ) * sizeof( void* ) );\
281             }                                   \
282             else                                \
283             {                                   \
284                 free( tab );                    \
285                 (tab) = NULL;                   \
286             }                                   \
287             (count)--;                          \
288         }                                       \
289     }
290
291 static inline sout_cfg_t *sout_cfg_find( sout_cfg_t *p_cfg, char *psz_name )
292 {
293     while( p_cfg && strcmp( p_cfg->psz_name, psz_name ) )
294     {
295         p_cfg = p_cfg->p_next;
296     }
297
298     return p_cfg;
299 }
300
301 static inline char *sout_cfg_find_value( sout_cfg_t *p_cfg, char *psz_name )
302 {
303     while( p_cfg && strcmp( p_cfg->psz_name, psz_name ) )
304     {
305         p_cfg = p_cfg->p_next;
306     }
307
308     if( p_cfg && p_cfg->psz_value )
309     {
310         return( p_cfg->psz_value );
311     }
312
313     return NULL;
314 }
315 /*****************************************************************************
316  * Prototypes
317  *****************************************************************************/
318 #define sout_NewInstance(a,b) __sout_NewInstance(VLC_OBJECT(a),b)
319 VLC_EXPORT( sout_instance_t *, __sout_NewInstance,  ( vlc_object_t *, char * ) );
320 VLC_EXPORT( void,              sout_DeleteInstance, ( sout_instance_t * ) );
321
322 VLC_EXPORT( sout_fifo_t *,   sout_FifoCreate,     ( sout_instance_t * ) );
323 VLC_EXPORT( void,            sout_FifoDestroy,    ( sout_instance_t *, sout_fifo_t * ) );
324 VLC_EXPORT( void,            sout_FifoFree,       ( sout_instance_t *,sout_fifo_t * ) );
325
326 VLC_EXPORT( void,            sout_FifoPut,        ( sout_fifo_t *, sout_buffer_t* ) );
327 VLC_EXPORT( sout_buffer_t *, sout_FifoGet,        ( sout_fifo_t * ) );
328 VLC_EXPORT( sout_buffer_t *, sout_FifoShow,       ( sout_fifo_t * ) );
329
330
331 #define sout_InputNew( a, b ) __sout_InputNew( VLC_OBJECT(a), b )
332 VLC_EXPORT( sout_packetizer_input_t *, __sout_InputNew,       ( vlc_object_t *, sout_format_t * ) );
333 VLC_EXPORT( int,            sout_InputDelete,      ( sout_packetizer_input_t * ) );
334 VLC_EXPORT( int,            sout_InputSendBuffer,  ( sout_packetizer_input_t *, sout_buffer_t* ) );
335
336 VLC_EXPORT( sout_buffer_t*, sout_BufferNew,    ( sout_instance_t *, size_t ) );
337 VLC_EXPORT( int,            sout_BufferRealloc,( sout_instance_t *, sout_buffer_t*, size_t ) );
338 VLC_EXPORT( int,            sout_BufferReallocFromPreHeader,( sout_instance_t *, sout_buffer_t*, size_t ) );
339 VLC_EXPORT( int,            sout_BufferDelete, ( sout_instance_t *, sout_buffer_t* ) );
340 VLC_EXPORT( sout_buffer_t*, sout_BufferDuplicate,(sout_instance_t *, sout_buffer_t * ) );
341 VLC_EXPORT( void,           sout_BufferChain,  ( sout_buffer_t **, sout_buffer_t * ) );
342
343 VLC_EXPORT( sout_access_out_t *, sout_AccessOutNew, ( sout_instance_t *, char *psz_access, char *psz_name ) );
344 VLC_EXPORT( void,                sout_AccessOutDelete, ( sout_access_out_t * ) );
345 VLC_EXPORT( int,                 sout_AccessOutSeek,   ( sout_access_out_t *, off_t ) );
346 VLC_EXPORT( int,                 sout_AccessOutWrite,  ( sout_access_out_t *, sout_buffer_t * ) );
347
348 VLC_EXPORT( sout_mux_t *,       sout_MuxNew,          ( sout_instance_t*, char *, sout_access_out_t * ) );
349 VLC_EXPORT( sout_input_t *,     sout_MuxAddStream,    ( sout_mux_t *, sout_format_t * ) );
350 VLC_EXPORT( void,               sout_MuxDeleteStream, ( sout_mux_t *, sout_input_t * ) );
351 VLC_EXPORT( void,               sout_MuxDelete,       ( sout_mux_t * ) );
352 VLC_EXPORT( void,               sout_MuxSendBuffer, ( sout_mux_t *, sout_input_t  *, sout_buffer_t * ) );
353
354 VLC_EXPORT( char *,             sout_cfg_parser, ( char **, sout_cfg_t **, char * ) );
355 VLC_EXPORT( sout_stream_t *,    sout_stream_new, ( sout_instance_t *, char *psz_chain ) );
356 VLC_EXPORT( void,               sout_stream_delete, ( sout_stream_t *p_stream ) );
357