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