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