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