]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
Restore some kind of SAP support
[vlc] / modules / stream_out / standard.c
1 /*****************************************************************************
2  * standard.c: standard 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 <vlc/vlc.h>
28 #include <vlc_sout.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 #include <vlc_network.h>
35 #include "vlc_url.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 #define ACCESS_TEXT N_("Output access method")
41 /// \bug [String] "Output method to use for the stream." 
42 #define ACCESS_LONGTEXT N_( \
43     "This is the output access method that will be used." )
44 #define MUX_TEXT N_("Output muxer")
45 /// \bug [String] "Muxer to use for the stream." 
46 #define MUX_LONGTEXT N_( \
47     "This is the muxer that will be used." )
48 #define DST_TEXT N_("Output destination")
49 /// \bug [String] "Destination (URL) to use for the stream." 
50 #define DST_LONGTEXT N_( \
51     "This is the destination (URL) that will be used for the stream." )
52 #define NAME_TEXT N_("Session name")
53 #define NAME_LONGTEXT N_( \
54   "This allows you to specify a name for the session, that will be announced "\
55   "if you choose to use SAP." )
56
57 #define GROUP_TEXT N_("Session groupname")
58 #define GROUP_LONGTEXT N_( \
59   "This allows you to specify a group for the session, that will be announced "\
60   "if you choose to use SAP." )
61
62 #define DESC_TEXT N_("Session descriptipn")
63 #define DESC_LONGTEXT N_( \
64     "This allows you to give a short description with details about the stream, " \
65     "that will be announced in the SDP (Session Descriptor)." )
66 #define URL_TEXT N_("Session URL")
67 #define URL_LONGTEXT N_( \
68     "This allows you to give an URL with more details about the stream " \
69     "(often the website of the streaming organization), that will " \
70     "be announced in the SDP (Session Descriptor)." )
71 #define EMAIL_TEXT N_("Session email")
72 #define EMAIL_LONGTEXT N_( \
73     "This allows you to give a contact mail address for the stream, that will " \
74     "be announced in the SDP (Session Descriptor)." )
75 #define PHONE_TEXT N_("Session phone number")
76 #define PHONE_LONGTEXT N_( \
77     "This allows you to give a contact telephone number for the stream, that will " \
78     "be announced in the SDP (Session Descriptor)." )
79
80
81 #define SAP_TEXT N_("SAP announcing")
82 #define SAP_LONGTEXT N_("Announce this session with SAP.")
83
84 static int      Open    ( vlc_object_t * );
85 static void     Close   ( vlc_object_t * );
86
87 #define SOUT_CFG_PREFIX "sout-standard-"
88
89 vlc_module_begin();
90     set_shortname( _("Standard"));
91     set_description( _("Standard stream output") );
92     set_capability( "sout stream", 50 );
93     add_shortcut( "standard" );
94     add_shortcut( "std" );
95     set_category( CAT_SOUT );
96     set_subcategory( SUBCAT_SOUT_STREAM );
97
98     add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
99                 ACCESS_LONGTEXT, VLC_FALSE );
100     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
101                 MUX_LONGTEXT, VLC_FALSE );
102     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
103                 DST_LONGTEXT, VLC_FALSE );
104
105     add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
106     add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
107                                         VLC_TRUE );
108     add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
109                                         VLC_TRUE );
110     add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
111                                         VLC_TRUE );
112     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
113                                         VLC_TRUE );
114     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
115                                         VLC_TRUE );
116     add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
117                                         VLC_TRUE );
118     add_suppressed_bool( SOUT_CFG_PREFIX "sap-ipv6" );
119
120     set_callbacks( Open, Close );
121 vlc_module_end();
122
123
124 /*****************************************************************************
125  * Exported prototypes
126  *****************************************************************************/
127 static const char *ppsz_sout_options[] = {
128     "access", "mux", "url", "dst",
129     "sap", "name", "group", "description", "url", "email", "phone", NULL
130 };
131
132 #define DEFAULT_PORT 1234
133
134 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
135 static int               Del ( sout_stream_t *, sout_stream_id_t * );
136 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
137
138 struct sout_stream_sys_t
139 {
140     sout_mux_t           *p_mux;
141     session_descriptor_t *p_session;
142 };
143
144 /*****************************************************************************
145  * Open:
146  *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
148 {
149     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
150     sout_instance_t     *p_sout = p_stream->p_sout;
151
152     char *psz_mux;
153     char *psz_access;
154     char *psz_url;
155
156     vlc_value_t val;
157
158     sout_access_out_t   *p_access;
159     sout_mux_t          *p_mux;
160
161     const char          *psz_mux_byext = NULL;
162
163     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
164                    p_stream->p_cfg );
165
166     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
167     psz_access = *val.psz_string ? val.psz_string : NULL;
168     if( !*val.psz_string ) free( val.psz_string );
169
170     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
171     psz_mux = *val.psz_string ? val.psz_string : NULL;
172     if( !*val.psz_string ) free( val.psz_string );
173
174
175     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
176     psz_url = *val.psz_string ? val.psz_string : NULL;
177     if( !*val.psz_string ) free( val.psz_string );
178
179     p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
180     p_stream->p_sys->p_session = NULL;
181
182     msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
183
184     /* ext -> muxer name */
185     if( psz_url && strrchr( psz_url, '.' ) )
186     {
187         /* by extension */
188         static struct { const char *ext; const char *mux; } exttomux[] =
189         {
190             { "avi", "avi" },
191             { "ogg", "ogg" },
192             { "ogm", "ogg" },
193             { "mp4", "mp4" },
194             { "mov", "mov" },
195             { "moov","mov" },
196             { "asf", "asf" },
197             { "wma", "asf" },
198             { "wmv", "asf" },
199             { "trp", "ts" },
200             { "ts",  "ts" },
201             { "mpg", "ps" },
202             { "mpeg","ps" },
203             { "ps",  "ps" },
204             { "mpeg1","mpeg1" },
205             { "wav","wav" },
206             { NULL,  NULL }
207         };
208         const char *psz_ext = strrchr( psz_url, '.' ) + 1;
209         int  i;
210
211         msg_Dbg( p_this, "extension is %s", psz_ext );
212         for( i = 0; exttomux[i].ext != NULL; i++ )
213         {
214             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
215             {
216                 psz_mux_byext = exttomux[i].mux;
217                 break;
218             }
219         }
220         msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
221     }
222
223     /* We fix access/mux to valid couple */
224
225     if( !psz_access && !psz_mux )
226     {
227         if( psz_mux_byext )
228         {
229             msg_Warn( p_stream,
230                       "no access _and_ no muxer, extension gives file/%s",
231                       psz_mux_byext );
232             psz_access = strdup("file");
233             psz_mux    = strdup(psz_mux_byext);
234         }
235         else
236         {
237             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
238             return VLC_EGENERIC;
239         }
240     }
241
242     if( psz_access && !psz_mux )
243     {
244         /* access given, no mux */
245         if( !strncmp( psz_access, "mmsh", 4 ) )
246         {
247             psz_mux = strdup("asfh");
248         }
249         else if (!strcmp (psz_access, "udp")
250               || !strcmp (psz_access, "rtp") || !strcmp (psz_access, "udplite")
251               || !strcmp (psz_access, "tcp") || !strcmp (psz_access, "sctp")
252               || !strcmp (psz_access, "dccp"))
253         {
254             psz_mux = strdup("ts");
255         }
256         else if( psz_mux_byext )
257         {
258             psz_mux = strdup(psz_mux_byext);
259         }
260         else
261         {
262             msg_Err( p_stream, "no mux specified or found by extension" );
263             return VLC_EGENERIC;
264         }
265     }
266     else if( psz_mux && !psz_access )
267     {
268         /* mux given, no access */
269         if( !strncmp( psz_mux, "asfh", 4 ) )
270         {
271             psz_access = strdup("mmsh");
272         }
273         else
274         {
275             /* default file */
276             psz_access = strdup("file");
277         }
278     }
279
280     /* fix or warn of incompatible couple */
281     if( psz_mux && psz_access )
282     {
283         if( !strncmp( psz_access, "mmsh", 4 ) &&
284             strncmp( psz_mux, "asfh", 4 ) )
285         {
286             char *p = strchr( psz_mux,'{' );
287
288             msg_Warn( p_stream, "fixing to mmsh/asfh" );
289             if( p )
290             {
291                 /* -> a little memleak but ... */
292                 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
293                 sprintf( psz_mux, "asfh%s", p );
294             }
295             else
296             {
297                 psz_mux = strdup("asfh");
298             }
299         }
300         else if( ( !strncmp( psz_access, "rtp", 3 ) ||
301                    !strncmp( psz_access, "udp", 3 ) ) &&
302                  strncmp( psz_mux, "ts", 2 ) )
303         {
304             msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
305         }
306         else if( strncmp( psz_access, "file", 4 ) &&
307                  ( !strncmp( psz_mux, "mov", 3 ) ||
308                    !strncmp( psz_mux, "mp4", 3 ) ) )
309         {
310             msg_Err( p_stream, "mov and mp4 work only with file output" );
311         }
312     }
313
314     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
315
316     /* *** find and open appropriate access module *** */
317     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
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_url );
322         if( psz_access ) free( psz_access );
323         if( psz_mux ) free( psz_mux );
324         return VLC_EGENERIC;
325     }
326     msg_Dbg( p_stream, "access opened" );
327
328     /* *** find and open appropriate mux module *** */
329     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
330     if( p_mux == NULL )
331     {
332         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
333                  psz_access, psz_mux, psz_url );
334
335         sout_AccessOutDelete( p_access );
336         if( psz_access ) free( psz_access );
337         if( psz_mux ) free( psz_mux );
338         return VLC_EGENERIC;
339     }
340     msg_Dbg( p_stream, "mux opened" );
341
342     /* *** Create the SAP Session structure *** */
343     if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
344     {
345         session_descriptor_t *p_session;
346         announce_method_t *p_method = sout_SAPMethod ();
347         vlc_url_t url;
348         const int payload_type = 33;
349
350         static const struct { const char *access; const char *fmt; } fmts[] =
351             {
352                 { "udp",      "udp mpeg" },
353                 { "rtp",      "RTP/AVP %d" },
354                 { "udplite",  "UDPLite/RTP/AVP %d" },
355                 /* Currently unsupported access outputs: */
356                 { "dccp",     "DCCP/RTP/AVP %d" },
357                 { "tcp",      "TCP/RTP/AVP %d" },
358                 /* TLS/DTLS variants (none implemented). */
359                 { "dtls",     "UDP/TLS/RTP/AVP %d" },
360                 { "dtlslite", "UDPLite/TLS/RTP/AVP %d" },
361                 { "dccps",    "DCCP/TLS/RTP/AVP %d" },
362                 { "tls",      "TCP/TLS/RTP/AVP %d" },
363                 /* SRTP (not implemented) */
364                 { "srtp",     "RTP/SAVP %d" },
365                 { "sudplite", "UDPLite/RTP/SAVP %d" },
366                 { "sdccp",    "DCCP/RTP/SAVP %d" },
367                 { "stcp",     "TCP/RTP/SAVP %d" },
368                 { NULL,       NULL }
369             };
370         const char *psz_sdp_fmt = NULL;
371         char *fmt, *src, *dst;
372         int sport, dport;
373
374         for (unsigned i = 0; fmts[i].access != NULL; i++)
375             if (strcasecmp (fmts[i].access, psz_access) == 0)
376             {
377                 psz_sdp_fmt = fmts[i].fmt;
378                 break;
379             }
380
381         src = var_GetNonEmptyString (p_access, "src-addr");
382         dst = var_GetNonEmptyString (p_access, "dst-addr");
383         sport = var_GetInteger (p_access, "src-port");
384         dport = var_GetInteger (p_access, "dst-port");
385         msg_Err (p_stream, "%p, %p, %d, %d", src, dst, sport, dport);
386
387         if ((psz_sdp_fmt == NULL)
388          || (asprintf (&fmt, psz_sdp_fmt, payload_type) == -1))
389             fmt = NULL;
390
391         msg_Dbg( p_stream, "SAP advertized format: %s", fmt);
392         if ((fmt == NULL) || ((src == NULL) && (dst == NULL)))
393         {
394             msg_Err (p_access, "SAP announces not supported for access %s",
395                      psz_access);
396             free (fmt);
397             free (src);
398             free (dst);
399             goto nosap;
400         }
401
402         p_session = sout_AnnounceSessionCreate (VLC_OBJECT (p_stream),
403                                                 SOUT_CFG_PREFIX);
404         sout_SessionSetMedia (VLC_OBJECT (p_stream), p_session, fmt,
405                               src, sport, dst, dport);
406
407         /* Now, parse the URL to extract host and port */
408         vlc_UrlParse( &url, psz_url , 0);
409
410         if( url.psz_host )
411         {
412             if( url.i_port == 0 ) url.i_port = DEFAULT_PORT;
413
414 #if 0
415             p_session->psz_uri = strdup( url.psz_host );
416             p_session->i_port = url.i_port;
417             p_session->b_rtp = strstr( psz_access, "rtp") ? 1 : 0;
418 #endif
419             msg_Info( p_this, "SAP Enabled");
420
421             sout_AnnounceRegister( p_sout, p_session, p_method );
422             p_stream->p_sys->p_session = p_session;
423         }
424         vlc_UrlClean( &url );
425         sout_MethodRelease (p_method);
426     }
427 nosap:
428
429     p_stream->pf_add    = Add;
430     p_stream->pf_del    = Del;
431     p_stream->pf_send   = Send;
432
433     p_stream->p_sys->p_mux = p_mux;
434
435     if( psz_access ) free( psz_access );
436     if( psz_mux ) free( psz_mux );
437     if( psz_url ) free( psz_url );
438
439
440     return VLC_SUCCESS;
441 }
442
443 /*****************************************************************************
444  * Close:
445  *****************************************************************************/
446 static void Close( vlc_object_t * p_this )
447 {
448     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
449     sout_stream_sys_t *p_sys    = p_stream->p_sys;
450     sout_access_out_t *p_access = p_sys->p_mux->p_access;
451
452     if( p_sys->p_session != NULL )
453     {
454         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
455         sout_AnnounceSessionDestroy( p_sys->p_session );
456     }
457
458
459     sout_MuxDelete( p_sys->p_mux );
460     sout_AccessOutDelete( p_access );
461
462     free( p_sys );
463 }
464
465 struct sout_stream_id_t
466 {
467     sout_input_t *p_input;
468 };
469
470
471 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
472 {
473     sout_stream_sys_t *p_sys = p_stream->p_sys;
474     sout_stream_id_t  *id;
475
476     id = malloc( sizeof( sout_stream_id_t ) );
477     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
478     {
479         free( id );
480
481         return NULL;
482     }
483
484     return id;
485 }
486
487 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
488 {
489     sout_stream_sys_t *p_sys = p_stream->p_sys;
490
491     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
492
493     free( id );
494
495     return VLC_SUCCESS;
496 }
497
498 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
499                  block_t *p_buffer )
500 {
501     sout_stream_sys_t *p_sys = p_stream->p_sys;
502
503     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
504
505     return VLC_SUCCESS;
506 }