AudioClipWorkflow.cpp 7.1 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"
27
#include "Workflow/Types.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
28

29 30
AudioClipWorkflow::AudioClipWorkflow( ClipHelper *ch ) :
        ClipWorkflow( ch )
31 32 33 34 35 36 37 38 39 40 41
{
    m_ptsOffset = 0;
}

AudioClipWorkflow::~AudioClipWorkflow()
{
    releasePrealocated();
}

void
AudioClipWorkflow::preallocate()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
42
{
43
    for ( quint32 i = 0; i < AudioClipWorkflow::nbBuffers; ++i )
44
    {
45
        Workflow::AudioSample *as = new Workflow::AudioSample;
46 47 48
        as->buff = NULL;
        m_availableBuffers.push_back( as );
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
49 50
}

51 52
void
AudioClipWorkflow::releasePrealocated()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
53
{
54
    while ( m_availableBuffers.isEmpty() == false )
55
    {
56
        Workflow::AudioSample *as = m_availableBuffers.takeFirst();
57 58 59
        delete as->buff;
        delete as;
    }
60
    while ( m_computedBuffers.isEmpty() == false )
61
    {
62
        Workflow::AudioSample *as = m_computedBuffers.takeFirst();
63 64 65
        delete as->buff;
        delete as;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
66 67
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
68 69
void*
AudioClipWorkflow::getLockCallback() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
70
{
71
    return reinterpret_cast<void*>( &AudioClipWorkflow::lock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
72 73
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
74 75
void*
AudioClipWorkflow::getUnlockCallback() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
76
{
77
    return reinterpret_cast<void*>( &AudioClipWorkflow::unlock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
78 79
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
80 81
void*
AudioClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
82 83
{
    QMutexLocker    lock( m_renderLock );
84

85
    if ( getNbComputedBuffers() == 0 )
86
        return NULL;
87
    if ( shouldRender() == false )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
88
        return NULL;
89 90
    if ( mode == ClipWorkflow::Get )
        qCritical() << "A sound buffer should never be asked with 'Get' mode";
91
    ::StackedBuffer<Workflow::AudioSample*> *buff = new StackedBuffer(
92
            m_computedBuffers.dequeue(), this, true );
93 94 95 96 97 98 99 100 101 102
    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;
    }
103
    postGetOutput();
104
    return buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
105 106
}

107 108
void
AudioClipWorkflow::initVlcOutput()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
109
{
110
    preallocate();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
111
    m_vlcMedia->addOption( ":no-sout-video" );
112
    m_vlcMedia->addOption( ":no-video" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
113 114 115 116
    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
117
    m_vlcMedia->addOption( ":sout-transcode-acodec=f32l" );
118 119
    m_vlcMedia->addOption( ":sout-transcode-samplerate=48000" );
    m_vlcMedia->addOption( ":sout-transcode-channels=2" );
120
    m_vlcMedia->addOption( ":no-sout-transcode-hurry-up" );
121 122 123 124
    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
125 126
}

127
Workflow::AudioSample*
128
AudioClipWorkflow::createBuffer( size_t size )
129
{
130
    Workflow::AudioSample *as = new Workflow::AudioSample;
131 132 133 134 135
    as->buff = new uchar[size];
    as->size = size;
    return as;
}

136 137
void
AudioClipWorkflow::lock( AudioClipWorkflow *cw, quint8 **pcm_buffer , quint32 size )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
138
{
139 140
    cw->m_renderLock->lock();

141
    Workflow::AudioSample     *as = NULL;
142 143 144
    if ( cw->m_availableBuffers.isEmpty() == true )
        as = cw->createBuffer( size );
    else
145
    {
146
        as = cw->m_availableBuffers.dequeue();
147 148 149 150 151 152 153
        if ( as->buff == NULL )
        {
            as->buff = new uchar[size];
            as->size = size;
        }
    }
    cw->m_computedBuffers.enqueue( as );
154
    *pcm_buffer = as->buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
155 156
}

157 158 159 160 161
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
162 163 164 165 166
{
    Q_UNUSED( pcm_buffer );
    Q_UNUSED( rate );
    Q_UNUSED( bits_per_sample );
    Q_UNUSED( size );
167

168
    pts -= cw->m_ptsOffset;
169
    Workflow::AudioSample* as = cw->m_computedBuffers.last();
170
    if ( as->buff != NULL )
171
    {
172 173
        as->nbSample = nb_samples;
        as->nbChannels = channels;
174 175 176 177 178 179 180 181 182 183 184 185 186 187
        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;
188
    }
189
    cw->commonUnlock();
190
    cw->m_renderLock->unlock();
191 192
}

193
void
194
AudioClipWorkflow::insertPastBlock( Workflow::AudioSample *as )
195
{
196 197
    QQueue<Workflow::AudioSample*>::iterator    it = m_computedBuffers.begin();
    QQueue<Workflow::AudioSample*>::iterator    end = m_computedBuffers.end();
198 199 200 201 202 203 204 205 206 207 208 209 210 211

    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 );
}

212 213
quint32
AudioClipWorkflow::getNbComputedBuffers() const
214 215 216
{
    return m_computedBuffers.count();
}
217

218 219
quint32
AudioClipWorkflow::getMaxComputedBuffers() const
220 221 222
{
    return AudioClipWorkflow::nbBuffers;
}
223

224
void
225
AudioClipWorkflow::releaseBuffer( Workflow::AudioSample *sample )
226
{
227
    QMutexLocker    lock( m_renderLock );
228 229
    m_availableBuffers.enqueue( sample );
}
230

231 232
void
AudioClipWorkflow::flushComputedBuffers()
233
{
234
    QMutexLocker        lock( m_renderLock );
235 236 237 238 239 240

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

242
AudioClipWorkflow::StackedBuffer::StackedBuffer( Workflow::AudioSample *as,
243 244
                                                AudioClipWorkflow *poolHandler,
                                                bool mustBeReleased) :
245
    ::StackedBuffer<Workflow::AudioSample*>( as, mustBeReleased ),
246 247 248 249
    m_poolHandler( poolHandler )
{
}

250 251
void
AudioClipWorkflow::StackedBuffer::release()
252
{
253
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
254 255 256
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}