]> git.sesse.net Git - vlc/blob - modules/stream_out/es.c
aout: add wait parameter to aout_DecFlush()
[vlc] / modules / stream_out / es.c
1 /*****************************************************************************
2  * es.c: Elementary stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
35 #include <vlc_sout.h>
36 #include <vlc_dialog.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define ACCESS_TEXT N_("Output access method")
42 #define ACCESS_LONGTEXT N_( \
43     "This is the default output access method that will be used." )
44
45 #define ACCESSA_TEXT N_("Audio output access method")
46 #define ACCESSA_LONGTEXT N_( \
47     "This is the output access method that will be used for audio." )
48 #define ACCESSV_TEXT N_("Video output access method")
49 #define ACCESSV_LONGTEXT N_( \
50     "This is the output access method that will be used for video." )
51
52 #define MUX_TEXT N_("Output muxer")
53 #define MUX_LONGTEXT N_( \
54     "This is the default muxer method that will be used." )
55 #define MUXA_TEXT N_("Audio output muxer")
56 #define MUXA_LONGTEXT N_( \
57     "This is the muxer that will be used for audio." )
58 #define MUXV_TEXT N_("Video output muxer")
59 #define MUXV_LONGTEXT N_( \
60     "This is the muxer that will be used for video." )
61
62 #define DEST_TEXT N_("Output URL")
63 #define DEST_LONGTEXT N_( \
64     "This is the default output URI." )
65 #define DESTA_TEXT N_("Audio output URL")
66 #define DESTA_LONGTEXT N_( \
67     "This is the output URI that will be used for audio." )
68 #define DESTV_TEXT N_("Video output URL")
69 #define DESTV_LONGTEXT N_( \
70     "This is the output URI that will be used for video." )
71
72 static int      Open    ( vlc_object_t * );
73 static void     Close   ( vlc_object_t * );
74
75 #define SOUT_CFG_PREFIX "sout-es-"
76
77 vlc_module_begin ()
78     set_shortname( "ES" )
79     set_description( N_("Elementary stream output") )
80     set_capability( "sout stream", 50 )
81     add_shortcut( "es" )
82     set_category( CAT_SOUT )
83     set_subcategory( SUBCAT_SOUT_STREAM )
84
85     set_section( N_("Generic"), NULL )
86     add_string( SOUT_CFG_PREFIX "access", "", ACCESS_TEXT,
87                 ACCESS_LONGTEXT, true )
88     add_string( SOUT_CFG_PREFIX "mux", "", MUX_TEXT,
89                 MUX_LONGTEXT, true )
90     add_string( SOUT_CFG_PREFIX "dst", "", DEST_TEXT,
91                 DEST_LONGTEXT, true )
92
93     set_section( N_("Audio"), NULL )
94     add_string( SOUT_CFG_PREFIX "access-audio", "", ACCESSA_TEXT,
95                 ACCESSA_LONGTEXT, true )
96     add_string( SOUT_CFG_PREFIX "mux-audio", "", MUXA_TEXT,
97                 MUXA_LONGTEXT, true )
98     add_string( SOUT_CFG_PREFIX "dst-audio", "", DESTA_TEXT,
99                 DESTA_LONGTEXT, true )
100
101     set_section( N_("Video"), NULL )
102     add_string( SOUT_CFG_PREFIX "access-video", "", ACCESSV_TEXT,
103                 ACCESSV_LONGTEXT, true )
104     add_string( SOUT_CFG_PREFIX "mux-video", "", MUXV_TEXT,
105                 MUXV_LONGTEXT, true )
106     add_string( SOUT_CFG_PREFIX "dst-video", "", DESTV_TEXT,
107                 DESTV_LONGTEXT, true )
108
109     set_callbacks( Open, Close )
110 vlc_module_end ()
111
112 /*****************************************************************************
113  * Exported prototypes
114  *****************************************************************************/
115 static const char *const ppsz_sout_options[] = {
116     "access", "access-audio", "access-video",
117     "mux", "mux-audio", "mux-video",
118     "dst", "dst-audio", "dst-video",
119     NULL
120 };
121
122 static sout_stream_id_sys_t *Add( sout_stream_t *, const es_format_t * );
123 static void              Del ( sout_stream_t *, sout_stream_id_sys_t * );
124 static int               Send( sout_stream_t *, sout_stream_id_sys_t *, block_t* );
125
126 struct sout_stream_sys_t
127 {
128     int  i_count_audio;
129     int  i_count_video;
130     int  i_count;
131
132     char *psz_mux;
133     char *psz_mux_audio;
134     char *psz_mux_video;
135
136     char *psz_access;
137     char *psz_access_audio;
138     char *psz_access_video;
139
140     char *psz_dst;
141     char *psz_dst_audio;
142     char *psz_dst_video;
143 };
144
145 /*****************************************************************************
146  * Open:
147  *****************************************************************************/
148 static int Open( vlc_object_t *p_this )
149 {
150     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
151     sout_stream_sys_t   *p_sys;
152
153     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
154     p_sys                   = malloc( sizeof( sout_stream_sys_t ) );
155
156     p_sys->i_count          = 0;
157     p_sys->i_count_audio    = 0;
158     p_sys->i_count_video    = 0;
159
160     p_sys->psz_access = var_GetString( p_stream, SOUT_CFG_PREFIX "access" );
161     p_sys->psz_access_audio = var_GetString( p_stream, SOUT_CFG_PREFIX "access-audio" );
162     p_sys->psz_access_video = var_GetString( p_stream, SOUT_CFG_PREFIX "access-video" );
163
164     p_sys->psz_mux = var_GetString( p_stream, SOUT_CFG_PREFIX "mux" );
165     p_sys->psz_mux_audio = var_GetString( p_stream, SOUT_CFG_PREFIX "mux-audio" );
166     p_sys->psz_mux_video = var_GetString( p_stream, SOUT_CFG_PREFIX "mux-video" );
167
168     p_sys->psz_dst       = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
169     p_sys->psz_dst_audio = var_GetString( p_stream, SOUT_CFG_PREFIX "dst-audio" );
170     p_sys->psz_dst_video = var_GetString( p_stream, SOUT_CFG_PREFIX "dst-video" );
171
172     p_stream->pf_add    = Add;
173     p_stream->pf_del    = Del;
174     p_stream->pf_send   = Send;
175
176     p_stream->p_sys     = p_sys;
177
178     return VLC_SUCCESS;
179 }
180
181 /*****************************************************************************
182  * Close:
183  *****************************************************************************/
184
185 static void Close( vlc_object_t * p_this )
186 {
187     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
188     sout_stream_sys_t *p_sys = p_stream->p_sys;
189
190     free( p_sys->psz_access );
191     free( p_sys->psz_access_audio );
192     free( p_sys->psz_access_video );
193
194     free( p_sys->psz_mux );
195     free( p_sys->psz_mux_audio );
196     free( p_sys->psz_mux_video );
197
198     free( p_sys->psz_dst );
199     free( p_sys->psz_dst_audio );
200     free( p_sys->psz_dst_video );
201
202     free( p_sys );
203 }
204
205 struct sout_stream_id_sys_t
206 {
207     sout_input_t *p_input;
208     sout_mux_t   *p_mux;
209 };
210
211 static char * es_print_url( const char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
212                             const char *psz_access, const char *psz_mux )
213 {
214     char *psz_dst, *p;
215
216     if( psz_fmt == NULL || !*psz_fmt )
217     {
218         psz_fmt = (char*)"stream-%n-%c.%m";
219     }
220
221     p = psz_dst = malloc( 4096 );
222     if( !psz_dst )
223         return NULL;
224     memset( p, 0, 4096 );
225     for( ;; )
226     {
227         if( *psz_fmt == '\0' )
228         {
229             *p = '\0';
230             break;
231         }
232
233         if( *psz_fmt != '%' )
234         {
235             *p++ = *psz_fmt++;
236         }
237         else
238         {
239             if( psz_fmt[1] == 'n' )
240             {
241                 p += sprintf( p, "%d", i_count );
242             }
243             else if( psz_fmt[1] == 'c' )
244             {
245                 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
246             }
247             else if( psz_fmt[1] == 'm' )
248             {
249                 p += sprintf( p, "%s", psz_mux );
250             }
251             else if( psz_fmt[1] == 'a' )
252             {
253                 p += sprintf( p, "%s", psz_access );
254             }
255             else if( psz_fmt[1] != '\0' )
256             {
257                 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
258             }
259             else
260             {
261                 p += sprintf( p, "%c", psz_fmt[0] );
262                 *p++ = '\0';
263                 break;
264             }
265             psz_fmt += 2;
266         }
267     }
268
269     return( psz_dst );
270 }
271
272 static sout_stream_id_sys_t *Add( sout_stream_t *p_stream, const es_format_t *p_fmt )
273 {
274     sout_stream_sys_t *p_sys = p_stream->p_sys;
275     sout_stream_id_sys_t  *id;
276
277     const char        *psz_access;
278     const char        *psz_mux;
279     char              *psz_dst;
280
281     sout_access_out_t *p_access;
282     sout_mux_t        *p_mux;
283
284     /* *** get access name *** */
285     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
286     {
287         psz_access = p_sys->psz_access_audio;
288     }
289     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
290     {
291         psz_access = p_sys->psz_access_video;
292     }
293     else
294     {
295         psz_access = p_sys->psz_access;
296     }
297
298     /* *** get mux name *** */
299     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
300     {
301         psz_mux = p_sys->psz_mux_audio;
302     }
303     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
304     {
305         psz_mux = p_sys->psz_mux_video;
306     }
307     else
308     {
309         psz_mux = p_sys->psz_mux;
310     }
311
312     /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
313     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
314     {
315         psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
316                                 p_sys->i_count_audio, psz_access, psz_mux );
317     }
318     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
319     {
320         psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
321                                 p_sys->i_count_video, psz_access, psz_mux );
322     }
323     else
324     {
325         int i_count;
326         if( p_fmt->i_cat == VIDEO_ES )
327         {
328             i_count = p_sys->i_count_video;
329         }
330         else if( p_fmt->i_cat == AUDIO_ES )
331         {
332             i_count = p_sys->i_count_audio;
333         }
334         else
335         {
336             i_count = p_sys->i_count;
337         }
338
339         psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
340                                 i_count, psz_access, psz_mux );
341     }
342
343     p_sys->i_count++;
344     if( p_fmt->i_cat == VIDEO_ES )
345     {
346         p_sys->i_count_video++;
347     }
348     else if( p_fmt->i_cat == AUDIO_ES )
349     {
350         p_sys->i_count_audio++;
351     }
352     msg_Dbg( p_stream, "creating `%s/%s://%s'",
353              psz_access, psz_mux, psz_dst );
354
355     /* *** find and open appropriate access module *** */
356     p_access = sout_AccessOutNew( p_stream, psz_access, psz_dst );
357     if( p_access == NULL )
358     {
359         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
360                  psz_access, psz_mux, psz_dst );
361         dialog_Fatal( p_stream,
362                     _("Streaming / Transcoding failed"),
363                     _("There is no suitable stream-output access module for \"%s/%s://%s\"."),
364                           psz_access,
365                           psz_mux, psz_dst );
366         free( psz_dst );
367         return( NULL );
368     }
369
370     /* *** find and open appropriate mux module *** */
371     p_mux = sout_MuxNew( p_stream->p_sout, psz_mux, p_access );
372     if( p_mux == NULL )
373     {
374         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
375                  psz_access, psz_mux, psz_dst );
376         dialog_Fatal( p_stream,
377                         _("Streaming / Transcoding failed"),
378                         _("There is no suitable stream-output access module "\
379                           "for \"%s/%s://%s\"."),
380                           psz_access, psz_mux, psz_dst );
381         sout_AccessOutDelete( p_access );
382         free( psz_dst );
383         return( NULL );
384     }
385     free( psz_dst );
386
387     id = malloc( sizeof( sout_stream_id_sys_t ) );
388     if( !id )
389     {
390         sout_MuxDelete( p_mux );
391         sout_AccessOutDelete( p_access );
392         return NULL;
393     }
394     id->p_mux = p_mux;
395     id->p_input = sout_MuxAddStream( p_mux, p_fmt );
396
397     if( id->p_input == NULL )
398     {
399         sout_MuxDelete( p_mux );
400         sout_AccessOutDelete( p_access );
401         free( id );
402         return NULL;
403     }
404
405     if( !sout_AccessOutCanControlPace( p_access ) )
406         p_stream->p_sout->i_out_pace_nocontrol++;
407
408     return id;
409 }
410
411 static void Del( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
412 {
413     VLC_UNUSED(p_stream);
414     sout_access_out_t *p_access = id->p_mux->p_access;
415
416     sout_MuxDeleteStream( id->p_mux, id->p_input );
417     sout_MuxDelete( id->p_mux );
418     if( !sout_AccessOutCanControlPace( p_access ) )
419         p_stream->p_sout->i_out_pace_nocontrol--;
420     sout_AccessOutDelete( p_access );
421
422     free( id );
423 }
424
425 static int Send( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
426                  block_t *p_buffer )
427 {
428     VLC_UNUSED(p_stream);
429     return sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );
430 }
431