AudioTrack.cpp 4.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24 25 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27 28
#include "AudioTrack.h"

29 30
#include "Media.h"

31 32 33 34

namespace medialibrary
{

35
const std::string policy::AudioTrackTable::Name = "AudioTrack";
36
const std::string policy::AudioTrackTable::PrimaryKeyColumn  = "id_track";
37
int64_t AudioTrack::* const policy::AudioTrackTable::PrimaryKey = &AudioTrack::m_id;
38

39
AudioTrack::AudioTrack( MediaLibraryPtr, sqlite::Row& row )
40
{
41 42 43 44
    row >> m_id
        >> m_codec
        >> m_bitrate
        >> m_sampleRate
45 46
        >> m_nbChannels
        >> m_language
47 48
        >> m_description
        >> m_mediaId;
49 50
}

51
AudioTrack::AudioTrack( MediaLibraryPtr, const std::string& codec, unsigned int bitrate, unsigned int sampleRate,
52
                        unsigned int nbChannels, const std::string& language, const std::string& desc,
53
                        int64_t mediaId )
54
    : m_id( 0 )
55 56
    , m_codec( codec )
    , m_bitrate( bitrate )
57 58
    , m_sampleRate( sampleRate )
    , m_nbChannels( nbChannels )
59 60
    , m_language( language )
    , m_description( desc )
61
    , m_mediaId( mediaId )
62 63 64
{
}

65
int64_t AudioTrack::id() const
66 67 68 69 70 71 72 73 74 75 76 77 78 79
{
    return m_id;
}

const std::string&AudioTrack::codec() const
{
    return m_codec;
}

unsigned int AudioTrack::bitrate() const
{
    return m_bitrate;
}

80 81 82 83 84 85 86 87 88 89
unsigned int AudioTrack::sampleRate() const
{
    return m_sampleRate;
}

unsigned int AudioTrack::nbChannels() const
{
    return m_nbChannels;
}

90 91 92 93 94 95 96 97 98 99
const std::string& AudioTrack::language() const
{
    return m_language;
}

const std::string& AudioTrack::description() const
{
    return m_description;
}

100
void AudioTrack::createTable( sqlite::Connection* dbConnection )
101
{
102
    //FIXME: Index on media_id ? Unless it's already implied by the foreign key
103
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AudioTrackTable::Name
104
            + "(" +
105
                policy::AudioTrackTable::PrimaryKeyColumn + " INTEGER PRIMARY KEY AUTOINCREMENT,"
106 107
                "codec TEXT,"
                "bitrate UNSIGNED INTEGER,"
108 109
                "samplerate UNSIGNED INTEGER,"
                "nb_channels UNSIGNED INTEGER,"
110 111
                "language TEXT,"
                "description TEXT,"
112 113 114
                "media_id UNSIGNED INT,"
                "FOREIGN KEY ( media_id ) REFERENCES " + policy::MediaTable::Name
                    + "( id_media ) ON DELETE CASCADE"
115
            ")";
116
    const std::string indexReq = "CREATE INDEX IF NOT EXISTS audio_track_media_idx ON " +
117
            policy::AudioTrackTable::Name + "(media_id)";
118 119
    sqlite::Tools::executeRequest( dbConnection, req );
    sqlite::Tools::executeRequest( dbConnection, indexReq );
120 121
}

122 123
std::shared_ptr<AudioTrack> AudioTrack::create( MediaLibraryPtr ml, const std::string& codec,
                                                unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels,
124
                                                const std::string& language, const std::string& desc, int64_t mediaId )
125 126
{
    static const std::string req = "INSERT INTO " + policy::AudioTrackTable::Name
127
            + "(codec, bitrate, samplerate, nb_channels, language, description, media_id) VALUES(?, ?, ?, ?, ?, ?, ?)";
128
    auto track = std::make_shared<AudioTrack>( ml, codec, bitrate, sampleRate, nbChannels, language, desc, mediaId );
129
    if ( insert( ml, track, req, codec, bitrate, sampleRate, nbChannels, language, desc, mediaId ) == false )
130 131 132
        return nullptr;
    return track;
}
133 134

}