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