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