]> git.sesse.net Git - vlc/blob - modules/stream_out/bridge.c
127634c4c15b83f2de07844dac41fd6ca9c4a1c1
[vlc] / modules / stream_out / bridge.c
1 /*****************************************************************************
2  * bridge.c: bridge stream output module
3  *****************************************************************************
4  * Copyright (C) 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Antoine Cellerier <dionoea at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <assert.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38 #include <vlc_block.h>
39 #include <vlc_memory.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 #define ID_TEXT N_("ID")
45 #define ID_LONGTEXT N_( \
46     "Integer identifier for this elementary stream. This will be used to " \
47     "\"find\" this stream later." )
48
49 #define DEST_TEXT N_( "Destination bridge-in name" )
50 #define DEST_LONGTEXT N_( \
51     "Name of the destination bridge-in. If you do not need more " \
52     "than one bridge-in at a time, you can discard this option." )
53
54 #define DELAY_TEXT N_("Delay")
55 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
56         "will be delayed according to this value (in milliseconds, should be "\
57         ">= 100 ms). For high values, you will need to raise caching values." )
58
59 #define ID_OFFSET_TEXT N_("ID Offset")
60 #define ID_OFFSET_LONGTEXT N_("Offset to add to the stream IDs specified in " \
61         "bridge_out to obtain the stream IDs bridge_in will register.")
62
63 #define NAME_TEXT N_( "Name of current instance" )
64 #define NAME_LONGTEXT N_( \
65     "Name of this bridge-in instance. If you do not need more " \
66     "than one bridge-in at a time, you can discard this option." )
67
68 #define PLACEHOLDER_TEXT N_( "Fallback to placeholder stream when out of data" )
69 #define PLACEHOLDER_LONGTEXT N_( \
70     "If set to true, the bridge will discard all input elementary streams " \
71     "except if it doesn't receive data from another bridge-in. This can " \
72     "be used to configure a place holder stream when the real source " \
73     "breaks. Source and placeholder streams should have the same format. " )
74
75 #define PLACEHOLDER_DELAY_TEXT N_( "Placeholder delay" )
76 #define PLACEHOLDER_DELAY_LONGTEXT N_( \
77     "Delay (in ms) before the placeholder kicks in." )
78
79 #define PLACEHOLDER_IFRAME_TEXT N_( "Wait for I frame before toggling placholder" )
80 #define PLACEHOLDER_IFRAME_LONGTEXT N_( \
81     "If enabled, switching between the placeholder and the normal stream " \
82     "will only occur on I frames. This will remove artifacts on stream " \
83     "switching at the expense of a slightly longer delay, depending on " \
84     "the frequence of I frames in the streams." )
85
86 static int  OpenOut ( vlc_object_t * );
87 static void CloseOut( vlc_object_t * );
88 static int  OpenIn  ( vlc_object_t * );
89 static void CloseIn ( vlc_object_t * );
90
91 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
92 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
93
94 vlc_module_begin ()
95     set_shortname( N_("Bridge"))
96     set_description( N_("Bridge stream output"))
97     add_submodule ()
98     set_section( N_("Bridge out"), NULL )
99     set_capability( "sout stream", 50 )
100     add_shortcut( "bridge-out" )
101     /* Only usable with VLM. No category so not in gui preferences
102     set_category( CAT_SOUT )
103     set_subcategory( SUBCAT_SOUT_STREAM )*/
104     add_integer( SOUT_CFG_PREFIX_OUT "id", 0, NULL, ID_TEXT, ID_LONGTEXT,
105                  false )
106     add_string( SOUT_CFG_PREFIX_OUT "in-name", "default", NULL,
107                 DEST_TEXT, DEST_LONGTEXT, false )
108     set_callbacks( OpenOut, CloseOut )
109
110     add_submodule ()
111     set_section( N_("Bridge in"), NULL )
112     set_capability( "sout stream", 50 )
113     add_shortcut( "bridge-in" )
114     /*set_category( CAT_SOUT )
115     set_subcategory( SUBCAT_SOUT_STREAM )*/
116     add_integer( SOUT_CFG_PREFIX_IN "delay", 0, NULL, DELAY_TEXT,
117                  DELAY_LONGTEXT, false )
118     add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
119                  ID_OFFSET_LONGTEXT, false )
120     add_string( SOUT_CFG_PREFIX_IN "name", "default", NULL,
121                 NAME_TEXT, NAME_LONGTEXT, false )
122     add_bool( SOUT_CFG_PREFIX_IN "placeholder", false, NULL,
123               PLACEHOLDER_TEXT, PLACEHOLDER_LONGTEXT, false )
124     add_integer( SOUT_CFG_PREFIX_IN "placeholder-delay", 200, NULL,
125                  PLACEHOLDER_DELAY_TEXT, PLACEHOLDER_DELAY_LONGTEXT, false )
126     add_bool( SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", true, NULL,
127               PLACEHOLDER_IFRAME_TEXT, PLACEHOLDER_IFRAME_LONGTEXT, false )
128     set_callbacks( OpenIn, CloseIn )
129
130 vlc_module_end ()
131
132
133 /*****************************************************************************
134  * Local prototypes
135  *****************************************************************************/
136 static const char *const ppsz_sout_options_out[] = {
137     "id", "in-name", NULL
138 };
139
140 static const char *const ppsz_sout_options_in[] = {
141     "delay", "id-offset", "name",
142     "placeholder", "placeholder-delay", "placeholder-switch-on-iframe",
143     NULL
144 };
145
146 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
147 static int               DelOut ( sout_stream_t *, sout_stream_id_t * );
148 static int               SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
149
150 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
151 static int               DelIn ( sout_stream_t *, sout_stream_id_t * );
152 static int               SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
153
154 typedef struct bridged_es_t
155 {
156     es_format_t fmt;
157     block_t *p_block;
158     block_t **pp_last;
159     bool b_empty;
160
161     /* bridge in part */
162     sout_stream_id_t *id;
163     mtime_t i_last;
164     bool b_changed;
165 } bridged_es_t;
166
167 typedef struct bridge_t
168 {
169     bridged_es_t **pp_es;
170     int i_es_num;
171 } bridge_t;
172
173
174 /*
175  * Bridge out
176  */
177
178 typedef struct out_sout_stream_sys_t
179 {
180     vlc_mutex_t *p_lock;
181     bridged_es_t *p_es;
182     int i_id;
183     bool b_inited;
184
185     char *psz_name;
186 } out_sout_stream_sys_t;
187
188 /*****************************************************************************
189  * OpenOut:
190  *****************************************************************************/
191 static int OpenOut( vlc_object_t *p_this )
192 {
193     sout_stream_t     *p_stream = (sout_stream_t *)p_this;
194     out_sout_stream_sys_t *p_sys;
195     vlc_value_t val;
196
197     config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
198                    p_stream->p_cfg );
199
200     p_sys          = malloc( sizeof( out_sout_stream_sys_t ) );
201     assert( p_sys );
202     p_sys->b_inited = false;
203
204     var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
205     var_Get( p_this->p_libvlc, "bridge-lock", &val );
206     p_sys->p_lock = val.p_address;
207
208     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
209     p_sys->i_id = val.i_int;
210
211     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
212     if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
213     {
214         free( val.psz_string );
215         free( p_sys );
216         return VLC_ENOMEM;
217     }
218     free( val.psz_string );
219
220     p_stream->pf_add    = AddOut;
221     p_stream->pf_del    = DelOut;
222     p_stream->pf_send   = SendOut;
223
224     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
225
226     p_stream->p_sout->i_out_pace_nocontrol++;
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * CloseOut:
233  *****************************************************************************/
234 static void CloseOut( vlc_object_t * p_this )
235 {
236     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
237     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
238
239     p_stream->p_sout->i_out_pace_nocontrol--;
240
241     free( p_sys->psz_name );
242     free( p_sys );
243 }
244
245 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
246 {
247     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
248     bridge_t *p_bridge;
249     bridged_es_t *p_es;
250     int i;
251
252     if ( p_sys->b_inited )
253     {
254         msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
255         return NULL;
256     }
257     p_sys->b_inited = true;
258
259     vlc_mutex_lock( p_sys->p_lock );
260
261     p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
262     if ( p_bridge == NULL )
263     {
264         p_bridge = malloc( sizeof( bridge_t ) );
265         assert( p_bridge );
266
267         var_Create( p_stream->p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
268         var_SetAddress( p_stream->p_libvlc, p_sys->psz_name, p_bridge );
269
270         p_bridge->i_es_num = 0;
271         p_bridge->pp_es = NULL;
272     }
273
274     for ( i = 0; i < p_bridge->i_es_num; i++ )
275     {
276         if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
277             break;
278     }
279
280     if ( i == p_bridge->i_es_num )
281     {
282         p_bridge->pp_es = realloc_or_free( p_bridge->pp_es,
283                           (p_bridge->i_es_num + 1) * sizeof(bridged_es_t *) );
284         assert( p_bridge->pp_es );
285         p_bridge->i_es_num++;
286         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
287         assert( p_bridge->pp_es[i] );
288     }
289
290     p_sys->p_es = p_es = p_bridge->pp_es[i];
291
292     p_es->fmt = *p_fmt;
293     p_es->fmt.i_id = p_sys->i_id;
294     p_es->p_block = NULL;
295     p_es->pp_last = &p_es->p_block;
296     p_es->b_empty = false;
297
298     p_es->id = NULL;
299     p_es->i_last = 0;
300     p_es->b_changed = true;
301
302     msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
303              (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
304
305     vlc_mutex_unlock( p_sys->p_lock );
306
307     return (sout_stream_id_t *)p_sys;
308 }
309
310 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
311 {
312     VLC_UNUSED(id);
313     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
314     bridged_es_t *p_es;
315
316     if ( !p_sys->b_inited )
317     {
318         return VLC_SUCCESS;
319     }
320
321     vlc_mutex_lock( p_sys->p_lock );
322
323     p_es = p_sys->p_es;
324
325     p_es->b_empty = true;
326     block_ChainRelease( p_es->p_block );
327     p_es->p_block = false;
328
329     p_es->b_changed = true;
330     vlc_mutex_unlock( p_sys->p_lock );
331
332     p_sys->b_inited = false;
333
334     return VLC_SUCCESS;
335 }
336
337 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
338                     block_t *p_buffer )
339 {
340     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
341     bridged_es_t *p_es;
342
343     if ( (out_sout_stream_sys_t *)id != p_sys )
344     {
345         block_ChainRelease( p_buffer );
346         return VLC_SUCCESS;
347     }
348
349     vlc_mutex_lock( p_sys->p_lock );
350
351     p_es = p_sys->p_es;
352     *p_es->pp_last = p_buffer;
353     while ( p_buffer != NULL )
354     {
355         p_es->pp_last = &p_buffer->p_next;
356         p_buffer = p_buffer->p_next;
357     }
358
359     vlc_mutex_unlock( p_sys->p_lock );
360
361     return VLC_SUCCESS;
362 }
363
364
365 /*
366  * Bridge in
367  */
368
369 typedef struct in_sout_stream_sys_t
370 {
371     sout_stream_t *p_out;
372     vlc_mutex_t *p_lock;
373     int i_id_offset;
374     mtime_t i_delay;
375
376     char *psz_name;
377
378     bool b_placeholder;
379     bool b_switch_on_iframe;
380     int i_state;
381     mtime_t i_placeholder_delay;
382     sout_stream_id_t *id_video;
383     mtime_t i_last_video;
384     sout_stream_id_t *id_audio;
385     mtime_t i_last_audio;
386 } in_sout_stream_sys_t;
387
388 enum { placeholder_on, placeholder_off };
389
390 /*****************************************************************************
391  * OpenIn:
392  *****************************************************************************/
393 static int OpenIn( vlc_object_t *p_this )
394 {
395     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
396     in_sout_stream_sys_t *p_sys;
397     vlc_value_t val;
398
399     p_sys          = malloc( sizeof( in_sout_stream_sys_t ) );
400     assert( p_sys );
401
402     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
403     if( !p_sys->p_out )
404     {
405         msg_Err( p_stream, "cannot create chain" );
406         free( p_sys );
407         return VLC_EGENERIC;
408     }
409
410     config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
411                    p_stream->p_cfg );
412
413     var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
414     var_Get( p_this->p_libvlc, "bridge-lock", &val );
415     p_sys->p_lock = val.p_address;
416
417     var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
418     p_sys->i_id_offset = val.i_int;
419
420     var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
421     p_sys->i_delay = (mtime_t)val.i_int * 1000;
422
423     var_Get( p_stream, SOUT_CFG_PREFIX_IN "name", &val );
424     if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
425     {
426         free( val.psz_string );
427         free( p_sys );
428         return VLC_ENOMEM;
429     }
430     free( val.psz_string );
431
432     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder", &val );
433     p_sys->b_placeholder = val.b_bool;
434
435     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", &val);
436     p_sys->b_switch_on_iframe = val.b_bool;
437
438     p_sys->i_state = placeholder_on;
439
440     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-delay", &val );
441     p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
442
443     p_sys->i_last_video = 0;
444     p_sys->i_last_audio = 0;
445     p_sys->id_video = NULL;
446     p_sys->id_audio = NULL;
447
448     p_stream->pf_add    = AddIn;
449     p_stream->pf_del    = DelIn;
450     p_stream->pf_send   = SendIn;
451
452     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
453
454     /* update p_sout->i_out_pace_nocontrol */
455     p_stream->p_sout->i_out_pace_nocontrol++;
456
457     return VLC_SUCCESS;
458 }
459
460 /*****************************************************************************
461  * CloseIn:
462  *****************************************************************************/
463 static void CloseIn( vlc_object_t * p_this )
464 {
465     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
466     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
467
468     sout_StreamDelete( p_sys->p_out );
469     p_stream->p_sout->i_out_pace_nocontrol--;
470
471     free( p_sys->psz_name );
472     free( p_sys );
473 }
474
475 struct sout_stream_id_t
476 {
477     sout_stream_id_t *id;
478     int i_cat; /* es category. Used for placeholder option */
479 };
480
481 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
482 {
483     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
484
485     sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
486     if( !id ) return NULL;
487
488     id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
489     if( !id->id )
490     {
491         free( id );
492         return NULL;
493     }
494
495     if( p_sys->b_placeholder )
496     {
497         id->i_cat = p_fmt->i_cat;
498         switch( p_fmt->i_cat )
499         {
500             case VIDEO_ES:
501                 if( p_sys->id_video != NULL )
502                     msg_Err( p_stream, "We already had a video es!" );
503                 p_sys->id_video = id->id;
504                 break;
505             case AUDIO_ES:
506                 if( p_sys->id_audio != NULL )
507                     msg_Err( p_stream, "We already had an audio es!" );
508                 p_sys->id_audio = id->id;
509                 break;
510         }
511     }
512
513     return id;
514 }
515
516 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
517 {
518     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
519
520     if( id == p_sys->id_video ) p_sys->id_video = NULL;
521     if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
522
523     int ret = p_sys->p_out->pf_del( p_sys->p_out, id->id );
524
525     free( id );
526     return ret;
527 }
528
529 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
530                    block_t *p_buffer )
531 {
532     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
533     bridge_t *p_bridge;
534     bool b_no_es = true;
535     int i;
536     int i_date = mdate();
537
538     /* First forward the packet for our own ES */
539     if( !p_sys->b_placeholder )
540         p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
541
542     /* Then check all bridged streams */
543     vlc_mutex_lock( p_sys->p_lock );
544
545     p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
546
547     if( p_bridge )
548     {
549     for ( i = 0; i < p_bridge->i_es_num; i++ )
550     {
551         if ( !p_bridge->pp_es[i]->b_empty )
552             b_no_es = false;
553
554         while ( p_bridge->pp_es[i]->p_block != NULL
555                  && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
556                        < i_date
557                       || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
558                           < p_bridge->pp_es[i]->i_last) )
559         {
560             block_t *p_block = p_bridge->pp_es[i]->p_block;
561             msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
562                      i_date - p_block->i_dts - p_sys->i_delay );
563             p_bridge->pp_es[i]->p_block
564                 = p_bridge->pp_es[i]->p_block->p_next;
565             block_Release( p_block );
566         }
567
568         if ( p_bridge->pp_es[i]->p_block == NULL )
569         {
570             p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
571         }
572
573         if ( p_bridge->pp_es[i]->b_changed )
574         {
575             if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
576             {
577                 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
578             }
579             else
580             {
581                 /* We need at least two packets to enter the mux. */
582                 if ( p_bridge->pp_es[i]->p_block == NULL
583                       || p_bridge->pp_es[i]->p_block->p_next == NULL )
584                 {
585                     continue;
586                 }
587
588                 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
589                 if( !p_sys->b_placeholder )
590                 {
591                     p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
592                                 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
593                     if ( p_bridge->pp_es[i]->id == NULL )
594                     {
595                         msg_Warn( p_stream, "couldn't create chain for id %d",
596                                   p_bridge->pp_es[i]->fmt.i_id );
597                     }
598                 }
599                 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
600                          (char*)&p_bridge->pp_es[i]->fmt.i_codec,
601                          p_bridge->pp_es[i]->fmt.i_id, i );
602             }
603         }
604         p_bridge->pp_es[i]->b_changed = false;
605
606         if ( p_bridge->pp_es[i]->b_empty )
607             continue;
608
609         if ( p_bridge->pp_es[i]->p_block == NULL )
610         {
611             if ( p_bridge->pp_es[i]->id != NULL
612                   && p_bridge->pp_es[i]->i_last < i_date )
613             {
614                 if( !p_sys->b_placeholder )
615                     p_sys->p_out->pf_del( p_sys->p_out,
616                                           p_bridge->pp_es[i]->id );
617                 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
618                 p_bridge->pp_es[i]->b_changed = true;
619                 p_bridge->pp_es[i]->id = NULL;
620             }
621             continue;
622         }
623
624         if ( p_bridge->pp_es[i]->id != NULL || p_sys->b_placeholder)
625         {
626             block_t *p_block = p_bridge->pp_es[i]->p_block;
627             while ( p_block != NULL )
628             {
629                 p_bridge->pp_es[i]->i_last = p_block->i_dts;
630                 p_block->i_pts += p_sys->i_delay;
631                 p_block->i_dts += p_sys->i_delay;
632                 p_block = p_block->p_next;
633             }
634             sout_stream_id_t *newid = NULL;
635             if( p_sys->b_placeholder )
636             {
637                 switch( p_bridge->pp_es[i]->fmt.i_cat )
638                 {
639                     case VIDEO_ES:
640                         p_sys->i_last_video = i_date;
641                         newid = p_sys->id_video;
642                         if( !newid )
643                             break;
644                         if( !p_sys->b_switch_on_iframe ||
645                             p_sys->i_state == placeholder_off ||
646                             ( p_bridge->pp_es[i]->fmt.i_cat == VIDEO_ES &&
647                               p_bridge->pp_es[i]->p_block->i_flags & BLOCK_FLAG_TYPE_I ) )
648                         {
649                             p_sys->p_out->pf_send( p_sys->p_out,
650                                        newid,
651                                        p_bridge->pp_es[i]->p_block );
652                             p_sys->i_state = placeholder_off;
653                         }
654                         break;
655                     case AUDIO_ES:
656                         newid = p_sys->id_audio;
657                         if( !newid )
658                             break;
659                         p_sys->i_last_audio = i_date;
660                     default:
661                         p_sys->p_out->pf_send( p_sys->p_out,
662                                    newid?newid:p_bridge->pp_es[i]->id,
663                                    p_bridge->pp_es[i]->p_block );
664                         break;
665                 }
666             }
667             else /* !b_placeholder */
668                 p_sys->p_out->pf_send( p_sys->p_out,
669                                        p_bridge->pp_es[i]->id,
670                                        p_bridge->pp_es[i]->p_block );
671         }
672         else
673         {
674             block_ChainRelease( p_bridge->pp_es[i]->p_block );
675         }
676
677         p_bridge->pp_es[i]->p_block = NULL;
678         p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
679     }
680
681     if( b_no_es )
682     {
683         for ( i = 0; i < p_bridge->i_es_num; i++ )
684             free( p_bridge->pp_es[i] );
685         free( p_bridge->pp_es );
686         free( p_bridge );
687         var_Destroy( p_stream->p_libvlc, p_sys->psz_name );
688     }
689     }
690
691     if( p_sys->b_placeholder )
692     {
693         switch( id->i_cat )
694         {
695             case VIDEO_ES:
696                 if( ( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date
697                     && (  !p_sys->b_switch_on_iframe
698                        || p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
699                   || p_sys->i_state == placeholder_on )
700                 {
701                     p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
702                     p_sys->i_state = placeholder_on;
703                 }
704                 else
705                     block_Release( p_buffer );
706                 break;
707
708             case AUDIO_ES:
709                 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
710                     p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
711                 else
712                     block_Release( p_buffer );
713                 break;
714
715             default:
716                 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
717                 break;
718         }
719     }
720
721     vlc_mutex_unlock( p_sys->p_lock );
722
723     return VLC_SUCCESS;
724 }