VLCMetadataService.cpp 6.92 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 <chrono>

29
#include "VLCMetadataService.h"
30
#include "Media.h"
31
#include "utils/VLCInstance.h"
32
#include "metadata_services/vlc/Common.hpp"
33
#include "utils/Filename.h"
34

35
36
37
namespace medialibrary
{

38
VLCMetadataService::VLCMetadataService()
39
40
    : m_ml( nullptr )
    , m_instance( VLCInstance::get() )
41
42
43
{
}

44
45
46
47
48
49
bool VLCMetadataService::initialize( MediaLibrary* ml )
{
    m_ml = ml;
    return true;
}

50
parser::Task::Status VLCMetadataService::run( parser::Task& task )
51
{
52
    auto mrl = task.item().mrl();
53
    LOG_INFO( "Parsing ", mrl );
54

55
56
    // Having a valid media means we're re-executing this parser after the thumbnailer,
    // which isn't expected, as we always mark this task as completed.
57
    VLC::Media vlcMedia{ m_instance, mrl, VLC::Media::FromType::FromLocation };
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
58

59
    VLC::Media::ParsedStatus status;
60
    bool done = false;
61

62
    auto event = vlcMedia.eventManager().onParsedChanged( [this, &status, &done](VLC::Media::ParsedStatus s ) {
63
        std::lock_guard<compat::Mutex> lock( m_mutex );
64
        status = s;
65
        done = true;
66
        m_cond.notify_all();
67
    });
68
69
70
    {
        std::unique_lock<compat::Mutex> lock( m_mutex );

71
        if ( vlcMedia.parseWithOptions( VLC::Media::ParseFlags::Local | VLC::Media::ParseFlags::Network |
72
73
74
75
76
77
                                             VLC::Media::ParseFlags::FetchLocal, 5000 ) == false )
            return parser::Task::Status::Fatal;
        m_cond.wait( lock, [&status, &done]() {
            return done == true;
        });
    }
78
    event->unregister();
79
    if ( status == VLC::Media::ParsedStatus::Failed || status == VLC::Media::ParsedStatus::Timeout )
80
        return parser::Task::Status::Fatal;
81
82
83
    auto tracks = vlcMedia.tracks();
    auto artworkMrl = vlcMedia.meta( libvlc_meta_ArtworkURL );
    if ( ( tracks.size() == 0 && vlcMedia.subitems()->count() == 0 ) ||
84
         utils::file::schemeIs( "attachment://", artworkMrl ) == true )
85
    {
86
        if ( tracks.size() == 0 && vlcMedia.subitems()->count() == 0 )
87
            LOG_WARN( "Failed to fetch any tracks for ", mrl, ". Falling back to playback" );
88
89
        VLC::MediaPlayer mp( vlcMedia );
        auto res = MetadataCommon::startPlayback( vlcMedia, mp );
90
91
        if ( res == false )
            return parser::Task::Status::Fatal;
92
    }
93
    mediaToItem( vlcMedia, task.item() );
94
95
    // Don't save the file parsing step yet, since all data are just in memory. Just mark
    // the extraction as done.
96
    task.markStepCompleted( parser::Task::ParserStep::MetadataExtraction );
97
    return parser::Task::Status::Success;
98
99
}

100
101
102
103
104
const char* VLCMetadataService::name() const
{
    return "VLC";
}

105
106
107
108
109
uint8_t VLCMetadataService::nbThreads() const
{
    return 1;
}

110
bool VLCMetadataService::isCompleted( const parser::Task& ) const
111
{
112
    // We always need to run this task if the metadata extraction isn't completed
113
    return false;
114
115
}

116
117
118
119
120
121
122
123
void VLCMetadataService::onFlushing()
{
}

void VLCMetadataService::onRestarted()
{
}

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
void VLCMetadataService::mediaToItem( VLC::Media& media, parser::Task::Item& item )
{
    item.setMeta( parser::Task::Item::Metadata::Title,
                  media.meta( libvlc_meta_Title ) );
    item.setMeta( parser::Task::Item::Metadata::ArtworkUrl,
                  media.meta( libvlc_meta_ArtworkURL ) );
    item.setMeta( parser::Task::Item::Metadata::ShowName,
                  media.meta( libvlc_meta_ShowName ) );
    item.setMeta( parser::Task::Item::Metadata::Episode,
                  media.meta( libvlc_meta_Episode ) );
    item.setMeta( parser::Task::Item::Metadata::Album,
                  media.meta( libvlc_meta_Album ) );
    item.setMeta( parser::Task::Item::Metadata::Genre,
                  media.meta( libvlc_meta_Genre ) );
    item.setMeta( parser::Task::Item::Metadata::Date,
                  media.meta( libvlc_meta_Date ) );
    item.setMeta( parser::Task::Item::Metadata::AlbumArtist,
                  media.meta( libvlc_meta_AlbumArtist ) );
    item.setMeta( parser::Task::Item::Metadata::Artist,
                  media.meta( libvlc_meta_Artist ) );
    item.setMeta( parser::Task::Item::Metadata::TrackNumber,
                  media.meta( libvlc_meta_TrackNumber ) );
    item.setMeta( parser::Task::Item::Metadata::DiscNumber,
                  media.meta( libvlc_meta_DiscNumber ) );
    item.setMeta( parser::Task::Item::Metadata::DiscTotal,
                  media.meta( libvlc_meta_DiscTotal ) );
150
    item.setDuration( media.duration() );
151

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    auto tracks = media.tracks();
    for ( const auto& track : tracks )
    {
        parser::Task::Item::Track t;

        if ( track.type() == VLC::MediaTrack::Type::Audio )
        {
            t.type = parser::Task::Item::Track::Type::Audio;
            t.a.nbChannels = track.channels();
            t.a.rate = track.rate();
        }
        else if ( track.type() == VLC::MediaTrack::Type::Video )
        {
            t.type = parser::Task::Item::Track::Type::Video;
            t.v.fpsNum = track.fpsNum();
            t.v.fpsDen = track.fpsDen();
            t.v.width = track.width();
            t.v.height = track.height();
            t.v.sarNum = track.sarNum();
            t.v.sarDen = track.sarDen();
        }
        else
            continue;
        auto codec = track.codec();
        std::string fcc( reinterpret_cast<const char*>( &codec ), 4 );
        t.codec = std::move( fcc );

        t.bitrate = track.bitrate();
        t.language = track.language();
        t.description = track.description();

        item.addTrack( std::move( t ) );
    }

186
187
188
189
190
191
192
193
194
195
196
197
198
199
    auto subItems = media.subitems();
    if ( subItems != nullptr )
    {
        for ( auto i = 0; i < subItems->count(); ++i )
        {
            auto vlcMedia = subItems->itemAtIndex( i );
            assert( vlcMedia != nullptr );
            parser::Task::Item subItem{ vlcMedia->mrl() };
            mediaToItem( *vlcMedia, subItem );
            item.addSubItem( std::move( subItem ) );
        }
    }
}

200
}