00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "avformat.h"
00025
00026 #include "internal.h"
00027 #include "network.h"
00028 #include "os_support.h"
00029 #include "rtsp.h"
00030 #include "rdt.h"
00031
00032
00033
00034
00035 static int rtsp_read_play(AVFormatContext *s)
00036 {
00037 RTSPState *rt = s->priv_data;
00038 RTSPMessageHeader reply1, *reply = &reply1;
00039 int i;
00040 char cmd[1024];
00041
00042 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
00043 rt->nb_byes = 0;
00044
00045 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
00046 if (rt->transport == RTSP_TRANSPORT_RTP) {
00047 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00048 RTSPStream *rtsp_st = rt->rtsp_streams[i];
00049 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
00050 if (!rtpctx)
00051 continue;
00052 ff_rtp_reset_packet_queue(rtpctx);
00053 rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
00054 rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
00055 rtpctx->base_timestamp = 0;
00056 rtpctx->rtcp_ts_offset = 0;
00057 }
00058 }
00059 if (rt->state == RTSP_STATE_PAUSED) {
00060 cmd[0] = 0;
00061 } else {
00062 snprintf(cmd, sizeof(cmd),
00063 "Range: npt=%0.3f-\r\n",
00064 (double)rt->seek_timestamp / AV_TIME_BASE);
00065 }
00066 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
00067 if (reply->status_code != RTSP_STATUS_OK) {
00068 return -1;
00069 }
00070 if (rt->transport == RTSP_TRANSPORT_RTP &&
00071 reply->range_start != AV_NOPTS_VALUE) {
00072 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00073 RTSPStream *rtsp_st = rt->rtsp_streams[i];
00074 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
00075 AVStream *st = NULL;
00076 if (!rtpctx || rtsp_st->stream_index < 0)
00077 continue;
00078 st = s->streams[rtsp_st->stream_index];
00079 rtpctx->range_start_offset =
00080 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
00081 st->time_base);
00082 }
00083 }
00084 }
00085 rt->state = RTSP_STATE_STREAMING;
00086 return 0;
00087 }
00088
00089
00090 static int rtsp_read_pause(AVFormatContext *s)
00091 {
00092 RTSPState *rt = s->priv_data;
00093 RTSPMessageHeader reply1, *reply = &reply1;
00094
00095 if (rt->state != RTSP_STATE_STREAMING)
00096 return 0;
00097 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
00098 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
00099 if (reply->status_code != RTSP_STATUS_OK) {
00100 return -1;
00101 }
00102 }
00103 rt->state = RTSP_STATE_PAUSED;
00104 return 0;
00105 }
00106
00107 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
00108 {
00109 RTSPState *rt = s->priv_data;
00110 char cmd[1024];
00111 unsigned char *content = NULL;
00112 int ret;
00113
00114
00115 snprintf(cmd, sizeof(cmd),
00116 "Accept: application/sdp\r\n");
00117 if (rt->server_type == RTSP_SERVER_REAL) {
00122 av_strlcat(cmd,
00123 "Require: com.real.retain-entity-for-setup\r\n",
00124 sizeof(cmd));
00125 }
00126 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
00127 if (!content)
00128 return AVERROR_INVALIDDATA;
00129 if (reply->status_code != RTSP_STATUS_OK) {
00130 av_freep(&content);
00131 return AVERROR_INVALIDDATA;
00132 }
00133
00134 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
00135
00136 ret = ff_sdp_parse(s, (const char *)content);
00137 av_freep(&content);
00138 if (ret < 0)
00139 return ret;
00140
00141 return 0;
00142 }
00143
00144 static int rtsp_probe(AVProbeData *p)
00145 {
00146 if (av_strstart(p->filename, "rtsp:", NULL))
00147 return AVPROBE_SCORE_MAX;
00148 return 0;
00149 }
00150
00151 static int rtsp_read_header(AVFormatContext *s,
00152 AVFormatParameters *ap)
00153 {
00154 RTSPState *rt = s->priv_data;
00155 int ret;
00156
00157 ret = ff_rtsp_connect(s);
00158 if (ret)
00159 return ret;
00160
00161 rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
00162 if (!rt->real_setup_cache)
00163 return AVERROR(ENOMEM);
00164 rt->real_setup = rt->real_setup_cache + s->nb_streams;
00165
00166 if (ap->initial_pause) {
00167
00168 } else {
00169 if (rtsp_read_play(s) < 0) {
00170 ff_rtsp_close_streams(s);
00171 ff_rtsp_close_connections(s);
00172 return AVERROR_INVALIDDATA;
00173 }
00174 }
00175
00176 return 0;
00177 }
00178
00179 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
00180 uint8_t *buf, int buf_size)
00181 {
00182 RTSPState *rt = s->priv_data;
00183 int id, len, i, ret;
00184 RTSPStream *rtsp_st;
00185
00186 #ifdef DEBUG_RTP_TCP
00187 av_dlog(s, "tcp_read_packet:\n");
00188 #endif
00189 redo:
00190 for (;;) {
00191 RTSPMessageHeader reply;
00192
00193 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
00194 if (ret < 0)
00195 return ret;
00196 if (ret == 1)
00197 break;
00198
00199 if (rt->state != RTSP_STATE_STREAMING)
00200 return 0;
00201 }
00202 ret = url_read_complete(rt->rtsp_hd, buf, 3);
00203 if (ret != 3)
00204 return -1;
00205 id = buf[0];
00206 len = AV_RB16(buf + 1);
00207 #ifdef DEBUG_RTP_TCP
00208 av_dlog(s, "id=%d len=%d\n", id, len);
00209 #endif
00210 if (len > buf_size || len < 12)
00211 goto redo;
00212
00213 ret = url_read_complete(rt->rtsp_hd, buf, len);
00214 if (ret != len)
00215 return -1;
00216 if (rt->transport == RTSP_TRANSPORT_RDT &&
00217 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
00218 return -1;
00219
00220
00221 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00222 rtsp_st = rt->rtsp_streams[i];
00223 if (id >= rtsp_st->interleaved_min &&
00224 id <= rtsp_st->interleaved_max)
00225 goto found;
00226 }
00227 goto redo;
00228 found:
00229 *prtsp_st = rtsp_st;
00230 return len;
00231 }
00232
00233 static int resetup_tcp(AVFormatContext *s)
00234 {
00235 RTSPState *rt = s->priv_data;
00236 char host[1024];
00237 int port;
00238
00239 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
00240 s->filename);
00241 ff_rtsp_undo_setup(s);
00242 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
00243 rt->real_challenge);
00244 }
00245
00246 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
00247 {
00248 RTSPState *rt = s->priv_data;
00249 int ret;
00250 RTSPMessageHeader reply1, *reply = &reply1;
00251 char cmd[1024];
00252
00253 retry:
00254 if (rt->server_type == RTSP_SERVER_REAL) {
00255 int i;
00256
00257 for (i = 0; i < s->nb_streams; i++)
00258 rt->real_setup[i] = s->streams[i]->discard;
00259
00260 if (!rt->need_subscription) {
00261 if (memcmp (rt->real_setup, rt->real_setup_cache,
00262 sizeof(enum AVDiscard) * s->nb_streams)) {
00263 snprintf(cmd, sizeof(cmd),
00264 "Unsubscribe: %s\r\n",
00265 rt->last_subscription);
00266 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
00267 cmd, reply, NULL);
00268 if (reply->status_code != RTSP_STATUS_OK)
00269 return AVERROR_INVALIDDATA;
00270 rt->need_subscription = 1;
00271 }
00272 }
00273
00274 if (rt->need_subscription) {
00275 int r, rule_nr, first = 1;
00276
00277 memcpy(rt->real_setup_cache, rt->real_setup,
00278 sizeof(enum AVDiscard) * s->nb_streams);
00279 rt->last_subscription[0] = 0;
00280
00281 snprintf(cmd, sizeof(cmd),
00282 "Subscribe: ");
00283 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00284 rule_nr = 0;
00285 for (r = 0; r < s->nb_streams; r++) {
00286 if (s->streams[r]->id == i) {
00287 if (s->streams[r]->discard != AVDISCARD_ALL) {
00288 if (!first)
00289 av_strlcat(rt->last_subscription, ",",
00290 sizeof(rt->last_subscription));
00291 ff_rdt_subscribe_rule(
00292 rt->last_subscription,
00293 sizeof(rt->last_subscription), i, rule_nr);
00294 first = 0;
00295 }
00296 rule_nr++;
00297 }
00298 }
00299 }
00300 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
00301 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
00302 cmd, reply, NULL);
00303 if (reply->status_code != RTSP_STATUS_OK)
00304 return AVERROR_INVALIDDATA;
00305 rt->need_subscription = 0;
00306
00307 if (rt->state == RTSP_STATE_STREAMING)
00308 rtsp_read_play (s);
00309 }
00310 }
00311
00312 ret = ff_rtsp_fetch_packet(s, pkt);
00313 if (ret < 0) {
00314 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
00315 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
00316 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
00317 RTSPMessageHeader reply1, *reply = &reply1;
00318 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
00319 if (rtsp_read_pause(s) != 0)
00320 return -1;
00321
00322
00323 if (rt->server_type == RTSP_SERVER_REAL)
00324 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
00325 reply, NULL);
00326 rt->session_id[0] = '\0';
00327 if (resetup_tcp(s) == 0) {
00328 rt->state = RTSP_STATE_IDLE;
00329 rt->need_subscription = 1;
00330 if (rtsp_read_play(s) != 0)
00331 return -1;
00332 goto retry;
00333 }
00334 }
00335 }
00336 return ret;
00337 }
00338 rt->packets++;
00339
00340
00341 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
00342 if (rt->server_type == RTSP_SERVER_WMS) {
00343 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
00344 } else {
00345 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
00346 }
00347 }
00348
00349 return 0;
00350 }
00351
00352 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
00353 int64_t timestamp, int flags)
00354 {
00355 RTSPState *rt = s->priv_data;
00356
00357 rt->seek_timestamp = av_rescale_q(timestamp,
00358 s->streams[stream_index]->time_base,
00359 AV_TIME_BASE_Q);
00360 switch(rt->state) {
00361 default:
00362 case RTSP_STATE_IDLE:
00363 break;
00364 case RTSP_STATE_STREAMING:
00365 if (rtsp_read_pause(s) != 0)
00366 return -1;
00367 rt->state = RTSP_STATE_SEEKING;
00368 if (rtsp_read_play(s) != 0)
00369 return -1;
00370 break;
00371 case RTSP_STATE_PAUSED:
00372 rt->state = RTSP_STATE_IDLE;
00373 break;
00374 }
00375 return 0;
00376 }
00377
00378 static int rtsp_read_close(AVFormatContext *s)
00379 {
00380 RTSPState *rt = s->priv_data;
00381
00382 #if 0
00383
00384 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
00385 avio_close(&rt->rtsp_gb);
00386 }
00387 #endif
00388 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
00389
00390 ff_rtsp_close_streams(s);
00391 ff_rtsp_close_connections(s);
00392 ff_network_close();
00393 rt->real_setup = NULL;
00394 av_freep(&rt->real_setup_cache);
00395 return 0;
00396 }
00397
00398 AVInputFormat ff_rtsp_demuxer = {
00399 "rtsp",
00400 NULL_IF_CONFIG_SMALL("RTSP input format"),
00401 sizeof(RTSPState),
00402 rtsp_probe,
00403 rtsp_read_header,
00404 rtsp_read_packet,
00405 rtsp_read_close,
00406 rtsp_read_seek,
00407 .flags = AVFMT_NOFILE,
00408 .read_play = rtsp_read_play,
00409 .read_pause = rtsp_read_pause,
00410 };