summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/multimedia/opieplayer/libmad/libmadplugin.cpp10
1 files changed, 2 insertions, 8 deletions
diff --git a/core/multimedia/opieplayer/libmad/libmadplugin.cpp b/core/multimedia/opieplayer/libmad/libmadplugin.cpp
index 3df23249..9a1ab2a 100644
--- a/core/multimedia/opieplayer/libmad/libmadplugin.cpp
+++ b/core/multimedia/opieplayer/libmad/libmadplugin.cpp
@@ -1,109 +1,110 @@
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> 20// largly modified by Maximilian Reiss <max.reiss@gmx.de>
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdarg.h> 23#include <stdarg.h>
24#include <stdlib.h> 24#include <stdlib.h>
25#include <sys/types.h> 25#include <sys/types.h>
26#include <sys/stat.h> 26#include <sys/stat.h>
27#include <fcntl.h> 27#include <fcntl.h>
28#include <unistd.h> 28#include <unistd.h>
29#include <string.h> 29#include <string.h>
30#include <ctype.h> 30#include <ctype.h>
31#include <errno.h> 31#include <errno.h>
32#include <time.h> 32#include <time.h>
33#include <locale.h> 33#include <locale.h>
34#include <math.h> 34#include <math.h>
35#include <assert.h> 35#include <assert.h>
36 36
37#include <qapplication.h> 37#include <qapplication.h>
38#include <qmessagebox.h> 38#include <qmessagebox.h>
39#include <qregexp.h> 39#include <qregexp.h>
40 40
41#include <qpe/config.h> 41#include <qpe/config.h>
42 42
43// for network handling 43// for network handling
44#include <netinet/in.h> 44#include <netinet/in.h>
45#include <netdb.h> 45#include <netdb.h>
46#include <linux/limits.h>
46#include <sys/socket.h> 47#include <sys/socket.h>
47#include <arpa/inet.h> 48#include <arpa/inet.h>
48#include <unistd.h> 49#include <unistd.h>
49 50
50 51
51//#define HAVE_MMAP 52//#define HAVE_MMAP
52 53
53#if defined(HAVE_MMAP) 54#if defined(HAVE_MMAP)
54# include <sys/mman.h> 55# include <sys/mman.h>
55#endif 56#endif
56#include "libmadplugin.h" 57#include "libmadplugin.h"
57 58
58 59
59extern "C" { 60extern "C" {
60#include "mad.h" 61#include "mad.h"
61} 62}
62 63
63 64
64#define MPEG_BUFFER_SIZE 65536 65#define MPEG_BUFFER_SIZE 65536
65//#define MPEG_BUFFER_SIZE 32768 //16384 // 8192 66//#define MPEG_BUFFER_SIZE 32768 //16384 // 8192
66//#define debugMsg(a) qDebug(a) 67//#define debugMsg(a) qDebug(a)
67#define debugMsg(a) 68#define debugMsg(a)
68 69
69 70
70class Input { 71class Input {
71public: 72public:
72 char const *path; 73 char const *path;
73 int fd; 74 int fd;
74#if defined(HAVE_MMAP) 75#if defined(HAVE_MMAP)
75 void *fdm; 76 void *fdm;
76#endif 77#endif
77 unsigned char *data; 78 unsigned char *data;
78 unsigned long length; 79 unsigned long length;
79 int eof; 80 int eof;
80}; 81};
81 82
82 83
83class Output { 84class Output {
84public: 85public:
85 mad_fixed_t attenuate; 86 mad_fixed_t attenuate;
86 struct filter *filters; 87 struct filter *filters;
87 unsigned int channels_in; 88 unsigned int channels_in;
88 unsigned int channels_out; 89 unsigned int channels_out;
89 unsigned int speed_in; 90 unsigned int speed_in;
90 unsigned int speed_out; 91 unsigned int speed_out;
91 const char *path; 92 const char *path;
92}; 93};
93 94
94 95
95# if defined(HAVE_MMAP) 96# if defined(HAVE_MMAP)
96static void *map_file(int fd, unsigned long *length) 97static void *map_file(int fd, unsigned long *length)
97{ 98{
98 void *fdm; 99 void *fdm;
99 100
100 *length += MAD_BUFFER_GUARD; 101 *length += MAD_BUFFER_GUARD;
101 102
102 fdm = mmap(0, *length, PROT_READ, MAP_SHARED, fd, 0); 103 fdm = mmap(0, *length, PROT_READ, MAP_SHARED, fd, 0);
103 if (fdm == MAP_FAILED) 104 if (fdm == MAP_FAILED)
104 return 0; 105 return 0;
105 106
106# if defined(HAVE_MADVISE) 107# if defined(HAVE_MADVISE)
107 madvise(fdm, *length, MADV_SEQUENTIAL); 108 madvise(fdm, *length, MADV_SEQUENTIAL);
108# endif 109# endif
109 110
@@ -373,153 +374,146 @@ int LibMadPlugin::http_open(const QString& path ) {
373 QString bitrate; 374 QString bitrate;
374 QString url; 375 QString url;
375 QString message = tr("Info: "); 376 QString message = tr("Info: ");
376 do { 377 do {
377 378
378 int len; 379 int len;
379 380
380 len = http_read_line(tcp_sock, http_request, sizeof(http_request)); 381 len = http_read_line(tcp_sock, http_request, sizeof(http_request));
381 382
382 if (len == -1) { 383 if (len == -1) {
383 qDebug( "http_open: "+ QString(strerror(errno)) +"\n"); 384 qDebug( "http_open: "+ QString(strerror(errno)) +"\n");
384 return 0; 385 return 0;
385 } 386 }
386 387
387 if (QString(http_request).left(9) == "Location:") { 388 if (QString(http_request).left(9) == "Location:") {
388 /* redirect */ 389 /* redirect */
389 ::close(tcp_sock); 390 ::close(tcp_sock);
390 http_request[strlen(http_request) - 1] = '\0'; 391 http_request[strlen(http_request) - 1] = '\0';
391 return http_open(&http_request[10]); 392 return http_open(&http_request[10]);
392 } 393 }
393 394
394 if (QString(http_request).left(4) == "ICY ") { 395 if (QString(http_request).left(4) == "ICY ") {
395 /* This is shoutcast/icecast streaming */ 396 /* This is shoutcast/icecast streaming */
396 if (strncmp(http_request + 4, "200 ", 4)) { 397 if (strncmp(http_request + 4, "200 ", 4)) {
397 qDebug("http_open: " + QString(http_request) + "\n"); 398 qDebug("http_open: " + QString(http_request) + "\n");
398 return 0; 399 return 0;
399 } 400 }
400 } else if (QString(http_request).left(4) == "icy-") { 401 } else if (QString(http_request).left(4) == "icy-") {
401 /* we can have: icy-noticeX, icy-name, icy-genre, icy-url, icy-pub, icy-metaint, icy-br */ 402 /* we can have: icy-noticeX, icy-name, icy-genre, icy-url, icy-pub, icy-metaint, icy-br */
402 if ( QString( http_request ).left( 8 ) == "icy-name" ) { 403 if ( QString( http_request ).left( 8 ) == "icy-name" ) {
403 name = tr("Name: ") + QString(http_request).mid(9, (QString(http_request).length())- 9 ); 404 name = tr("Name: ") + QString(http_request).mid(9, (QString(http_request).length())- 9 );
404 } else if ( QString( http_request ).left( 9 ) == "icy-genre" ) { 405 } else if ( QString( http_request ).left( 9 ) == "icy-genre" ) {
405 genre = tr("Genre: ") + QString(http_request).mid(10, (QString(http_request).length())-10 ); 406 genre = tr("Genre: ") + QString(http_request).mid(10, (QString(http_request).length())-10 );
406 } else if ( QString( http_request ).left( 6 ) == "icy-br" ) { 407 } else if ( QString( http_request ).left( 6 ) == "icy-br" ) {
407 bitrate = tr("Bitrate: ") + QString(http_request).mid(7, (QString(http_request).length())- 7 ); 408 bitrate = tr("Bitrate: ") + QString(http_request).mid(7, (QString(http_request).length())- 7 );
408 } else if ( QString( http_request ).left( 7 ) == "icy-url" ) { 409 } else if ( QString( http_request ).left( 7 ) == "icy-url" ) {
409 url = tr("URL: ") + QString(http_request).mid(8, (QString(http_request).length())- 8 ); 410 url = tr("URL: ") + QString(http_request).mid(8, (QString(http_request).length())- 8 );
410 } else if ( QString( http_request ).left( 10 ) == "icy-notice" ) { 411 } else if ( QString( http_request ).left( 10 ) == "icy-notice" ) {
411 message += QString(http_request).mid(11, QString(http_request).length()-11 ) ; 412 message += QString(http_request).mid(11, QString(http_request).length()-11 ) ;
412 } 413 }
413 } 414 }
414 } while (strcmp(http_request, "\n") != 0); 415 } while (strcmp(http_request, "\n") != 0);
415 416
416 info = QString(name + genre + url + bitrate + message).replace( QRegExp("\n"), " : " ); 417 info = QString(name + genre + url + bitrate + message).replace( QRegExp("\n"), " : " );
417 418
418 qDebug("Stream info: " + info); 419 qDebug("Stream info: " + info);
419 420
420 return (tcp_sock); 421 return (tcp_sock);
421} 422}
422 423
423 424
424 425
425bool LibMadPlugin::open( const QString& path ) { 426bool LibMadPlugin::open( const QString& path ) {
426 debugMsg( "LibMadPlugin::open" ); 427 debugMsg( "LibMadPlugin::open" );
427 Config cfg("OpiePlayer"); 428 Config cfg("OpiePlayer");
428 cfg.setGroup("Options"); 429 cfg.setGroup("Options");
429 bufferSize = cfg.readNumEntry("MPeg_BufferSize",MPEG_BUFFER_SIZE); 430 bufferSize = cfg.readNumEntry("MPeg_BufferSize",MPEG_BUFFER_SIZE);
430 qDebug("buffer size is %d", bufferSize); 431 qDebug("buffer size is %d", bufferSize);
431 d->bad_last_frame = 0; 432 d->bad_last_frame = 0;
432 d->flush = TRUE; 433 d->flush = TRUE;
433 info = QString( "" ); 434 info = QString( "" );
434 435
435 //qDebug( "Opening %s", path.latin1() ); 436 //qDebug( "Opening %s", path.latin1() );
436 437
437
438 if (path.left( 4 ) == "http" ) { 438 if (path.left( 4 ) == "http" ) {
439 qDebug("Test2");
440 // in case of any error we get 0 here 439 // in case of any error we get 0 here
441 if ( !(http_open(path) == 0) ) { 440 if ( !(http_open(path) == 0) ) {
442 qDebug("Test3");
443 d->input.fd = http_open(path); 441 d->input.fd = http_open(path);
444 } else { 442 } else {
445 qDebug("Test5");
446 return FALSE; 443 return FALSE;
447 } 444 }
448 } else { 445 } else {
449 qDebug("Test4");
450 d->input.path = path.latin1(); 446 d->input.path = path.latin1();
451 d->input.fd = ::open( d->input.path, O_RDONLY ); 447 d->input.fd = ::open( d->input.path, O_RDONLY );
452 // thats a better place, since it should only seek for ID3 tags on mp3 files, not streams 448 // thats a better place, since it should only seek for ID3 tags on mp3 files, not streams
453 printID3Tags(); 449 printID3Tags();
454 } 450 }
455 if (d->input.fd == -1) { 451 if (d->input.fd == -1) {
456 qDebug("error opening %s", d->input.path ); 452 qDebug("error opening %s", d->input.path );
457 return FALSE; 453 return FALSE;
458 } 454 }
459 455
460// printID3Tags();
461
462#if defined(HAVE_MMAP) 456#if defined(HAVE_MMAP)
463 struct stat stat; 457 struct stat stat;
464 if (fstat(d->input.fd, &stat) == -1) { 458 if (fstat(d->input.fd, &stat) == -1) {
465 //qDebug("error calling fstat"); return FALSE; 459 //qDebug("error calling fstat"); return FALSE;
466 } 460 }
467 if (S_ISREG(stat.st_mode) && stat.st_size > 0) { 461 if (S_ISREG(stat.st_mode) && stat.st_size > 0) {
468 d->input.length = stat.st_size; 462 d->input.length = stat.st_size;
469 d->input.fdm = map_file(d->input.fd, &d->input.length); 463 d->input.fdm = map_file(d->input.fd, &d->input.length);
470 if (d->input.fdm == 0) { 464 if (d->input.fdm == 0) {
471 qDebug("error mmapping file"); return FALSE; 465 qDebug("error mmapping file"); return FALSE;
472 } 466 }
473 d->input.data = (unsigned char *)d->input.fdm; 467 d->input.data = (unsigned char *)d->input.fdm;
474 } 468 }
475#endif 469#endif
476 470
477 if (d->input.data == 0) { 471 if (d->input.data == 0) {
478 d->input.data = (unsigned char *)malloc( bufferSize /*MPEG_BUFFER_SIZE*/); 472 d->input.data = (unsigned char *)malloc( bufferSize /*MPEG_BUFFER_SIZE*/);
479 if (d->input.data == 0) { 473 if (d->input.data == 0) {
480 qDebug("error allocating input buffer"); 474 qDebug("error allocating input buffer");
481 return FALSE; 475 return FALSE;
482 } 476 }
483 d->input.length = 0; 477 d->input.length = 0;
484 } 478 }
485 479
486 d->input.eof = 0; 480 d->input.eof = 0;
487 481
488 mad_stream_init(&d->stream); 482 mad_stream_init(&d->stream);
489 mad_frame_init(&d->frame); 483 mad_frame_init(&d->frame);
490 mad_synth_init(&d->synth); 484 mad_synth_init(&d->synth);
491 485
492 return TRUE; 486 return TRUE;
493} 487}
494 488
495 489
496bool LibMadPlugin::close() { 490bool LibMadPlugin::close() {
497 debugMsg( "LibMadPlugin::close" ); 491 debugMsg( "LibMadPlugin::close" );
498 492
499 int result = TRUE; 493 int result = TRUE;
500 494
501 mad_synth_finish(&d->synth); 495 mad_synth_finish(&d->synth);
502 mad_frame_finish(&d->frame); 496 mad_frame_finish(&d->frame);
503 mad_stream_finish(&d->stream); 497 mad_stream_finish(&d->stream);
504 498
505#if defined(HAVE_MMAP) 499#if defined(HAVE_MMAP)
506 if (d->input.fdm) { 500 if (d->input.fdm) {
507 if (unmap_file(d->input.fdm, d->input.length) == -1) { 501 if (unmap_file(d->input.fdm, d->input.length) == -1) {
508 qDebug("error munmapping file"); 502 qDebug("error munmapping file");
509 result = FALSE; 503 result = FALSE;
510 } 504 }
511 d->input.fdm = 0; 505 d->input.fdm = 0;
512 d->input.data = 0; 506 d->input.data = 0;
513 } 507 }
514#endif 508#endif
515 509
516 if (d->input.data) { 510 if (d->input.data) {
517 free(d->input.data); 511 free(d->input.data);
518 d->input.data = 0; 512 d->input.data = 0;
519 } 513 }
520 514
521 if (::close(d->input.fd) == -1) { 515 if (::close(d->input.fd) == -1) {
522 qDebug("error closing file %s", d->input.path); 516 qDebug("error closing file %s", d->input.path);
523 result = FALSE; 517 result = FALSE;
524 } 518 }
525 519