vo_glamo: sub.h was moved to sub directory in c9026cb3210205b07e2e068467a18ee40f9259a3
[mplayer/glamo.git] / stream / stream_netstream.c
blob6240aa7044a0867fd0951b68a9fb1965bccd6079
1 /*
2 * Copyright (C) Alban Bedel - 04/2003
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Net stream allow you to access MPlayer stream accross a tcp
23 * connection.
24 * Note that at least mf and tv use a dummy stream (they are
25 * implemented at the demuxer level) so you won't be able to
26 * access those :(( but dvd, vcd and so on should work perfectly
27 * (if you have the bandwidth ;)
28 * A simple server is in TOOLS/netstream.
33 #include "config.h"
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <inttypes.h>
43 #include <errno.h>
45 #if !HAVE_WINSOCK2_H
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #else
50 #include <winsock2.h>
51 #endif
53 #include "mp_msg.h"
54 #include "stream.h"
55 #include "m_option.h"
56 #include "m_struct.h"
57 #include "libavutil/common.h"
58 #include "mpbswap.h"
60 #include "network.h"
61 #include "stream_netstream.h"
62 #include "tcp.h"
64 static struct stream_priv_s {
65 char* host;
66 int port;
67 char* url;
68 } stream_priv_dflts = {
69 NULL,
70 10000,
71 NULL
74 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
75 /// URL definition
76 static const m_option_t stream_opts_fields[] = {
77 {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL},
78 {"port", ST_OFF(port), CONF_TYPE_INT, M_OPT_MIN, 1 ,0, NULL},
79 {"filename", ST_OFF(url), CONF_TYPE_STRING, 0, 0 ,0, NULL},
80 { NULL, NULL, 0, 0, 0, 0, NULL }
82 static const struct m_struct_st stream_opts = {
83 "netstream",
84 sizeof(struct stream_priv_s),
85 &stream_priv_dflts,
86 stream_opts_fields
89 //// When the cache is running we need a lock as
90 //// fill_buffer is called from another proccess
91 static int lock_fd(int fd) {
92 #if !HAVE_WINSOCK2_H
93 struct flock lock;
95 memset(&lock,0,sizeof(struct flock));
96 lock.l_type = F_WRLCK;
98 mp_msg(MSGT_STREAM,MSGL_DBG2, "Lock (%d)\n",getpid());
99 do {
100 if(fcntl(fd,F_SETLKW,&lock)) {
101 if(errno == EAGAIN) continue;
102 mp_msg(MSGT_STREAM,MSGL_ERR, "Failed to get the lock: %s\n",
103 strerror(errno));
104 return 0;
106 } while(0);
107 mp_msg(MSGT_STREAM,MSGL_DBG2, "Locked (%d)\n",getpid());
108 #else
109 printf("FIXME? should lock here\n");
110 #endif
111 return 1;
114 static int unlock_fd(int fd) {
115 #if !HAVE_WINSOCK2_H
116 struct flock lock;
118 memset(&lock,0,sizeof(struct flock));
119 lock.l_type = F_UNLCK;
121 mp_msg(MSGT_STREAM,MSGL_DBG2, "Unlock (%d)\n",getpid());
122 if(fcntl(fd,F_SETLK,&lock)) {
123 mp_msg(MSGT_STREAM,MSGL_ERR, "Failed to release the lock: %s\n",
124 strerror(errno));
125 return 0;
127 #else
128 printf("FIXME? should unlock here\n");
129 #endif
130 return 1;
133 static mp_net_stream_packet_t* send_net_stream_cmd(stream_t *s,uint16_t cmd,char* data,int len) {
134 mp_net_stream_packet_t* pack;
136 // Cache is enabled : lock
137 if(s->cache_data && !lock_fd(s->fd))
138 return NULL;
139 // Send a command
140 if(!write_packet(s->fd,cmd,data,len)) {
141 if(s->cache_data) unlock_fd(s->fd);
142 return 0;
144 // Read the response
145 pack = read_packet(s->fd);
146 // Now we can unlock
147 if(s->cache_data) unlock_fd(s->fd);
149 if(!pack)
150 return NULL;
152 switch(pack->cmd) {
153 case NET_STREAM_OK:
154 return pack;
155 case NET_STREAM_ERROR:
156 if(pack->len > sizeof(mp_net_stream_packet_t))
157 mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed: %s\n",pack->data);
158 else
159 mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed\n");
160 free(pack);
161 return NULL;
164 mp_msg(MSGT_STREAM,MSGL_ERR, "Unknown response to %d: %d\n",cmd,pack->cmd);
165 free(pack);
166 return NULL;
169 static int fill_buffer(stream_t *s, char* buffer, int max_len){
170 uint16_t len = le2me_16(max_len);
171 mp_net_stream_packet_t* pack;
173 pack = send_net_stream_cmd(s,NET_STREAM_FILL_BUFFER,(char*)&len,2);
174 if(!pack) {
175 return -1;
177 len = pack->len - sizeof(mp_net_stream_packet_t);
178 if(len > max_len) {
179 mp_msg(MSGT_STREAM,MSGL_ERR, "Got a too big a packet %d / %d\n",len,max_len);
180 free(pack);
181 return 0;
183 if(len > 0)
184 memcpy(buffer,pack->data,len);
185 free(pack);
186 return len;
190 static int seek(stream_t *s,off_t newpos) {
191 uint64_t pos = le2me_64((uint64_t)newpos);
192 mp_net_stream_packet_t* pack;
194 pack = send_net_stream_cmd(s,NET_STREAM_SEEK,(char*)&pos,8);
195 if(!pack) {
196 return 0;
198 s->pos = newpos;
199 free(pack);
200 return 1;
203 static int net_stream_reset(struct stream *s) {
204 mp_net_stream_packet_t* pack;
206 pack = send_net_stream_cmd(s,NET_STREAM_RESET,NULL,0);
207 if(!pack) {
208 return 0;
210 free(pack);
211 return 1;
214 static int control(struct stream *s,int cmd,void* arg) {
215 switch(cmd) {
216 case STREAM_CTRL_RESET:
217 return net_stream_reset(s);
219 return STREAM_UNSUPPORTED;
222 static void close_s(struct stream *s) {
223 mp_net_stream_packet_t* pack;
225 pack = send_net_stream_cmd(s,NET_STREAM_CLOSE,NULL,0);
226 free(pack);
229 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
230 int f;
231 struct stream_priv_s* p = (struct stream_priv_s*)opts;
232 mp_net_stream_packet_t* pack;
233 mp_net_stream_opened_t* opened;
235 if(mode != STREAM_READ)
236 return STREAM_UNSUPPORTED;
238 if(!p->host) {
239 mp_msg(MSGT_OPEN,MSGL_ERR, "We need an host name (ex: mpst://server.net/cdda://5)\n");
240 m_struct_free(&stream_opts,opts);
241 return STREAM_ERROR;
243 if(!p->url || strlen(p->url) == 0) {
244 mp_msg(MSGT_OPEN,MSGL_ERR, "We need a remote url (ex: mpst://server.net/cdda://5)\n");
245 m_struct_free(&stream_opts,opts);
246 return STREAM_ERROR;
249 f = connect2Server(p->host,p->port,1);
250 if(f < 0) {
251 mp_msg(MSGT_OPEN,MSGL_ERR, "Connection to %s:%d failed\n",p->host,p->port);
252 m_struct_free(&stream_opts,opts);
253 return STREAM_ERROR;
255 stream->fd = f;
256 /// Now send an open command
257 pack = send_net_stream_cmd(stream,NET_STREAM_OPEN,p->url,strlen(p->url) + 1);
258 if(!pack) {
259 goto error;
262 if(pack->len != sizeof(mp_net_stream_packet_t) +
263 sizeof(mp_net_stream_opened_t)) {
264 mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid open response packet len (%d bytes)\n",pack->len);
265 free(pack);
266 goto error;
269 opened = (mp_net_stream_opened_t*)pack->data;
270 net_stream_opened_2_me(opened);
272 *file_format = opened->file_format;
273 stream->flags = opened->flags;
274 stream->sector_size = opened->sector_size;
275 stream->start_pos = opened->start_pos;
276 stream->end_pos = opened->end_pos;
278 stream->fill_buffer = fill_buffer;
279 stream->control = control;
280 if(stream->flags & MP_STREAM_SEEK)
281 stream->seek = seek;
282 stream->close = close_s;
284 free(pack);
285 m_struct_free(&stream_opts,opts);
287 return STREAM_OK;
289 error:
290 closesocket(f);
291 m_struct_free(&stream_opts,opts);
292 return STREAM_ERROR;
295 const stream_info_t stream_info_netstream = {
296 "Net stream",
297 "netstream",
298 "Albeu",
300 open_s,
301 { "mpst",NULL },
302 &stream_opts,
303 1 // Url is an option string