VLCMetadataService.cpp 5.15 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 <chrono>
24
25
26
#ifdef __ANDROID__
# include <cstdlib>
#endif
27

28
#include "VLCMetadataService.h"
29
#include "Media.h"
30
#include "utils/VLCInstance.h"
31

32
33
VLCMetadataService::VLCMetadataService()
    : m_instance( VLCInstance::get() )
34
35
36
{
}

37
parser::Task::Status VLCMetadataService::run( parser::Task& task )
38
{
39
40
    auto media = task.media;
    auto file = task.file;
41
    // FIXME: This is now becomming an invalid predicate
42
    if ( media->duration() != -1 )
43
    {
44
        LOG_INFO( file->mrl(), " was already parsed" );
45
        return parser::Task::Status::Success;
46
    }
47

48
    LOG_INFO( "Parsing ", file->mrl() );
49
    auto chrono = std::chrono::steady_clock::now();
50

51
    auto vlcMedia = VLC::Media( m_instance, file->mrl(), VLC::Media::FromPath );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
52

53
    std::unique_lock<std::mutex> lock( m_mutex );
54
    VLC::Media::ParsedStatus status;
55
    bool done = false;
56

57
    auto event = vlcMedia.eventManager().onParsedChanged( [this, &status, &done](VLC::Media::ParsedStatus s ) {
58
        std::lock_guard<std::mutex> lock( m_mutex );
59
        status = s;
60
        done = true;
61
        m_cond.notify_all();
62
    });
63
    if ( vlcMedia.parseWithOptions( VLC::Media::ParseFlags::Local, 5000 ) == false )
64
        return parser::Task::Status::Fatal;
65
66
    m_cond.wait( lock, [&status, &done]() {
        return done == true;
67
    });
68
    event->unregister();
69
    if ( status == VLC::Media::ParsedStatus::Failed || status == VLC::Media::ParsedStatus::Timeout )
70
        return parser::Task::Status::Fatal;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    auto tracks = vlcMedia.tracks();
    if ( tracks.size() == 0 )
    {
        LOG_ERROR( "Failed to fetch any tracks" );
        return parser::Task::Status::Fatal;
    }
    for ( const auto& track : tracks )
    {
        auto codec = track.codec();
        std::string fcc( (const char*)&codec, 4 );
        if ( track.type() == VLC::MediaTrack::Video )
        {
            auto fps = (float)track.fpsNum() / (float)track.fpsDen();
            task.videoTracks.emplace_back( fcc, fps, track.width(), track.height() );
        }
        else if ( track.type() == VLC::MediaTrack::Audio )
        {
            task.audioTracks.emplace_back( fcc, track.bitrate(), track.rate(), track.channels(),
                                            track.language(), track.description() );
        }
    }
    storeMeta( task, vlcMedia );
93
    auto duration = std::chrono::steady_clock::now() - chrono;
94
95
    LOG_DEBUG("VLC parsing done in ", std::chrono::duration_cast<std::chrono::microseconds>( duration ).count(), "µs" );
    return parser::Task::Status::Success;
96
97
}

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

103
104
105
106
107
uint8_t VLCMetadataService::nbThreads() const
{
    return 1;
}

108
109
void VLCMetadataService::storeMeta( parser::Task& task, VLC::Media& vlcMedia )
{
110
#if LIBVLC_VERSION_INT >= LIBVLC_VERSION(3, 0, 0, 0)
111
    task.albumArtist = vlcMedia.meta( libvlc_meta_AlbumArtist );
112
113
114
115
116
117
    task.discNumber = toInt( vlcMedia, libvlc_meta_DiscNumber, "disc number" );
    task.discTotal = toInt( vlcMedia, libvlc_meta_DiscTotal, "disc total" );
#else
    task.discNumber = 0;
    task.discTotal = 0;
#endif
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    task.artist = vlcMedia.meta( libvlc_meta_Artist );
    task.artworkMrl = vlcMedia.meta( libvlc_meta_ArtworkURL );
    task.title = vlcMedia.meta( libvlc_meta_Title );
    task.genre = vlcMedia.meta( libvlc_meta_Genre );
    task.releaseDate = vlcMedia.meta( libvlc_meta_Date );
    task.showName = vlcMedia.meta( libvlc_meta_ShowName );
    task.albumName = vlcMedia.meta( libvlc_meta_Album );
    task.duration = vlcMedia.duration();

    task.trackNumber = toInt( vlcMedia, libvlc_meta_TrackNumber, "track number" );
    task.episode = toInt( vlcMedia, libvlc_meta_Episode, "episode number" );
}

int VLCMetadataService::toInt( VLC::Media& vlcMedia, libvlc_meta_t meta, const char* name )
{
    auto str = vlcMedia.meta( meta );
    if ( str.empty() == false )
    {
136
#ifndef __ANDROID__
137
138
139
140
141
142
143
144
        try
        {
            return std::stoi( str );
        }
        catch( std::logic_error& ex)
        {
            LOG_WARN( "Invalid ", name, " provided (", str, "): ", ex.what() );
        }
145
146
147
#else
        return atoi( str.c_str() );
#endif
148
149
150
151
    }
    return 0;
}