-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMetaDataWorker.cpp
301 lines (269 loc) · 9.53 KB
/
MetaDataWorker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
150
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
186
187
188
189
190
191
192
193
194
195
196
197
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*****************************************************************************
* MetaDataWorker.cpp: Implement the thread that will get the media informations
*****************************************************************************
* Copyright (C) 2008-2010 VideoLAN
*
* Authors: Hugo Beauzee-Luyssen <[email protected]>
*
* 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.
*****************************************************************************/
#include <QtDebug>
#include <QPainter>
#include <QLabel>
#include <QImage>
#include "vlmc.h"
#include "MetaDataWorker.h"
#include "Library.h"
#include "SettingsManager.h"
#include "VLCMediaPlayer.h"
#include "VLCMedia.h"
#include "Clip.h"
#include <QThreadPool>
#include <QRunnable>
MetaDataWorker::MetaDataWorker( LibVLCpp::MediaPlayer* mediaPlayer, Media* media ) :
m_mediaPlayer( mediaPlayer ),
m_media( media ),
m_mediaIsPlaying( false),
m_lengthHasChanged( false ),
m_audioBuffer( NULL )
{
}
MetaDataWorker::~MetaDataWorker()
{
if ( m_audioBuffer )
delete m_audioBuffer;
}
void
MetaDataWorker::compute()
{
if ( m_media->fileType() == Media::Video ||
m_media->fileType() == Media::Audio )
computeDynamicFileMetaData();
else if ( m_media->fileType() == Media::Image )
computeImageMetaData();
m_media->addConstantParam( ":vout=dummy" );
m_mediaPlayer->setMedia( m_media->vlcMedia() );
connect( m_mediaPlayer, SIGNAL( playing() ),
this, SLOT( entrypointPlaying() ), Qt::QueuedConnection );
connect( m_mediaPlayer, SIGNAL( errorEncountered() ), this, SLOT( failure() ) );
m_mediaPlayer->play();
m_media->flushVolatileParameters();
}
void
MetaDataWorker::computeDynamicFileMetaData()
{
//Disabling audio for this specific use of the media
if ( m_media->fileType() == Media::Audio )
m_media->addVolatileParam( ":volume 0", ":volume 512" );
else
m_media->addVolatileParam( ":no-audio", ":audio" );
connect( m_mediaPlayer, SIGNAL( lengthChanged( qint64 ) ),
this, SLOT( entrypointLengthChanged( qint64 ) ), Qt::QueuedConnection );
}
void
MetaDataWorker::computeImageMetaData()
{
m_media->addVolatileParam( ":access=fake", ":access=''" );
m_media->addVolatileParam( ":fake-duration=10000", ":fake-duration=''" );
//There can't be a length for an image file, so we don't have to wait for it to be updated.
m_lengthHasChanged = true;
}
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::metaDataAvailable()
{
m_mediaIsPlaying = false;
m_lengthHasChanged = false;
//In order to wait for the VOUT to be ready:
//Until we have a way of knowing when it is, both getWidth and getHeight method
//will trigger exception... so we shut it up.
if ( m_media->fileType() != Media::Audio )
{
m_timer.restart();
while ( m_mediaPlayer->hasVout() == false &&
m_timer.elapsed() < 3000 )
{
SleepMS( 10 ); //Ugly isn't it :)
}
if ( m_mediaPlayer->hasVout() == false )
{
emit failed( m_media );
return ;
}
quint32 width, height;
m_mediaPlayer->getSize( &width, &height );
m_media->setWidth( width );
m_media->setHeight( height );
m_media->setFps( m_mediaPlayer->getFps() );
if ( m_media->fps() == .0f )
{
qWarning() << "Invalid FPS for media:" << m_media->fileInfo()->absoluteFilePath();
m_media->setFps( Clip::DefaultFPS );
}
}
else
{
double fps = VLMC_PROJECT_GET_DOUBLE( "video/VLMCOutputFPS" );
m_media->setFps( fps );
}
m_media->setLength( m_mediaPlayer->getLength() );
m_media->setNbAudioTrack( m_mediaPlayer->getNbAudioTrack() );
m_media->setNbVideoTrack( m_mediaPlayer->getNbVideoTrack() );
m_media->setNbFrames( (m_media->lengthMS() / 1000) * m_media->fps() );
m_media->emitMetaDataComputed();
//Setting time for snapshot :
if ( m_media->fileType() == Media::Video ||
m_media->fileType() == Media::Image )
{
connect( m_mediaPlayer, SIGNAL( positionChanged( float ) ), this, SLOT( renderSnapshot() ) );
m_mediaPlayer->setTime( m_mediaPlayer->getLength() / 3 );
}
else
finalize();
}
void
MetaDataWorker::renderSnapshot()
{
if ( m_media->fileType() == Media::Video ||
m_media->fileType() == Media::Audio )
disconnect( m_mediaPlayer, SIGNAL( positionChanged( float ) ), this, SLOT( renderSnapshot() ) );
QTemporaryFile tmp;
tmp.setAutoRemove( false );
tmp.open();
connect( m_mediaPlayer, SIGNAL( snapshotTaken( const char* ) ),
this, SLOT( setSnapshot( const char* ) ), Qt::QueuedConnection );
//The slot should be triggered in this methode
m_mediaPlayer->takeSnapshot( tmp.fileName().toStdString().c_str(), 0, 0 );
//Snapshot slot should has been called (but maybe not in next version...)
}
void
MetaDataWorker::setSnapshot( const char* filename )
{
QPixmap* pixmap = new QPixmap( filename );
if ( pixmap->isNull() )
delete pixmap;
else
m_media->setSnapshot( pixmap );
//TODO : we shouldn't have to do this... patch vlc to get a memory snapshot.
QFile tmp( filename );
tmp.remove();
disconnect( m_mediaPlayer, SIGNAL( snapshotTaken(const char*) ),
this, SLOT( setSnapshot( const char* ) ) );
//CHECKME:
//This is synchrone, but it may become asynchrone in the future...
// connect( m_mediaPlayer, SIGNAL( stopped () ), this, SLOT( mediaPlayerStopped() ), Qt::QueuedConnection );
m_media->emitSnapshotComputed();
finalize();
}
void
MetaDataWorker::finalize()
{
m_media->disconnect( this );
emit computed();
delete this;
}
void
MetaDataWorker::entrypointLengthChanged( qint64 newLength )
{
if ( newLength <= 0 )
return ;
disconnect( m_mediaPlayer, SIGNAL( lengthChanged( qint64 ) ),
this, SLOT( entrypointLengthChanged( qint64 ) ) );
m_lengthHasChanged = true;
if ( m_mediaIsPlaying == true )
metaDataAvailable();
}
void
MetaDataWorker::entrypointPlaying()
{
disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( entrypointPlaying() ) );
m_mediaIsPlaying = true;
if ( m_lengthHasChanged == true )
metaDataAvailable();
}
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 );
}
void
MetaDataWorker::failure()
{
emit failed( m_media );
deleteLater();
}