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