Mercurial > mplayer.hg
annotate libmpdemux/network.c @ 3513:74cf5ea291bc
updated
author | arpi |
---|---|
date | Sun, 16 Dec 2001 03:21:47 +0000 |
parents | fb9de639ed30 |
children | cb985ea11ed5 |
rev | line source |
---|---|
903 | 1 /* |
2 * Network layer for MPlayer | |
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 */ | |
6 | |
1028 | 7 //#define DUMP2FILE |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
8 |
1430 | 9 #include <stdio.h> |
10 #include <stdlib.h> | |
11 #include <string.h> | |
833 | 12 #include <unistd.h> |
1430 | 13 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
14 #include <errno.h> |
903 | 15 #include <ctype.h> |
833 | 16 |
2555
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
17 #include "config.h" |
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
18 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
19 #include "stream.h" |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
20 #include "demuxer.h" |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
21 |
841 | 22 #include "network.h" |
903 | 23 #include "http.h" |
24 #include "url.h" | |
25 #include "asf.h" | |
841 | 26 |
3042 | 27 static struct { |
28 char *mime_type; | |
29 int demuxer_type; | |
30 } mime_type_table[] = { | |
31 // MP3 streaming, some MP3 streaming server answer with audio/mpeg | |
32 { "audio/mpeg", DEMUXER_TYPE_MPEG_PS }, | |
33 // MPEG streaming | |
34 { "video/mpeg", DEMUXER_TYPE_MPEG_PS }, | |
35 // AVI ??? => video/x-msvideo | |
36 { "video/x-msvideo", DEMUXER_TYPE_AVI }, | |
37 // MOV => video/quicktime | |
38 { "video/quicktime", DEMUXER_TYPE_MOV }, | |
39 // ASF | |
40 { "audio/x-ms-wax", DEMUXER_TYPE_ASF }, | |
41 { "audio/x-ms-wma", DEMUXER_TYPE_ASF }, | |
42 { "video/x-ms-asf", DEMUXER_TYPE_ASF }, | |
43 { "video/x-ms-afs", DEMUXER_TYPE_ASF }, | |
44 { "video/x-ms-wvx", DEMUXER_TYPE_ASF }, | |
45 { "video/x-ms-wmv", DEMUXER_TYPE_ASF }, | |
46 { "video/x-ms-wma", DEMUXER_TYPE_ASF }, | |
47 { "text/plain", DEMUXER_TYPE_ASF }, // This is the mime type that a web server send when sending a raw asf without streaming encapsulation. | |
48 }; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
49 |
3042 | 50 static struct { |
51 char *extension; | |
52 int demuxer_type; | |
53 } extensions_table[] = { | |
54 { "mpeg", DEMUXER_TYPE_MPEG_PS }, | |
55 { "mpg", DEMUXER_TYPE_MPEG_PS }, | |
3072 | 56 { "mpe", DEMUXER_TYPE_MPEG_ES }, |
3042 | 57 { "avi", DEMUXER_TYPE_AVI }, |
58 { "mov", DEMUXER_TYPE_MOV }, | |
3072 | 59 { "qt", DEMUXER_TYPE_MOV }, |
3042 | 60 { "asx", DEMUXER_TYPE_ASF }, |
61 { "asf", DEMUXER_TYPE_ASF }, | |
62 { "wmv", DEMUXER_TYPE_ASF }, | |
63 { "wma", DEMUXER_TYPE_ASF }, | |
3072 | 64 { "viv", DEMUXER_TYPE_VIVO }, |
3042 | 65 }; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
66 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
67 streaming_ctrl_t * |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
68 streaming_ctrl_new( ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
69 streaming_ctrl_t *streaming_ctrl; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
70 streaming_ctrl = (streaming_ctrl_t*)malloc(sizeof(streaming_ctrl_t)); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
71 if( streaming_ctrl==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
72 printf("Failed to allocate memory\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
73 return NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
74 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
75 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
76 return streaming_ctrl; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
77 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
78 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
79 void |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
80 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
81 if( streaming_ctrl==NULL ) return; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
82 free( streaming_ctrl ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
83 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
84 |
903 | 85 // Connect to a server using a TCP connection |
833 | 86 int |
87 connect2Server(char *host, int port) { | |
88 int socket_server_fd; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
89 int err, err_len; |
3042 | 90 int ret; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
91 fd_set set; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
92 struct timeval tv; |
833 | 93 struct sockaddr_in server_address; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
94 |
903 | 95 printf("Connecting to server %s:%d ...\n", host, port ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
96 |
833 | 97 socket_server_fd = socket(AF_INET, SOCK_STREAM, 0); |
98 if( socket_server_fd==-1 ) { | |
99 perror("Failed to create socket"); | |
841 | 100 return -1; |
833 | 101 } |
102 | |
103 if( isalpha(host[0]) ) { | |
3042 | 104 struct hostent *hp; |
105 hp=(struct hostent*)gethostbyname( host ); | |
841 | 106 if( hp==NULL ) { |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
107 printf("Counldn't resolve name: %s\n", host); |
841 | 108 return -1; |
833 | 109 } |
841 | 110 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr, hp->h_length ); |
833 | 111 } else { |
112 inet_pton(AF_INET, host, &server_address.sin_addr); | |
113 } | |
114 server_address.sin_family=AF_INET; | |
115 server_address.sin_port=htons(port); | |
3042 | 116 |
117 // Turn the socket as non blocking so we can timeout on the connection | |
118 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK ); | |
833 | 119 if( connect( socket_server_fd, (struct sockaddr*)&server_address, sizeof(server_address) )==-1 ) { |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
120 if( errno!=EINPROGRESS ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
121 perror("Failed to connect to server"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
122 close(socket_server_fd); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
123 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
124 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
125 } |
3042 | 126 tv.tv_sec = 5; // 5 seconds timeout on connection |
127 tv.tv_usec = 0; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
128 FD_ZERO( &set ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
129 FD_SET( socket_server_fd, &set ); |
3042 | 130 // When the connection will be made, we will have a writable fd |
131 ret = select(socket_server_fd+1, NULL, &set, NULL, &tv); | |
132 if( ret<=0 ) { | |
133 if( ret<0 ) perror("select failed"); | |
134 else printf("Connection timeout\n"); | |
135 return -1; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
136 } |
3042 | 137 |
138 // Turn back the socket as blocking | |
139 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK ); | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
140 // Check if there were any error |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
141 err_len = sizeof(int); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
142 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
143 if(ret < 0) { |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
144 printf("getsockopt failed : %s\n",strerror(errno)); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
145 return -1; |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
146 } |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
147 if(err > 0) { |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
148 printf("Connect error : %s\n",strerror(err)); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
149 return -1; |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
150 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
151 return socket_server_fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
152 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
153 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
154 int |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
155 http_send_request( URL_t *url ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
156 HTTP_header_t *http_hdr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
157 int fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
158 http_hdr = http_new_header(); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
159 http_set_uri( http_hdr, url->file ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
160 http_set_field( http_hdr, "User-Agent: MPlayer"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
161 http_set_field( http_hdr, "Connection: closed"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
162 if( http_build_request( http_hdr )==NULL ) { |
841 | 163 return -1; |
833 | 164 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
165 |
3453
10577da4a7b1
Added a data field in the streaming_ctrl_t struct, to store any
bertrand
parents:
3424
diff
changeset
|
166 if( url->port==0 ) url->port = 80; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
167 fd = connect2Server( url->hostname, url->port ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
168 if( fd<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
169 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
170 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
171 write( fd, http_hdr->buffer, http_hdr->buffer_size ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
172 http_free( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
173 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
174 return fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
175 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
176 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
177 HTTP_header_t * |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
178 http_read_response( int fd ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
179 HTTP_header_t *http_hdr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
180 char response[BUFFER_SIZE]; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
181 int i; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
182 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
183 http_hdr = http_new_header(); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
184 if( http_hdr==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
185 return NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
186 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
187 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
188 do { |
3042 | 189 i = read( fd, response, BUFFER_SIZE ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
190 if( i<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
191 printf("Read failed\n"); |
3365 | 192 http_free( http_hdr ); |
193 return NULL; | |
194 } | |
195 if( i==0 ) { | |
196 printf("http_read_response read 0 -ie- EOF\n"); | |
197 http_free( http_hdr ); | |
198 return NULL; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
199 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
200 http_response_append( http_hdr, response, i ); |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
201 } while( !http_is_header_entire( http_hdr ) ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
202 http_response_parse( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
203 return http_hdr; |
833 | 204 } |
205 | |
903 | 206 // By using the protocol, the extension of the file or the content-type |
207 // we might be able to guess the streaming type. | |
208 int | |
209 autodetectProtocol(URL_t *url, int *fd_out) { | |
210 HTTP_header_t *http_hdr; | |
211 int fd=-1; | |
212 int i; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
213 int redirect; |
903 | 214 char *extension; |
215 char *content_type; | |
216 char *next_url; | |
217 char response[1024]; | |
218 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
219 do { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
220 *fd_out=-1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
221 next_url = NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
222 extension = NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
223 content_type = NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
224 redirect = 0; |
903 | 225 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
226 if( url==NULL ) return DEMUXER_TYPE_UNKNOWN; |
903 | 227 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
228 // Get the extension of the file if present |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
229 if( url->file!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
230 for( i=strlen(url->file) ; i>0 ; i-- ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
231 if( url->file[i]=='.' ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
232 extension=(url->file)+i+1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
233 break; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
234 } |
903 | 235 } |
236 } | |
3042 | 237 extension=NULL; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
238 if( extension!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
239 printf("Extension: %s\n", extension ); |
3042 | 240 // Look for the extension in the extensions table |
241 for( i=0 ; i<(sizeof(extensions_table)/sizeof(extensions_table[0])) ; i++ ) { | |
242 if( !strcasecmp(extension, extensions_table[i].extension) ) { | |
3453
10577da4a7b1
Added a data field in the streaming_ctrl_t struct, to store any
bertrand
parents:
3424
diff
changeset
|
243 //if( url->port==0 ) url->port = 80; |
3042 | 244 return extensions_table[i].demuxer_type; |
245 } | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
246 } |
903 | 247 } |
248 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
249 // Checking for RTSP |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
250 if( !strcasecmp(url->protocol, "rtsp") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
251 printf("RTSP protocol not yet implemented!\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
252 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
253 } |
903 | 254 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
255 // Checking for ASF |
3453
10577da4a7b1
Added a data field in the streaming_ctrl_t struct, to store any
bertrand
parents:
3424
diff
changeset
|
256 if( !strncasecmp(url->protocol, "mms", 3) ) { |
10577da4a7b1
Added a data field in the streaming_ctrl_t struct, to store any
bertrand
parents:
3424
diff
changeset
|
257 //if( url->port==0 ) url->port = 80; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
258 return DEMUXER_TYPE_ASF; |
903 | 259 } |
260 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
261 // HTTP based protocol |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
262 if( !strcasecmp(url->protocol, "http") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
263 if( url->port==0 ) url->port = 80; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
264 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
265 fd = http_send_request( url ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
266 if( fd<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
267 *fd_out=-1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
268 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
269 } |
903 | 270 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
271 http_hdr = http_read_response( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
272 if( http_hdr==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
273 close( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
274 *fd_out=-1; |
3042 | 275 http_free( http_hdr ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
276 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
277 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
278 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
279 *fd_out=fd; |
3042 | 280 http_debug_hdr( http_hdr ); |
903 | 281 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
282 // Check if the response is an ICY status_code reason_phrase |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
283 if( !strcasecmp(http_hdr->protocol, "ICY") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
284 // Ok, we have detected an mp3 streaming |
3042 | 285 http_free( http_hdr ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
286 return DEMUXER_TYPE_MPEG_PS; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
287 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
288 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
289 switch( http_hdr->status_code ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
290 case 200: // OK |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
291 // Look if we can use the Content-Type |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
292 content_type = http_get_field( http_hdr, "Content-Type" ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
293 if( content_type!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
294 printf("Content-Type: [%s]\n", content_type ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
295 printf("Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); |
3042 | 296 // Check in the mime type table for a demuxer type |
297 for( i=0 ; i<(sizeof(mime_type_table)/sizeof(mime_type_table[0])) ; i++ ) { | |
298 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { | |
299 http_free( http_hdr ); | |
300 return mime_type_table[i].demuxer_type; | |
301 } | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
302 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
303 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
304 break; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
305 // Redirect |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
306 case 301: // Permanently |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
307 case 302: // Temporarily |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
308 // TODO: RFC 2616, recommand to detect infinite redirection loops |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
309 next_url = http_get_field( http_hdr, "Location" ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
310 if( next_url!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
311 close( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
312 url_free( url ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
313 url = url_new( next_url ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
314 redirect = 1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
315 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
316 break; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
317 default: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
318 printf("Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
319 close( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
320 *fd_out=-1; |
3042 | 321 http_free( http_hdr ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
322 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
323 } |
3453
10577da4a7b1
Added a data field in the streaming_ctrl_t struct, to store any
bertrand
parents:
3424
diff
changeset
|
324 } else { |
10577da4a7b1
Added a data field in the streaming_ctrl_t struct, to store any
bertrand
parents:
3424
diff
changeset
|
325 printf("Unknown protocol '%s'\n", url->protocol ); |
10577da4a7b1
Added a data field in the streaming_ctrl_t struct, to store any
bertrand
parents:
3424
diff
changeset
|
326 return DEMUXER_TYPE_UNKNOWN; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
327 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
328 } while( redirect ); |
903 | 329 |
3042 | 330 http_free( http_hdr ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
331 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
332 } |
903 | 333 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
334 int |
3042 | 335 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) { |
336 printf("streaming_bufferize\n"); | |
337 streaming_ctrl->buffer = (char*)malloc(size); | |
338 if( streaming_ctrl->buffer==NULL ) { | |
339 printf("Memory allocation failed\n"); | |
340 return -1; | |
341 } | |
342 memcpy( streaming_ctrl->buffer, buffer, size ); | |
343 streaming_ctrl->buffer_size = size; | |
344 } | |
345 | |
346 int | |
347 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) { | |
348 int len=0; | |
349 //printf("nop_streaming_read\n"); | |
350 if( stream_ctrl->buffer_size!=0 ) { | |
351 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos; | |
352 printf("%d bytes in buffer\n", stream_ctrl->buffer_size); | |
353 len = (size<buffer_len)?size:buffer_len; | |
354 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len ); | |
355 stream_ctrl->buffer_pos += len; | |
356 printf("buffer_pos = %d\n", stream_ctrl->buffer_pos ); | |
357 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) { | |
358 free( stream_ctrl->buffer ); | |
359 stream_ctrl->buffer = NULL; | |
360 stream_ctrl->buffer_size = 0; | |
361 stream_ctrl->buffer_pos = 0; | |
362 printf("buffer cleaned\n"); | |
363 } | |
364 printf("read %d bytes from buffer\n", len ); | |
365 } | |
366 | |
367 if( len<size ) { | |
3365 | 368 int ret; |
369 ret = read( fd, buffer+len, size-len ); | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
370 if( ret<0 ) { |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
371 printf("nop_streaming_read error : %s\n",strerror(errno)); |
3365 | 372 } |
373 len += ret; | |
3042 | 374 //printf("read %d bytes from network\n", len ); |
375 } | |
376 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
377 return len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
378 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
379 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
380 int |
3042 | 381 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) { |
382 return -1; | |
383 } | |
384 | |
385 int | |
386 nop_streaming_start( stream_t *stream ) { | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
387 HTTP_header_t *http_hdr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
388 int fd; |
3042 | 389 if( stream==NULL ) return -1; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
390 |
3042 | 391 fd = stream->fd; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
392 if( fd<0 ) { |
3042 | 393 fd = http_send_request( stream->streaming_ctrl->url ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
394 if( fd<0 ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
395 http_hdr = http_read_response( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
396 if( http_hdr==NULL ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
397 |
903 | 398 switch( http_hdr->status_code ) { |
399 case 200: // OK | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
400 printf("Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
401 printf("Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
402 if( http_hdr->body_size>0 ) { |
3042 | 403 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { |
404 http_free( http_hdr ); | |
405 return -1; | |
406 } | |
903 | 407 } |
408 break; | |
409 default: | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
410 printf("Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); |
903 | 411 close( fd ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
412 fd = -1; |
903 | 413 } |
3042 | 414 stream->fd = fd; |
903 | 415 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
416 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
417 http_free( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
418 |
3042 | 419 stream->streaming_ctrl->streaming_read = nop_streaming_read; |
420 stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
421 stream->streaming_ctrl->prebuffer_size = 180000; | |
422 // stream->streaming_ctrl->prebuffer_size = 0; | |
423 stream->streaming_ctrl->buffering = 1; | |
424 // stream->streaming_ctrl->buffering = 0; | |
425 stream->streaming_ctrl->status = streaming_playing_e; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
426 return fd; |
903 | 427 } |
428 | |
429 int | |
3042 | 430 streaming_start(stream_t *stream, URL_t *url, int demuxer_type) { |
431 int ret=-1; | |
432 if( stream==NULL ) return -1; | |
433 | |
434 stream->streaming_ctrl = streaming_ctrl_new( ); | |
435 if( stream->streaming_ctrl==NULL ) { | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
436 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
437 } |
3042 | 438 |
439 stream->streaming_ctrl->url = url_copy(url); | |
440 // stream->streaming_ctrl->demuxer_type = demuxer_type; | |
441 stream->fd = -1; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
442 |
3042 | 443 switch( demuxer_type ) { |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
444 case DEMUXER_TYPE_ASF: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
445 // Send the appropriate HTTP request |
3042 | 446 // Need to filter the network stream. |
447 // ASF raw stream is encapsulated. | |
448 ret = asf_streaming_start( stream ); | |
903 | 449 break; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
450 case DEMUXER_TYPE_AVI: |
3042 | 451 case DEMUXER_TYPE_MOV: |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
452 case DEMUXER_TYPE_MPEG_ES: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
453 case DEMUXER_TYPE_MPEG_PS: |
3042 | 454 // Generic start, doesn't need to filter |
455 // the network stream, it's a raw stream | |
456 ret = nop_streaming_start( stream ); | |
903 | 457 break; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
458 case DEMUXER_TYPE_UNKNOWN: |
903 | 459 default: |
460 printf("Unable to detect the streaming type\n"); | |
3042 | 461 ret = -1; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
462 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
463 |
3042 | 464 if( ret<0 ) { |
465 free( stream->streaming_ctrl ); | |
466 } else { | |
467 // bufferize( stream ); | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
468 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
469 |
3042 | 470 return ret; |
903 | 471 } |
472 | |
473 int | |
3042 | 474 streaming_stop( stream_t *stream ) { |
475 stream->streaming_ctrl->status = streaming_stopped_e; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
476 return 0; |
903 | 477 } |