3
3
* Copyright (c) 2002 Fabrice Bellard.
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
21
#include "avformat.h"
21
24
#include <unistd.h> /* for select() prototype */
23
#include <netinet/in.h>
24
#include <sys/socket.h>
26
# include <arpa/inet.h>
28
# include "barpainet.h"
27
#include "rtp_internal.h"
32
30
//#define DEBUG_RTP_TCP
32
enum RTSPClientState {
34
38
typedef struct RTSPState {
35
39
URLContext *rtsp_hd; /* RTSP TCP connexion handle */
41
struct RTSPStream **rtsp_streams;
43
enum RTSPClientState state;
44
int64_t seek_timestamp;
36
46
/* XXX: currently we use unbuffered input */
37
47
// ByteIOContext rtsp_gb;
38
48
int seq; /* RTSP command sequence number */
39
49
char session_id[512];
40
50
enum RTSPProtocol protocol;
41
51
char last_reply[2048]; /* XXX: allocate ? */
52
RTPDemuxContext *cur_rtp;
44
55
typedef struct RTSPStream {
56
URLContext *rtp_handle; /* RTP stream handle */
57
RTPDemuxContext *rtp_ctx; /* RTP parse context */
59
int stream_index; /* corresponding stream index, if any. -1 if none (MPEG2TS case) */
46
60
int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
47
61
char control_url[1024]; /* url for this stream (from SDP) */
128
142
/* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
130
static int sdp_parse_rtpmap(AVCodecContext *codec, const char *p)
144
static int sdp_parse_rtpmap(AVCodecContext *codec, RTSPStream *rtsp_st, int payload_type, const char *p)
151
/* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
152
see if we can handle this kind of payload */
135
153
get_word_sep(buf, sizeof(buf), "/", &p);
136
if (!strcmp(buf, "MP4V-ES")) {
137
codec->codec_id = CODEC_ID_MPEG4;
154
if (payload_type >= RTP_PT_PRIVATE) {
155
RTPDynamicProtocolHandler *handler= RTPFirstDynamicPayloadHandler;
157
if (!strcmp(buf, handler->enc_name) && (codec->codec_type == handler->codec_type)) {
158
codec->codec_id = handler->codec_id;
159
rtsp_st->dynamic_handler= handler;
161
rtsp_st->dynamic_protocol_context= handler->open();
165
handler= handler->next;
168
/* We are in a standard case ( from http://www.iana.org/assignments/rtp-parameters) */
169
/* search into AVRtpPayloadTypes[] */
170
for (i = 0; AVRtpPayloadTypes[i].pt >= 0; ++i)
171
if (!strcmp(buf, AVRtpPayloadTypes[i].enc_name) && (codec->codec_type == AVRtpPayloadTypes[i].codec_type)){
172
codec->codec_id = AVRtpPayloadTypes[i].codec_id;
177
c = avcodec_find_decoder(codec->codec_id);
181
c_name = (char *)NULL;
184
get_word_sep(buf, sizeof(buf), "/", &p);
186
switch (codec->codec_type) {
187
case CODEC_TYPE_AUDIO:
188
av_log(codec, AV_LOG_DEBUG, " audio codec set to : %s\n", c_name);
189
codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
190
codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
192
codec->sample_rate = i;
193
get_word_sep(buf, sizeof(buf), "/", &p);
197
// TODO: there is a bug here; if it is a mono stream, and less than 22000Hz, faad upconverts to stereo and twice the
198
// frequency. No problem, but the sample rate is being set here by the sdp line. Upcoming patch forthcoming. (rdm)
200
av_log(codec, AV_LOG_DEBUG, " audio samplerate set to : %i\n", codec->sample_rate);
201
av_log(codec, AV_LOG_DEBUG, " audio channels set to : %i\n", codec->channels);
203
case CODEC_TYPE_VIDEO:
204
av_log(codec, AV_LOG_DEBUG, " video codec set to : %s\n", c_name);
144
215
/* return the length and optionnaly the data */
173
static void sdp_parse_fmtp(AVCodecContext *codec, const char *p)
244
static void sdp_parse_fmtp_config(AVCodecContext *codec, char *attr, char *value)
179
/* loop on each attribute */
184
get_word_sep(attr, sizeof(attr), "=", &p);
187
get_word_sep(value, sizeof(value), ";", &p);
190
/* handle MPEG4 video */
191
switch(codec->codec_id) {
246
switch (codec->codec_id) {
192
247
case CODEC_ID_MPEG4:
193
249
if (!strcmp(attr, "config")) {
194
250
/* decode the hexa encoded parameter */
195
len = hex_to_data(NULL, value);
196
codec->extradata = av_mallocz(len);
251
int len = hex_to_data(NULL, value);
252
codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
197
253
if (!codec->extradata)
199
255
codec->extradata_size = len;
200
256
hex_to_data(codec->extradata, value);
204
/* ignore data for other codecs */
265
typedef struct attrname_map
272
/* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
273
#define ATTR_NAME_TYPE_INT 0
274
#define ATTR_NAME_TYPE_STR 1
275
static attrname_map_t attr_names[]=
277
{"SizeLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, sizelength)},
278
{"IndexLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, indexlength)},
279
{"IndexDeltaLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, indexdeltalength)},
280
{"profile-level-id", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, profile_level_id)},
281
{"StreamType", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, streamtype)},
282
{"mode", ATTR_NAME_TYPE_STR, offsetof(rtp_payload_data_t, mode)},
286
/** parse the attribute line from the fmtp a line of an sdp resonse. This is broken out as a function
287
* because it is used in rtp_h264.c, which is forthcoming.
289
int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size)
294
get_word_sep(attr, attr_size, "=", p);
297
get_word_sep(value, value_size, ";", p);
305
/* parse a SDP line and save stream attributes */
306
static void sdp_parse_fmtp(AVStream *st, const char *p)
312
RTSPStream *rtsp_st = st->priv_data;
313
AVCodecContext *codec = st->codec;
314
rtp_payload_data_t *rtp_payload_data = &rtsp_st->rtp_payload_data;
316
/* loop on each attribute */
317
while(rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value)))
319
/* grab the codec extra_data from the config parameter of the fmtp line */
320
sdp_parse_fmtp_config(codec, attr, value);
321
/* Looking for a known attribute */
322
for (i = 0; attr_names[i].str; ++i) {
323
if (!strcasecmp(attr, attr_names[i].str)) {
324
if (attr_names[i].type == ATTR_NAME_TYPE_INT)
325
*(int *)((char *)rtp_payload_data + attr_names[i].offset) = atoi(value);
326
else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
327
*(char **)((char *)rtp_payload_data + attr_names[i].offset) = av_strdup(value);
208
// printf("'%s' = '%s'\n", attr, value);
333
/** Parse a string \p in the form of Range:npt=xx-xx, and determine the start
335
* Used for seeking in the rtp stream.
337
static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
342
if (!stristart(p, "npt=", &p))
345
*start = AV_NOPTS_VALUE;
346
*end = AV_NOPTS_VALUE;
348
get_word_sep(buf, sizeof(buf), "-", &p);
349
*start = parse_date(buf, 1);
352
get_word_sep(buf, sizeof(buf), "-", &p);
353
*end = parse_date(buf, 1);
355
// av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
356
// av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
212
359
typedef struct SDPParseState {
280
428
rtsp_st = av_mallocz(sizeof(RTSPStream));
283
st = av_new_stream(s, s->nb_streams);
286
st->priv_data = rtsp_st;
431
rtsp_st->stream_index = -1;
432
dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
288
434
rtsp_st->sdp_ip = s1->default_ip;
289
435
rtsp_st->sdp_ttl = s1->default_ttl;
291
st->codec.codec_type = codec_type;
293
437
get_word(buf1, sizeof(buf1), &p); /* port */
294
438
rtsp_st->sdp_port = atoi(buf1);
296
440
get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
298
442
/* XXX: handle list of formats */
299
443
get_word(buf1, sizeof(buf1), &p); /* format list */
300
444
rtsp_st->sdp_payload_type = atoi(buf1);
301
if (rtsp_st->sdp_payload_type < 96) {
302
/* if standard payload type, we can find the codec right now */
303
rtp_get_codec_info(&st->codec, rtsp_st->sdp_payload_type);
446
if (!strcmp(AVRtpPayloadTypes[rtsp_st->sdp_payload_type].enc_name, "MP2T")) {
447
/* no corresponding stream */
449
st = av_new_stream(s, 0);
452
st->priv_data = rtsp_st;
453
rtsp_st->stream_index = st->index;
454
st->codec->codec_type = codec_type;
455
if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
456
/* if standard payload type, we can find the codec right now */
457
rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
306
460
/* put a default control url */
307
461
pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
325
479
} else if (strstart(p, "rtpmap:", &p)) {
326
480
/* NOTE: rtpmap is only supported AFTER the 'm=' tag */
327
get_word(buf1, sizeof(buf1), &p);
481
get_word(buf1, sizeof(buf1), &p);
328
482
payload_type = atoi(buf1);
329
483
for(i = 0; i < s->nb_streams;i++) {
330
484
st = s->streams[i];
331
485
rtsp_st = st->priv_data;
332
486
if (rtsp_st->sdp_payload_type == payload_type) {
333
sdp_parse_rtpmap(&st->codec, p);
487
sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
336
490
} else if (strstart(p, "fmtp:", &p)) {
337
491
/* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
338
get_word(buf1, sizeof(buf1), &p);
339
payload_type = atoi(buf1);
340
for(i = 0; i < s->nb_streams;i++) {
342
rtsp_st = st->priv_data;
343
if (rtsp_st->sdp_payload_type == payload_type) {
344
sdp_parse_fmtp(&st->codec, p);
492
get_word(buf1, sizeof(buf1), &p);
493
payload_type = atoi(buf1);
494
for(i = 0; i < s->nb_streams;i++) {
496
rtsp_st = st->priv_data;
497
if (rtsp_st->sdp_payload_type == payload_type) {
498
if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
499
if(!rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf)) {
500
sdp_parse_fmtp(st, p);
503
sdp_parse_fmtp(st, p);
507
} else if(strstart(p, "framesize:", &p)) {
508
// let dynamic protocol handlers have a stab at the line.
509
get_word(buf1, sizeof(buf1), &p);
510
payload_type = atoi(buf1);
511
for(i = 0; i < s->nb_streams;i++) {
513
rtsp_st = st->priv_data;
514
if (rtsp_st->sdp_payload_type == payload_type) {
515
if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
516
rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf);
520
} else if(strstart(p, "range:", &p)) {
523
// this is so that seeking on a streamed file can work.
524
rtsp_parse_range_npt(p, &start, &end);
525
s->start_time= start;
526
s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
828
/* close and free RTSP streams */
829
static void rtsp_close_streams(RTSPState *rt)
834
for(i=0;i<rt->nb_rtsp_streams;i++) {
835
rtsp_st = rt->rtsp_streams[i];
837
if (rtsp_st->rtp_ctx)
838
rtp_parse_close(rtsp_st->rtp_ctx);
839
if (rtsp_st->rtp_handle)
840
url_close(rtsp_st->rtp_handle);
841
if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
842
rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
846
av_free(rt->rtsp_streams);
632
849
static int rtsp_read_header(AVFormatContext *s,
633
850
AVFormatParameters *ap)
635
852
RTSPState *rt = s->priv_data;
636
853
char host[1024], path[1024], tcpname[1024], cmd[2048];
637
854
URLContext *rtsp_hd;
638
int port, i, ret, err;
855
int port, i, j, ret, err;
639
856
RTSPHeader reply1, *reply = &reply1;
640
857
unsigned char *content = NULL;
642
858
RTSPStream *rtsp_st;
643
859
int protocol_mask;
645
862
/* extract hostname and port */
863
url_split(NULL, 0, NULL, 0,
647
864
host, sizeof(host), &port, path, sizeof(path), s->filename);
649
866
port = RTSP_DEFAULT_PORT;
697
913
if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
701
916
/* first try in specified port range */
702
if (rtsp_rtp_port_min != 0) {
703
for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
917
if (RTSP_RTP_PORT_MIN != 0) {
918
while(j <= RTSP_RTP_PORT_MAX) {
704
919
snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
705
if (!av_open_input_file(&rtsp_st->ic, buf,
706
&rtp_demux, 0, NULL))
920
if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
921
j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
711
/* then try on any port */
712
if (av_open_input_file(&rtsp_st->ic, "rtp://",
713
&rtp_demux, 0, NULL) < 0) {
714
err = AVERROR_INVALIDDATA;
927
/* then try on any port
928
** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
929
** err = AVERROR_INVALIDDATA;
719
port = rtp_get_local_port(url_fileno(&rtsp_st->ic->pb));
935
port = rtp_get_local_port(rtsp_st->rtp_handle);
720
936
if (transport[0] != '\0')
721
937
pstrcat(transport, sizeof(transport), ",");
722
938
snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
728
if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
944
else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
729
945
if (transport[0] != '\0')
730
946
pstrcat(transport, sizeof(transport), ",");
731
947
snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
735
if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
951
else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
736
952
if (transport[0] != '\0')
737
953
pstrcat(transport, sizeof(transport), ",");
738
snprintf(transport + strlen(transport),
954
snprintf(transport + strlen(transport),
739
955
sizeof(transport) - strlen(transport) - 1,
740
956
"RTP/AVP/UDP;multicast");
742
snprintf(cmd, sizeof(cmd),
958
snprintf(cmd, sizeof(cmd),
743
959
"SETUP %s RTSP/1.0\r\n"
744
960
"Transport: %s\r\n",
745
961
rtsp_st->control_url, transport);
763
979
/* close RTP connection if not choosen */
764
980
if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
765
981
(protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
766
av_close_input_file(rtsp_st->ic);
982
url_close(rtsp_st->rtp_handle);
983
rtsp_st->rtp_handle = NULL;
770
986
switch(reply->transports[0].protocol) {
771
987
case RTSP_PROTOCOL_RTP_TCP:
773
if (av_open_input_file(&rtsp_st->ic, "null", fmt, 0, NULL) < 0) {
774
err = AVERROR_INVALIDDATA;
777
988
rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
778
989
rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
781
992
case RTSP_PROTOCOL_RTP_UDP:
785
996
/* XXX: also use address if specified */
786
snprintf(url, sizeof(url), "rtp://%s:%d",
997
snprintf(url, sizeof(url), "rtp://%s:%d",
787
998
host, reply->transports[0].server_port_min);
788
if (rtp_set_remote_url(url_fileno(&rtsp_st->ic->pb), url) < 0) {
999
if (rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
789
1000
err = AVERROR_INVALIDDATA;
800
1010
ttl = reply->transports[0].ttl;
803
snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
1013
snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
805
1015
reply->transports[0].server_port_min,
807
if (av_open_input_file(&rtsp_st->ic, url, fmt, 0, NULL) < 0) {
1017
if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
808
1018
err = AVERROR_INVALIDDATA;
1024
/* open the RTP context */
1026
if (rtsp_st->stream_index >= 0)
1027
st = s->streams[rtsp_st->stream_index];
1029
s->ctx_flags |= AVFMTCTX_NOHEADER;
1030
rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
1032
if (!rtsp_st->rtp_ctx) {
1033
err = AVERROR_NOMEM;
1036
if(rtsp_st->dynamic_handler) {
1037
rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
1038
rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
816
1043
/* use callback if available to extend setup */
817
1044
if (ff_rtsp_callback) {
818
if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
1045
if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
819
1046
NULL, 0, rt->last_reply) < 0) {
820
1047
err = AVERROR_INVALIDDATA;
826
snprintf(cmd, sizeof(cmd),
827
"PLAY %s RTSP/1.0\r\n"
830
rtsp_send_cmd(s, cmd, reply, NULL);
831
if (reply->status_code != RTSP_STATUS_OK) {
832
err = AVERROR_INVALIDDATA;
837
/* open TCP with bufferized input */
838
if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
839
if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
1053
rt->state = RTSP_STATE_IDLE;
1054
rt->seek_timestamp = 0; /* default is to start stream at position
1056
if (ap->initial_pause) {
1057
/* do not start immediately */
1059
if (rtsp_read_play(s) < 0) {
1060
err = AVERROR_INVALIDDATA;
848
for(i=0;i<s->nb_streams;i++) {
850
rtsp_st = st->priv_data;
853
av_close_input_file(rtsp_st->ic);
1066
rtsp_close_streams(rt);
857
1067
av_freep(&content);
858
1068
url_close(rt->rtsp_hd);
862
static int tcp_read_packet(AVFormatContext *s,
1072
static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
1073
uint8_t *buf, int buf_size)
865
1075
RTSPState *rt = s->priv_data;
866
1076
int id, len, i, ret;
868
1077
RTSPStream *rtsp_st;
869
uint8_t buf[RTP_MAX_PACKET_LENGTH];
871
1079
#ifdef DEBUG_RTP_TCP
872
1080
printf("tcp_read_packet:\n");
876
ret = url_read(rt->rtsp_hd, buf, 1);
1084
ret = url_readbuf(rt->rtsp_hd, buf, 1);
877
1085
#ifdef DEBUG_RTP_TCP
878
1086
printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
882
1090
if (buf[0] == '$')
885
ret = url_read(rt->rtsp_hd, buf, 3);
1093
ret = url_readbuf(rt->rtsp_hd, buf, 3);
889
1097
len = (buf[1] << 8) | buf[2];
890
1098
#ifdef DEBUG_RTP_TCP
891
1099
printf("id=%d len=%d\n", id, len);
893
if (len > RTP_MAX_PACKET_LENGTH || len < 12)
1101
if (len > buf_size || len < 12)
895
1103
/* get the data */
896
ret = url_read(rt->rtsp_hd, buf, len);
1104
ret = url_readbuf(rt->rtsp_hd, buf, len);
900
1108
/* find the matching stream */
901
for(i = 0; i < s->nb_streams; i++) {
903
rtsp_st = st->priv_data;
904
if (id >= rtsp_st->interleaved_min &&
905
id <= rtsp_st->interleaved_max)
1109
for(i = 0; i < rt->nb_rtsp_streams; i++) {
1110
rtsp_st = rt->rtsp_streams[i];
1111
if (id >= rtsp_st->interleaved_min &&
1112
id <= rtsp_st->interleaved_max)
910
ret = rtp_parse_packet(rtsp_st->ic, pkt, buf, len);
913
pkt->stream_index = i;
1117
*prtsp_st = rtsp_st;
917
/* NOTE: output one packet at a time. May need to add a small fifo */
918
static int udp_read_packet(AVFormatContext *s,
1121
static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
1122
uint8_t *buf, int buf_size)
1124
RTSPState *rt = s->priv_data;
923
1125
RTSPStream *rtsp_st;
925
1127
int fd1, fd2, fd_max, n, i, ret;
926
char buf[RTP_MAX_PACKET_LENGTH];
927
1128
struct timeval tv;
930
1131
if (url_interrupt_cb())
934
for(i = 0; i < s->nb_streams; i++) {
936
rtsp_st = st->priv_data;
1135
for(i = 0; i < rt->nb_rtsp_streams; i++) {
1136
rtsp_st = rt->rtsp_streams[i];
938
1137
/* currently, we cannot probe RTCP handle because of blocking restrictions */
939
rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
1138
rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
940
1139
if (fd1 > fd_max)
942
1141
FD_SET(fd1, &rfds);
944
/* XXX: also add proper API to abort */
946
1144
tv.tv_usec = 100 * 1000;
947
1145
n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
949
for(i = 0; i < s->nb_streams; i++) {
951
rtsp_st = st->priv_data;
953
rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
1147
for(i = 0; i < rt->nb_rtsp_streams; i++) {
1148
rtsp_st = rt->rtsp_streams[i];
1149
rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
954
1150
if (FD_ISSET(fd1, &rfds)) {
955
ret = url_read(url_fileno(&ic->pb), buf, sizeof(buf));
957
rtp_parse_packet(ic, pkt, buf, ret) == 0) {
958
pkt->stream_index = i;
1151
ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
1153
*prtsp_st = rtsp_st;
970
1165
RTSPState *rt = s->priv_data;
1166
RTSPStream *rtsp_st;
1168
uint8_t buf[RTP_MAX_PACKET_LENGTH];
1170
/* get next frames from the same RTP packet */
1172
ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
1176
} else if (ret == 1) {
1183
/* read next RTP packet */
973
1185
switch(rt->protocol) {
975
1187
case RTSP_PROTOCOL_RTP_TCP:
976
ret = tcp_read_packet(s, pkt);
1188
len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
978
1190
case RTSP_PROTOCOL_RTP_UDP:
979
ret = udp_read_packet(s, pkt);
1191
case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
1192
len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1193
if (rtsp_st->rtp_ctx)
1194
rtp_check_and_send_back_rr(rtsp_st->rtp_ctx, len);
1199
ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
1203
/* more packets may follow, so we save the RTP context */
1204
rt->cur_rtp = rtsp_st->rtp_ctx;
1209
static int rtsp_read_play(AVFormatContext *s)
1211
RTSPState *rt = s->priv_data;
1212
RTSPHeader reply1, *reply = &reply1;
1215
av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
1217
if (rt->state == RTSP_STATE_PAUSED) {
1218
snprintf(cmd, sizeof(cmd),
1219
"PLAY %s RTSP/1.0\r\n",
1222
snprintf(cmd, sizeof(cmd),
1223
"PLAY %s RTSP/1.0\r\n"
1224
"Range: npt=%0.3f-\r\n",
1226
(double)rt->seek_timestamp / AV_TIME_BASE);
1228
rtsp_send_cmd(s, cmd, reply, NULL);
1229
if (reply->status_code != RTSP_STATUS_OK) {
1232
rt->state = RTSP_STATE_PLAYING;
985
1237
/* pause the stream */
986
int rtsp_pause(AVFormatContext *s)
1238
static int rtsp_read_pause(AVFormatContext *s)
1240
RTSPState *rt = s->priv_data;
989
1241
RTSPHeader reply1, *reply = &reply1;
992
if (s->iformat != &rtsp_demux)
995
1244
rt = s->priv_data;
997
snprintf(cmd, sizeof(cmd),
1246
if (rt->state != RTSP_STATE_PLAYING)
1249
snprintf(cmd, sizeof(cmd),
998
1250
"PAUSE %s RTSP/1.0\r\n",
1000
1252
rtsp_send_cmd(s, cmd, reply, NULL);
1001
1253
if (reply->status_code != RTSP_STATUS_OK) {
1256
rt->state = RTSP_STATE_PAUSED;
1008
/* resume the stream */
1009
int rtsp_resume(AVFormatContext *s)
1261
static int rtsp_read_seek(AVFormatContext *s, int stream_index,
1262
int64_t timestamp, int flags)
1012
RTSPHeader reply1, *reply = &reply1;
1264
RTSPState *rt = s->priv_data;
1015
if (s->iformat != &rtsp_demux)
1020
snprintf(cmd, sizeof(cmd),
1021
"PLAY %s RTSP/1.0\r\n",
1023
rtsp_send_cmd(s, cmd, reply, NULL);
1024
if (reply->status_code != RTSP_STATUS_OK) {
1266
rt->seek_timestamp = timestamp;
1269
case RTSP_STATE_IDLE:
1271
case RTSP_STATE_PLAYING:
1272
if (rtsp_read_play(s) != 0)
1275
case RTSP_STATE_PAUSED:
1276
rt->state = RTSP_STATE_IDLE;
1031
1282
static int rtsp_read_close(AVFormatContext *s)
1033
1284
RTSPState *rt = s->priv_data;
1035
RTSPStream *rtsp_st;
1036
1285
RTSPHeader reply1, *reply = &reply1;
1038
1286
char cmd[1024];
1121
1364
av_free(content);
1123
1366
/* open each RTP stream */
1124
for(i=0;i<s->nb_streams;i++) {
1126
rtsp_st = st->priv_data;
1128
snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
1129
inet_ntoa(rtsp_st->sdp_ip),
1367
for(i=0;i<rt->nb_rtsp_streams;i++) {
1368
rtsp_st = rt->rtsp_streams[i];
1370
snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
1371
inet_ntoa(rtsp_st->sdp_ip),
1130
1372
rtsp_st->sdp_port,
1131
1373
rtsp_st->sdp_ttl);
1132
if (av_open_input_file(&rtsp_st->ic, url, &rtp_demux, 0, NULL) < 0) {
1374
if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
1133
1375
err = AVERROR_INVALIDDATA;
1378
/* open the RTP context */
1380
if (rtsp_st->stream_index >= 0)
1381
st = s->streams[rtsp_st->stream_index];
1383
s->ctx_flags |= AVFMTCTX_NOHEADER;
1384
rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
1385
if (!rtsp_st->rtp_ctx) {
1386
err = AVERROR_NOMEM;
1389
if(rtsp_st->dynamic_handler) {
1390
rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
1391
rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
1139
for(i=0;i<s->nb_streams;i++) {
1141
rtsp_st = st->priv_data;
1144
av_close_input_file(rtsp_st->ic);
1397
rtsp_close_streams(rt);
1151
1401
static int sdp_read_packet(AVFormatContext *s,
1154
return udp_read_packet(s, pkt);
1404
return rtsp_read_packet(s, pkt);
1157
1407
static int sdp_read_close(AVFormatContext *s)
1160
RTSPStream *rtsp_st;
1163
for(i=0;i<s->nb_streams;i++) {
1165
rtsp_st = st->priv_data;
1168
av_close_input_file(rtsp_st->ic);
1409
RTSPState *rt = s->priv_data;
1410
rtsp_close_streams(rt);
1176
static AVInputFormat sdp_demux = {
1414
#ifdef CONFIG_SDP_DEMUXER
1415
AVInputFormat sdp_demuxer = {
1179
1418
sizeof(RTSPState),