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 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
00090 {
00091 RTSPState *rt = s->priv_data;
00092 char cmd[1024];
00093 unsigned char *content = NULL;
00094 int ret;
00095
00096
00097 snprintf(cmd, sizeof(cmd),
00098 "Accept: application/sdp\r\n");
00099 if (rt->server_type == RTSP_SERVER_REAL) {
00104 av_strlcat(cmd,
00105 "Require: com.real.retain-entity-for-setup\r\n",
00106 sizeof(cmd));
00107 }
00108 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
00109 if (!content)
00110 return AVERROR_INVALIDDATA;
00111 if (reply->status_code != RTSP_STATUS_OK) {
00112 av_freep(&content);
00113 return AVERROR_INVALIDDATA;
00114 }
00115
00116 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
00117
00118 ret = ff_sdp_parse(s, (const char *)content);
00119 av_freep(&content);
00120 if (ret < 0)
00121 return AVERROR_INVALIDDATA;
00122
00123 return 0;
00124 }
00125
00126 static int rtsp_probe(AVProbeData *p)
00127 {
00128 if (av_strstart(p->filename, "rtsp:", NULL))
00129 return AVPROBE_SCORE_MAX;
00130 return 0;
00131 }
00132
00133 static int rtsp_read_header(AVFormatContext *s,
00134 AVFormatParameters *ap)
00135 {
00136 RTSPState *rt = s->priv_data;
00137 int ret;
00138
00139 ret = ff_rtsp_connect(s);
00140 if (ret)
00141 return ret;
00142
00143 rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
00144 if (!rt->real_setup_cache)
00145 return AVERROR(ENOMEM);
00146 rt->real_setup = rt->real_setup_cache + s->nb_streams;
00147
00148 if (ap->initial_pause) {
00149
00150 } else {
00151 if (rtsp_read_play(s) < 0) {
00152 ff_rtsp_close_streams(s);
00153 ff_rtsp_close_connections(s);
00154 return AVERROR_INVALIDDATA;
00155 }
00156 }
00157
00158 return 0;
00159 }
00160
00161 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
00162 uint8_t *buf, int buf_size)
00163 {
00164 RTSPState *rt = s->priv_data;
00165 int id, len, i, ret;
00166 RTSPStream *rtsp_st;
00167
00168 #ifdef DEBUG_RTP_TCP
00169 dprintf(s, "tcp_read_packet:\n");
00170 #endif
00171 redo:
00172 for (;;) {
00173 RTSPMessageHeader reply;
00174
00175 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
00176 if (ret < 0)
00177 return ret;
00178 if (ret == 1)
00179 break;
00180
00181 if (rt->state != RTSP_STATE_STREAMING)
00182 return 0;
00183 }
00184 ret = url_read_complete(rt->rtsp_hd, buf, 3);
00185 if (ret != 3)
00186 return -1;
00187 id = buf[0];
00188 len = AV_RB16(buf + 1);
00189 #ifdef DEBUG_RTP_TCP
00190 dprintf(s, "id=%d len=%d\n", id, len);
00191 #endif
00192 if (len > buf_size || len < 12)
00193 goto redo;
00194
00195 ret = url_read_complete(rt->rtsp_hd, buf, len);
00196 if (ret != len)
00197 return -1;
00198 if (rt->transport == RTSP_TRANSPORT_RDT &&
00199 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
00200 return -1;
00201
00202
00203 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00204 rtsp_st = rt->rtsp_streams[i];
00205 if (id >= rtsp_st->interleaved_min &&
00206 id <= rtsp_st->interleaved_max)
00207 goto found;
00208 }
00209 goto redo;
00210 found:
00211 *prtsp_st = rtsp_st;
00212 return len;
00213 }
00214 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
00215 {
00216 RTSPState *rt = s->priv_data;
00217 int ret;
00218 RTSPMessageHeader reply1, *reply = &reply1;
00219 char cmd[1024];
00220
00221 if (rt->server_type == RTSP_SERVER_REAL) {
00222 int i;
00223
00224 for (i = 0; i < s->nb_streams; i++)
00225 rt->real_setup[i] = s->streams[i]->discard;
00226
00227 if (!rt->need_subscription) {
00228 if (memcmp (rt->real_setup, rt->real_setup_cache,
00229 sizeof(enum AVDiscard) * s->nb_streams)) {
00230 snprintf(cmd, sizeof(cmd),
00231 "Unsubscribe: %s\r\n",
00232 rt->last_subscription);
00233 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
00234 cmd, reply, NULL);
00235 if (reply->status_code != RTSP_STATUS_OK)
00236 return AVERROR_INVALIDDATA;
00237 rt->need_subscription = 1;
00238 }
00239 }
00240
00241 if (rt->need_subscription) {
00242 int r, rule_nr, first = 1;
00243
00244 memcpy(rt->real_setup_cache, rt->real_setup,
00245 sizeof(enum AVDiscard) * s->nb_streams);
00246 rt->last_subscription[0] = 0;
00247
00248 snprintf(cmd, sizeof(cmd),
00249 "Subscribe: ");
00250 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00251 rule_nr = 0;
00252 for (r = 0; r < s->nb_streams; r++) {
00253 if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
00254 if (s->streams[r]->discard != AVDISCARD_ALL) {
00255 if (!first)
00256 av_strlcat(rt->last_subscription, ",",
00257 sizeof(rt->last_subscription));
00258 ff_rdt_subscribe_rule(
00259 rt->last_subscription,
00260 sizeof(rt->last_subscription), i, rule_nr);
00261 first = 0;
00262 }
00263 rule_nr++;
00264 }
00265 }
00266 }
00267 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
00268 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
00269 cmd, reply, NULL);
00270 if (reply->status_code != RTSP_STATUS_OK)
00271 return AVERROR_INVALIDDATA;
00272 rt->need_subscription = 0;
00273
00274 if (rt->state == RTSP_STATE_STREAMING)
00275 rtsp_read_play (s);
00276 }
00277 }
00278
00279 ret = ff_rtsp_fetch_packet(s, pkt);
00280 if (ret < 0)
00281 return ret;
00282
00283
00284 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
00285 if (rt->server_type == RTSP_SERVER_WMS) {
00286 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
00287 } else {
00288 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
00289 }
00290 }
00291
00292 return 0;
00293 }
00294
00295
00296 static int rtsp_read_pause(AVFormatContext *s)
00297 {
00298 RTSPState *rt = s->priv_data;
00299 RTSPMessageHeader reply1, *reply = &reply1;
00300
00301 if (rt->state != RTSP_STATE_STREAMING)
00302 return 0;
00303 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
00304 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
00305 if (reply->status_code != RTSP_STATUS_OK) {
00306 return -1;
00307 }
00308 }
00309 rt->state = RTSP_STATE_PAUSED;
00310 return 0;
00311 }
00312
00313 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
00314 int64_t timestamp, int flags)
00315 {
00316 RTSPState *rt = s->priv_data;
00317
00318 rt->seek_timestamp = av_rescale_q(timestamp,
00319 s->streams[stream_index]->time_base,
00320 AV_TIME_BASE_Q);
00321 switch(rt->state) {
00322 default:
00323 case RTSP_STATE_IDLE:
00324 break;
00325 case RTSP_STATE_STREAMING:
00326 if (rtsp_read_pause(s) != 0)
00327 return -1;
00328 rt->state = RTSP_STATE_SEEKING;
00329 if (rtsp_read_play(s) != 0)
00330 return -1;
00331 break;
00332 case RTSP_STATE_PAUSED:
00333 rt->state = RTSP_STATE_IDLE;
00334 break;
00335 }
00336 return 0;
00337 }
00338
00339 static int rtsp_read_close(AVFormatContext *s)
00340 {
00341 RTSPState *rt = s->priv_data;
00342
00343 #if 0
00344
00345 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
00346 url_fclose(&rt->rtsp_gb);
00347 }
00348 #endif
00349 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
00350
00351 ff_rtsp_close_streams(s);
00352 ff_rtsp_close_connections(s);
00353 ff_network_close();
00354 rt->real_setup = NULL;
00355 av_freep(&rt->real_setup_cache);
00356 return 0;
00357 }
00358
00359 AVInputFormat rtsp_demuxer = {
00360 "rtsp",
00361 NULL_IF_CONFIG_SMALL("RTSP input format"),
00362 sizeof(RTSPState),
00363 rtsp_probe,
00364 rtsp_read_header,
00365 rtsp_read_packet,
00366 rtsp_read_close,
00367 rtsp_read_seek,
00368 .flags = AVFMT_NOFILE,
00369 .read_play = rtsp_read_play,
00370 .read_pause = rtsp_read_pause,
00371 };