Media.cpp 13 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
#include <cassert>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
24 25
#include <cstdlib>
#include <cstring>
26
#include <ctime>
27

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
28
#include "Album.h"
29
#include "AlbumTrack.h"
30
#include "AudioTrack.h"
31
#include "Media.h"
32
#include "Folder.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
33
#include "Label.h"
34
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
35
#include "Movie.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36
#include "ShowEpisode.h"
37
#include "database/SqliteTools.h"
38
#include "VideoTrack.h"
39
#include "filesystem/IFile.h"
40

41 42 43
const std::string policy::MediaTable::Name = "Media";
const std::string policy::MediaTable::CacheColumn = "mrl";
unsigned int Media::* const policy::MediaTable::PrimaryKey = &Media::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44

45
Media::Media( DBConnection dbConnection, sqlite3_stmt* stmt )
46 47 48 49
    : m_dbConnection( dbConnection )
{
    m_id = sqlite3_column_int( stmt, 0 );
    m_type = (Type)sqlite3_column_int( stmt, 1 );
50
    m_duration = sqlite::Traits<int64_t>::Load( stmt, 2 );
51
    m_albumTrackId = sqlite3_column_int( stmt, 3 );
52
    m_playCount = sqlite3_column_int( stmt, 4 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
53
    m_showEpisodeId = sqlite3_column_int( stmt, 5 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
54
    m_mrl = (const char*)sqlite3_column_text( stmt, 6 );
55 56
    m_movieId = sqlite::Traits<unsigned int>::Load( stmt, 7 );
    m_folderId = sqlite::Traits<unsigned int>::Load( stmt, 8 );
57
    m_lastModificationDate = sqlite::Traits<unsigned int>::Load( stmt, 9 );
58
    m_snapshot = sqlite::Traits<std::string>::Load( stmt, 10 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59
    m_isParsed = sqlite::Traits<bool>::Load( stmt, 11 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60
    m_name = sqlite::Traits<std::string>::Load( stmt, 12 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
61 62
}

63
Media::Media( const fs::IFile* file, unsigned int folderId, const std::string& name, Type type )
64
    : m_id( 0 )
65
    , m_type( type )
66
    , m_duration( -1 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67 68 69
    , m_albumTrackId( 0 )
    , m_playCount( 0 )
    , m_showEpisodeId( 0 )
70
    , m_mrl( file->fullPath() )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
71
    , m_movieId( 0 )
72
    , m_folderId( folderId )
73
    , m_lastModificationDate( file->lastModificationDate() )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74
    , m_isParsed( false )
75
    , m_name( name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
76
{
77 78
}

79
std::shared_ptr<Media> Media::create( DBConnection dbConnection, Type type, const fs::IFile* file, unsigned int folderId )
80
{
81 82
    auto self = std::make_shared<Media>( file, folderId, file->name(), type );
    static const std::string req = "INSERT INTO " + policy::MediaTable::Name +
83
            "(type, mrl, folder_id, last_modification_date, name) VALUES(?, ?, ?, ?, ?)";
84

85 86
    using type_t = std::underlying_type<Type>::type;
    if ( _Cache::insert( dbConnection, self, req, static_cast<type_t>( type ), self->m_mrl, sqlite::ForeignKey( folderId ),
87
                         self->m_lastModificationDate, self->m_name ) == false )
88
        return nullptr;
89 90
    self->m_dbConnection = dbConnection;
    return self;
91 92
}

93
AlbumTrackPtr Media::albumTrack()
94
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
95
    if ( m_albumTrack == nullptr && m_albumTrackId != 0 )
96
    {
97
        m_albumTrack = AlbumTrack::fetch( m_dbConnection, m_albumTrackId );
98 99
    }
    return m_albumTrack;
100 101
}

102
bool Media::setAlbumTrack( AlbumTrackPtr albumTrack )
103
{
104 105
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET album_track_id = ? "
            "WHERE id_media = ?";
106
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, albumTrack->id(), m_id ) == false )
107 108 109 110 111 112
        return false;
    m_albumTrackId = albumTrack->id();
    m_albumTrack = albumTrack;
    return true;
}

113
int64_t Media::duration() const
114 115 116 117
{
    return m_duration;
}

118
bool Media::setDuration( int64_t duration )
119
{
120 121
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET duration = ? "
            "WHERE id_media = ?";
122 123 124 125 126 127
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, duration, m_id ) == false )
        return false;
    m_duration = duration;
    return true;
}

128
std::shared_ptr<IShowEpisode> Media::showEpisode()
129
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
130
    if ( m_showEpisode == nullptr && m_showEpisodeId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
131
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
132
        m_showEpisode = ShowEpisode::fetch( m_dbConnection, m_showEpisodeId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
133 134
    }
    return m_showEpisode;
135 136
}

137
bool Media::setShowEpisode(ShowEpisodePtr showEpisode)
138
{
139 140
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET show_episode_id = ?  WHERE id_media = ?";
141
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, showEpisode->id(), m_id ) == false )
142 143 144 145 146 147
        return false;
    m_showEpisodeId = showEpisode->id();
    m_showEpisode = showEpisode;
    return true;
}

148
std::vector<std::shared_ptr<ILabel> > Media::labels()
149
{
150
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
151
            "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
152
            "WHERE lfr.id_media = ?";
153
    return Label::fetchAll( m_dbConnection, req, m_id );
154 155
}

156
int Media::playCount() const
157
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
158
    return m_playCount;
159 160
}

161
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
162 163 164 165
{
    return m_mrl;
}

166
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
167 168 169 170 171 172 173 174
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

175
bool Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
176
{
177 178
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET movie_id = ? WHERE id_media = ?";
179
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, movie->id(), m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
180 181 182 183 184 185
        return false;
    m_movie = movie;
    m_movieId = movie->id();
    return true;
}

186
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
187 188 189 190 191 192 193 194 195 196
{
    static const std::string req = "INSERT INTO VideoTrackFileRelation VALUES(?, ?)";

    auto track = VideoTrack::fetch( m_dbConnection, codec, width, height, fps );
    if ( track == nullptr )
    {
        track = VideoTrack::create( m_dbConnection, codec, width, height, fps );
        if ( track == nullptr )
            return false;
    }
197
    return sqlite::Tools::executeRequest( m_dbConnection, req, track->id(), m_id );
198 199
}

200
std::vector<VideoTrackPtr> Media::videoTracks()
201 202 203
{
    static const std::string req = "SELECT t.* FROM " + policy::VideoTrackTable::Name +
            " t LEFT JOIN VideoTrackFileRelation vtfr ON vtfr.id_track = t.id_track"
204
            " WHERE vtfr.id_media = ?";
205
    return VideoTrack::fetchAll( m_dbConnection, req, m_id );
206 207
}

208
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
209
                          unsigned int sampleRate, unsigned int nbChannels )
210 211 212
{
    static const std::string req = "INSERT INTO AudioTrackFileRelation VALUES(?, ?)";

213
    auto track = AudioTrack::fetch( m_dbConnection, codec, bitrate, sampleRate, nbChannels );
214 215
    if ( track == nullptr )
    {
216
        track = AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels );
217 218 219
        if ( track == nullptr )
            return false;
    }
220
    return sqlite::Tools::executeRequest( m_dbConnection, req, track->id(), m_id );
221 222
}

223
std::vector<AudioTrackPtr> Media::audioTracks()
224 225 226
{
    static const std::string req = "SELECT t.* FROM " + policy::AudioTrackTable::Name +
            " t LEFT JOIN AudioTrackFileRelation atfr ON atfr.id_track = t.id_track"
227
            " WHERE atfr.id_media = ?";
228
    return AudioTrack::fetchAll( m_dbConnection, req, m_id );
229 230
}

231
const std::string &Media::snapshot()
232 233 234 235
{
    return m_snapshot;
}

236
bool Media::setSnapshot(const std::string &snapshot)
237
{
238 239
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET snapshot = ? WHERE id_media = ?";
240 241 242 243 244 245
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, snapshot, m_id ) == false )
        return false;
    m_snapshot = snapshot;
    return true;
}

246
bool Media::isStandAlone()
247 248 249 250
{
    return m_folderId == 0;
}

251
unsigned int Media::lastModificationDate()
252 253 254 255
{
    return m_lastModificationDate;
}

256
bool Media::isParsed() const
257
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
258
    return m_isParsed;
259 260
}

261
bool Media::markParsed()
262
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
263 264
    if ( m_isParsed == true )
        return true;
265 266
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET parsed = ? WHERE id_media = ?";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
267 268 269 270
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, true, m_id ) == false )
        return false;
    m_isParsed = true;
    return true;
271 272
}

273
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
274 275 276 277
{
    return m_id;
}

278
IMedia::Type Media::type()
279 280 281 282
{
    return m_type;
}

283
bool Media::setType( Type type )
284
{
285 286
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET type = ? WHERE id_media = ?";
287 288 289 290 291 292 293 294
    // We need to convert to an integer representation for the sqlite traits to work properly
    using type_t = std::underlying_type<Type>::type;
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, static_cast<type_t>( type ), m_id ) == false )
        return false;
    m_type = type;
    return true;
}

295
const std::string &Media::name()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
296 297 298 299
{
    return m_name;
}

300
bool Media::setName( const std::string &name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
301
{
302 303
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET name = ? WHERE id_media = ?";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
304 305 306 307 308 309
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, name, m_id ) == false )
        return false;
    m_name = name;
    return true;
}

310
bool Media::createTable( DBConnection connection )
311
{
312 313
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MediaTable::Name + "("
            "id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
314
            "type INTEGER,"
315
            "duration INTEGER,"
316
            "album_track_id UNSIGNED INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
317 318
            "play_count UNSIGNED INTEGER,"
            "show_episode_id UNSIGNED INTEGER,"
319
            "mrl TEXT UNIQUE ON CONFLICT FAIL,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
320
            "movie_id UNSIGNED INTEGER,"
321
            "folder_id UNSIGNED INTEGER,"
322
            "last_modification_date UNSIGNED INTEGER,"
323
            "snapshot TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
324
            "parsed BOOLEAN,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
325
            "name TEXT,"
326
            "FOREIGN KEY (album_track_id) REFERENCES " + policy::AlbumTrackTable::Name
327 328
            + "(id_track) ON DELETE CASCADE,"
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
329 330
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
331 332 333
            + "(id_movie) ON DELETE CASCADE,"
            "FOREIGN KEY (folder_id) REFERENCES " + policy::FolderTable::Name
            + "(id_folder) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
334
            ")";
335
    if ( sqlite::Tools::executeRequest( connection, req ) == false )
336 337 338
        return false;
    req = "CREATE TABLE IF NOT EXISTS VideoTrackFileRelation("
                "id_track INTEGER,"
339 340
                "id_media INTEGER,"
                "PRIMARY KEY ( id_track, id_media ), "
341 342
                "FOREIGN KEY ( id_track ) REFERENCES " + policy::VideoTrackTable::Name +
                    "(id_track) ON DELETE CASCADE,"
343 344
                "FOREIGN KEY ( id_media ) REFERENCES " + policy::MediaTable::Name
                + "(id_media) ON DELETE CASCADE"
345
            ")";
346
    if ( sqlite::Tools::executeRequest( connection, req ) == false )
347 348 349
        return false;
    req = "CREATE TABLE IF NOT EXISTS AudioTrackFileRelation("
                "id_track INTEGER,"
350 351
                "id_media INTEGER,"
                "PRIMARY KEY ( id_track, id_media ), "
352 353
                "FOREIGN KEY ( id_track ) REFERENCES " + policy::AudioTrackTable::Name +
                    "(id_track) ON DELETE CASCADE,"
354 355
                "FOREIGN KEY ( id_media ) REFERENCES " + policy::MediaTable::Name
                + "(id_media) ON DELETE CASCADE"
356
            ")";
357
    return sqlite::Tools::executeRequest( connection, req );
358
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
359

360
bool Media::addLabel( LabelPtr label )
361
{
362
    if ( m_id == 0 || label->id() == 0 )
363
    {
364
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
365 366 367
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
368
    return sqlite::Tools::executeRequest( m_dbConnection, req, label->id(), m_id );
369 370
}

371
bool Media::removeLabel( LabelPtr label )
372
{
373
    if ( m_id == 0 || label->id() == 0 )
374
    {
375
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
376 377
        return false;
    }
378
    const char* req = "DELETE FROM LabelFileRelation WHERE id_label = ? AND id_media = ?";
379
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
380
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
381

382
const std::string& policy::MediaCache::key(const std::shared_ptr<Media> self )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
383 384 385 386
{
    return self->mrl();
}

387
std::string policy::MediaCache::key(sqlite3_stmt* stmt)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
388
{
389
    return sqlite::Traits<std::string>::Load( stmt, 6 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
390
}