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