]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
* all: split muxer and access into independant part.
[vlc] / 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.13 2003/02/16 14:10:44 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_mux_preheader = 0;
86     p_sout->i_nb_inputs = 0;
87     p_sout->pp_inputs = NULL;
88     vlc_mutex_init( p_sout, &p_sout->lock );
89
90     /* Skip the plug-in names */
91     while( *psz_parser && *psz_parser != ':' )
92     {
93         psz_parser++;
94     }
95 #if defined( WIN32 ) || defined( UNDER_CE )
96     if( psz_parser - p_sout->psz_dest == 1 )
97     {
98         msg_Warn( p_sout, "drive letter %c: found in source string",
99                           *p_sout->psz_dest ) ;
100         psz_parser = "";
101     }
102 #endif
103
104     if( !*psz_parser )
105     {
106         p_sout->psz_access = p_sout->psz_mux = "";
107         p_sout->psz_name = p_sout->psz_dest;
108     }
109     else
110     {
111         *psz_parser++ = '\0';
112
113         /* let's skip '//' */
114         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
115         {
116             psz_parser += 2 ;
117         } 
118
119         p_sout->psz_name = psz_parser ;
120
121         /* Come back to parse the access and mux plug-ins */
122         psz_parser = p_sout->psz_dest;
123
124         if( !*psz_parser )
125         {
126             /* No access */
127             p_sout->psz_access = "";
128         }
129         else if( *psz_parser == '/' )
130         {
131             /* No access */
132             p_sout->psz_access = "";
133             psz_parser++;
134         }
135         else
136         {
137             p_sout->psz_access = psz_parser;
138
139             while( *psz_parser && *psz_parser != '/' )
140             {
141                 psz_parser++;
142             }
143
144             if( *psz_parser == '/' )
145             {
146                 *psz_parser++ = '\0';
147             }
148         }
149
150         if( !*psz_parser )
151         {
152             /* No mux */
153             p_sout->psz_mux = "";
154         }
155         else
156         {
157             p_sout->psz_mux = psz_parser;
158         }
159     }
160
161     msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
162              p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
163
164
165     /* Find and open appropriate access module */
166     p_sout->p_access =
167         sout_AccessOutNew( p_sout, p_sout->psz_access, p_sout->psz_name );
168     if( p_sout->p_access == NULL )
169     {
170         msg_Err( p_sout, "no suitable sout access module for `%s/%s://%s'",
171                  p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
172         return -1;
173     }
174
175
176     /* Find and open appropriate mux module */
177     p_sout->p_mux =
178         module_Need( p_sout, "sout mux", p_sout->psz_mux );
179
180     if( p_sout->p_mux == NULL )
181     {
182         msg_Err( p_sout, "no suitable mux module for `%s/%s://%s'",
183                  p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
184
185         sout_AccessDelete( 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         sout_AccessDelete( 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  * sout_AccessOutNew: allocate a new access out
220  *****************************************************************************/
221 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
222                                       char *psz_access, char *psz_name )
223 {
224     sout_access_out_t *p_access;
225
226     if( !( p_access = vlc_object_create( p_sout,
227                                          sizeof( sout_access_out_t ) ) ) )
228     {
229         msg_Err( p_sout, "out of memory" );
230         return NULL;
231     }
232     p_access->psz_access = strdup( psz_access ? psz_access : "" );
233     p_access->psz_name   = strdup( psz_name ? psz_name : "" );
234     p_access->p_sout     = p_sout;
235     p_access->p_sys = NULL;
236     p_access->pf_seek    = NULL;
237     p_access->pf_write   = NULL;
238
239     p_access->p_module   = module_Need( p_access,
240                                         "sout access",
241                                         p_access->psz_access );;
242
243     if( !p_access->p_module )
244     {
245         vlc_object_destroy( p_access );
246         p_access = NULL;
247     }
248
249     return p_access;
250 }
251 /*****************************************************************************
252  * sout_AccessDelete: delete an access out
253  *****************************************************************************/
254 void sout_AccessDelete( sout_access_out_t *p_access )
255 {
256     if( p_access->p_module )
257     {
258         module_Unneed( p_access, p_access->p_module );
259     }
260     free( p_access->psz_access );
261     free( p_access->psz_name );
262
263     vlc_object_destroy( p_access );
264 }
265
266 /*****************************************************************************
267  * sout_AccessSeek:
268  *****************************************************************************/
269 int  sout_AccessSeek( sout_access_out_t *p_access, off_t i_pos )
270 {
271     return( p_access->pf_seek( p_access, i_pos ) );
272 }
273
274 /*****************************************************************************
275  * sout_AccessWrite:
276  *****************************************************************************/
277 int  sout_AccessWrite( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
278 {
279     return( p_access->pf_write( p_access, p_buffer ) );
280 }
281
282
283
284 /*****************************************************************************
285  *
286  *****************************************************************************/
287 sout_input_t *__sout_InputNew( vlc_object_t *p_this,
288                                 sout_packet_format_t *p_format )
289 {
290     sout_instance_t *p_sout = NULL;
291     sout_input_t    *p_input;
292     int             i_try;
293
294     /* search an stream output */
295     for( i_try = 0; i_try < 200; i_try++ )
296     {
297         p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
298         if( !p_sout )
299         {
300             msleep( 100*1000 );
301             msg_Dbg( p_this, "waiting for sout" );
302         }
303         else
304         {
305             break;
306         }
307     }
308
309     if( !p_sout )
310     {
311         msg_Err( p_this, "cannot find any stream ouput" );
312         return( NULL );
313     }
314
315     msg_Dbg( p_sout, "adding a new input" );
316
317     /* create a new sout input */
318     p_input = malloc( sizeof( sout_input_t ) );
319
320     p_input->p_sout = p_sout;
321     vlc_mutex_init( p_sout, &p_input->lock );
322     memcpy( &p_input->input_format,
323             p_format,
324             sizeof( sout_packet_format_t ) );
325     p_input->p_fifo = sout_FifoCreate( p_sout );
326     p_input->p_mux_data = NULL;
327
328     if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
329     {
330         /* add this new one to p_sout */
331         vlc_mutex_lock( &p_sout->lock );
332         if( p_sout->i_nb_inputs == 0 )
333         {
334             p_sout->pp_inputs = malloc( sizeof( sout_input_t * ) );
335         }
336         else
337         {
338             p_sout->pp_inputs = realloc( p_sout->pp_inputs,
339                                         sizeof( sout_input_t * ) *
340                                                 ( p_sout->i_nb_inputs + 1 ) );
341         }
342         p_sout->pp_inputs[p_sout->i_nb_inputs] = p_input;
343         p_sout->i_nb_inputs++;
344
345         if( p_sout->pf_mux_addstream( p_sout, p_input ) < 0 )
346         {
347             msg_Err( p_sout, "cannot add this stream" );
348
349             vlc_mutex_unlock( &p_sout->lock );
350             sout_InputDelete( p_input );
351             vlc_mutex_lock( &p_sout->lock );
352
353             p_input = NULL;
354         }
355         vlc_mutex_unlock( &p_sout->lock );
356     }
357
358     vlc_object_release( p_sout );
359
360     return( p_input );
361 }
362
363
364 int sout_InputDelete( sout_input_t *p_input )
365 {
366     sout_instance_t     *p_sout = p_input->p_sout;
367     int                 i_input;
368
369
370     msg_Dbg( p_sout, "removing an input" );
371
372     vlc_mutex_lock( &p_sout->lock );
373
374     sout_FifoDestroy( p_sout, p_input->p_fifo );
375     vlc_mutex_destroy( &p_input->lock );
376
377     for( i_input = 0; i_input < p_sout->i_nb_inputs; i_input++ )
378     {
379         if( p_sout->pp_inputs[i_input] == p_input )
380         {
381             break;
382         }
383     }
384     if( i_input < p_sout->i_nb_inputs )
385     {
386         if( p_sout->pf_mux_delstream( p_sout, p_input ) < 0 )
387         {
388             msg_Err( p_sout, "cannot del this stream from mux" );
389         }
390
391         /* remove the entry */
392         if( p_sout->i_nb_inputs > 1 )
393         {
394             memmove( &p_sout->pp_inputs[i_input],
395                      &p_sout->pp_inputs[i_input+1],
396                      (p_sout->i_nb_inputs - i_input - 1) * sizeof( sout_input_t*) );
397         }
398         else
399         {
400             free( p_sout->pp_inputs );
401         }
402         p_sout->i_nb_inputs--;
403
404         if( p_sout->i_nb_inputs == 0 )
405         {
406             msg_Warn( p_sout, "no more input stream" );
407         }
408     }
409     else if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
410     {
411         msg_Err( p_sout, "cannot find the input to be deleted" );
412     }
413
414     free( p_input );
415
416     vlc_mutex_unlock( &p_sout->lock );
417
418     return( 0 );
419 }
420
421
422 int sout_InputSendBuffer( sout_input_t *p_input, sout_buffer_t *p_buffer )
423 {
424 /*    msg_Dbg( p_input->p_sout,
425              "send buffer, size:%d", p_buffer->i_size ); */
426
427     if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
428     {
429         sout_FifoPut( p_input->p_fifo, p_buffer );
430
431         vlc_mutex_lock( &p_input->p_sout->lock );
432         p_input->p_sout->pf_mux( p_input->p_sout );
433         vlc_mutex_unlock( &p_input->p_sout->lock );
434
435     }
436     else
437     {
438         sout_BufferDelete( p_input->p_sout, p_buffer );
439     }
440
441     return( 0 );
442 }
443
444 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
445 {
446     sout_fifo_t *p_fifo;
447
448     if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
449     {
450         return( NULL );
451     }
452
453     vlc_mutex_init( p_sout, &p_fifo->lock );
454     vlc_cond_init ( p_sout, &p_fifo->wait );
455     p_fifo->i_depth = 0;
456     p_fifo->p_first = NULL;
457     p_fifo->pp_last = &p_fifo->p_first;
458
459     return( p_fifo );
460 }
461
462 void       sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
463 {
464     sout_buffer_t *p_buffer;
465
466     vlc_mutex_lock( &p_fifo->lock );
467     p_buffer = p_fifo->p_first;
468     while( p_buffer )
469     {
470         sout_buffer_t *p_next;
471         p_next = p_buffer->p_next;
472         sout_BufferDelete( p_sout, p_buffer );
473         p_buffer = p_next;
474     }
475     vlc_mutex_unlock( &p_fifo->lock );
476
477     return;
478 }
479 void       sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
480 {
481     sout_FifoFree( p_sout, p_fifo );
482     vlc_mutex_destroy( &p_fifo->lock );
483     vlc_cond_destroy ( &p_fifo->wait );
484
485     free( p_fifo );
486 }
487
488 void        sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
489 {
490     vlc_mutex_lock( &p_fifo->lock );
491
492     do
493     {
494         *p_fifo->pp_last = p_buffer;
495         p_fifo->pp_last = &p_buffer->p_next;
496         p_fifo->i_depth++;
497
498         p_buffer = p_buffer->p_next;
499
500     } while( p_buffer );
501
502     /* warm there is data in this fifo */
503     vlc_cond_signal( &p_fifo->wait );
504     vlc_mutex_unlock( &p_fifo->lock );
505 }
506
507 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
508 {
509     sout_buffer_t *p_buffer;
510
511     vlc_mutex_lock( &p_fifo->lock );
512
513     if( p_fifo->p_first == NULL )
514     {
515         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
516     }
517
518     p_buffer = p_fifo->p_first;
519
520     p_fifo->p_first = p_buffer->p_next;
521     p_fifo->i_depth--;
522
523     if( p_fifo->p_first == NULL )
524     {
525         p_fifo->pp_last = &p_fifo->p_first;
526     }
527
528     vlc_mutex_unlock( &p_fifo->lock );
529
530     p_buffer->p_next = NULL;
531     return( p_buffer );
532 }
533
534 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
535 {
536     sout_buffer_t *p_buffer;
537
538     vlc_mutex_lock( &p_fifo->lock );
539
540     if( p_fifo->p_first == NULL )
541     {
542         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
543     }
544
545     p_buffer = p_fifo->p_first;
546
547     vlc_mutex_unlock( &p_fifo->lock );
548
549     return( p_buffer );
550 }
551
552 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
553 {
554     sout_buffer_t *p_buffer;
555     size_t        i_prehader;
556
557 #ifdef DEBUG_BUFFER
558     msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
559 #endif
560
561     p_buffer = malloc( sizeof( sout_buffer_t ) );
562     i_prehader = p_sout->i_mux_preheader;
563
564     if( i_size > 0 )
565     {
566         p_buffer->p_allocated_buffer = malloc( i_size + i_prehader );
567         p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
568     }
569     else
570     {
571         p_buffer->p_allocated_buffer = NULL;
572         p_buffer->p_buffer = NULL;
573     }
574     p_buffer->i_allocated_size = i_size + i_prehader;
575     p_buffer->i_buffer_size = i_size;
576
577     p_buffer->i_size = i_size;
578     p_buffer->i_length = 0;
579     p_buffer->i_dts = 0;
580     p_buffer->i_pts = 0;
581     p_buffer->i_bitrate = 0;
582     p_buffer->p_next = NULL;
583
584     return( p_buffer );
585 }
586 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
587 {
588     size_t          i_prehader;
589
590 #ifdef DEBUG_BUFFER
591     msg_Dbg( p_sout,
592              "realloc buffer old size:%d new size:%d",
593              (uint32_t)p_buffer->i_allocated_size,
594              (uint32_t)i_size );
595 #endif
596
597     i_prehader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
598
599     if( !( p_buffer->p_allocated_buffer = realloc( p_buffer->p_allocated_buffer, i_size + i_prehader ) ) )
600     {
601         msg_Err( p_sout, "realloc failed" );
602         p_buffer->i_allocated_size = 0;
603         p_buffer->i_buffer_size = 0;
604         p_buffer->i_size = 0;
605         p_buffer->p_buffer = NULL;
606         return( -1 );
607     }
608     p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
609
610     p_buffer->i_allocated_size = i_size + i_prehader;
611     p_buffer->i_buffer_size = i_size;
612
613     return( 0 );
614 }
615
616 int sout_BufferReallocFromPreHeader( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
617 {
618     size_t  i_preheader;
619
620     i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
621
622     if( i_preheader < i_size )
623     {
624         return( -1 );
625     }
626
627     p_buffer->p_buffer -= i_size;
628     p_buffer->i_size += i_size;
629     p_buffer->i_buffer_size += i_size;
630
631     return( 0 );
632 }
633
634 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
635 {
636 #ifdef DEBUG_BUFFER
637     msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
638 #endif
639     if( p_buffer->p_allocated_buffer )
640     {
641         free( p_buffer->p_allocated_buffer );
642     }
643     free( p_buffer );
644     return( 0 );
645 }
646
647 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
648                                      sout_buffer_t *p_buffer )
649 {
650     sout_buffer_t *p_dup;
651
652     p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
653
654     p_dup->i_bitrate= p_buffer->i_bitrate;
655     p_dup->i_dts    = p_buffer->i_dts;
656     p_dup->i_pts    = p_buffer->i_pts;
657     p_dup->i_length = p_buffer->i_length;
658     p_sout->p_vlc->pf_memcpy( p_dup->p_buffer, p_buffer->p_buffer, p_buffer->i_size );
659
660     return( p_dup );
661 }
662
663 void sout_BufferChain( sout_buffer_t **pp_chain,
664                        sout_buffer_t *p_buffer )
665 {
666     if( *pp_chain == NULL )
667     {
668         *pp_chain = p_buffer;
669     }
670     else
671     {
672         sout_buffer_t *p = *pp_chain;
673
674         while( p->p_next )
675         {
676             p = p->p_next;
677         }
678
679         p->p_next = p_buffer;
680     }
681 }