Mercurial > mplayer.hg
annotate libmpdemux/realrtsp/rtsp.c @ 16715:66d2212ef659
sync with 1.26
author | diego |
---|---|
date | Sun, 09 Oct 2005 11:35:24 +0000 |
parents | b313a38c69cb |
children | 7d0773bb1e42 |
rev | line source |
---|---|
9922 | 1 /* |
2 * This file was ported to MPlayer from xine CVS rtsp.c,v 1.9 2003/04/10 02:30:48 | |
3 */ | |
4 | |
5 /* | |
6 * Copyright (C) 2000-2002 the xine project | |
7 * | |
8 * This file is part of xine, a free video player. | |
9 * | |
10 * xine is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * xine is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
23 * | |
24 * | |
25 * a minimalistic implementation of rtsp protocol, | |
26 * *not* RFC 2326 compilant yet. | |
27 */ | |
28 | |
29 #include <unistd.h> | |
30 #include <stdio.h> | |
31 #include <assert.h> | |
10281 | 32 #include "config.h" |
33 #ifndef HAVE_WINSOCK2 | |
34 #define closesocket close | |
9922 | 35 #include <sys/socket.h> |
36 #include <netinet/in.h> | |
37 #include <netdb.h> | |
10281 | 38 #else |
39 #include <winsock2.h> | |
40 #endif | |
9922 | 41 #include <string.h> |
42 #include <sys/stat.h> | |
43 #include <fcntl.h> | |
44 #include <errno.h> | |
45 #include <stdlib.h> | |
46 #include <time.h> | |
47 #include <sys/time.h> | |
48 #include <sys/types.h> | |
9939
6b88f67564ce
Fix compilation for *BSD, Mac OS X and maybe others (info by Steven M. Schultz and Dan Christiansen)
rtognimp
parents:
9922
diff
changeset
|
49 #include <inttypes.h> |
9922 | 50 |
51 #include "rtsp.h" | |
15585 | 52 #include "../stream.h" |
53 #include "../demuxer.h" | |
54 #include "rtsp_session.h" | |
9922 | 55 |
56 /* | |
57 #define LOG | |
58 */ | |
59 | |
60 #define BUF_SIZE 4096 | |
61 #define HEADER_SIZE 1024 | |
62 #define MAX_FIELDS 256 | |
63 | |
15585 | 64 extern int network_bandwidth; |
9922 | 65 struct rtsp_s { |
66 | |
67 int s; | |
68 | |
69 char *host; | |
70 int port; | |
71 char *path; | |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
72 char *param; |
9922 | 73 char *mrl; |
74 char *user_agent; | |
75 | |
76 char *server; | |
77 unsigned int server_state; | |
78 uint32_t server_caps; | |
79 | |
80 unsigned int cseq; | |
81 char *session; | |
82 | |
83 char *answers[MAX_FIELDS]; /* data of last message */ | |
84 char *scheduled[MAX_FIELDS]; /* will be sent with next message */ | |
85 }; | |
86 | |
87 /* | |
88 * constants | |
89 */ | |
90 | |
91 const char rtsp_protocol_version[]="RTSP/1.0"; | |
92 | |
93 /* server states */ | |
94 #define RTSP_CONNECTED 1 | |
95 #define RTSP_INIT 2 | |
96 #define RTSP_READY 4 | |
97 #define RTSP_PLAYING 8 | |
98 #define RTSP_RECORDING 16 | |
99 | |
100 /* server capabilities */ | |
101 #define RTSP_OPTIONS 0x001 | |
102 #define RTSP_DESCRIBE 0x002 | |
103 #define RTSP_ANNOUNCE 0x004 | |
104 #define RTSP_SETUP 0x008 | |
105 #define RTSP_GET_PARAMETER 0x010 | |
106 #define RTSP_SET_PARAMETER 0x020 | |
107 #define RTSP_TEARDOWN 0x040 | |
108 #define RTSP_PLAY 0x080 | |
109 #define RTSP_RECORD 0x100 | |
110 | |
111 /* | |
112 * network utilities | |
113 */ | |
114 | |
115 static int host_connect_attempt(struct in_addr ia, int port) { | |
116 | |
117 int s; | |
118 struct sockaddr_in sin; | |
119 | |
120 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
121 if (s == -1) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
122 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: socket(): %s\n", strerror(errno)); |
9922 | 123 return -1; |
124 } | |
125 | |
126 sin.sin_family = AF_INET; | |
127 sin.sin_addr = ia; | |
128 sin.sin_port = htons(port); | |
129 | |
130 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1 | |
10281 | 131 #ifndef HAVE_WINSOCK2 |
9922 | 132 && errno != EINPROGRESS) { |
10281 | 133 #else |
134 && WSAGetLastError() == WSAEINPROGRESS) { | |
135 #endif | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
136 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: connect(): %s\n", strerror(errno)); |
10281 | 137 closesocket(s); |
9922 | 138 return -1; |
139 } | |
140 | |
141 return s; | |
142 } | |
143 | |
144 static int host_connect(const char *host, int port) { | |
145 | |
146 struct hostent *h; | |
147 int i, s; | |
148 | |
149 h = gethostbyname(host); | |
150 if (h == NULL) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
151 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: unable to resolve '%s'.\n", host); |
9922 | 152 return -1; |
153 } | |
154 | |
155 for (i = 0; h->h_addr_list[i]; i++) { | |
156 struct in_addr ia; | |
157 | |
158 memcpy (&ia, h->h_addr_list[i], 4); | |
159 s = host_connect_attempt(ia, port); | |
160 if(s != -1) | |
161 return s; | |
162 } | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
163 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: unable to connect to '%s'.\n", host); |
9922 | 164 return -1; |
165 } | |
166 | |
167 static int write_stream(int s, const char *buf, int len) { | |
168 int total, timeout; | |
169 | |
170 total = 0; timeout = 30; | |
171 while (total < len){ | |
172 int n; | |
173 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9939
diff
changeset
|
174 n = send (s, &buf[total], len - total, 0); |
9922 | 175 |
176 if (n > 0) | |
177 total += n; | |
178 else if (n < 0) { | |
10281 | 179 #ifndef HAVE_WINSOCK2 |
9922 | 180 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) { |
10281 | 181 #else |
182 if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) { | |
183 #endif | |
16372
b313a38c69cb
replace sleep with usec_sleep, required for recent mingw versions, patch by Robert Swain <robert.swain at gmail.com>
faust3
parents:
15797
diff
changeset
|
184 usec_sleep (1000000); timeout--; |
9922 | 185 } else |
186 return -1; | |
187 } | |
188 } | |
189 | |
190 return total; | |
191 } | |
192 | |
193 static ssize_t read_stream(int fd, void *buf, size_t count) { | |
194 | |
195 ssize_t ret, total; | |
196 | |
197 total = 0; | |
198 | |
199 while (total < count) { | |
200 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9939
diff
changeset
|
201 ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); |
9922 | 202 |
203 if (ret<0) { | |
204 if(errno == EAGAIN) { | |
205 fd_set rset; | |
206 struct timeval timeout; | |
207 | |
208 FD_ZERO (&rset); | |
209 FD_SET (fd, &rset); | |
210 | |
211 timeout.tv_sec = 30; | |
212 timeout.tv_usec = 0; | |
213 | |
214 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { | |
215 return -1; | |
216 } | |
217 continue; | |
218 } | |
219 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
220 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: read error.\n"); |
9922 | 221 return ret; |
222 } else | |
223 total += ret; | |
224 | |
225 /* end of stream */ | |
226 if (!ret) break; | |
227 } | |
228 | |
229 return total; | |
230 } | |
231 | |
232 /* | |
233 * debugging utilities | |
234 */ | |
235 #if 0 | |
236 static void hexdump (char *buf, int length) { | |
237 | |
238 int i; | |
239 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
240 mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: ascii>"); |
9922 | 241 for (i = 0; i < length; i++) { |
242 unsigned char c = buf[i]; | |
243 | |
244 if ((c >= 32) && (c <= 128)) | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
245 mp_msg(MSGT_OPEN, MSGL_INFO, "%c", c); |
9922 | 246 else |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
247 mp_msg(MSGT_OPEN, MSGL_INFO, "."); |
9922 | 248 } |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
249 mp_msg(MSGT_OPEN, MSGL_INFO, "\n"); |
9922 | 250 |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
251 mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: hexdump> "); |
9922 | 252 for (i = 0; i < length; i++) { |
253 unsigned char c = buf[i]; | |
254 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
255 mp_msg(MSGT_OPEN, MSGL_INFO, "%02x", c); |
9922 | 256 |
257 if ((i % 16) == 15) | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
258 mp_msg(MSGT_OPEN, MSGL_INFO, "\nrtsp: "); |
9922 | 259 |
260 if ((i % 2) == 1) | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
261 mp_msg(MSGT_OPEN, MSGL_INFO, " "); |
9922 | 262 |
263 } | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
264 mp_msg(MSGT_OPEN, MSGL_INFO, "\n"); |
9922 | 265 } |
266 #endif | |
267 | |
268 /* | |
269 * rtsp_get gets a line from stream | |
270 * and returns a null terminated string. | |
271 */ | |
272 | |
273 static char *rtsp_get(rtsp_t *s) { | |
274 | |
15797 | 275 int n=1; |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
276 char *buffer = malloc(BUF_SIZE); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
277 char *string = NULL; |
9922 | 278 |
15797 | 279 read_stream(s->s, buffer, 1); |
9922 | 280 while (n<BUF_SIZE) { |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
281 read_stream(s->s, &(buffer[n]), 1); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
282 if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break; |
9922 | 283 n++; |
284 } | |
285 | |
286 if (n>=BUF_SIZE) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
287 mp_msg(MSGT_OPEN, MSGL_FATAL, "librtsp: buffer overflow in rtsp_get\n"); |
9922 | 288 exit(1); |
289 } | |
290 string=malloc(sizeof(char)*n); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
291 memcpy(string,buffer,n-1); |
9922 | 292 string[n-1]=0; |
293 | |
294 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
295 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << '%s'\n", string); |
9922 | 296 #endif |
297 | |
298 | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
299 free(buffer); |
9922 | 300 return string; |
301 } | |
302 | |
303 /* | |
304 * rtsp_put puts a line on stream | |
305 */ | |
306 | |
307 static void rtsp_put(rtsp_t *s, const char *string) { | |
308 | |
309 int len=strlen(string); | |
310 char *buf=malloc(sizeof(char)*len+2); | |
311 | |
312 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
313 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: >> '%s'", string); |
9922 | 314 #endif |
315 | |
316 memcpy(buf,string,len); | |
317 buf[len]=0x0d; | |
318 buf[len+1]=0x0a; | |
319 | |
320 write_stream(s->s, buf, len+2); | |
321 | |
322 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
323 mp_msg(MSGT_OPEN, MSGL_INFO, " done.\n"); |
9922 | 324 #endif |
325 | |
326 free(buf); | |
327 } | |
328 | |
329 /* | |
330 * extract server status code | |
331 */ | |
332 | |
333 static int rtsp_get_code(const char *string) { | |
334 | |
335 char buf[4]; | |
336 int code=0; | |
337 | |
338 if (!strncmp(string, rtsp_protocol_version, strlen(rtsp_protocol_version))) | |
339 { | |
340 memcpy(buf, string+strlen(rtsp_protocol_version)+1, 3); | |
341 buf[3]=0; | |
342 code=atoi(buf); | |
343 } else if (!strncmp(string, "SET_PARAMETER",8)) | |
344 { | |
345 return RTSP_STATUS_SET_PARAMETER; | |
346 } | |
347 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
348 if(code != 200) mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: server responds: '%s'\n",string); |
9922 | 349 |
350 return code; | |
351 } | |
352 | |
353 /* | |
354 * send a request | |
355 */ | |
356 | |
357 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) { | |
358 | |
359 char **payload=s->scheduled; | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
360 char *buf; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
361 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
362 buf = malloc(strlen(type)+strlen(what)+strlen(rtsp_protocol_version)+3); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
363 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
364 sprintf(buf,"%s %s %s",type, what, rtsp_protocol_version); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
365 rtsp_put(s,buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
366 free(buf); |
9922 | 367 if (payload) |
368 while (*payload) { | |
369 rtsp_put(s,*payload); | |
370 payload++; | |
371 } | |
372 rtsp_put(s,""); | |
373 rtsp_unschedule_all(s); | |
374 } | |
375 | |
376 /* | |
377 * schedule standard fields | |
378 */ | |
379 | |
380 static void rtsp_schedule_standard(rtsp_t *s) { | |
381 | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
382 char tmp[16]; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
383 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
384 snprintf(tmp, 16, "Cseq: %u", s->cseq); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
385 rtsp_schedule_field(s, tmp); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
386 |
9922 | 387 if (s->session) { |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
388 char *buf; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
389 buf = malloc(strlen(s->session)+15); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
390 sprintf(buf, "Session: %s", s->session); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
391 rtsp_schedule_field(s, buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
392 free(buf); |
9922 | 393 } |
394 } | |
395 /* | |
396 * get the answers, if server responses with something != 200, return NULL | |
397 */ | |
398 | |
399 static int rtsp_get_answers(rtsp_t *s) { | |
400 | |
401 char *answer=NULL; | |
402 unsigned int answer_seq; | |
403 char **answer_ptr=s->answers; | |
404 int code; | |
15172
56541efe420b
Fix potential buffer overflow if server answers with too many lines
rtognimp
parents:
13677
diff
changeset
|
405 int ans_count = 0; |
9922 | 406 |
407 answer=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
408 if (!answer) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
409 return 0; |
9922 | 410 code=rtsp_get_code(answer); |
411 free(answer); | |
412 | |
413 rtsp_free_answers(s); | |
414 | |
415 do { /* while we get answer lines */ | |
416 | |
417 answer=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
418 if (!answer) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
419 return 0; |
9922 | 420 |
421 if (!strncmp(answer,"Cseq:",5)) { | |
422 sscanf(answer,"Cseq: %u",&answer_seq); | |
423 if (s->cseq != answer_seq) { | |
424 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
425 mp_msg(MSGT_OPEN, MSGL_WARN, "librtsp: warning: Cseq mismatch. got %u, assumed %u", answer_seq, s->cseq); |
9922 | 426 #endif |
427 s->cseq=answer_seq; | |
428 } | |
429 } | |
430 if (!strncmp(answer,"Server:",7)) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
431 char *buf = malloc(strlen(answer)); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
432 sscanf(answer,"Server: %s",buf); |
9922 | 433 if (s->server) free(s->server); |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
434 s->server=strdup(buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
435 free(buf); |
9922 | 436 } |
437 if (!strncmp(answer,"Session:",8)) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
438 char *buf = calloc(1, strlen(answer)); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
439 sscanf(answer,"Session: %s",buf); |
9922 | 440 if (s->session) { |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
441 if (strcmp(buf, s->session)) { |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
442 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: setting NEW session: %s\n", buf); |
9922 | 443 free(s->session); |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
444 s->session=strdup(buf); |
9922 | 445 } |
446 } else | |
447 { | |
448 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
449 mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: setting session id to: %s\n", buf); |
9922 | 450 #endif |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
451 s->session=strdup(buf); |
9922 | 452 } |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
453 free(buf); |
9922 | 454 } |
455 *answer_ptr=answer; | |
456 answer_ptr++; | |
15172
56541efe420b
Fix potential buffer overflow if server answers with too many lines
rtognimp
parents:
13677
diff
changeset
|
457 } while ((strlen(answer)!=0) && (++ans_count < MAX_FIELDS)); |
9922 | 458 |
459 s->cseq++; | |
460 | |
461 *answer_ptr=NULL; | |
462 rtsp_schedule_standard(s); | |
463 | |
464 return code; | |
465 } | |
466 | |
467 /* | |
468 * send an ok message | |
469 */ | |
470 | |
471 int rtsp_send_ok(rtsp_t *s) { | |
472 char cseq[16]; | |
473 | |
474 rtsp_put(s, "RTSP/1.0 200 OK"); | |
475 sprintf(cseq,"CSeq: %u", s->cseq); | |
476 rtsp_put(s, cseq); | |
477 rtsp_put(s, ""); | |
478 return 0; | |
479 } | |
480 | |
481 /* | |
482 * implementation of must-have rtsp requests; functions return | |
483 * server status code. | |
484 */ | |
485 | |
486 int rtsp_request_options(rtsp_t *s, const char *what) { | |
487 | |
488 char *buf; | |
489 | |
490 if (what) { | |
491 buf=strdup(what); | |
492 } else | |
493 { | |
494 buf=malloc(sizeof(char)*(strlen(s->host)+16)); | |
495 sprintf(buf,"rtsp://%s:%i", s->host, s->port); | |
496 } | |
497 rtsp_send_request(s,"OPTIONS",buf); | |
498 free(buf); | |
499 | |
500 return rtsp_get_answers(s); | |
501 } | |
502 | |
503 int rtsp_request_describe(rtsp_t *s, const char *what) { | |
504 | |
505 char *buf; | |
506 | |
507 if (what) { | |
508 buf=strdup(what); | |
509 } else | |
510 { | |
511 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
512 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
513 } | |
514 rtsp_send_request(s,"DESCRIBE",buf); | |
515 free(buf); | |
516 | |
517 return rtsp_get_answers(s); | |
518 } | |
519 | |
520 int rtsp_request_setup(rtsp_t *s, const char *what) { | |
521 | |
522 rtsp_send_request(s,"SETUP",what); | |
523 | |
524 return rtsp_get_answers(s); | |
525 } | |
526 | |
527 int rtsp_request_setparameter(rtsp_t *s, const char *what) { | |
528 | |
529 char *buf; | |
530 | |
531 if (what) { | |
532 buf=strdup(what); | |
533 } else | |
534 { | |
535 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
536 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
537 } | |
538 rtsp_send_request(s,"SET_PARAMETER",buf); | |
539 free(buf); | |
540 | |
541 return rtsp_get_answers(s); | |
542 } | |
543 | |
544 int rtsp_request_play(rtsp_t *s, const char *what) { | |
545 | |
546 char *buf; | |
547 | |
548 if (what) { | |
549 buf=strdup(what); | |
550 } else | |
551 { | |
552 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
553 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
554 } | |
555 rtsp_send_request(s,"PLAY",buf); | |
556 free(buf); | |
557 | |
558 return rtsp_get_answers(s); | |
559 } | |
560 | |
561 int rtsp_request_tearoff(rtsp_t *s, const char *what) { | |
562 | |
563 rtsp_send_request(s,"TEAROFF",what); | |
564 | |
565 return rtsp_get_answers(s); | |
566 } | |
567 | |
568 /* | |
569 * read opaque data from stream | |
570 */ | |
571 | |
572 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) { | |
573 | |
574 int i,seq; | |
575 | |
576 if (size>=4) { | |
577 i=read_stream(s->s, buffer, 4); | |
578 if (i<4) return i; | |
579 if ((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) | |
580 { | |
581 char *rest=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
582 if (!rest) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
583 return -1; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
584 |
9922 | 585 seq=-1; |
586 do { | |
587 free(rest); | |
588 rest=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
589 if (!rest) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
590 return -1; |
9922 | 591 if (!strncmp(rest,"Cseq:",5)) |
592 sscanf(rest,"Cseq: %u",&seq); | |
593 } while (strlen(rest)!=0); | |
594 free(rest); | |
595 if (seq<0) { | |
596 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
597 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: cseq not recognized!\n"); |
9922 | 598 #endif |
599 seq=1; | |
600 } | |
11000 | 601 /* let's make the server happy */ |
9922 | 602 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood"); |
603 rest=malloc(sizeof(char)*16); | |
604 sprintf(rest,"CSeq: %u", seq); | |
605 rtsp_put(s, rest); | |
606 rtsp_put(s, ""); | |
607 i=read_stream(s->s, buffer, size); | |
608 } else | |
609 { | |
610 i=read_stream(s->s, buffer+4, size-4); | |
611 i+=4; | |
612 } | |
613 } else | |
614 i=read_stream(s->s, buffer, size); | |
615 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
616 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << %d of %d bytes\n", i, size); |
9922 | 617 #endif |
618 | |
619 return i; | |
620 } | |
621 | |
622 /* | |
623 * connect to a rtsp server | |
624 */ | |
625 | |
626 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) { | |
627 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) { | |
628 | |
629 rtsp_t *s=malloc(sizeof(rtsp_t)); | |
630 int i; | |
631 | |
632 for (i=0; i<MAX_FIELDS; i++) { | |
633 s->answers[i]=NULL; | |
634 s->scheduled[i]=NULL; | |
635 } | |
636 | |
637 s->server=NULL; | |
638 s->server_state=0; | |
639 s->server_caps=0; | |
640 | |
12678
9cdff452833b
cseq starts from 1 according to the standard, patch by Yoshinori Sato
alex
parents:
12271
diff
changeset
|
641 s->cseq=1; |
9922 | 642 s->session=NULL; |
643 | |
644 if (user_agent) | |
645 s->user_agent=strdup(user_agent); | |
646 else | |
647 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)"); | |
648 | |
649 s->mrl = strdup(mrl); | |
650 s->host = strdup(host); | |
651 s->port = port; | |
11663
d700a93ab34c
10l let path behave like before the start/stop patch
rtognimp
parents:
11506
diff
changeset
|
652 s->path = strdup(path); |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
653 while (*path == '/') |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
654 path++; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
655 if ((s->param = strchr(s->path, '?')) != NULL) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
656 s->param++; |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
657 //mp_msg(MSGT_OPEN, MSGL_INFO, "path=%s\n", s->path); |
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
658 //mp_msg(MSGT_OPEN, MSGL_INFO, "param=%s\n", s->param ? s->param : "NULL"); |
9922 | 659 s->s = fd; |
660 | |
661 if (s->s < 0) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
662 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: failed to connect to '%s'\n", s->host); |
9922 | 663 rtsp_close(s); |
664 return NULL; | |
665 } | |
666 | |
667 s->server_state=RTSP_CONNECTED; | |
668 | |
11000 | 669 /* now let's send an options request. */ |
9922 | 670 rtsp_schedule_field(s, "CSeq: 1"); |
671 rtsp_schedule_field(s, s->user_agent); | |
672 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7"); | |
673 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]"); | |
674 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw=="); | |
675 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000"); | |
676 rtsp_schedule_field(s, "RegionData: 0"); | |
677 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586"); | |
678 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/ | |
679 rtsp_request_options(s, NULL); | |
680 | |
681 return s; | |
682 } | |
683 | |
684 | |
685 /* | |
686 * closes an rtsp connection | |
687 */ | |
688 | |
689 void rtsp_close(rtsp_t *s) { | |
690 | |
10281 | 691 if (s->server_state) closesocket(s->s); /* TODO: send a TEAROFF */ |
9922 | 692 if (s->path) free(s->path); |
693 if (s->host) free(s->host); | |
694 if (s->mrl) free(s->mrl); | |
695 if (s->session) free(s->session); | |
696 if (s->user_agent) free(s->user_agent); | |
697 rtsp_free_answers(s); | |
698 rtsp_unschedule_all(s); | |
699 free(s); | |
700 } | |
701 | |
702 /* | |
703 * search in answers for tags. returns a pointer to the content | |
704 * after the first matched tag. returns NULL if no match found. | |
705 */ | |
706 | |
707 char *rtsp_search_answers(rtsp_t *s, const char *tag) { | |
708 | |
709 char **answer; | |
710 char *ptr; | |
711 | |
712 if (!s->answers) return NULL; | |
713 answer=s->answers; | |
714 | |
715 while (*answer) { | |
716 if (!strncasecmp(*answer,tag,strlen(tag))) { | |
717 ptr=strchr(*answer,':'); | |
718 ptr++; | |
719 while(*ptr==' ') ptr++; | |
720 return ptr; | |
721 } | |
722 answer++; | |
723 } | |
724 | |
725 return NULL; | |
726 } | |
727 | |
728 /* | |
729 * session id management | |
730 */ | |
731 | |
732 void rtsp_set_session(rtsp_t *s, const char *id) { | |
733 | |
734 if (s->session) free(s->session); | |
735 | |
736 s->session=strdup(id); | |
737 | |
738 } | |
739 | |
740 char *rtsp_get_session(rtsp_t *s) { | |
741 | |
742 return s->session; | |
743 | |
744 } | |
745 | |
746 char *rtsp_get_mrl(rtsp_t *s) { | |
747 | |
748 return s->mrl; | |
749 | |
750 } | |
751 | |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
752 char *rtsp_get_param(rtsp_t *s, char *p) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
753 int len; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
754 char *param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
755 if (!s->param) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
756 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
757 if (!p) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
758 return strdup(s->param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
759 len = strlen(p); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
760 param = s->param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
761 while (param && *param) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
762 char *nparam = strchr(param, '&'); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
763 if (strncmp(param, p, len) == 0 && param[len] == '=') { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
764 param += len + 1; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
765 len = nparam ? nparam - param : strlen(param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
766 nparam = malloc(len + 1); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
767 memcpy(nparam, param, len); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
768 nparam[len] = 0; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
769 return nparam; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
770 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
771 param = nparam ? nparam + 1 : NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
772 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
773 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
774 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
775 |
9922 | 776 /* |
777 * schedules a field for transmission | |
778 */ | |
779 | |
780 void rtsp_schedule_field(rtsp_t *s, const char *string) { | |
781 | |
782 int i=0; | |
783 | |
784 if (!string) return; | |
785 | |
786 while(s->scheduled[i]) { | |
787 i++; | |
788 } | |
789 s->scheduled[i]=strdup(string); | |
790 } | |
791 | |
792 /* | |
793 * removes the first scheduled field which prefix matches string. | |
794 */ | |
795 | |
796 void rtsp_unschedule_field(rtsp_t *s, const char *string) { | |
797 | |
798 char **ptr=s->scheduled; | |
799 | |
800 if (!string) return; | |
801 | |
802 while(*ptr) { | |
803 if (!strncmp(*ptr, string, strlen(string))) | |
804 break; | |
805 } | |
806 if (*ptr) free(*ptr); | |
807 ptr++; | |
808 do { | |
809 *(ptr-1)=*ptr; | |
810 } while(*ptr); | |
811 } | |
812 | |
813 /* | |
814 * unschedule all fields | |
815 */ | |
816 | |
817 void rtsp_unschedule_all(rtsp_t *s) { | |
818 | |
819 char **ptr; | |
820 | |
821 if (!s->scheduled) return; | |
822 ptr=s->scheduled; | |
823 | |
824 while (*ptr) { | |
825 free(*ptr); | |
826 *ptr=NULL; | |
827 ptr++; | |
828 } | |
829 } | |
830 /* | |
831 * free answers | |
832 */ | |
833 | |
834 void rtsp_free_answers(rtsp_t *s) { | |
835 | |
836 char **answer; | |
837 | |
838 if (!s->answers) return; | |
839 answer=s->answers; | |
840 | |
841 while (*answer) { | |
842 free(*answer); | |
843 *answer=NULL; | |
844 answer++; | |
845 } | |
846 } | |
15585 | 847 |
848 static int realrtsp_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) { | |
849 return rtsp_session_read(stream_ctrl->data, buffer, size); | |
850 } | |
851 | |
852 | |
853 static int realrtsp_streaming_start( stream_t *stream ) { | |
854 int fd; | |
855 rtsp_session_t *rtsp; | |
856 char *mrl; | |
857 char *file; | |
858 int port; | |
859 int redirected, temp; | |
860 if( stream==NULL ) return -1; | |
861 | |
862 temp = 5; // counter so we don't get caught in infinite redirections (you never know) | |
863 | |
864 do { | |
865 redirected = 0; | |
866 | |
867 fd = connect2Server( stream->streaming_ctrl->url->hostname, | |
868 port = (stream->streaming_ctrl->url->port ? stream->streaming_ctrl->url->port : 554),1 ); | |
869 if(fd<0 && !stream->streaming_ctrl->url->port) | |
870 fd = connect2Server(stream->streaming_ctrl->url->hostname, port = 7070, 1); | |
871 if(fd<0) return -1; | |
872 | |
873 file = stream->streaming_ctrl->url->file; | |
874 if (file[0] == '/') | |
875 file++; | |
876 mrl = malloc(sizeof(char)*(strlen(stream->streaming_ctrl->url->hostname)+strlen(file)+16)); | |
877 sprintf(mrl,"rtsp://%s:%i/%s",stream->streaming_ctrl->url->hostname,port,file); | |
878 rtsp = rtsp_session_start(fd,&mrl, file, stream->streaming_ctrl->url->hostname, port, &redirected); | |
879 | |
880 if( redirected == 1) { | |
881 url_free(stream->streaming_ctrl->url); | |
882 stream->streaming_ctrl->url = url_new(mrl); | |
883 closesocket(fd); | |
884 } | |
885 | |
886 free(mrl); | |
887 temp--; | |
888 } while( (redirected != 0) && (temp > 0) ); | |
889 | |
890 if(!rtsp) return -1; | |
891 | |
892 stream->fd=fd; | |
893 stream->streaming_ctrl->data=rtsp; | |
894 | |
895 stream->streaming_ctrl->streaming_read = realrtsp_streaming_read; | |
896 //stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
897 stream->streaming_ctrl->prebuffer_size = 128*1024; // 8 KBytes | |
898 stream->streaming_ctrl->buffering = 1; | |
899 stream->streaming_ctrl->status = streaming_playing_e; | |
900 return 0; | |
901 } | |
902 | |
903 | |
904 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) { | |
905 URL_t *url; | |
906 | |
907 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_RTSP, URL: %s\n", stream->url); | |
908 stream->streaming_ctrl = streaming_ctrl_new(); | |
909 if( stream->streaming_ctrl==NULL ) | |
910 return STREAM_ERROR; | |
911 | |
912 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
913 url = url_new(stream->url); | |
914 stream->streaming_ctrl->url = check4proxies(url); | |
915 //url_free(url); | |
916 | |
917 stream->fd = -1; | |
918 if(realrtsp_streaming_start( stream ) < 0) { | |
919 streaming_ctrl_free(stream->streaming_ctrl); | |
920 stream->streaming_ctrl = NULL; | |
921 return STREAM_UNSUPORTED; | |
922 } | |
923 | |
924 fixup_network_stream_cache(stream); | |
925 stream->type = STREAMTYPE_STREAM; | |
926 return STREAM_OK; | |
927 } | |
928 | |
929 | |
930 stream_info_t stream_info_rtsp = { | |
931 "RealNetworks rtsp streaming", | |
932 "realrtsp", | |
933 "Roberto Togni, xine team", | |
934 "ported from xine", | |
935 open_s, | |
936 {"rtsp", NULL}, | |
937 NULL, | |
938 0 // Urls are an option string | |
939 }; |