Rename all sout_Cfg* stuff to config_Chain* (as it isn't really sout specific)
[vlc.git] / modules / stream_out / bridge.c
1 /*****************************************************************************
2  * bridge.c: bridge stream output module
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@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
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 #define ID_TEXT N_("ID")
37 #define ID_LONGTEXT N_( \
38     "Integer identifier for this elementary stream. This will be used to " \
39     "\"find\" this stream later." )
40
41 #define DELAY_TEXT N_("Delay")
42 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
43         "will be delayed according to this value (in milliseconds, should be "\
44         ">= 100 ms). For high values, you will need to raise caching values." )
45
46 #define ID_OFFSET_TEXT N_("ID Offset")
47 #define ID_OFFSET_LONGTEXT N_("Offset to add to the stream IDs specified in " \
48         "bridge_out to obtain the stream IDs bridge_in will register.")
49
50 static int  OpenOut ( vlc_object_t * );
51 static void CloseOut( vlc_object_t * );
52 static int  OpenIn  ( vlc_object_t * );
53 static void CloseIn ( vlc_object_t * );
54
55 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
56 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
57
58 vlc_module_begin();
59     set_shortname( _("Bridge"));
60     set_description( _("Bridge stream output"));
61     add_submodule();
62     set_section( N_("Bridge out"), NULL );
63     set_capability( "sout stream", 50 );
64     add_shortcut( "bridge-out" );
65     /* Only usable with VLM. No category so not in gui preferences
66     set_category( CAT_SOUT );
67     set_subcategory( SUBCAT_SOUT_STREAM );*/
68     add_integer( SOUT_CFG_PREFIX_OUT "id", 0, NULL, ID_TEXT, ID_LONGTEXT,
69                  VLC_FALSE );
70     set_callbacks( OpenOut, CloseOut );
71
72     add_submodule();
73     set_section( N_("Bridge in"), NULL );
74     set_capability( "sout stream", 50 );
75     add_shortcut( "bridge-in" );
76     /*set_category( CAT_SOUT );
77     set_subcategory( SUBCAT_SOUT_STREAM );*/
78     add_integer( SOUT_CFG_PREFIX_IN "delay", 0, NULL, DELAY_TEXT,
79                  DELAY_LONGTEXT, VLC_FALSE );
80     add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
81                  ID_OFFSET_LONGTEXT, VLC_FALSE );
82     set_callbacks( OpenIn, CloseIn );
83
84     var_Create( p_module->p_libvlc_global, "bridge-lock", VLC_VAR_MUTEX );
85 vlc_module_end();
86
87
88 /*****************************************************************************
89  * Local prototypes
90  *****************************************************************************/
91 static const char *ppsz_sout_options_out[] = {
92     "id", NULL
93 };
94
95 static const char *ppsz_sout_options_in[] = {
96     "delay", "id-offset", NULL
97 };
98
99 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
100 static int               DelOut ( sout_stream_t *, sout_stream_id_t * );
101 static int               SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
102
103 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
104 static int               DelIn ( sout_stream_t *, sout_stream_id_t * );
105 static int               SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
106
107 typedef struct bridged_es_t
108 {
109     es_format_t fmt;
110     block_t *p_block;
111     block_t **pp_last;
112     vlc_bool_t b_empty;
113
114     /* bridge in part */
115     sout_stream_id_t *id;
116     mtime_t i_last;
117     vlc_bool_t b_changed;
118 } bridged_es_t;
119
120 typedef struct bridge_t
121 {
122     bridged_es_t **pp_es;
123     int i_es_num;
124 } bridge_t;
125
126 #define GetBridge(a) __GetBridge( VLC_OBJECT(a) )
127 static bridge_t *__GetBridge( vlc_object_t *p_object )
128 {
129     libvlc_global_data_t *p_libvlc_global = p_object->p_libvlc_global;
130     bridge_t *p_bridge;
131     vlc_value_t val;
132
133     if( var_Get( p_libvlc_global, "bridge-struct", &val ) != VLC_SUCCESS )
134     {
135         p_bridge = NULL;
136     }
137     else
138     {
139         p_bridge = val.p_address;
140     }
141
142     return p_bridge;
143 }
144
145
146 /*
147  * Bridge out
148  */
149
150 typedef struct out_sout_stream_sys_t
151 {
152     vlc_mutex_t *p_lock;
153     bridged_es_t *p_es;
154     int i_id;
155     vlc_bool_t b_inited;
156 } out_sout_stream_sys_t;
157
158 /*****************************************************************************
159  * OpenOut:
160  *****************************************************************************/
161 static int OpenOut( vlc_object_t *p_this )
162 {
163     sout_stream_t     *p_stream = (sout_stream_t *)p_this;
164     out_sout_stream_sys_t *p_sys;
165     vlc_value_t val;
166
167     config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
168                    p_stream->p_cfg );
169
170     p_sys          = malloc( sizeof( out_sout_stream_sys_t ) );
171     p_sys->b_inited = VLC_FALSE;
172
173     var_Get( p_this->p_libvlc_global, "bridge-lock", &val );
174     p_sys->p_lock = val.p_address;
175
176     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
177     p_sys->i_id = val.i_int;
178
179     p_stream->pf_add    = AddOut;
180     p_stream->pf_del    = DelOut;
181     p_stream->pf_send   = SendOut;
182
183     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
184
185     p_stream->p_sout->i_out_pace_nocontrol++;
186
187     return VLC_SUCCESS;
188 }
189
190 /*****************************************************************************
191  * CloseOut:
192  *****************************************************************************/
193 static void CloseOut( vlc_object_t * p_this )
194 {
195     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
196     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
197
198     p_stream->p_sout->i_out_pace_nocontrol--;
199
200     free( p_sys );
201 }
202
203 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
204 {
205     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
206     bridge_t *p_bridge;
207     bridged_es_t *p_es;
208     int i;
209
210     if ( p_sys->b_inited )
211     {
212         return NULL;
213     }
214     p_sys->b_inited = VLC_TRUE;
215
216     vlc_mutex_lock( p_sys->p_lock );
217
218     p_bridge = GetBridge( p_stream );
219     if ( p_bridge == NULL )
220     {
221         libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
222         vlc_value_t val;
223
224         p_bridge = malloc( sizeof( bridge_t ) );
225
226         var_Create( p_libvlc_global, "bridge-struct", VLC_VAR_ADDRESS );
227         val.p_address = p_bridge;
228         var_Set( p_libvlc_global, "bridge-struct", val );
229
230         p_bridge->i_es_num = 0;
231         p_bridge->pp_es = NULL;
232     }
233
234     for ( i = 0; i < p_bridge->i_es_num; i++ )
235     {
236         if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
237             break;
238     }
239
240     if ( i == p_bridge->i_es_num )
241     {
242         p_bridge->pp_es = realloc( p_bridge->pp_es,
243                                    (p_bridge->i_es_num + 1)
244                                      * sizeof(bridged_es_t *) );
245         p_bridge->i_es_num++;
246         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
247     }
248
249     p_sys->p_es = p_es = p_bridge->pp_es[i];
250
251     p_es->fmt = *p_fmt;
252     p_es->fmt.i_id = p_sys->i_id;
253     p_es->p_block = NULL;
254     p_es->pp_last = &p_es->p_block;
255     p_es->b_empty = VLC_FALSE;
256
257     p_es->id = NULL;
258     p_es->i_last = 0;
259     p_es->b_changed = VLC_TRUE;
260
261     msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
262              (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
263
264     vlc_mutex_unlock( p_sys->p_lock );
265
266     return (sout_stream_id_t *)p_sys;
267 }
268
269 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
270 {
271     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
272     bridged_es_t *p_es;
273
274     if ( !p_sys->b_inited )
275     {
276         return VLC_SUCCESS;
277     }
278
279     vlc_mutex_lock( p_sys->p_lock );
280
281     p_es = p_sys->p_es;
282
283     p_es->b_empty = VLC_TRUE;
284     block_ChainRelease( p_es->p_block );
285     p_es->p_block = VLC_FALSE;
286
287     p_es->b_changed = VLC_TRUE;
288     vlc_mutex_unlock( p_sys->p_lock );
289
290     p_sys->b_inited = VLC_FALSE;
291
292     return VLC_SUCCESS;
293 }
294
295 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
296                     block_t *p_buffer )
297 {
298     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
299     bridged_es_t *p_es;
300
301     if ( (out_sout_stream_sys_t *)id != p_sys )
302     {
303         block_ChainRelease( p_buffer );
304         return VLC_SUCCESS;
305     }
306
307     vlc_mutex_lock( p_sys->p_lock );
308
309     p_es = p_sys->p_es;
310     *p_es->pp_last = p_buffer;
311     while ( p_buffer != NULL )
312     {
313         p_es->pp_last = &p_buffer->p_next;
314         p_buffer = p_buffer->p_next;
315     }
316
317     vlc_mutex_unlock( p_sys->p_lock );
318
319     return VLC_SUCCESS;
320 }
321
322
323 /*
324  * Bridge in
325  */
326
327 typedef struct in_sout_stream_sys_t
328 {
329     sout_stream_t *p_out;
330     vlc_mutex_t *p_lock;
331     int i_id_offset;
332     mtime_t i_delay;
333 } in_sout_stream_sys_t;
334
335 /*****************************************************************************
336  * OpenIn:
337  *****************************************************************************/
338 static int OpenIn( vlc_object_t *p_this )
339 {
340     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
341     in_sout_stream_sys_t *p_sys;
342     vlc_value_t val;
343
344     p_sys          = malloc( sizeof( in_sout_stream_sys_t ) );
345
346     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
347     if( !p_sys->p_out )
348     {
349         msg_Err( p_stream, "cannot create chain" );
350         free( p_sys );
351         return VLC_EGENERIC;
352     }
353
354     config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
355                    p_stream->p_cfg );
356
357     var_Get( p_this->p_libvlc_global, "bridge-lock", &val );
358     p_sys->p_lock = val.p_address;
359
360     var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
361     p_sys->i_id_offset = val.i_int;
362
363     var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
364     p_sys->i_delay = (mtime_t)val.i_int * 1000;
365
366     p_stream->pf_add    = AddIn;
367     p_stream->pf_del    = DelIn;
368     p_stream->pf_send   = SendIn;
369
370     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
371
372     /* update p_sout->i_out_pace_nocontrol */
373     p_stream->p_sout->i_out_pace_nocontrol++;
374
375     return VLC_SUCCESS;
376 }
377
378 /*****************************************************************************
379  * CloseIn:
380  *****************************************************************************/
381 static void CloseIn( vlc_object_t * p_this )
382 {
383     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
384     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
385
386     sout_StreamDelete( p_sys->p_out );
387     p_stream->p_sout->i_out_pace_nocontrol--;
388
389     free( p_sys );
390 }
391
392 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
393 {
394     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
395
396     return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
397 }
398
399 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
400 {
401     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
402
403     return p_sys->p_out->pf_del( p_sys->p_out, id );
404 }
405
406 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
407                    block_t *p_buffer )
408 {
409     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
410     bridge_t *p_bridge;
411     vlc_bool_t b_no_es = VLC_TRUE;
412     int i;
413
414     /* First forward the packet for our own ES */
415     p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );
416
417     /* Then check all bridged streams */
418     vlc_mutex_lock( p_sys->p_lock );
419
420     p_bridge = GetBridge( p_stream );
421     if ( p_bridge == NULL )
422     {
423         vlc_mutex_unlock( p_sys->p_lock );
424         return VLC_SUCCESS;
425     }
426
427     for ( i = 0; i < p_bridge->i_es_num; i++ )
428     {
429         if ( !p_bridge->pp_es[i]->b_empty )
430             b_no_es = VLC_FALSE;
431
432         while ( p_bridge->pp_es[i]->p_block != NULL
433                  && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
434                        < mdate()
435                       || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
436                           < p_bridge->pp_es[i]->i_last) )
437         {
438             block_t *p_block = p_bridge->pp_es[i]->p_block;
439             msg_Dbg( p_stream, "dropping a packet (" I64Fd ")",
440                      mdate() - p_block->i_dts - p_sys->i_delay );
441             p_bridge->pp_es[i]->p_block
442                 = p_bridge->pp_es[i]->p_block->p_next;
443             block_Release( p_block );
444         }
445
446         if ( p_bridge->pp_es[i]->p_block == NULL )
447         {
448             p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
449         }
450
451         if ( p_bridge->pp_es[i]->b_changed )
452         {
453             if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
454             {
455                 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
456             }
457             else
458             {
459                 /* We need at least two packets to enter the mux. */
460                 if ( p_bridge->pp_es[i]->p_block == NULL
461                       || p_bridge->pp_es[i]->p_block->p_next == NULL )
462                 {
463                     continue;
464                 }
465
466                 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
467                 p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
468                             p_sys->p_out, &p_bridge->pp_es[i]->fmt );
469                 if ( p_bridge->pp_es[i]->id == NULL )
470                 {
471                     msg_Warn( p_stream, "couldn't create chain for id %d",
472                               p_bridge->pp_es[i]->fmt.i_id );
473                 }
474                 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
475                          (char*)&p_bridge->pp_es[i]->fmt.i_codec,
476                          p_bridge->pp_es[i]->fmt.i_id, i );
477             }
478         }
479         p_bridge->pp_es[i]->b_changed = VLC_FALSE;
480
481         if ( p_bridge->pp_es[i]->b_empty )
482             continue;
483
484         if ( p_bridge->pp_es[i]->p_block == NULL )
485         {
486             if ( p_bridge->pp_es[i]->id != NULL
487                   && p_bridge->pp_es[i]->i_last < mdate() )
488             {
489                 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
490                 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
491                 p_bridge->pp_es[i]->b_changed = VLC_TRUE;
492                 p_bridge->pp_es[i]->id = NULL;
493             }
494             continue;
495         }
496
497         if ( p_bridge->pp_es[i]->id != NULL )
498         {
499             block_t *p_block = p_bridge->pp_es[i]->p_block;
500             while ( p_block != NULL )
501             {
502                 p_bridge->pp_es[i]->i_last = p_block->i_dts;
503                 p_block->i_pts += p_sys->i_delay;
504                 p_block->i_dts += p_sys->i_delay;
505                 p_block = p_block->p_next;
506             }
507             p_sys->p_out->pf_send( p_sys->p_out, p_bridge->pp_es[i]->id,
508                                    p_bridge->pp_es[i]->p_block );
509         }
510         else
511         {
512             block_ChainRelease( p_bridge->pp_es[i]->p_block );
513         }
514
515         p_bridge->pp_es[i]->p_block = NULL;
516         p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
517     }
518
519     if( b_no_es )
520     {
521         libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
522         for ( i = 0; i < p_bridge->i_es_num; i++ )
523             free( p_bridge->pp_es[i] );
524         free( p_bridge->pp_es );
525         free( p_bridge );
526         var_Destroy( p_libvlc_global, "bridge-struct" );
527     }
528
529     vlc_mutex_unlock( p_sys->p_lock );
530
531     return VLC_SUCCESS;
532 }