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