]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/rtp.c
rtsp: fix authentication variables
[vlc] / modules / stream_out / rtp.c
index 42a9c896444b5258080b235268a86529b5b4ad18..613f6c067445b2df58985212d44389aa3004a53f 100644 (file)
@@ -42,6 +42,8 @@
 #include <vlc_rand.h>
 #ifdef HAVE_SRTP
 # include <srtp.h>
+# include <gcrypt.h>
+# include <vlc_gcrypt.h>
 #endif
 
 #include "rtp.h"
@@ -77,7 +79,7 @@
 #define SDP_TEXT N_("SDP")
 #define SDP_LONGTEXT N_( \
     "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
-    "session will be made available. You must use an url: http://location to " \
+    "session will be made available. You must use a url: http://location to " \
     "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
     "for the SDP to be announced via SAP." )
 #define SAP_TEXT N_("SAP announcing")
@@ -97,7 +99,7 @@
     "that will be announced in the SDP (Session Descriptor)." )
 #define URL_TEXT N_("Session URL")
 #define URL_LONGTEXT N_( \
-    "This allows you to give an URL with more details about the stream " \
+    "This allows you to give a URL with more details about the stream " \
     "(often the website of the streaming organization), that will " \
     "be announced in the SDP (Session Descriptor)." )
 #define EMAIL_TEXT N_("Session email")
@@ -173,6 +175,13 @@ static const char *const ppsz_protocols[] = {
     "negative value or zero disables timeouts. The default is 60 (one " \
     "minute)." )
 
+#define RTSP_USER_TEXT N_("Username")
+#define RTSP_USER_LONGTEXT N_("User name that will be " \
+                              "requested to access the stream." )
+#define RTSP_PASS_TEXT N_("Password")
+#define RTSP_PASS_LONGTEXT N_("Password that will be " \
+                              "requested to access the stream." )
+
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
@@ -248,6 +257,10 @@ vlc_module_begin ()
                  RTSP_HOST_LONGTEXT, true )
     add_integer( "rtsp-timeout", 60, RTSP_TIMEOUT_TEXT,
                  RTSP_TIMEOUT_LONGTEXT, true )
+    add_string( "sout-rtsp-user", "",
+                RTSP_USER_TEXT, RTSP_USER_LONGTEXT, true )
+    add_password( "sout-rtsp-pwd", "",
+                  RTSP_PASS_TEXT, RTSP_PASS_LONGTEXT, true )
 
 vlc_module_end ()
 
@@ -316,7 +329,6 @@ struct sout_stream_sys_t
     uint16_t  i_port_video;
     uint8_t   proto;
     bool      rtcp_mux;
-    int       i_ttl:9;
     bool      b_latm;
 
     /* VoD */
@@ -345,6 +357,7 @@ struct sout_stream_id_t
     sout_stream_t *p_stream;
     /* rtp field */
     uint16_t    i_sequence;
+    bool        b_first_packet;
     bool        b_ts_init;
     uint32_t    i_ts_offset;
     uint8_t     ssrc[4];
@@ -502,14 +515,11 @@ static int Open( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-    p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
-    if( p_sys->i_ttl == -1 )
+    int i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
+    if( i_ttl != -1 )
     {
-        /* Normally, we should let the default hop limit up to the core,
-         * but we have to know it to write our RTSP headers properly,
-         * which is why we ask the core. FIXME: broken when neither
-         * sout-rtp-ttl nor ttl are set. */
-        p_sys->i_ttl = var_InheritInteger( p_stream, "ttl" );
+        var_Create( p_stream, "ttl", VLC_VAR_INTEGER );
+        var_SetInteger( p_stream, "ttl", i_ttl );
     }
 
     p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
@@ -718,7 +728,27 @@ static void SDPHandleUrl( sout_stream_t *p_stream, const char *psz_url )
             goto out;
         }
 
-        p_sys->rtsp = RtspSetup( VLC_OBJECT(p_stream), NULL, &url );
+        if( url.psz_host != NULL && *url.psz_host )
+        {
+            /* msg_Err( p_stream, "\"%s\" RTSP host ignored", url.psz_host );
+            msg_Info( p_stream, "Pass --rtsp-host=%s on the command line "
+                      "instead.", url.psz_host ); */
+
+            var_Create( p_stream, "rtsp-host", VLC_VAR_STRING );
+            var_SetString( p_stream, "rtsp-host", url.psz_host );
+        }
+        /* if( url.i_port != 0 )
+        {
+            msg_Err( p_stream, "\"%u\" RTSP port ignored", url.i_port );
+            msg_Info( p_stream, "Pass --rtsp-port=%u on the command line "
+                      "instead.", url.i_port );
+        } */
+
+        if( url.i_port <= 0 ) url.i_port = 554;
+        var_Create( p_stream, "rtsp-port", VLC_VAR_INTEGER );
+        var_SetInteger( p_stream, "rtsp-port", url.i_port );
+
+        p_sys->rtsp = RtspSetup( VLC_OBJECT(p_stream), NULL, url.psz_path );
         if( p_sys->rtsp == NULL )
             msg_Err( p_stream, "cannot export SDP as RTSP" );
     }
@@ -888,7 +918,8 @@ char *SDPGenerate( sout_stream_t *p_stream, const char *rtsp_url )
                 sdp_AddAttribute( &psz_sdp, "setup", "passive" );
             if( p_sys->proto == IPPROTO_DCCP )
                 sdp_AddAttribute( &psz_sdp, "dccp-service-code",
-                                  "SC:RTP%c", toupper( mime_major[0] ) );
+                                  "SC:RTP%c",
+                                  toupper( (unsigned char)mime_major[0] ) );
         }
     }
 out:
@@ -952,6 +983,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     id->p_fifo = NULL;
     id->listen.fd = NULL;
 
+    id->b_first_packet = true;
     id->i_caching =
         (int64_t)1000 * var_GetInteger( p_stream, SOUT_CFG_PREFIX "caching");
 
@@ -976,7 +1008,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
             id->i_sequence = id->i_seq_sent_next;
         }
         /* vod_init_id() may fail either because the ES wasn't found in
-         * the VoD media, or because that track wasn't SETUP. In the
+         * the VoD media, or because the RTSP session is gone. In the
          * former case, id->rtp_fmt was left untouched. */
         format = (id->rtp_fmt.ptname != NULL);
     }
@@ -994,9 +1026,10 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     }
 
 #ifdef HAVE_SRTP
-    char *key = var_CreateGetNonEmptyString (p_stream, SOUT_CFG_PREFIX"key");
+    char *key = var_GetNonEmptyString (p_stream, SOUT_CFG_PREFIX"key");
     if (key)
     {
+        vlc_gcrypt_init ();
         id->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
                                    SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
         if (id->srtp == NULL)
@@ -1005,7 +1038,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
             goto error;
         }
 
-        char *salt = var_CreateGetNonEmptyString (p_stream, SOUT_CFG_PREFIX"salt");
+        char *salt = var_GetNonEmptyString (p_stream, SOUT_CFG_PREFIX"salt");
         errno = srtp_setkeystring (id->srtp, key, salt ? salt : "");
         free (salt);
         free (key);
@@ -1020,6 +1053,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
     id->i_seq_sent_next = id->i_sequence;
 
+    int mcast_fd = -1;
     if( p_sys->psz_destination != NULL )
     {
         /* Choose the port */
@@ -1094,9 +1128,8 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
             default:
             {
-                int ttl = (p_sys->i_ttl >= 0) ? p_sys->i_ttl : -1;
                 int fd = net_ConnectDgram( p_stream, p_sys->psz_destination,
-                                           i_port, ttl, p_sys->proto );
+                                           i_port, -1, p_sys->proto );
                 if( fd == -1 )
                 {
                     msg_Err( p_stream, "cannot create RTP socket" );
@@ -1107,6 +1140,8 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
                 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
                             sizeof (int));
                 rtp_add_sink( id, fd, p_sys->rtcp_mux, NULL );
+                /* FIXME: test if this is multicast  */
+                mcast_fd = fd;
             }
         }
     }
@@ -1143,11 +1178,8 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
                                           p_sys->i_pts_offset );
 
     if( p_sys->rtsp != NULL )
-        id->rtsp_id = RtspAddId( p_sys->rtsp, id,
-                                 GetDWBE( id->ssrc ),
-                                 id->rtp_fmt.clock_rate,
-                                 p_sys->psz_destination,
-                                 p_sys->i_ttl, id->i_port, id->i_port + 1 );
+        id->rtsp_id = RtspAddId( p_sys->rtsp, id, GetDWBE( id->ssrc ),
+                                 id->rtp_fmt.clock_rate, mcast_fd );
 
     id->p_fifo = block_FifoNew();
     if( unlikely(id->p_fifo == NULL) )
@@ -1244,11 +1276,14 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
 
         /* Send a Vorbis/Theora Packed Configuration packet (RFC 5215 ยง3.1)
          * as the first packet of the stream */
-        if (id->i_sequence == id->i_seq_sent_next
-            && (!strcmp(id->rtp_fmt.ptname, "vorbis")
-                || !strcmp(id->rtp_fmt.ptname, "theora")))
+        if (id->b_first_packet)
+        {
+            id->b_first_packet = false;
+            if (!strcmp(id->rtp_fmt.ptname, "vorbis") ||
+                !strcmp(id->rtp_fmt.ptname, "theora"))
                 rtp_packetize_xiph_config(id, id->rtp_fmt.fmtp,
                                           p_buffer->i_pts);
+        }
 
         if( id->rtp_fmt.pf_packetize( id, p_buffer ) )
             break;
@@ -1275,14 +1310,9 @@ static int SapSetup( sout_stream_t *p_stream )
     }
 
     if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
-    {
-        announce_method_t *p_method = sout_SAPMethod();
         p_sys->p_session = sout_AnnounceRegisterSDP( p_sout,
                                                      p_sys->psz_sdp,
-                                                     p_sys->psz_destination,
-                                                     p_method );
-        sout_MethodRelease( p_method );
-    }
+                                                     p_sys->psz_destination );
 
     return VLC_SUCCESS;
 }
@@ -1322,8 +1352,7 @@ static int HttpSetup( sout_stream_t *p_stream, const vlc_url_t *url)
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
 
-    p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
-                                         url->i_port > 0 ? url->i_port : 80 );
+    p_sys->p_httpd_host = vlc_http_HostNew( VLC_OBJECT(p_stream) );
     if( p_sys->p_httpd_host )
     {
         p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
@@ -1407,11 +1436,14 @@ static void* ThreadSend( void *data )
                 out->i_buffer = len;
         }
         if (out)
-#endif
             mwait (out->i_dts + i_caching);
         vlc_cleanup_pop ();
         if (out == NULL)
             continue;
+#else
+        mwait (out->i_dts + i_caching);
+        vlc_cleanup_pop ();
+#endif
 
         ssize_t len = out->i_buffer;
         int canc = vlc_savecancel ();
@@ -1427,31 +1459,20 @@ static void* ThreadSend( void *data )
 #endif
                 SendRTCP( id->sinkv[i].rtcp, out );
 
-            if( send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 ) >= 0 )
-                continue;
-            switch( net_errno )
+            if( send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 ) == -1
+             && net_errno != EAGAIN && net_errno != EWOULDBLOCK
+             && net_errno != ENOBUFS && net_errno != ENOMEM )
             {
-                /* Soft errors (e.g. ICMP): */
-                case ECONNREFUSED: /* Port unreachable */
-                case ENOPROTOOPT:
-#ifdef EPROTO
-                case EPROTO:       /* Protocol unreachable */
-#endif
-                case EHOSTUNREACH: /* Host unreachable */
-                case ENETUNREACH:  /* Network unreachable */
-                case ENETDOWN:     /* Entire network down */
+                int type;
+                getsockopt( id->sinkv[i].rtp_fd, SOL_SOCKET, SO_TYPE,
+                            &type, &(socklen_t){ sizeof(type) });
+                if( type == SOCK_DGRAM )
+                    /* ICMP soft error: ignore and retry */
                     send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 );
-                /* Transient congestion: */
-                case ENOMEM: /* out of socket buffers */
-                case ENOBUFS:
-                case EAGAIN:
-#if (EAGAIN != EWOULDBLOCK)
-                case EWOULDBLOCK:
-#endif
-                    continue;
+                else
+                    /* Broken connection */
+                    deadv[deadc++] = id->sinkv[i].rtp_fd;
             }
-
-            deadv[deadc++] = id->sinkv[i].rtp_fd;
         }
         id->i_seq_sent_next = ntohs(((uint16_t *) out->p_buffer)[1]) + 1;
         vlc_mutex_unlock( &id->lock_sink );
@@ -1553,7 +1574,7 @@ static int64_t rtp_init_ts( const vod_media_t *p_media,
     uint64_t i_ts_init;
     /* As per RFC 2326, session identifiers are at least 8 bytes long */
     strncpy((char *)&i_ts_init, psz_vod_session, sizeof(uint64_t));
-    i_ts_init ^= (uint64_t) p_media;
+    i_ts_init ^= (uintptr_t)p_media;
     /* Limit the timestamp to 48 bytes, this is enough and allows us
      * to stay away from overflows */
     i_ts_init &= 0xFFFFFFFFFFFF;
@@ -1562,11 +1583,16 @@ static int64_t rtp_init_ts( const vod_media_t *p_media,
 
 /* Return a timestamp corresponding to packets being sent now, and that
  * can be passed to rtp_compute_ts() to get rtptime values for each ES.
- * If the stream output is not started, the initial timestamp that will
- * be used with the first packets is returned instead. */
+ * Also return the NPT corresponding to this timestamp. If the stream
+ * output is not started, the initial timestamp that will be used with
+ * the first packets for NPT=0 is returned instead. */
 int64_t rtp_get_ts( const sout_stream_t *p_stream, const sout_stream_id_t *id,
-                    const vod_media_t *p_media, const char *psz_vod_session )
+                    const vod_media_t *p_media, const char *psz_vod_session,
+                    int64_t *p_npt )
 {
+    if (p_npt != NULL)
+        *p_npt = 0;
+
     if (id != NULL)
         p_stream = id->p_stream;
 
@@ -1586,7 +1612,11 @@ int64_t rtp_get_ts( const sout_stream_t *p_stream, const sout_stream_id_t *id,
     if( now < i_npt_zero )
         return p_sys->i_pts_zero;
 
-    return p_sys->i_pts_zero + (now - i_npt_zero); 
+    int64_t npt = now - i_npt_zero;
+    if (p_npt != NULL)
+        *p_npt = npt;
+
+    return p_sys->i_pts_zero + npt; 
 }
 
 void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
@@ -1764,7 +1794,7 @@ static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
 {
     sout_access_out_t *p_grab;
 
-    p_grab = vlc_object_create( p_stream->p_sout, sizeof( *p_grab ) );
+    p_grab = vlc_object_create( p_stream, sizeof( *p_grab ) );
     if( p_grab == NULL )
         return NULL;
 
@@ -1775,6 +1805,5 @@ static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
     p_grab->p_sys       = (sout_access_out_sys_t *)p_stream;
     p_grab->pf_seek     = NULL;
     p_grab->pf_write    = AccessOutGrabberWrite;
-    vlc_object_attach( p_grab, p_stream );
     return p_grab;
 }