diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2007-09-18 17:43:25 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2007-09-18 17:43:25 +0000 |
| commit | d80498e562ae70cfe5f0d2400046e6e4db6af260 (patch) | |
| tree | f45e3480c94b6cf1c756b4f35a5c42eead8fabc4 /lib | |
| parent | 41c154d03342aa03cc3932ca032eba387c7a2bff (diff) | |
| download | qmmp-d80498e562ae70cfe5f0d2400046e6e4db6af260.tar.gz qmmp-d80498e562ae70cfe5f0d2400046e6e4db6af260.tar.bz2 qmmp-d80498e562ae70cfe5f0d2400046e6e4db6af260.zip | |
improved stream support
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@157 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/downloader.cpp | 20 | ||||
| -rw-r--r-- | lib/downloader.h | 12 | ||||
| -rw-r--r-- | lib/soundcore.cpp | 85 | ||||
| -rw-r--r-- | lib/soundcore.h | 6 | ||||
| -rw-r--r-- | lib/streamreader.cpp | 12 | ||||
| -rw-r--r-- | lib/streamreader.h | 6 |
6 files changed, 86 insertions, 55 deletions
diff --git a/lib/downloader.cpp b/lib/downloader.cpp index a83c68ef2..b9a9c67ab 100644 --- a/lib/downloader.cpp +++ b/lib/downloader.cpp @@ -38,6 +38,7 @@ static size_t curl_write_data(void *data, size_t size, size_t nmemb, dl->stream()->buf = (char *)realloc (dl->stream()->buf, dl->stream()->buf_fill); memcpy (dl->stream()->buf + buf_start, data, data_size); dl->mutex()->unlock(); + dl->checkBuffer(); return data_size; } @@ -232,7 +233,7 @@ void Downloader::run() curl_easy_setopt(m_handle, CURLOPT_PROGRESSFUNCTION, curl_progress); // Any kind of authentication curl_easy_setopt(m_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - //curl_easy_setopt(m_handle, CURLOPT_VERBOSE, 1); + curl_easy_setopt(m_handle, CURLOPT_VERBOSE, 1); // Auto referrer curl_easy_setopt(m_handle, CURLOPT_AUTOREFERER, 1); // Follow redirections @@ -250,6 +251,7 @@ void Downloader::run() m_stream.buf = 0; m_stream.aborted = FALSE; m_stream.header.clear (); + m_ready = FALSE; int return_code; qDebug("Downloader: starting libcurl"); m_mutex.unlock(); @@ -284,6 +286,22 @@ const QString &Downloader::title() const return m_title; } +void Downloader::checkBuffer() +{ + if(m_stream.buf_fill > BUFFER_SIZE && !m_ready) + { + m_ready = TRUE; + qDebug("Downloader: ready"); + emit readyRead(); + } + +} + +bool Downloader::isReady() +{ + return m_ready; +} + void Downloader::readICYMetaData() { uint8_t packet_size; diff --git a/lib/downloader.h b/lib/downloader.h index 13be0121e..05175e379 100644 --- a/lib/downloader.h +++ b/lib/downloader.h @@ -27,8 +27,10 @@ #include <curl/curl.h> +#define BUFFER_SIZE 128000 + /** - @author Ilya Kotov <forkotov02@hotmail.ru> + @author Ilya Kotov <forkotov02@hotmail.ru> */ struct Stream @@ -44,7 +46,7 @@ struct Stream class Downloader : public QThread { -Q_OBJECT + Q_OBJECT public: Downloader(QObject *parent, const QString &url); @@ -57,9 +59,12 @@ public: void abort(); int bytesAvailable(); const QString& title() const; + void checkBuffer(); + bool isReady(); signals: - void titleChanged (); + void titleChanged(); + void readyRead(); private: qint64 readBuffer(char* data, qint64 maxlen); @@ -71,6 +76,7 @@ private: QString m_url; int m_metacount; QString m_title; + bool m_ready; protected: void run(); diff --git a/lib/soundcore.cpp b/lib/soundcore.cpp index 8c59d015b..4b4d9a6fb 100644 --- a/lib/soundcore.cpp +++ b/lib/soundcore.cpp @@ -78,6 +78,7 @@ bool SoundCore::play(const QString &source) m_input = new StreamReader(source, this); connect(m_input, SIGNAL(titleChanged(const QString&)), SIGNAL(titleChanged(const QString&))); + connect(m_input, SIGNAL(readyRead()),SLOT(decode())); } else m_input = new QFile(source); @@ -93,6 +94,7 @@ bool SoundCore::play(const QString &source) } connect(m_output, SIGNAL(stateChanged(const OutputState&)), SIGNAL(outputStateChanged(const OutputState&))); + connect(m_input, SIGNAL(readyRead()),SLOT(read())); } if (! m_output->initialize()) return FALSE; @@ -104,41 +106,13 @@ bool SoundCore::play(const QString &source) m_vis->setOutput(m_output); m_output->addVisual(m_vis); } + m_source = source; + if (source.left(4) != "http") + return decode(); + else + qobject_cast<StreamReader *>(m_input)->downloadFile(); + return TRUE; - if (! m_decoder) - { - m_block = TRUE; - qDebug ("SoundCore: creating decoder"); - m_decoder = Decoder::create(this, source, m_input, m_output); - - if (! m_decoder) - { - qWarning("SoundCore: unsupported fileformat"); - m_block = FALSE; - stop(); - emit decoderStateChanged(DecoderState(DecoderState::Error)); - return FALSE; - } - qDebug ("ok"); - m_decoder->setBlockSize(globalBlockSize); - connect(m_decoder, SIGNAL(stateChanged(const DecoderState&)), - SIGNAL(decoderStateChanged(const DecoderState&))); - setEQ(m_bands, m_preamp); - setEQEnabled(m_useEQ); - } - qDebug("SoundCore: decoder was created successfully"); - - if (m_decoder->initialize()) - { - m_output->start(); - m_decoder->start(); - m_error = NoError; - m_block = FALSE; - return TRUE; - } - stop(); - m_block = FALSE; - return FALSE; } uint SoundCore::error() @@ -157,7 +131,6 @@ void SoundCore::stop() m_decoder->stop(); m_decoder->mutex()->unlock(); } - if (m_output) { m_output->mutex()->lock (); @@ -172,20 +145,16 @@ void SoundCore::stop() m_decoder->cond()->wakeAll(); m_decoder->mutex()->unlock(); } - if (m_output) { m_output->recycler()->mutex()->lock (); m_output->recycler()->cond()->wakeAll(); m_output->recycler()->mutex()->unlock(); } - if (m_decoder) m_decoder->wait(); - if (m_output) m_output->wait(); - if (m_output && m_output->isInitialized()) { m_output->uninitialize(); @@ -324,3 +293,41 @@ void SoundCore::addVisualization(Visualization *visual) { m_vis = visual; } + +bool SoundCore::decode() +{ + if (! m_decoder) + { + m_block = TRUE; + qDebug ("SoundCore: creating decoder"); + m_decoder = Decoder::create(this, m_source, m_input, m_output); + + if (! m_decoder) + { + qWarning("SoundCore: unsupported fileformat"); + m_block = FALSE; + stop(); + emit decoderStateChanged(DecoderState(DecoderState::Error)); + return FALSE; + } + qDebug ("ok"); + m_decoder->setBlockSize(globalBlockSize); + connect(m_decoder, SIGNAL(stateChanged(const DecoderState&)), + SIGNAL(decoderStateChanged(const DecoderState&))); + setEQ(m_bands, m_preamp); + setEQEnabled(m_useEQ); + } + qDebug("SoundCore: decoder was created successfully"); + + if (m_decoder->initialize()) + { + m_output->start(); + m_decoder->start(); + m_error = NoError; + m_block = FALSE; + return TRUE; + } + stop(); + m_block = FALSE; + return FALSE; +} diff --git a/lib/soundcore.h b/lib/soundcore.h index 7e88942f7..c1c3a3be5 100644 --- a/lib/soundcore.h +++ b/lib/soundcore.h @@ -20,7 +20,9 @@ #ifndef SOUNDCORE_H #define SOUNDCORE_H + #include <QObject> +#include <QString> #include "decoder.h" #include "output.h" @@ -158,6 +160,9 @@ signals: */ void titleChanged(const QString& title); +private slots: + bool decode(); + private: Decoder* m_decoder; Output* m_output; @@ -170,6 +175,7 @@ private: int m_preamp; int m_bands[10]; Visualization *m_vis; + QString m_source; }; #endif diff --git a/lib/streamreader.cpp b/lib/streamreader.cpp index d43f8c4f8..30bb9c3a7 100644 --- a/lib/streamreader.cpp +++ b/lib/streamreader.cpp @@ -28,6 +28,7 @@ StreamReader::StreamReader(const QString &name, QObject *parent) { m_downloader = new Downloader(this, name); connect(m_downloader, SIGNAL(titleChanged()),SLOT(updateTitle())); + connect(m_downloader, SIGNAL(readyRead()), SIGNAL(readyRead())); } StreamReader::~StreamReader() @@ -70,9 +71,9 @@ bool StreamReader::open ( OpenMode mode ) { if (mode != QIODevice::ReadOnly) return FALSE; - downloadFile(); + //downloadFile(); setOpenMode(QIODevice::ReadOnly); - if (m_downloader->isRunning()) + if (m_downloader->isRunning() && m_downloader->isReady()) return TRUE; else return FALSE; @@ -119,14 +120,7 @@ qint64 StreamReader::writeData(const char*, qint64) void StreamReader::downloadFile() { - qDebug("StreamReader: buffering..."); m_downloader->start(); - while (m_downloader->bytesAvailable () < BUFFER_SIZE*0.5 && m_downloader->isRunning ()) - { - qApp->processEvents(); - //sleep(1); - } - qDebug("StreamReader: ready"); } void StreamReader::updateTitle() diff --git a/lib/streamreader.h b/lib/streamreader.h index 906ddd2ac..c8182153a 100644 --- a/lib/streamreader.h +++ b/lib/streamreader.h @@ -24,8 +24,6 @@ #include <QIODevice> #include <QUrl> -#define BUFFER_SIZE 524288 - class QFileInfo; class Downloader; @@ -62,9 +60,11 @@ public: * returns content type of a stream */ const QString &contentType(); + void downloadFile(); signals: void titleChanged(const QString&); + void readyRead(); protected: qint64 readData(char*, qint64); @@ -75,7 +75,7 @@ private slots: void updateTitle(); private: - void downloadFile(); + //void downloadFile(); void fillBuffer(); QUrl m_url; QString m_contentType; |
