1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: stream_output.c,v 1.9 2003/01/13 02:33:13 fenrir Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Erioc Petit <titer@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strerror() */
37 /*****************************************************************************
39 *****************************************************************************/
40 static int InitInstance ( sout_instance_t * );
42 /*****************************************************************************
43 * sout_NewInstance: creates a new stream output instance
44 *****************************************************************************/
45 sout_instance_t * __sout_NewInstance ( vlc_object_t *p_parent,
48 sout_instance_t * p_sout;
50 /* Allocate descriptor */
51 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
54 msg_Err( p_parent, "out of memory" );
58 p_sout->psz_dest = strdup( psz_dest );
60 if ( InitInstance( p_sout ) == -1 )
62 vlc_object_destroy( p_sout );
66 vlc_object_attach( p_sout, p_parent );
71 /*****************************************************************************
72 * InitInstance: opens appropriate modules
73 *****************************************************************************/
74 static int InitInstance( sout_instance_t * p_sout )
76 /* Parse dest string. Syntax : [[<access>][/<mux>]:][<dest>] */
77 /* This code is identical to input.c:InitThread. FIXME : factorize it ? */
78 char * psz_parser = p_sout->psz_dest;
80 p_sout->psz_access = "";
82 p_sout->psz_name = "";
83 p_sout->p_access = NULL;
85 p_sout->i_access_preheader = 0;
86 p_sout->i_mux_preheader = 0;
87 p_sout->i_nb_inputs = 0;
88 p_sout->pp_inputs = NULL;
89 vlc_mutex_init( p_sout, &p_sout->lock );
91 /* Skip the plug-in names */
92 while( *psz_parser && *psz_parser != ':' )
96 #if defined( WIN32 ) || defined( UNDER_CE )
97 if( psz_parser - p_sout->psz_dest == 1 )
99 msg_Warn( p_sout, "drive letter %c: found in source string",
100 *p_sout->psz_dest ) ;
107 p_sout->psz_access = p_sout->psz_mux = "";
108 p_sout->psz_name = p_sout->psz_dest;
112 *psz_parser++ = '\0';
114 /* let's skip '//' */
115 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
120 p_sout->psz_name = psz_parser ;
122 /* Come back to parse the access and mux plug-ins */
123 psz_parser = p_sout->psz_dest;
128 p_sout->psz_access = "";
130 else if( *psz_parser == '/' )
133 p_sout->psz_access = "";
138 p_sout->psz_access = psz_parser;
140 while( *psz_parser && *psz_parser != '/' )
145 if( *psz_parser == '/' )
147 *psz_parser++ = '\0';
154 p_sout->psz_mux = "";
158 p_sout->psz_mux = psz_parser;
162 msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
163 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
166 /* Find and open appropriate access module */
168 module_Need( p_sout, "sout access", p_sout->psz_access );
170 if( p_sout->p_access == NULL )
172 msg_Err( p_sout, "no suitable sout access module for `%s/%s://%s'",
173 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
177 /* Find and open appropriate mux module */
179 module_Need( p_sout, "sout mux", p_sout->psz_mux );
181 if( p_sout->p_mux == NULL )
183 msg_Err( p_sout, "no suitable mux module for `%s/%s://%s'",
184 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
185 module_Unneed( p_sout, p_sout->p_access );
189 p_sout->i_nb_inputs = 0;
190 p_sout->pp_inputs = NULL;
196 /*****************************************************************************
197 * sout_DeleteInstance: delete a previously allocated instance
198 *****************************************************************************/
199 void sout_DeleteInstance( sout_instance_t * p_sout )
202 vlc_object_detach( p_sout );
205 module_Unneed( p_sout, p_sout->p_mux );
207 if( p_sout->p_access )
209 module_Unneed( p_sout, p_sout->p_access );
212 vlc_mutex_destroy( &p_sout->lock );
215 vlc_object_destroy( p_sout );
219 /*****************************************************************************
221 *****************************************************************************/
222 sout_input_t *__sout_InputNew( vlc_object_t *p_this,
223 sout_packet_format_t *p_format )
225 sout_instance_t *p_sout = NULL;
226 sout_input_t *p_input;
229 /* search an stream output */
230 for( i_try = 0; i_try < 200; i_try++ )
232 p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
236 msg_Dbg( p_this, "waiting for sout" );
246 msg_Err( p_this, "cannot find any stream ouput" );
250 msg_Dbg( p_sout, "adding a new input" );
252 /* create a new sout input */
253 p_input = malloc( sizeof( sout_input_t ) );
255 p_input->p_sout = p_sout;
256 vlc_mutex_init( p_sout, &p_input->lock );
257 memcpy( &p_input->input_format,
259 sizeof( sout_packet_format_t ) );
260 p_input->p_fifo = sout_FifoCreate( p_sout );
261 p_input->p_mux_data = NULL;
263 /* add this new one to p_sout */
264 vlc_mutex_lock( &p_sout->lock );
265 if( p_sout->i_nb_inputs == 0 )
267 p_sout->pp_inputs = malloc( sizeof( sout_input_t * ) );
271 p_sout->pp_inputs = realloc( p_sout->pp_inputs,
272 sizeof( sout_input_t * ) *
273 ( p_sout->i_nb_inputs + 1 ) );
275 p_sout->pp_inputs[p_sout->i_nb_inputs] = p_input;
276 p_sout->i_nb_inputs++;
278 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) &&
279 p_sout->pf_mux_addstream( p_sout, p_input ) < 0 )
281 /* vlc_mutex_unlock( &p_sout->lock ); */
282 msg_Err( p_sout, "cannot add this stream" );
285 vlc_mutex_unlock( &p_sout->lock );
287 vlc_object_release( p_sout );
293 int sout_InputDelete( sout_input_t *p_input )
295 sout_instance_t *p_sout = p_input->p_sout;
299 vlc_mutex_lock( &p_sout->lock );
300 for( i_input = 0; i_input < p_sout->i_nb_inputs; i_input++ )
302 if( p_sout->pp_inputs[i_input] == p_input )
308 if( i_input >= p_sout->i_nb_inputs )
310 msg_Err( p_sout, "cannot find input to delete" );
314 msg_Dbg( p_sout, "removing an input" );
316 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) &&
317 p_sout->pf_mux_delstream( p_sout, p_input ) < 0 )
319 msg_Err( p_sout, "cannot del this stream" );
323 /* remove the entry */
324 if( p_sout->i_nb_inputs > 1 )
326 memmove( &p_sout->pp_inputs[i_input],
327 &p_sout->pp_inputs[i_input+1],
328 (p_sout->i_nb_inputs - i_input - 1) * sizeof( sout_input_t*) );
332 free( p_sout->pp_inputs );
334 p_sout->i_nb_inputs--;
336 sout_FifoDestroy( p_sout, p_input->p_fifo );
337 vlc_mutex_destroy( &p_input->lock );
340 if( p_sout->i_nb_inputs == 0 )
342 msg_Warn( p_sout, "no more input stream" );
344 vlc_mutex_unlock( &p_sout->lock );
350 int sout_InputSendBuffer( sout_input_t *p_input, sout_buffer_t *p_buffer )
352 /* msg_Dbg( p_input->p_sout,
353 "send buffer, size:%d", p_buffer->i_size ); */
355 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
357 sout_FifoPut( p_input->p_fifo, p_buffer );
359 vlc_mutex_lock( &p_input->p_sout->lock );
360 p_input->p_sout->pf_mux( p_input->p_sout );
361 vlc_mutex_unlock( &p_input->p_sout->lock );
366 sout_BufferDelete( p_input->p_sout, p_buffer );
372 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
376 if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
381 vlc_mutex_init( p_sout, &p_fifo->lock );
382 vlc_cond_init ( p_sout, &p_fifo->wait );
384 p_fifo->p_first = NULL;
385 p_fifo->pp_last = &p_fifo->p_first;
390 void sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
392 sout_buffer_t *p_buffer;
394 vlc_mutex_lock( &p_fifo->lock );
395 p_buffer = p_fifo->p_first;
398 sout_buffer_t *p_next;
399 p_next = p_buffer->p_next;
400 sout_BufferDelete( p_sout, p_buffer );
403 vlc_mutex_unlock( &p_fifo->lock );
407 void sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
409 sout_FifoFree( p_sout, p_fifo );
410 vlc_mutex_destroy( &p_fifo->lock );
411 vlc_cond_destroy ( &p_fifo->wait );
416 void sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
418 vlc_mutex_lock( &p_fifo->lock );
422 *p_fifo->pp_last = p_buffer;
423 p_fifo->pp_last = &p_buffer->p_next;
426 p_buffer = p_buffer->p_next;
430 /* warm there is data in this fifo */
431 vlc_cond_signal( &p_fifo->wait );
432 vlc_mutex_unlock( &p_fifo->lock );
435 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
437 sout_buffer_t *p_buffer;
439 vlc_mutex_lock( &p_fifo->lock );
441 if( p_fifo->p_first == NULL )
443 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
446 p_buffer = p_fifo->p_first;
448 p_fifo->p_first = p_buffer->p_next;
451 if( p_fifo->p_first == NULL )
453 p_fifo->pp_last = &p_fifo->p_first;
456 vlc_mutex_unlock( &p_fifo->lock );
458 p_buffer->p_next = NULL;
462 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
464 sout_buffer_t *p_buffer;
466 vlc_mutex_lock( &p_fifo->lock );
468 if( p_fifo->p_first == NULL )
470 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
473 p_buffer = p_fifo->p_first;
475 vlc_mutex_unlock( &p_fifo->lock );
480 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
482 sout_buffer_t *p_buffer;
486 msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
489 p_buffer = malloc( sizeof( sout_buffer_t ) );
490 i_prehader = p_sout->i_access_preheader + p_sout->i_mux_preheader;
494 p_buffer->p_allocated_buffer = malloc( i_size + i_prehader );
495 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
499 p_buffer->p_allocated_buffer = NULL;
500 p_buffer->p_buffer = NULL;
502 p_buffer->i_allocated_size = i_size + i_prehader;
503 p_buffer->i_buffer_size = i_size;
505 p_buffer->i_size = i_size;
506 p_buffer->i_length = 0;
509 p_buffer->i_bitrate = 0;
510 p_buffer->p_next = NULL;
514 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
520 "realloc buffer old size:%d new size:%d",
521 (uint32_t)p_buffer->i_allocated_size,
525 i_prehader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
527 if( !( p_buffer->p_allocated_buffer = realloc( p_buffer->p_allocated_buffer, i_size + i_prehader ) ) )
529 msg_Err( p_sout, "realloc failed" );
530 p_buffer->i_allocated_size = 0;
531 p_buffer->i_buffer_size = 0;
532 p_buffer->i_size = 0;
533 p_buffer->p_buffer = NULL;
536 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
538 p_buffer->i_allocated_size = i_size + i_prehader;
539 p_buffer->i_buffer_size = i_size;
544 int sout_BufferReallocFromPreHeader( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
548 i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
550 if( i_preheader < i_size )
555 p_buffer->p_buffer -= i_size;
556 p_buffer->i_size += i_size;
557 p_buffer->i_buffer_size += i_size;
560 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
563 msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
565 if( p_buffer->p_allocated_buffer )
567 free( p_buffer->p_allocated_buffer );
573 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
574 sout_buffer_t *p_buffer )
576 sout_buffer_t *p_dup;
578 p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
580 p_dup->i_bitrate= p_buffer->i_bitrate;
581 p_dup->i_dts = p_buffer->i_dts;
582 p_dup->i_pts = p_buffer->i_pts;
583 p_dup->i_length = p_buffer->i_length;
584 memcpy( p_dup->p_buffer, p_buffer->p_buffer, p_buffer->i_size );
589 void sout_BufferChain( sout_buffer_t **pp_chain,
590 sout_buffer_t *p_buffer )
592 if( *pp_chain == NULL )
594 *pp_chain = p_buffer;
598 sout_buffer_t *p = *pp_chain;
605 p->p_next = p_buffer;