00001
00002
00003
00004
00005
00006
00007 #include "decoder.h"
00008 #include "constants.h"
00009 #include "metadata.h"
00010 #include "metaio.h"
00011
00012 #include <QDir>
00013
00014 #include <mythcontext.h>
00015 #include <output.h>
00016 #include <visual.h>
00017
00018 QEvent::Type DecoderEvent::Decoding =
00019 (QEvent::Type) QEvent::registerEventType();
00020 QEvent::Type DecoderEvent::Stopped =
00021 (QEvent::Type) QEvent::registerEventType();
00022 QEvent::Type DecoderEvent::Finished =
00023 (QEvent::Type) QEvent::registerEventType();
00024 QEvent::Type DecoderEvent::Error =
00025 (QEvent::Type) QEvent::registerEventType();
00026
00027 Decoder::Decoder(DecoderFactory *d, QIODevice *i, AudioOutput *o) :
00028 MThread("MythMusicDecoder"), fctry(d), in(i), out(o)
00029 {
00030 }
00031
00032 Decoder::~Decoder()
00033 {
00034 fctry = 0;
00035 in = 0;
00036 out = 0;
00037 }
00038
00039 void Decoder::setInput(QIODevice *i)
00040 {
00041 lock();
00042 in = i;
00043 unlock();
00044 }
00045
00046 void Decoder::setOutput(AudioOutput *o)
00047 {
00048 lock();
00049 out = o;
00050 unlock();
00051 }
00052
00053 void Decoder::error(const QString &e)
00054 {
00055 QString *str = new QString(e.toUtf8());
00056 DecoderEvent ev(str);
00057 dispatch(ev);
00058 }
00059
00067 Metadata *Decoder::readMetadata(void)
00068 {
00069 Metadata *mdata = NULL;
00070 MetaIO* p_tagger = doCreateTagger();
00071
00072 if (p_tagger)
00073 {
00074 if (!ignore_id3)
00075 mdata = p_tagger->read(filename);
00076
00077 if (ignore_id3 || !mdata)
00078 mdata = p_tagger->readFromFilename(filename);
00079
00080 delete p_tagger;
00081 }
00082
00083 if (!mdata)
00084 {
00085 LOG(VB_GENERAL, LOG_ERR,
00086 QString("Decoder::readMetadata(): Could not read '%1'")
00087 .arg(filename));
00088 }
00089
00090 return mdata;
00091 }
00092
00093
00102 Metadata* Decoder::getMetadata(void)
00103 {
00104
00105 Metadata *mdata = new Metadata(filename);
00106 if (mdata->isInDatabase())
00107 {
00108 return mdata;
00109 }
00110
00111 delete mdata;
00112
00113 return readMetadata();
00114 }
00115
00116
00136 MetaIO *Decoder::doCreateTagger(void)
00137 {
00138 return NULL;
00139 }
00140
00150 void Decoder::commitMetadata(Metadata *mdata)
00151 {
00152 MetaIO* p_tagger = doCreateTagger();
00153 if (p_tagger)
00154 {
00155 p_tagger->write(mdata);
00156 delete p_tagger;
00157 }
00158 }
00159
00170 void Decoder::commitVolatileMetadata(const Metadata *mdata)
00171 {
00172 if (!mdata || !GetMythDB()->GetNumSetting("AllowTagWriting", 0))
00173 return;
00174
00175 MetaIO* p_tagger = doCreateTagger();
00176 if (p_tagger)
00177 {
00178 p_tagger->writeVolatileMetadata(mdata);
00179 delete p_tagger;
00180 }
00181
00182 mdata->UpdateModTime();
00183 }
00184
00185
00186
00187 int Decoder::ignore_id3 = 0;
00188 QString Decoder::musiclocation;
00189
00190 void Decoder::SetLocationFormatUseTags(void)
00191 {
00192 QString startdir = gCoreContext->GetSetting("MusicLocation");
00193 startdir = QDir::cleanPath(startdir);
00194 if (!startdir.endsWith("/"))
00195 startdir += "/";
00196
00197 musiclocation = startdir;
00198
00199 ignore_id3 = gCoreContext->GetNumSetting("Ignore_ID3", 0);
00200 }
00201
00202 static QList<DecoderFactory*> *factories = NULL;
00203
00204 static void checkFactories()
00205 {
00206 if (!factories)
00207 {
00208 factories = new QList<DecoderFactory*>;
00209
00210 #ifdef HAVE_CDIO
00211 Decoder::registerFactory(new CdDecoderFactory);
00212 #endif
00213 Decoder::registerFactory(new avfDecoderFactory);
00214 }
00215 }
00216
00217 QStringList Decoder::all()
00218 {
00219 checkFactories();
00220
00221 QStringList l;
00222
00223 QList<DecoderFactory*>::iterator it = factories->begin();
00224 for (; it != factories->end(); ++it)
00225 l += (*it)->description();
00226
00227 return l;
00228 }
00229
00230 bool Decoder::supports(const QString &source)
00231 {
00232 checkFactories();
00233
00234 QList<DecoderFactory*>::iterator it = factories->begin();
00235 for (; it != factories->end(); ++it)
00236 {
00237 if ((*it)->supports(source))
00238 return true;
00239 }
00240
00241 return false;
00242 }
00243
00244 void Decoder::registerFactory(DecoderFactory *fact)
00245 {
00246 factories->push_back(fact);
00247 }
00248
00249 Decoder *Decoder::create(const QString &source, QIODevice *input,
00250 AudioOutput *output, bool deletable)
00251 {
00252 checkFactories();
00253
00254 QList<DecoderFactory*>::iterator it = factories->begin();
00255 for (; it != factories->end(); ++it)
00256 {
00257 if ((*it)->supports(source))
00258 return (*it)->create(source, input, output, deletable);
00259 }
00260
00261 return NULL;
00262 }
00263
00264