Permalink
Browse files

MythMusic: re-factor the IODevice handling

This makes the various DecoderIOFactory*'s responsible for the IODevice they
create and avoids passing pointers to them around.
  • Loading branch information...
Paul Harrison
Paul Harrison committed Apr 21, 2013
1 parent 6187d76 commit 16f27bacda7d5d8431228582e06adec348b72ea5
@@ -29,6 +29,7 @@
#include <audiooutput.h>
#include <audiooutpututil.h>
#include <mythlogging.h>
#include <decoderhandler.h>
using namespace std;
@@ -171,9 +172,8 @@ static void myth_av_log(void *ptr, int level, const char* fmt, va_list vl)
string_lock.unlock();
}
avfDecoder::avfDecoder(const QString &file, DecoderFactory *d, QIODevice *i,
AudioOutput *o) :
Decoder(d, i, o),
avfDecoder::avfDecoder(const QString &file, DecoderFactory *d, AudioOutput *o) :
Decoder(d, o),
inited(false), user_stop(false),
stat(0), output_buf(NULL),
output_at(0), bks(0),
@@ -183,8 +183,8 @@ avfDecoder::avfDecoder(const QString &file, DecoderFactory *d, QIODevice *i,
m_sampleFmt(FORMAT_NONE), m_channels(0),
seekTime(-1.0), devicename(""),
m_inputFormat(NULL), m_inputContext(NULL),
m_codec(NULL),
m_audioDec(NULL), m_inputIsFile(false),
m_codec(NULL), m_audioDec(NULL),
m_inputIsFile(false),
m_buffer(NULL), m_byteIOContext(NULL),
errcode(0)
{
@@ -430,7 +430,6 @@ void avfDecoder::deinit()
freq = bitrate = 0;
stat = m_channels = 0;
m_sampleFmt = FORMAT_NONE;
setInput(0);
setOutput(0);
// Cleanup here
@@ -612,20 +611,18 @@ const QString &avfDecoderFactory::description() const
return desc;
}
Decoder *avfDecoderFactory::create(const QString &file, QIODevice *input,
AudioOutput *output, bool deletable)
Decoder *avfDecoderFactory::create(const QString &file, AudioOutput *output, bool deletable)
{
if (deletable)
return new avfDecoder(file, this, input, output);
return new avfDecoder(file, this, output);
static avfDecoder *decoder = 0;
if (!decoder)
{
decoder = new avfDecoder(file, this, input, output);
decoder = new avfDecoder(file, this, output);
}
else
{
decoder->setInput(input);
decoder->setOutput(output);
}
@@ -17,7 +17,7 @@ class Metadata;
class avfDecoder : public Decoder
{
public:
avfDecoder(const QString &file, DecoderFactory *, QIODevice *, AudioOutput *);
avfDecoder(const QString &file, DecoderFactory *, AudioOutput *);
virtual ~avfDecoder(void);
bool initialize();
@@ -91,9 +91,8 @@ class StCdioDevice
};
CdDecoder::CdDecoder(const QString &file, DecoderFactory *d, QIODevice *i,
AudioOutput *o) :
Decoder(d, i, o),
CdDecoder::CdDecoder(const QString &file, DecoderFactory *d, AudioOutput *o) :
Decoder(d, o),
m_inited(false), m_user_stop(false),
m_devicename(""),
m_stat(DecoderEvent::Error),
@@ -308,7 +307,6 @@ void CdDecoder::deinit()
m_freq = m_bitrate = 0L;
m_stat = DecoderEvent::Finished;
m_chan = 0;
setInput(0);
setOutput(0);
}
@@ -844,17 +842,18 @@ const QString &CdDecoderFactory::description() const
}
// pure virtual
Decoder *CdDecoderFactory::create(const QString &file, QIODevice *input,
AudioOutput *output, bool deletable)
Decoder *CdDecoderFactory::create(const QString &file, AudioOutput *output, bool deletable)
{
if (deletable)
return new CdDecoder(file, this, input, output);
return new CdDecoder(file, this, output);
static CdDecoder *decoder;
if (! decoder) {
decoder = new CdDecoder(file, this, input, output);
} else {
decoder->setInput(input);
if (! decoder)
{
decoder = new CdDecoder(file, this, output);
}
else
{
decoder->setFilename(file);
decoder->setOutput(output);
}
@@ -20,7 +20,7 @@ class Metadata;
class CdDecoder : public Decoder
{
public:
CdDecoder(const QString &file, DecoderFactory *, QIODevice *, AudioOutput *);
CdDecoder(const QString &file, DecoderFactory *, AudioOutput *);
virtual ~CdDecoder();
// Decoder implementation
@@ -752,7 +752,7 @@ void Ripper::scanCD(void)
if (m_decoder)
delete m_decoder;
m_decoder = new CdDecoder("cda", NULL, NULL, NULL);
m_decoder = new CdDecoder("cda", NULL, NULL);
if (m_decoder)
m_decoder->setDevice(m_CDdevice);
}
@@ -8,6 +8,7 @@
#include "constants.h"
#include "metadata.h"
#include "metaio.h"
#include "musicplayer.h"
#include <QDir>
@@ -24,23 +25,20 @@ QEvent::Type DecoderEvent::Finished =
QEvent::Type DecoderEvent::Error =
(QEvent::Type) QEvent::registerEventType();
Decoder::Decoder(DecoderFactory *d, QIODevice *i, AudioOutput *o) :
MThread("MythMusicDecoder"), fctry(d), in(i), out(o)
Decoder::Decoder(DecoderFactory *d, AudioOutput *o) :
MThread("MythMusicDecoder"), fctry(d), out(o)
{
}
Decoder::~Decoder()
{
fctry = 0;
in = 0;
out = 0;
}
void Decoder::setInput(QIODevice *i)
QIODevice *Decoder::input(void)
{
lock();
in = i;
unlock();
return gPlayer->getDecoderHandler()->getIOFactory()->getInput();
}
void Decoder::setOutput(AudioOutput *o)
@@ -105,19 +103,16 @@ void Decoder::registerFactory(DecoderFactory *fact)
factories->push_back(fact);
}
Decoder *Decoder::create(const QString &source, QIODevice *input,
AudioOutput *output, bool deletable)
Decoder *Decoder::create(const QString &source, AudioOutput *output, bool deletable)
{
checkFactories();
QList<DecoderFactory*>::iterator it = factories->begin();
for (; it != factories->end(); ++it)
{
if ((*it)->supports(source))
return (*it)->create(source, input, output, deletable);
return (*it)->create(source, output, deletable);
}
return NULL;
}
@@ -74,9 +74,8 @@ class Decoder : public MThread, public MythObservable
DecoderFactory *factory() const { return fctry; }
QIODevice *input() { return in; }
QIODevice *input(void);
AudioOutput *output() { return out; }
void setInput(QIODevice *);
void setOutput(AudioOutput *);
void setFilename(const QString &newName) { filename = newName; }
@@ -93,11 +92,10 @@ class Decoder : public MThread, public MythObservable
static QStringList all();
static bool supports(const QString &);
static void registerFactory(DecoderFactory *);
static Decoder *create(const QString &, QIODevice *, AudioOutput *,
bool = false);
static Decoder *create(const QString &, AudioOutput *, bool = false);
protected:
Decoder(DecoderFactory *, QIODevice *, AudioOutput *);
Decoder(DecoderFactory *, AudioOutput *);
QMutex* getMutex(void) { return &mtx; }
void error(const QString &);
@@ -106,7 +104,6 @@ class Decoder : public MThread, public MythObservable
private:
DecoderFactory *fctry;
QIODevice *in;
AudioOutput *out;
QMutex mtx;
@@ -120,7 +117,7 @@ class DecoderFactory
virtual bool supports(const QString &source) const = 0;
virtual const QString &extension() const = 0; // file extension, ie. ".mp3" or ".ogg"
virtual const QString &description() const = 0; // file type, ie. "MPEG Audio Files"
virtual Decoder *create(const QString &, QIODevice *, AudioOutput *, bool) = 0;
virtual Decoder *create(const QString &, AudioOutput *, bool) = 0;
virtual ~DecoderFactory() {}
};
@@ -130,7 +127,7 @@ class CdDecoderFactory : public DecoderFactory
bool supports(const QString &) const;
const QString &extension() const;
const QString &description() const;
Decoder *create(const QString &, QIODevice *, AudioOutput *, bool);
Decoder *create(const QString &, AudioOutput *, bool);
};
class avfDecoderFactory : public DecoderFactory
@@ -139,7 +136,7 @@ class avfDecoderFactory : public DecoderFactory
bool supports(const QString &) const;
const QString &extension() const;
const QString &description() const;
Decoder *create(const QString &, QIODevice *, AudioOutput *, bool);
Decoder *create(const QString &, AudioOutput *, bool);
};
#endif
@@ -119,11 +119,9 @@ DecoderIOFactoryFile::~DecoderIOFactoryFile(void)
delete m_input;
}
QIODevice* DecoderIOFactoryFile::takeInput(void)
QIODevice* DecoderIOFactoryFile::getInput(void)
{
QIODevice *result = m_input;
m_input = NULL;
return result;
return m_input;
}
void DecoderIOFactoryFile::start(void)
@@ -150,11 +148,9 @@ DecoderIOFactorySG::~DecoderIOFactorySG(void)
delete m_input;
}
QIODevice* DecoderIOFactorySG::takeInput(void)
QIODevice* DecoderIOFactorySG::getInput(void)
{
QIODevice *result = m_input;
m_input = NULL;
return result;
return m_input;
}
void DecoderIOFactorySG::start(void)
@@ -190,11 +186,9 @@ DecoderIOFactoryUrl::~DecoderIOFactoryUrl(void)
delete m_input;
}
QIODevice* DecoderIOFactoryUrl::takeInput(void)
QIODevice* DecoderIOFactoryUrl::getInput(void)
{
QIODevice *result = m_input;
//m_input = NULL;
return result;
return m_input;
}
void DecoderIOFactoryUrl::start(void)
@@ -426,7 +420,6 @@ void DecoderHandler::stop(void)
if (m_decoder)
{
m_decoder->wait();
//delete m_decoder->input(); // TODO: need to sort out who is responsible for the input
delete m_decoder;
m_decoder = NULL;
}
@@ -568,14 +561,13 @@ void DecoderHandler::doConnectDecoder(const QUrl &url, const QString &format)
if (!m_decoder)
{
if ((m_decoder = Decoder::create(format, NULL, NULL, true)) == NULL)
if ((m_decoder = Decoder::create(format, NULL, true)) == NULL)
{
doFailed(url, QString("No decoder for this format '%1'").arg(format));
return;
}
}
m_decoder->setInput(getIOFactory()->takeInput());
m_decoder->setFilename(url.toString());
DecoderHandlerEvent ev(DecoderHandlerEvent::Ready);
@@ -758,8 +750,6 @@ int MusicIODevice::ungetch(int)
MusicSGIODevice::MusicSGIODevice(const QString &url)
{
m_remotefile = new RemoteFile(url);
m_remotefile->Open();
setOpenMode(ReadWrite);
}
@@ -99,6 +99,8 @@ class DecoderHandler : public QObject, public MythObservable
virtual ~DecoderHandler(void);
Decoder *getDecoder(void) { return m_decoder; }
DecoderIOFactory *getIOFactory(void) { return m_io_factory; }
void start(Metadata *mdata);
void stop(void);
@@ -121,7 +123,6 @@ class DecoderHandler : public QObject, public MythObservable
void createPlaylistFromRemoteUrl(const QUrl &url);
bool haveIOFactory(void) { return m_io_factory != NULL; }
DecoderIOFactory *getIOFactory(void) { return m_io_factory; }
void createIOFactory(const QUrl &url);
void deleteIOFactory(void);
@@ -150,7 +151,7 @@ class DecoderIOFactory : public QObject, public MythObservable
virtual void start(void) = 0;
virtual void stop(void) = 0;
virtual QIODevice *takeInput(void) = 0;
virtual QIODevice *getInput(void) = 0;
void setUrl (const QUrl &url) { m_url = url; }
void setMeta (Metadata *meta) { m_meta = *meta; }
@@ -183,7 +184,7 @@ class DecoderIOFactoryFile : public DecoderIOFactory
~DecoderIOFactoryFile(void);
void start(void);
void stop() {}
QIODevice *takeInput(void);
QIODevice *getInput(void);
private:
QIODevice *m_input;
@@ -198,7 +199,7 @@ class DecoderIOFactorySG : public DecoderIOFactory
~DecoderIOFactorySG(void);
void start(void);
void stop() {}
QIODevice *takeInput(void);
QIODevice *getInput(void);
private:
QIODevice *m_input;
@@ -214,7 +215,7 @@ class DecoderIOFactoryUrl : public DecoderIOFactory
void start(void);
void stop(void);
QIODevice *takeInput(void);
QIODevice *getInput(void);
protected slots:
void replyFinished(QNetworkReply *reply);
@@ -291,7 +292,7 @@ class MusicSGIODevice : public QIODevice
public:
MusicSGIODevice(const QString &url);
~MusicSGIODevice(void);
virtual ~MusicSGIODevice(void);
virtual bool open(OpenMode mode);
virtual void close(void) { };
@@ -300,7 +301,7 @@ class MusicSGIODevice : public QIODevice
qint64 size(void) const;
qint64 pos(void) const { return 0; }
qint64 bytesAvailable(void) const;
bool isSequential(void) const { return true; }
bool isSequential(void) const { return false; }
bool seek(qint64 pos);
qint64 readData(char *data, qint64 sz);
Oops, something went wrong.

0 comments on commit 16f27ba

Please sign in to comment.