From a16530e63730b4887d8f647c10e4a45d446ee133 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 13 Jan 2013 17:22:28 +0100 Subject: [PATCH] stream_netstream: Remove netstream support This allowed to move the input stream layer across the network, allowing the user to play anything that mplayer could play remotely. For example, playing a DVD related on a remote server (say, with the host name "remotehost1") could be done by starting the netstream server (from TOOLS/netstream.c) on that remote server, and then running: mplayer mpst://remotehost1/dvd:// This would open the DVD on the remote host, and transfer the raw DVD sector reads over network. It works the same for other protocols, and all accesses to the stream layer are marshaled over network. It's comparable to the way the cache layer (--cache) works. It has questionable use and most likely was barely used at all. There's lots of potential for breakage, because it doesn't translate the stream CTRLs to network packets. Just get rid of it. --- Makefile | 8 +- TOOLS/README | 35 ----- TOOLS/netstream.c | 366 ---------------------------------------------- cfg-mplayer.h | 4 - mp_msg.h | 4 - stream/stream.c | 2 - stream/stream_netstream.c | 304 -------------------------------------- stream/stream_netstream.h | 166 --------------------- 8 files changed, 3 insertions(+), 886 deletions(-) delete mode 100644 TOOLS/netstream.c delete mode 100644 stream/stream_netstream.c delete mode 100644 stream/stream_netstream.h diff --git a/Makefile b/Makefile index 21a32a2760..c9609ffd6b 100644 --- a/Makefile +++ b/Makefile @@ -109,8 +109,7 @@ SRCS_COMMON-$(NEED_SHMEM) += osdep/shmem.c SRCS_COMMON-$(NEED_STRSEP) += osdep/strsep.c SRCS_COMMON-$(NEED_SWAB) += osdep/swab.c SRCS_COMMON-$(NEED_VSSCANF) += osdep/vsscanf.c -SRCS_COMMON-$(NETWORKING) += stream/stream_netstream.c \ - stream/asf_mmst_streaming.c \ +SRCS_COMMON-$(NETWORKING) += stream/asf_mmst_streaming.c \ stream/asf_streaming.c \ stream/cookies.c \ stream/http.c \ @@ -718,7 +717,7 @@ tests: $(addsuffix $(EXESUF),$(TESTS)) testsclean: -$(RM) $(call ADD_ALL_EXESUFS,$(TESTS)) -TOOLS = $(addprefix TOOLS/,alaw-gen asfinfo avi-fix avisubdump compare dump_mp4 movinfo netstream subrip vivodump) +TOOLS = $(addprefix TOOLS/,alaw-gen asfinfo avi-fix avisubdump compare dump_mp4 movinfo subrip vivodump) ifdef ARCH_X86 TOOLS += TOOLS/fastmemcpybench TOOLS/modify_reg @@ -743,9 +742,8 @@ TOOLS/vfw2menc$(EXESUF): -lwinmm -lole32 mplayer-nomain.o: mplayer.c $(CC) $(CFLAGS) -DDISABLE_MAIN -c -o $@ $< -TOOLS/netstream$(EXESUF): TOOLS/netstream.c TOOLS/vivodump$(EXESUF): TOOLS/vivodump.c -TOOLS/netstream$(EXESUF) TOOLS/vivodump$(EXESUF): $(subst mplayer.o,mplayer-nomain.o,$(OBJS_MPLAYER)) $(OBJS_COMMON) $(COMMON_LIBS) +TOOLS/vivodump$(EXESUF): $(subst mplayer.o,mplayer-nomain.o,$(OBJS_MPLAYER)) $(OBJS_COMMON) $(COMMON_LIBS) $(CC) $(CFLAGS) -o $@ $^ $(EXTRALIBS_MPLAYER) $(EXTRALIBS) REAL_SRCS = $(wildcard TOOLS/realcodecs/*.c) diff --git a/TOOLS/README b/TOOLS/README index d1e3775684..58c1adeb56 100644 --- a/TOOLS/README +++ b/TOOLS/README @@ -440,38 +440,3 @@ Usage: - Set the path to the RealPlayer codecs directory in the C files. - Put the wrappers in the RealPlayer codecs directory. Notes: Known to work at least on Linux x86 with RealPlayer8. - - - -netstream - access remote streams ---------------------------------- - -netstream allows you to access most MPlayer stream types from a remote host. -The main purpose of this feature is to make it possible to directly use the -CD or DVD drive of another computer on the network (provided you have the -required bandwidth). On the downside some stream types (currently TV and MF) -are not usable remotely because they are implemented at the demuxer level. - -Be aware that currently the server is not secure at all. - -Compile the server by running 'make TOOLS/netstream' ('make TOOLS/netstream.exe' -on windows) from the root of mplayer source and then copy the netstream binary -to the right place on your system (usually /usr/local/bin on Unix). Start the -netstream server on the computer you intend to access remotely. There are no -command line arguments. - -Play the second track of a VCD on the server with: - - mplayer -cache 5000 mpst://servername/vcd://2 - -Access files on this server with: - - mplayer -cache 5000 mpst://servername//usr/local/movies/lol.avi - -Note that paths which do not start with a / will be interpreted as relative to -the directory where the server is running. The '-cache' option is not needed -but highly recommended. - -netstream will not take -dvd-device, so you must either symlink -your drive to /dev/dvd/ or use the dvd:///path/to/dvd. -for example: mpst://audioserver/dvd:////dev/optical/lg_black_dvd diff --git a/TOOLS/netstream.c b/TOOLS/netstream.c deleted file mode 100644 index e7f95817e5..0000000000 --- a/TOOLS/netstream.c +++ /dev/null @@ -1,366 +0,0 @@ -/* - * Copyright (C) Alban Bedel - 04/2003 - * - * This file is part of MPlayer. - * - * MPlayer is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MPlayer is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#if !HAVE_WINSOCK2_H -#include -#include -#include -#else -#include -#include -#endif - -#include "stream/stream.h" -#include "libmpdemux/demuxer.h" -#include "mp_msg.h" -#include "libavutil/common.h" -#include "mpbswap.h" - -/// Netstream packets def and some helpers -#include "stream/stream_netstream.h" - -// linking hacks -char *info_name; -char *info_artist; -char *info_genre; -char *info_subject; -char *info_copyright; -char *info_sourceform; -char *info_comment; - -char* out_filename = NULL; -char* force_fourcc=NULL; -char* passtmpfile="divx2pass.log"; - -static unsigned short int port = 10000; - -typedef struct client_st client_t; -struct client_st { - int fd; - stream_t* stream; - client_t* next; - client_t* prev; -}; - -static int write_error(int fd,char* msg) { - int len = strlen(msg) + 1; - return write_packet(fd,NET_STREAM_ERROR,msg,len); -} - -static int net_stream_open(client_t* cl,char* url) { - int file_format=DEMUXER_TYPE_UNKNOWN; - mp_net_stream_opened_t ret; - - if(cl->stream) { - if(!write_error(cl->fd,"A stream is currently opened\n")) - return 0; - return 1; - } - - mp_msg(MSGT_NETST,MSGL_V,"Open stream %s\n",url); - cl->stream = open_stream(url,NULL,&file_format); - if(!cl->stream) { - if(!write_error(cl->fd,"Open failed\n")) - return 0; - return 1; - } - stream_reset(cl->stream); - stream_seek(cl->stream,cl->stream->start_pos); - ret.file_format = file_format; - ret.flags = cl->stream->flags; - ret.sector_size = cl->stream->sector_size; - ret.start_pos = cl->stream->start_pos; - ret.end_pos = cl->stream->end_pos; - net_stream_opened_2_me(&ret); - - if(!write_packet(cl->fd,NET_STREAM_OK,(char*)&ret,sizeof(mp_net_stream_opened_t))) - return 0; - return 1; -} - -static int net_stream_fill_buffer(client_t* cl,uint16_t max_len) { - int r; - mp_net_stream_packet_t *pack; - - if(!cl->stream) { - if(!write_error(cl->fd,"No stream is currently opened\n")) - return 0; - return 1; - } - if(max_len == 0) { - if(!write_error(cl->fd,"Fill buffer called with 0 length\n")) - return 0; - return 1; - } - pack = malloc(max_len + sizeof(mp_net_stream_packet_t)); - pack->cmd = NET_STREAM_OK; - r = stream_read(cl->stream,pack->data,max_len); - pack->len = le2me_16(r + sizeof(mp_net_stream_packet_t)); - if(!net_write(cl->fd,(char*)pack,le2me_16(pack->len))) { - free(pack); - return 0; - } - free(pack); - return 1; -} - -static int net_stream_seek(client_t* cl, uint64_t pos) { - - if(!cl->stream) { - if(!write_error(cl->fd,"No stream is currently opened\n")) - return 0; - return 1; - } - - if(!stream_seek(cl->stream,(off_t)pos)) { - if(!write_error(cl->fd,"Seek failed\n")) - return 0; - return 1; - } - if(!write_packet(cl->fd,NET_STREAM_OK,NULL,0)) - return 0; - return 1; -} - -static int net_stream_reset(client_t* cl) { - if(!cl->stream) { - if(!write_error(cl->fd,"No stream is currently opened\n")) - return 0; - return 1; - } - stream_reset(cl->stream); - if(!write_packet(cl->fd,NET_STREAM_OK,NULL,0)) - return 0; - return 1; -} - -static int net_stream_close(client_t* cl) { - if(!cl->stream) { - if(!write_error(cl->fd,"No stream is currently opened\n")) - return 0; - return 1; - } - - free_stream(cl->stream); - cl->stream = NULL; - - if(!write_packet(cl->fd,NET_STREAM_OK,NULL,0)) - return 0; - return 1; -} - -static int handle_client(client_t* cl,mp_net_stream_packet_t* pack) { - - if(!pack) - return 0; - - switch(pack->cmd) { - case NET_STREAM_OPEN: - if(((char*)pack)[pack->len-1] != '\0') { - mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid open packet\n"); - return 0; - } - return net_stream_open(cl,pack->data); - case NET_STREAM_FILL_BUFFER: - if(pack->len != sizeof(mp_net_stream_packet_t) + 2) { - mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid fill buffer packet\n"); - return 0; - } - return net_stream_fill_buffer(cl,le2me_16(*((uint16_t*)pack->data))); - case NET_STREAM_SEEK: - if(pack->len != sizeof(mp_net_stream_packet_t) + 8) { - mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid fill buffer packet\n"); - return 0; - } - return net_stream_seek(cl,le2me_64(*((uint64_t*)pack->data))); - case NET_STREAM_RESET: - return net_stream_reset(cl); - case NET_STREAM_CLOSE: - if(pack->len != sizeof(mp_net_stream_packet_t)){ - mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid fill buffer packet\n"); - return 0; - } - return net_stream_close(cl); - default: - mp_msg(MSGT_NETST,MSGL_WARN,"Got unknown command %d\n",pack->cmd); - if(!write_error(cl->fd,"Unknown command\n")) - return 0; - } - return 0; -} - -static client_t* add_client(client_t *head,int fd) { - client_t *new = calloc(1,sizeof(client_t)); - new->fd = fd; - if(!head) return new; - new->next = head; - head->prev = new; - return new; -} - -static int make_fd_set(fd_set* fds, client_t** _cl, int listen) { - int max_fd = listen; - client_t *cl = *_cl; - FD_ZERO(fds); - FD_SET(listen,fds); - while(cl) { - // Remove this client - if(cl->fd < 0) { - client_t* f = cl; - if(cl->prev) cl->prev->next = cl->next; - if(cl->next) cl->next->prev = cl->prev; - if(cl->stream) free_stream(cl->stream); - if(!cl->prev) // Remove the head - *_cl = cl->next; - cl = cl->next; - free(f); - continue; - } - FD_SET(cl->fd,fds); - if(cl->fd > max_fd) max_fd = cl->fd; - cl = cl->next; - } - return max_fd+1; -} - -/// Hack to 'cleanly' exit -static int run_server = 1; - -void exit_sig(int sig) { - static int count = 0; - sig++; // gcc warning - count++; - if(count==3) exit(1); - if(count > 3) -#ifdef __MINGW32__ - WSACleanup(); -#else - kill(getpid(),SIGKILL); -#endif - run_server = 0; -} - -static int main_loop(int listen_fd) { - client_t *clients = NULL,*iter; - fd_set fds; - - signal(SIGTERM,exit_sig); // kill -#ifndef __MINGW32__ - signal(SIGHUP,exit_sig); // kill -HUP / xterm closed - signal(SIGINT,exit_sig); // Interrupt from keyboard - signal(SIGQUIT,exit_sig); // Quit from keyboard -#endif - - while(run_server) { - int sel_n = make_fd_set(&fds,&clients,listen_fd); - int n = select(sel_n,&fds,NULL,NULL,NULL); - if(n < 0) { - if(errno == EINTR) - continue; - mp_msg(MSGT_NETST,MSGL_FATAL,"Select error: %s\n",strerror(errno)); - return 1; - } - // New connection - if(FD_ISSET(listen_fd,&fds)) { - struct sockaddr_in addr; - socklen_t slen = sizeof(struct sockaddr_in); - int client_fd = accept(listen_fd,(struct sockaddr*)&addr,&slen); - if(client_fd < 0) { - mp_msg(MSGT_NETST,MSGL_ERR,"accept failed: %s\n",strerror(errno)); - continue; - } - mp_msg(MSGT_NETST,MSGL_V,"New client from %s\n",inet_ntoa(addr.sin_addr)); - clients = add_client(clients,client_fd); - if(n == 1) continue; - } - // Look for the clients - for(iter = clients ; iter ; iter = iter->next) { - mp_net_stream_packet_t* pack; - if(!FD_ISSET(iter->fd,&fds)) continue; - pack = read_packet(iter->fd); - if(!pack) { - close(iter->fd); - iter->fd = -1; - continue; - } - if(!handle_client(iter,pack)) { - close(iter->fd); - iter->fd = -1; - } - free(pack); - } - } - mp_msg(MSGT_NETST,MSGL_INFO,"Exit ....\n"); - close(listen_fd); -#ifdef __MINGW32__ - WSACleanup(); -#endif - while(clients) { - client_t* f = clients; - if(f->stream) free_stream(f->stream); - if(f->fd > 0) close(f->fd); - free(f); - clients = clients->next; - } - return 0; -} - -int main(void) { - int listen_fd; - struct sockaddr_in addr; - - mp_msg_init(); - // mp_msg_set_level(verbose+MSGL_STATUS); - -#ifdef __MINGW32__ - WSADATA wsaData; - WSAStartup(MAKEWORD(1,1), &wsaData); -#endif - listen_fd = socket(AF_INET, SOCK_STREAM, 0); - if(listen_fd < 0) { - mp_msg(MSGT_NETST,MSGL_FATAL,"Failed to create listen_fd: %s\n",strerror(errno)); - return -1; - } - memset(&addr,0,sizeof(struct sockaddr)); - addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = htons(port); - addr.sin_family = AF_INET; - if(bind(listen_fd,(struct sockaddr*)&addr,sizeof(struct sockaddr))) { - mp_msg(MSGT_NETST,MSGL_FATAL,"Failed to bind listen socket: %s\n",strerror(errno)); - return -1; - } - - - if(listen(listen_fd,1)) { - mp_msg(MSGT_NETST,MSGL_FATAL,"Failed to turn the socket in listen state: %s\n",strerror(errno)); - return -1; - } - return main_loop(listen_fd); -} diff --git a/cfg-mplayer.h b/cfg-mplayer.h index f99c948296..ff8868f644 100644 --- a/cfg-mplayer.h +++ b/cfg-mplayer.h @@ -298,11 +298,9 @@ const m_option_t msgl_config[]={ { "network", &mp_msg_levels[MSGT_NETWORK], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "cpudetect", &mp_msg_levels[MSGT_CPUDETECT], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "codeccfg", &mp_msg_levels[MSGT_CODECCFG], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, - { "sws", &mp_msg_levels[MSGT_SWS], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "vobsub", &mp_msg_levels[MSGT_VOBSUB], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "subreader", &mp_msg_levels[MSGT_SUBREADER], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "afilter", &mp_msg_levels[MSGT_AFILTER], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, - { "netst", &mp_msg_levels[MSGT_NETST], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "muxer", &mp_msg_levels[MSGT_MUXER], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "identify", &mp_msg_levels[MSGT_IDENTIFY], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, { "ass", &mp_msg_levels[MSGT_ASS], CONF_TYPE_INT, CONF_RANGE, -1, 9, NULL }, @@ -341,11 +339,9 @@ const m_option_t msgl_config[]={ " network\n" " cpudetect\n" " codeccfg\n" - " sws\n" " vobsub\n" " subreader\n" " afilter - Audio filter messages\n" - " netst - Netstream\n" " muxer - muxer layer\n" " identify - identify output\n" " ass - libass messages\n" diff --git a/mp_msg.h b/mp_msg.h index 686ed8316c..300ee3cb3b 100644 --- a/mp_msg.h +++ b/mp_msg.h @@ -101,15 +101,11 @@ extern int verbose; #define MSGT_CODECCFG 33 -#define MSGT_SWS 34 - #define MSGT_VOBSUB 35 #define MSGT_SUBREADER 36 #define MSGT_AFILTER 37 // Audio filter messages -#define MSGT_NETST 38 // Netstream - #define MSGT_MUXER 39 // muxer layer #define MSGT_IDENTIFY 41 // -identify output diff --git a/stream/stream.c b/stream/stream.c index 1f61ba257e..d4d08a501b 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -64,7 +64,6 @@ static struct input_ctx *stream_check_interrupt_ctx; extern const stream_info_t stream_info_vcd; extern const stream_info_t stream_info_cdda; -extern const stream_info_t stream_info_netstream; extern const stream_info_t stream_info_pnm; extern const stream_info_t stream_info_asf; extern const stream_info_t stream_info_rtp; @@ -98,7 +97,6 @@ static const stream_info_t* const auto_open_streams[] = { #endif &stream_info_ffmpeg, // use for rstp:// before http fallback #ifdef CONFIG_NETWORKING - &stream_info_netstream, &stream_info_http1, &stream_info_asf, &stream_info_pnm, diff --git a/stream/stream_netstream.c b/stream/stream_netstream.c deleted file mode 100644 index 6240aa7044..0000000000 --- a/stream/stream_netstream.c +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Copyright (C) Alban Bedel - 04/2003 - * - * This file is part of MPlayer. - * - * MPlayer is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MPlayer is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/* - * Net stream allow you to access MPlayer stream accross a tcp - * connection. - * Note that at least mf and tv use a dummy stream (they are - * implemented at the demuxer level) so you won't be able to - * access those :(( but dvd, vcd and so on should work perfectly - * (if you have the bandwidth ;) - * A simple server is in TOOLS/netstream. - * - */ - - -#include "config.h" - -#include -#include -#include -#include - -#include -#include -#include -#include - -#if !HAVE_WINSOCK2_H -#include -#include -#include -#else -#include -#endif - -#include "mp_msg.h" -#include "stream.h" -#include "m_option.h" -#include "m_struct.h" -#include "libavutil/common.h" -#include "mpbswap.h" - -#include "network.h" -#include "stream_netstream.h" -#include "tcp.h" - -static struct stream_priv_s { - char* host; - int port; - char* url; -} stream_priv_dflts = { - NULL, - 10000, - NULL -}; - -#define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f) -/// URL definition -static const m_option_t stream_opts_fields[] = { - {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL}, - {"port", ST_OFF(port), CONF_TYPE_INT, M_OPT_MIN, 1 ,0, NULL}, - {"filename", ST_OFF(url), CONF_TYPE_STRING, 0, 0 ,0, NULL}, - { NULL, NULL, 0, 0, 0, 0, NULL } -}; -static const struct m_struct_st stream_opts = { - "netstream", - sizeof(struct stream_priv_s), - &stream_priv_dflts, - stream_opts_fields -}; - -//// When the cache is running we need a lock as -//// fill_buffer is called from another proccess -static int lock_fd(int fd) { -#if !HAVE_WINSOCK2_H - struct flock lock; - - memset(&lock,0,sizeof(struct flock)); - lock.l_type = F_WRLCK; - - mp_msg(MSGT_STREAM,MSGL_DBG2, "Lock (%d)\n",getpid()); - do { - if(fcntl(fd,F_SETLKW,&lock)) { - if(errno == EAGAIN) continue; - mp_msg(MSGT_STREAM,MSGL_ERR, "Failed to get the lock: %s\n", - strerror(errno)); - return 0; - } - } while(0); - mp_msg(MSGT_STREAM,MSGL_DBG2, "Locked (%d)\n",getpid()); -#else -printf("FIXME? should lock here\n"); -#endif - return 1; -} - -static int unlock_fd(int fd) { -#if !HAVE_WINSOCK2_H - struct flock lock; - - memset(&lock,0,sizeof(struct flock)); - lock.l_type = F_UNLCK; - - mp_msg(MSGT_STREAM,MSGL_DBG2, "Unlock (%d)\n",getpid()); - if(fcntl(fd,F_SETLK,&lock)) { - mp_msg(MSGT_STREAM,MSGL_ERR, "Failed to release the lock: %s\n", - strerror(errno)); - return 0; - } -#else -printf("FIXME? should unlock here\n"); -#endif - return 1; -} - -static mp_net_stream_packet_t* send_net_stream_cmd(stream_t *s,uint16_t cmd,char* data,int len) { - mp_net_stream_packet_t* pack; - - // Cache is enabled : lock - if(s->cache_data && !lock_fd(s->fd)) - return NULL; - // Send a command - if(!write_packet(s->fd,cmd,data,len)) { - if(s->cache_data) unlock_fd(s->fd); - return 0; - } - // Read the response - pack = read_packet(s->fd); - // Now we can unlock - if(s->cache_data) unlock_fd(s->fd); - - if(!pack) - return NULL; - - switch(pack->cmd) { - case NET_STREAM_OK: - return pack; - case NET_STREAM_ERROR: - if(pack->len > sizeof(mp_net_stream_packet_t)) - mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed: %s\n",pack->data); - else - mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed\n"); - free(pack); - return NULL; - } - - mp_msg(MSGT_STREAM,MSGL_ERR, "Unknown response to %d: %d\n",cmd,pack->cmd); - free(pack); - return NULL; -} - -static int fill_buffer(stream_t *s, char* buffer, int max_len){ - uint16_t len = le2me_16(max_len); - mp_net_stream_packet_t* pack; - - pack = send_net_stream_cmd(s,NET_STREAM_FILL_BUFFER,(char*)&len,2); - if(!pack) { - return -1; - } - len = pack->len - sizeof(mp_net_stream_packet_t); - if(len > max_len) { - mp_msg(MSGT_STREAM,MSGL_ERR, "Got a too big a packet %d / %d\n",len,max_len); - free(pack); - return 0; - } - if(len > 0) - memcpy(buffer,pack->data,len); - free(pack); - return len; -} - - -static int seek(stream_t *s,off_t newpos) { - uint64_t pos = le2me_64((uint64_t)newpos); - mp_net_stream_packet_t* pack; - - pack = send_net_stream_cmd(s,NET_STREAM_SEEK,(char*)&pos,8); - if(!pack) { - return 0; - } - s->pos = newpos; - free(pack); - return 1; -} - -static int net_stream_reset(struct stream *s) { - mp_net_stream_packet_t* pack; - - pack = send_net_stream_cmd(s,NET_STREAM_RESET,NULL,0); - if(!pack) { - return 0; - } - free(pack); - return 1; -} - -static int control(struct stream *s,int cmd,void* arg) { - switch(cmd) { - case STREAM_CTRL_RESET: - return net_stream_reset(s); - } - return STREAM_UNSUPPORTED; -} - -static void close_s(struct stream *s) { - mp_net_stream_packet_t* pack; - - pack = send_net_stream_cmd(s,NET_STREAM_CLOSE,NULL,0); - free(pack); -} - -static int open_s(stream_t *stream,int mode, void* opts, int* file_format) { - int f; - struct stream_priv_s* p = (struct stream_priv_s*)opts; - mp_net_stream_packet_t* pack; - mp_net_stream_opened_t* opened; - - if(mode != STREAM_READ) - return STREAM_UNSUPPORTED; - - if(!p->host) { - mp_msg(MSGT_OPEN,MSGL_ERR, "We need an host name (ex: mpst://server.net/cdda://5)\n"); - m_struct_free(&stream_opts,opts); - return STREAM_ERROR; - } - if(!p->url || strlen(p->url) == 0) { - mp_msg(MSGT_OPEN,MSGL_ERR, "We need a remote url (ex: mpst://server.net/cdda://5)\n"); - m_struct_free(&stream_opts,opts); - return STREAM_ERROR; - } - - f = connect2Server(p->host,p->port,1); - if(f < 0) { - mp_msg(MSGT_OPEN,MSGL_ERR, "Connection to %s:%d failed\n",p->host,p->port); - m_struct_free(&stream_opts,opts); - return STREAM_ERROR; - } - stream->fd = f; - /// Now send an open command - pack = send_net_stream_cmd(stream,NET_STREAM_OPEN,p->url,strlen(p->url) + 1); - if(!pack) { - goto error; - } - - if(pack->len != sizeof(mp_net_stream_packet_t) + - sizeof(mp_net_stream_opened_t)) { - mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid open response packet len (%d bytes)\n",pack->len); - free(pack); - goto error; - } - - opened = (mp_net_stream_opened_t*)pack->data; - net_stream_opened_2_me(opened); - - *file_format = opened->file_format; - stream->flags = opened->flags; - stream->sector_size = opened->sector_size; - stream->start_pos = opened->start_pos; - stream->end_pos = opened->end_pos; - - stream->fill_buffer = fill_buffer; - stream->control = control; - if(stream->flags & MP_STREAM_SEEK) - stream->seek = seek; - stream->close = close_s; - - free(pack); - m_struct_free(&stream_opts,opts); - - return STREAM_OK; - - error: - closesocket(f); - m_struct_free(&stream_opts,opts); - return STREAM_ERROR; -} - -const stream_info_t stream_info_netstream = { - "Net stream", - "netstream", - "Albeu", - "", - open_s, - { "mpst",NULL }, - &stream_opts, - 1 // Url is an option string -}; diff --git a/stream/stream_netstream.h b/stream/stream_netstream.h deleted file mode 100644 index 3843ee938d..0000000000 --- a/stream/stream_netstream.h +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Common stuff for netstream - * Packets and so on are defined here along with a few helpers - * wich are used by both the client and the server - * - * Data is always low endian - * - * This file is part of MPlayer. - * - * MPlayer is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MPlayer is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPLAYER_NETSTREAM_H -#define MPLAYER_NETSTREAM_H - -#include "config.h" -#include -#include -#include -#include -#if !HAVE_WINSOCK2_H -#include -#endif -#include "mp_msg.h" -#include "mpbswap.h" - -typedef struct mp_net_stream_packet_st { - uint16_t len; - uint8_t cmd; - char data[0]; -} __attribute__ ((packed)) mp_net_stream_packet_t; - -#define PACKET_MAX_SIZE 4096 - -// Commands sent by the client -#define NET_STREAM_OPEN 0 -// data is the url -#define NET_STREAM_FILL_BUFFER 1 -// data is an uint16 wich is the max len of the data to return -#define NET_STREAM_SEEK 3 -// data is an uint64 wich the pos where to seek -#define NET_STREAM_CLOSE 4 -// no data -#define NET_STREAM_RESET 5 -// no data - -// Server response -#define NET_STREAM_OK 128 -// Data returned if open is successful -typedef struct mp_net_stream_opened_st { - uint32_t file_format; - uint32_t flags; - uint32_t sector_size; - uint64_t start_pos; - uint64_t end_pos; -} __attribute__ ((packed)) mp_net_stream_opened_t; -// FILL_BUFFER return the data -// CLOSE return nothing -#define NET_STREAM_ERROR 129 -// Data is the error message (if any ;) - -static int net_read(int fd, char* buf, int len) { - int r = 0; - while(len) { - r = recv(fd,buf,len,0); - if(r <= 0) { - if(errno == EINTR) continue; - if(r < 0) - mp_msg(MSGT_NETST,MSGL_ERR,"Read failed: %s\n",strerror(errno)); - return 0; - } - len -= r; - buf += r; - } - return 1; -} - -static mp_net_stream_packet_t* read_packet(int fd) { - uint16_t len; - mp_net_stream_packet_t* pack = malloc(sizeof(mp_net_stream_packet_t)); - - if(!net_read(fd,(char*)pack,sizeof(mp_net_stream_packet_t))) { - free(pack); - return NULL; - } - pack->len = le2me_16(pack->len); - - if(pack->len < sizeof(mp_net_stream_packet_t)) { - mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too small: %d)\n",pack->len); - free(pack); - return NULL; - } - if(pack->len > PACKET_MAX_SIZE) { - mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len); - free(pack); - return NULL; - } - len = pack->len; - if(len > sizeof(mp_net_stream_packet_t)) { - pack = realloc(pack,len); - if(!pack) { - mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len); - return NULL; - } - if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t))) - return NULL; - } - // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len); - return pack; -} - -static int net_write(int fd, char* buf, int len) { - int w; - while(len) { - w = send(fd,buf,len,DEFAULT_SEND_FLAGS); - if(w <= 0) { - if(errno == EINTR) continue; - if(w < 0) - mp_msg(MSGT_NETST,MSGL_ERR,"Write failed: %s\n",strerror(errno)); - return 0; - } - len -= w; - buf += w; - } - return 1; -} - -static int write_packet(int fd, uint8_t cmd,char* data,int len) { - mp_net_stream_packet_t* pack = malloc(len + sizeof(mp_net_stream_packet_t)); - - if(len > 0 && data) - memcpy(pack->data,data,len); - pack->len = len + sizeof(mp_net_stream_packet_t); - pack->cmd = cmd; - - // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len); - pack->len = le2me_16(pack->len); - if(net_write(fd,(char*)pack,pack->len)) { - free(pack); - return 1; - } - free(pack); - return 0; -} - -static void net_stream_opened_2_me(mp_net_stream_opened_t* o) { - o->file_format = le2me_32(o->file_format); - o->flags = le2me_32(o->flags); - o->sector_size = le2me_32(o->sector_size); - o->start_pos = le2me_64(o->start_pos); - o->end_pos = le2me_64(o->end_pos); -} - -#endif /* MPLAYER_NETSTREAM_H */ -- 2.11.4.GIT