9922
|
1 /*
|
|
2 * This file was ported to MPlayer from xine CVS rtsp_session.c,v 1.9 2003/02/11 16:20:40
|
|
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 * high level interface to rtsp servers.
|
|
26 */
|
|
27
|
|
28 #include <sys/types.h>
|
|
29 #include <sys/socket.h>
|
|
30 #include <netinet/in.h>
|
|
31 #include <netdb.h>
|
|
32 #include <unistd.h>
|
|
33 #include <stdio.h>
|
|
34 #include <fcntl.h>
|
|
35 #include <stdlib.h>
|
|
36 #include <string.h>
|
|
37
|
|
38 #include "rtsp.h"
|
|
39 #include "rtsp_session.h"
|
|
40 #include "real.h"
|
|
41 #include "rmff.h"
|
|
42 #include "asmrp.h"
|
|
43
|
|
44 /*
|
|
45 #define LOG
|
|
46 */
|
|
47
|
|
48 #define BUF_SIZE 4096
|
|
49 #define HEADER_SIZE 4096
|
|
50
|
|
51 struct rtsp_session_s {
|
|
52
|
|
53 rtsp_t *s;
|
|
54
|
|
55 /* receive buffer */
|
|
56 uint8_t recv[BUF_SIZE];
|
|
57 int recv_size;
|
|
58 int recv_read;
|
|
59
|
|
60 /* header buffer */
|
|
61 uint8_t header[HEADER_SIZE];
|
|
62 int header_len;
|
|
63 int header_read;
|
|
64
|
|
65 };
|
|
66
|
|
67 //rtsp_session_t *rtsp_session_start(char *mrl) {
|
10199
|
68 rtsp_session_t *rtsp_session_start(int fd, char **mrl, char *path, char *host, int port, int *redir) {
|
9922
|
69
|
|
70 rtsp_session_t *rtsp_session=malloc(sizeof(rtsp_session_t));
|
|
71 char *server;
|
|
72 char *mrl_line = NULL;
|
|
73 rmff_header_t *h;
|
|
74 uint32_t bandwidth=10485800;
|
|
75
|
10199
|
76 //connect:
|
|
77 *redir = 0;
|
9922
|
78
|
|
79 /* connect to server */
|
10199
|
80 rtsp_session->s=rtsp_connect(fd,*mrl,path,host,port,NULL);
|
9922
|
81 if (!rtsp_session->s)
|
|
82 {
|
|
83 printf("rtsp_session: failed to connect to server %s\n", path);
|
|
84 free(rtsp_session);
|
|
85 return NULL;
|
|
86 }
|
|
87
|
|
88 /* looking for server type */
|
|
89 if (rtsp_search_answers(rtsp_session->s,"Server"))
|
|
90 server=strdup(rtsp_search_answers(rtsp_session->s,"Server"));
|
|
91 else {
|
|
92 if (rtsp_search_answers(rtsp_session->s,"RealChallenge1"))
|
|
93 server=strdup("Real");
|
|
94 else
|
|
95 server=strdup("unknown");
|
|
96 }
|
10198
|
97 if (strstr(server,"Real") || strstr(server,"Helix"))
|
9922
|
98 {
|
|
99 /* we are talking to a real server ... */
|
|
100
|
|
101 h=real_setup_and_get_header(rtsp_session->s, bandwidth);
|
|
102 if (!h) {
|
|
103 /* got an redirect? */
|
|
104 if (rtsp_search_answers(rtsp_session->s, "Location"))
|
|
105 {
|
|
106 free(mrl_line);
|
|
107 mrl_line=strdup(rtsp_search_answers(rtsp_session->s, "Location"));
|
|
108 printf("rtsp_session: redirected to %s\n", mrl_line);
|
|
109 rtsp_close(rtsp_session->s);
|
|
110 free(server);
|
10199
|
111 free(*mrl);
|
|
112 free(rtsp_session);
|
|
113 /* tell the caller to redirect, return url to redirect to in mrl */
|
|
114 *mrl = mrl_line;
|
|
115 *redir = 1;
|
|
116 return NULL;
|
|
117 // goto connect; /* *shudder* i made a design mistake somewhere */
|
9922
|
118 } else
|
|
119 {
|
|
120 printf("rtsp_session: session can not be established.\n");
|
|
121 rtsp_close(rtsp_session->s);
|
|
122 free(rtsp_session);
|
|
123 return NULL;
|
|
124 }
|
|
125 }
|
|
126
|
|
127 rtsp_session->header_len=rmff_dump_header(h,rtsp_session->header,1024);
|
|
128
|
|
129 memcpy(rtsp_session->recv, rtsp_session->header, rtsp_session->header_len);
|
|
130 rtsp_session->recv_size = rtsp_session->header_len;
|
|
131 rtsp_session->recv_read = 0;
|
|
132
|
|
133 } else
|
|
134 {
|
|
135 printf("rtsp_session: rtsp server type is '%s' instead of Real. Please report.\n",server);
|
|
136 rtsp_close(rtsp_session->s);
|
|
137 free(server);
|
|
138 free(rtsp_session);
|
|
139 return NULL;
|
|
140 }
|
|
141 free(server);
|
|
142
|
|
143 return rtsp_session;
|
|
144 }
|
|
145
|
|
146 int rtsp_session_read (rtsp_session_t *this, char *data, int len) {
|
|
147
|
|
148 int to_copy=len;
|
|
149 char *dest=data;
|
|
150 char *source=this->recv + this->recv_read;
|
|
151 int fill=this->recv_size - this->recv_read;
|
|
152
|
|
153 if (len < 0) return 0;
|
|
154 while (to_copy > fill) {
|
|
155
|
|
156 memcpy(dest, source, fill);
|
|
157 to_copy -= fill;
|
|
158 dest += fill;
|
|
159 this->recv_read = 0;
|
|
160 source = this->recv;
|
|
161 this->recv_size = real_get_rdt_chunk (this->s, source);
|
|
162 fill = this->recv_size;
|
|
163
|
|
164 if (this->recv_size == 0) {
|
|
165 #ifdef LOG
|
|
166 printf ("librtsp: %d of %d bytes provided\n", len-to_copy, len);
|
|
167 #endif
|
|
168 return len-to_copy;
|
|
169 }
|
|
170 }
|
|
171
|
|
172 memcpy(dest, source, to_copy);
|
|
173 this->recv_read += to_copy;
|
|
174
|
|
175 #ifdef LOG
|
|
176 printf ("librtsp: %d bytes provided\n", len);
|
|
177 #endif
|
|
178
|
|
179 return len;
|
|
180 }
|
|
181
|
|
182 int rtsp_session_peek_header(rtsp_session_t *this, char *buf, int maxsize) {
|
|
183
|
|
184 int len;
|
|
185
|
|
186 len = (this->header_len < maxsize) ? this->header_len : maxsize;
|
|
187
|
|
188 memcpy(buf, this->header, len);
|
|
189 return len;
|
|
190 }
|
|
191
|
|
192 void rtsp_session_end(rtsp_session_t *session) {
|
|
193
|
|
194 rtsp_close(session->s);
|
|
195 free(session);
|
|
196 }
|