Mercurial > mplayer.hg
annotate TOOLS/netstream/netstream.c @ 10789:cb5698ab3421
fixes
author | gabucino |
---|---|
date | Mon, 01 Sep 2003 19:45:36 +0000 |
parents | 2cae82f2ab02 |
children | d051cf39331e |
rev | line source |
---|---|
9856 | 1 /* |
2 * netstream.c | |
3 * | |
4 * Copyright (C) Alban Bedel - 04/2003 | |
5 * | |
6 * This file is part of MPlayer, a free movie player. | |
7 * | |
8 * MPlayer is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2, or (at your option) | |
11 * any later version. | |
12 * | |
13 * MPlayer is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with GNU Make; see the file COPYING. If not, write to | |
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 * | |
22 * | |
23 */ | |
24 | |
25 #include <stdlib.h> | |
26 #include <stdio.h> | |
27 #include <unistd.h> | |
28 #include <inttypes.h> | |
29 #include <errno.h> | |
30 #include <signal.h> | |
10281 | 31 #include <sys/types.h> |
9856 | 32 |
10281 | 33 #include "config.h" |
34 | |
35 #ifndef HAVE_WINSOCK2 | |
9856 | 36 #include <sys/socket.h> |
37 #include <netinet/in.h> | |
38 #include <arpa/inet.h> | |
10281 | 39 #else |
40 #include <winsock2.h> | |
41 #endif | |
9856 | 42 |
43 #include <libmpdemux/stream.h> | |
44 #include <mp_msg.h> | |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9856
diff
changeset
|
45 #include <bswap.h> |
9856 | 46 |
47 /// Netstream packets def and some helpers | |
48 #include <libmpdemux/netstream.h> | |
49 | |
50 static unsigned short int port = 10000; | |
51 | |
52 typedef struct client_st client_t; | |
53 struct client_st { | |
54 int fd; | |
55 stream_t* stream; | |
56 client_t* next; | |
57 client_t* prev; | |
58 }; | |
59 | |
60 static int write_error(int fd,char* msg) { | |
61 int len = strlen(msg) + 1; | |
62 return write_packet(fd,NET_STREAM_ERROR,msg,len); | |
63 } | |
64 | |
65 static int net_stream_open(client_t* cl,char* url) { | |
66 int file_format; | |
67 mp_net_stream_opened_t ret; | |
68 | |
69 if(cl->stream) { | |
70 if(!write_error(cl->fd,"A stream is currently opened\n")) | |
71 return 0; | |
72 return 1; | |
73 } | |
74 | |
75 mp_msg(MSGT_NETST,MSGL_V,"Open stream %s\n",url); | |
76 cl->stream = open_stream(url,NULL,&file_format); | |
77 if(!cl->stream) { | |
78 if(!write_error(cl->fd,"Open failed\n")) | |
79 return 0; | |
80 return 1; | |
81 } | |
82 stream_reset(cl->stream); | |
83 stream_seek(cl->stream,cl->stream->start_pos); | |
84 ret.file_format = file_format; | |
85 ret.flags = cl->stream->flags; | |
86 ret.sector_size = cl->stream->sector_size; | |
87 ret.start_pos = cl->stream->start_pos; | |
88 ret.end_pos = cl->stream->end_pos; | |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9856
diff
changeset
|
89 net_stream_opened_2_me(&ret); |
9856 | 90 |
91 if(!write_packet(cl->fd,NET_STREAM_OK,(char*)&ret,sizeof(mp_net_stream_opened_t))) | |
92 return 0; | |
93 return 1; | |
94 } | |
95 | |
96 static int net_stream_fill_buffer(client_t* cl,uint16_t max_len) { | |
97 int r; | |
98 mp_net_stream_packet_t *pack; | |
99 | |
100 if(!cl->stream) { | |
101 if(!write_error(cl->fd,"No stream is currently opened\n")) | |
102 return 0; | |
103 return 1; | |
104 } | |
105 if(max_len == 0) { | |
106 if(!write_error(cl->fd,"Fill buffer called with 0 lenght\n")) | |
107 return 0; | |
108 return 1; | |
109 } | |
110 pack = malloc(max_len + sizeof(mp_net_stream_packet_t)); | |
111 pack->cmd = NET_STREAM_OK; | |
112 r = stream_read(cl->stream,pack->data,max_len); | |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9856
diff
changeset
|
113 pack->len = le2me_16(r + sizeof(mp_net_stream_packet_t)); |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9856
diff
changeset
|
114 if(!net_write(cl->fd,(char*)pack,le2me_16(pack->len))) { |
9856 | 115 free(pack); |
116 return 0; | |
117 } | |
118 free(pack); | |
119 return 1; | |
120 } | |
121 | |
122 static int net_stream_seek(client_t* cl, uint64_t pos) { | |
123 | |
124 if(!cl->stream) { | |
125 if(!write_error(cl->fd,"No stream is currently opened\n")) | |
126 return 0; | |
127 return 1; | |
128 } | |
129 | |
130 if(!stream_seek(cl->stream,(off_t)pos)) { | |
131 if(!write_error(cl->fd,"Seek failed\n")) | |
132 return 0; | |
133 return 1; | |
134 } | |
135 if(!write_packet(cl->fd,NET_STREAM_OK,NULL,0)) | |
136 return 0; | |
137 return 1; | |
138 } | |
139 | |
140 static int net_stream_reset(client_t* cl) { | |
141 if(!cl->stream) { | |
142 if(!write_error(cl->fd,"No stream is currently opened\n")) | |
143 return 0; | |
144 return 1; | |
145 } | |
146 stream_reset(cl->stream); | |
147 if(!write_packet(cl->fd,NET_STREAM_OK,NULL,0)) | |
148 return 0; | |
149 return 1; | |
150 } | |
151 | |
152 static int net_stream_close(client_t* cl) { | |
153 if(!cl->stream) { | |
154 if(!write_error(cl->fd,"No stream is currently opened\n")) | |
155 return 0; | |
156 return 1; | |
157 } | |
158 | |
159 free_stream(cl->stream); | |
160 cl->stream = NULL; | |
161 | |
162 if(!write_packet(cl->fd,NET_STREAM_OK,NULL,0)) | |
163 return 0; | |
164 return 1; | |
165 } | |
166 | |
167 int handle_client(client_t* cl,mp_net_stream_packet_t* pack) { | |
168 | |
169 if(!pack) | |
170 return 0; | |
171 | |
172 switch(pack->cmd) { | |
173 case NET_STREAM_OPEN: | |
174 if(((char*)pack)[pack->len-1] != '\0') { | |
175 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid open packet\n"); | |
176 return 0; | |
177 } | |
178 return net_stream_open(cl,pack->data); | |
179 case NET_STREAM_FILL_BUFFER: | |
180 if(pack->len != sizeof(mp_net_stream_packet_t) + 2) { | |
181 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid fill buffer packet\n"); | |
182 return 0; | |
183 } | |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9856
diff
changeset
|
184 return net_stream_fill_buffer(cl,le2me_16(*((uint16_t*)pack->data))); |
9856 | 185 case NET_STREAM_SEEK: |
186 if(pack->len != sizeof(mp_net_stream_packet_t) + 8) { | |
187 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid fill buffer packet\n"); | |
188 return 0; | |
189 } | |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9856
diff
changeset
|
190 return net_stream_seek(cl,le2me_64(*((uint64_t*)pack->data))); |
9856 | 191 case NET_STREAM_RESET: |
192 return net_stream_reset(cl); | |
193 case NET_STREAM_CLOSE: | |
194 if(pack->len != sizeof(mp_net_stream_packet_t)){ | |
195 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid fill buffer packet\n"); | |
196 return 0; | |
197 } | |
198 return net_stream_close(cl); | |
199 default: | |
10608 | 200 mp_msg(MSGT_NETST,MSGL_WARN,"Got unknown command %d\n",pack->cmd); |
201 if(!write_error(cl->fd,"Unknown command\n")) | |
9856 | 202 return 0; |
203 } | |
204 return 0; | |
205 } | |
206 | |
207 static client_t* add_client(client_t *head,int fd) { | |
208 client_t *new = calloc(1,sizeof(client_t)); | |
209 new->fd = fd; | |
210 if(!head) return new; | |
211 new->next = head; | |
212 head->prev = new; | |
213 return new; | |
214 } | |
215 | |
216 static int make_fd_set(fd_set* fds, client_t** _cl, int listen) { | |
217 int max_fd = listen; | |
218 client_t *cl = *_cl; | |
219 FD_ZERO(fds); | |
220 FD_SET(listen,fds); | |
221 while(cl) { | |
222 // Remove this client | |
223 if(cl->fd < 0) { | |
224 client_t* f = cl; | |
225 if(cl->prev) cl->prev->next = cl->next; | |
226 if(cl->next) cl->next->prev = cl->prev; | |
227 if(cl->stream) free_stream(cl->stream); | |
228 if(!cl->prev) // Remove the head | |
229 *_cl = cl->next; | |
230 cl = cl->next; | |
231 free(f); | |
232 continue; | |
233 } | |
234 FD_SET(cl->fd,fds); | |
235 if(cl->fd > max_fd) max_fd = cl->fd; | |
236 cl = cl->next; | |
237 } | |
238 return max_fd+1; | |
239 } | |
240 | |
241 /// Hack to 'cleanly' exit | |
242 static int run_server = 1; | |
243 | |
244 void exit_sig(int sig) { | |
245 static int count = 0; | |
246 sig++; // gcc warning | |
247 count++; | |
248 if(count==3) exit(1); | |
249 if(count > 3) | |
250 kill(getpid(),SIGKILL); | |
251 run_server = 0; | |
252 } | |
253 | |
254 static int main_loop(int listen_fd) { | |
255 client_t *clients = NULL,*iter; | |
256 fd_set fds; | |
257 | |
258 signal(SIGTERM,exit_sig); // kill | |
259 signal(SIGHUP,exit_sig); // kill -HUP / xterm closed | |
260 signal(SIGINT,exit_sig); // Interrupt from keyboard | |
261 signal(SIGQUIT,exit_sig); // Quit from keyboard | |
262 | |
263 | |
264 while(run_server) { | |
265 int sel_n = make_fd_set(&fds,&clients,listen_fd); | |
266 int n = select(sel_n,&fds,NULL,NULL,NULL); | |
267 if(n < 0) { | |
268 if(errno == EINTR) | |
269 continue; | |
270 mp_msg(MSGT_NETST,MSGL_FATAL,"Select error: %s\n",strerror(errno)); | |
271 return 1; | |
272 } | |
273 // New connection | |
274 if(FD_ISSET(listen_fd,&fds)) { | |
275 struct sockaddr_in addr; | |
276 socklen_t slen = sizeof(struct sockaddr_in); | |
277 int client_fd = accept(listen_fd,(struct sockaddr*)&addr,&slen); | |
278 if(client_fd < 0) { | |
279 mp_msg(MSGT_NETST,MSGL_ERR,"accept failed: %s\n",strerror(errno)); | |
280 continue; | |
281 } | |
282 mp_msg(MSGT_NETST,MSGL_V,"New client from %s\n",inet_ntoa(addr.sin_addr)); | |
283 clients = add_client(clients,client_fd); | |
284 if(n == 1) continue; | |
285 } | |
286 // Look for the clients | |
287 for(iter = clients ; iter ; iter = iter->next) { | |
288 mp_net_stream_packet_t* pack; | |
289 if(!FD_ISSET(iter->fd,&fds)) continue; | |
290 pack = read_packet(iter->fd); | |
291 if(!pack) { | |
292 close(iter->fd); | |
293 iter->fd = -1; | |
294 continue; | |
295 } | |
296 if(!handle_client(iter,pack)) { | |
297 close(iter->fd); | |
298 iter->fd = -1; | |
299 } | |
300 free(pack); | |
301 } | |
302 } | |
303 mp_msg(MSGT_NETST,MSGL_INFO,"Exit ....\n"); | |
304 close(listen_fd); | |
305 while(clients) { | |
306 client_t* f = clients; | |
307 if(f->stream) free_stream(f->stream); | |
308 if(f->fd > 0) close(f->fd); | |
309 free(f); | |
310 clients = clients->next; | |
311 } | |
312 return 0; | |
313 } | |
314 | |
315 int main(int argc, char** argv) { | |
316 int listen_fd; | |
317 struct sockaddr_in addr; | |
318 | |
319 mp_msg_init(); | |
320 mp_msg_set_level(verbose+MSGL_STATUS); | |
321 | |
322 listen_fd = socket(AF_INET, SOCK_STREAM, 0); | |
323 if(listen_fd < 0) { | |
324 mp_msg(MSGT_NETST,MSGL_FATAL,"Failed to create listen_fd: %s\n",strerror(errno)); | |
325 return -1; | |
326 } | |
327 memset(&addr,0,sizeof(struct sockaddr)); | |
328 addr.sin_addr.s_addr = INADDR_ANY; | |
329 addr.sin_port = htons(port); | |
330 addr.sin_family = AF_INET; | |
331 if(bind(listen_fd,(struct sockaddr*)&addr,sizeof(struct sockaddr))) { | |
332 mp_msg(MSGT_NETST,MSGL_FATAL,"Failed to bind listen socket: %s\n",strerror(errno)); | |
333 return -1; | |
334 } | |
335 | |
336 | |
337 if(listen(listen_fd,1)) { | |
338 mp_msg(MSGT_NETST,MSGL_FATAL,"Failed to turn the socket in listen state: %s\n",strerror(errno)); | |
339 return -1; | |
340 } | |
341 return main_loop(listen_fd); | |
342 } | |
343 | |
344 | |
345 | |
346 //---- For libmpdemux | |
347 | |
348 #include <libmpdemux/demuxer.h> | |
349 #include <libmpdemux/stheader.h> | |
350 | |
351 // audio stream skip/resync functions requires only for seeking. | |
352 // (they should be implemented in the audio codec layer) | |
353 void skip_audio_frame(sh_audio_t *sh_audio){ | |
354 sh_audio=NULL; | |
355 } | |
356 void resync_audio_stream(sh_audio_t *sh_audio){ | |
357 sh_audio=NULL; | |
358 } | |
359 | |
360 int mp_input_check_interrupt(int time){ | |
361 if(time) usleep(time); | |
362 return 0; | |
363 } | |
364 | |
365 // for libmpdvdkit2: | |
366 #include "../get_path.c" | |
367 | |
368 int verbose=0; | |
369 | |
370 int stream_cache_size=0; | |
371 | |
372 // for demux_ogg: | |
373 void* vo_sub=NULL; | |
374 int vo_osd_changed(int new_value){ new_value++; return 0;} | |
375 int subcc_enabled=0; | |
376 | |
377 float sub_fps=0; | |
378 int sub_utf8=0; | |
379 int suboverlap_enabled = 1; | |
380 float sub_delay=0; | |
381 | |
382 //--------------- |