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