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