annotate TOOLS/netstream/netstream.c @ 10789:cb5698ab3421

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