]> git.sesse.net Git - vlc/blob - modules/stream_out/es.c
* all: use sout_ParseCfg. But "standard" can't use it for sap/slp
[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 static int      Open    ( vlc_object_t * );
38 static void     Close   ( vlc_object_t * );
39
40 #define SOUT_CFG_PREFIX "sout-es-"
41
42 vlc_module_begin();
43     set_description( _("Elementary stream output") );
44     set_capability( "sout stream", 50 );
45     add_shortcut( "es" );
46
47     add_string( SOUT_CFG_PREFIX "access", "", NULL, "access", "", VLC_TRUE );
48     add_string( SOUT_CFG_PREFIX "access-audio", "", NULL, "access audio", "", VLC_TRUE );
49     add_string( SOUT_CFG_PREFIX "access-video", "", NULL, "access video", "", VLC_TRUE );
50
51     add_string( SOUT_CFG_PREFIX "mux", "", NULL, "mux", "", VLC_TRUE );
52     add_string( SOUT_CFG_PREFIX "mux-audio", "", NULL, "mux audio", "", VLC_TRUE );
53     add_string( SOUT_CFG_PREFIX "mux-video", "", NULL, "mux video", "", VLC_TRUE );
54
55     add_string( SOUT_CFG_PREFIX "dst", "", NULL, "dst", "", VLC_TRUE );
56     add_string( SOUT_CFG_PREFIX "dst-audio", "", NULL, "dst audio", "", VLC_TRUE );
57     add_string( SOUT_CFG_PREFIX "dst-video", "", NULL, "dst video", "", VLC_TRUE );
58
59     set_callbacks( Open, Close );
60 vlc_module_end();
61
62
63 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
64 /*****************************************************************************
65  * Exported prototypes
66  *****************************************************************************/
67 static const char *ppsz_sout_options[] = {
68     "access", "access-audio", "access-video",
69     "mux", "mux-audio", "mux-video",
70     "dst", "dst-audio", "dst-video",
71     NULL
72 };
73
74 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
75 static int               Del ( sout_stream_t *, sout_stream_id_t * );
76 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
77
78 struct sout_stream_sys_t
79 {
80     int  i_count_audio;
81     int  i_count_video;
82     int  i_count;
83
84     char *psz_mux;
85     char *psz_mux_audio;
86     char *psz_mux_video;
87
88     char *psz_access;
89     char *psz_access_audio;
90     char *psz_access_video;
91
92     char *psz_dst;
93     char *psz_dst_audio;
94     char *psz_dst_video;
95 };
96
97 /*****************************************************************************
98  * Open:
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
103     sout_stream_sys_t   *p_sys;
104     vlc_value_t         val;
105
106     sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
107     p_sys                   = malloc( sizeof( sout_stream_sys_t ) );
108
109     p_sys->i_count          = 0;
110     p_sys->i_count_audio    = 0;
111     p_sys->i_count_video    = 0;
112
113     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
114     p_sys->psz_access       = val.psz_string;
115     var_Get( p_stream, SOUT_CFG_PREFIX "access-audio", &val );
116     p_sys->psz_access_audio = val.psz_string;
117     var_Get( p_stream, SOUT_CFG_PREFIX "access-video", &val );
118     p_sys->psz_access_video = val.psz_string;
119
120     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
121     p_sys->psz_mux       = val.psz_string;
122     var_Get( p_stream, SOUT_CFG_PREFIX "mux-audio", &val );
123     p_sys->psz_mux_audio = val.psz_string;
124     var_Get( p_stream, SOUT_CFG_PREFIX "mux-video", &val );
125     p_sys->psz_mux_video = val.psz_string;
126
127     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
128     p_sys->psz_dst       = val.psz_string;
129     var_Get( p_stream, SOUT_CFG_PREFIX "dst-audio", &val );
130     p_sys->psz_dst_audio = val.psz_string;
131     var_Get( p_stream, SOUT_CFG_PREFIX "dst-video", &val );
132     p_sys->psz_dst_video = val.psz_string;
133
134     p_stream->pf_add    = Add;
135     p_stream->pf_del    = Del;
136     p_stream->pf_send   = Send;
137
138     p_stream->p_sys     = p_sys;
139
140     return VLC_SUCCESS;
141 }
142
143 /*****************************************************************************
144  * Close:
145  *****************************************************************************/
146
147 static void Close( vlc_object_t * p_this )
148 {
149     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
150     sout_stream_sys_t *p_sys = p_stream->p_sys;
151
152     free( p_sys->psz_access );
153     free( p_sys->psz_access_audio );
154     free( p_sys->psz_access_video );
155
156     free( p_sys->psz_mux );
157     free( p_sys->psz_mux_audio );
158     free( p_sys->psz_mux_video );
159
160     free( p_sys->psz_dst );
161     free( p_sys->psz_dst_audio );
162     free( p_sys->psz_dst_video );
163
164     free( p_sys );
165 }
166
167 struct sout_stream_id_t
168 {
169     sout_input_t *p_input;
170     sout_mux_t   *p_mux;
171 };
172
173 static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
174                             char *psz_access, char *psz_mux )
175 {
176     char *psz_dst, *p;
177
178     if( psz_fmt == NULL || !*psz_fmt )
179     {
180         psz_fmt = "stream-%n-%c.%m";
181     }
182
183     p = psz_dst = malloc( 4096 );
184     memset( p, 0, 4096 );
185     for( ;; )
186     {
187         if( *psz_fmt == '\0' )
188         {
189             *p = '\0';
190             break;
191         }
192
193         if( *psz_fmt != '%' )
194         {
195             *p++ = *psz_fmt++;
196         }
197         else
198         {
199             if( psz_fmt[1] == 'n' )
200             {
201                 p += sprintf( p, "%d", i_count );
202             }
203             else if( psz_fmt[1] == 'c' )
204             {
205                 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
206             }
207             else if( psz_fmt[1] == 'm' )
208             {
209                 p += sprintf( p, "%s", psz_mux );
210             }
211             else if( psz_fmt[1] == 'a' )
212             {
213                 p += sprintf( p, "%s", psz_access );
214             }
215             else if( psz_fmt[1] != '\0' )
216             {
217                 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
218             }
219             else
220             {
221                 p += sprintf( p, "%c", psz_fmt[0] );
222                 *p++ = '\0';
223                 break;
224             }
225             psz_fmt += 2;
226         }
227     }
228
229     return( psz_dst );
230 }
231
232 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
233 {
234     sout_stream_sys_t *p_sys = p_stream->p_sys;
235     sout_instance_t   *p_sout = p_stream->p_sout;
236     sout_stream_id_t  *id;
237
238     char              *psz_access;
239     char              *psz_mux;
240     char              *psz_dst;
241
242     sout_access_out_t *p_access;
243     sout_mux_t        *p_mux;
244
245     /* *** get access name *** */
246     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
247     {
248         psz_access = p_sys->psz_access_audio;
249     }
250     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
251     {
252         psz_access = p_sys->psz_access_video;
253     }
254     else
255     {
256         psz_access = p_sys->psz_access;
257     }
258
259     /* *** get mux name *** */
260     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
261     {
262         psz_mux = p_sys->psz_mux_audio;
263     }
264     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
265     {
266         psz_mux = p_sys->psz_mux_video;
267     }
268     else
269     {
270         psz_mux = p_sys->psz_mux;
271     }
272
273     /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
274     if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
275     {
276         psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
277                                 p_sys->i_count_audio, psz_access, psz_mux );
278     }
279     else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
280     {
281         psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
282                                 p_sys->i_count_video, psz_access, psz_mux );
283     }
284     else
285     {
286         int i_count;
287         if( p_fmt->i_cat == VIDEO_ES )
288         {
289             i_count = p_sys->i_count_video;
290         }
291         else if( p_fmt->i_cat == AUDIO_ES )
292         {
293             i_count = p_sys->i_count_audio;
294         }
295         else
296         {
297             i_count = p_sys->i_count;
298         }
299
300         psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
301                                 i_count, psz_access, psz_mux );
302     }
303
304     p_sys->i_count++;
305     if( p_fmt->i_cat == VIDEO_ES )
306     {
307         p_sys->i_count_video++;
308     }
309     else if( p_fmt->i_cat == AUDIO_ES )
310     {
311         p_sys->i_count_audio++;
312     }
313     msg_Dbg( p_stream, "creating `%s/%s://%s'",
314              psz_access, psz_mux, psz_dst );
315
316     /* *** find and open appropriate access module *** */
317     p_access = sout_AccessOutNew( p_sout, psz_access, psz_dst );
318     if( p_access == NULL )
319     {
320         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
321                  psz_access, psz_mux, psz_dst );
322         return( NULL );
323     }
324
325     /* *** find and open appropriate mux module *** */
326     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
327     if( p_mux == NULL )
328     {
329         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
330                  psz_access, psz_mux, psz_dst );
331         sout_AccessOutDelete( p_access );
332         return( NULL );
333     }
334
335     id = malloc( sizeof( sout_stream_id_t ) );
336     id->p_mux = p_mux;
337     id->p_input = sout_MuxAddStream( p_mux, p_fmt );
338
339     if( id->p_input == NULL )
340     {
341         free( id );
342
343         sout_MuxDelete( p_mux );
344         sout_AccessOutDelete( p_access );
345         free( id );
346         return NULL;
347     }
348
349     return id;
350 }
351
352 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
353 {
354     sout_access_out_t *p_access = id->p_mux->p_access;
355
356     sout_MuxDeleteStream( id->p_mux, id->p_input );
357     sout_AccessOutDelete( p_access );
358
359     free( id );
360     return VLC_SUCCESS;
361 }
362
363 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
364                  block_t *p_buffer )
365 {
366     sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );
367
368     return VLC_SUCCESS;
369 }
370