d0b17ffa115c8b2ce425e21fd5ade1404dd07d95
[vlc.git] / src / stream_output / stream_output.c
1 /*****************************************************************************
2  * stream_output.c : stream output module
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: stream_output.c,v 1.10 2003/01/13 04:46:49 fenrir Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Erioc Petit <titer@videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34
35 #include <vlc/sout.h>
36 #undef DEBUG_BUFFER
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int      InitInstance      ( sout_instance_t * );
41
42 /*****************************************************************************
43  * sout_NewInstance: creates a new stream output instance
44  *****************************************************************************/
45 sout_instance_t * __sout_NewInstance ( vlc_object_t *p_parent,
46                                        char * psz_dest )
47 {
48     sout_instance_t * p_sout;
49
50     /* Allocate descriptor */
51     p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
52     if( p_sout == NULL )
53     {
54         msg_Err( p_parent, "out of memory" );
55         return NULL;
56     }
57
58     p_sout->psz_dest = strdup( psz_dest );
59
60     if ( InitInstance( p_sout ) == -1 )
61     {
62         vlc_object_destroy( p_sout );
63         return NULL;
64     }
65
66     vlc_object_attach( p_sout, p_parent );
67
68     return p_sout;
69 }
70
71 /*****************************************************************************
72  * InitInstance: opens appropriate modules
73  *****************************************************************************/
74 static int InitInstance( sout_instance_t * p_sout )
75 {
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;
79
80     p_sout->psz_access = "";
81     p_sout->psz_mux = "";
82     p_sout->psz_name = "";
83     p_sout->p_access = NULL;
84     p_sout->p_mux = 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 );
90
91     /* Skip the plug-in names */
92     while( *psz_parser && *psz_parser != ':' )
93     {
94         psz_parser++;
95     }
96 #if defined( WIN32 ) || defined( UNDER_CE )
97     if( psz_parser - p_sout->psz_dest == 1 )
98     {
99         msg_Warn( p_sout, "drive letter %c: found in source string",
100                           *p_sout->psz_dest ) ;
101         psz_parser = "";
102     }
103 #endif
104
105     if( !*psz_parser )
106     {
107         p_sout->psz_access = p_sout->psz_mux = "";
108         p_sout->psz_name = p_sout->psz_dest;
109     }
110     else
111     {
112         *psz_parser++ = '\0';
113
114         /* let's skip '//' */
115         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
116         {
117             psz_parser += 2 ;
118         } 
119
120         p_sout->psz_name = psz_parser ;
121
122         /* Come back to parse the access and mux plug-ins */
123         psz_parser = p_sout->psz_dest;
124
125         if( !*psz_parser )
126         {
127             /* No access */
128             p_sout->psz_access = "";
129         }
130         else if( *psz_parser == '/' )
131         {
132             /* No access */
133             p_sout->psz_access = "";
134             psz_parser++;
135         }
136         else
137         {
138             p_sout->psz_access = psz_parser;
139
140             while( *psz_parser && *psz_parser != '/' )
141             {
142                 psz_parser++;
143             }
144
145             if( *psz_parser == '/' )
146             {
147                 *psz_parser++ = '\0';
148             }
149         }
150
151         if( !*psz_parser )
152         {
153             /* No mux */
154             p_sout->psz_mux = "";
155         }
156         else
157         {
158             p_sout->psz_mux = psz_parser;
159         }
160     }
161
162     msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
163              p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
164
165
166     /* Find and open appropriate access module */
167     p_sout->p_access =
168         module_Need( p_sout, "sout access", p_sout->psz_access );
169
170     if( p_sout->p_access == NULL )
171     {
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 );
174         return -1;
175     }
176
177     /* Find and open appropriate mux module */
178     p_sout->p_mux =
179         module_Need( p_sout, "sout mux", p_sout->psz_mux );
180
181     if( p_sout->p_mux == NULL )
182     {
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 );
186         return -1;
187     }
188
189     p_sout->i_nb_inputs = 0;
190     p_sout->pp_inputs = NULL;
191
192     return 0;
193 }
194
195
196 /*****************************************************************************
197  * sout_DeleteInstance: delete a previously allocated instance
198  *****************************************************************************/
199 void sout_DeleteInstance( sout_instance_t * p_sout )
200 {
201     /* Unlink object */
202     vlc_object_detach( p_sout );
203     if( p_sout->p_mux )
204     {
205         module_Unneed( p_sout, p_sout->p_mux );
206     }
207     if( p_sout->p_access )
208     {
209         module_Unneed( p_sout, p_sout->p_access );
210     }
211
212     vlc_mutex_destroy( &p_sout->lock );
213
214     /* Free structure */
215     vlc_object_destroy( p_sout );
216 }
217
218
219 /*****************************************************************************
220  *
221  *****************************************************************************/
222 sout_input_t *__sout_InputNew( vlc_object_t *p_this,
223                                 sout_packet_format_t *p_format )
224 {
225     sout_instance_t *p_sout = NULL;
226     sout_input_t    *p_input;
227     int             i_try;
228
229     /* search an stream output */
230     for( i_try = 0; i_try < 200; i_try++ )
231     {
232         p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
233         if( !p_sout )
234         {
235             msleep( 100*1000 );
236             msg_Dbg( p_this, "waiting for sout" );
237         }
238         else
239         {
240             break;
241         }
242     }
243
244     if( !p_sout )
245     {
246         msg_Err( p_this, "cannot find any stream ouput" );
247         return( NULL );
248     }
249
250     msg_Dbg( p_sout, "adding a new input" );
251
252     /* create a new sout input */
253     p_input = malloc( sizeof( sout_input_t ) );
254
255     p_input->p_sout = p_sout;
256     vlc_mutex_init( p_sout, &p_input->lock );
257     memcpy( &p_input->input_format,
258             p_format,
259             sizeof( sout_packet_format_t ) );
260     p_input->p_fifo = sout_FifoCreate( p_sout );
261     p_input->p_mux_data = NULL;
262
263     /* add this new one to p_sout */
264     vlc_mutex_lock( &p_sout->lock );
265     if( p_sout->i_nb_inputs == 0 )
266     {
267         p_sout->pp_inputs = malloc( sizeof( sout_input_t * ) );
268     }
269     else
270     {
271         p_sout->pp_inputs = realloc( p_sout->pp_inputs,
272                                     sizeof( sout_input_t * ) *
273                                             ( p_sout->i_nb_inputs + 1 ) );
274     }
275     p_sout->pp_inputs[p_sout->i_nb_inputs] = p_input;
276     p_sout->i_nb_inputs++;
277
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 )
280     {
281         /* vlc_mutex_unlock( &p_sout->lock ); */
282         msg_Err( p_sout, "cannot add this stream" );
283         /* FIXME FIXME */
284     }
285     vlc_mutex_unlock( &p_sout->lock );
286
287     vlc_object_release( p_sout );
288
289     return( p_input );
290 }
291
292
293 int sout_InputDelete( sout_input_t *p_input )
294 {
295     sout_instance_t     *p_sout = p_input->p_sout;
296     int                 i_input;
297
298
299     vlc_mutex_lock( &p_sout->lock );
300     for( i_input = 0; i_input < p_sout->i_nb_inputs; i_input++ )
301     {
302         if( p_sout->pp_inputs[i_input] == p_input )
303         {
304             break;
305         }
306     }
307
308     if( i_input >= p_sout->i_nb_inputs )
309     {
310         msg_Err( p_sout, "cannot find input to delete" );
311         return( -1 );
312     }
313
314     msg_Dbg( p_sout, "removing an input" );
315
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 )
318     {
319         msg_Err( p_sout, "cannot del this stream" );
320         /* FIXME FIXME */
321     }
322
323     /* remove the entry */
324     if( p_sout->i_nb_inputs > 1 )
325     {
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*) );
329     }
330     else
331     {
332         free( p_sout->pp_inputs );
333     }
334     p_sout->i_nb_inputs--;
335
336     sout_FifoDestroy( p_sout, p_input->p_fifo );
337     vlc_mutex_destroy( &p_input->lock );
338     free( p_input );
339
340     if( p_sout->i_nb_inputs == 0 )
341     {
342         msg_Warn( p_sout, "no more input stream" );
343     }
344     vlc_mutex_unlock( &p_sout->lock );
345
346     return( 0 );
347 }
348
349
350 int sout_InputSendBuffer( sout_input_t *p_input, sout_buffer_t *p_buffer )
351 {
352 /*    msg_Dbg( p_input->p_sout,
353              "send buffer, size:%d", p_buffer->i_size ); */
354
355     if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
356     {
357         sout_FifoPut( p_input->p_fifo, p_buffer );
358
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 );
362
363     }
364     else
365     {
366         sout_BufferDelete( p_input->p_sout, p_buffer );
367     }
368
369     return( 0 );
370 }
371
372 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
373 {
374     sout_fifo_t *p_fifo;
375
376     if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
377     {
378         return( NULL );
379     }
380
381     vlc_mutex_init( p_sout, &p_fifo->lock );
382     vlc_cond_init ( p_sout, &p_fifo->wait );
383     p_fifo->i_depth = 0;
384     p_fifo->p_first = NULL;
385     p_fifo->pp_last = &p_fifo->p_first;
386
387     return( p_fifo );
388 }
389
390 void       sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
391 {
392     sout_buffer_t *p_buffer;
393
394     vlc_mutex_lock( &p_fifo->lock );
395     p_buffer = p_fifo->p_first;
396     while( p_buffer )
397     {
398         sout_buffer_t *p_next;
399         p_next = p_buffer->p_next;
400         sout_BufferDelete( p_sout, p_buffer );
401         p_buffer = p_next;
402     }
403     vlc_mutex_unlock( &p_fifo->lock );
404
405     return;
406 }
407 void       sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
408 {
409     sout_FifoFree( p_sout, p_fifo );
410     vlc_mutex_destroy( &p_fifo->lock );
411     vlc_cond_destroy ( &p_fifo->wait );
412
413     free( p_fifo );
414 }
415
416 void        sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
417 {
418     vlc_mutex_lock( &p_fifo->lock );
419
420     do
421     {
422         *p_fifo->pp_last = p_buffer;
423         p_fifo->pp_last = &p_buffer->p_next;
424         p_fifo->i_depth++;
425
426         p_buffer = p_buffer->p_next;
427
428     } while( p_buffer );
429
430     /* warm there is data in this fifo */
431     vlc_cond_signal( &p_fifo->wait );
432     vlc_mutex_unlock( &p_fifo->lock );
433 }
434
435 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
436 {
437     sout_buffer_t *p_buffer;
438
439     vlc_mutex_lock( &p_fifo->lock );
440
441     if( p_fifo->p_first == NULL )
442     {
443         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
444     }
445
446     p_buffer = p_fifo->p_first;
447
448     p_fifo->p_first = p_buffer->p_next;
449     p_fifo->i_depth--;
450
451     if( p_fifo->p_first == NULL )
452     {
453         p_fifo->pp_last = &p_fifo->p_first;
454     }
455
456     vlc_mutex_unlock( &p_fifo->lock );
457
458     p_buffer->p_next = NULL;
459     return( p_buffer );
460 }
461
462 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
463 {
464     sout_buffer_t *p_buffer;
465
466     vlc_mutex_lock( &p_fifo->lock );
467
468     if( p_fifo->p_first == NULL )
469     {
470         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
471     }
472
473     p_buffer = p_fifo->p_first;
474
475     vlc_mutex_unlock( &p_fifo->lock );
476
477     return( p_buffer );
478 }
479
480 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
481 {
482     sout_buffer_t *p_buffer;
483     size_t        i_prehader;
484
485 #ifdef DEBUG_BUFFER
486     msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
487 #endif
488
489     p_buffer = malloc( sizeof( sout_buffer_t ) );
490     i_prehader = p_sout->i_access_preheader + p_sout->i_mux_preheader;
491
492     if( i_size > 0 )
493     {
494         p_buffer->p_allocated_buffer = malloc( i_size + i_prehader );
495         p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
496     }
497     else
498     {
499         p_buffer->p_allocated_buffer = NULL;
500         p_buffer->p_buffer = NULL;
501     }
502     p_buffer->i_allocated_size = i_size + i_prehader;
503     p_buffer->i_buffer_size = i_size;
504
505     p_buffer->i_size = i_size;
506     p_buffer->i_length = 0;
507     p_buffer->i_dts = 0;
508     p_buffer->i_pts = 0;
509     p_buffer->i_bitrate = 0;
510     p_buffer->p_next = NULL;
511
512     return( p_buffer );
513 }
514 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
515 {
516     size_t          i_prehader;
517
518 #ifdef DEBUG_BUFFER
519     msg_Dbg( p_sout,
520              "realloc buffer old size:%d new size:%d",
521              (uint32_t)p_buffer->i_allocated_size,
522              (uint32_t)i_size );
523 #endif
524
525     i_prehader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
526
527     if( !( p_buffer->p_allocated_buffer = realloc( p_buffer->p_allocated_buffer, i_size + i_prehader ) ) )
528     {
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;
534         return( -1 );
535     }
536     p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
537
538     p_buffer->i_allocated_size = i_size + i_prehader;
539     p_buffer->i_buffer_size = i_size;
540
541     return( 0 );
542 }
543
544 int sout_BufferReallocFromPreHeader( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
545 {
546     size_t  i_preheader;
547
548     i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
549
550     if( i_preheader < i_size )
551     {
552         return( -1 );
553     }
554
555     p_buffer->p_buffer -= i_size;
556     p_buffer->i_size += i_size;
557     p_buffer->i_buffer_size += i_size;
558
559     return( 0 );
560 }
561
562 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
563 {
564 #ifdef DEBUG_BUFFER
565     msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
566 #endif
567     if( p_buffer->p_allocated_buffer )
568     {
569         free( p_buffer->p_allocated_buffer );
570     }
571     free( p_buffer );
572     return( 0 );
573 }
574
575 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
576                                      sout_buffer_t *p_buffer )
577 {
578     sout_buffer_t *p_dup;
579
580     p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
581
582     p_dup->i_bitrate= p_buffer->i_bitrate;
583     p_dup->i_dts    = p_buffer->i_dts;
584     p_dup->i_pts    = p_buffer->i_pts;
585     p_dup->i_length = p_buffer->i_length;
586     memcpy( p_dup->p_buffer, p_buffer->p_buffer, p_buffer->i_size );
587
588     return( p_dup );
589 }
590
591 void sout_BufferChain( sout_buffer_t **pp_chain,
592                        sout_buffer_t *p_buffer )
593 {
594     if( *pp_chain == NULL )
595     {
596         *pp_chain = p_buffer;
597     }
598     else
599     {
600         sout_buffer_t *p = *pp_chain;
601
602         while( p->p_next )
603         {
604             p = p->p_next;
605         }
606
607         p->p_next = p_buffer;
608     }
609 }