Mercurial > mplayer.hg
annotate stream/librtsp/rtsp.c @ 36428:7766530caa76
Add new stream control command STREAM_CTRL_GET_CURRENT_CHANNEL.
This provides the name of the currently selected DVB channel.
(Unsupported for analog TV though.)
author | ib |
---|---|
date | Fri, 29 Nov 2013 12:34:21 +0000 |
parents | 389d43c448b3 |
children |
rev | line source |
---|---|
18799 | 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 * 2006, Benjamin Zores and Vincent Mussard | |
29 * fixed a lot of RFC compliance issues. | |
30 */ | |
31 | |
32 #include <unistd.h> | |
33 #include <stdio.h> | |
34 #include <assert.h> | |
35 #include "config.h" | |
36 #include <string.h> | |
35903 | 37 #include <strings.h> |
18799 | 38 #include <sys/stat.h> |
39 #include <fcntl.h> | |
40 #include <errno.h> | |
41 #include <stdlib.h> | |
42 #include <time.h> | |
43 #include <sys/time.h> | |
44 #include <sys/types.h> | |
45 #include <inttypes.h> | |
28402 | 46 #if HAVE_WINSOCK2_H |
27109 | 47 #include <winsock2.h> |
27320
44341e4d5621
Do not include sys/socket.h when using winsock2, it is pointless
reimar
parents:
27202
diff
changeset
|
48 #else |
44341e4d5621
Do not include sys/socket.h when using winsock2, it is pointless
reimar
parents:
27202
diff
changeset
|
49 #include <sys/socket.h> |
27109 | 50 #endif |
18799 | 51 #include "mp_msg.h" |
52 #include "rtsp.h" | |
53 #include "rtsp_session.h" | |
54 #include "osdep/timer.h" | |
31500 | 55 #include "stream/network.h" |
18799 | 56 |
57 /* | |
58 #define LOG | |
59 */ | |
60 | |
61 /* | |
62 * network utilities | |
63 */ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
64 |
18799 | 65 static int write_stream(int s, const char *buf, int len) { |
66 int total, timeout; | |
67 | |
68 total = 0; timeout = 30; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
69 while (total < len){ |
18799 | 70 int n; |
71 | |
31500 | 72 n = send (s, &buf[total], len - total, DEFAULT_SEND_FLAGS); |
18799 | 73 |
74 if (n > 0) | |
75 total += n; | |
76 else if (n < 0) { | |
28402 | 77 #if !HAVE_WINSOCK2_H |
18799 | 78 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) { |
79 #else | |
80 if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) { | |
81 #endif | |
82 usec_sleep (1000000); timeout--; | |
83 } else | |
84 return -1; | |
85 } | |
86 } | |
87 | |
88 return total; | |
89 } | |
90 | |
91 static ssize_t read_stream(int fd, void *buf, size_t count) { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
92 |
18799 | 93 ssize_t ret, total; |
94 | |
95 total = 0; | |
96 | |
97 while (total < count) { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
98 |
18799 | 99 ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); |
100 | |
101 if (ret<0) { | |
102 if(errno == EAGAIN) { | |
103 fd_set rset; | |
104 struct timeval timeout; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
105 |
18799 | 106 FD_ZERO (&rset); |
107 FD_SET (fd, &rset); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
108 |
18799 | 109 timeout.tv_sec = 30; |
110 timeout.tv_usec = 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
111 |
18799 | 112 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { |
113 return -1; | |
114 } | |
115 continue; | |
116 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
117 |
18799 | 118 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: read error.\n"); |
119 return ret; | |
120 } else | |
121 total += ret; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
122 |
18799 | 123 /* end of stream */ |
124 if (!ret) break; | |
125 } | |
126 | |
127 return total; | |
128 } | |
129 | |
130 /* | |
131 * rtsp_get gets a line from stream | |
132 * and returns a null terminated string. | |
133 */ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
134 |
18799 | 135 static char *rtsp_get(rtsp_t *s) { |
136 | |
137 int n=1; | |
138 char *buffer = malloc(BUF_SIZE); | |
139 char *string = NULL; | |
140 | |
141 read_stream(s->s, buffer, 1); | |
142 while (n<BUF_SIZE) { | |
143 read_stream(s->s, &(buffer[n]), 1); | |
144 if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break; | |
145 n++; | |
146 } | |
147 | |
148 if (n>=BUF_SIZE) { | |
149 mp_msg(MSGT_OPEN, MSGL_FATAL, "librtsp: buffer overflow in rtsp_get\n"); | |
150 exit(1); | |
151 } | |
18854
a973acb2e572
cosmetic patch to remove useless sizeof(char) statements
ben
parents:
18853
diff
changeset
|
152 string=malloc(n); |
18799 | 153 memcpy(string,buffer,n-1); |
154 string[n-1]=0; | |
155 | |
156 #ifdef LOG | |
157 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << '%s'\n", string); | |
158 #endif | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
159 |
18799 | 160 |
161 free(buffer); | |
162 return string; | |
163 } | |
164 | |
165 /* | |
166 * rtsp_put puts a line on stream | |
167 */ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
168 |
18799 | 169 static void rtsp_put(rtsp_t *s, const char *string) { |
170 | |
171 int len=strlen(string); | |
18854
a973acb2e572
cosmetic patch to remove useless sizeof(char) statements
ben
parents:
18853
diff
changeset
|
172 char *buf=malloc(len+2); |
18799 | 173 |
174 #ifdef LOG | |
175 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: >> '%s'", string); | |
176 #endif | |
177 | |
178 memcpy(buf,string,len); | |
179 buf[len]=0x0d; | |
180 buf[len+1]=0x0a; | |
181 | |
182 write_stream(s->s, buf, len+2); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
183 |
18799 | 184 #ifdef LOG |
185 mp_msg(MSGT_OPEN, MSGL_INFO, " done.\n"); | |
186 #endif | |
187 | |
188 free(buf); | |
189 } | |
190 | |
191 /* | |
192 * extract server status code | |
193 */ | |
194 | |
195 static int rtsp_get_code(const char *string) { | |
196 | |
197 char buf[4]; | |
198 int code=0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
199 |
18799 | 200 if (!strncmp(string, RTSP_PROTOCOL_VERSION, strlen(RTSP_PROTOCOL_VERSION))) |
201 { | |
202 memcpy(buf, string+strlen(RTSP_PROTOCOL_VERSION)+1, 3); | |
203 buf[3]=0; | |
204 code=atoi(buf); | |
205 } else if (!strncmp(string, RTSP_METHOD_SET_PARAMETER,8)) | |
206 { | |
207 return RTSP_STATUS_SET_PARAMETER; | |
208 } | |
209 | |
210 if(code != RTSP_STATUS_OK) mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: server responds: '%s'\n",string); | |
211 | |
212 return code; | |
213 } | |
214 | |
215 /* | |
216 * send a request | |
217 */ | |
218 | |
219 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) { | |
220 | |
221 char **payload=s->scheduled; | |
222 char *buf; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
223 |
18799 | 224 buf = malloc(strlen(type)+strlen(what)+strlen(RTSP_PROTOCOL_VERSION)+3); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
225 |
18799 | 226 sprintf(buf,"%s %s %s",type, what, RTSP_PROTOCOL_VERSION); |
227 rtsp_put(s,buf); | |
228 free(buf); | |
229 if (payload) | |
230 while (*payload) { | |
231 rtsp_put(s,*payload); | |
232 payload++; | |
233 } | |
234 rtsp_put(s,""); | |
235 rtsp_unschedule_all(s); | |
236 } | |
237 | |
238 /* | |
239 * schedule standard fields | |
240 */ | |
241 | |
242 static void rtsp_schedule_standard(rtsp_t *s) { | |
243 | |
18853
1ec19da0c642
increase buffer size, "CSeq: %u" has a worst case of 17 (fix 1.18 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.17&r2=1.18)
ben
parents:
18852
diff
changeset
|
244 char tmp[17]; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
245 |
18853
1ec19da0c642
increase buffer size, "CSeq: %u" has a worst case of 17 (fix 1.18 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.17&r2=1.18)
ben
parents:
18852
diff
changeset
|
246 snprintf(tmp, 17, "CSeq: %u", s->cseq); |
18799 | 247 rtsp_schedule_field(s, tmp); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
248 |
18799 | 249 if (s->session) { |
250 char *buf; | |
251 buf = malloc(strlen(s->session)+15); | |
252 sprintf(buf, "Session: %s", s->session); | |
253 rtsp_schedule_field(s, buf); | |
254 free(buf); | |
255 } | |
256 } | |
257 /* | |
258 * get the answers, if server responses with something != 200, return NULL | |
259 */ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
260 |
18799 | 261 static int rtsp_get_answers(rtsp_t *s) { |
262 | |
263 char *answer=NULL; | |
264 unsigned int answer_seq; | |
265 char **answer_ptr=s->answers; | |
266 int code; | |
267 int ans_count = 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
268 |
18799 | 269 answer=rtsp_get(s); |
270 if (!answer) | |
271 return 0; | |
272 code=rtsp_get_code(answer); | |
273 free(answer); | |
274 | |
275 rtsp_free_answers(s); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
276 |
18799 | 277 do { /* while we get answer lines */ |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
278 |
18799 | 279 answer=rtsp_get(s); |
280 if (!answer) | |
281 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
282 |
18852
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
283 if (!strncasecmp(answer,"CSeq:",5)) { |
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
284 sscanf(answer,"%*s %u",&answer_seq); |
18799 | 285 if (s->cseq != answer_seq) { |
286 #ifdef LOG | |
287 mp_msg(MSGT_OPEN, MSGL_WARN, "librtsp: warning: CSeq mismatch. got %u, assumed %u", answer_seq, s->cseq); | |
288 #endif | |
289 s->cseq=answer_seq; | |
290 } | |
291 } | |
18852
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
292 if (!strncasecmp(answer,"Server:",7)) { |
18799 | 293 char *buf = malloc(strlen(answer)); |
18852
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
294 sscanf(answer,"%*s %s",buf); |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
31500
diff
changeset
|
295 free(s->server); |
18799 | 296 s->server=strdup(buf); |
297 free(buf); | |
298 } | |
18852
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
299 if (!strncasecmp(answer,"Session:",8)) { |
18799 | 300 char *buf = calloc(1, strlen(answer)); |
18852
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
301 sscanf(answer,"%*s %s",buf); |
18799 | 302 if (s->session) { |
303 if (strcmp(buf, s->session)) { | |
304 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: setting NEW session: %s\n", buf); | |
305 free(s->session); | |
306 s->session=strdup(buf); | |
307 } | |
308 } else | |
309 { | |
310 #ifdef LOG | |
311 mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: setting session id to: %s\n", buf); | |
312 #endif | |
313 s->session=strdup(buf); | |
314 } | |
315 free(buf); | |
316 } | |
317 *answer_ptr=answer; | |
318 answer_ptr++; | |
319 } while ((strlen(answer)!=0) && (++ans_count < MAX_FIELDS)); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
320 |
18799 | 321 s->cseq++; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
322 |
18799 | 323 *answer_ptr=NULL; |
324 rtsp_schedule_standard(s); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
325 |
18799 | 326 return code; |
327 } | |
328 | |
329 /* | |
330 * send an ok message | |
331 */ | |
332 | |
333 int rtsp_send_ok(rtsp_t *s) { | |
334 char cseq[16]; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
335 |
18799 | 336 rtsp_put(s, "RTSP/1.0 200 OK"); |
337 sprintf(cseq,"CSeq: %u", s->cseq); | |
338 rtsp_put(s, cseq); | |
339 rtsp_put(s, ""); | |
340 return 0; | |
341 } | |
342 | |
343 /* | |
344 * implementation of must-have rtsp requests; functions return | |
345 * server status code. | |
346 */ | |
347 | |
348 int rtsp_request_options(rtsp_t *s, const char *what) { | |
349 | |
350 char *buf; | |
351 | |
352 if (what) { | |
353 buf=strdup(what); | |
354 } else | |
355 { | |
18854
a973acb2e572
cosmetic patch to remove useless sizeof(char) statements
ben
parents:
18853
diff
changeset
|
356 buf=malloc(strlen(s->host)+16); |
18799 | 357 sprintf(buf,"rtsp://%s:%i", s->host, s->port); |
358 } | |
359 rtsp_send_request(s,RTSP_METHOD_OPTIONS,buf); | |
360 free(buf); | |
361 | |
362 return rtsp_get_answers(s); | |
363 } | |
364 | |
365 int rtsp_request_describe(rtsp_t *s, const char *what) { | |
366 | |
367 char *buf; | |
368 | |
369 if (what) { | |
370 buf=strdup(what); | |
371 } else | |
372 { | |
18854
a973acb2e572
cosmetic patch to remove useless sizeof(char) statements
ben
parents:
18853
diff
changeset
|
373 buf=malloc(strlen(s->host)+strlen(s->path)+16); |
18799 | 374 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); |
375 } | |
376 rtsp_send_request(s,RTSP_METHOD_DESCRIBE,buf); | |
377 free(buf); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
378 |
18799 | 379 return rtsp_get_answers(s); |
380 } | |
381 | |
382 int rtsp_request_setup(rtsp_t *s, const char *what, char *control) { | |
383 | |
384 char *buf = NULL; | |
385 | |
386 if (what) | |
387 buf = strdup (what); | |
388 else | |
389 { | |
390 int len = strlen (s->host) + strlen (s->path) + 16; | |
391 if (control) | |
392 len += strlen (control) + 1; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
393 |
18799 | 394 buf = malloc (len); |
395 sprintf (buf, "rtsp://%s:%i/%s%s%s", s->host, s->port, s->path, | |
396 control ? "/" : "", control ? control : ""); | |
397 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
398 |
18799 | 399 rtsp_send_request (s, RTSP_METHOD_SETUP, buf); |
400 free (buf); | |
401 return rtsp_get_answers (s); | |
402 } | |
403 | |
404 int rtsp_request_setparameter(rtsp_t *s, const char *what) { | |
405 | |
406 char *buf; | |
407 | |
408 if (what) { | |
409 buf=strdup(what); | |
410 } else | |
411 { | |
18854
a973acb2e572
cosmetic patch to remove useless sizeof(char) statements
ben
parents:
18853
diff
changeset
|
412 buf=malloc(strlen(s->host)+strlen(s->path)+16); |
18799 | 413 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); |
414 } | |
415 rtsp_send_request(s,RTSP_METHOD_SET_PARAMETER,buf); | |
416 free(buf); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
417 |
18799 | 418 return rtsp_get_answers(s); |
419 } | |
420 | |
421 int rtsp_request_play(rtsp_t *s, const char *what) { | |
422 | |
423 char *buf; | |
424 int ret; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
425 |
18799 | 426 if (what) { |
427 buf=strdup(what); | |
428 } else | |
429 { | |
18854
a973acb2e572
cosmetic patch to remove useless sizeof(char) statements
ben
parents:
18853
diff
changeset
|
430 buf=malloc(strlen(s->host)+strlen(s->path)+16); |
18799 | 431 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); |
432 } | |
433 rtsp_send_request(s,RTSP_METHOD_PLAY,buf); | |
434 free(buf); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
435 |
18799 | 436 ret = rtsp_get_answers (s); |
437 if (ret == RTSP_STATUS_OK) | |
438 s->server_state = RTSP_PLAYING; | |
439 | |
440 return ret; | |
441 } | |
442 | |
443 int rtsp_request_teardown(rtsp_t *s, const char *what) { | |
444 | |
445 char *buf; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
446 |
18799 | 447 if (what) |
448 buf = strdup (what); | |
449 else | |
450 { | |
451 buf = | |
452 malloc (strlen (s->host) + strlen (s->path) + 16); | |
453 sprintf (buf, "rtsp://%s:%i/%s", s->host, s->port, s->path); | |
454 } | |
455 rtsp_send_request (s, RTSP_METHOD_TEARDOWN, buf); | |
456 free (buf); | |
18801 | 457 |
458 /* after teardown we're done with RTSP streaming, no need to get answer as | |
459 reading more will only result to garbage and buffer overflow */ | |
460 return RTSP_STATUS_OK; | |
18799 | 461 } |
462 | |
463 /* | |
464 * read opaque data from stream | |
465 */ | |
466 | |
467 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) { | |
468 | |
469 int i,seq; | |
470 | |
471 if (size>=4) { | |
472 i=read_stream(s->s, buffer, 4); | |
473 if (i<4) return i; | |
474 if (((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) || | |
475 ((buffer[0]=='O')&&(buffer[1]=='P')&&(buffer[2]=='T')&&(buffer[3]=='I'))) // OPTIONS | |
476 { | |
477 char *rest=rtsp_get(s); | |
478 if (!rest) | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
479 return -1; |
18799 | 480 |
481 seq=-1; | |
482 do { | |
483 free(rest); | |
484 rest=rtsp_get(s); | |
485 if (!rest) | |
486 return -1; | |
18852
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
487 if (!strncasecmp(rest,"CSeq:",5)) |
b17648e2762e
be more tolerant on server responses parameters case (fix 1.17 from xine, see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.16&r2=1.17)
ben
parents:
18851
diff
changeset
|
488 sscanf(rest,"%*s %u",&seq); |
18799 | 489 } while (strlen(rest)!=0); |
490 free(rest); | |
491 if (seq<0) { | |
492 #ifdef LOG | |
493 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: CSeq not recognized!\n"); | |
494 #endif | |
495 seq=1; | |
496 } | |
497 /* let's make the server happy */ | |
498 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood"); | |
18854
a973acb2e572
cosmetic patch to remove useless sizeof(char) statements
ben
parents:
18853
diff
changeset
|
499 rest=malloc(17); |
18799 | 500 sprintf(rest,"CSeq: %u", seq); |
501 rtsp_put(s, rest); | |
18855
a60bcf4969c3
coverity report #179 from xine: variable "rest" not freed or pointed-to in function "rtsp_put" (see http://xine.cvs.sourceforge.net/xine/xine-lib/src/input/librtsp/rtsp.c?r1=1.19&r2=1.20)
ben
parents:
18854
diff
changeset
|
502 free(rest); |
18799 | 503 rtsp_put(s, ""); |
504 i=read_stream(s->s, buffer, size); | |
505 } else | |
506 { | |
507 i=read_stream(s->s, buffer+4, size-4); | |
508 i+=4; | |
509 } | |
510 } else | |
511 i=read_stream(s->s, buffer, size); | |
512 #ifdef LOG | |
513 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << %d of %d bytes\n", i, size); | |
514 #endif | |
515 | |
516 return i; | |
517 } | |
518 | |
519 /* | |
520 * connect to a rtsp server | |
521 */ | |
522 | |
523 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) { | |
524 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) { | |
525 | |
27103
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
526 rtsp_t *s; |
18799 | 527 int i; |
27103
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
528 |
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
529 if (fd < 0) { |
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
530 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: failed to connect to '%s'\n", host); |
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
531 return NULL; |
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
532 } |
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
533 |
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
534 s = malloc(sizeof(rtsp_t)); |
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
535 |
18799 | 536 for (i=0; i<MAX_FIELDS; i++) { |
537 s->answers[i]=NULL; | |
538 s->scheduled[i]=NULL; | |
539 } | |
540 | |
27103
a3f6dc43b585
Move rtsp_close away by simplification - avoids symbol clash with libnemesi
lu_zero
parents:
19271
diff
changeset
|
541 s->s = fd; |
18799 | 542 s->server=NULL; |
543 s->server_state=0; | |
544 s->server_caps=0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
545 |
18799 | 546 s->cseq=0; |
547 s->session=NULL; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
548 |
18799 | 549 if (user_agent) |
550 s->user_agent=strdup(user_agent); | |
551 else | |
552 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)"); | |
553 | |
554 s->mrl = strdup(mrl); | |
555 s->host = strdup(host); | |
556 s->port = port; | |
557 s->path = strdup(path); | |
558 while (*path == '/') | |
559 path++; | |
560 if ((s->param = strchr(s->path, '?')) != NULL) | |
561 s->param++; | |
562 //mp_msg(MSGT_OPEN, MSGL_INFO, "path=%s\n", s->path); | |
563 //mp_msg(MSGT_OPEN, MSGL_INFO, "param=%s\n", s->param ? s->param : "NULL"); | |
564 | |
565 s->server_state=RTSP_CONNECTED; | |
566 | |
567 /* now let's send an options request. */ | |
568 rtsp_schedule_field(s, "CSeq: 1"); | |
569 rtsp_schedule_field(s, s->user_agent); | |
570 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7"); | |
571 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]"); | |
572 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw=="); | |
573 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000"); | |
574 rtsp_schedule_field(s, "RegionData: 0"); | |
575 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586"); | |
576 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/ | |
577 rtsp_request_options(s, NULL); | |
578 | |
579 return s; | |
580 } | |
581 | |
582 | |
583 /* | |
584 * search in answers for tags. returns a pointer to the content | |
585 * after the first matched tag. returns NULL if no match found. | |
586 */ | |
587 | |
588 char *rtsp_search_answers(rtsp_t *s, const char *tag) { | |
589 | |
590 char **answer; | |
591 char *ptr; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
592 |
18799 | 593 if (!s->answers) return NULL; |
594 answer=s->answers; | |
595 | |
596 while (*answer) { | |
597 if (!strncasecmp(*answer,tag,strlen(tag))) { | |
598 ptr=strchr(*answer,':'); | |
599 if (!ptr) return NULL; | |
600 ptr++; | |
601 while(*ptr==' ') ptr++; | |
602 return ptr; | |
603 } | |
604 answer++; | |
605 } | |
606 | |
607 return NULL; | |
608 } | |
609 | |
610 /* | |
611 * session id management | |
612 */ | |
613 | |
614 void rtsp_set_session(rtsp_t *s, const char *id) { | |
615 | |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
31500
diff
changeset
|
616 free(s->session); |
18799 | 617 |
618 s->session=strdup(id); | |
619 | |
620 } | |
621 | |
622 char *rtsp_get_session(rtsp_t *s) { | |
623 | |
624 return s->session; | |
625 | |
626 } | |
627 | |
628 char *rtsp_get_mrl(rtsp_t *s) { | |
629 | |
630 return s->mrl; | |
631 | |
632 } | |
633 | |
19108
5e767cabf4cd
marks several read-only string parameters and function return-values which can only be used read-only as const. Patch by Stefan Huehner, stefan _AT huener-org
reynaldo
parents:
18855
diff
changeset
|
634 char *rtsp_get_param(rtsp_t *s, const char *p) { |
18799 | 635 int len; |
636 char *param; | |
637 if (!s->param) | |
638 return NULL; | |
639 if (!p) | |
640 return strdup(s->param); | |
641 len = strlen(p); | |
642 param = s->param; | |
643 while (param && *param) { | |
644 char *nparam = strchr(param, '&'); | |
645 if (strncmp(param, p, len) == 0 && param[len] == '=') { | |
646 param += len + 1; | |
647 len = nparam ? nparam - param : strlen(param); | |
648 nparam = malloc(len + 1); | |
649 memcpy(nparam, param, len); | |
650 nparam[len] = 0; | |
651 return nparam; | |
652 } | |
653 param = nparam ? nparam + 1 : NULL; | |
654 } | |
655 return NULL; | |
656 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
657 |
18799 | 658 /* |
659 * schedules a field for transmission | |
660 */ | |
661 | |
662 void rtsp_schedule_field(rtsp_t *s, const char *string) { | |
663 | |
664 int i=0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
665 |
18799 | 666 if (!string) return; |
667 | |
668 while(s->scheduled[i]) { | |
669 i++; | |
670 } | |
671 s->scheduled[i]=strdup(string); | |
672 } | |
673 | |
674 /* | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
675 * removes the first scheduled field which prefix matches string. |
18799 | 676 */ |
677 | |
678 void rtsp_unschedule_field(rtsp_t *s, const char *string) { | |
679 | |
680 char **ptr=s->scheduled; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
681 |
18799 | 682 if (!string) return; |
683 | |
684 while(*ptr) { | |
685 if (!strncmp(*ptr, string, strlen(string))) | |
686 break; | |
687 else | |
688 ptr++; | |
689 } | |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
31500
diff
changeset
|
690 free(*ptr); |
18799 | 691 ptr++; |
692 do { | |
693 *(ptr-1)=*ptr; | |
694 } while(*ptr); | |
695 } | |
696 | |
697 /* | |
698 * unschedule all fields | |
699 */ | |
700 | |
701 void rtsp_unschedule_all(rtsp_t *s) { | |
702 | |
703 char **ptr; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
704 |
18799 | 705 if (!s->scheduled) return; |
706 ptr=s->scheduled; | |
707 | |
708 while (*ptr) { | |
709 free(*ptr); | |
710 *ptr=NULL; | |
711 ptr++; | |
712 } | |
713 } | |
714 /* | |
715 * free answers | |
716 */ | |
717 | |
718 void rtsp_free_answers(rtsp_t *s) { | |
719 | |
720 char **answer; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
721 |
18799 | 722 if (!s->answers) return; |
723 answer=s->answers; | |
724 | |
725 while (*answer) { | |
726 free(*answer); | |
727 *answer=NULL; | |
728 answer++; | |
729 } | |
730 } |