]> git.sesse.net Git - vlc/blob - modules/stream_out/es.c
Sort options.
[vlc] / modules / stream_out / es.c
1 /*****************************************************************************
2  * es.c: Elementary stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, 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_interface.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", "", NULL, ACCESS_TEXT,
87                 ACCESS_LONGTEXT, true );
88     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
89                 MUX_LONGTEXT, true );
90     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
91                 DEST_LONGTEXT, true );
92         change_unsafe();
93
94     set_section( N_("Audio"), NULL );
95     add_string( SOUT_CFG_PREFIX "access-audio", "", NULL, ACCESSA_TEXT,
96                 ACCESSA_LONGTEXT, true );
97     add_string( SOUT_CFG_PREFIX "mux-audio", "", NULL, MUXA_TEXT,
98                 MUXA_LONGTEXT, true );
99     add_string( SOUT_CFG_PREFIX "dst-audio", "", NULL, DESTA_TEXT,
100                 DESTA_LONGTEXT, true );
101         change_unsafe();
102
103     set_section( N_("Video"), NULL );
104     add_string( SOUT_CFG_PREFIX "access-video", "", NULL, ACCESSV_TEXT,
105                 ACCESSV_LONGTEXT, true );
106     add_string( SOUT_CFG_PREFIX "mux-video", "", NULL, MUXV_TEXT,
107                 MUXV_LONGTEXT, true );
108     add_string( SOUT_CFG_PREFIX "dst-video", "", NULL, DESTV_TEXT,
109                 DESTV_LONGTEXT, true );
110         change_unsafe();
111
112     set_callbacks( Open, Close );
113 vlc_module_end();
114
115 /*****************************************************************************
116  * Exported prototypes
117  *****************************************************************************/
118 static const char *const ppsz_sout_options[] = {
119     "access", "access-audio", "access-video",
120     "mux", "mux-audio", "mux-video",
121     "dst", "dst-audio", "dst-video",
122     NULL
123 };
124
125 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
126 static int               Del ( sout_stream_t *, sout_stream_id_t * );
127 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
128
129 struct sout_stream_sys_t
130 {
131     int  i_count_audio;
132     int  i_count_video;
133     int  i_count;
134
135     char *psz_mux;
136     char *psz_mux_audio;
137     char *psz_mux_video;
138
139     char *psz_access;
140     char *psz_access_audio;
141     char *psz_access_video;
142
143     char *psz_dst;
144     char *psz_dst_audio;
145     char *psz_dst_video;
146 };
147
148 /*****************************************************************************
149  * Open:
150  *****************************************************************************/
151 static int Open( vlc_object_t *p_this )
152 {
153     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
154     sout_stream_sys_t   *p_sys;
155     vlc_value_t         val;
156
157     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
158     p_sys                   = malloc( sizeof( sout_stream_sys_t ) );
159
160     p_sys->i_count          = 0;
161     p_sys->i_count_audio    = 0;
162     p_sys->i_count_video    = 0;
163
164     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
165     p_sys->psz_access       = val.psz_string;
166     var_Get( p_stream, SOUT_CFG_PREFIX "access-audio", &val );
167     p_sys->psz_access_audio = val.psz_string;
168     var_Get( p_stream, SOUT_CFG_PREFIX "access-video", &val );
169     p_sys->psz_access_video = val.psz_string;
170
171     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
172     p_sys->psz_mux       = val.psz_string;
173     var_Get( p_stream, SOUT_CFG_PREFIX "mux-audio", &val );
174     p_sys->psz_mux_audio = val.psz_string;
175     var_Get( p_stream, SOUT_CFG_PREFIX "mux-video", &val );
176     p_sys->psz_mux_video = val.psz_string;
177
178     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
179     p_sys->psz_dst       = val.psz_string;
180     var_Get( p_stream, SOUT_CFG_PREFIX "dst-audio", &val );
181     p_sys->psz_dst_audio = val.psz_string;
182     var_Get( p_stream, SOUT_CFG_PREFIX "dst-video", &val );
183     p_sys->psz_dst_video = val.psz_string;
184
185     p_stream->pf_add    = Add;
186     p_stream->pf_del    = Del;
187     p_stream->pf_send   = Send;
188
189     p_stream->p_sys     = p_sys;
190
191     return VLC_SUCCESS;
192 }
193
194 /*****************************************************************************
195  * Close:
196  *****************************************************************************/
197
198 static void Close( vlc_object_t * p_this )
199 {
200     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
201     sout_stream_sys_t *p_sys = p_stream->p_sys;
202
203     free( p_sys->psz_access );
204     free( p_sys->psz_access_audio );
205     free( p_sys->psz_access_video );
206
207     free( p_sys->psz_mux );
208     free( p_sys->psz_mux_audio );
209     free( p_sys->psz_mux_video );
210
211     free( p_sys->psz_dst );
212     free( p_sys->psz_dst_audio );
213     free( p_sys->psz_dst_video );
214
215     free( p_sys );
216 }
217
218 struct sout_stream_id_t
219 {
220     sout_input_t *p_input;
221     sout_mux_t   *p_mux;
222 };
223
224 static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
225                             char *psz_access, char *psz_mux )
226 {
227     char *psz_dst, *p;
228
229     if( psz_fmt == NULL || !*psz_fmt )
230     {
231         psz_fmt = (char*)"stream-%n-%c.%m";
232     }
233
234     p = psz_dst = malloc( 4096 );
235     if( !psz_dst )
236         return NULL;
237     memset( p, 0, 4096 );
238     for( ;; )
239     {
240         if( *psz_fmt == '\0' )
241         {
242             *p = '\0';
243             break;
244         }
245
246         if( *psz_fmt != '%' )
247         {
248             *p++ = *psz_fmt++;
249         }
250         else
251         {
252             if( psz_fmt[1] == 'n' )
253             {
254                 p += sprintf( p, "%d", i_count );
255             }
256             else if( psz_fmt[1] == 'c' )
257             {
258                 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
259             }
260             else if( psz_fmt[1] == 'm' )
261             {
262                 p += sprintf( p, "%s", psz_mux );
263             }
264             else if( psz_fmt[1] == 'a' )
265             {
266                 p += sprintf( p, "%s", psz_access );
267             }
268             else if( psz_fmt[1] != '\0' )
269             {
270                 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
271             }
272             else
273             {
274                 p += sprintf( p, "%c", psz_fmt[0] );
275                 *p++ = '\0';
276                 break;
277             }
278             psz_fmt += 2;
279         }
280     }
281
282     return( psz_dst );
283 }
284
285 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
286 {
287     sout_stream_sys_t *p_sys = p_stream->p_sys;
288     sout_instance_t   *p_sout = p_stream->p_sout;
289     sout_stream_id_t  *id;
290
291     char              *psz_access;
292     char              *psz_mux;
293     char              *psz_dst;
294
295     sout_access_out_t *p_access;
296     sout_mux_t        *p_mux;
297
298     /* *** get access name *** */
299     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
300     {
301         psz_access = p_sys->psz_access_audio;
302     }
303     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
304     {
305         psz_access = p_sys->psz_access_video;
306     }
307     else
308     {
309         psz_access = p_sys->psz_access;
310     }
311
312     /* *** get mux name *** */
313     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
314     {
315         psz_mux = p_sys->psz_mux_audio;
316     }
317     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
318     {
319         psz_mux = p_sys->psz_mux_video;
320     }
321     else
322     {
323         psz_mux = p_sys->psz_mux;
324     }
325
326     /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
327     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
328     {
329         psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
330                                 p_sys->i_count_audio, psz_access, psz_mux );
331     }
332     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
333     {
334         psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
335                                 p_sys->i_count_video, psz_access, psz_mux );
336     }
337     else
338     {
339         int i_count;
340         if( p_fmt->i_cat == VIDEO_ES )
341         {
342             i_count = p_sys->i_count_video;
343         }
344         else if( p_fmt->i_cat == AUDIO_ES )
345         {
346             i_count = p_sys->i_count_audio;
347         }
348         else
349         {
350             i_count = p_sys->i_count;
351         }
352
353         psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
354                                 i_count, psz_access, psz_mux );
355     }
356
357     p_sys->i_count++;
358     if( p_fmt->i_cat == VIDEO_ES )
359     {
360         p_sys->i_count_video++;
361     }
362     else if( p_fmt->i_cat == AUDIO_ES )
363     {
364         p_sys->i_count_audio++;
365     }
366     msg_Dbg( p_stream, "creating `%s/%s://%s'",
367              psz_access, psz_mux, psz_dst );
368
369     /* *** find and open appropriate access module *** */
370     p_access = sout_AccessOutNew( p_sout, psz_access, psz_dst );
371     if( p_access == NULL )
372     {
373         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
374                  psz_access, psz_mux, psz_dst );
375         intf_UserFatal( p_stream, false,
376                     _("Streaming / Transcoding failed"),
377                     _("There is no suitable stream-output access module for \"%s/%s://%s\"."),
378                           psz_access,
379                           psz_mux, psz_dst );
380         free( psz_dst );
381         return( NULL );
382     }
383
384     /* *** find and open appropriate mux module *** */
385     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
386     if( p_mux == NULL )
387     {
388         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
389                  psz_access, psz_mux, psz_dst );
390         intf_UserFatal( p_stream, false,
391                         _("Streaming / Transcoding failed"),
392                         _("There is no suitable stream-output access module "\
393                           "for \"%s/%s://%s\"."),
394                           psz_access, psz_mux, psz_dst );
395         sout_AccessOutDelete( p_access );
396         free( psz_dst );
397         return( NULL );
398     }
399     free( psz_dst );
400
401     id = malloc( sizeof( sout_stream_id_t ) );
402     if( !id )
403     {
404         sout_MuxDelete( p_mux );
405         sout_AccessOutDelete( p_access );
406         return NULL;
407     }
408     id->p_mux = p_mux;
409     id->p_input = sout_MuxAddStream( p_mux, p_fmt );
410
411     if( id->p_input == NULL )
412     {
413         sout_MuxDelete( p_mux );
414         sout_AccessOutDelete( p_access );
415         free( id );
416         return NULL;
417     }
418
419     if( !sout_AccessOutCanControlPace( p_access ) )
420         p_sout->i_out_pace_nocontrol++;
421
422     return id;
423 }
424
425 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
426 {
427     VLC_UNUSED(p_stream);
428     sout_access_out_t *p_access = id->p_mux->p_access;
429     sout_MuxDelete( id->p_mux );
430     sout_MuxDeleteStream( id->p_mux, id->p_input );
431     if( !sout_AccessOutCanControlPace( p_access ) )
432         p_stream->p_sout->i_out_pace_nocontrol--;
433     sout_AccessOutDelete( p_access );
434
435     free( id );
436     return VLC_SUCCESS;
437 }
438
439 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
440                  block_t *p_buffer )
441 {
442     VLC_UNUSED(p_stream);
443     sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );
444
445     return VLC_SUCCESS;
446 }
447