1 /*****************************************************************************
2 * rtsp.c: RTSP support for RTP stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004, 2010 VLC authors and VideoLAN
5 * Copyright © 2007 Rémi Denis-Courmont
9 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
37 #include <vlc_httpd.h>
39 #include <vlc_charset.h>
41 #include <vlc_network.h>
57 typedef struct rtsp_session_t rtsp_session_t
;
63 vod_media_t
*vod_media
;
70 rtsp_session_t
**sessionv
;
77 static int RtspCallback( httpd_callback_sys_t
*p_args
,
78 httpd_client_t
*cl
, httpd_message_t
*answer
,
79 const httpd_message_t
*query
);
80 static int RtspCallbackId( httpd_callback_sys_t
*p_args
,
81 httpd_client_t
*cl
, httpd_message_t
*answer
,
82 const httpd_message_t
*query
);
83 static void RtspClientDel( rtsp_stream_t
*rtsp
, rtsp_session_t
*session
);
85 static void RtspTimeOut( void *data
);
87 rtsp_stream_t
*RtspSetup( vlc_object_t
*owner
, vod_media_t
*media
,
90 rtsp_stream_t
*rtsp
= calloc( 1, sizeof( *rtsp
) );
92 if( unlikely(rtsp
== NULL
) )
96 rtsp
->vod_media
= media
;
97 vlc_mutex_init( &rtsp
->lock
);
99 rtsp
->timeout
= var_InheritInteger(owner
, "rtsp-timeout");
100 if (rtsp
->timeout
> 0)
102 if (vlc_timer_create(&rtsp
->timer
, RtspTimeOut
, rtsp
))
106 rtsp
->psz_path
= strdup( (path
!= NULL
) ? path
: "/" );
107 if( rtsp
->psz_path
== NULL
)
110 msg_Dbg( owner
, "RTSP stream at %s", rtsp
->psz_path
);
112 rtsp
->host
= vlc_rtsp_HostNew( VLC_OBJECT(owner
) );
113 if( rtsp
->host
== NULL
)
116 char *user
= var_InheritString(owner
, "sout-rtsp-user");
117 char *pwd
= var_InheritString(owner
, "sout-rtsp-pwd");
119 rtsp
->url
= httpd_UrlNew( rtsp
->host
, rtsp
->psz_path
, user
, pwd
);
122 if( rtsp
->url
== NULL
)
125 httpd_UrlCatch( rtsp
->url
, HTTPD_MSG_DESCRIBE
, RtspCallback
, (void*)rtsp
);
126 httpd_UrlCatch( rtsp
->url
, HTTPD_MSG_SETUP
, RtspCallback
, (void*)rtsp
);
127 httpd_UrlCatch( rtsp
->url
, HTTPD_MSG_PLAY
, RtspCallback
, (void*)rtsp
);
128 httpd_UrlCatch( rtsp
->url
, HTTPD_MSG_PAUSE
, RtspCallback
, (void*)rtsp
);
129 httpd_UrlCatch( rtsp
->url
, HTTPD_MSG_GETPARAMETER
, RtspCallback
,
131 httpd_UrlCatch( rtsp
->url
, HTTPD_MSG_TEARDOWN
, RtspCallback
, (void*)rtsp
);
140 void RtspUnsetup( rtsp_stream_t
*rtsp
)
143 httpd_UrlDelete( rtsp
->url
);
146 httpd_HostDelete( rtsp
->host
);
148 while( rtsp
->sessionc
> 0 )
149 RtspClientDel( rtsp
, rtsp
->sessionv
[0] );
151 if (rtsp
->timeout
> 0)
152 vlc_timer_destroy(rtsp
->timer
);
154 free( rtsp
->psz_path
);
155 vlc_mutex_destroy( &rtsp
->lock
);
161 struct rtsp_stream_id_t
163 rtsp_stream_t
*stream
;
164 sout_stream_id_sys_t
*sout_id
;
168 unsigned clock_rate
; /* needed to compute rtptime in RTP-Info */
173 typedef struct rtsp_strack_t rtsp_strack_t
;
175 /* For unicast streaming */
176 struct rtsp_session_t
178 rtsp_stream_t
*stream
;
180 mtime_t last_seen
; /* for timeouts */
182 /* output (id-access) */
184 rtsp_strack_t
*trackv
;
188 /* Unicast session track */
191 rtsp_stream_id_t
*id
;
192 sout_stream_id_sys_t
*sout_id
;
193 int setup_fd
; /* socket created by the SETUP request */
194 int rtp_fd
; /* socket used by the RTP output, when playing */
199 static void RtspTrackClose( rtsp_strack_t
*tr
);
201 #define TRACK_PATH_SIZE (sizeof("/trackID=999") - 1)
203 char *RtspAppendTrackPath( rtsp_stream_id_t
*id
, const char *base
)
205 const char *sep
= strlen( base
) > 0 && base
[strlen( base
) - 1] == '/' ?
209 if( asprintf( &url
, "%s%strackID=%u", base
, sep
, id
->track_id
) == -1 )
215 rtsp_stream_id_t
*RtspAddId( rtsp_stream_t
*rtsp
, sout_stream_id_sys_t
*sid
,
216 uint32_t ssrc
, unsigned clock_rate
,
219 if (rtsp
->track_id
> 999)
221 msg_Err(rtsp
->owner
, "RTSP: too many IDs!");
226 rtsp_stream_id_t
*id
= malloc( sizeof( *id
) );
234 id
->track_id
= rtsp
->track_id
;
236 id
->clock_rate
= clock_rate
;
237 id
->mcast_fd
= mcast_fd
;
239 urlbuf
= RtspAppendTrackPath( id
, rtsp
->psz_path
);
246 msg_Dbg( rtsp
->owner
, "RTSP: adding %s", urlbuf
);
248 char *user
= var_InheritString(rtsp
->owner
, "sout-rtsp-user");
249 char *pwd
= var_InheritString(rtsp
->owner
, "sout-rtsp-pwd");
251 url
= id
->url
= httpd_UrlNew( rtsp
->host
, urlbuf
, user
, pwd
);
262 httpd_UrlCatch( url
, HTTPD_MSG_DESCRIBE
, RtspCallbackId
, (void *)id
);
263 httpd_UrlCatch( url
, HTTPD_MSG_SETUP
, RtspCallbackId
, (void *)id
);
264 httpd_UrlCatch( url
, HTTPD_MSG_PLAY
, RtspCallbackId
, (void *)id
);
265 httpd_UrlCatch( url
, HTTPD_MSG_PAUSE
, RtspCallbackId
, (void *)id
);
266 httpd_UrlCatch( url
, HTTPD_MSG_GETPARAMETER
, RtspCallbackId
, (void *)id
);
267 httpd_UrlCatch( url
, HTTPD_MSG_TEARDOWN
, RtspCallbackId
, (void *)id
);
275 void RtspDelId( rtsp_stream_t
*rtsp
, rtsp_stream_id_t
*id
)
277 httpd_UrlDelete( id
->url
);
279 vlc_mutex_lock( &rtsp
->lock
);
280 for( int i
= 0; i
< rtsp
->sessionc
; i
++ )
282 rtsp_session_t
*ses
= rtsp
->sessionv
[i
];
284 for( int j
= 0; j
< ses
->trackc
; j
++ )
286 if( ses
->trackv
[j
].id
== id
)
288 rtsp_strack_t
*tr
= ses
->trackv
+ j
;
289 RtspTrackClose( tr
);
290 TAB_ERASE(ses
->trackc
, ses
->trackv
, j
);
295 vlc_mutex_unlock( &rtsp
->lock
);
300 /** rtsp must be locked */
301 static void RtspUpdateTimer( rtsp_stream_t
*rtsp
)
303 if (rtsp
->timeout
<= 0)
307 for (int i
= 0; i
< rtsp
->sessionc
; i
++)
309 if (timeout
== 0 || rtsp
->sessionv
[i
]->last_seen
< timeout
)
310 timeout
= rtsp
->sessionv
[i
]->last_seen
;
313 timeout
+= rtsp
->timeout
* CLOCK_FREQ
;
314 vlc_timer_schedule(rtsp
->timer
, true, timeout
, 0);
318 static void RtspTimeOut( void *data
)
320 rtsp_stream_t
*rtsp
= data
;
322 vlc_mutex_lock(&rtsp
->lock
);
323 mtime_t now
= mdate();
324 for (int i
= rtsp
->sessionc
- 1; i
>= 0; i
--)
326 if (rtsp
->sessionv
[i
]->last_seen
+ rtsp
->timeout
* CLOCK_FREQ
< now
)
328 if (rtsp
->vod_media
!= NULL
)
331 snprintf( psz_sesbuf
, sizeof( psz_sesbuf
), "%"PRIx64
,
332 rtsp
->sessionv
[i
]->id
);
333 vod_stop(rtsp
->vod_media
, psz_sesbuf
);
335 RtspClientDel(rtsp
, rtsp
->sessionv
[i
]);
338 RtspUpdateTimer(rtsp
);
339 vlc_mutex_unlock(&rtsp
->lock
);
343 /** rtsp must be locked */
345 rtsp_session_t
*RtspClientNew( rtsp_stream_t
*rtsp
)
347 rtsp_session_t
*s
= malloc( sizeof( *s
) );
352 vlc_rand_bytes (&s
->id
, sizeof (s
->id
));
356 TAB_APPEND( rtsp
->sessionc
, rtsp
->sessionv
, s
);
362 /** rtsp must be locked */
364 rtsp_session_t
*RtspClientGet( rtsp_stream_t
*rtsp
, const char *name
)
374 id
= strtoull( name
, &end
, 0x10 );
378 /* FIXME: use a hash/dictionary */
379 for( i
= 0; i
< rtsp
->sessionc
; i
++ )
381 if( rtsp
->sessionv
[i
]->id
== id
)
382 return rtsp
->sessionv
[i
];
388 /** rtsp must be locked */
390 void RtspClientDel( rtsp_stream_t
*rtsp
, rtsp_session_t
*session
)
393 TAB_REMOVE( rtsp
->sessionc
, rtsp
->sessionv
, session
);
395 for( i
= 0; i
< session
->trackc
; i
++ )
396 RtspTrackClose( &session
->trackv
[i
] );
398 free( session
->trackv
);
403 /** rtsp must be locked */
404 static void RtspClientAlive( rtsp_session_t
*session
)
406 if (session
->stream
->timeout
<= 0)
409 session
->last_seen
= mdate();
410 RtspUpdateTimer(session
->stream
);
413 static int dup_socket(int oldfd
)
417 newfd
= vlc_dup(oldfd
);
419 WSAPROTOCOL_INFO info
;
420 WSADuplicateSocket (oldfd
, GetCurrentProcessId (), &info
);
421 newfd
= WSASocket (info
.iAddressFamily
, info
.iSocketType
,
422 info
.iProtocol
, &info
, 0, 0);
427 /* Attach a starting VoD RTP id to its RTSP track, and let it
428 * initialize with the parameters of the SETUP request */
429 int RtspTrackAttach( rtsp_stream_t
*rtsp
, const char *name
,
430 rtsp_stream_id_t
*id
, sout_stream_id_sys_t
*sout_id
,
431 uint32_t *ssrc
, uint16_t *seq_init
)
433 int val
= VLC_EGENERIC
;
434 rtsp_session_t
*session
;
436 vlc_mutex_lock(&rtsp
->lock
);
437 session
= RtspClientGet(rtsp
, name
);
442 rtsp_strack_t
*tr
= NULL
;
443 for (int i
= 0; i
< session
->trackc
; i
++)
445 if (session
->trackv
[i
].id
== id
)
447 tr
= session
->trackv
+ i
;
454 tr
->sout_id
= sout_id
;
455 tr
->rtp_fd
= dup_socket(tr
->setup_fd
);
459 /* The track was not SETUP. We still create one because we'll
460 * need the sout_id if we set it up later. */
461 rtsp_strack_t track
= { .id
= id
, .sout_id
= sout_id
,
462 .setup_fd
= -1, .rtp_fd
= -1 };
463 vlc_rand_bytes (&track
.seq_init
, sizeof (track
.seq_init
));
464 vlc_rand_bytes (&track
.ssrc
, sizeof (track
.ssrc
));
466 TAB_APPEND(session
->trackc
, session
->trackv
, track
);
467 tr
= session
->trackv
+ session
->trackc
- 1;
470 *ssrc
= ntohl(tr
->ssrc
);
471 *seq_init
= tr
->seq_init
;
473 if (tr
->rtp_fd
!= -1)
476 rtp_add_sink(tr
->sout_id
, tr
->rtp_fd
, false, &seq
);
477 /* To avoid race conditions, sout_id->i_seq_sent_next must
478 * be set here and now. Make sure the caller did its job
479 * properly when passing seq_init. */
480 assert(tr
->seq_init
== seq
);
485 vlc_mutex_unlock(&rtsp
->lock
);
490 /* Remove references to the RTP id when it is stopped */
491 void RtspTrackDetach( rtsp_stream_t
*rtsp
, const char *name
,
492 sout_stream_id_sys_t
*sout_id
)
494 rtsp_session_t
*session
;
496 vlc_mutex_lock(&rtsp
->lock
);
497 session
= RtspClientGet(rtsp
, name
);
502 for (int i
= 0; i
< session
->trackc
; i
++)
504 rtsp_strack_t
*tr
= session
->trackv
+ i
;
505 if (tr
->sout_id
== sout_id
)
507 if (tr
->setup_fd
== -1)
509 /* No (more) SETUP information: better get rid of the
510 * track so that we can have new random ssrc and
511 * seq_init next time. */
512 TAB_ERASE(session
->trackc
, session
->trackv
, i
);
515 /* We keep the SETUP information of the track, but stop it */
516 if (tr
->rtp_fd
!= -1)
518 rtp_del_sink(tr
->sout_id
, tr
->rtp_fd
);
527 vlc_mutex_unlock(&rtsp
->lock
);
531 /** rtsp must be locked */
532 static void RtspTrackClose( rtsp_strack_t
*tr
)
534 if (tr
->setup_fd
!= -1)
536 if (tr
->rtp_fd
!= -1)
538 rtp_del_sink(tr
->sout_id
, tr
->rtp_fd
);
541 net_Close(tr
->setup_fd
);
547 /** Finds the next transport choice */
548 static inline const char *transport_next( const char *str
)
550 /* Looks for comma */
551 str
= strchr( str
, ',' );
553 return NULL
; /* No more transport options */
555 str
++; /* skips comma */
556 while( strchr( "\r\n\t ", *str
) )
559 return (*str
) ? str
: NULL
;
563 /** Finds the next transport parameter */
564 static inline const char *parameter_next( const char *str
)
566 while( strchr( ",;", *str
) == NULL
)
569 return (*str
== ';') ? (str
+ 1) : NULL
;
573 static int64_t ParseNPT (const char *str
)
575 locale_t loc
= newlocale (LC_NUMERIC_MASK
, "C", NULL
);
576 locale_t oldloc
= uselocale (loc
);
580 if (sscanf (str
, "%u:%u:%f", &hour
, &min
, &sec
) == 3)
581 sec
+= ((hour
* 60) + min
) * 60;
583 if (sscanf (str
, "%f", &sec
) != 1)
586 if (loc
!= (locale_t
)0)
591 return sec
< 0 ? -1 : sec
* CLOCK_FREQ
;
595 /** RTSP requests handler
596 * @param id selected track for non-aggregate URLs,
597 * NULL for aggregate URLs
599 static int RtspHandler( rtsp_stream_t
*rtsp
, rtsp_stream_id_t
*id
,
601 httpd_message_t
*answer
,
602 const httpd_message_t
*query
)
604 vlc_object_t
*owner
= rtsp
->owner
;
606 const char *psz_session
= NULL
, *psz
;
607 char control
[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
608 + strlen( rtsp
->psz_path
)];
609 bool vod
= rtsp
->vod_media
!= NULL
;
614 if( answer
== NULL
|| query
== NULL
|| cl
== NULL
)
618 /* Build self-referential control URL */
619 char ip
[NI_MAXNUMERICHOST
], *ptr
;
622 httpd_ServerIP( cl
, ip
, &port
);
623 ptr
= strchr( ip
, '%' );
627 if( strchr( ip
, ':' ) != NULL
)
628 sprintf( control
, "rtsp://[%s]:%d%s", ip
, port
, rtsp
->psz_path
);
630 sprintf( control
, "rtsp://%s:%d%s", ip
, port
, rtsp
->psz_path
);
634 answer
->i_proto
= HTTPD_PROTO_RTSP
;
635 answer
->i_version
= 0;
636 answer
->i_type
= HTTPD_MSG_ANSWER
;
638 answer
->p_body
= NULL
;
640 httpd_MsgAdd( answer
, "Server", "VLC/%s", VERSION
);
642 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
644 if (gmtime_r (&now
, &ut
) != NULL
)
645 { /* RFC1123 format, GMT is mandatory */
646 static const char wdays
[7][4] = {
647 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
648 static const char mons
[12][4] = {
649 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
650 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
651 httpd_MsgAdd (answer
, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
652 wdays
[ut
.tm_wday
], ut
.tm_mday
, mons
[ut
.tm_mon
],
653 1900 + ut
.tm_year
, ut
.tm_hour
, ut
.tm_min
, ut
.tm_sec
);
656 if( query
->i_proto
!= HTTPD_PROTO_RTSP
)
658 answer
->i_status
= 505;
661 if( httpd_MsgGet( query
, "Require" ) != NULL
)
663 answer
->i_status
= 551;
664 httpd_MsgAdd( answer
, "Unsupported", "%s",
665 httpd_MsgGet( query
, "Require" ) );
668 switch( query
->i_type
)
670 case HTTPD_MSG_DESCRIBE
:
671 { /* Aggregate-only */
674 answer
->i_status
= 460;
678 answer
->i_status
= 200;
679 httpd_MsgAdd( answer
, "Content-Type", "%s", "application/sdp" );
680 httpd_MsgAdd( answer
, "Content-Base", "%s", control
);
682 answer
->p_body
= (uint8_t *) ( vod
?
683 SDPGenerateVoD( rtsp
->vod_media
, control
) :
684 SDPGenerate( (sout_stream_t
*)owner
, control
) );
685 if( answer
->p_body
!= NULL
)
686 answer
->i_body
= strlen( (char *)answer
->p_body
);
688 answer
->i_status
= 500;
692 case HTTPD_MSG_SETUP
:
693 /* Non-aggregate-only */
696 answer
->i_status
= 459;
700 psz_session
= httpd_MsgGet( query
, "Session" );
701 answer
->i_status
= 461;
703 for( const char *tpt
= httpd_MsgGet( query
, "Transport" );
705 tpt
= transport_next( tpt
) )
707 bool b_multicast
= true, b_unsupp
= false;
708 unsigned loport
= 5004, hiport
; /* from RFC3551 */
710 /* Check transport protocol. */
711 /* Currently, we only support RTP/AVP over UDP */
712 if( strncmp( tpt
, "RTP/AVP", 7 ) )
715 if( strncmp( tpt
, "/UDP", 4 ) == 0 )
717 if( strchr( ";,", *tpt
) == NULL
)
720 /* Parse transport options */
721 for( const char *opt
= parameter_next( tpt
);
723 opt
= parameter_next( opt
) )
725 if( strncmp( opt
, "multicast", 9 ) == 0)
728 if( strncmp( opt
, "unicast", 7 ) == 0 )
731 if( sscanf( opt
, "client_port=%u-%u", &loport
, &hiport
)
735 if( strncmp( opt
, "mode=", 5 ) == 0 )
737 if( strncasecmp( opt
+ 5, "play", 4 )
738 && strncasecmp( opt
+ 5, "\"PLAY\"", 6 ) )
746 if( strncmp( opt
,"destination=", 12 ) == 0 )
748 answer
->i_status
= 403;
754 * Every other option is unsupported:
756 * "source" and "append" are invalid (server-only);
757 * "ssrc" also (as clarified per RFC2326bis).
759 * For multicast, "port", "layers", "ttl" are set by the
760 * stream output configuration.
762 * For unicast, we want to decide "server_port" values.
764 * "interleaved" is not implemented.
776 char dst
[NI_MAXNUMERICHOST
];
778 if( id
->mcast_fd
== -1 )
781 net_GetPeerAddress(id
->mcast_fd
, dst
, &dport
);
783 ttl
= var_InheritInteger(owner
, "ttl");
785 /* FIXME: the TTL is left to the OS default, we can
786 * only guess that it's 1. */
789 if( psz_session
== NULL
)
791 /* Create a dummy session ID */
792 snprintf( psz_sesbuf
, sizeof( psz_sesbuf
), "%lu",
794 psz_session
= psz_sesbuf
;
796 answer
->i_status
= 200;
798 httpd_MsgAdd( answer
, "Transport",
799 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
801 dst
, dport
, dport
+ 1, ttl
);
802 /* FIXME: this doesn't work with RTP + RTCP mux */
806 char ip
[NI_MAXNUMERICHOST
], src
[NI_MAXNUMERICHOST
];
807 rtsp_session_t
*ses
= NULL
;
811 if( httpd_ClientIP( cl
, ip
, NULL
) == NULL
)
813 answer
->i_status
= 500;
817 fd
= net_ConnectDgram( owner
, ip
, loport
, -1,
822 "cannot create RTP socket for %s port %u",
824 answer
->i_status
= 500;
828 /* Ignore any unexpected incoming packet */
829 setsockopt (fd
, SOL_SOCKET
, SO_RCVBUF
, &(int){ 0 },
831 net_GetSockAddress( fd
, src
, &sport
);
833 vlc_mutex_lock( &rtsp
->lock
);
834 if( psz_session
== NULL
)
836 ses
= RtspClientNew( rtsp
);
837 snprintf( psz_sesbuf
, sizeof( psz_sesbuf
), "%"PRIx64
,
839 psz_session
= psz_sesbuf
;
843 ses
= RtspClientGet( rtsp
, psz_session
);
846 answer
->i_status
= 454;
847 vlc_mutex_unlock( &rtsp
->lock
);
852 RtspClientAlive(ses
);
854 rtsp_strack_t
*tr
= NULL
;
855 for (int i
= 0; i
< ses
->trackc
; i
++)
857 if (ses
->trackv
[i
].id
== id
)
859 tr
= ses
->trackv
+ i
;
866 /* Set up a new track */
867 rtsp_strack_t track
= { .id
= id
,
868 .sout_id
= id
->sout_id
,
874 vlc_rand_bytes (&track
.seq_init
,
875 sizeof (track
.seq_init
));
876 vlc_rand_bytes (&track
.ssrc
, sizeof (track
.ssrc
));
882 TAB_APPEND(ses
->trackc
, ses
->trackv
, track
);
884 else if (tr
->setup_fd
== -1)
886 /* The track was not SETUP, but it exists
887 * because there is a sout_id running for it */
893 /* The track is already set up, and we don't
894 * support changing the transport parameters on
896 vlc_mutex_unlock( &rtsp
->lock
);
897 answer
->i_status
= 455;
901 vlc_mutex_unlock( &rtsp
->lock
);
903 httpd_ServerIP( cl
, ip
, NULL
);
905 /* Specify source IP only if it is different from the
906 * RTSP control connection server address */
907 if( strcmp( src
, ip
) )
909 char *ptr
= strchr( src
, '%' );
910 if( ptr
!= NULL
) *ptr
= '\0'; /* remove scope ID */
915 httpd_MsgAdd( answer
, "Transport",
916 "RTP/AVP/UDP;unicast%s%s;"
917 "client_port=%u-%u;server_port=%u-%u;"
918 "ssrc=%08X;mode=play",
919 src
[0] ? ";source=" : "", src
,
920 loport
, loport
+ 1, sport
, sport
+ 1, ssrc
);
922 answer
->i_status
= 200;
931 answer
->i_status
= 200;
933 psz_session
= httpd_MsgGet( query
, "Session" );
934 int64_t start
= -1, end
= -1, npt
;
935 const char *range
= httpd_MsgGet (query
, "Range");
938 if (strncmp (range
, "npt=", 4))
940 answer
->i_status
= 501;
944 start
= ParseNPT (range
+ 4);
945 range
= strchr(range
, '-');
946 if (range
!= NULL
&& *(range
+ 1))
947 end
= ParseNPT (range
+ 1);
949 if (end
>= 0 && end
< start
)
951 answer
->i_status
= 457;
957 if (vod_check_range(rtsp
->vod_media
, psz_session
,
958 start
, end
) != VLC_SUCCESS
)
960 answer
->i_status
= 457;
964 /* We accept start times of 0 even for broadcast streams
965 * that already started */
966 else if (start
> 0 || end
>= 0)
968 answer
->i_status
= 456;
972 vlc_mutex_lock( &rtsp
->lock
);
973 ses
= RtspClientGet( rtsp
, psz_session
);
976 char info
[ses
->trackc
* ( strlen( control
) + TRACK_PATH_SIZE
977 + sizeof("url=;seq=65535;rtptime=4294967295, ")
980 RtspClientAlive(ses
);
982 sout_stream_id_sys_t
*sout_id
= NULL
;
985 /* We don't keep a reference to the sout_stream_t,
986 * so we check if a sout_id is available instead. */
987 for (int i
= 0; i
< ses
->trackc
; i
++)
989 sout_id
= ses
->trackv
[i
].sout_id
;
994 int64_t ts
= rtp_get_ts(vod
? NULL
: (sout_stream_t
*)owner
,
995 sout_id
, rtsp
->vod_media
, psz_session
,
998 for( int i
= 0; i
< ses
->trackc
; i
++ )
1000 rtsp_strack_t
*tr
= ses
->trackv
+ i
;
1001 if( ( id
== NULL
) || ( tr
->id
== id
) )
1003 if (tr
->setup_fd
== -1)
1004 /* Track not SETUP */
1008 if( tr
->rtp_fd
== -1 )
1010 /* Track not PLAYing yet */
1011 if (tr
->sout_id
== NULL
)
1012 /* Instance not running yet (VoD) */
1016 /* Instance running, add a sink to it */
1017 tr
->rtp_fd
= dup_socket(tr
->setup_fd
);
1018 if (tr
->rtp_fd
== -1)
1021 rtp_add_sink( tr
->sout_id
, tr
->rtp_fd
,
1027 /* Track already playing */
1028 assert( tr
->sout_id
!= NULL
);
1029 seq
= rtp_get_seq( tr
->sout_id
);
1031 char *url
= RtspAppendTrackPath( tr
->id
, control
);
1032 infolen
+= sprintf( info
+ infolen
,
1033 "url=%s;seq=%u;rtptime=%u, ",
1034 url
!= NULL
? url
: "", seq
,
1035 rtp_compute_ts( tr
->id
->clock_rate
, ts
) );
1041 info
[infolen
- 2] = '\0'; /* remove trailing ", " */
1042 httpd_MsgAdd( answer
, "RTP-Info", "%s", info
);
1045 vlc_mutex_unlock( &rtsp
->lock
);
1051 vod_play(rtsp
->vod_media
, psz_session
, &start
, end
);
1055 double f_npt
= (double) npt
/ CLOCK_FREQ
;
1056 httpd_MsgAdd( answer
, "Range", "npt=%f-", f_npt
);
1059 if( httpd_MsgGet( query
, "Scale" ) != NULL
)
1060 httpd_MsgAdd( answer
, "Scale", "1." );
1064 case HTTPD_MSG_PAUSE
:
1066 if (id
== NULL
&& !vod
)
1068 answer
->i_status
= 405;
1069 httpd_MsgAdd( answer
, "Allow",
1070 "DESCRIBE, TEARDOWN, PLAY, GET_PARAMETER" );
1074 rtsp_session_t
*ses
;
1075 answer
->i_status
= 200;
1076 psz_session
= httpd_MsgGet( query
, "Session" );
1077 vlc_mutex_lock( &rtsp
->lock
);
1078 ses
= RtspClientGet( rtsp
, psz_session
);
1081 if (id
!= NULL
) /* "Mute" the selected track */
1084 for (int i
= 0; i
< ses
->trackc
; i
++)
1086 rtsp_strack_t
*tr
= ses
->trackv
+ i
;;
1089 if (tr
->setup_fd
== -1)
1093 if (tr
->rtp_fd
!= -1)
1095 rtp_del_sink(tr
->sout_id
, tr
->rtp_fd
);
1102 answer
->i_status
= 455;
1104 RtspClientAlive(ses
);
1106 vlc_mutex_unlock( &rtsp
->lock
);
1108 if (ses
!= NULL
&& id
== NULL
)
1112 vod_pause(rtsp
->vod_media
, psz_session
, &npt
);
1113 double f_npt
= (double) npt
/ CLOCK_FREQ
;
1114 httpd_MsgAdd( answer
, "Range", "npt=%f-", f_npt
);
1119 case HTTPD_MSG_GETPARAMETER
:
1120 if( query
->i_body
> 0 )
1122 answer
->i_status
= 451;
1126 psz_session
= httpd_MsgGet( query
, "Session" );
1127 answer
->i_status
= 200;
1128 vlc_mutex_lock( &rtsp
->lock
);
1129 rtsp_session_t
*ses
= RtspClientGet( rtsp
, psz_session
);
1131 RtspClientAlive(ses
);
1132 vlc_mutex_unlock( &rtsp
->lock
);
1135 case HTTPD_MSG_TEARDOWN
:
1137 rtsp_session_t
*ses
;
1139 answer
->i_status
= 200;
1141 psz_session
= httpd_MsgGet( query
, "Session" );
1143 vlc_mutex_lock( &rtsp
->lock
);
1144 ses
= RtspClientGet( rtsp
, psz_session
);
1147 if( id
== NULL
) /* Delete the entire session */
1149 RtspClientDel( rtsp
, ses
);
1151 vod_stop(rtsp
->vod_media
, psz_session
);
1152 RtspUpdateTimer(rtsp
);
1154 else /* Delete one track from the session */
1156 for( int i
= 0; i
< ses
->trackc
; i
++ )
1158 if( ses
->trackv
[i
].id
== id
)
1160 RtspTrackClose( &ses
->trackv
[i
] );
1161 /* Keep VoD tracks whose instance is still
1163 if (!(vod
&& ses
->trackv
[i
].sout_id
!= NULL
))
1164 TAB_ERASE(ses
->trackc
, ses
->trackv
, i
);
1167 RtspClientAlive(ses
);
1170 vlc_mutex_unlock( &rtsp
->lock
);
1175 return VLC_EGENERIC
;
1180 if (rtsp
->timeout
> 0)
1181 httpd_MsgAdd( answer
, "Session", "%s;timeout=%d", psz_session
,
1184 httpd_MsgAdd( answer
, "Session", "%s", psz_session
);
1187 httpd_MsgAdd( answer
, "Content-Length", "%d", answer
->i_body
);
1188 httpd_MsgAdd( answer
, "Cache-Control", "no-cache" );
1190 psz
= httpd_MsgGet( query
, "Cseq" );
1192 httpd_MsgAdd( answer
, "Cseq", "%s", psz
);
1193 psz
= httpd_MsgGet( query
, "Timestamp" );
1195 httpd_MsgAdd( answer
, "Timestamp", "%s", psz
);
1201 /** Aggregate RTSP callback */
1202 static int RtspCallback( httpd_callback_sys_t
*p_args
,
1204 httpd_message_t
*answer
,
1205 const httpd_message_t
*query
)
1207 return RtspHandler( (rtsp_stream_t
*)p_args
, NULL
, cl
, answer
, query
);
1211 /** Non-aggregate RTSP callback */
1212 static int RtspCallbackId( httpd_callback_sys_t
*p_args
,
1214 httpd_message_t
*answer
,
1215 const httpd_message_t
*query
)
1217 rtsp_stream_id_t
*id
= (rtsp_stream_id_t
*)p_args
;
1218 return RtspHandler( id
->stream
, id
, cl
, answer
, query
);