]> git.sesse.net Git - vlc/blob - modules/stream_out/es.c
vlc security: As i've seen very little improvement on that front, i've decided to...
[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 #include <vlc/vlc.h>
29 #include <vlc_input.h>
30 #include <vlc_sout.h>
31 #include <vlc_interface.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 #define ACCESS_TEXT N_("Output access method")
37 #define ACCESS_LONGTEXT N_( \
38     "This is the default output access method that will be used." )
39
40 #define ACCESSA_TEXT N_("Audio output access method")
41 #define ACCESSA_LONGTEXT N_( \
42     "This is the output access method that will be used for audio." )
43 #define ACCESSV_TEXT N_("Video output access method")
44 #define ACCESSV_LONGTEXT N_( \
45     "This is the output access method that will be used for video." )
46
47 #define MUX_TEXT N_("Output muxer")
48 #define MUX_LONGTEXT N_( \
49     "This is the default muxer method that will be used." )
50 #define MUXA_TEXT N_("Audio output muxer")
51 #define MUXA_LONGTEXT N_( \
52     "This is the muxer that will be used for audio." )
53 #define MUXV_TEXT N_("Video output muxer")
54 #define MUXV_LONGTEXT N_( \
55     "This is the muxer that will be used for video." )
56
57 #define DEST_TEXT N_("Output URL")
58 #define DEST_LONGTEXT N_( \
59     "This is the default output URI." )
60 #define DESTA_TEXT N_("Audio output URL")
61 #define DESTA_LONGTEXT N_( \
62     "This is the output URI that will be used for audio." )
63 #define DESTV_TEXT N_("Video output URL")
64 #define DESTV_LONGTEXT N_( \
65     "This is the output URI that will be used for video." )
66
67 static int      Open    ( vlc_object_t * );
68 static void     Close   ( vlc_object_t * );
69
70 #define SOUT_CFG_PREFIX "sout-es-"
71
72 vlc_module_begin();
73     set_shortname( "ES" );
74     set_description( _("Elementary stream output") );
75     set_capability( "sout stream", 50 );
76     add_shortcut( "es" );
77     set_category( CAT_SOUT );
78     set_subcategory( SUBCAT_SOUT_STREAM );
79
80     add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
81                 ACCESS_LONGTEXT, VLC_TRUE );
82     add_string( SOUT_CFG_PREFIX "access-audio", "", NULL, ACCESSA_TEXT,
83                 ACCESSA_LONGTEXT, VLC_TRUE );
84     add_string( SOUT_CFG_PREFIX "access-video", "", NULL, ACCESSV_TEXT,
85                 ACCESSV_LONGTEXT, VLC_TRUE );
86
87     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
88                 MUX_LONGTEXT, VLC_TRUE );
89     add_string( SOUT_CFG_PREFIX "mux-audio", "", NULL, MUXA_TEXT,
90                 MUXA_LONGTEXT, VLC_TRUE );
91     add_string( SOUT_CFG_PREFIX "mux-video", "", NULL, MUXV_TEXT,
92                 MUXV_LONGTEXT, VLC_TRUE );
93
94     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
95                 DEST_LONGTEXT, VLC_TRUE );
96         change_unsafe();
97     add_string( SOUT_CFG_PREFIX "dst-audio", "", NULL, DESTA_TEXT,
98                 DESTA_LONGTEXT, VLC_TRUE );
99         change_unsafe();
100     add_string( SOUT_CFG_PREFIX "dst-video", "", NULL, DESTV_TEXT,
101                 DESTV_LONGTEXT, VLC_TRUE );
102         change_unsafe();
103
104     set_callbacks( Open, Close );
105 vlc_module_end();
106
107 /*****************************************************************************
108  * Exported prototypes
109  *****************************************************************************/
110 static const char *ppsz_sout_options[] = {
111     "access", "access-audio", "access-video",
112     "mux", "mux-audio", "mux-video",
113     "dst", "dst-audio", "dst-video",
114     NULL
115 };
116
117 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
118 static int               Del ( sout_stream_t *, sout_stream_id_t * );
119 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
120
121 struct sout_stream_sys_t
122 {
123     int  i_count_audio;
124     int  i_count_video;
125     int  i_count;
126
127     char *psz_mux;
128     char *psz_mux_audio;
129     char *psz_mux_video;
130
131     char *psz_access;
132     char *psz_access_audio;
133     char *psz_access_video;
134
135     char *psz_dst;
136     char *psz_dst_audio;
137     char *psz_dst_video;
138 };
139
140 /*****************************************************************************
141  * Open:
142  *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
144 {
145     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
146     sout_stream_sys_t   *p_sys;
147     vlc_value_t         val;
148
149     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
150     p_sys                   = malloc( sizeof( sout_stream_sys_t ) );
151
152     p_sys->i_count          = 0;
153     p_sys->i_count_audio    = 0;
154     p_sys->i_count_video    = 0;
155
156     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
157     p_sys->psz_access       = val.psz_string;
158     var_Get( p_stream, SOUT_CFG_PREFIX "access-audio", &val );
159     p_sys->psz_access_audio = val.psz_string;
160     var_Get( p_stream, SOUT_CFG_PREFIX "access-video", &val );
161     p_sys->psz_access_video = val.psz_string;
162
163     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
164     p_sys->psz_mux       = val.psz_string;
165     var_Get( p_stream, SOUT_CFG_PREFIX "mux-audio", &val );
166     p_sys->psz_mux_audio = val.psz_string;
167     var_Get( p_stream, SOUT_CFG_PREFIX "mux-video", &val );
168     p_sys->psz_mux_video = val.psz_string;
169
170     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
171     p_sys->psz_dst       = val.psz_string;
172     var_Get( p_stream, SOUT_CFG_PREFIX "dst-audio", &val );
173     p_sys->psz_dst_audio = val.psz_string;
174     var_Get( p_stream, SOUT_CFG_PREFIX "dst-video", &val );
175     p_sys->psz_dst_video = val.psz_string;
176
177     p_stream->pf_add    = Add;
178     p_stream->pf_del    = Del;
179     p_stream->pf_send   = Send;
180
181     p_stream->p_sys     = p_sys;
182
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  * Close:
188  *****************************************************************************/
189
190 static void Close( vlc_object_t * p_this )
191 {
192     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
193     sout_stream_sys_t *p_sys = p_stream->p_sys;
194
195     free( p_sys->psz_access );
196     free( p_sys->psz_access_audio );
197     free( p_sys->psz_access_video );
198
199     free( p_sys->psz_mux );
200     free( p_sys->psz_mux_audio );
201     free( p_sys->psz_mux_video );
202
203     free( p_sys->psz_dst );
204     free( p_sys->psz_dst_audio );
205     free( p_sys->psz_dst_video );
206
207     free( p_sys );
208 }
209
210 struct sout_stream_id_t
211 {
212     sout_input_t *p_input;
213     sout_mux_t   *p_mux;
214 };
215
216 static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
217                             char *psz_access, char *psz_mux )
218 {
219     char *psz_dst, *p;
220
221     if( psz_fmt == NULL || !*psz_fmt )
222     {
223         psz_fmt = "stream-%n-%c.%m";
224     }
225
226     p = psz_dst = malloc( 4096 );
227     memset( p, 0, 4096 );
228     for( ;; )
229     {
230         if( *psz_fmt == '\0' )
231         {
232             *p = '\0';
233             break;
234         }
235
236         if( *psz_fmt != '%' )
237         {
238             *p++ = *psz_fmt++;
239         }
240         else
241         {
242             if( psz_fmt[1] == 'n' )
243             {
244                 p += sprintf( p, "%d", i_count );
245             }
246             else if( psz_fmt[1] == 'c' )
247             {
248                 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
249             }
250             else if( psz_fmt[1] == 'm' )
251             {
252                 p += sprintf( p, "%s", psz_mux );
253             }
254             else if( psz_fmt[1] == 'a' )
255             {
256                 p += sprintf( p, "%s", psz_access );
257             }
258             else if( psz_fmt[1] != '\0' )
259             {
260                 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
261             }
262             else
263             {
264                 p += sprintf( p, "%c", psz_fmt[0] );
265                 *p++ = '\0';
266                 break;
267             }
268             psz_fmt += 2;
269         }
270     }
271
272     return( psz_dst );
273 }
274
275 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
276 {
277     sout_stream_sys_t *p_sys = p_stream->p_sys;
278     sout_instance_t   *p_sout = p_stream->p_sout;
279     sout_stream_id_t  *id;
280
281     char              *psz_access;
282     char              *psz_mux;
283     char              *psz_dst;
284
285     sout_access_out_t *p_access;
286     sout_mux_t        *p_mux;
287
288     /* *** get access name *** */
289     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
290     {
291         psz_access = p_sys->psz_access_audio;
292     }
293     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
294     {
295         psz_access = p_sys->psz_access_video;
296     }
297     else
298     {
299         psz_access = p_sys->psz_access;
300     }
301
302     /* *** get mux name *** */
303     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
304     {
305         psz_mux = p_sys->psz_mux_audio;
306     }
307     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
308     {
309         psz_mux = p_sys->psz_mux_video;
310     }
311     else
312     {
313         psz_mux = p_sys->psz_mux;
314     }
315
316     /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
317     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
318     {
319         psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
320                                 p_sys->i_count_audio, psz_access, psz_mux );
321     }
322     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
323     {
324         psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
325                                 p_sys->i_count_video, psz_access, psz_mux );
326     }
327     else
328     {
329         int i_count;
330         if( p_fmt->i_cat == VIDEO_ES )
331         {
332             i_count = p_sys->i_count_video;
333         }
334         else if( p_fmt->i_cat == AUDIO_ES )
335         {
336             i_count = p_sys->i_count_audio;
337         }
338         else
339         {
340             i_count = p_sys->i_count;
341         }
342
343         psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
344                                 i_count, psz_access, psz_mux );
345     }
346
347     p_sys->i_count++;
348     if( p_fmt->i_cat == VIDEO_ES )
349     {
350         p_sys->i_count_video++;
351     }
352     else if( p_fmt->i_cat == AUDIO_ES )
353     {
354         p_sys->i_count_audio++;
355     }
356     msg_Dbg( p_stream, "creating `%s/%s://%s'",
357              psz_access, psz_mux, psz_dst );
358
359     /* *** find and open appropriate access module *** */
360     p_access = sout_AccessOutNew( p_sout, psz_access, psz_dst );
361     if( p_access == NULL )
362     {
363         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
364                  psz_access, psz_mux, psz_dst );
365         intf_UserFatal( p_stream, VLC_FALSE,
366                     _("Streaming / Transcoding failed"),
367                     _("There is no suitable stream-output access module for \"%s/%s://%s\"."),
368                           psz_access,
369                           psz_mux, psz_dst );
370         return( NULL );
371     }
372
373     /* *** find and open appropriate mux module *** */
374     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
375     if( p_mux == NULL )
376     {
377         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
378                  psz_access, psz_mux, psz_dst );
379         intf_UserFatal( p_stream, VLC_FALSE,
380                         _("Streaming / Transcoding failed"),
381                         _("There is no suitable stream-output access module "\
382                           "for \"%s/%s://%s\"."),
383                           psz_access, psz_mux, psz_dst );
384         sout_AccessOutDelete( p_access );
385         return( NULL );
386     }
387
388     id = malloc( sizeof( sout_stream_id_t ) );
389     id->p_mux = p_mux;
390     id->p_input = sout_MuxAddStream( p_mux, p_fmt );
391
392     if( id->p_input == NULL )
393     {
394         free( id );
395
396         sout_MuxDelete( p_mux );
397         sout_AccessOutDelete( p_access );
398         free( id );
399         return NULL;
400     }
401
402     return id;
403 }
404
405 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
406 {
407     sout_access_out_t *p_access = id->p_mux->p_access;
408     sout_MuxDelete( id->p_mux );
409     sout_MuxDeleteStream( id->p_mux, id->p_input );
410     sout_AccessOutDelete( p_access );
411
412     free( id );
413     return VLC_SUCCESS;
414 }
415
416 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
417                  block_t *p_buffer )
418 {
419     sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );
420
421     return VLC_SUCCESS;
422 }
423