Ticket #341: decoder-refactor.patch

File decoder-refactor.patch, 14.1 KB (added by eskil <myth@…>, 18 years ago)

refactoring of Decoder and subclasses

  • mythplugins/mythmusic/mythmusic/decoder.h

     
    88#include <qptrlist.h>
    99
    1010class Metadata;
     11class MetaIO;
    1112class Decoder;
    1213class DecoderFactory;
    1314
     
    7980    static Decoder *create(const QString &, QIODevice *, AudioOutput *,
    8081                           bool = FALSE);
    8182
    82     virtual Metadata *getMetadata() = 0;
    83     virtual void commitMetadata(Metadata *mdata) = 0;
     83    /** \brief Read the metadata from \p filename directly.
     84       
     85        Creates a \p MetaIO object using \p Decoder::doCreateTagger and uses
     86        the MetaIO object to read the metadata.
     87       
     88        \returns an instance of \p Metadata owned by the caller
     89     */
     90    virtual Metadata *readMetadata();
    8491
     92    /** \brief Get the metadata  for \p filename
     93       
     94        First tries to read the metadata from the database. If there
     95        is no database entry, it'll call \p Decoder::readMetadata.
     96
     97                \returns an instance of \p Metadata owned by the caller
     98    */
     99    virtual Metadata *getMetadata();
     100
     101    /** \brief Create a \p MetaIO object for the format.
     102
     103        This method should be overwritten by subclasses to return an
     104        instance of the appropriate MetaIO subtype. It is used by \p
     105        Decoder::getMetadata, \p Decoder::readMetadata and \p
     106        Decoder::commitMetadata.
     107       
     108        The default implementation returns a NULL pointer, which
     109        essentially means, that if the decoder does not overrider this
     110        method or all of the users (see previous paragraph), files
     111        that the decoder supports cannot be indexed using metadata in
     112        the file.
     113
     114                Eg. the mp3 decoder (\p MadDecoder) implements this, whereas
     115                the audio CD decoder (\p CdDecoder) does not.
     116
     117                \returns an instance of \p MetaIO owned by the caller
     118     */
     119    virtual MetaIO *doCreateTagger ();
     120
     121    /** \brief Write the given metadata to the \p filename.
     122       
     123        Creates a \p MetaIO object using \p Decoder::doCreateTagger and
     124        asks the MetaIO object to write the contents of mdata to \p
     125        filename.
     126
     127        \params mdata the metadata to write to the disk
     128     */
     129    virtual void commitMetadata(Metadata *mdata);
     130
    85131    static void SetLocationFormatUseTags(void);
    86132
    87133    QString getFilename(void) { return filename; }
  • mythplugins/mythmusic/mythmusic/aacdecoder.cpp

     
    614614    deinit();
    615615}
    616616
    617 Metadata* aacDecoder::getMetadata()
     617MetaIO* aacDecoder::doCreateTagger()
    618618{
    619 
    620     Metadata *mdata = new Metadata(filename);
    621     if (mdata->isInDatabase(musiclocation))
    622     {
    623       return mdata;
    624     }
    625    
    626     delete mdata;
    627 
    628     MetaIOMP4* p_tagger = new MetaIOMP4;
    629     if (ignore_id3) {
    630         mdata = p_tagger->readFromFilename(filename);
    631     } else {
    632         mdata = p_tagger->read(filename);
    633     }
    634    
    635     delete p_tagger;
    636 
    637     if (mdata)
    638         mdata->dumpToDatabase(musiclocation);
    639     else
    640       error(QString("aacdecoder.o: Could not read metadata from \"%1\"").arg(filename.local8Bit()));
    641 
    642     return mdata;
    643 }   
    644 
    645 void aacDecoder::commitMetadata(Metadata *mdata)
    646 {
    647     MetaIOMP4* p_tagger = new MetaIOMP4;
    648     p_tagger->write(mdata);
    649     delete p_tagger;
     619    return new MetaIOMP4;
    650620}
    651621
    652622uint32_t aacDecoder::aacRead(char *buffer, uint32_t length)
  • mythplugins/mythmusic/mythmusic/decoder.cpp

     
    66
    77#include "decoder.h"
    88#include "constants.h"
     9#include "metadata.h"
     10#include "metaio.h"
    911#include <mythtv/output.h>
    1012#include <mythtv/visual.h>
    1113
     
    8789    listeners.remove(object);
    8890}
    8991
     92Metadata *Decoder::readMetadata () {
     93    Metadata *mdata = NULL;
     94    MetaIO* p_tagger = doCreateTagger ();
    9095
     96    if (p_tagger) {
     97        if (ignore_id3)
     98            mdata = p_tagger->readFromFilename(filename);
     99        else
     100            mdata = p_tagger->read(filename);
     101       
     102        delete p_tagger;
     103    } else {
     104        if (!mdata)
     105            cerr << "maddecoder.o: Could not read metadata from " << filename.local8Bit() << endl;
     106    }
     107
     108    return mdata;   
     109}
     110
     111Metadata* Decoder::getMetadata()
     112{
     113
     114    Metadata *mdata = new Metadata(filename);
     115    if (mdata->isInDatabase(musiclocation))
     116    {
     117      return mdata;
     118    }
     119   
     120    delete mdata;
     121
     122    return readMetadata ();
     123}   
     124
     125void Decoder::commitMetadata(Metadata *mdata)
     126{
     127    MetaIO* p_tagger = doCreateTagger ();
     128    if (p_tagger) {
     129        p_tagger->write(mdata);
     130        delete p_tagger;
     131    }
     132}
     133
     134MetaIO *Decoder::doCreateTagger () {
     135    return NULL;
     136}
     137
    91138// static methods
    92139
    93140int Decoder::ignore_id3 = 0;
  • mythplugins/mythmusic/mythmusic/aacdecoder.h

     
    2929    void seek(double);
    3030    void stop();
    3131
    32     Metadata *getMetadata();
    33     void commitMetadata(Metadata *mdata);
     32    MetaIO *doCreateTagger();
    3433
    3534    bool     initializeMP4();
    3635    int      getAACTrack(mp4ff_t *infile);
  • mythplugins/mythmusic/mythmusic/vorbisdecoder.cpp

     
    293293    deinit();
    294294}
    295295
    296 Metadata *VorbisDecoder::getMetadata()
     296MetaIO *VorbisDecoder::doCreateTagger()
    297297{
    298     Metadata *mdata = new Metadata(filename);
    299     if (mdata->isInDatabase(musiclocation))
    300     {
    301         return mdata;
    302     }
    303 
    304     delete mdata;
    305 
    306 
    307     MetaIOOggVorbisComment* p_tagger = new MetaIOOggVorbisComment;
    308     if (ignore_id3)
    309         mdata = p_tagger->readFromFilename(filename);
    310     else
    311         mdata = p_tagger->read(filename);
    312 
    313     delete p_tagger;
    314 
    315     if (mdata)
    316         mdata->dumpToDatabase(musiclocation);
    317     else
    318         cerr << "vorbisdecoder.o: Could not read metadata from " << filename.local8Bit() << endl;   
    319 
    320     return mdata;
    321 }   
    322 
    323 void VorbisDecoder::commitMetadata(Metadata *mdata)
    324 {
    325     MetaIOOggVorbisComment* p_tagger = new MetaIOOggVorbisComment;
    326     p_tagger->write(mdata);
    327     delete p_tagger;
     298    return new MetaIOOggVorbisComment;
    328299}
    329300
    330301
  • mythplugins/mythmusic/mythmusic/flacdecoder.h

     
    2222    void doWrite(const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
    2323    void setFlacMetadata(const FLAC__StreamMetadata *metadata);
    2424
    25     Metadata *getMetadata();
    26     void commitMetadata(Metadata *mdata);
     25    MetaIO *doCreateTagger();
    2726
    2827  private:
    2928    void run();
  • mythplugins/mythmusic/mythmusic/avfdecoder.cpp

     
    357357    deinit();
    358358}
    359359
    360 Metadata* avfDecoder::getMetadata()
     360MetaIO* avfDecoder::doCreateTagger()
    361361{
    362     Metadata *mdata = new Metadata(filename);
    363     if (mdata->isInDatabase(musiclocation))
    364     {
    365         return mdata;
    366     }
    367 
    368     delete mdata;
    369 
    370 
    371     MetaIOAVFComment* p_tagger = new MetaIOAVFComment;
    372     if (ignore_id3)
    373         mdata = p_tagger->readFromFilename(filename);
    374     else
    375         mdata = p_tagger->read(filename);
    376    
    377     delete p_tagger;
    378 
    379     if (mdata)
    380         mdata->dumpToDatabase(musiclocation);
    381     else
    382         cerr << "avfdecoder.o: Could not read metadata from " << filename << endl;
    383 
    384     return mdata;
     362    return new MetaIOAVFComment;
    385363}   
    386364
    387 void avfDecoder::commitMetadata(Metadata *mdata)
    388 {
    389     MetaIOAVFComment* p_tagger = new MetaIOAVFComment;
    390     p_tagger->write(mdata);
    391     delete p_tagger;
    392 }
    393 
    394365bool avfDecoderFactory::supports(const QString &source) const
    395366{
    396367    return (source.right(extension().length()).lower() == extension());
  • mythplugins/mythmusic/mythmusic/main.cpp

     
    6565    return decoder;
    6666}
    6767
    68 void CheckFile(const QString &filename)
     68void AddFileToDB(const QString &directory, const QString &filename)
    6969{
    7070    Decoder *decoder = getDecoder(filename);
    7171
    7272    if (decoder)
    7373    {
    7474        Metadata *data = decoder->getMetadata();
    75         if (data)
     75        if (data) {
     76            data->dumpToDatabase(directory);
    7677            delete data;
     78        }
    7779
    7880        delete decoder;
    7981    }
    8082}
    8183
     84// Remove a file from the database
     85void RemoveFileFromDB (const QString &directory, const QString &filename)
     86{
     87    QString name(filename);
     88    name.remove(0, directory.length());
     89    MSqlQuery query(MSqlQuery::InitCon());
     90    query.prepare("DELETE FROM musicmetadata WHERE "
     91                  "filename = :NAME ;");
     92    query.bindValue(":NAME", name.utf8());
     93    query.exec();
     94}
     95
    8296enum MusicFileLocation
    8397{
    8498    kFileSystem,
     
    215229    for (iter = music_files.begin(); iter != music_files.end(); iter++)
    216230    {
    217231        if (*iter == kFileSystem)
    218         {
    219             CheckFile(iter.key());
    220         }
     232            AddFileToDB(directory, iter.key());
    221233        else if (*iter == kDatabase)
    222         {
    223             QString name(iter.key());
    224             name.remove(0, directory.length());
     234            RemoveFileFromDB(directory, iter.key ());
    225235
    226             query.prepare("DELETE FROM musicmetadata WHERE "
    227                           "filename = :NAME ;");
    228             query.bindValue(":NAME", name.utf8());
    229             query.exec();
    230         }
    231 
    232236        file_checking->setProgress(++counter);
    233237    }
    234238    file_checking->Close();
  • mythplugins/mythmusic/mythmusic/flacdecoder.cpp

     
    385385        char *field_value;
    386386} Argument_VcField;
    387387
    388 Metadata *FlacDecoder::getMetadata()
     388MetaIO *FlacDecoder::doCreateTagger()
    389389{
    390     Metadata *mdata = new Metadata(filename);
    391     if (mdata->isInDatabase(musiclocation))
    392     {
    393         return mdata;
    394     }
     390    return new MetaIOFLACVorbisComment;
     391}
    395392
    396     delete mdata;
    397393
    398     MetaIOFLACVorbisComment* p_tagger = new MetaIOFLACVorbisComment;
    399     if (ignore_id3)
    400         mdata = p_tagger->readFromFilename(filename);
    401     else
    402         mdata = p_tagger->read(filename);
    403 
    404     delete p_tagger;
    405 
    406     if (mdata)
    407         mdata->dumpToDatabase(musiclocation);
    408     else
    409         cerr << "flacdecoder.o: Could not read metadata from " << filename.local8Bit() << endl;
    410 
    411     return mdata;
    412 }   
    413 
    414 void FlacDecoder::commitMetadata(Metadata *mdata)
    415 {
    416     MetaIOFLACVorbisComment* p_tagger = new MetaIOFLACVorbisComment;
    417     p_tagger->write(mdata);
    418     delete p_tagger;
    419     }
    420 
    421 
    422394bool FlacDecoderFactory::supports(const QString &source) const
    423395{
    424396    return (source.right(extension().length()).lower() == extension());
  • mythplugins/mythmusic/mythmusic/maddecoder.h

     
    2424    static const int maxFrameCheck;
    2525    static const int initialFrameSize;
    2626
    27     Metadata *getMetadata();
    28     void commitMetadata(Metadata *mdata);
     27    MetaIO *doCreateTagger();
    2928
    3029private:
    3130    void run();
  • mythplugins/mythmusic/mythmusic/vorbisdecoder.h

     
    1818    void seek(double);
    1919    void stop();
    2020
    21     Metadata *getMetadata();
    22     void commitMetadata(Metadata *mdata);
     21    MetaIO *doCreateTagger();
    2322
    2423  private:
    2524    void run();
  • mythplugins/mythmusic/mythmusic/avfdecoder.h

     
    1818    void seek(double);
    1919    void stop();
    2020
    21     Metadata *getMetadata();
    22     void commitMetadata(Metadata *mdata);
     21    MetaIO *doCreateTagger();
    2322
    2423  private:
    2524    void run();
  • mythplugins/mythmusic/mythmusic/maddecoder.cpp

     
    512512    return MAD_FLOW_STOP;
    513513}
    514514
    515 Metadata *MadDecoder::getMetadata()
     515MetaIO *MadDecoder::doCreateTagger()
    516516{
    517     Metadata *mdata = new Metadata(filename);
    518     if (mdata->isInDatabase(musiclocation))
    519     {
    520         return mdata;
    521     }
    522 
    523     delete mdata;
    524 
    525 
    526     MetaIOID3v2* p_tagger = new MetaIOID3v2;
    527     if (ignore_id3)
    528         mdata = p_tagger->readFromFilename(filename);
    529     else
    530         mdata = p_tagger->read(filename);
    531 
    532     delete p_tagger;
    533 
    534     if (mdata)
    535         mdata->dumpToDatabase(musiclocation);
    536     else
    537         cerr << "maddecoder.o: Could not read metadata from " << filename.local8Bit() << endl;
    538 
    539     return mdata;
     517    return new MetaIOID3v2;
    540518}
    541519
    542 void MadDecoder::commitMetadata(Metadata *mdata)
    543 {
    544     MetaIOID3v2* p_tagger = new MetaIOID3v2;
    545     p_tagger->write(mdata);
    546     delete p_tagger;
    547 }
    548 
    549520bool MadDecoderFactory::supports(const QString &source) const
    550521{
    551522    bool res = false;