annotate TOOLS/netstream/netstream.c @ 13970:948e8164f1d0

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