annotate stream/stream_netstream.c @ 37188:32aab8a15bfc

bluray: Fix STREAM_CTRL_SEEK_TO_TIME for out-of-bound values.
author reimar
date Wed, 17 Sep 2014 18:16:22 +0000
parents f8f6da5f5c57
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
1 /*
26737
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
2 * Copyright (C) Alban Bedel - 04/2003
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
3 *
26737
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
4 * This file is part of MPlayer.
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
5 *
26737
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
6 * MPlayer is free software; you can redistribute it and/or modify
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
7 * it under the terms of the GNU General Public License as published by
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
9 * (at your option) any later version.
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
10 *
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
11 * MPlayer is distributed in the hope that it will be useful,
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
14 * GNU General Public License for more details.
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
15 *
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
16 * You should have received a copy of the GNU General Public License along
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
a26e50cae389 Use standard license headers with standard formatting.
diego
parents: 25691
diff changeset
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
19 */
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 /*
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
22 * Net stream allow you to access MPlayer stream accross a tcp
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
23 * connection.
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
24 * Note that at least mf and tv use a dummy stream (they are
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
25 * implemented at the demuxer level) so you won't be able to
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
26 * access those :(( but dvd, vcd and so on should work perfectly
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
27 * (if you have the bandwidth ;)
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
28 * A simple server is in TOOLS/netstream.
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
29 *
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
30 */
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
31
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 #include "config.h"
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
34
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
35 #include <sys/types.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
36 #include <sys/stat.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
37 #include <fcntl.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
38 #include <unistd.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
39
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
40 #include <stdlib.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
41 #include <stdio.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
42 #include <inttypes.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
43 #include <errno.h>
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
44
28402
c884d17bd005 Convert HAVE_WINSOCK2_H into a 0/1 definition.
diego
parents: 27745
diff changeset
45 #if !HAVE_WINSOCK2_H
27472
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
46 #include <sys/socket.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
47 #include <netinet/in.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
48 #include <arpa/inet.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
49 #else
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
50 #include <winsock2.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
51 #endif
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
52
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
53 #include "mp_msg.h"
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
54 #include "stream.h"
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
55 #include "help_mp.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 10735
diff changeset
56 #include "m_option.h"
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 10735
diff changeset
57 #include "m_struct.h"
21372
1767c271d710 Remove bswap.h, use libavutil/bswap.h instead.
diego
parents: 19335
diff changeset
58 #include "libavutil/common.h"
21507
fa99b3d31d13 Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents: 21372
diff changeset
59 #include "mpbswap.h"
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
60
27473
ae5da477539e Move '#define closesocket close' preprocessor directive to a common place
diego
parents: 27472
diff changeset
61 #include "network.h"
27745
a5ed200519dc Rename stream/netstream.h to stream/stream_netstream.h; netstream.h to make it
diego
parents: 27473
diff changeset
62 #include "stream_netstream.h"
19335
2a9d669e5ff6 isolated tcp socket code from network.c to a dedicated file
ben
parents: 19271
diff changeset
63 #include "tcp.h"
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
64
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
65 static struct stream_priv_s {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
66 char* host;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
67 int port;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
68 char* url;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
69 } stream_priv_dflts = {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
70 NULL,
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
71 10000,
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
72 NULL
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
73 };
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
74
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
75 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
76 /// URL definition
25242
371a40dcc1cc stream_opts arrays should be const
reimar
parents: 25211
diff changeset
77 static const m_option_t stream_opts_fields[] = {
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
78 {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL},
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
79 {"port", ST_OFF(port), CONF_TYPE_INT, M_OPT_MIN, 1 ,0, NULL},
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
80 {"filename", ST_OFF(url), CONF_TYPE_STRING, 0, 0 ,0, NULL},
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
81 { NULL, NULL, 0, 0, 0, 0, NULL }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
82 };
25691
68015115f63a stream_opts should be const
reimar
parents: 25242
diff changeset
83 static const struct m_struct_st stream_opts = {
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
84 "netstream",
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
85 sizeof(struct stream_priv_s),
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
86 &stream_priv_dflts,
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
87 stream_opts_fields
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
88 };
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
89
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
90 //// When the cache is running we need a lock as
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
91 //// fill_buffer is called from another proccess
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
92 static int lock_fd(int fd) {
28402
c884d17bd005 Convert HAVE_WINSOCK2_H into a 0/1 definition.
diego
parents: 27745
diff changeset
93 #if !HAVE_WINSOCK2_H
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
94 struct flock lock;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
95
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
96 memset(&lock,0,sizeof(struct flock));
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
97 lock.l_type = F_WRLCK;
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 mp_msg(MSGT_STREAM,MSGL_DBG2, "Lock (%d)\n",getpid());
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
100 do {
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
101 if(fcntl(fd,F_SETLKW,&lock)) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
102 if(errno == EAGAIN) continue;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
103 mp_msg(MSGT_STREAM,MSGL_ERR, "Failed to get the lock: %s\n",
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
104 strerror(errno));
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
105 return 0;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
106 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
107 } while(0);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
108 mp_msg(MSGT_STREAM,MSGL_DBG2, "Locked (%d)\n",getpid());
10281
54bcbf28698a Networking support under MinGW.
diego
parents: 10121
diff changeset
109 #else
54bcbf28698a Networking support under MinGW.
diego
parents: 10121
diff changeset
110 printf("FIXME? should lock here\n");
54bcbf28698a Networking support under MinGW.
diego
parents: 10121
diff changeset
111 #endif
9850
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 unlock_fd(int fd) {
28402
c884d17bd005 Convert HAVE_WINSOCK2_H into a 0/1 definition.
diego
parents: 27745
diff changeset
116 #if !HAVE_WINSOCK2_H
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
117 struct flock lock;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
118
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
119 memset(&lock,0,sizeof(struct flock));
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
120 lock.l_type = F_UNLCK;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
121
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
122 mp_msg(MSGT_STREAM,MSGL_DBG2, "Unlock (%d)\n",getpid());
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
123 if(fcntl(fd,F_SETLK,&lock)) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
124 mp_msg(MSGT_STREAM,MSGL_ERR, "Failed to release the lock: %s\n",
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
125 strerror(errno));
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
126 return 0;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
127 }
10281
54bcbf28698a Networking support under MinGW.
diego
parents: 10121
diff changeset
128 #else
54bcbf28698a Networking support under MinGW.
diego
parents: 10121
diff changeset
129 printf("FIXME? should unlock here\n");
54bcbf28698a Networking support under MinGW.
diego
parents: 10121
diff changeset
130 #endif
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
131 return 1;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
132 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
133
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
134 static mp_net_stream_packet_t* send_net_stream_cmd(stream_t *s,uint16_t cmd,char* data,int len) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
135 mp_net_stream_packet_t* pack;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
136
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
137 // Cache is enabled : lock
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
138 if(s->cache_data && !lock_fd(s->fd))
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
139 return NULL;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
140 // Send a command
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
141 if(!write_packet(s->fd,cmd,data,len)) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
142 if(s->cache_data) unlock_fd(s->fd);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
143 return 0;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
144 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
145 // Read the response
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
146 pack = read_packet(s->fd);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
147 // Now we can unlock
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
148 if(s->cache_data) unlock_fd(s->fd);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
149
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
150 if(!pack)
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
151 return NULL;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
152
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
153 switch(pack->cmd) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
154 case NET_STREAM_OK:
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
155 return pack;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
156 case NET_STREAM_ERROR:
36633
f8f6da5f5c57 stream_netstream: Simplify packet handling.
reimar
parents: 36632
diff changeset
157 if(pack->len > 0)
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
158 mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed: %s\n",pack->data);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
159 else
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
160 mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed\n");
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
161 free(pack);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
162 return NULL;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
163 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
164
10735
8a10d5d0ce86 serious bugs - 1l absinth (changed to absinth against cola inflation)
alex
parents: 10625
diff changeset
165 mp_msg(MSGT_STREAM,MSGL_ERR, "Unknown response to %d: %d\n",cmd,pack->cmd);
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
166 free(pack);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
167 return NULL;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
168 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
169
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
170 static int fill_buffer(stream_t *s, char* buffer, int max_len){
9863
4c6c6c361f24 It should now be endian aware. Untested as i only have le box :(
albeu
parents: 9850
diff changeset
171 uint16_t len = le2me_16(max_len);
36632
43575335eab4 stream_netstream: merge declaration and initialization.
reimar
parents: 35885
diff changeset
172 mp_net_stream_packet_t *pack = send_net_stream_cmd(s,NET_STREAM_FILL_BUFFER,(char*)&len,2);
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
173 if(!pack) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
174 return -1;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
175 }
36633
f8f6da5f5c57 stream_netstream: Simplify packet handling.
reimar
parents: 36632
diff changeset
176 len = pack->len;
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
177 if(len > max_len) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
178 mp_msg(MSGT_STREAM,MSGL_ERR, "Got a too big a packet %d / %d\n",len,max_len);
36633
f8f6da5f5c57 stream_netstream: Simplify packet handling.
reimar
parents: 36632
diff changeset
179 goto err_out;
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
180 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
181 if(len > 0)
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
182 memcpy(buffer,pack->data,len);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
183 free(pack);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
184 return len;
36633
f8f6da5f5c57 stream_netstream: Simplify packet handling.
reimar
parents: 36632
diff changeset
185 err_out:
f8f6da5f5c57 stream_netstream: Simplify packet handling.
reimar
parents: 36632
diff changeset
186 free(pack);
f8f6da5f5c57 stream_netstream: Simplify packet handling.
reimar
parents: 36632
diff changeset
187 return 0;
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
188 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
189
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
190
35885
3389262720da Fix previous commit, off_t must be replaced by int64_t
reimar
parents: 35881
diff changeset
191 static int seek(stream_t *s, int64_t newpos) {
35881
b5abdfe9bc61 Replace some uses of off_t by uint64_t.
reimar
parents: 32511
diff changeset
192 uint64_t pos = le2me_64(newpos);
36632
43575335eab4 stream_netstream: merge declaration and initialization.
reimar
parents: 35885
diff changeset
193 mp_net_stream_packet_t *pack = send_net_stream_cmd(s,NET_STREAM_SEEK,(char*)&pos,8);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
194 if(!pack) {
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
195 return 0;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
196 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
197 s->pos = newpos;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
198 free(pack);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
199 return 1;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
200 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
201
31179
de190efe4da3 Drop pointless _st suffix from 'struct stream'.
diego
parents: 29920
diff changeset
202 static int net_stream_reset(struct stream *s) {
36632
43575335eab4 stream_netstream: merge declaration and initialization.
reimar
parents: 35885
diff changeset
203 mp_net_stream_packet_t *pack = send_net_stream_cmd(s,NET_STREAM_RESET,NULL,0);
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
204 if(!pack) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
205 return 0;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
206 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
207 free(pack);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
208 return 1;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
209 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
210
31179
de190efe4da3 Drop pointless _st suffix from 'struct stream'.
diego
parents: 29920
diff changeset
211 static int control(struct stream *s,int cmd,void* arg) {
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
212 switch(cmd) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
213 case STREAM_CTRL_RESET:
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
214 return net_stream_reset(s);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
215 }
24257
d261f5109660 cosmetics: typo fix UNSUPORTED --> UNSUPPORTED
diego
parents: 21977
diff changeset
216 return STREAM_UNSUPPORTED;
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
217 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
218
31179
de190efe4da3 Drop pointless _st suffix from 'struct stream'.
diego
parents: 29920
diff changeset
219 static void close_s(struct stream *s) {
36632
43575335eab4 stream_netstream: merge declaration and initialization.
reimar
parents: 35885
diff changeset
220 mp_net_stream_packet_t *pack = send_net_stream_cmd(s,NET_STREAM_CLOSE,NULL,0);
32511
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 31179
diff changeset
221 free(pack);
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
222 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
223
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
224 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
225 int f;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
226 struct stream_priv_s* p = (struct stream_priv_s*)opts;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
227 mp_net_stream_packet_t* pack;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
228 mp_net_stream_opened_t* opened;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
229
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
230 if(mode != STREAM_READ)
24257
d261f5109660 cosmetics: typo fix UNSUPORTED --> UNSUPPORTED
diego
parents: 21977
diff changeset
231 return STREAM_UNSUPPORTED;
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
232
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
233 if(!p->host) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
234 mp_msg(MSGT_OPEN,MSGL_ERR, "We need an host name (ex: mpst://server.net/cdda://5)\n");
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
235 m_struct_free(&stream_opts,opts);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
236 return STREAM_ERROR;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
237 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
238 if(!p->url || strlen(p->url) == 0) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
239 mp_msg(MSGT_OPEN,MSGL_ERR, "We need a remote url (ex: mpst://server.net/cdda://5)\n");
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
240 m_struct_free(&stream_opts,opts);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
241 return STREAM_ERROR;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
242 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
243
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents: 10608
diff changeset
244 f = connect2Server(p->host,p->port,1);
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
245 if(f < 0) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
246 mp_msg(MSGT_OPEN,MSGL_ERR, "Connection to %s:%d failed\n",p->host,p->port);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
247 m_struct_free(&stream_opts,opts);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
248 return STREAM_ERROR;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
249 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
250 stream->fd = f;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
251 /// Now send an open command
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
252 pack = send_net_stream_cmd(stream,NET_STREAM_OPEN,p->url,strlen(p->url) + 1);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
253 if(!pack) {
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
254 goto error;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
255 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
256
36633
f8f6da5f5c57 stream_netstream: Simplify packet handling.
reimar
parents: 36632
diff changeset
257 if(pack->len != sizeof(mp_net_stream_opened_t)) {
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
258 mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid open response packet len (%d bytes)\n",pack->len);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
259 free(pack);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
260 goto error;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
261 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
262
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
263 opened = (mp_net_stream_opened_t*)pack->data;
9863
4c6c6c361f24 It should now be endian aware. Untested as i only have le box :(
albeu
parents: 9850
diff changeset
264 net_stream_opened_2_me(opened);
4c6c6c361f24 It should now be endian aware. Untested as i only have le box :(
albeu
parents: 9850
diff changeset
265
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
266 *file_format = opened->file_format;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
267 stream->flags = opened->flags;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
268 stream->sector_size = opened->sector_size;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
269 stream->start_pos = opened->start_pos;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
270 stream->end_pos = opened->end_pos;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
271
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
272 stream->fill_buffer = fill_buffer;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
273 stream->control = control;
29920
4f740437ed2b Finally rename the STREAM_SEEK define to MP_STREAM_SEEK, there are just too many
reimar
parents: 29263
diff changeset
274 if(stream->flags & MP_STREAM_SEEK)
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
275 stream->seek = seek;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
276 stream->close = close_s;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
277
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
278 free(pack);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
279 m_struct_free(&stream_opts,opts);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
280
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
281 return STREAM_OK;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
282
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
283 error:
10281
54bcbf28698a Networking support under MinGW.
diego
parents: 10121
diff changeset
284 closesocket(f);
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
285 m_struct_free(&stream_opts,opts);
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
286 return STREAM_ERROR;
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
287 }
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
288
25211
c1d17bd6683c Mark all stream_info_t as const
reimar
parents: 24257
diff changeset
289 const stream_info_t stream_info_netstream = {
9850
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
290 "Net stream",
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
291 "netstream",
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
292 "Albeu",
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
293 "",
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
294 open_s,
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
295 { "mpst",NULL },
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
296 &stream_opts,
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
297 1 // Url is an option string
564678d0bc15 A new stream wich allow access to MPlayer stream accross the network.
albeu
parents:
diff changeset
298 };