rtsp.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:24k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * rtsp.c: RTSP support for RTP stream output module
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2004 the VideoLAN team
  5.  * Copyright © 2007 Rémi Denis-Courmont
  6.  *
  7.  * $Id: adb5696af5d0d283214979ac9db98c8462e47e11 $
  8.  *
  9.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24.  *****************************************************************************/
  25. /*****************************************************************************
  26.  * Preamble
  27.  *****************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. # include "config.h"
  30. #endif
  31. #include <vlc_common.h>
  32. #include <vlc_sout.h>
  33. #include <vlc_httpd.h>
  34. #include <vlc_url.h>
  35. #include <vlc_network.h>
  36. #include <vlc_rand.h>
  37. #include <assert.h>
  38. #include <errno.h>
  39. #include <stdlib.h>
  40. #include "rtp.h"
  41. typedef struct rtsp_session_t rtsp_session_t;
  42. struct rtsp_stream_t
  43. {
  44.     vlc_mutex_t     lock;
  45.     sout_stream_t  *owner;
  46.     httpd_host_t   *host;
  47.     httpd_url_t    *url;
  48.     char           *psz_path;
  49.     const char     *track_fmt;
  50.     unsigned        port;
  51.     int             sessionc;
  52.     rtsp_session_t **sessionv;
  53. };
  54. static int  RtspCallback( httpd_callback_sys_t *p_args,
  55.                           httpd_client_t *cl, httpd_message_t *answer,
  56.                           const httpd_message_t *query );
  57. static int  RtspCallbackId( httpd_callback_sys_t *p_args,
  58.                             httpd_client_t *cl, httpd_message_t *answer,
  59.                             const httpd_message_t *query );
  60. static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
  61. rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
  62. {
  63.     rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
  64.     if( rtsp == NULL || ( url->i_port > 99999 ) )
  65.     {
  66.         free( rtsp );
  67.         return NULL;
  68.     }
  69.     rtsp->owner = p_stream;
  70.     rtsp->sessionc = 0;
  71.     rtsp->sessionv = NULL;
  72.     rtsp->host = NULL;
  73.     rtsp->url = NULL;
  74.     rtsp->psz_path = NULL;
  75.     vlc_mutex_init( &rtsp->lock );
  76.     rtsp->port = (url->i_port > 0) ? url->i_port : 554;
  77.     rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
  78.     if( rtsp->psz_path == NULL )
  79.         goto error;
  80.     assert( strlen( rtsp->psz_path ) > 0 );
  81.     if( rtsp->psz_path[strlen( rtsp->psz_path ) - 1] == '/' )
  82.         rtsp->track_fmt = "%strackID=%u";
  83.     else
  84.         rtsp->track_fmt = "%s/trackID=%u";
  85.     msg_Dbg( p_stream, "RTSP stream: host %s port %d at %s",
  86.              url->psz_host, rtsp->port, rtsp->psz_path );
  87.     rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
  88.                                 rtsp->port );
  89.     if( rtsp->host == NULL )
  90.         goto error;
  91.     rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
  92.                                     NULL, NULL, NULL );
  93.     if( rtsp->url == NULL )
  94.         goto error;
  95.     httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
  96.     httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP,    RtspCallback, (void*)rtsp );
  97.     httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY,     RtspCallback, (void*)rtsp );
  98.     httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)rtsp );
  99.     httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
  100.                     (void*)rtsp );
  101.     httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
  102.     return rtsp;
  103. error:
  104.     RtspUnsetup( rtsp );
  105.     return NULL;
  106. }
  107. void RtspUnsetup( rtsp_stream_t *rtsp )
  108. {
  109.     if( rtsp->url )
  110.         httpd_UrlDelete( rtsp->url );
  111.     while( rtsp->sessionc > 0 )
  112.         RtspClientDel( rtsp, rtsp->sessionv[0] );
  113.     if( rtsp->host )
  114.         httpd_HostDelete( rtsp->host );
  115.     free( rtsp->psz_path );
  116.     vlc_mutex_destroy( &rtsp->lock );
  117.     free( rtsp );
  118. }
  119. struct rtsp_stream_id_t
  120. {
  121.     rtsp_stream_t    *stream;
  122.     sout_stream_id_t *sout_id;
  123.     httpd_url_t      *url;
  124.     const char       *dst;
  125.     int               ttl;
  126.     uint32_t          ssrc;
  127.     uint16_t          loport, hiport;
  128. };
  129. typedef struct rtsp_strack_t rtsp_strack_t;
  130. /* For unicast streaming */
  131. struct rtsp_session_t
  132. {
  133.     rtsp_stream_t *stream;
  134.     uint64_t       id;
  135.     /* output (id-access) */
  136.     int            trackc;
  137.     rtsp_strack_t *trackv;
  138. };
  139. /* Unicast session track */
  140. struct rtsp_strack_t
  141. {
  142.     sout_stream_id_t  *id;
  143.     int                fd;
  144.     bool         playing;
  145. };
  146. rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
  147.                              unsigned num, uint32_t ssrc,
  148.                              /* Multicast stuff - TODO: cleanup */
  149.                              const char *dst, int ttl,
  150.                              unsigned loport, unsigned hiport )
  151. {
  152.     char urlbuf[sizeof( "/trackID=123" ) + strlen( rtsp->psz_path )];
  153.     rtsp_stream_id_t *id = malloc( sizeof( *id ) );
  154.     httpd_url_t *url;
  155.     if( id == NULL )
  156.         return NULL;
  157.     id->stream = rtsp;
  158.     id->sout_id = sid;
  159.     id->ssrc = ssrc;
  160.     /* TODO: can we assume that this need not be strdup'd? */
  161.     id->dst = dst;
  162.     if( id->dst != NULL )
  163.     {
  164.         id->ttl = ttl;
  165.         id->loport = loport;
  166.         id->hiport = hiport;
  167.     }
  168.     /* FIXME: num screws up if any ES has been removed and re-added */
  169.     snprintf( urlbuf, sizeof( urlbuf ), rtsp->track_fmt, rtsp->psz_path,
  170.               num );
  171.     msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
  172.     url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
  173.     if( url == NULL )
  174.     {
  175.         free( id );
  176.         return NULL;
  177.     }
  178.     httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
  179.     httpd_UrlCatch( url, HTTPD_MSG_SETUP,    RtspCallbackId, (void *)id );
  180.     httpd_UrlCatch( url, HTTPD_MSG_PLAY,     RtspCallbackId, (void *)id );
  181.     httpd_UrlCatch( url, HTTPD_MSG_PAUSE,    RtspCallbackId, (void *)id );
  182.     httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
  183.     httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
  184.     return id;
  185. }
  186. void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
  187. {
  188.     httpd_UrlDelete( id->url );
  189.     vlc_mutex_lock( &rtsp->lock );
  190.     for( int i = 0; i < rtsp->sessionc; i++ )
  191.     {
  192.         rtsp_session_t *ses = rtsp->sessionv[i];
  193.         for( int j = 0; j < ses->trackc; j++ )
  194.         {
  195.             if( ses->trackv[j].id == id->sout_id )
  196.             {
  197.                 rtsp_strack_t *tr = ses->trackv + j;
  198.                 rtp_del_sink( tr->id, tr->fd );
  199.                 REMOVE_ELEM( ses->trackv, ses->trackc, j );
  200.             }
  201.         }
  202.     }
  203.     vlc_mutex_unlock( &rtsp->lock );
  204.     free( id );
  205. }
  206. /** rtsp must be locked */
  207. static
  208. rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
  209. {
  210.     rtsp_session_t *s = malloc( sizeof( *s ) );
  211.     if( s == NULL )
  212.         return NULL;
  213.     s->stream = rtsp;
  214.     vlc_rand_bytes (&s->id, sizeof (s->id));
  215.     s->trackc = 0;
  216.     s->trackv = NULL;
  217.     TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
  218.     return s;
  219. }
  220. /** rtsp must be locked */
  221. static
  222. rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
  223. {
  224.     char *end;
  225.     uint64_t id;
  226.     int i;
  227.     if( name == NULL )
  228.         return NULL;
  229.     errno = 0;
  230.     id = strtoull( name, &end, 0x10 );
  231.     if( errno || *end )
  232.         return NULL;
  233.     /* FIXME: use a hash/dictionary */
  234.     for( i = 0; i < rtsp->sessionc; i++ )
  235.     {
  236.         if( rtsp->sessionv[i]->id == id )
  237.             return rtsp->sessionv[i];
  238.     }
  239.     return NULL;
  240. }
  241. /** rtsp must be locked */
  242. static
  243. void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
  244. {
  245.     int i;
  246.     TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
  247.     for( i = 0; i < session->trackc; i++ )
  248.         rtp_del_sink( session->trackv[i].id, session->trackv[i].fd );
  249.     free( session->trackv );
  250.     free( session );
  251. }
  252. /** Finds the next transport choice */
  253. static inline const char *transport_next( const char *str )
  254. {
  255.     /* Looks for comma */
  256.     str = strchr( str, ',' );
  257.     if( str == NULL )
  258.         return NULL; /* No more transport options */
  259.     str++; /* skips comma */
  260.     while( strchr( "rnt ", *str ) )
  261.         str++;
  262.     return (*str) ? str : NULL;
  263. }
  264. /** Finds the next transport parameter */
  265. static inline const char *parameter_next( const char *str )
  266. {
  267.     while( strchr( ",;", *str ) == NULL )
  268.         str++;
  269.     return (*str == ';') ? (str + 1) : NULL;
  270. }
  271. /** RTSP requests handler
  272.  * @param id selected track for non-aggregate URLs,
  273.  *           NULL for aggregate URLs
  274.  */
  275. static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
  276.                         httpd_client_t *cl,
  277.                         httpd_message_t *answer,
  278.                         const httpd_message_t *query )
  279. {
  280.     sout_stream_t *p_stream = rtsp->owner;
  281.     char psz_sesbuf[17];
  282.     const char *psz_session = NULL, *psz;
  283.     char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
  284.                   + strlen( rtsp->psz_path )];
  285.     time_t now;
  286.     time (&now);
  287.     if( answer == NULL || query == NULL || cl == NULL )
  288.         return VLC_SUCCESS;
  289.     else
  290.     {
  291.         /* Build self-referential control URL */
  292.         char ip[NI_MAXNUMERICHOST], *ptr;
  293.         httpd_ServerIP( cl, ip );
  294.         ptr = strchr( ip, '%' );
  295.         if( ptr != NULL )
  296.             *ptr = '';
  297.         if( strchr( ip, ':' ) != NULL )
  298.             sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
  299.                      rtsp->psz_path );
  300.         else
  301.             sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
  302.                      rtsp->psz_path );
  303.     }
  304.     /* */
  305.     answer->i_proto = HTTPD_PROTO_RTSP;
  306.     answer->i_version= 0;
  307.     answer->i_type   = HTTPD_MSG_ANSWER;
  308.     answer->i_body = 0;
  309.     answer->p_body = NULL;
  310.     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
  311.     /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
  312.     struct tm ut;
  313.     if (gmtime_r (&now, &ut) != NULL)
  314.     {   /* RFC1123 format, GMT is mandatory */
  315.         static const char wdays[7][4] = {
  316.             "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  317.         static const char mons[12][4] = {
  318.             "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  319.             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  320.         httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
  321.                       wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
  322.                       1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
  323.     }
  324.     if( query->i_proto != HTTPD_PROTO_RTSP )
  325.     {
  326.         answer->i_status = 505;
  327.     }
  328.     else
  329.     if( httpd_MsgGet( query, "Require" ) != NULL )
  330.     {
  331.         answer->i_status = 551;
  332.         httpd_MsgAdd( answer, "Unsupported", "%s",
  333.                       httpd_MsgGet( query, "Require" ) );
  334.     }
  335.     else
  336.     switch( query->i_type )
  337.     {
  338.         case HTTPD_MSG_DESCRIBE:
  339.         {   /* Aggregate-only */
  340.             if( id != NULL )
  341.             {
  342.                 answer->i_status = 460;
  343.                 break;
  344.             }
  345.             answer->i_status = 200;
  346.             httpd_MsgAdd( answer, "Content-Type",  "%s", "application/sdp" );
  347.             httpd_MsgAdd( answer, "Content-Base",  "%s", control );
  348.             answer->p_body = (uint8_t *)SDPGenerate( rtsp->owner, control );
  349.             if( answer->p_body != NULL )
  350.                 answer->i_body = strlen( (char *)answer->p_body );
  351.             else
  352.                 answer->i_status = 500;
  353.             break;
  354.         }
  355.         case HTTPD_MSG_SETUP:
  356.             /* Non-aggregate-only */
  357.             if( id == NULL )
  358.             {
  359.                 answer->i_status = 459;
  360.                 break;
  361.             }
  362.             psz_session = httpd_MsgGet( query, "Session" );
  363.             answer->i_status = 461;
  364.             for( const char *tpt = httpd_MsgGet( query, "Transport" );
  365.                  tpt != NULL;
  366.                  tpt = transport_next( tpt ) )
  367.             {
  368.                 bool b_multicast = true, b_unsupp = false;
  369.                 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
  370.                 /* Check transport protocol. */
  371.                 /* Currently, we only support RTP/AVP over UDP */
  372.                 if( strncmp( tpt, "RTP/AVP", 7 ) )
  373.                     continue;
  374.                 tpt += 7;
  375.                 if( strncmp( tpt, "/UDP", 4 ) == 0 )
  376.                     tpt += 4;
  377.                 if( strchr( ";,", *tpt ) == NULL )
  378.                     continue;
  379.                 /* Parse transport options */
  380.                 for( const char *opt = parameter_next( tpt );
  381.                      opt != NULL;
  382.                      opt = parameter_next( opt ) )
  383.                 {
  384.                     if( strncmp( opt, "multicast", 9 ) == 0)
  385.                         b_multicast = true;
  386.                     else
  387.                     if( strncmp( opt, "unicast", 7 ) == 0 )
  388.                         b_multicast = false;
  389.                     else
  390.                     if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
  391.                                 == 2 )
  392.                         ;
  393.                     else
  394.                     if( strncmp( opt, "mode=", 5 ) == 0 )
  395.                     {
  396.                         if( strncasecmp( opt + 5, "play", 4 )
  397.                          && strncasecmp( opt + 5, ""PLAY"", 6 ) )
  398.                         {
  399.                             /* Not playing?! */
  400.                             b_unsupp = true;
  401.                             break;
  402.                         }
  403.                     }
  404.                     else
  405.                     if( strncmp( opt,"destination=", 12 ) == 0 )
  406.                     {
  407.                         answer->i_status = 403;
  408.                         b_unsupp = true;
  409.                     }
  410.                     else
  411.                     {
  412.                     /*
  413.                      * Every other option is unsupported:
  414.                      *
  415.                      * "source" and "append" are invalid (server-only);
  416.                      * "ssrc" also (as clarified per RFC2326bis).
  417.                      *
  418.                      * For multicast, "port", "layers", "ttl" are set by the
  419.                      * stream output configuration.
  420.                      *
  421.                      * For unicast, we want to decide "server_port" values.
  422.                      *
  423.                      * "interleaved" is not implemented.
  424.                      */
  425.                         b_unsupp = true;
  426.                         break;
  427.                     }
  428.                 }
  429.                 if( b_unsupp )
  430.                     continue;
  431.                 if( b_multicast )
  432.                 {
  433.                     const char *dst = id->dst;
  434.                     if( dst == NULL )
  435.                         continue;
  436.                     if( psz_session == NULL )
  437.                     {
  438.                         /* Create a dummy session ID */
  439.                         snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%d",
  440.                                   rand() );
  441.                         psz_session = psz_sesbuf;
  442.                     }
  443.                     answer->i_status = 200;
  444.                     httpd_MsgAdd( answer, "Transport",
  445.                                   "RTP/AVP/UDP;destination=%s;port=%u-%u;"
  446.                                   "ttl=%d;mode=play",
  447.                                   dst, id->loport, id->hiport,
  448.                                   ( id->ttl > 0 ) ? id->ttl : 1 );
  449.                 }
  450.                 else
  451.                 {
  452.                     char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
  453.                     rtsp_session_t *ses = NULL;
  454.                     rtsp_strack_t track = { id->sout_id, -1, false };
  455.                     int sport;
  456.                     if( httpd_ClientIP( cl, ip ) == NULL )
  457.                     {
  458.                         answer->i_status = 500;
  459.                         continue;
  460.                     }
  461.                     track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
  462.                                                  IPPROTO_UDP );
  463.                     if( track.fd == -1 )
  464.                     {
  465.                         msg_Err( p_stream,
  466.                                  "cannot create RTP socket for %s port %u",
  467.                                  ip, loport );
  468.                         answer->i_status = 500;
  469.                         continue;
  470.                     }
  471.                     net_GetSockAddress( track.fd, src, &sport );
  472.                     vlc_mutex_lock( &rtsp->lock );
  473.                     if( psz_session == NULL )
  474.                     {
  475.                         ses = RtspClientNew( rtsp );
  476.                         snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
  477.                                   ses->id );
  478.                         psz_session = psz_sesbuf;
  479.                     }
  480.                     else
  481.                     {
  482.                         /* FIXME: we probably need to remove an access out,
  483.                          * if there is already one for the same ID */
  484.                         ses = RtspClientGet( rtsp, psz_session );
  485.                         if( ses == NULL )
  486.                         {
  487.                             answer->i_status = 454;
  488.                             vlc_mutex_unlock( &rtsp->lock );
  489.                             continue;
  490.                         }
  491.                     }
  492.                     INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
  493.                                  track );
  494.                     vlc_mutex_unlock( &rtsp->lock );
  495.                     httpd_ServerIP( cl, ip );
  496.                     if( strcmp( src, ip ) )
  497.                     {
  498.                         /* Specify source IP if it is different from the RTSP
  499.                          * control connection server address */
  500.                         char *ptr = strchr( src, '%' );
  501.                         if( ptr != NULL ) *ptr = ''; /* remove scope ID */
  502.                         httpd_MsgAdd( answer, "Transport",
  503.                                       "RTP/AVP/UDP;unicast;source=%s;"
  504.                                       "client_port=%u-%u;server_port=%u-%u;"
  505.                                       "ssrc=%08X;mode=play",
  506.                                       src, loport, loport + 1, sport,
  507.                                       sport + 1, id->ssrc );
  508.                     }
  509.                     else
  510.                     {
  511.                         httpd_MsgAdd( answer, "Transport",
  512.                                       "RTP/AVP/UDP;unicast;"
  513.                                       "client_port=%u-%u;server_port=%u-%u;"
  514.                                       "ssrc=%08X;mode=play",
  515.                                       loport, loport + 1, sport, sport + 1,
  516.                                       id->ssrc );
  517.                     }
  518.                     answer->i_status = 200;
  519.                 }
  520.                 break;
  521.             }
  522.             break;
  523.         case HTTPD_MSG_PLAY:
  524.         {
  525.             rtsp_session_t *ses;
  526.             answer->i_status = 200;
  527.             psz_session = httpd_MsgGet( query, "Session" );
  528.             const char *range = httpd_MsgGet (query, "Range");
  529.             if (range && strncmp (range, "npt=", 4))
  530.             {
  531.                 answer->i_status = 501;
  532.                 break;
  533.             }
  534.             vlc_mutex_lock( &rtsp->lock );
  535.             ses = RtspClientGet( rtsp, psz_session );
  536.             if( ses != NULL )
  537.             {
  538.                 /* FIXME: we really need to limit the number of tracks... */
  539.                 char info[ses->trackc * ( strlen( control )
  540.                               + sizeof("url=/trackID=123;seq=65535;"
  541.                                        "rtptime=4294967295, ") ) + 1];
  542.                 size_t infolen = 0;
  543.                 for( int i = 0; i < ses->trackc; i++ )
  544.                 {
  545.                     rtsp_strack_t *tr = ses->trackv + i;
  546.                     if( ( id == NULL ) || ( tr->id == id->sout_id ) )
  547.                     {
  548.                         if( !tr->playing )
  549.                         {
  550.                             tr->playing = true;
  551.                             rtp_add_sink( tr->id, tr->fd, false );
  552.                         }
  553.                         /* This is racy, as the first packets may have
  554.                          * already been sent before we fetch this info:
  555.                          * these extra packets might confuse the client. */
  556.                         infolen += sprintf( info + infolen,
  557.                                     "url=%s/trackID=%u;seq=%u;rtptime=%u, ",
  558.                                             control,
  559.                                             rtp_get_num( tr->id ),
  560.                                             rtp_get_seq( tr->id ),
  561.                                             rtp_get_ts( tr->id ) );
  562.                     }
  563.                 }
  564.                 if( infolen > 0 )
  565.                 {
  566.                     info[infolen - 2] = ''; /* remove trailing ", " */
  567.                     httpd_MsgAdd( answer, "RTP-Info", "%s", info );
  568.                 }
  569.             }
  570.             vlc_mutex_unlock( &rtsp->lock );
  571.             if( httpd_MsgGet( query, "Scale" ) != NULL )
  572.                 httpd_MsgAdd( answer, "Scale", "1." );
  573.             break;
  574.         }
  575.         case HTTPD_MSG_PAUSE:
  576.             answer->i_status = 405;
  577.             httpd_MsgAdd( answer, "Allow",
  578.                           "%s, TEARDOWN, PLAY, GET_PARAMETER",
  579.                           ( id != NULL ) ? "SETUP" : "DESCRIBE" );
  580.             break;
  581.         case HTTPD_MSG_GETPARAMETER:
  582.             if( query->i_body > 0 )
  583.             {
  584.                 answer->i_status = 451;
  585.                 break;
  586.             }
  587.             psz_session = httpd_MsgGet( query, "Session" );
  588.             answer->i_status = 200;
  589.             break;
  590.         case HTTPD_MSG_TEARDOWN:
  591.         {
  592.             rtsp_session_t *ses;
  593.             answer->i_status = 200;
  594.             psz_session = httpd_MsgGet( query, "Session" );
  595.             vlc_mutex_lock( &rtsp->lock );
  596.             ses = RtspClientGet( rtsp, psz_session );
  597.             if( ses != NULL )
  598.             {
  599.                 if( id == NULL ) /* Delete the entire session */
  600.                     RtspClientDel( rtsp, ses );
  601.                 else /* Delete one track from the session */
  602.                 for( int i = 0; i < ses->trackc; i++ )
  603.                 {
  604.                     if( ses->trackv[i].id == id->sout_id )
  605.                     {
  606.                         rtp_del_sink( id->sout_id, ses->trackv[i].fd );
  607.                         REMOVE_ELEM( ses->trackv, ses->trackc, i );
  608.                     }
  609.                 }
  610.             }
  611.             vlc_mutex_unlock( &rtsp->lock );
  612.             break;
  613.         }
  614.         default:
  615.             return VLC_EGENERIC;
  616.     }
  617.     if( psz_session )
  618.         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
  619.     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
  620.     httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
  621.     psz = httpd_MsgGet( query, "Cseq" );
  622.     if( psz != NULL )
  623.         httpd_MsgAdd( answer, "Cseq", "%s", psz );
  624.     psz = httpd_MsgGet( query, "Timestamp" );
  625.     if( psz != NULL )
  626.         httpd_MsgAdd( answer, "Timestamp", "%s", psz );
  627.     return VLC_SUCCESS;
  628. }
  629. /** Aggregate RTSP callback */
  630. static int RtspCallback( httpd_callback_sys_t *p_args,
  631.                          httpd_client_t *cl,
  632.                          httpd_message_t *answer,
  633.                          const httpd_message_t *query )
  634. {
  635.     return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
  636. }
  637. /** Non-aggregate RTSP callback */
  638. static int RtspCallbackId( httpd_callback_sys_t *p_args,
  639.                            httpd_client_t *cl,
  640.                            httpd_message_t *answer,
  641.                            const httpd_message_t *query )
  642. {
  643.     rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
  644.     return RtspHandler( id->stream, id, cl, answer, query );
  645. }