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