Mercurial > mplayer.hg
annotate stream/netstream.h @ 26155:ba3e34112e47
sync with FFmpeg r12354
author | diego |
---|---|
date | Fri, 07 Mar 2008 00:22:09 +0000 |
parents | 4129c8cfa742 |
children | 7ee4ae1648e6 |
rev | line source |
---|---|
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
1 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
2 /* |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
3 * Common stuff for netstream |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
4 * Packets and so on are defined here along with a few helpers |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
5 * wich are used by both the client and the server |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
6 * |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
7 * Data is always low endian |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
8 */ |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
9 |
26029 | 10 #ifndef MPLAYER_NETSTREAM_H |
11 #define MPLAYER_NETSTREAM_H | |
25553
6ac1ece1f9fe
Add multiple inclusion guards to all header files that lack them.
diego
parents:
19271
diff
changeset
|
12 |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
13 typedef struct mp_net_stream_packet_st { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
14 uint16_t len; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
15 uint8_t cmd; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
16 char data[0]; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
17 } __attribute__ ((packed)) mp_net_stream_packet_t; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
18 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
19 #define PACKET_MAX_SIZE 4096 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
20 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
21 // Commands sent by the client |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
22 #define NET_STREAM_OPEN 0 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
23 // data is the url |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
24 #define NET_STREAM_FILL_BUFFER 1 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
25 // data is an uint16 wich is the max len of the data to return |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
26 #define NET_STREAM_SEEK 3 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
27 // data is an uint64 wich the pos where to seek |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
28 #define NET_STREAM_CLOSE 4 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
29 // no data |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
30 #define NET_STREAM_RESET 5 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
31 // no data |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
32 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
33 // Server response |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
34 #define NET_STREAM_OK 128 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
35 // Data returned if open is successful |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
36 typedef struct mp_net_stream_opened_st { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
37 uint32_t file_format; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
38 uint32_t flags; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
39 uint32_t sector_size; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
40 uint64_t start_pos; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
41 uint64_t end_pos; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
42 } __attribute__ ((packed)) mp_net_stream_opened_t; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
43 // FILL_BUFFER return the data |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
44 // CLOSE return nothing |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
45 #define NET_STREAM_ERROR 129 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
46 // Data is the error message (if any ;) |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
47 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
48 static int net_read(int fd, char* buf, int len) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
49 int r = 0; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
50 while(len) { |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9863
diff
changeset
|
51 r = recv(fd,buf,len,0); |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
52 if(r <= 0) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
53 if(errno == EINTR) continue; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
54 if(r < 0) |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
55 mp_msg(MSGT_NETST,MSGL_ERR,"Read failed: %s\n",strerror(errno)); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
56 return 0; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
57 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
58 len -= r; |
12640
7513f753af91
tcp fragging bugfix by Song Du <freewizard at gmail.com>
alex
parents:
10206
diff
changeset
|
59 buf += r; |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
60 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
61 return 1; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
62 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
63 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
64 static mp_net_stream_packet_t* read_packet(int fd) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
65 uint16_t len; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
66 mp_net_stream_packet_t* pack = |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
67 (mp_net_stream_packet_t*)malloc(sizeof(mp_net_stream_packet_t)); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
68 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
69 if(!net_read(fd,(char*)pack,sizeof(mp_net_stream_packet_t))) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
70 free(pack); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
71 return NULL; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
72 } |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
73 pack->len = le2me_16(pack->len); |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
74 |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
75 if(pack->len < sizeof(mp_net_stream_packet_t)) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
76 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too small: %d)\n",pack->len); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
77 free(pack); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
78 return NULL; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
79 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
80 if(pack->len > PACKET_MAX_SIZE) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
81 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
82 free(pack); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
83 return NULL; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
84 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
85 len = pack->len; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
86 if(len > sizeof(mp_net_stream_packet_t)) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
87 pack = realloc(pack,len); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
88 if(!pack) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
89 mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
90 return NULL; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
91 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
92 if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t))) |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
93 return NULL; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
94 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
95 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
96 return pack; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
97 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
98 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
99 static int net_write(int fd, char* buf, int len) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
100 int w; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
101 while(len) { |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9863
diff
changeset
|
102 w = send(fd,buf,len,0); |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
103 if(w <= 0) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
104 if(errno == EINTR) continue; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
105 if(w < 0) |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
106 mp_msg(MSGT_NETST,MSGL_ERR,"Write failed: %s\n",strerror(errno)); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
107 return 0; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
108 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
109 len -= w; |
12640
7513f753af91
tcp fragging bugfix by Song Du <freewizard at gmail.com>
alex
parents:
10206
diff
changeset
|
110 buf += w; |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
111 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
112 return 1; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
113 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
114 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
115 static int write_packet(int fd, uint8_t cmd,char* data,int len) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
116 mp_net_stream_packet_t* pack = malloc(len + sizeof(mp_net_stream_packet_t)); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
117 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
118 if(len > 0 && data) |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
119 memcpy(pack->data,data,len); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
120 pack->len = len + sizeof(mp_net_stream_packet_t); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
121 pack->cmd = cmd; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
122 |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
123 // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len); |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
124 pack->len = le2me_16(pack->len); |
9850
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
125 if(net_write(fd,(char*)pack,pack->len)) { |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
126 free(pack); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
127 return 1; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
128 } |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
129 free(pack); |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
130 return 0; |
564678d0bc15
A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff
changeset
|
131 } |
9863
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
132 |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
133 static void net_stream_opened_2_me(mp_net_stream_opened_t* o) { |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
134 o->file_format = le2me_32(o->file_format); |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
135 o->flags = le2me_32(o->flags); |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
136 o->sector_size = le2me_32(o->sector_size); |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
137 o->start_pos = le2me_64(o->start_pos); |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
138 o->end_pos = le2me_64(o->end_pos); |
4c6c6c361f24
It should now be endian aware. Untested as i only have le box :(
albeu
parents:
9850
diff
changeset
|
139 } |
25553
6ac1ece1f9fe
Add multiple inclusion guards to all header files that lack them.
diego
parents:
19271
diff
changeset
|
140 |
26029 | 141 #endif /* MPLAYER_NETSTREAM_H */ |