Commit 64e7d463 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Add a SubtitleTrack entity

refs #39
parent 33b56357
......@@ -40,6 +40,7 @@ libmedialibrary_la_HEADERS = \
include/medialibrary/IFolder.h \
include/medialibrary/IQuery.h \
include/medialibrary/IMetadata.h \
include/medialibrary/ISubtitleTrack.h \
$(NULL)
libmedialibraryfsdir = $(libmedialibrary_ladir)/filesystem
......@@ -78,6 +79,7 @@ libmedialibrary_la_SOURCES = \
src/ShowEpisode.cpp \
src/VideoTrack.cpp \
src/Metadata.cpp \
src/SubtitleTrack.cpp \
src/database/SqliteConnection.cpp \
src/database/SqliteTools.cpp \
src/database/SqliteTransaction.cpp \
......@@ -164,6 +166,7 @@ noinst_HEADERS = \
src/compat/Thread.h \
src/compat/Mutex.h \
src/compat/ConditionVariable.h \
src/SubtitleTrack.h \
$(NULL)
......@@ -300,6 +303,7 @@ unittest_SOURCES = \
test/unittest/VideoTrackTests.cpp \
test/unittest/MiscTests.cpp \
test/unittest/ThumbnailTests.cpp \
test/unittest/SubtitleTrackTests.cpp \
$(NULL)
EXTRA_DIST += test/unittest/db_v3.sql
......
......@@ -156,6 +156,7 @@ class IMedia
virtual Query<ILabel> labels() const = 0;
virtual Query<IVideoTrack> videoTracks() const = 0;
virtual Query<IAudioTrack> audioTracks() const = 0;
virtual Query<ISubtitleTrack> subtitleTracks() const = 0;
///
/// \brief thumbnail Returns the path of a thumbnail for this media
/// \return A path, relative to the thumbnailPath configured when initializing
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 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.
*****************************************************************************/
#pragma once
#include <string>
namespace medialibrary
{
class ISubtitleTrack
{
public:
virtual ~ISubtitleTrack() = default;
virtual int64_t id() const = 0;
virtual const std::string& codec() const = 0;
virtual const std::string& language() const = 0;
virtual const std::string& description() const = 0;
virtual const std::string& encoding() const = 0;
};
}
......@@ -46,6 +46,7 @@ class IMediaLibraryCb;
class IDeviceLister;
class IDeviceListerCb;
class IFolder;
class ISubtitleTrack;
namespace parser
{
......@@ -72,6 +73,7 @@ using ShowPtr = std::shared_ptr<IShow>;
using VideoTrackPtr = std::shared_ptr<IVideoTrack>;
using DeviceListerPtr = std::shared_ptr<IDeviceLister>;
using FolderPtr = std::shared_ptr<IFolder>;
using SubtitleTrackPtr = std::shared_ptr<ISubtitleTrack>;
}
......@@ -42,6 +42,7 @@
#include "logging/Logger.h"
#include "Movie.h"
#include "ShowEpisode.h"
#include "SubtitleTrack.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
......@@ -289,6 +290,14 @@ bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
return AudioTrack::create( m_ml, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
}
bool Media::addSubtitleTrack( std::string codec, std::string language,
std::string description, std::string encoding )
{
return SubtitleTrack::create( m_ml, std::move( codec ), std::move( language ),
std::move( description ), std::move( encoding ),
m_id ) != nullptr;
}
Query<IAudioTrack> Media::audioTracks() const
{
static const std::string req = "FROM " + AudioTrack::Table::Name +
......@@ -296,6 +305,13 @@ Query<IAudioTrack> Media::audioTracks() const
return make_query<AudioTrack, IAudioTrack>( m_ml, "*", req, "", m_id );
}
Query<ISubtitleTrack> Media::subtitleTracks() const
{
static const std::string req = "FROM " + SubtitleTrack::Table::Name +
" WHERE media_id = ?";
return make_query<SubtitleTrack, ISubtitleTrack>( m_ml, "*", req, "", m_id );
}
const std::string& Media::thumbnail() const
{
if ( m_thumbnailId == 0 || m_thumbnailGenerated == false )
......
......@@ -110,7 +110,10 @@ class Media : public IMedia, public DatabaseHelpers<Media>
virtual Query<IVideoTrack> videoTracks() const override;
bool addAudioTrack( const std::string& codec, unsigned int bitrate, unsigned int sampleRate,
unsigned int nbChannels, const std::string& language, const std::string& desc );
bool addSubtitleTrack( std::string codec, std::string language,
std::string description, std::string encoding );
virtual Query<IAudioTrack> audioTracks() const override;
virtual Query<ISubtitleTrack> subtitleTracks() const override;
virtual const std::string& thumbnail() const override;
virtual bool isThumbnailGenerated() const override;
virtual bool setThumbnail( const std::string &thumbnail ) override;
......
......@@ -50,6 +50,7 @@
#include "Playlist.h"
#include "Show.h"
#include "ShowEpisode.h"
#include "SubtitleTrack.h"
#include "Thumbnail.h"
#include "database/SqliteTools.h"
#include "database/SqliteConnection.h"
......@@ -141,6 +142,7 @@ void MediaLibrary::clearCache()
Playlist::clear();
Genre::clear();
Thumbnail::clear();
SubtitleTrack::clear();
}
void MediaLibrary::createAllTables()
......@@ -171,6 +173,7 @@ void MediaLibrary::createAllTables()
Settings::createTable( m_dbConnection.get() );
parser::Task::createTable( m_dbConnection.get() );
Metadata::createTable( m_dbConnection.get() );
SubtitleTrack::createTable( m_dbConnection.get() );
}
void MediaLibrary::createAllTriggers()
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 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.
*****************************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "SubtitleTrack.h"
#include "Media.h"
namespace medialibrary
{
const std::string SubtitleTrack::Table::Name = "SubtitleTrack";
const std::string SubtitleTrack::Table::PrimaryKeyColumn = "id_track";
int64_t SubtitleTrack::* const SubtitleTrack::Table::PrimaryKey = &SubtitleTrack::m_id;
SubtitleTrack::SubtitleTrack( MediaLibraryPtr, sqlite::Row& row )
: m_id( row.extract<decltype(m_id)>() )
, m_codec( row.extract<decltype(m_codec)>() )
, m_language( row.extract<decltype(m_language)>() )
, m_description( row.extract<decltype(m_description)>() )
, m_encoding( row.extract<decltype(m_encoding)>() )
{
}
SubtitleTrack::SubtitleTrack( MediaLibraryPtr, std::string codec,
std::string language, std::string description,
std::string encoding )
: m_id( 0 )
, m_codec( std::move( codec ) )
, m_language( std::move( language ) )
, m_description( std::move( description ) )
, m_encoding( std::move( encoding ) )
{
}
int64_t SubtitleTrack::id() const
{
return m_id;
}
const std::string&SubtitleTrack::codec() const
{
return m_codec;
}
const std::string&SubtitleTrack::language() const
{
return m_language;
}
const std::string&SubtitleTrack::description() const
{
return m_description;
}
const std::string&SubtitleTrack::encoding() const
{
return m_encoding;
}
void SubtitleTrack::createTable( sqlite::Connection* dbConnection )
{
const std::string req = "CREATE TABLE IF NOT EXISTS " + Table::Name +
"(" +
Table::PrimaryKeyColumn + " INTEGER PRIMARY KEY AUTOINCREMENT,"
"codec TEXT,"
"language TEXT,"
"description TEXT,"
"encoding TEXT,"
"media_id UNSIGNED INT,"
"FOREIGN KEY( media_id ) REFERENCES " + Media::Table::Name +
"(id_media) ON DELETE CASCADE"
")";
const std::string indexReq = "CREATE INDEX IF NOT EXISTS subtitle_track_media_idx "
" ON " + SubtitleTrack::Table::Name + "(media_id)";
sqlite::Tools::executeRequest( dbConnection, req );
sqlite::Tools::executeRequest( dbConnection, indexReq );
}
std::shared_ptr<SubtitleTrack> SubtitleTrack::create( MediaLibraryPtr ml,
std::string codec, std::string language, std::string description,
std::string encoding, int64_t mediaId )
{
const std::string req = "INSERT INTO " + Table::Name + "(codec, language,"
"description, encoding, media_id) VALUES(?, ?, ?, ?, ?)";
auto track = std::make_shared<SubtitleTrack>( ml, std::move( codec ),
std::move( language ), std::move( description ),
std::move( encoding ) );
if ( insert( ml, track, req, track->codec(), track->language(),
track->description(), track->encoding(), mediaId ) == false )
return nullptr;
return track;
}
}
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 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.
*****************************************************************************/
#pragma once
#include "medialibrary/ISubtitleTrack.h"
#include "database/DatabaseHelpers.h"
namespace medialibrary
{
class SubtitleTrack : public ISubtitleTrack, public DatabaseHelpers<SubtitleTrack>
{
public:
struct Table
{
static const std::string Name;
static const std::string PrimaryKeyColumn;
static int64_t SubtitleTrack::* const PrimaryKey;
};
SubtitleTrack( MediaLibraryPtr ml, sqlite::Row& row );
SubtitleTrack( MediaLibraryPtr ml, std::string codec, std::string language,
std::string description, std::string encoding );
virtual int64_t id() const override;
virtual const std::string& codec() const override;
virtual const std::string& language() const override;
virtual const std::string& description() const override;
virtual const std::string& encoding() const override;
static void createTable( sqlite::Connection* dbConnection );
static std::shared_ptr<SubtitleTrack> create( MediaLibraryPtr ml,
std::string codec, std::string language, std::string description,
std::string encoding, int64_t mediaId );
private:
int64_t m_id;
std::string m_codec;
std::string m_language;
std::string m_description;
std::string m_encoding;
};
}
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 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.
*****************************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "Tests.h"
#include "SubtitleTrack.h"
#include "Media.h"
class SubtitleTracks : public Tests
{
};
TEST_F( SubtitleTracks, AddTrack )
{
auto media = std::static_pointer_cast<Media>( ml->addMedia( "media.mkv" ) );
auto res = media->addSubtitleTrack( "sea", "otter", "awareness", "week" );
ASSERT_TRUE( res );
}
TEST_F( SubtitleTracks, FetchTracks )
{
auto media = std::static_pointer_cast<Media>( ml->addMedia( "media.mkv" ) );
media->addSubtitleTrack( "sea", "otter", "awareness", "week" );
media->addSubtitleTrack( "best", "time", "of", "year" );
auto tracks = media->subtitleTracks()->all();
ASSERT_EQ( 2u, tracks.size() );
ASSERT_EQ( "sea", tracks[0]->codec() );
ASSERT_EQ( "otter", tracks[0]->language() );
ASSERT_EQ( "awareness", tracks[0]->description() );
ASSERT_EQ( "week", tracks[0]->encoding() );
ASSERT_EQ( "best", tracks[1]->codec() );
ASSERT_EQ( "time", tracks[1]->language() );
ASSERT_EQ( "of", tracks[1]->description() );
ASSERT_EQ( "year", tracks[1]->encoding() );
Reload();
media = ml->media( media->id() );
tracks = media->subtitleTracks()->all();
ASSERT_EQ( 2u, tracks.size() );
ASSERT_EQ( "sea", tracks[0]->codec() );
ASSERT_EQ( "otter", tracks[0]->language() );
ASSERT_EQ( "awareness", tracks[0]->description() );
ASSERT_EQ( "week", tracks[0]->encoding() );
ASSERT_EQ( "best", tracks[1]->codec() );
ASSERT_EQ( "time", tracks[1]->language() );
ASSERT_EQ( "of", tracks[1]->description() );
ASSERT_EQ( "year", tracks[1]->encoding() );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment