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