AudioClipWorkflow.cpp 7.02 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1 2 3
/*****************************************************************************
 * AudioClipWorkflow.cpp : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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.
 *****************************************************************************/

#include <QtDebug>

#include "AudioClipWorkflow.h"
26
#include "VLCMedia.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
27

28
AudioClipWorkflow::AudioClipWorkflow( Clip *clip ) :
29
        ClipWorkflow( clip )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
30
{
31
    for ( quint32 i = 0; i < AudioClipWorkflow::nbBuffers; ++i )
32
    {
33
        AudioSample *as = new AudioSample;
34 35
        as->buff = NULL;
        m_availableBuffers.push_back( as );
36
        as->debugId = i;
37 38
    }
    debugType = 1;
39
    m_ptsOffset = 0;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
40 41 42 43
}

AudioClipWorkflow::~AudioClipWorkflow()
{
44
    while ( m_availableBuffers.isEmpty() == false )
45
        delete m_availableBuffers.dequeue();
46
    while ( m_computedBuffers.isEmpty() == false )
47
        delete m_computedBuffers.dequeue();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
48 49
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
50 51
void*
AudioClipWorkflow::getLockCallback() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
52
{
53
    return reinterpret_cast<void*>( &AudioClipWorkflow::lock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
54 55
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56 57
void*
AudioClipWorkflow::getUnlockCallback() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
58
{
59
    return reinterpret_cast<void*>( &AudioClipWorkflow::unlock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
60 61
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
62 63
void*
AudioClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64 65
{
    QMutexLocker    lock( m_renderLock );
66
    QMutexLocker    lock2( m_computedBuffersMutex );
67 68 69

    if ( preGetOutput() == false )
        return NULL;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
70 71
    if ( isEndReached() == true )
        return NULL;
72 73
    if ( mode == ClipWorkflow::Get )
        qCritical() << "A sound buffer should never be asked with 'Get' mode";
74
    ::StackedBuffer<AudioSample*> *buff = new StackedBuffer(
75
            m_computedBuffers.dequeue(), this, true );
76 77 78 79 80 81 82 83 84 85
    if ( m_previousPts == -1 )
    {
        buff->get()->ptsDiff = 0;
        m_previousPts = buff->get()->pts;
    }
    else
    {
        buff->get()->ptsDiff = buff->get()->pts - m_previousPts;
        m_previousPts = buff->get()->pts;
    }
86
    postGetOutput();
87
    return buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
88 89
}

90 91
void
AudioClipWorkflow::initVlcOutput()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
92 93
{
    m_vlcMedia->addOption( ":no-sout-video" );
94
    m_vlcMedia->addOption( ":no-video" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
95 96 97 98
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
    m_vlcMedia->setAudioDataCtx( this );
    m_vlcMedia->setAudioLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setAudioUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
99
    m_vlcMedia->addOption( ":sout-transcode-acodec=f32l" );
100 101
    m_vlcMedia->addOption( ":sout-transcode-samplerate=48000" );
    m_vlcMedia->addOption( ":sout-transcode-channels=2" );
102
    m_vlcMedia->addOption( ":no-sout-transcode-hurry-up" );
103 104 105 106
    if ( m_fullSpeedRender == false )
        m_vlcMedia->addOption( ":sout-smem-time-sync" );
    else
        m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
107 108
}

109 110
AudioClipWorkflow::AudioSample*
AudioClipWorkflow::createBuffer( size_t size )
111
{
112
    AudioSample *as = new AudioSample;
113 114
    as->buff = new uchar[size];
    as->size = size;
115
    as->debugId = -1;
116 117 118
    return as;
}

119 120
void
AudioClipWorkflow::lock( AudioClipWorkflow *cw, quint8 **pcm_buffer , quint32 size )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
121
{
122
    QMutexLocker    lock( cw->m_availableBuffersMutex );
123
    cw->m_renderLock->lock();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
124
    cw->m_computedBuffersMutex->lock();
125

126
    AudioSample     *as = NULL;
127 128 129
    if ( cw->m_availableBuffers.isEmpty() == true )
        as = cw->createBuffer( size );
    else
130
    {
131
        as = cw->m_availableBuffers.dequeue();
132 133 134 135 136 137 138
        if ( as->buff == NULL )
        {
            as->buff = new uchar[size];
            as->size = size;
        }
    }
    cw->m_computedBuffers.enqueue( as );
139
    *pcm_buffer = as->buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
140 141
}

142 143 144 145 146
void
AudioClipWorkflow::unlock( AudioClipWorkflow *cw, quint8 *pcm_buffer,
                                      quint32 channels, quint32 rate,
                                      quint32 nb_samples, quint32 bits_per_sample,
                                      quint32 size, qint64 pts )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
147 148 149 150 151
{
    Q_UNUSED( pcm_buffer );
    Q_UNUSED( rate );
    Q_UNUSED( bits_per_sample );
    Q_UNUSED( size );
152

153
    pts -= cw->m_ptsOffset;
154
    AudioSample* as = cw->m_computedBuffers.last();
155
    if ( as->buff != NULL )
156
    {
157 158
        as->nbSample = nb_samples;
        as->nbChannels = channels;
159 160 161 162 163 164 165 166 167 168 169 170 171 172
        as->ptsDiff = 0;
        as->pts = pts;
        if ( cw->m_pauseDuration != -1 )
        {
            cw->m_ptsOffset += cw->m_pauseDuration;
            cw->m_pauseDuration = -1;
        }
        if ( cw->m_currentPts > pts )
        {
            cw->m_computedBuffers.removeLast();
            cw->insertPastBlock( as );
        }
        else
            cw->m_currentPts = pts;
173
    }
174
    cw->commonUnlock();
175
    cw->m_renderLock->unlock();
176
    cw->m_computedBuffersMutex->unlock();
177 178
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
void
AudioClipWorkflow::insertPastBlock( AudioSample *as )
{
    QQueue<AudioSample*>::iterator    it = m_computedBuffers.begin();
    QQueue<AudioSample*>::iterator    end = m_computedBuffers.end();

    while ( it != end )
    {
        if ( (*it)->pts > as->pts )
        {
            m_computedBuffers.insert( it, as );
            return ;
        }
        ++it;
    }
    //Fail safe: reinsert the block at the end.
    m_computedBuffers.push_back( as );
}

198 199
quint32
AudioClipWorkflow::getNbComputedBuffers() const
200 201 202
{
    return m_computedBuffers.count();
}
203

204 205
quint32
AudioClipWorkflow::getMaxComputedBuffers() const
206 207 208
{
    return AudioClipWorkflow::nbBuffers;
}
209

210 211
void
AudioClipWorkflow::releaseBuffer( AudioSample *sample )
212 213 214 215
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( sample );
}
216

217 218
void
AudioClipWorkflow::flushComputedBuffers()
219
{
220 221
    QMutexLocker    lock( m_availableBuffersMutex );
    QMutexLocker    lock2( m_computedBuffersMutex );
222 223 224 225 226 227

    while ( m_computedBuffers.isEmpty() == false )
    {
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
    }
}
228 229

AudioClipWorkflow::StackedBuffer::StackedBuffer( AudioClipWorkflow::AudioSample *as,
230 231
                                                AudioClipWorkflow *poolHandler,
                                                bool mustBeReleased) :
232 233 234 235 236
    ::StackedBuffer<AudioClipWorkflow::AudioSample*>( as, mustBeReleased ),
    m_poolHandler( poolHandler )
{
}

237 238
void
AudioClipWorkflow::StackedBuffer::release()
239
{
240
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
241 242 243
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}