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