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