diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/lib.pro | 8 | ||||
| -rw-r--r-- | lib/soundcore.cpp | 27 | ||||
| -rw-r--r-- | lib/streamreader.cpp | 208 | ||||
| -rw-r--r-- | lib/streamreader.h | 77 |
4 files changed, 308 insertions, 12 deletions
diff --git a/lib/lib.pro b/lib/lib.pro index 723998b9f..0bf2eb9bc 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -15,14 +15,18 @@ HEADERS += recycler.h \ equ\iir.h \ decoderfactory.h \ soundcore.h \ - visualization.h + visualization.h \ + streamreader.h SOURCES += recycler.cpp \ decoder.cpp \ output.cpp \ equ\iir.c \ equ\iir_cfs.c \ equ\iir_fpu.c \ - soundcore.cpp + soundcore.cpp \ + streamreader.cpp + +QT += network TARGET = qmmp CONFIG += release \ warn_on \ diff --git a/lib/soundcore.cpp b/lib/soundcore.cpp index a0716afe8..4f295d133 100644 --- a/lib/soundcore.cpp +++ b/lib/soundcore.cpp @@ -24,6 +24,7 @@ #include "decoderfactory.h" #include "constants.h" +#include "streamreader.h" #include "soundcore.h" @@ -71,7 +72,13 @@ bool SoundCore::play(const QString &source) m_error = DecoderError; return FALSE; } - m_input = new QFile(source); + if(source.left(4) == "http") + { + m_input = new StreamReader(source, this); + //m_input->open(QIODevice::ReadOnly); + } + else + m_input = new QFile(source); m_error = OutputError; if (!m_output) @@ -96,10 +103,6 @@ bool SoundCore::play(const QString &source) m_output->addVisual(m_vis); } - //if (m_decoder && ! m_decoder->factory()->supports(source)) - // m_decoder = 0; - - if (! m_decoder) { qDebug ("SoundCore: creating decoder"); @@ -119,14 +122,17 @@ bool SoundCore::play(const QString &source) setEQ(m_bands, m_preamp); setEQEnabled(m_useEQ); } + qDebug("Decoder create OK"); if (m_decoder->initialize()) { m_output->start(); m_decoder->start(); m_error = NoError; + return TRUE; } + qDebug("12345678"); stop(); return FALSE; } @@ -178,17 +184,18 @@ void SoundCore::stop() { m_output->uninitialize(); } - if (m_input) - { - delete m_input; - m_input = 0; - } + //display->setTime(0); if (m_decoder) { delete m_decoder; m_decoder = 0; } + if (m_input) + { + delete m_input; + m_input = 0; + } // recreate output if (m_update && m_output) { diff --git a/lib/streamreader.cpp b/lib/streamreader.cpp new file mode 100644 index 000000000..c3b69161b --- /dev/null +++ b/lib/streamreader.cpp @@ -0,0 +1,208 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include <QApplication> +#include <QHttp> +#include <QUrl> + +#include "streamreader.h" + +StreamReader::StreamReader(const QString &name, QObject *parent) + : QIODevice(parent) +{ + + m_name =name; + qDebug(qPrintable(name)); + http = new QHttp(this); + + connect(http, SIGNAL(requestFinished(int, bool)), + this, SLOT(httpRequestFinished(int, bool))); + connect(http, SIGNAL(dataReadProgress(int, int)), + this, SLOT(updateDataReadProgress(int, int))); + connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), + this, SLOT(readResponseHeader(const QHttpResponseHeader &))); +} +StreamReader::~StreamReader() +{ + http->abort(); +} + + +bool StreamReader::atEnd () const +{ + return FALSE; +} + +qint64 StreamReader::bytesAvailable () const +{ + return http->bytesAvailable (); +} + +qint64 StreamReader::bytesToWrite () const +{ + return -1; +} + +bool StreamReader::canReadLine () const +{ + return FALSE; +} + +void StreamReader::close () +{ + httpRequestAborted = TRUE; + http->close(); +} + +bool StreamReader::isSequential () const +{ + return TRUE; +} + +bool StreamReader::open ( OpenMode mode ) +{ + if (mode != QIODevice::ReadOnly) + return FALSE; + downloadFile(); + if (httpRequestAborted) + return FALSE; + setOpenMode(QIODevice::ReadOnly); + return TRUE; +} + +qint64 StreamReader::pos () const +{ + return 0; +} + +bool StreamReader::reset () +{ + return TRUE; +} + +bool StreamReader::seek ( qint64 pos ) +{ + return FALSE; +} + +qint64 StreamReader::size () const +{ + return bytesAvailable (); +} + +bool StreamReader::waitForBytesWritten ( int msecs ) +{ + usleep(msecs*1000); + return TRUE; +} + +bool StreamReader::waitForReadyRead ( int msecs ) +{ + usleep(msecs*1000); + return TRUE; +} + +qint64 StreamReader::readData(char* data, qint64 maxlen) +{ + return http->read (data, maxlen); +} + +qint64 StreamReader::writeData(const char*, qint64) +{ + return 0; +} + +void StreamReader::downloadFile() +{ + QUrl url(m_name); + + http->setHost(url.host(), url.port() != -1 ? url.port() : 80); + if (!url.userName().isEmpty()) + http->setUser(url.userName(), url.password()); + + httpRequestAborted = false; + f = false; + qDebug("StreamReader: connecting..."); + httpGetId = http->get(url.path(), 0); + qDebug("StreamReader: buffering..."); + while (http->bytesAvailable () < BUFFER_SIZE*0.5 && !httpRequestAborted) + { + qApp->processEvents(); + } + qDebug("StreamReader: ready"); +} + +void StreamReader::cancelDownload() +{ + httpRequestAborted = true; + http->abort(); +} + +void StreamReader::httpRequestFinished(int requestId, bool error) +{ + if (httpRequestAborted) + { + return; + } + + if (requestId != httpGetId) + return; + + if (error) + { + qDebug(qPrintable(QString("StreamReader: %1").arg(http->errorString()))); + } + else + { + qDebug("StreamReader: end of file"); + } + +} + +void StreamReader::readResponseHeader(const QHttpResponseHeader &responseHeader) +{ + if (responseHeader.statusCode() != 200) + { + qDebug(qPrintable(QString("Download failed: %1.") .arg(responseHeader.reasonPhrase()))); + httpRequestAborted = true; + http->abort(); + return; + } +} + +void StreamReader::updateDataReadProgress(int bytesRead, int totalBytes) +{ + fillBuffer(); + //qDebug("%d, %d, %d",bytesRead,totalBytes,http->bytesAvailable ()); +} + +void StreamReader::fillBuffer() +{ + if (http->bytesAvailable () > BUFFER_SIZE && !httpRequestAborted) + { + while (http->bytesAvailable () > BUFFER_SIZE*0.75 && !httpRequestAborted) + { + qDebug("StreamReader: skipping data..."); + char *data = new char[BUFFER_SIZE/20]; + http->read (data, BUFFER_SIZE/20); + qApp->processEvents(); + delete data; + } + } +} diff --git a/lib/streamreader.h b/lib/streamreader.h new file mode 100644 index 000000000..f8bcc753c --- /dev/null +++ b/lib/streamreader.h @@ -0,0 +1,77 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifndef STREAMREADER_H +#define STREAMREADER_H + +#include <QObject> +#include <QIODevice> +#include <QHttp> +#define BUFFER_SIZE 524288 + +class QUrl; +class QFileInfo; + +/** + @author Ilya Kotov <forkotov02@hotmail.ru> +*/ +class StreamReader : public QIODevice +{ + Q_OBJECT +public: + StreamReader(const QString &name, QObject *parent = 0); + + ~StreamReader(); + + bool atEnd () const; + qint64 bytesAvailable () const; + qint64 bytesToWrite () const; + bool canReadLine () const; + void close (); + bool isSequential () const; + bool open ( OpenMode mode ); + qint64 pos () const; + bool reset (); + bool seek ( qint64 pos ); + qint64 size () const; + bool waitForBytesWritten ( int msecs ); + bool waitForReadyRead ( int msecs ); + +protected: + qint64 readData(char*, qint64); + qint64 writeData(const char*, qint64); + + +private slots: + void downloadFile(); + void cancelDownload(); + void httpRequestFinished(int, bool); + void readResponseHeader(const QHttpResponseHeader &responseHeader); + void updateDataReadProgress(int bytesRead, int totalBytes); + +private: + void fillBuffer(); + QString m_name; + QHttp *http; + bool httpRequestAborted; + int httpGetId; + bool f; +}; + +#endif |
