author | harlekin <harlekin> | 2002-04-22 21:47:09 (UTC) |
---|---|---|
committer | harlekin <harlekin> | 2002-04-22 21:47:09 (UTC) |
commit | f77f012023dbe7582a4b4297f61c6521ad8a3aa5 (patch) (unidiff) | |
tree | 3d41a0320169e8b2749e7cee98f14e0435fe05e3 | |
parent | cca7f8ae4f60ae9b6c704d200ab015233abdd4d7 (diff) | |
download | opie-f77f012023dbe7582a4b4297f61c6521ad8a3aa5.zip opie-f77f012023dbe7582a4b4297f61c6521ad8a3aa5.tar.gz opie-f77f012023dbe7582a4b4297f61c6521ad8a3aa5.tar.bz2 |
get shoutcast support better, now all stream information are shown in the mediaplayer
-rw-r--r-- | core/multimedia/opieplayer/libmad/libmadplugin.cpp | 449 |
1 files changed, 228 insertions, 221 deletions
diff --git a/core/multimedia/opieplayer/libmad/libmadplugin.cpp b/core/multimedia/opieplayer/libmad/libmadplugin.cpp index 7de4282..0f952f4 100644 --- a/core/multimedia/opieplayer/libmad/libmadplugin.cpp +++ b/core/multimedia/opieplayer/libmad/libmadplugin.cpp | |||
@@ -1,828 +1,835 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | // largly modified by Maximilian Reiss <max.reiss@gmx.de> | ||
21 | |||
20 | #include <stdio.h> | 22 | #include <stdio.h> |
21 | #include <stdarg.h> | 23 | #include <stdarg.h> |
22 | #include <stdlib.h> | 24 | #include <stdlib.h> |
23 | #include <sys/types.h> | 25 | #include <sys/types.h> |
24 | #include <sys/stat.h> | 26 | #include <sys/stat.h> |
25 | #include <fcntl.h> | 27 | #include <fcntl.h> |
26 | #include <unistd.h> | 28 | #include <unistd.h> |
27 | #include <string.h> | 29 | #include <string.h> |
28 | #include <ctype.h> | 30 | #include <ctype.h> |
29 | #include <errno.h> | 31 | #include <errno.h> |
30 | #include <time.h> | 32 | #include <time.h> |
31 | #include <locale.h> | 33 | #include <locale.h> |
32 | #include <math.h> | 34 | #include <math.h> |
33 | #include <assert.h> | 35 | #include <assert.h> |
36 | |||
34 | #include <qapplication.h> | 37 | #include <qapplication.h> |
38 | #include <qmessagebox.h> | ||
39 | |||
35 | #include <qpe/config.h> | 40 | #include <qpe/config.h> |
36 | 41 | ||
37 | // for network handling | 42 | // for network handling |
38 | #include <netinet/in.h> | 43 | #include <netinet/in.h> |
39 | #include <netdb.h> | 44 | #include <netdb.h> |
40 | #include <sys/socket.h> | 45 | #include <sys/socket.h> |
41 | #include <arpa/inet.h> | 46 | #include <arpa/inet.h> |
42 | #include <unistd.h> | 47 | #include <unistd.h> |
43 | 48 | ||
44 | 49 | ||
45 | //#define HAVE_MMAP | 50 | //#define HAVE_MMAP |
46 | 51 | ||
47 | #if defined(HAVE_MMAP) | 52 | #if defined(HAVE_MMAP) |
48 | # include <sys/mman.h> | 53 | # include <sys/mman.h> |
49 | #endif | 54 | #endif |
50 | #include "libmadplugin.h" | 55 | #include "libmadplugin.h" |
51 | 56 | ||
52 | 57 | ||
53 | extern "C" { | 58 | extern "C" { |
54 | #include "mad.h" | 59 | #include "mad.h" |
55 | } | 60 | } |
56 | 61 | ||
57 | 62 | ||
58 | #define MPEG_BUFFER_SIZE 65536 | 63 | #define MPEG_BUFFER_SIZE 65536 |
59 | //#define MPEG_BUFFER_SIZE 32768 //16384 // 8192 | 64 | //#define MPEG_BUFFER_SIZE 32768 //16384 // 8192 |
60 | //#define debugMsg(a) qDebug(a) | 65 | //#define debugMsg(a) qDebug(a) |
61 | #define debugMsg(a) | 66 | #define debugMsg(a) |
62 | 67 | ||
63 | 68 | ||
64 | class Input { | 69 | class Input { |
65 | public: | 70 | public: |
66 | char const *path; | 71 | char const *path; |
67 | int fd; | 72 | int fd; |
68 | #if defined(HAVE_MMAP) | 73 | #if defined(HAVE_MMAP) |
69 | void *fdm; | 74 | void *fdm; |
70 | #endif | 75 | #endif |
71 | unsigned char *data; | 76 | unsigned char *data; |
72 | unsigned long length; | 77 | unsigned long length; |
73 | int eof; | 78 | int eof; |
74 | }; | 79 | }; |
75 | 80 | ||
76 | 81 | ||
77 | class Output { | 82 | class Output { |
78 | public: | 83 | public: |
79 | mad_fixed_t attenuate; | 84 | mad_fixed_t attenuate; |
80 | struct filter *filters; | 85 | struct filter *filters; |
81 | unsigned int channels_in; | 86 | unsigned int channels_in; |
82 | unsigned int channels_out; | 87 | unsigned int channels_out; |
83 | unsigned int speed_in; | 88 | unsigned int speed_in; |
84 | unsigned int speed_out; | 89 | unsigned int speed_out; |
85 | const char *path; | 90 | const char *path; |
86 | }; | 91 | }; |
87 | 92 | ||
88 | 93 | ||
89 | # if defined(HAVE_MMAP) | 94 | # if defined(HAVE_MMAP) |
90 | static void *map_file(int fd, unsigned long *length) | 95 | static void *map_file(int fd, unsigned long *length) |
91 | { | 96 | { |
92 | void *fdm; | 97 | void *fdm; |
93 | 98 | ||
94 | *length += MAD_BUFFER_GUARD; | 99 | *length += MAD_BUFFER_GUARD; |
95 | 100 | ||
96 | fdm = mmap(0, *length, PROT_READ, MAP_SHARED, fd, 0); | 101 | fdm = mmap(0, *length, PROT_READ, MAP_SHARED, fd, 0); |
97 | if (fdm == MAP_FAILED) | 102 | if (fdm == MAP_FAILED) |
98 | return 0; | 103 | return 0; |
99 | 104 | ||
100 | # if defined(HAVE_MADVISE) | 105 | # if defined(HAVE_MADVISE) |
101 | madvise(fdm, *length, MADV_SEQUENTIAL); | 106 | madvise(fdm, *length, MADV_SEQUENTIAL); |
102 | # endif | 107 | # endif |
103 | 108 | ||
104 | return fdm; | 109 | return fdm; |
105 | } | 110 | } |
106 | 111 | ||
107 | 112 | ||
108 | static int unmap_file(void *fdm, unsigned long length) | 113 | static int unmap_file(void *fdm, unsigned long length) |
109 | { | 114 | { |
110 | if (munmap(fdm, length) == -1) | 115 | if (munmap(fdm, length) == -1) |
111 | return -1; | 116 | return -1; |
112 | 117 | ||
113 | return 0; | 118 | return 0; |
114 | } | 119 | } |
115 | # endif | 120 | # endif |
116 | 121 | ||
117 | 122 | ||
118 | static inline QString tr( const char *str ) { | 123 | static inline QString tr( const char *str ) { |
119 | // Apparently this is okay from a plugin as it runs in the process space of the owner of the plugin | 124 | // Apparently this is okay from a plugin as it runs in the process space of the owner of the plugin |
120 | return qApp->translate( "OpiePlayer", str, "libmad strings for mp3 file info" ); | 125 | return qApp->translate( "OpiePlayer", str, "libmad strings for mp3 file info" ); |
121 | } | 126 | } |
122 | 127 | ||
123 | 128 | ||
124 | class LibMadPluginData { | 129 | class LibMadPluginData { |
125 | public: | 130 | public: |
126 | Input input; | 131 | Input input; |
127 | Output output; | 132 | Output output; |
128 | int bad_last_frame; | 133 | int bad_last_frame; |
129 | struct mad_stream stream; | 134 | struct mad_stream stream; |
130 | struct mad_frame frame; | 135 | struct mad_frame frame; |
131 | struct mad_synth synth; | 136 | struct mad_synth synth; |
132 | bool flush; | 137 | bool flush; |
133 | }; | 138 | }; |
134 | 139 | ||
135 | 140 | ||
136 | LibMadPlugin::LibMadPlugin() { | 141 | LibMadPlugin::LibMadPlugin() { |
137 | d = new LibMadPluginData; | 142 | d = new LibMadPluginData; |
138 | d->input.fd = 0; | 143 | d->input.fd = 0; |
139 | #if defined(HAVE_MMAP) | 144 | #if defined(HAVE_MMAP) |
140 | d->input.fdm = 0; | 145 | d->input.fdm = 0; |
141 | #endif | 146 | #endif |
142 | d->input.data = 0; | 147 | d->input.data = 0; |
143 | d->flush = TRUE; | 148 | d->flush = TRUE; |
144 | info = tr( "No Song Open" ); | 149 | info = tr( "No Song Open" ); |
145 | } | 150 | } |
146 | 151 | ||
147 | 152 | ||
148 | LibMadPlugin::~LibMadPlugin() { | 153 | LibMadPlugin::~LibMadPlugin() { |
149 | close(); | 154 | close(); |
150 | delete d; | 155 | delete d; |
151 | } | 156 | } |
152 | 157 | ||
153 | 158 | ||
154 | bool LibMadPlugin::isFileSupported( const QString& path ) { | 159 | bool LibMadPlugin::isFileSupported( const QString& path ) { |
155 | debugMsg( "LibMadPlugin::isFileSupported" ); | 160 | debugMsg( "LibMadPlugin::isFileSupported" ); |
156 | 161 | ||
157 | // Mpeg file extensions | 162 | // Mpeg file extensions |
158 | // "mp2","mp3","m1v","m2v","m2s","mpg","vob","mpeg","ac3" | 163 | // "mp2","mp3","m1v","m2v","m2s","mpg","vob","mpeg","ac3" |
159 | // Other media extensions | 164 | // Other media extensions |
160 | // "wav","mid","mod","s3m","ogg","avi","mov","sid" | 165 | // "wav","mid","mod","s3m","ogg","avi","mov","sid" |
161 | 166 | ||
162 | char *ext = strrchr( path.latin1(), '.' ); | 167 | char *ext = strrchr( path.latin1(), '.' ); |
163 | 168 | ||
164 | // Test file extension | 169 | // Test file extension |
165 | if ( ext ) { | 170 | if ( ext ) { |
166 | if ( strncasecmp(ext, ".mp2", 4) == 0 ) | 171 | if ( strncasecmp(ext, ".mp2", 4) == 0 ) |
167 | return TRUE; | 172 | return TRUE; |
168 | if ( strncasecmp(ext, ".mp3", 4) == 0 ) | 173 | if ( strncasecmp(ext, ".mp3", 4) == 0 ) |
169 | return TRUE; | 174 | return TRUE; |
170 | } | 175 | } |
171 | 176 | ||
172 | // UGLY - just for fast testing | 177 | // UGLY - just for fast testing |
173 | if ( path.left(4) == "http") { | 178 | if ( path.left(4) == "http") { |
174 | return TRUE; | 179 | return TRUE; |
175 | } | 180 | } |
176 | 181 | ||
177 | return FALSE; | 182 | return FALSE; |
178 | } | 183 | } |
179 | 184 | ||
180 | 185 | ||
181 | 186 | ||
182 | int LibMadPlugin::is_address_multicast(unsigned long address) { | 187 | int LibMadPlugin::is_address_multicast(unsigned long address) { |
183 | if ((address & 255) >= 224 && (address & 255) <= 239) | 188 | if ((address & 255) >= 224 && (address & 255) <= 239) |
184 | return (1); | 189 | return (1); |
185 | return (0); | 190 | return (0); |
186 | } | 191 | } |
187 | 192 | ||
188 | 193 | ||
189 | int LibMadPlugin::udp_open(char *address, int port) { | 194 | int LibMadPlugin::udp_open(char *address, int port) { |
190 | 195 | ||
191 | int enable = 1L; | 196 | int enable = 1L; |
192 | struct sockaddr_in stAddr; | 197 | struct sockaddr_in stAddr; |
193 | struct sockaddr_in stLclAddr; | 198 | struct sockaddr_in stLclAddr; |
194 | struct ip_mreq stMreq; | 199 | struct ip_mreq stMreq; |
195 | struct hostent *host; | 200 | struct hostent *host; |
196 | int sock; | 201 | int sock; |
197 | 202 | ||
198 | stAddr.sin_family = AF_INET; | 203 | stAddr.sin_family = AF_INET; |
199 | stAddr.sin_port = htons(port); | 204 | stAddr.sin_port = htons(port); |
200 | 205 | ||
201 | if ((host = gethostbyname(address)) == NULL) | 206 | if ((host = gethostbyname(address)) == NULL) { |
202 | return (0); | 207 | return (0); |
208 | } | ||
203 | 209 | ||
204 | stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]); | 210 | stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]); |
205 | 211 | ||
206 | /* Create a UDP socket */ | 212 | /* Create a UDP socket */ |
207 | if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) | 213 | if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
208 | return (0); | 214 | return (0); |
215 | } | ||
209 | 216 | ||
210 | /* Allow multiple instance of the client to share the same address and port */ | 217 | /* Allow multiple instance of the client to share the same address and port */ |
211 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&enable, sizeof(unsigned long int)) < 0) | 218 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&enable, sizeof(unsigned long int)) < 0) { |
212 | return (0); | 219 | return (0); |
220 | } | ||
213 | 221 | ||
214 | /* If the address is multicast, register to the multicast group */ | 222 | /* If the address is multicast, register to the multicast group */ |
215 | if (is_address_multicast(stAddr.sin_addr.s_addr)) | 223 | if (is_address_multicast(stAddr.sin_addr.s_addr)) { |
216 | { | ||
217 | /* Bind the socket to port */ | 224 | /* Bind the socket to port */ |
218 | stLclAddr.sin_family = AF_INET; | 225 | stLclAddr.sin_family = AF_INET; |
219 | stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); | 226 | stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
220 | stLclAddr.sin_port = stAddr.sin_port; | 227 | stLclAddr.sin_port = stAddr.sin_port; |
221 | if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0) | 228 | if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0) { |
222 | return (0); | 229 | return (0); |
230 | } | ||
223 | 231 | ||
224 | /* Register to a multicast address */ | 232 | /* Register to a multicast address */ |
225 | stMreq.imr_multiaddr.s_addr = stAddr.sin_addr.s_addr; | 233 | stMreq.imr_multiaddr.s_addr = stAddr.sin_addr.s_addr; |
226 | stMreq.imr_interface.s_addr = INADDR_ANY; | 234 | stMreq.imr_interface.s_addr = INADDR_ANY; |
227 | if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)) < 0) | 235 | if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)) < 0) { |
228 | return (0); | 236 | return (0); |
229 | } | 237 | } |
230 | else | 238 | } else { |
231 | { | ||
232 | /* Bind the socket to port */ | 239 | /* Bind the socket to port */ |
233 | stLclAddr.sin_family = AF_INET; | 240 | stLclAddr.sin_family = AF_INET; |
234 | stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); | 241 | stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
235 | stLclAddr.sin_port = htons(0); | 242 | stLclAddr.sin_port = htons(0); |
236 | if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0) | 243 | if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0) { |
237 | return (0); | 244 | return (0); |
245 | } | ||
238 | } | 246 | } |
239 | |||
240 | return (sock); | 247 | return (sock); |
241 | } | 248 | } |
242 | 249 | ||
243 | int LibMadPlugin::tcp_open(char *address, int port) { | 250 | int LibMadPlugin::tcp_open(char *address, int port) { |
244 | struct sockaddr_in stAddr; | 251 | struct sockaddr_in stAddr; |
245 | struct hostent *host; | 252 | struct hostent *host; |
246 | int sock; | 253 | int sock; |
247 | struct linger l; | 254 | struct linger l; |
248 | 255 | ||
249 | memset(&stAddr, 0, sizeof(stAddr)); | 256 | memset(&stAddr, 0, sizeof(stAddr)); |
250 | stAddr.sin_family = AF_INET; | 257 | stAddr.sin_family = AF_INET; |
251 | stAddr.sin_port = htons(port); | 258 | stAddr.sin_port = htons(port); |
252 | 259 | ||
253 | if ((host = gethostbyname(address)) == NULL) | 260 | if ((host = gethostbyname(address)) == NULL) { |
254 | return (0); | 261 | return (0); |
262 | } | ||
255 | 263 | ||
256 | stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]); | 264 | stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]); |
257 | 265 | ||
258 | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) | 266 | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { |
259 | return (0); | 267 | return (0); |
268 | } | ||
260 | 269 | ||
261 | l.l_onoff = 1; | 270 | l.l_onoff = 1; |
262 | l.l_linger = 5; | 271 | l.l_linger = 5; |
263 | if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l)) < 0) | 272 | if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l)) < 0) { |
264 | return (0); | 273 | return (0); |
274 | } | ||
265 | 275 | ||
266 | if (connect(sock, (struct sockaddr *)&stAddr, sizeof(stAddr)) < 0) | 276 | if (connect(sock, (struct sockaddr *)&stAddr, sizeof(stAddr)) < 0) { |
267 | return (0); | 277 | return (0); |
278 | } | ||
268 | 279 | ||
269 | return (sock); | 280 | return (sock); |
270 | } | 281 | } |
271 | 282 | ||
272 | 283 | ||
273 | /** | 284 | /** |
274 | * Read a http line header. | 285 | * Read a http line header. |
275 | * This function read character by character. | 286 | * This function read character by character. |
276 | * @param tcp_sock the socket use to read the stream | 287 | * @param tcp_sock the socket use to read the stream |
277 | * @param buf a buffer to receive the data | 288 | * @param buf a buffer to receive the data |
278 | * @param size size of the buffer | 289 | * @param size size of the buffer |
279 | * @return the size of the stream read or -1 if an error occured | 290 | * @return the size of the stream read or -1 if an error occured |
280 | */ | 291 | */ |
281 | int LibMadPlugin::http_read_line(int tcp_sock, char *buf, int size) { | 292 | int LibMadPlugin::http_read_line(int tcp_sock, char *buf, int size) { |
282 | int offset = 0; | 293 | int offset = 0; |
283 | 294 | ||
284 | do | 295 | do { |
285 | { | ||
286 | if (std::read(tcp_sock, buf + offset, 1) < 0) | 296 | if (std::read(tcp_sock, buf + offset, 1) < 0) |
287 | return -1; | 297 | return -1; |
288 | if (buf[offset] != '\r') /* Strip \r from answer */ | 298 | if (buf[offset] != '\r') /* Strip \r from answer */ |
289 | offset++; | 299 | offset++; |
290 | } | 300 | } while (offset < size - 1 && buf[offset - 1] != '\n'); |
291 | while (offset < size - 1 && buf[offset - 1] != '\n'); | ||
292 | 301 | ||
293 | buf[offset] = 0; | 302 | buf[offset] = 0; |
294 | return offset; | 303 | return offset; |
295 | } | 304 | } |
296 | 305 | ||
297 | int LibMadPlugin::http_open(const QString& path ) { | 306 | int LibMadPlugin::http_open(const QString& path ) { |
298 | char *host; | 307 | char *host; |
299 | int port; | 308 | int port; |
300 | char *request; | 309 | char *request; |
301 | int tcp_sock; | 310 | int tcp_sock; |
302 | char http_request[PATH_MAX]; | 311 | char http_request[PATH_MAX]; |
303 | char filename[PATH_MAX]; | 312 | char filename[PATH_MAX]; |
304 | char c; | 313 | char c; |
305 | char *arg =strdup(path.latin1()); | 314 | char *arg =strdup(path.latin1()); |
306 | 315 | ||
307 | /* Check for URL syntax */ | 316 | /* Check for URL syntax */ |
308 | if (strncmp(arg, "http://", strlen("http://"))) | 317 | if (strncmp(arg, "http://", strlen("http://"))) { |
309 | return (0); | 318 | return (0); |
319 | } | ||
310 | 320 | ||
311 | /* Parse URL */ | 321 | /* Parse URL */ |
312 | port = 80; | 322 | port = 80; |
313 | host = arg + strlen("http://"); | 323 | host = arg + strlen("http://"); |
314 | if ((request = strchr(host, '/')) == NULL) | 324 | if ((request = strchr(host, '/')) == NULL) { |
315 | return (0); | 325 | return (0); |
326 | } | ||
327 | |||
316 | *request++ = 0; | 328 | *request++ = 0; |
317 | 329 | ||
318 | if (strchr(host, ':') != NULL) /* port is specified */ | 330 | if (strchr(host, ':') != NULL) { /* port is specified */ |
319 | { | ||
320 | port = atoi(strchr(host, ':') + 1); | 331 | port = atoi(strchr(host, ':') + 1); |
321 | *strchr(host, ':') = 0; | 332 | *strchr(host, ':') = 0; |
322 | } | 333 | } |
323 | 334 | ||
324 | /* Open a TCP socket */ | 335 | /* Open a TCP socket */ |
325 | if (!(tcp_sock = tcp_open(host, port))) | 336 | if (!(tcp_sock = tcp_open(host, port))) { |
326 | { | ||
327 | perror("http_open"); | 337 | perror("http_open"); |
328 | return (0); | 338 | return (0); |
329 | } | 339 | } |
330 | 340 | ||
331 | snprintf(filename, sizeof(filename) - strlen(host) - 75, "%s", request); | 341 | snprintf(filename, sizeof(filename) - strlen(host) - 75, "%s", request); |
332 | 342 | ||
333 | /* Send HTTP GET request */ | 343 | /* Send HTTP GET request */ |
334 | /* Please don't use a Agent know by shoutcast (Lynx, Mozilla) seems to be reconized and print | 344 | /* Please don't use a Agent know by shoutcast (Lynx, Mozilla) seems to be reconized and print |
335 | * a html page and not the stream */ | 345 | * a html page and not the stream */ |
336 | snprintf(http_request, sizeof(http_request), "GET /%s HTTP/1.0\r\n" | 346 | snprintf(http_request, sizeof(http_request), "GET /%s HTTP/1.0\r\n" |
337 | /* "User-Agent: Mozilla/2.0 (Win95; I)\r\n" */ | 347 | /* "User-Agent: Mozilla/2.0 (Win95; I)\r\n" */ |
338 | "Pragma: no-cache\r\n" "Host: %s\r\n" "Accept: */*\r\n" "\r\n", filename, host); | 348 | "Pragma: no-cache\r\n" "Host: %s\r\n" "Accept: */*\r\n" "\r\n", filename, host); |
339 | 349 | ||
340 | send(tcp_sock, http_request, strlen(http_request), 0); | 350 | send(tcp_sock, http_request, strlen(http_request), 0); |
341 | 351 | ||
342 | /* Parse server reply */ | 352 | /* Parse server reply */ |
343 | #if 0 | 353 | #if 0 |
344 | do | 354 | do |
345 | read(tcp_sock, &c, sizeof(char)); | 355 | read(tcp_sock, &c, sizeof(char)); |
346 | while (c != ' '); | 356 | while (c != ' '); |
347 | read(tcp_sock, http_request, 4 * sizeof(char)); | 357 | read(tcp_sock, http_request, 4 * sizeof(char)); |
348 | http_request[4] = 0; | 358 | http_request[4] = 0; |
349 | if (strcmp(http_request, "200 ")) | 359 | if (strcmp(http_request, "200 ")) { |
350 | { | ||
351 | fprintf(stderr, "http_open: "); | 360 | fprintf(stderr, "http_open: "); |
352 | do | 361 | do { |
353 | { | ||
354 | read(tcp_sock, &c, sizeof(char)); | 362 | read(tcp_sock, &c, sizeof(char)); |
355 | fprintf(stderr, "%c", c); | 363 | fprintf(stderr, "%c", c); |
356 | } | 364 | } while (c != '\r'); |
357 | while (c != '\r'); | ||
358 | fprintf(stderr, "\n"); | 365 | fprintf(stderr, "\n"); |
359 | return (0); | 366 | return (0); |
360 | } | 367 | } |
361 | #endif | 368 | #endif |
362 | 369 | ||
363 | do | 370 | QString name; |
364 | { | 371 | QString genre; |
372 | QString bitrate; | ||
373 | QString url; | ||
374 | QString message = tr("Info: "); | ||
375 | |||
376 | do { | ||
377 | |||
365 | int len; | 378 | int len; |
366 | 379 | ||
367 | len = http_read_line(tcp_sock, http_request, sizeof(http_request)); | 380 | len = http_read_line(tcp_sock, http_request, sizeof(http_request)); |
368 | 381 | ||
369 | if (len == -1) | 382 | if (len == -1) { |
370 | { | ||
371 | fprintf(stderr, "http_open: %s\n", strerror(errno)); | 383 | fprintf(stderr, "http_open: %s\n", strerror(errno)); |
372 | return 0; | 384 | return 0; |
373 | } | 385 | } |
374 | 386 | ||
375 | if (strncmp(http_request, "Location:", 9) == 0) | 387 | if (strncmp(http_request, "Location:", 9) == 0) { |
376 | { | ||
377 | /* redirect */ | 388 | /* redirect */ |
378 | std::close(tcp_sock); | 389 | std::close(tcp_sock); |
379 | 390 | ||
380 | http_request[strlen(http_request) - 1] = '\0'; | 391 | http_request[strlen(http_request) - 1] = '\0'; |
381 | 392 | ||
382 | return http_open(&http_request[10]); | 393 | return http_open(&http_request[10]); |
383 | } | 394 | } |
384 | 395 | ||
385 | if (strncmp(http_request, "ICY ", 4) == 0) | 396 | if (strncmp(http_request, "ICY ", 4) == 0) { |
386 | { | ||
387 | /* This is icecast streaming */ | 397 | /* This is icecast streaming */ |
388 | if (strncmp(http_request + 4, "200 ", 4)) | 398 | if (strncmp(http_request + 4, "200 ", 4)) { |
389 | { | ||
390 | fprintf(stderr, "http_open: %s\n", http_request); | 399 | fprintf(stderr, "http_open: %s\n", http_request); |
391 | return 0; | 400 | return 0; |
392 | } | 401 | } |
393 | } | 402 | } else if (strncmp(http_request, "icy-", 4) == 0) { |
394 | else if (strncmp(http_request, "icy-", 4) == 0) | ||
395 | { | ||
396 | /* we can have: icy-noticeX, icy-name, icy-genre, icy-url, icy-pub, icy-metaint, icy-br */ | 403 | /* we can have: icy-noticeX, icy-name, icy-genre, icy-url, icy-pub, icy-metaint, icy-br */ |
397 | /* Don't print these - mpg123 doesn't */ | 404 | if ( QString( http_request ).left( 8 ) == "icy-name" ) { |
398 | /* fprintf(stderr,"%s\n",http_request); */ | 405 | name = tr("Name: ") + QString(http_request).mid(9, (QString(http_request).length())- 9 ); |
406 | } else if ( QString( http_request ).left( 9 ) == "icy-genre" ) { | ||
407 | genre = tr("Genre: ") + QString(http_request).mid(10, (QString(http_request).length())-10 ); | ||
408 | } else if ( QString( http_request ).left( 6 ) == "icy-br" ) { | ||
409 | bitrate = tr("Bitrate: ") + QString(http_request).mid(7, (QString(http_request).length())- 7 ); | ||
410 | } else if ( QString( http_request ).left( 7 ) == "icy-url" ) { | ||
411 | url = tr("URL: ") + QString(http_request).mid(8, (QString(http_request).length())- 8 ); | ||
412 | } else if ( QString( http_request ).left( 10 ) == "icy-notice" ) { | ||
413 | message += QString(http_request).mid(11, QString(http_request).length()-11 ) ; | ||
414 | } | ||
399 | } | 415 | } |
400 | } | 416 | } while (strcmp(http_request, "\n") != 0); |
401 | while (strcmp(http_request, "\n") != 0); | 417 | |
418 | info = QString(name + genre + url + bitrate + message).replace( QRegExp("\n"), " : " ); | ||
419 | |||
420 | qDebug("Stream info: " + info); | ||
402 | 421 | ||
403 | return (tcp_sock); | 422 | return (tcp_sock); |
404 | } | 423 | } |
405 | 424 | ||
406 | 425 | ||
426 | |||
407 | bool LibMadPlugin::open( const QString& path ) { | 427 | bool LibMadPlugin::open( const QString& path ) { |
408 | debugMsg( "LibMadPlugin::open" ); | 428 | debugMsg( "LibMadPlugin::open" ); |
409 | Config cfg("MediaPlayer"); | 429 | Config cfg("MediaPlayer"); |
410 | cfg.setGroup("Options"); | 430 | cfg.setGroup("Options"); |
411 | bufferSize = cfg.readNumEntry("MPeg_BufferSize",MPEG_BUFFER_SIZE); | 431 | bufferSize = cfg.readNumEntry("MPeg_BufferSize",MPEG_BUFFER_SIZE); |
412 | qDebug("buffer size is %d", bufferSize); | 432 | qDebug("buffer size is %d", bufferSize); |
413 | d->bad_last_frame = 0; | 433 | d->bad_last_frame = 0; |
414 | d->flush = TRUE; | 434 | d->flush = TRUE; |
415 | info = QString( "" ); | 435 | info = QString( "" ); |
416 | 436 | ||
417 | //qDebug( "Opening %s", path.latin1() ); | 437 | //qDebug( "Opening %s", path.latin1() ); |
418 | 438 | ||
419 | 439 | ||
420 | if (path.left( 4 ) == "http" ) { | 440 | if (path.left( 4 ) == "http" ) { |
421 | d->input.fd = http_open(path); | 441 | // in case of any error we get 0 here |
422 | 442 | if ( !(http_open(path)==0) ) { | |
443 | d->input.fd = http_open(path); | ||
444 | } | ||
423 | } else { | 445 | } else { |
424 | d->input.path = path.latin1(); | 446 | d->input.path = path.latin1(); |
425 | d->input.fd = ::open( d->input.path, O_RDONLY ); | 447 | d->input.fd = ::open( d->input.path, O_RDONLY ); |
448 | // thats a better place, since it should only seek for ID3 tags on mp3 files, not streams | ||
449 | printID3Tags(); | ||
426 | } | 450 | } |
427 | if (d->input.fd == -1) { | 451 | if (d->input.fd == -1) { |
428 | qDebug("error opening %s", d->input.path ); | 452 | // qDebug("error opening %s", d->input.path ); |
429 | return FALSE; | 453 | return FALSE; |
430 | } | 454 | } |
431 | 455 | ||
432 | printID3Tags(); | 456 | // printID3Tags(); |
433 | 457 | ||
434 | #if defined(HAVE_MMAP) | 458 | #if defined(HAVE_MMAP) |
435 | struct stat stat; | 459 | struct stat stat; |
436 | if (fstat(d->input.fd, &stat) == -1) { | 460 | if (fstat(d->input.fd, &stat) == -1) { |
437 | qDebug("error calling fstat"); return FALSE; | 461 | //qDebug("error calling fstat"); return FALSE; |
438 | } | 462 | } |
439 | if (S_ISREG(stat.st_mode) && stat.st_size > 0) { | 463 | if (S_ISREG(stat.st_mode) && stat.st_size > 0) { |
440 | d->input.length = stat.st_size; | 464 | d->input.length = stat.st_size; |
441 | d->input.fdm = map_file(d->input.fd, &d->input.length); | 465 | d->input.fdm = map_file(d->input.fd, &d->input.length); |
442 | if (d->input.fdm == 0) { | 466 | if (d->input.fdm == 0) { |
443 | qDebug("error mmapping file"); return FALSE; | 467 | qDebug("error mmapping file"); return FALSE; |
444 | } | 468 | } |
445 | d->input.data = (unsigned char *)d->input.fdm; | 469 | d->input.data = (unsigned char *)d->input.fdm; |
446 | } | 470 | } |
447 | #endif | 471 | #endif |
448 | 472 | ||
449 | if (d->input.data == 0) { | 473 | if (d->input.data == 0) { |
450 | d->input.data = (unsigned char *)malloc( bufferSize /*MPEG_BUFFER_SIZE*/); | 474 | d->input.data = (unsigned char *)malloc( bufferSize /*MPEG_BUFFER_SIZE*/); |
451 | if (d->input.data == 0) { | 475 | if (d->input.data == 0) { |
452 | qDebug("error allocating input buffer"); | 476 | qDebug("error allocating input buffer"); |
453 | return FALSE; | 477 | return FALSE; |
454 | } | 478 | } |
455 | d->input.length = 0; | 479 | d->input.length = 0; |
456 | } | 480 | } |
457 | 481 | ||
458 | d->input.eof = 0; | 482 | d->input.eof = 0; |
459 | 483 | ||
460 | mad_stream_init(&d->stream); | 484 | mad_stream_init(&d->stream); |
461 | mad_frame_init(&d->frame); | 485 | mad_frame_init(&d->frame); |
462 | mad_synth_init(&d->synth); | 486 | mad_synth_init(&d->synth); |
463 | 487 | ||
464 | return TRUE; | 488 | return TRUE; |
465 | } | 489 | } |
466 | 490 | ||
467 | 491 | ||
468 | bool LibMadPlugin::close() { | 492 | bool LibMadPlugin::close() { |
469 | debugMsg( "LibMadPlugin::close" ); | 493 | debugMsg( "LibMadPlugin::close" ); |
470 | 494 | ||
471 | int result = TRUE; | 495 | int result = TRUE; |
472 | 496 | ||
473 | mad_synth_finish(&d->synth); | 497 | mad_synth_finish(&d->synth); |
474 | mad_frame_finish(&d->frame); | 498 | mad_frame_finish(&d->frame); |
475 | mad_stream_finish(&d->stream); | 499 | mad_stream_finish(&d->stream); |
476 | 500 | ||
477 | #if defined(HAVE_MMAP) | 501 | #if defined(HAVE_MMAP) |
478 | if (d->input.fdm) { | 502 | if (d->input.fdm) { |
479 | if (unmap_file(d->input.fdm, d->input.length) == -1) { | 503 | if (unmap_file(d->input.fdm, d->input.length) == -1) { |
480 | qDebug("error munmapping file"); | 504 | qDebug("error munmapping file"); |
481 | result = FALSE; | 505 | result = FALSE; |
482 | } | 506 | } |
483 | d->input.fdm = 0; | 507 | d->input.fdm = 0; |
484 | d->input.data = 0; | 508 | d->input.data = 0; |
485 | } | 509 | } |
486 | #endif | 510 | #endif |
487 | 511 | ||
488 | if (d->input.data) { | 512 | if (d->input.data) { |
489 | free(d->input.data); | 513 | free(d->input.data); |
490 | d->input.data = 0; | 514 | d->input.data = 0; |
491 | } | 515 | } |
492 | 516 | ||
493 | if (::close(d->input.fd) == -1) { | 517 | if (::close(d->input.fd) == -1) { |
494 | qDebug("error closing file %s", d->input.path); | 518 | qDebug("error closing file %s", d->input.path); |
495 | result = FALSE; | 519 | result = FALSE; |
496 | } | 520 | } |
497 | 521 | ||
498 | d->input.fd = 0; | 522 | d->input.fd = 0; |
499 | 523 | ||
500 | return result; | 524 | return result; |
501 | } | 525 | } |
502 | 526 | ||
503 | 527 | ||
504 | bool LibMadPlugin::isOpen() { | 528 | bool LibMadPlugin::isOpen() { |
505 | debugMsg( "LibMadPlugin::isOpen" ); | 529 | debugMsg( "LibMadPlugin::isOpen" ); |
506 | return ( d->input.fd != 0 ); | 530 | return ( d->input.fd != 0 ); |
507 | } | 531 | } |
508 | 532 | ||
509 | 533 | ||
510 | int LibMadPlugin::audioStreams() { | 534 | int LibMadPlugin::audioStreams() { |
511 | debugMsg( "LibMadPlugin::audioStreams" ); | 535 | debugMsg( "LibMadPlugin::audioStreams" ); |
512 | return 1; | 536 | return 1; |
513 | } | 537 | } |
514 | 538 | ||
515 | 539 | ||
516 | int LibMadPlugin::audioChannels( int ) { | 540 | int LibMadPlugin::audioChannels( int ) { |
517 | debugMsg( "LibMadPlugin::audioChannels" ); | 541 | debugMsg( "LibMadPlugin::audioChannels" ); |
518 | /* | 542 | /* |
519 | long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 ); | 543 | long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 ); |
520 | qDebug( "LibMadPlugin::audioChannels: %i", d->frame.header.mode > 0 ? 2 : 1 ); | 544 | qDebug( "LibMadPlugin::audioChannels: %i", d->frame.header.mode > 0 ? 2 : 1 ); |
521 | return d->frame.header.mode > 0 ? 2 : 1; | 545 | return d->frame.header.mode > 0 ? 2 : 1; |
522 | */ | 546 | */ |
523 | return 2; | 547 | return 2; |
524 | } | 548 | } |
525 | 549 | ||
526 | 550 | ||
527 | int LibMadPlugin::audioFrequency( int ) { | 551 | int LibMadPlugin::audioFrequency( int ) { |
528 | debugMsg( "LibMadPlugin::audioFrequency" ); | 552 | debugMsg( "LibMadPlugin::audioFrequency" ); |
529 | long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 ); | 553 | long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 ); |
530 | qDebug( "LibMadPlugin::audioFrequency: %i", d->frame.header.samplerate ); | 554 | qDebug( "LibMadPlugin::audioFrequency: %i", d->frame.header.samplerate ); |
531 | return d->frame.header.samplerate; | 555 | return d->frame.header.samplerate; |
532 | } | 556 | } |
533 | 557 | ||
534 | 558 | ||
535 | int LibMadPlugin::audioSamples( int ) { | 559 | int LibMadPlugin::audioSamples( int ) { |
536 | debugMsg( "LibMadPlugin::audioSamples" ); | 560 | debugMsg( "LibMadPlugin::audioSamples" ); |
537 | 561 | ||
538 | // long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 ); | 562 | // long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 ); |
539 | // mad_header_decode( (struct mad_header *)&d->frame.header, &d->stream ); | 563 | // mad_header_decode( (struct mad_header *)&d->frame.header, &d->stream ); |
540 | // qDebug( "LibMadPlugin::audioSamples: %i*%i", d->frame.header.duration.seconds, d->frame.header.samplerate ); | 564 | // qDebug( "LibMadPlugin::audioSamples: %i*%i", d->frame.header.duration.seconds, d->frame.header.samplerate ); |
541 | // return d->frame.header.duration.seconds * d->frame.header.samplerate; | 565 | // return d->frame.header.duration.seconds * d->frame.header.samplerate; |
542 | 566 | ||
543 | return 10000000; | 567 | return 10000000; |
544 | } | 568 | } |
545 | 569 | ||
546 | 570 | ||
547 | bool LibMadPlugin::audioSetSample( long, int ) { | 571 | bool LibMadPlugin::audioSetSample( long, int ) { |
548 | debugMsg( "LibMadPlugin::audioSetSample" ); | 572 | debugMsg( "LibMadPlugin::audioSetSample" ); |
549 | return FALSE; | 573 | return FALSE; |
550 | } | 574 | } |
551 | 575 | ||
552 | 576 | ||
553 | long LibMadPlugin::audioGetSample( int ) { | 577 | long LibMadPlugin::audioGetSample( int ) { |
554 | debugMsg( "LibMadPlugin::audioGetSample" ); | 578 | debugMsg( "LibMadPlugin::audioGetSample" ); |
555 | return 0; | 579 | return 0; |
556 | } | 580 | } |
557 | 581 | ||
558 | /* | 582 | /* |
559 | bool LibMadPlugin::audioReadSamples( short *, int, long, int ) { | 583 | bool LibMadPlugin::audioReadSamples( short *, int, long, int ) { |
560 | debugMsg( "LibMadPlugin::audioReadSamples" ); | 584 | debugMsg( "LibMadPlugin::audioReadSamples" ); |
561 | return FALSE; | 585 | return FALSE; |
562 | } | 586 | } |
563 | 587 | ||
564 | 588 | ||
565 | bool LibMadPlugin::audioReReadSamples( short *, int, long, int ) { | 589 | bool LibMadPlugin::audioReReadSamples( short *, int, long, int ) { |
566 | debugMsg( "LibMadPlugin::audioReReadSamples" ); | 590 | debugMsg( "LibMadPlugin::audioReReadSamples" ); |
567 | return FALSE; | 591 | return FALSE; |
568 | } | 592 | } |
569 | */ | 593 | */ |
570 | 594 | ||
571 | bool LibMadPlugin::read() { | 595 | bool LibMadPlugin::read() { |
572 | debugMsg( "LibMadPlugin::read" ); | 596 | debugMsg( "LibMadPlugin::read" ); |
573 | int len; | 597 | int len; |
574 | 598 | ||
575 | if (d->input.eof) | 599 | if (d->input.eof) |
576 | return FALSE; | 600 | return FALSE; |
577 | 601 | ||
578 | #if defined(HAVE_MMAP) | 602 | #if defined(HAVE_MMAP) |
579 | if (d->input.fdm) { | 603 | if (d->input.fdm) { |
580 | unsigned long skip = 0; | 604 | unsigned long skip = 0; |
581 | 605 | ||
582 | if (d->stream.next_frame) { | 606 | if (d->stream.next_frame) { |
583 | struct stat stat; | 607 | struct stat stat; |
584 | 608 | ||
585 | if (fstat(d->input.fd, &stat) == -1) | 609 | if (fstat(d->input.fd, &stat) == -1) |
586 | return FALSE; | 610 | return FALSE; |
587 | 611 | ||
588 | if (stat.st_size + MAD_BUFFER_GUARD <= (signed)d->input.length) | 612 | if (stat.st_size + MAD_BUFFER_GUARD <= (signed)d->input.length) |
589 | return FALSE; | 613 | return FALSE; |
590 | 614 | ||
591 | // file size changed; update memory map | 615 | // file size changed; update memory map |
592 | skip = d->stream.next_frame - d->input.data; | 616 | skip = d->stream.next_frame - d->input.data; |
593 | 617 | ||
594 | if (unmap_file(d->input.fdm, d->input.length) == -1) { | 618 | if (unmap_file(d->input.fdm, d->input.length) == -1) { |
595 | d->input.fdm = 0; | 619 | d->input.fdm = 0; |
596 | d->input.data = 0; | 620 | d->input.data = 0; |
597 | return FALSE; | 621 | return FALSE; |
598 | } | 622 | } |
599 | 623 | ||
600 | d->input.length = stat.st_size; | 624 | d->input.length = stat.st_size; |
601 | 625 | ||
602 | d->input.fdm = map_file(d->input.fd, &d->input.length); | 626 | d->input.fdm = map_file(d->input.fd, &d->input.length); |
603 | if (d->input.fdm == 0) { | 627 | if (d->input.fdm == 0) { |
604 | d->input.data = 0; | 628 | d->input.data = 0; |
605 | return FALSE; | 629 | return FALSE; |
606 | } | 630 | } |
607 | 631 | ||
608 | d->input.data = (unsigned char *)d->input.fdm; | 632 | d->input.data = (unsigned char *)d->input.fdm; |
609 | } | 633 | } |
610 | 634 | ||
611 | mad_stream_buffer(&d->stream, d->input.data + skip, d->input.length - skip); | 635 | mad_stream_buffer(&d->stream, d->input.data + skip, d->input.length - skip); |
612 | 636 | ||
613 | } else | 637 | } else |
614 | #endif | 638 | #endif |
615 | { | 639 | { |
616 | if (d->stream.next_frame) { | 640 | if (d->stream.next_frame) { |
617 | memmove(d->input.data, d->stream.next_frame, | 641 | memmove(d->input.data, d->stream.next_frame, |
618 | d->input.length = &d->input.data[d->input.length] - d->stream.next_frame); | 642 | d->input.length = &d->input.data[d->input.length] - d->stream.next_frame); |
619 | } | 643 | } |
620 | 644 | ||
621 | do { | 645 | do { |
622 | len = ::read(d->input.fd, d->input.data + d->input.length, bufferSize /* MPEG_BUFFER_SIZE*/ - d->input.length); | 646 | len = ::read(d->input.fd, d->input.data + d->input.length, bufferSize /* MPEG_BUFFER_SIZE*/ - d->input.length); |
623 | } | 647 | } |
624 | while (len == -1 && errno == EINTR); | 648 | while (len == -1 && errno == EINTR); |
625 | 649 | ||
626 | if (len == -1) { | 650 | if (len == -1) { |
627 | qDebug("error reading audio"); | 651 | qDebug("error reading audio"); |
628 | return FALSE; | 652 | return FALSE; |
629 | } | 653 | } |
630 | else if (len == 0) { | 654 | else if (len == 0) { |
631 | d->input.eof = 1; | 655 | d->input.eof = 1; |
632 | 656 | ||
633 | assert(bufferSize /*MPEG_BUFFER_SIZE*/ - d->input.length >= MAD_BUFFER_GUARD); | 657 | assert(bufferSize /*MPEG_BUFFER_SIZE*/ - d->input.length >= MAD_BUFFER_GUARD); |
634 | 658 | ||
635 | while (len < MAD_BUFFER_GUARD) | 659 | while (len < MAD_BUFFER_GUARD) |
636 | d->input.data[d->input.length + len++] = 0; | 660 | d->input.data[d->input.length + len++] = 0; |
637 | } | 661 | } |
638 | 662 | ||
639 | mad_stream_buffer(&d->stream, d->input.data, d->input.length += len); | 663 | mad_stream_buffer(&d->stream, d->input.data, d->input.length += len); |
640 | } | 664 | } |
641 | 665 | ||
642 | return TRUE; | 666 | return TRUE; |
643 | } | 667 | } |
644 | 668 | ||
645 | 669 | ||
646 | static mad_fixed_t left_err, right_err; | 670 | static mad_fixed_t left_err, right_err; |
647 | static const int bits = 16; | 671 | static const int bits = 16; |
648 | static const int shift = MAD_F_FRACBITS + 1 - bits; | 672 | static const int shift = MAD_F_FRACBITS + 1 - bits; |
649 | 673 | ||
650 | 674 | ||
651 | inline long audio_linear_dither( mad_fixed_t sample, mad_fixed_t& error ) | 675 | inline long audio_linear_dither( mad_fixed_t sample, mad_fixed_t& error ) { |
652 | { | ||
653 | sample += error; | 676 | sample += error; |
654 | mad_fixed_t quantized = (sample >= MAD_F_ONE) ? MAD_F_ONE - 1 : ( (sample < -MAD_F_ONE) ? -MAD_F_ONE : sample ); | 677 | mad_fixed_t quantized = (sample >= MAD_F_ONE) ? MAD_F_ONE - 1 : ( (sample < -MAD_F_ONE) ? -MAD_F_ONE : sample ); |
655 | quantized &= ~((1L << shift) - 1); | 678 | quantized &= ~((1L << shift) - 1); |
656 | error = sample - quantized; | 679 | error = sample - quantized; |
657 | return quantized >> shift; | 680 | return quantized >> shift; |
658 | } | 681 | } |
659 | 682 | ||
660 | 683 | ||
661 | inline void audio_pcm( short *data, unsigned int nsamples, mad_fixed_t *left, mad_fixed_t *right ) | 684 | inline void audio_pcm( short *data, unsigned int nsamples, mad_fixed_t *left, mad_fixed_t *right ) { |
662 | { | ||
663 | if ( right ) { | 685 | if ( right ) { |
664 | while (nsamples--) { | 686 | while (nsamples--) { |
665 | data[0] = audio_linear_dither( *left++, left_err ); | 687 | data[0] = audio_linear_dither( *left++, left_err ); |
666 | data[1] = audio_linear_dither( *right++, right_err ); | 688 | data[1] = audio_linear_dither( *right++, right_err ); |
667 | data += 2; | 689 | data += 2; |
668 | } | 690 | } |
669 | } else { | 691 | } else { |
670 | while (nsamples--) { | 692 | while (nsamples--) { |
671 | data[0] = data[1] = audio_linear_dither( *left++, left_err ); | 693 | data[0] = data[1] = audio_linear_dither( *left++, left_err ); |
672 | data += 2; | 694 | data += 2; |
673 | } | 695 | } |
674 | } | 696 | } |
675 | } | 697 | } |
676 | 698 | ||
677 | 699 | ||
678 | bool LibMadPlugin::decode( short *output, long samples, long& samplesMade ) { | 700 | bool LibMadPlugin::decode( short *output, long samples, long& samplesMade ) { |
679 | debugMsg( "LibMadPlugin::decode" ); | 701 | debugMsg( "LibMadPlugin::decode" ); |
680 | 702 | ||
681 | static int buffered = 0; | 703 | static int buffered = 0; |
682 | static mad_fixed_t buffer[2][65536 * 2]; | 704 | static mad_fixed_t buffer[2][65536 * 2]; |
683 | int offset = buffered; | 705 | int offset = buffered; |
684 | samplesMade = 0; | 706 | samplesMade = 0; |
685 | 707 | ||
686 | static int maxBuffered = 8000; // 65536; | 708 | static int maxBuffered = 8000; // 65536; |
687 | 709 | ||
688 | if ( samples > maxBuffered ) | 710 | if ( samples > maxBuffered ) { |
689 | samples = maxBuffered; | 711 | samples = maxBuffered; |
712 | } | ||
690 | 713 | ||
691 | if ( d->flush ) { | 714 | if ( d->flush ) { |
692 | buffered = 0; | 715 | buffered = 0; |
693 | offset = 0; | 716 | offset = 0; |
694 | d->flush = FALSE; | 717 | d->flush = FALSE; |
695 | } | 718 | } |
696 | 719 | ||
697 | while ( buffered < maxBuffered ) { | 720 | while ( buffered < maxBuffered ) { |
698 | 721 | ||
699 | while (mad_frame_decode(&d->frame, &d->stream) == -1) { | 722 | while (mad_frame_decode(&d->frame, &d->stream) == -1) { |
700 | if (!MAD_RECOVERABLE(d->stream.error)) { | 723 | if (!MAD_RECOVERABLE(d->stream.error)) { |
701 | debugMsg( "feed me" ); | 724 | debugMsg( "feed me" ); |
702 | return FALSE; // Feed me | 725 | return FALSE; // Feed me |
703 | } | 726 | } |
704 | if ( d->stream.error == MAD_ERROR_BADCRC ) { | 727 | if ( d->stream.error == MAD_ERROR_BADCRC ) { |
705 | mad_frame_mute(&d->frame); | 728 | mad_frame_mute(&d->frame); |
706 | qDebug( "error decoding, bad crc" ); | 729 | qDebug( "error decoding, bad crc" ); |
707 | } | 730 | } |
708 | } | 731 | } |
709 | 732 | ||
710 | mad_synth_frame(&d->synth, &d->frame); | 733 | mad_synth_frame(&d->synth, &d->frame); |
711 | int decodedSamples = d->synth.pcm.length; | 734 | int decodedSamples = d->synth.pcm.length; |
712 | memcpy( &(buffer[0][offset]), d->synth.pcm.samples[0], decodedSamples * sizeof(mad_fixed_t) ); | 735 | memcpy( &(buffer[0][offset]), d->synth.pcm.samples[0], decodedSamples * sizeof(mad_fixed_t) ); |
713 | if ( d->synth.pcm.channels == 2 ) | 736 | if ( d->synth.pcm.channels == 2 ) |
714 | memcpy( &(buffer[1][offset]), d->synth.pcm.samples[1], decodedSamples * sizeof(mad_fixed_t) ); | 737 | memcpy( &(buffer[1][offset]), d->synth.pcm.samples[1], decodedSamples * sizeof(mad_fixed_t) ); |
715 | offset += decodedSamples; | 738 | offset += decodedSamples; |
716 | buffered += decodedSamples; | 739 | buffered += decodedSamples; |
717 | } | 740 | } |
741 | |||
718 | //qApp->processEvents(); | 742 | //qApp->processEvents(); |
719 | audio_pcm( output, samples, buffer[0], (d->synth.pcm.channels == 2) ? buffer[1] : 0 ); | 743 | audio_pcm( output, samples, buffer[0], (d->synth.pcm.channels == 2) ? buffer[1] : 0 ); |
720 | // audio_pcm( output, samples, buffer[1], buffer[0] ); | 744 | // audio_pcm( output, samples, buffer[1], buffer[0] ); |
721 | // audio_pcm( output, samples, buffer[0], buffer[1] ); | 745 | // audio_pcm( output, samples, buffer[0], buffer[1] ); |
722 | samplesMade = samples; | 746 | samplesMade = samples; |
723 | memmove( buffer[0], &(buffer[0][samples]), (buffered - samples) * sizeof(mad_fixed_t) ); | 747 | memmove( buffer[0], &(buffer[0][samples]), (buffered - samples) * sizeof(mad_fixed_t) ); |
724 | if ( d->synth.pcm.channels == 2 ) | 748 | if ( d->synth.pcm.channels == 2 ) { |
725 | memmove( buffer[1], &(buffer[1][samples]), (buffered - samples) * sizeof(mad_fixed_t) ); | 749 | memmove( buffer[1], &(buffer[1][samples]), (buffered - samples) * sizeof(mad_fixed_t) ); |
750 | } | ||
726 | buffered -= samples; | 751 | buffered -= samples; |
727 | 752 | ||
728 | return TRUE; | 753 | return TRUE; |
729 | } | 754 | } |
730 | 755 | ||
731 | /* | 756 | /*bool LibMadPlugin::audioReadStereoSamples( short *output, long samples, long& samplesMade, int ) { |
732 | bool LibMadPlugin::audioReadMonoSamples( short *, long, long&, int ) { | ||
733 | debugMsg( "LibMadPlugin::audioReadMonoSamples" ); | ||
734 | return FALSE; | ||
735 | } | ||
736 | |||
737 | |||
738 | bool LibMadPlugin::audioReadStereoSamples( short *output, long samples, long& samplesMade, int ) { | ||
739 | */ | 757 | */ |
740 | bool LibMadPlugin::audioReadSamples( short *output, int /*channels*/, long samples, long& samplesMade, int ) { | 758 | bool LibMadPlugin::audioReadSamples( short *output, int /*channels*/, long samples, long& samplesMade, int ) { |
741 | debugMsg( "LibMadPlugin::audioReadStereoSamples" ); | 759 | debugMsg( "LibMadPlugin::audioReadStereoSamples" ); |
742 | 760 | ||
743 | static bool needInput = TRUE; | 761 | static bool needInput = TRUE; |
744 | 762 | ||
745 | if ( samples == 0 ) | 763 | if ( samples == 0 ) |
746 | return FALSE; | 764 | return FALSE; |
747 | 765 | ||
748 | do { | 766 | do { |
749 | if ( needInput ) | 767 | if ( needInput ) |
750 | if ( !read() ) { | 768 | if ( !read() ) { |
751 | // if ( d->input.eof ) | 769 | return FALSE; |
752 | // needInput = FALSE; | 770 | } |
753 | // else | ||
754 | return FALSE; | ||
755 | } | ||
756 | 771 | ||
757 | needInput = FALSE; | 772 | needInput = FALSE; |
758 | 773 | ||
759 | if ( decode( output, samples, samplesMade ) ) | 774 | if ( decode( output, samples, samplesMade ) ) |
760 | return TRUE; | 775 | return TRUE; |
761 | else | 776 | else |
762 | needInput = TRUE; | 777 | needInput = TRUE; |
763 | } | 778 | } |
764 | while ( ( samplesMade < samples ) && ( !d->input.eof ) ); | 779 | while ( ( samplesMade < samples ) && ( !d->input.eof ) ); |
765 | /* | ||
766 | static bool firstTimeThru = TRUE; | ||
767 | 780 | ||
768 | if ( firstTimeThru ) { | 781 | return FALSE; |
769 | firstTimeThru = FALSE; | ||
770 | decode( output, samples, samplesMade ); | ||
771 | return FALSE; | ||
772 | } else | ||
773 | */ | ||
774 | return FALSE; | ||
775 | } | 782 | } |
776 | 783 | ||
777 | 784 | ||
778 | double LibMadPlugin::getTime() { | 785 | double LibMadPlugin::getTime() { |
779 | debugMsg( "LibMadPlugin::getTime" ); | 786 | debugMsg( "LibMadPlugin::getTime" ); |
780 | return 0.0; | 787 | return 0.0; |
781 | } | 788 | } |
782 | 789 | ||
783 | 790 | ||
784 | void LibMadPlugin::printID3Tags() { | 791 | void LibMadPlugin::printID3Tags() { |
785 | qDebug( "LibMadPlugin::printID3Tags" ); | 792 | qDebug( "LibMadPlugin::printID3Tags" ); |
786 | 793 | ||
787 | char id3v1[128 + 1]; | 794 | char id3v1[128 + 1]; |
788 | 795 | ||
789 | if ( ::lseek( d->input.fd, -128, SEEK_END ) == -1 ) { | 796 | if ( ::lseek( d->input.fd, -128, SEEK_END ) == -1 ) { |
790 | qDebug( "error seeking to id3 tags" ); | 797 | qDebug( "error seeking to id3 tags" ); |
791 | return; | 798 | return; |
792 | } | 799 | } |
793 | 800 | ||
794 | if ( ::read( d->input.fd, id3v1, 128 ) != 128 ) { | 801 | if ( ::read( d->input.fd, id3v1, 128 ) != 128 ) { |
795 | qDebug( "error reading in id3 tags" ); | 802 | qDebug( "error reading in id3 tags" ); |
796 | return; | 803 | return; |
797 | } | 804 | } |
798 | 805 | ||
799 | if ( ::strncmp( (const char *)id3v1, "TAG", 3 ) != 0 ) { | 806 | if ( ::strncmp( (const char *)id3v1, "TAG", 3 ) != 0 ) { |
800 | debugMsg( "sorry, no id3 tags" ); | 807 | debugMsg( "sorry, no id3 tags" ); |
801 | } else { | 808 | } else { |
802 | int len[5] = { 30, 30, 30, 4, 30 }; | 809 | int len[5] = { 30, 30, 30, 4, 30 }; |
803 | QString label[5] = { tr( "Title" ), tr( "Artist" ), tr( "Album" ), tr( "Year" ), tr( "Comment" ) }; | 810 | QString label[5] = { tr( "Title" ), tr( "Artist" ), tr( "Album" ), tr( "Year" ), tr( "Comment" ) }; |
804 | char *ptr = id3v1 + 3, *ptr2 = ptr + len[0]; | 811 | char *ptr = id3v1 + 3, *ptr2 = ptr + len[0]; |
805 | qDebug( "ID3 tags in file:" ); | 812 | qDebug( "ID3 tags in file:" ); |
806 | info = ""; | 813 | info = ""; |
807 | for ( int i = 0; i < 5; ptr += len[i], i++, ptr2 += len[i] ) { | 814 | for ( int i = 0; i < 5; ptr += len[i], i++, ptr2 += len[i] ) { |
808 | char push = *ptr2; | 815 | char push = *ptr2; |
809 | *ptr2 = '\0'; | 816 | *ptr2 = '\0'; |
810 | char *ptr3 = ptr2; | 817 | char *ptr3 = ptr2; |
811 | while ( ptr3-1 >= ptr && isspace(ptr3[-1]) ) ptr3--; | 818 | while ( ptr3-1 >= ptr && isspace(ptr3[-1]) ) ptr3--; |
812 | char push2 = *ptr3; *ptr3 = '\0'; | 819 | char push2 = *ptr3; *ptr3 = '\0'; |
813 | if ( strcmp( ptr, "" ) ) | 820 | if ( strcmp( ptr, "" ) ) |
814 | info += ( i != 0 ? ", " : "" ) + label[i] + ": " + ptr; | 821 | info += ( i != 0 ? ", " : "" ) + label[i] + ": " + ptr; |
815 | //qDebug( info.latin1() ); | 822 | //qDebug( info.latin1() ); |
816 | *ptr3 = push2; | 823 | *ptr3 = push2; |
817 | *ptr2 = push; | 824 | *ptr2 = push; |
818 | } | 825 | } |
819 | if (id3v1[126] == 0 && id3v1[127] != 0) | 826 | if (id3v1[126] == 0 && id3v1[127] != 0) |
820 | info += tr( ", Track: " ) + id3v1[127]; | 827 | info += tr( ", Track: " ) + id3v1[127]; |
821 | } | 828 | } |
822 | 829 | ||
823 | if ( ::lseek(d->input.fd, 0, SEEK_SET) == -1 ) { | 830 | if ( ::lseek(d->input.fd, 0, SEEK_SET) == -1 ) { |
824 | qDebug( "error seeking back to beginning" ); | 831 | qDebug( "error seeking back to beginning" ); |
825 | return; | 832 | return; |
826 | } | 833 | } |
827 | } | 834 | } |
828 | 835 | ||