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