Mercurial > mplayer.hg
annotate libmpdemux/realrtsp/rtsp.c @ 15789:32d9c8a0a02b
adds some more -identify output, patch by kiriuja < mplayer DASH patches PAM en DASH directo POUM net>
author | gpoirier |
---|---|
date | Mon, 20 Jun 2005 23:07:35 +0000 |
parents | 941b1a71351f |
children | 06a9914af502 |
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 | |
9922 | 184 sleep (1); timeout--; |
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 | |
275 int n=0; | |
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 |
279 while (n<BUF_SIZE) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
280 read_stream(s->s, &(buffer[n]), 1); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
281 if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break; |
9922 | 282 n++; |
283 } | |
284 | |
285 if (n>=BUF_SIZE) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
286 mp_msg(MSGT_OPEN, MSGL_FATAL, "librtsp: buffer overflow in rtsp_get\n"); |
9922 | 287 exit(1); |
288 } | |
289 string=malloc(sizeof(char)*n); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
290 memcpy(string,buffer,n-1); |
9922 | 291 string[n-1]=0; |
292 | |
293 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
294 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << '%s'\n", string); |
9922 | 295 #endif |
296 | |
297 | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
298 free(buffer); |
9922 | 299 return string; |
300 } | |
301 | |
302 /* | |
303 * rtsp_put puts a line on stream | |
304 */ | |
305 | |
306 static void rtsp_put(rtsp_t *s, const char *string) { | |
307 | |
308 int len=strlen(string); | |
309 char *buf=malloc(sizeof(char)*len+2); | |
310 | |
311 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
312 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: >> '%s'", string); |
9922 | 313 #endif |
314 | |
315 memcpy(buf,string,len); | |
316 buf[len]=0x0d; | |
317 buf[len+1]=0x0a; | |
318 | |
319 write_stream(s->s, buf, len+2); | |
320 | |
321 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
322 mp_msg(MSGT_OPEN, MSGL_INFO, " done.\n"); |
9922 | 323 #endif |
324 | |
325 free(buf); | |
326 } | |
327 | |
328 /* | |
329 * extract server status code | |
330 */ | |
331 | |
332 static int rtsp_get_code(const char *string) { | |
333 | |
334 char buf[4]; | |
335 int code=0; | |
336 | |
337 if (!strncmp(string, rtsp_protocol_version, strlen(rtsp_protocol_version))) | |
338 { | |
339 memcpy(buf, string+strlen(rtsp_protocol_version)+1, 3); | |
340 buf[3]=0; | |
341 code=atoi(buf); | |
342 } else if (!strncmp(string, "SET_PARAMETER",8)) | |
343 { | |
344 return RTSP_STATUS_SET_PARAMETER; | |
345 } | |
346 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
347 if(code != 200) mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: server responds: '%s'\n",string); |
9922 | 348 |
349 return code; | |
350 } | |
351 | |
352 /* | |
353 * send a request | |
354 */ | |
355 | |
356 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) { | |
357 | |
358 char **payload=s->scheduled; | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
359 char *buf; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
360 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
361 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
|
362 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
363 sprintf(buf,"%s %s %s",type, what, rtsp_protocol_version); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
364 rtsp_put(s,buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
365 free(buf); |
9922 | 366 if (payload) |
367 while (*payload) { | |
368 rtsp_put(s,*payload); | |
369 payload++; | |
370 } | |
371 rtsp_put(s,""); | |
372 rtsp_unschedule_all(s); | |
373 } | |
374 | |
375 /* | |
376 * schedule standard fields | |
377 */ | |
378 | |
379 static void rtsp_schedule_standard(rtsp_t *s) { | |
380 | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
381 char tmp[16]; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
382 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
383 snprintf(tmp, 16, "Cseq: %u", s->cseq); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
384 rtsp_schedule_field(s, tmp); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
385 |
9922 | 386 if (s->session) { |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
387 char *buf; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
388 buf = malloc(strlen(s->session)+15); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
389 sprintf(buf, "Session: %s", s->session); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
390 rtsp_schedule_field(s, buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
391 free(buf); |
9922 | 392 } |
393 } | |
394 /* | |
395 * get the answers, if server responses with something != 200, return NULL | |
396 */ | |
397 | |
398 static int rtsp_get_answers(rtsp_t *s) { | |
399 | |
400 char *answer=NULL; | |
401 unsigned int answer_seq; | |
402 char **answer_ptr=s->answers; | |
403 int code; | |
15172
56541efe420b
Fix potential buffer overflow if server answers with too many lines
rtognimp
parents:
13677
diff
changeset
|
404 int ans_count = 0; |
9922 | 405 |
406 answer=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
407 if (!answer) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
408 return 0; |
9922 | 409 code=rtsp_get_code(answer); |
410 free(answer); | |
411 | |
412 rtsp_free_answers(s); | |
413 | |
414 do { /* while we get answer lines */ | |
415 | |
416 answer=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
417 if (!answer) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
418 return 0; |
9922 | 419 |
420 if (!strncmp(answer,"Cseq:",5)) { | |
421 sscanf(answer,"Cseq: %u",&answer_seq); | |
422 if (s->cseq != answer_seq) { | |
423 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
424 mp_msg(MSGT_OPEN, MSGL_WARN, "librtsp: warning: Cseq mismatch. got %u, assumed %u", answer_seq, s->cseq); |
9922 | 425 #endif |
426 s->cseq=answer_seq; | |
427 } | |
428 } | |
429 if (!strncmp(answer,"Server:",7)) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
430 char *buf = malloc(strlen(answer)); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
431 sscanf(answer,"Server: %s",buf); |
9922 | 432 if (s->server) free(s->server); |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
433 s->server=strdup(buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
434 free(buf); |
9922 | 435 } |
436 if (!strncmp(answer,"Session:",8)) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
437 char *buf = calloc(1, strlen(answer)); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
438 sscanf(answer,"Session: %s",buf); |
9922 | 439 if (s->session) { |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
440 if (strcmp(buf, s->session)) { |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
441 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: setting NEW session: %s\n", buf); |
9922 | 442 free(s->session); |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
443 s->session=strdup(buf); |
9922 | 444 } |
445 } else | |
446 { | |
447 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
448 mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: setting session id to: %s\n", buf); |
9922 | 449 #endif |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
450 s->session=strdup(buf); |
9922 | 451 } |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
452 free(buf); |
9922 | 453 } |
454 *answer_ptr=answer; | |
455 answer_ptr++; | |
15172
56541efe420b
Fix potential buffer overflow if server answers with too many lines
rtognimp
parents:
13677
diff
changeset
|
456 } while ((strlen(answer)!=0) && (++ans_count < MAX_FIELDS)); |
9922 | 457 |
458 s->cseq++; | |
459 | |
460 *answer_ptr=NULL; | |
461 rtsp_schedule_standard(s); | |
462 | |
463 return code; | |
464 } | |
465 | |
466 /* | |
467 * send an ok message | |
468 */ | |
469 | |
470 int rtsp_send_ok(rtsp_t *s) { | |
471 char cseq[16]; | |
472 | |
473 rtsp_put(s, "RTSP/1.0 200 OK"); | |
474 sprintf(cseq,"CSeq: %u", s->cseq); | |
475 rtsp_put(s, cseq); | |
476 rtsp_put(s, ""); | |
477 return 0; | |
478 } | |
479 | |
480 /* | |
481 * implementation of must-have rtsp requests; functions return | |
482 * server status code. | |
483 */ | |
484 | |
485 int rtsp_request_options(rtsp_t *s, const char *what) { | |
486 | |
487 char *buf; | |
488 | |
489 if (what) { | |
490 buf=strdup(what); | |
491 } else | |
492 { | |
493 buf=malloc(sizeof(char)*(strlen(s->host)+16)); | |
494 sprintf(buf,"rtsp://%s:%i", s->host, s->port); | |
495 } | |
496 rtsp_send_request(s,"OPTIONS",buf); | |
497 free(buf); | |
498 | |
499 return rtsp_get_answers(s); | |
500 } | |
501 | |
502 int rtsp_request_describe(rtsp_t *s, const char *what) { | |
503 | |
504 char *buf; | |
505 | |
506 if (what) { | |
507 buf=strdup(what); | |
508 } else | |
509 { | |
510 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
511 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
512 } | |
513 rtsp_send_request(s,"DESCRIBE",buf); | |
514 free(buf); | |
515 | |
516 return rtsp_get_answers(s); | |
517 } | |
518 | |
519 int rtsp_request_setup(rtsp_t *s, const char *what) { | |
520 | |
521 rtsp_send_request(s,"SETUP",what); | |
522 | |
523 return rtsp_get_answers(s); | |
524 } | |
525 | |
526 int rtsp_request_setparameter(rtsp_t *s, const char *what) { | |
527 | |
528 char *buf; | |
529 | |
530 if (what) { | |
531 buf=strdup(what); | |
532 } else | |
533 { | |
534 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
535 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
536 } | |
537 rtsp_send_request(s,"SET_PARAMETER",buf); | |
538 free(buf); | |
539 | |
540 return rtsp_get_answers(s); | |
541 } | |
542 | |
543 int rtsp_request_play(rtsp_t *s, const char *what) { | |
544 | |
545 char *buf; | |
546 | |
547 if (what) { | |
548 buf=strdup(what); | |
549 } else | |
550 { | |
551 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
552 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
553 } | |
554 rtsp_send_request(s,"PLAY",buf); | |
555 free(buf); | |
556 | |
557 return rtsp_get_answers(s); | |
558 } | |
559 | |
560 int rtsp_request_tearoff(rtsp_t *s, const char *what) { | |
561 | |
562 rtsp_send_request(s,"TEAROFF",what); | |
563 | |
564 return rtsp_get_answers(s); | |
565 } | |
566 | |
567 /* | |
568 * read opaque data from stream | |
569 */ | |
570 | |
571 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) { | |
572 | |
573 int i,seq; | |
574 | |
575 if (size>=4) { | |
576 i=read_stream(s->s, buffer, 4); | |
577 if (i<4) return i; | |
578 if ((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) | |
579 { | |
580 char *rest=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
581 if (!rest) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
582 return -1; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
583 |
9922 | 584 seq=-1; |
585 do { | |
586 free(rest); | |
587 rest=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
588 if (!rest) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
589 return -1; |
9922 | 590 if (!strncmp(rest,"Cseq:",5)) |
591 sscanf(rest,"Cseq: %u",&seq); | |
592 } while (strlen(rest)!=0); | |
593 free(rest); | |
594 if (seq<0) { | |
595 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
596 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: cseq not recognized!\n"); |
9922 | 597 #endif |
598 seq=1; | |
599 } | |
11000 | 600 /* let's make the server happy */ |
9922 | 601 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood"); |
602 rest=malloc(sizeof(char)*16); | |
603 sprintf(rest,"CSeq: %u", seq); | |
604 rtsp_put(s, rest); | |
605 rtsp_put(s, ""); | |
606 i=read_stream(s->s, buffer, size); | |
607 } else | |
608 { | |
609 i=read_stream(s->s, buffer+4, size-4); | |
610 i+=4; | |
611 } | |
612 } else | |
613 i=read_stream(s->s, buffer, size); | |
614 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
615 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << %d of %d bytes\n", i, size); |
9922 | 616 #endif |
617 | |
618 return i; | |
619 } | |
620 | |
621 /* | |
622 * connect to a rtsp server | |
623 */ | |
624 | |
625 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) { | |
626 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) { | |
627 | |
628 rtsp_t *s=malloc(sizeof(rtsp_t)); | |
629 int i; | |
630 | |
631 for (i=0; i<MAX_FIELDS; i++) { | |
632 s->answers[i]=NULL; | |
633 s->scheduled[i]=NULL; | |
634 } | |
635 | |
636 s->server=NULL; | |
637 s->server_state=0; | |
638 s->server_caps=0; | |
639 | |
12678
9cdff452833b
cseq starts from 1 according to the standard, patch by Yoshinori Sato
alex
parents:
12271
diff
changeset
|
640 s->cseq=1; |
9922 | 641 s->session=NULL; |
642 | |
643 if (user_agent) | |
644 s->user_agent=strdup(user_agent); | |
645 else | |
646 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)"); | |
647 | |
648 s->mrl = strdup(mrl); | |
649 s->host = strdup(host); | |
650 s->port = port; | |
11663
d700a93ab34c
10l let path behave like before the start/stop patch
rtognimp
parents:
11506
diff
changeset
|
651 s->path = strdup(path); |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
652 while (*path == '/') |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
653 path++; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
654 if ((s->param = strchr(s->path, '?')) != NULL) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
655 s->param++; |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
656 //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
|
657 //mp_msg(MSGT_OPEN, MSGL_INFO, "param=%s\n", s->param ? s->param : "NULL"); |
9922 | 658 s->s = fd; |
659 | |
660 if (s->s < 0) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15585
diff
changeset
|
661 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: failed to connect to '%s'\n", s->host); |
9922 | 662 rtsp_close(s); |
663 return NULL; | |
664 } | |
665 | |
666 s->server_state=RTSP_CONNECTED; | |
667 | |
11000 | 668 /* now let's send an options request. */ |
9922 | 669 rtsp_schedule_field(s, "CSeq: 1"); |
670 rtsp_schedule_field(s, s->user_agent); | |
671 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7"); | |
672 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]"); | |
673 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw=="); | |
674 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000"); | |
675 rtsp_schedule_field(s, "RegionData: 0"); | |
676 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586"); | |
677 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/ | |
678 rtsp_request_options(s, NULL); | |
679 | |
680 return s; | |
681 } | |
682 | |
683 | |
684 /* | |
685 * closes an rtsp connection | |
686 */ | |
687 | |
688 void rtsp_close(rtsp_t *s) { | |
689 | |
10281 | 690 if (s->server_state) closesocket(s->s); /* TODO: send a TEAROFF */ |
9922 | 691 if (s->path) free(s->path); |
692 if (s->host) free(s->host); | |
693 if (s->mrl) free(s->mrl); | |
694 if (s->session) free(s->session); | |
695 if (s->user_agent) free(s->user_agent); | |
696 rtsp_free_answers(s); | |
697 rtsp_unschedule_all(s); | |
698 free(s); | |
699 } | |
700 | |
701 /* | |
702 * search in answers for tags. returns a pointer to the content | |
703 * after the first matched tag. returns NULL if no match found. | |
704 */ | |
705 | |
706 char *rtsp_search_answers(rtsp_t *s, const char *tag) { | |
707 | |
708 char **answer; | |
709 char *ptr; | |
710 | |
711 if (!s->answers) return NULL; | |
712 answer=s->answers; | |
713 | |
714 while (*answer) { | |
715 if (!strncasecmp(*answer,tag,strlen(tag))) { | |
716 ptr=strchr(*answer,':'); | |
717 ptr++; | |
718 while(*ptr==' ') ptr++; | |
719 return ptr; | |
720 } | |
721 answer++; | |
722 } | |
723 | |
724 return NULL; | |
725 } | |
726 | |
727 /* | |
728 * session id management | |
729 */ | |
730 | |
731 void rtsp_set_session(rtsp_t *s, const char *id) { | |
732 | |
733 if (s->session) free(s->session); | |
734 | |
735 s->session=strdup(id); | |
736 | |
737 } | |
738 | |
739 char *rtsp_get_session(rtsp_t *s) { | |
740 | |
741 return s->session; | |
742 | |
743 } | |
744 | |
745 char *rtsp_get_mrl(rtsp_t *s) { | |
746 | |
747 return s->mrl; | |
748 | |
749 } | |
750 | |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
751 char *rtsp_get_param(rtsp_t *s, char *p) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
752 int len; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
753 char *param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
754 if (!s->param) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
755 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
756 if (!p) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
757 return strdup(s->param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
758 len = strlen(p); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
759 param = s->param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
760 while (param && *param) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
761 char *nparam = strchr(param, '&'); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
762 if (strncmp(param, p, len) == 0 && param[len] == '=') { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
763 param += len + 1; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
764 len = nparam ? nparam - param : strlen(param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
765 nparam = malloc(len + 1); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
766 memcpy(nparam, param, len); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
767 nparam[len] = 0; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
768 return nparam; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
769 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
770 param = nparam ? nparam + 1 : NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
771 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
772 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
773 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
774 |
9922 | 775 /* |
776 * schedules a field for transmission | |
777 */ | |
778 | |
779 void rtsp_schedule_field(rtsp_t *s, const char *string) { | |
780 | |
781 int i=0; | |
782 | |
783 if (!string) return; | |
784 | |
785 while(s->scheduled[i]) { | |
786 i++; | |
787 } | |
788 s->scheduled[i]=strdup(string); | |
789 } | |
790 | |
791 /* | |
792 * removes the first scheduled field which prefix matches string. | |
793 */ | |
794 | |
795 void rtsp_unschedule_field(rtsp_t *s, const char *string) { | |
796 | |
797 char **ptr=s->scheduled; | |
798 | |
799 if (!string) return; | |
800 | |
801 while(*ptr) { | |
802 if (!strncmp(*ptr, string, strlen(string))) | |
803 break; | |
804 } | |
805 if (*ptr) free(*ptr); | |
806 ptr++; | |
807 do { | |
808 *(ptr-1)=*ptr; | |
809 } while(*ptr); | |
810 } | |
811 | |
812 /* | |
813 * unschedule all fields | |
814 */ | |
815 | |
816 void rtsp_unschedule_all(rtsp_t *s) { | |
817 | |
818 char **ptr; | |
819 | |
820 if (!s->scheduled) return; | |
821 ptr=s->scheduled; | |
822 | |
823 while (*ptr) { | |
824 free(*ptr); | |
825 *ptr=NULL; | |
826 ptr++; | |
827 } | |
828 } | |
829 /* | |
830 * free answers | |
831 */ | |
832 | |
833 void rtsp_free_answers(rtsp_t *s) { | |
834 | |
835 char **answer; | |
836 | |
837 if (!s->answers) return; | |
838 answer=s->answers; | |
839 | |
840 while (*answer) { | |
841 free(*answer); | |
842 *answer=NULL; | |
843 answer++; | |
844 } | |
845 } | |
15585 | 846 |
847 static int realrtsp_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) { | |
848 return rtsp_session_read(stream_ctrl->data, buffer, size); | |
849 } | |
850 | |
851 | |
852 static int realrtsp_streaming_start( stream_t *stream ) { | |
853 int fd; | |
854 rtsp_session_t *rtsp; | |
855 char *mrl; | |
856 char *file; | |
857 int port; | |
858 int redirected, temp; | |
859 if( stream==NULL ) return -1; | |
860 | |
861 temp = 5; // counter so we don't get caught in infinite redirections (you never know) | |
862 | |
863 do { | |
864 redirected = 0; | |
865 | |
866 fd = connect2Server( stream->streaming_ctrl->url->hostname, | |
867 port = (stream->streaming_ctrl->url->port ? stream->streaming_ctrl->url->port : 554),1 ); | |
868 if(fd<0 && !stream->streaming_ctrl->url->port) | |
869 fd = connect2Server(stream->streaming_ctrl->url->hostname, port = 7070, 1); | |
870 if(fd<0) return -1; | |
871 | |
872 file = stream->streaming_ctrl->url->file; | |
873 if (file[0] == '/') | |
874 file++; | |
875 mrl = malloc(sizeof(char)*(strlen(stream->streaming_ctrl->url->hostname)+strlen(file)+16)); | |
876 sprintf(mrl,"rtsp://%s:%i/%s",stream->streaming_ctrl->url->hostname,port,file); | |
877 rtsp = rtsp_session_start(fd,&mrl, file, stream->streaming_ctrl->url->hostname, port, &redirected); | |
878 | |
879 if( redirected == 1) { | |
880 url_free(stream->streaming_ctrl->url); | |
881 stream->streaming_ctrl->url = url_new(mrl); | |
882 closesocket(fd); | |
883 } | |
884 | |
885 free(mrl); | |
886 temp--; | |
887 } while( (redirected != 0) && (temp > 0) ); | |
888 | |
889 if(!rtsp) return -1; | |
890 | |
891 stream->fd=fd; | |
892 stream->streaming_ctrl->data=rtsp; | |
893 | |
894 stream->streaming_ctrl->streaming_read = realrtsp_streaming_read; | |
895 //stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
896 stream->streaming_ctrl->prebuffer_size = 128*1024; // 8 KBytes | |
897 stream->streaming_ctrl->buffering = 1; | |
898 stream->streaming_ctrl->status = streaming_playing_e; | |
899 return 0; | |
900 } | |
901 | |
902 | |
903 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) { | |
904 URL_t *url; | |
905 | |
906 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_RTSP, URL: %s\n", stream->url); | |
907 stream->streaming_ctrl = streaming_ctrl_new(); | |
908 if( stream->streaming_ctrl==NULL ) | |
909 return STREAM_ERROR; | |
910 | |
911 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
912 url = url_new(stream->url); | |
913 stream->streaming_ctrl->url = check4proxies(url); | |
914 //url_free(url); | |
915 | |
916 stream->fd = -1; | |
917 if(realrtsp_streaming_start( stream ) < 0) { | |
918 streaming_ctrl_free(stream->streaming_ctrl); | |
919 stream->streaming_ctrl = NULL; | |
920 return STREAM_UNSUPORTED; | |
921 } | |
922 | |
923 fixup_network_stream_cache(stream); | |
924 stream->type = STREAMTYPE_STREAM; | |
925 return STREAM_OK; | |
926 } | |
927 | |
928 | |
929 stream_info_t stream_info_rtsp = { | |
930 "RealNetworks rtsp streaming", | |
931 "realrtsp", | |
932 "Roberto Togni, xine team", | |
933 "ported from xine", | |
934 open_s, | |
935 {"rtsp", NULL}, | |
936 NULL, | |
937 0 // Urls are an option string | |
938 }; |