]> git.sesse.net Git - vlc/blobdiff - modules/misc/rtsp.c
Fix a bunch of gcc warnings
[vlc] / modules / misc / rtsp.c
index 124340b68083a728853dcfe64ef903bb966b2f4c..0bd6638ae4f7991544d560fdfbe3cf4833589036 100644 (file)
@@ -25,6 +25,7 @@
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+#define _GNU_SOURCE
 #include <stdlib.h>
 
 #include <errno.h>
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-#define HOST_TEXT N_( "Host address" )
+#define HOST_TEXT N_( "RTSP host address" )
+/// \bug [String] extra space
 #define HOST_LONGTEXT N_( \
-    "You can set the address, port and path the rtsp interface will bind to." \
-    "\nSyntax is address:port/path. Default is to bind to any address "\
-    "on port 554, with no path." )
+    "This defines the address, port and path the RTSP VOD server will listen " \
+    "on.\nSyntax is address:port/path. The default is to listen on all "\
+    "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
+    "only on the local interface, use \"localhost\" as address." )
 
 #define THROTLE_TEXT N_( "Maximum number of connections" )
-#define THROTLE_LONGTEXT N_( "Limit the number of connections " \
-    "to a maximum. (0 = unlimited, N = maximum clients)" )
+#define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
+    "that can connect to the RTSP VOD. 0 means no limit."  )
+
+#define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
 
 vlc_module_begin();
     set_shortname( _("RTSP VoD" ) );
@@ -64,7 +69,9 @@ vlc_module_begin();
     set_callbacks( Open, Close );
     add_shortcut( "rtsp" );
     add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
-    add_integer( "rtsp-throtle-users", 0, NULL, THROTLE_TEXT, THROTLE_LONGTEXT, VLC_TRUE );
+    add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
+    add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
+                                           THROTLE_LONGTEXT, VLC_TRUE );
 vlc_module_end();
 
 /*****************************************************************************
@@ -160,9 +167,11 @@ struct vod_sys_t
     httpd_host_t *p_rtsp_host;
     char *psz_path;
     int i_port;
-    int i_throtle_users;
+    int i_throttle_users;
     int i_connections;
 
+    char *psz_raw_mux;
+
     /* List of media */
     int i_media;
     vod_media_t **media;
@@ -217,16 +226,19 @@ static int Open( vlc_object_t *p_this )
     if( !p_sys ) goto error;
     p_sys->p_rtsp_host = 0;
 
-    var_Create( p_this, "rtsp-throtle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    p_sys->i_throtle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
-    msg_Dbg( p_this, "Allowing up to %d connections", p_sys->i_throtle_users );
+    var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
+    p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
+    msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
     p_sys->i_connections = 0;
 
+    var_Create( p_this, "rtsp-raw-mux", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    p_sys->psz_raw_mux = var_GetString( p_this, "rtsp-raw-mux" );
+
     p_sys->p_rtsp_host =
         httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
     if( !p_sys->p_rtsp_host )
     {
-        msg_Err( p_vod, "cannot create http server (%s:%i)",
+        msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
                  url.psz_host, url.i_port );
         goto error;
     }
@@ -247,6 +259,7 @@ static int Open( vlc_object_t *p_this )
 
 error:
     if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
+    if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
     if( p_sys ) free( p_sys );
     vlc_UrlClean( &url );
 
@@ -262,10 +275,12 @@ static void Close( vlc_object_t * p_this )
     vod_sys_t *p_sys = p_vod->p_sys;
 
     httpd_HostDelete( p_sys->p_rtsp_host );
-    var_Destroy( p_this, "rtsp-throtle-users" );
+    var_Destroy( p_this, "rtsp-throttle-users" );
+    var_Destroy( p_this, "rtsp-raw-mux" );
 
     /* TODO delete medias */
     free( p_sys->psz_path );
+    free( p_sys->psz_raw_mux );
     free( p_sys );
 }
 
@@ -298,13 +313,13 @@ static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
 
     if( !p_media->p_rtsp_url )
     {
-        msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
+        msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
         free( p_media->psz_rtsp_path );
         free( p_media );
         return NULL;
     }
 
-    msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
+    msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
 
     asprintf( &p_media->psz_rtsp_control_v4,
                "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
@@ -321,6 +336,8 @@ static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
                     RtspCallback, (void*)p_media );
     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
                     RtspCallback, (void*)p_media );
+    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
+                    RtspCallback, (void*)p_media );
     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
                     RtspCallback, (void*)p_media );
 
@@ -432,6 +449,11 @@ static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
             p_es->i_payload_type = p_media->i_payload_type++;
             p_es->psz_rtpmap = strdup( "H263-1998/90000" );
             break;
+        case VLC_FOURCC( 'h', '2', '6', '4' ):
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = strdup( "H264/90000" );
+            p_es->psz_fmtp = strdup( "packetization-mode=1" );
+            break;
         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
             p_es->i_payload_type = p_media->i_payload_type++;
             p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
@@ -497,7 +519,7 @@ static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
 
     if( !p_es->p_rtsp_url )
     {
-        msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
+        msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
         free( psz_urlc );
         free( p_es );
         return VLC_EGENERIC;
@@ -601,7 +623,7 @@ static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
 
     p_media->p_vod->p_sys->i_connections++;
     msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
-             psz_session, p_media->p_vod->p_sys->i_throtle_users );
+             psz_session, p_media->p_vod->p_sys->i_throttle_users );
 
     return p_rtsp;
 }
@@ -625,7 +647,7 @@ static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
 {
     p_media->p_vod->p_sys->i_connections--;
     msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
-             p_rtsp->psz_session, p_media->p_vod->p_sys->i_throtle_users );
+             p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
 
     while( p_rtsp->i_es-- )
     {
@@ -649,8 +671,10 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
     char *psz_transport = NULL;
     char *psz_playnow = NULL; /* support option: x-playNow */
     char *psz_session = NULL;
+    char *psz_cseq = NULL;
     rtsp_client_t *p_rtsp;
     int i_port = 0;
+    int i_cseq = 0;
 
     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
 
@@ -681,6 +705,7 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
                 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
                     strstr( psz_transport, "RAW/RAW/UDP" ) )
                 {
+                    p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
                     p_media->b_raw = VLC_TRUE;
                 }
 
@@ -699,8 +724,8 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
                 psz_session = httpd_MsgGet( query, "Session" );
                 if( !psz_session || !*psz_session )
                 {
-                    if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
-                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
+                    if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
+                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
                     {
                         answer->i_status = 503;
                         answer->psz_status = strdup( "Too many connections" );
@@ -754,7 +779,8 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
                 answer->p_body = NULL;
             }
 
-            if( !psz_playnow || !*psz_playnow )
+            /* Intentional fall-through on x-playNow option in RTSP request */
+            if( !psz_playnow )
                 break;
         }
 
@@ -779,6 +805,61 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
             p_rtsp = RtspClientGet( p_media, psz_session );
             if( !p_rtsp ) break;
 
+            if( p_rtsp->b_playing )
+            {
+                char *psz_position = httpd_MsgGet( query, "Range" );
+                char *psz_scale = httpd_MsgGet( query, "Scale" );
+                if( psz_position ) psz_position = strstr( psz_position, "npt=" );
+                if( psz_position && !psz_scale )
+                {
+                    double f_pos;
+                    char *end;
+
+                    msg_Dbg( p_vod, "seeking request: %s", psz_position );
+                    psz_position += 4;
+                    /* FIXME: npt= is not necessarily formatted as a float */
+                    f_pos = us_strtod( psz_position, &end );
+                    if( end > psz_position )
+                    {
+                        f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
+                        vod_MediaControl( p_vod, p_media, psz_session,
+                                      VOD_MEDIA_SEEK, f_pos );
+                    }
+                    break;
+                }
+                if( psz_scale )
+                {
+                    double f_scale = 0.0;
+                    char *end;
+
+                    f_scale = us_strtod( psz_scale, &end );
+                    if( end > psz_scale )
+                    {
+                        f_scale = (f_scale * 30.0);
+                        if( psz_scale[0] == '-' ) /* rewind */
+                        {
+                            msg_Dbg( p_vod, "rewind request: %s", psz_scale );
+                            vod_MediaControl( p_vod, p_media, psz_session,
+                                                VOD_MEDIA_REWIND, f_scale );
+                        }
+                        else if(psz_scale[0] != '1' ) /* fast-forward */
+                        {
+                            msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
+                            vod_MediaControl( p_vod, p_media, psz_session,
+                                                VOD_MEDIA_FORWARD, f_scale );
+                        }
+
+                        if( p_rtsp->b_paused == VLC_TRUE )
+                        {
+                            p_rtsp->b_paused = VLC_FALSE;
+                            vod_MediaControl( p_vod, p_media, psz_session,
+                                                VOD_MEDIA_PAUSE, psz_output );
+                        }
+                    }
+                    break;
+                }
+            }
+
             if( p_rtsp->b_playing && p_rtsp->b_paused )
             {
                 vod_MediaControl( p_vod, p_media, psz_session,
@@ -883,14 +964,22 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
             RtspClientDel( p_media, p_rtsp );
             break;
 
+        case HTTPD_MSG_GETPARAMETER:
+            answer->i_status = 200;
+            answer->psz_status = strdup( "OK" );
+            answer->i_body = 0;
+            answer->p_body = NULL;
+            break;
+
         default:
             return VLC_EGENERIC;
     }
 
     httpd_MsgAdd( answer, "Server", "VLC Server" );
     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
-    httpd_MsgAdd( answer, "Cseq", "%d",
-                  atoi( httpd_MsgGet( query, "Cseq" ) ) );
+    psz_cseq = httpd_MsgGet( query, "Cseq" );
+    psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
+    httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
 
     if( psz_session )
@@ -912,6 +1001,8 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
     char *psz_playnow = NULL; /* support option: x-playNow */
     char *psz_session = NULL;
     char *psz_position = NULL;
+    char *psz_cseq = NULL;
+    int i_cseq = 0;
     int i;
 
     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
@@ -956,8 +1047,8 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
                 psz_session = httpd_MsgGet( query, "Session" );
                 if( !psz_session || !*psz_session )
                 {
-                    if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
-                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
+                    if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
+                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
                     {
                         answer->i_status = 503;
                         answer->psz_status = strdup( "Too many connections" );
@@ -1019,7 +1110,8 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
                 answer->p_body = NULL;
             }
 
-            if( !psz_playnow || !*psz_playnow )
+            /* Intentional fall-through on x-playNow option in RTSP request */
+            if( !psz_playnow )
                 break;
 
         case HTTPD_MSG_PLAY:
@@ -1114,8 +1206,12 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
 
     httpd_MsgAdd( answer, "Server", "VLC Server" );
     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
-    httpd_MsgAdd( answer, "Cseq", "%d",
-                  atoi( httpd_MsgGet( query, "Cseq" ) ) );
+    psz_cseq = httpd_MsgGet( query, "Cseq" );
+    if (psz_cseq)
+        i_cseq = atoi( psz_cseq );
+    else
+        i_cseq = 0;
+    httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
 
     if( psz_session )
@@ -1150,9 +1246,9 @@ static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
+        sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
         sizeof( "t=0 0\r\n" ) + /* FIXME */
         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
-        sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
         sizeof( "a=range:npt=0-1000000000.000\r\n" );
 
     psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
@@ -1188,15 +1284,14 @@ static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
     if( *p_media->psz_session_email )
         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
 
+    p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
 
-    p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
-
     if( p_media->i_length > 0 )
     {
         lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
-        p += sprintf( p, "a=range:npt=0-"I64Fd".%03u\r\n", d.quot,
+        p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
                       (unsigned)d.rem );
     }