comparison TOOLS/netstream.c @ 22855:d258dd05e0ab

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