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