Mercurial > mplayer.hg
comparison libmpdemux/netstream.h @ 9850:564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
URL is mpst://host[:port]/remote_url where remote_url is any valid
MPlayer url.
author | albeu |
---|---|
date | Sun, 06 Apr 2003 16:33:13 +0000 |
parents | |
children | 4c6c6c361f24 |
comparison
equal
deleted
inserted
replaced
9849:5b649442fe72 | 9850:564678d0bc15 |
---|---|
1 | |
2 /* | |
3 * Common stuff for netstream | |
4 * Packets and so on are defined here along with a few helpers | |
5 * wich are used by both the client and the server | |
6 */ | |
7 | |
8 typedef struct mp_net_stream_packet_st { | |
9 uint16_t len; | |
10 uint8_t cmd; | |
11 char data[0]; | |
12 } __attribute__ ((packed)) mp_net_stream_packet_t; | |
13 | |
14 #define PACKET_MAX_SIZE 4096 | |
15 | |
16 // Commands sent by the client | |
17 #define NET_STREAM_OPEN 0 | |
18 // data is the url | |
19 #define NET_STREAM_FILL_BUFFER 1 | |
20 // data is an uint16 wich is the max len of the data to return | |
21 #define NET_STREAM_SEEK 3 | |
22 // data is an uint64 wich the pos where to seek | |
23 #define NET_STREAM_CLOSE 4 | |
24 // no data | |
25 #define NET_STREAM_RESET 5 | |
26 // no data | |
27 | |
28 // Server response | |
29 #define NET_STREAM_OK 128 | |
30 // Data returned if open is successful | |
31 typedef struct mp_net_stream_opened_st { | |
32 uint32_t file_format; | |
33 uint32_t flags; | |
34 uint32_t sector_size; | |
35 uint64_t start_pos; | |
36 uint64_t end_pos; | |
37 } __attribute__ ((packed)) mp_net_stream_opened_t; | |
38 // FILL_BUFFER return the data | |
39 // CLOSE return nothing | |
40 #define NET_STREAM_ERROR 129 | |
41 // Data is the error message (if any ;) | |
42 | |
43 static int net_read(int fd, char* buf, int len) { | |
44 int r = 0; | |
45 while(len) { | |
46 r = read(fd,buf,len); | |
47 if(r <= 0) { | |
48 if(errno == EINTR) continue; | |
49 if(r < 0) | |
50 mp_msg(MSGT_NETST,MSGL_ERR,"Read failed: %s\n",strerror(errno)); | |
51 return 0; | |
52 } | |
53 len -= r; | |
54 } | |
55 return 1; | |
56 } | |
57 | |
58 static mp_net_stream_packet_t* read_packet(int fd) { | |
59 uint16_t len; | |
60 mp_net_stream_packet_t* pack = | |
61 (mp_net_stream_packet_t*)malloc(sizeof(mp_net_stream_packet_t)); | |
62 | |
63 if(!net_read(fd,(char*)pack,sizeof(mp_net_stream_packet_t))) { | |
64 free(pack); | |
65 return NULL; | |
66 } | |
67 | |
68 if(pack->len < sizeof(mp_net_stream_packet_t)) { | |
69 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too small: %d)\n",pack->len); | |
70 free(pack); | |
71 return NULL; | |
72 } | |
73 if(pack->len > PACKET_MAX_SIZE) { | |
74 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len); | |
75 free(pack); | |
76 return NULL; | |
77 } | |
78 len = pack->len; | |
79 if(len > sizeof(mp_net_stream_packet_t)) { | |
80 pack = realloc(pack,len); | |
81 if(!pack) { | |
82 mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len); | |
83 return NULL; | |
84 } | |
85 if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t))) | |
86 return NULL; | |
87 } | |
88 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len); | |
89 return pack; | |
90 } | |
91 | |
92 static int net_write(int fd, char* buf, int len) { | |
93 int w; | |
94 while(len) { | |
95 w = write(fd,buf,len); | |
96 if(w <= 0) { | |
97 if(errno == EINTR) continue; | |
98 if(w < 0) | |
99 mp_msg(MSGT_NETST,MSGL_ERR,"Write failed: %s\n",strerror(errno)); | |
100 return 0; | |
101 } | |
102 len -= w; | |
103 } | |
104 return 1; | |
105 } | |
106 | |
107 static int write_packet(int fd, uint8_t cmd,char* data,int len) { | |
108 mp_net_stream_packet_t* pack = malloc(len + sizeof(mp_net_stream_packet_t)); | |
109 | |
110 if(len > 0 && data) | |
111 memcpy(pack->data,data,len); | |
112 pack->len = len + sizeof(mp_net_stream_packet_t); | |
113 pack->cmd = cmd; | |
114 | |
115 // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len); | |
116 if(net_write(fd,(char*)pack,pack->len)) { | |
117 free(pack); | |
118 return 1; | |
119 } | |
120 free(pack); | |
121 return 0; | |
122 } |