MetaDataWorker.cpp 9.16 KB
Newer Older
Christophe Courtaut's avatar
Christophe Courtaut committed
1
/*****************************************************************************
2
 * MetaDataWorker.cpp: Implement the thread that will get the media informations
Christophe Courtaut's avatar
Christophe Courtaut committed
3
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
Christophe Courtaut's avatar
Christophe Courtaut committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/
22

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
23
#include <QtDebug>
24

25
#include "vlmc.h"
26
#include "MetaDataWorker.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
27
#include "Library.h"
28
#include "SettingsManager.h"
29
#include "VLCMediaPlayer.h"
30 31
#include "VLCMedia.h"
#include "Clip.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
32

33 34 35 36 37
#ifdef WITH_GUI
# include <QPainter>
# include <QLabel>
# include <QImage>
#endif
38

39 40
#include <QTimer>

41
MetaDataWorker::MetaDataWorker( LibVLCpp::MediaPlayer* mediaPlayer, Media* media ) :
42 43
        m_mediaPlayer( mediaPlayer ),
        m_media( media ),
44
        m_audioBuffer( NULL )
45
{
46
    connect( this, SIGNAL( finished() ), this, SLOT( deleteLater() ) );
47 48
}

49
MetaDataWorker::~MetaDataWorker()
50
{
51 52
    if ( m_audioBuffer )
        delete m_audioBuffer;
53 54
}

55 56 57 58 59 60
static bool checkEvent(const LibVLCpp::MediaPlayer*, const libvlc_event_t* event)
{
    return event->type == libvlc_MediaPlayerTimeChanged &&
            event->u.media_player_time_changed.new_time > 0;
}

61
void
62
MetaDataWorker::run()
63
{
64 65 66 67 68
    QList<int>  cancel;

    cancel << libvlc_MediaPlayerEncounteredError << libvlc_MediaPlayerEndReached;

    m_mediaPlayer->configureWaitForEvent( libvlc_MediaPlayerTimeChanged, cancel, &checkEvent );
69

70
    m_media->addConstantParam( ":vout=dummy" );
71 72
    // In VLC 2.x we can't set the volume before the playback has started
    // so just switch off the audio-output in any case.
73
    m_mediaPlayer->setAudioOutput( "dummy" );
74
    m_mediaPlayer->setMedia( m_media->vlcMedia() );
75

76 77 78
    m_mediaPlayer->play();
    LibVLCpp::MediaPlayer::EventWaitResult res = m_mediaPlayer->waitForEvent();
    if ( res != LibVLCpp::MediaPlayer::Success )
79
    {
80 81 82
        qWarning() << "Got" << (res == LibVLCpp::MediaPlayer::Timeout ? "timeout" : "failure")
                      << "while launching metadata processing";
        failure();
83
    }
84 85
    else
        metaDataAvailable();
86 87
}

88 89
void
MetaDataWorker::metaDataAvailable()
90
{
91 92
    m_media->setNbAudioTrack( m_mediaPlayer->getNbAudioTrack() );
    m_media->setNbVideoTrack( m_mediaPlayer->getNbVideoTrack() );
93 94 95 96

    Q_ASSERT_X( m_media->hasAudioTrack() == true || m_media->hasVideoTrack() == true,
                "metadata parsing", "Position can't be non 0 if no track is available" );

97 98 99 100 101 102
    //Don't be fooled by the extension, and probe the file for it's actual type:
    if ( m_media->fileType() == Media::Video )
    {
        if ( m_media->hasVideoTrack() == false )
            m_media->setFileType( Media::Audio );
    }
103 104 105 106 107
    else if ( m_media->fileType() == Media::Audio )
    {
        if ( m_media->hasVideoTrack() == true )
            m_media->setFileType( Media::Video );
    }
108
    if ( m_media->fileType() != Media::Audio )
109
    {
110 111 112 113
        // In theory the vout is created before the position actually changes.
        // If this happens to be true, we will have to re-add the old vout-waiting code
        Q_ASSERT_X( m_mediaPlayer->hasVout() == true, "metadata parsing",
                    "A Vout should have already been available. Please report the problem." );
114 115 116 117
        quint32     width, height;
        m_mediaPlayer->getSize( &width, &height );
        m_media->setWidth( width );
        m_media->setHeight( height );
118
        m_media->setFps( m_mediaPlayer->getFps() );
119
        if ( m_media->fps() == .0f )
120
        {
121
            qWarning() << "Invalid FPS for media:" << m_media->fileInfo()->absoluteFilePath();
122 123
            m_media->setFps( Clip::DefaultFPS );
        }
124
    }
125 126
    else
    {
127
        double fps = VLMC_PROJECT_GET_DOUBLE( "video/VLMCOutputFPS" );
128
        m_media->setFps( fps );
129
    }
130 131 132 133
    if ( m_media->fileType() == Media::Image )
        m_media->setLength( 10000 );
    else
        m_media->setLength( m_mediaPlayer->getLength() );
134

135
    m_media->setNbFrames( (m_media->lengthMS() / 1000) * m_media->fps() );
136 137

    m_media->emitMetaDataComputed();
138
#ifdef WITH_GUI
139
    //Setting time for snapshot :
140
    if ( m_media->fileType() != Media::Audio && m_media->hasSnapshot() == false )
141
    {
142
        computeSnapshot();
143 144
        return ;
    }
145
#endif
146
    finalize();
147
}
148

149
#ifdef WITH_GUI
150
void
151
MetaDataWorker::computeSnapshot()
152
{
153 154
    QList<int>  cancel;
    cancel << libvlc_MediaPlayerEncounteredError << libvlc_MediaPlayerEndReached;
155

156
    m_mediaPlayer->setTime( m_mediaPlayer->getLength() / 3 );
157

158 159 160 161 162 163
    // Here we don't care about losing a TimeChanged event, so we don't lock before
    // the call to the method that would trigger the event. Anyway, TimeChanged event is triggered
    // almost often enough for us not to care if we missed one or not. However,
    // we don't want to catch one too early.
    m_mediaPlayer->configureWaitForEvent( libvlc_MediaPlayerTimeChanged, cancel, &checkEvent );
    LibVLCpp::MediaPlayer::EventWaitResult res = m_mediaPlayer->waitForEvent();
164

165 166 167 168 169 170 171
    if ( res != LibVLCpp::MediaPlayer::Success )
    {
        qWarning() << "Got" << (res == LibVLCpp::MediaPlayer::Timeout ? "timeout" : "failure")
                      << "while launching metadata processing";
        failure();
        return ;
    }
172

173 174 175
    QTemporaryFile tmp;
    tmp.open();
    tmp.setAutoRemove( false );
176

177
    // Although this function is synchrone, we have to be in the main thread to
178
    // handle a QPixmap, hence the QueuedConnection
179
    connect( m_mediaPlayer, SIGNAL( snapshotTaken( const char* ) ),
180 181
             m_media, SLOT( snapshotReady( const char* ) ),
             Qt::QueuedConnection );
182
    m_mediaPlayer->takeSnapshot( tmp.fileName().toUtf8().constData(), 0, 0 );
183
    finalize();
184
}
185
#endif
186

187 188
void
MetaDataWorker::finalize()
189
{
190
    emit computed();
191 192
}

193
void
194
MetaDataWorker::failure()
195
{
196 197
    emit failed( m_media );
    deleteLater();
198 199
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
//void
//MetaDataWorker::prepareAudioSpectrumComputing()
//{
//    m_media->vlcMedia()->addOption( ":no-sout-video" );
//    m_media->vlcMedia()->addOption( ":sout=#transcode{}:smem" );
//    m_media->vlcMedia()->setAudioDataCtx( this );
//    m_media->vlcMedia()->setAudioLockCallback( reinterpret_cast<void*>( lock ) );
//    m_media->vlcMedia()->setAudioUnlockCallback( reinterpret_cast<void*>( unlock ) );
//    m_media->vlcMedia()->addOption( ":sout-transcode-acodec=fl32" );
//    m_media->vlcMedia()->addOption( ":no-sout-smem-time-sync" );
//    m_media->vlcMedia()->addOption( ":no-sout-keep" );
//    connect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( generateAudioSpectrum() ), Qt::QueuedConnection );
//}
//
//void
//MetaDataWorker::lock( MetaDataWorker* metaDataWorker, uint8_t** pcm_buffer , unsigned int size )
//{
//    if ( metaDataWorker->m_audioBuffer == NULL )
//        metaDataWorker->m_audioBuffer = new unsigned char[size];
//    *pcm_buffer = metaDataWorker->m_audioBuffer;
//}
//
//void
//MetaDataWorker::unlock( MetaDataWorker* metaDataWorker, uint8_t* pcm_buffer,
//                                      unsigned int channels, unsigned int rate,
//                                      unsigned int nb_samples, unsigned int bits_per_sample,
//                                      unsigned int size, int pts )
//{
//    Q_UNUSED( rate );
//    Q_UNUSED( size );
//    Q_UNUSED( pts );
//
//    int bytePerChannelPerSample = bits_per_sample / 8;
//
//    int leftAverage = 0;
//    int rightAverage = 0;
//
//    int it = 0;
//    for ( unsigned int i = 0; i < nb_samples; i++)
//    {
//        int left = 0;
//        int right = 0;
//        for ( int u = 0 ; u < bytePerChannelPerSample; u++, it++ )
//        {
//            int increment = 0;
//            if ( channels == 2 )
//                increment = bytePerChannelPerSample;
//            left <<= 8;
//            left += pcm_buffer[ it ];
//            right <<= 8;
//            right += pcm_buffer[ it + increment ];
//        }
//        leftAverage += left;
//        rightAverage += right;
//    }
//    leftAverage /= nb_samples;
//    metaDataWorker->addAudioValue( leftAverage );
//}
//
//void
//MetaDataWorker::generateAudioSpectrum()
//{
//    disconnect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( generateAudioSpectrum() ) );
//    m_mediaPlayer->stop();
////    AudioSpectrumHelper* audioSpectrum = new AudioSpectrumHelper( m_media->getAudioValues() );
////    audioSpectrum->setAutoDelete( true );
////    QThreadPool::globalInstance()->start( audioSpectrum );
//    m_media->emitAudioSpectrumComuted();
//    delete this;
//}
//
//void
//MetaDataWorker::addAudioValue( int value )
//{
//    m_media->audioValues()->append( value );
//}
//