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