ad_ffmpeg: remove incorrect request_sample_fmt setting
[mplayer.git] / stream / stream_netstream.h
blob3843ee938d9e9adbab47a5c2197b7dfea9c8aeec
1 /*
2 * Common stuff for netstream
3 * Packets and so on are defined here along with a few helpers
4 * wich are used by both the client and the server
6 * Data is always low endian
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #ifndef MPLAYER_NETSTREAM_H
26 #define MPLAYER_NETSTREAM_H
28 #include "config.h"
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #if !HAVE_WINSOCK2_H
34 #include <sys/socket.h>
35 #endif
36 #include "mp_msg.h"
37 #include "mpbswap.h"
39 typedef struct mp_net_stream_packet_st {
40 uint16_t len;
41 uint8_t cmd;
42 char data[0];
43 } __attribute__ ((packed)) mp_net_stream_packet_t;
45 #define PACKET_MAX_SIZE 4096
47 // Commands sent by the client
48 #define NET_STREAM_OPEN 0
49 // data is the url
50 #define NET_STREAM_FILL_BUFFER 1
51 // data is an uint16 wich is the max len of the data to return
52 #define NET_STREAM_SEEK 3
53 // data is an uint64 wich the pos where to seek
54 #define NET_STREAM_CLOSE 4
55 // no data
56 #define NET_STREAM_RESET 5
57 // no data
59 // Server response
60 #define NET_STREAM_OK 128
61 // Data returned if open is successful
62 typedef struct mp_net_stream_opened_st {
63 uint32_t file_format;
64 uint32_t flags;
65 uint32_t sector_size;
66 uint64_t start_pos;
67 uint64_t end_pos;
68 } __attribute__ ((packed)) mp_net_stream_opened_t;
69 // FILL_BUFFER return the data
70 // CLOSE return nothing
71 #define NET_STREAM_ERROR 129
72 // Data is the error message (if any ;)
74 static int net_read(int fd, char* buf, int len) {
75 int r = 0;
76 while(len) {
77 r = recv(fd,buf,len,0);
78 if(r <= 0) {
79 if(errno == EINTR) continue;
80 if(r < 0)
81 mp_msg(MSGT_NETST,MSGL_ERR,"Read failed: %s\n",strerror(errno));
82 return 0;
84 len -= r;
85 buf += r;
87 return 1;
90 static mp_net_stream_packet_t* read_packet(int fd) {
91 uint16_t len;
92 mp_net_stream_packet_t* pack = malloc(sizeof(mp_net_stream_packet_t));
94 if(!net_read(fd,(char*)pack,sizeof(mp_net_stream_packet_t))) {
95 free(pack);
96 return NULL;
98 pack->len = le2me_16(pack->len);
100 if(pack->len < sizeof(mp_net_stream_packet_t)) {
101 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too small: %d)\n",pack->len);
102 free(pack);
103 return NULL;
105 if(pack->len > PACKET_MAX_SIZE) {
106 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len);
107 free(pack);
108 return NULL;
110 len = pack->len;
111 if(len > sizeof(mp_net_stream_packet_t)) {
112 pack = realloc(pack,len);
113 if(!pack) {
114 mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len);
115 return NULL;
117 if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t)))
118 return NULL;
120 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
121 return pack;
124 static int net_write(int fd, char* buf, int len) {
125 int w;
126 while(len) {
127 w = send(fd,buf,len,DEFAULT_SEND_FLAGS);
128 if(w <= 0) {
129 if(errno == EINTR) continue;
130 if(w < 0)
131 mp_msg(MSGT_NETST,MSGL_ERR,"Write failed: %s\n",strerror(errno));
132 return 0;
134 len -= w;
135 buf += w;
137 return 1;
140 static int write_packet(int fd, uint8_t cmd,char* data,int len) {
141 mp_net_stream_packet_t* pack = malloc(len + sizeof(mp_net_stream_packet_t));
143 if(len > 0 && data)
144 memcpy(pack->data,data,len);
145 pack->len = len + sizeof(mp_net_stream_packet_t);
146 pack->cmd = cmd;
148 // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len);
149 pack->len = le2me_16(pack->len);
150 if(net_write(fd,(char*)pack,pack->len)) {
151 free(pack);
152 return 1;
154 free(pack);
155 return 0;
158 static void net_stream_opened_2_me(mp_net_stream_opened_t* o) {
159 o->file_format = le2me_32(o->file_format);
160 o->flags = le2me_32(o->flags);
161 o->sector_size = le2me_32(o->sector_size);
162 o->start_pos = le2me_64(o->start_pos);
163 o->end_pos = le2me_64(o->end_pos);
166 #endif /* MPLAYER_NETSTREAM_H */