Mercurial > mplayer.hg
annotate libmpdemux/network.c @ 2622:fb7220c79b69
verbose fixed
author | arpi |
---|---|
date | Fri, 02 Nov 2001 02:08:00 +0000 |
parents | 66837325b929 |
children | 6b6fa2be9b97 |
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 <fcntl.h> |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
15 #include <netdb.h> |
841 | 16 #include <netinet/in.h> |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
17 #include <sys/time.h> |
833 | 18 #include <sys/types.h> |
19 #include <sys/socket.h> | |
903 | 20 #include <arpa/inet.h> |
833 | 21 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
22 #include <pthread.h> |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
23 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
24 #include <errno.h> |
903 | 25 #include <ctype.h> |
833 | 26 |
2555
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
27 #include "config.h" |
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
28 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
29 #include "stream.h" |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
30 #include "demuxer.h" |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
31 |
841 | 32 #include "network.h" |
903 | 33 #include "http.h" |
34 #include "url.h" | |
35 #include "asf.h" | |
841 | 36 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
37 streaming_ctrl_t *streaming_ctrl; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
38 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
39 static ASF_StreamType_e streaming_type = ASF_Unknown_e; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
40 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
41 Net_Fifo * |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
42 net_fifo_new() { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
43 Net_Fifo *net_fifo; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
44 net_fifo = (Net_Fifo*)malloc(sizeof(Net_Fifo)); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
45 if( net_fifo==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
46 printf("Memory allocation failed\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
47 return NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
48 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
49 memset( net_fifo, 0, sizeof(Net_Fifo) ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
50 return net_fifo; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
51 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
52 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
53 void |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
54 net_fifo_free( Net_Fifo *net_fifo ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
55 if( net_fifo->buffer!=NULL ) free( net_fifo->buffer ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
56 free( net_fifo ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
57 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
58 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
59 int |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
60 net_fifo_push(Net_Fifo *net_fifo, char *buffer, int length ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
61 char *ptr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
62 if( net_fifo==NULL || buffer==NULL || length<0 ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
63 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
64 ptr = (char*)malloc(length+net_fifo->length); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
65 if( ptr==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
66 printf("Memory allocation failed\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
67 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
68 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
69 if( net_fifo->buffer!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
70 memcpy( ptr, net_fifo->buffer, net_fifo->length ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
71 free( net_fifo->buffer ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
72 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
73 memcpy( ptr+net_fifo->length, buffer, length ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
74 net_fifo->buffer = ptr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
75 net_fifo->length += length; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
76 return net_fifo->length; |
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 int |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
80 net_fifo_pop(Net_Fifo *net_fifo, char *buffer, int length ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
81 char *ptr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
82 int len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
83 if( net_fifo==NULL || buffer==NULL || length<0 ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
84 if( net_fifo->buffer==NULL || net_fifo->length==0 ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
85 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
86 len = MIN(net_fifo->length, length); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
87 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
88 ptr = (char*)malloc(net_fifo->length-len); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
89 if( ptr==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
90 printf("Memory allocation failed\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
91 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
92 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
93 memcpy( buffer, net_fifo->buffer, len ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
94 if( net_fifo->length-len!=0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
95 memcpy( ptr, net_fifo->buffer+len, net_fifo->length-len ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
96 free( net_fifo->buffer ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
97 net_fifo->buffer = ptr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
98 net_fifo->length -= len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
99 } else { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
100 free( net_fifo->buffer ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
101 net_fifo->buffer = NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
102 net_fifo->length = 0; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
103 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
104 return len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
105 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
106 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
107 streaming_ctrl_t * |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
108 streaming_ctrl_new( ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
109 streaming_ctrl_t *streaming_ctrl; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
110 streaming_ctrl = (streaming_ctrl_t*)malloc(sizeof(streaming_ctrl_t)); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
111 if( streaming_ctrl==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
112 printf("Failed to allocate memory\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
113 return NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
114 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
115 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
116 streaming_ctrl->buffer = net_fifo_new(); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
117 return streaming_ctrl; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
118 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
119 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
120 void |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
121 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
122 if( streaming_ctrl==NULL ) return; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
123 if( streaming_ctrl->buffer!=NULL ) net_fifo_free( streaming_ctrl->buffer ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
124 free( streaming_ctrl ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
125 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
126 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
127 int |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
128 readFromServer(int fd, char *buffer, int length) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
129 int ret; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
130 int done=0; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
131 fd_set set; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
132 struct timeval tv; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
133 if( buffer==NULL || length<0 ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
134 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
135 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
136 // fcntl( fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
137 return read( fd, buffer, length ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
138 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
139 do { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
140 tv.tv_sec = 0; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
141 tv.tv_usec = 10000; // 10 milli-seconds timeout |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
142 FD_ZERO( &set ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
143 FD_SET( fd, &set ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
144 ret = select( fd+1, &set, NULL, NULL, &tv ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
145 if( ret<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
146 perror("select"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
147 } else if( ret==0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
148 printf("timeout\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
149 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
150 if( FD_ISSET(fd, &set) ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
151 ret = read( fd, buffer, length ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
152 if( ret<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
153 if( errno!=EINPROGRESS ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
154 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
155 } else { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
156 done = 1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
157 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
158 } else { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
159 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
160 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
161 } while( !done ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
162 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
163 return ret; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
164 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
165 |
903 | 166 // Connect to a server using a TCP connection |
833 | 167 int |
168 connect2Server(char *host, int port) { | |
169 int socket_server_fd; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
170 int err, err_len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
171 fd_set set; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
172 struct timeval tv; |
833 | 173 struct sockaddr_in server_address; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
174 |
903 | 175 printf("Connecting to server %s:%d ...\n", host, port ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
176 |
833 | 177 socket_server_fd = socket(AF_INET, SOCK_STREAM, 0); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
178 // fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK ); |
833 | 179 if( socket_server_fd==-1 ) { |
180 perror("Failed to create socket"); | |
841 | 181 return -1; |
833 | 182 } |
183 | |
184 if( isalpha(host[0]) ) { | |
841 | 185 struct hostent *hp =(struct hostent*)gethostbyname( host ); |
186 if( hp==NULL ) { | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
187 printf("Counldn't resolve name: %s\n", host); |
841 | 188 return -1; |
833 | 189 } |
841 | 190 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr, hp->h_length ); |
833 | 191 } else { |
192 inet_pton(AF_INET, host, &server_address.sin_addr); | |
193 } | |
194 server_address.sin_family=AF_INET; | |
195 server_address.sin_port=htons(port); | |
196 | |
197 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
|
198 if( errno!=EINPROGRESS ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
199 perror("Failed to connect to server"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
200 close(socket_server_fd); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
201 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
202 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
203 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
204 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
205 tv.tv_sec = 0; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
206 tv.tv_usec = 10000; // 10 milli-seconds timeout |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
207 FD_ZERO( &set ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
208 FD_SET( socket_server_fd, &set ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
209 if( select(socket_server_fd+1, NULL, &set, NULL, &tv)>0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
210 err_len = sizeof( err ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
211 getsockopt( socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
212 if( err ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
213 printf("Couldn't connect to host %s\n", host ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
214 printf("Socket error: %d\n", err ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
215 close(socket_server_fd); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
216 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
217 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
218 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
219 return socket_server_fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
220 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
221 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
222 int |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
223 http_send_request( URL_t *url ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
224 HTTP_header_t *http_hdr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
225 int fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
226 http_hdr = http_new_header(); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
227 http_set_uri( http_hdr, url->file ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
228 http_set_field( http_hdr, "User-Agent: MPlayer"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
229 http_set_field( http_hdr, "Connection: closed"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
230 if( http_build_request( http_hdr )==NULL ) { |
841 | 231 return -1; |
833 | 232 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
233 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
234 fd = connect2Server( url->hostname, url->port ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
235 if( fd<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
236 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
237 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
238 write( fd, http_hdr->buffer, http_hdr->buffer_size ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
239 http_free( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
240 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
241 return fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
242 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
243 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
244 HTTP_header_t * |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
245 http_read_response( int fd ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
246 HTTP_header_t *http_hdr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
247 char response[BUFFER_SIZE]; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
248 int i; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
249 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
250 http_hdr = http_new_header(); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
251 if( http_hdr==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
252 return NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
253 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
254 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
255 do { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
256 i = readFromServer( fd, response, BUFFER_SIZE ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
257 if( i<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
258 printf("Read failed\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
259 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
260 http_response_append( http_hdr, response, i ); |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
261 } while( !http_is_header_entire( http_hdr ) ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
262 http_response_parse( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
263 return http_hdr; |
833 | 264 } |
265 | |
903 | 266 // By using the protocol, the extension of the file or the content-type |
267 // we might be able to guess the streaming type. | |
268 int | |
269 autodetectProtocol(URL_t *url, int *fd_out) { | |
270 HTTP_header_t *http_hdr; | |
271 int fd=-1; | |
272 int i; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
273 int redirect; |
903 | 274 char *extension; |
275 char *content_type; | |
276 char *next_url; | |
277 char response[1024]; | |
278 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
279 do { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
280 *fd_out=-1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
281 next_url = NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
282 extension = NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
283 content_type = NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
284 redirect = 0; |
903 | 285 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
286 if( url==NULL ) return DEMUXER_TYPE_UNKNOWN; |
903 | 287 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
288 // Get the extension of the file if present |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
289 if( url->file!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
290 for( i=strlen(url->file) ; i>0 ; i-- ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
291 if( url->file[i]=='.' ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
292 extension=(url->file)+i+1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
293 break; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
294 } |
903 | 295 } |
296 } | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
297 // extension=NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
298 if( extension!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
299 printf("Extension: %s\n", extension ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
300 if( !strcasecmp(extension, "asf") || |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
301 !strcasecmp(extension, "wmv") || |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
302 !strcasecmp(extension, "asx") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
303 if( url->port==0 ) url->port = 80; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
304 return DEMUXER_TYPE_ASF; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
305 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
306 if( !strcasecmp(extension, "mpg") || |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
307 !strcasecmp(extension, "mpeg") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
308 if( url->port==0 ) url->port = 80; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
309 return DEMUXER_TYPE_MPEG_PS; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
310 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
311 if( !strcasecmp(extension, "avi") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
312 if( url->port==0 ) url->port = 80; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
313 return DEMUXER_TYPE_AVI; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
314 } |
903 | 315 } |
316 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
317 // Checking for RTSP |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
318 if( !strcasecmp(url->protocol, "rtsp") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
319 printf("RTSP protocol not yet implemented!\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
320 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
321 } |
903 | 322 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
323 // Checking for ASF |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
324 if( !strcasecmp(url->protocol, "mms") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
325 if( url->port==0 ) url->port = 80; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
326 return DEMUXER_TYPE_ASF; |
903 | 327 } |
328 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
329 // HTTP based protocol |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
330 if( !strcasecmp(url->protocol, "http") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
331 if( url->port==0 ) url->port = 80; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
332 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
333 fd = http_send_request( url ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
334 if( fd<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
335 *fd_out=-1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
336 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
337 } |
903 | 338 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
339 http_hdr = http_read_response( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
340 if( http_hdr==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
341 close( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
342 *fd_out=-1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
343 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
344 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
345 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
346 *fd_out=fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
347 //http_debug_hdr( http_hdr ); |
903 | 348 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
349 // Check if the response is an ICY status_code reason_phrase |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
350 if( !strcasecmp(http_hdr->protocol, "ICY") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
351 // Ok, we have detected an mp3 streaming |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
352 return DEMUXER_TYPE_MPEG_PS; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
353 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
354 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
355 switch( http_hdr->status_code ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
356 case 200: // OK |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
357 // Look if we can use the Content-Type |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
358 content_type = http_get_field( http_hdr, "Content-Type" ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
359 if( content_type!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
360 printf("Content-Type: [%s]\n", content_type ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
361 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
|
362 // Check for ASF |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
363 if( asf_http_streaming_type(content_type, NULL)!=ASF_Unknown_e ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
364 return DEMUXER_TYPE_ASF; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
365 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
366 // Check for MP3 streaming |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
367 // Some MP3 streaming server answer with audio/mpeg |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
368 if( !strcasecmp(content_type, "audio/mpeg") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
369 return DEMUXER_TYPE_MPEG_PS; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
370 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
371 // Check for MPEG streaming |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
372 if( !strcasecmp(content_type, "video/mpeg") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
373 return DEMUXER_TYPE_MPEG_PS; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
374 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
375 // AVI ??? => video/x-msvideo |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
376 if( !strcasecmp(content_type, "video/x-msvideo") ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
377 return DEMUXER_TYPE_AVI; |
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 break; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
381 // Redirect |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
382 case 301: // Permanently |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
383 case 302: // Temporarily |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
384 // TODO: RFC 2616, recommand to detect infinite redirection loops |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
385 next_url = http_get_field( http_hdr, "Location" ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
386 if( next_url!=NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
387 close( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
388 url_free( url ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
389 url = url_new( next_url ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
390 redirect = 1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
391 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
392 break; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
393 default: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
394 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
|
395 close( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
396 *fd_out=-1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
397 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
398 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
399 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
400 } while( redirect ); |
903 | 401 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
402 return DEMUXER_TYPE_UNKNOWN; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
403 } |
903 | 404 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
405 int |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
406 nop_streaming_read( streaming_ctrl_t *streaming_ctrl ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
407 char *buffer; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
408 int len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
409 if( streaming_ctrl==NULL ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
410 len = streaming_ctrl->buffer->length; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
411 if( len==0 ) return 0; |
903 | 412 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
413 buffer = (char*)malloc( len ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
414 if( buffer==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
415 printf("Memory allocation failed\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
416 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
417 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
418 net_fifo_pop( streaming_ctrl->buffer, buffer, len ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
419 write( streaming_ctrl->fd_pipe_in, buffer, len ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
420 free( buffer ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
421 return len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
422 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
423 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
424 int |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
425 nop_streaming_start( streaming_ctrl_t *streaming_ctrl ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
426 HTTP_header_t *http_hdr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
427 int fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
428 if( streaming_ctrl==NULL ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
429 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
430 fd = streaming_ctrl->fd_net; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
431 if( fd<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
432 fd = http_send_request( *(streaming_ctrl->url) ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
433 if( fd<0 ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
434 http_hdr = http_read_response( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
435 if( http_hdr==NULL ) return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
436 |
903 | 437 switch( http_hdr->status_code ) { |
438 case 200: // OK | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
439 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
|
440 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
|
441 if( http_hdr->body_size>0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
442 write( streaming_ctrl->fd_pipe_in, http_hdr->body, http_hdr->body_size ); |
903 | 443 } |
444 break; | |
445 default: | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
446 printf("Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); |
903 | 447 close( fd ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
448 fd = -1; |
903 | 449 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
450 streaming_ctrl->fd_net = fd; |
903 | 451 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
452 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
453 http_free( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
454 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
455 streaming_ctrl->streaming_read = nop_streaming_read; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
456 streaming_ctrl->prebuffer_size = 180000; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
457 // streaming_ctrl->prebuffer_size = 0; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
458 streaming_ctrl->buffering = 1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
459 // streaming_ctrl->buffering = 0; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
460 streaming_ctrl->status = streaming_playing_e; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
461 return fd; |
903 | 462 } |
463 | |
464 void | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
465 network_streaming(void *arg) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
466 char buffer[BUFFER_SIZE]; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
467 fd_set fd_net_in; |
903 | 468 int ret; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
469 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
470 arg = arg; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
471 |
903 | 472 do { |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
473 FD_ZERO( &fd_net_in ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
474 FD_SET( streaming_ctrl->fd_net, &fd_net_in ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
475 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
476 ret = select( streaming_ctrl->fd_net+1, &fd_net_in, NULL, NULL, NULL ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
477 if( ret<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
478 perror("select"); |
1631 | 479 return; //exit(1); // FIXME! |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
480 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
481 if( FD_ISSET( streaming_ctrl->fd_net, &fd_net_in ) ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
482 ret = readFromServer( streaming_ctrl->fd_net, buffer, BUFFER_SIZE ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
483 if( ret<=0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
484 streaming_ctrl->status=streaming_stopped_e; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
485 } else { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
486 //printf(" push: 0x%02X\n", *((unsigned int*)buffer) ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
487 net_fifo_push( streaming_ctrl->buffer, buffer, ret ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
488 if( !streaming_ctrl->buffering ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
489 do { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
490 ret = streaming_ctrl->streaming_read( streaming_ctrl ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
491 if( ret<0 && streaming_ctrl->buffer->length<streaming_ctrl->prebuffer_size ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
492 // Need buffering |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
493 streaming_ctrl->buffering = 1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
494 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
495 } while( streaming_ctrl->buffer->length>streaming_ctrl->prebuffer_size ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
496 } else { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
497 if( streaming_ctrl->buffer->length>streaming_ctrl->prebuffer_size ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
498 streaming_ctrl->buffering = 0; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
499 printf("\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
500 } else { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
501 printf(" Buffering: %d \%\r", (int)((float)(((float)streaming_ctrl->buffer->length)/((float)streaming_ctrl->prebuffer_size))*100) ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
502 fflush(stdout); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
503 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
504 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
505 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
506 } else { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
507 printf("Network fd not set\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
508 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
509 } while( streaming_ctrl->status==streaming_playing_e ); |
903 | 510 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
511 // Flush the buffer |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
512 while( streaming_ctrl->buffer->length>0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
513 ret = streaming_ctrl->streaming_read( streaming_ctrl ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
514 if( ret<0 ) break; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
515 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
516 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
517 printf("Network thread done\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
518 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
519 // Close to the pipe to stop mplayer. |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
520 close( streaming_ctrl->fd_pipe_in ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
521 |
903 | 522 } |
523 | |
524 int | |
525 streaming_start(URL_t **url, int fd, int streaming_type) { | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
526 int fd_pipe[2]; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
527 // Open the pipe |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
528 if( pipe(fd_pipe)<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
529 printf("Pipe creation failed\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
530 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
531 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
532 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
533 streaming_ctrl = streaming_ctrl_new( ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
534 if( streaming_ctrl==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
535 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
536 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
537 streaming_ctrl->url = url; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
538 streaming_ctrl->fd_pipe_in = fd_pipe[1]; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
539 streaming_ctrl->fd_net = fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
540 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
541 #ifdef DUMP2FILE |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
542 { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
543 int fd_file; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
544 fd_file = open("dump.stream", O_WRONLY | O_CREAT ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
545 if( fd_file<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
546 perror("open"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
547 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
548 streaming_ctrl->fd_pipe_in = fd_file; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
549 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
550 #endif |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
551 |
903 | 552 switch( streaming_type ) { |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
553 case DEMUXER_TYPE_ASF: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
554 // Send the appropriate HTTP request |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
555 fd = asf_http_streaming_start( streaming_ctrl ); |
903 | 556 break; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
557 case DEMUXER_TYPE_AVI: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
558 case DEMUXER_TYPE_MPEG_ES: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
559 case DEMUXER_TYPE_MPEG_PS: |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
560 fd = nop_streaming_start( streaming_ctrl ); |
903 | 561 break; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
562 case DEMUXER_TYPE_UNKNOWN: |
903 | 563 default: |
564 printf("Unable to detect the streaming type\n"); | |
565 close( fd ); | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
566 free( streaming_ctrl ); |
903 | 567 return -1; |
568 } | |
569 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
570 if( fd<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
571 free( streaming_ctrl ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
572 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
573 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
574 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
575 // Start the network thread |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
576 if( pthread_create( &(streaming_ctrl->thread_id), NULL , (void*)network_streaming, (void*)NULL)<0 ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
577 printf("Unable to start the network thread.\n"); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
578 close( fd ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
579 free( streaming_ctrl ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
580 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
581 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
582 printf("Network thread created with id: %d\n", streaming_ctrl->thread_id ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
583 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
584 // streaming_ctrl->status = streaming_stopped_e; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
585 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
586 // return fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
587 return fd_pipe[0]; |
903 | 588 } |
589 | |
590 int | |
591 streaming_stop( ) { | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
592 streaming_ctrl->status = streaming_stopped_e; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
593 return 0; |
903 | 594 } |