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