annotate TOOLS/netstream/netstream.c @ 18821:ac61a42feb47

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