AudioClipWorkflow.cpp 5.92 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;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
39 40 41 42
}

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

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

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

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

    if ( preGetOutput() == false )
        return NULL;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
69 70
    if ( isEndReached() == true )
        return NULL;
71 72
    if ( mode == ClipWorkflow::Get )
        qCritical() << "A sound buffer should never be asked with 'Get' mode";
73
    ::StackedBuffer<AudioSample*> *buff = new StackedBuffer(
74
            m_computedBuffers.dequeue(), this, true );
75
    postGetOutput();
76
    return buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
77 78
}

79 80
void
AudioClipWorkflow::initVlcOutput()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
81 82
{
    m_vlcMedia->addOption( ":no-sout-video" );
83
    m_vlcMedia->addOption( ":no-video" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
84 85 86 87
    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
88
    m_vlcMedia->addOption( ":sout-transcode-acodec=f32l" );
89 90
    m_vlcMedia->addOption( ":sout-transcode-samplerate=48000" );
    m_vlcMedia->addOption( ":sout-transcode-channels=2" );
91 92 93 94
    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
95 96
}

97 98
AudioClipWorkflow::AudioSample*
AudioClipWorkflow::createBuffer( size_t size )
99
{
100
    AudioSample *as = new AudioSample;
101 102
    as->buff = new uchar[size];
    as->size = size;
103
    as->debugId = -1;
104 105 106
    return as;
}

107 108
void
AudioClipWorkflow::lock( AudioClipWorkflow *cw, quint8 **pcm_buffer , quint32 size )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
109
{
110
    QMutexLocker    lock( cw->m_availableBuffersMutex );
111
    cw->m_renderLock->lock();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
112
    cw->m_computedBuffersMutex->lock();
113

114
    AudioSample     *as = NULL;
115 116 117
    if ( cw->m_availableBuffers.isEmpty() == true )
        as = cw->createBuffer( size );
    else
118
    {
119
        as = cw->m_availableBuffers.dequeue();
120 121 122 123 124 125 126
        if ( as->buff == NULL )
        {
            as->buff = new uchar[size];
            as->size = size;
        }
    }
    cw->m_computedBuffers.enqueue( as );
127
    *pcm_buffer = as->buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
128 129
}

130 131 132 133 134
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
135 136 137 138 139
{
    Q_UNUSED( pcm_buffer );
    Q_UNUSED( rate );
    Q_UNUSED( bits_per_sample );
    Q_UNUSED( size );
140

141
    cw->computePtsDiff( pts );
142
    AudioSample* as = cw->m_computedBuffers.last();
143
    if ( as->buff != NULL )
144
    {
145 146 147
        as->nbSample = nb_samples;
        as->nbChannels = channels;
        as->ptsDiff = cw->m_currentPts - cw->m_previousPts;
148
    }
149
    cw->commonUnlock();
150
    cw->m_renderLock->unlock();
151
    cw->m_computedBuffersMutex->unlock();
152 153
}

154 155
quint32
AudioClipWorkflow::getNbComputedBuffers() const
156 157 158
{
    return m_computedBuffers.count();
}
159

160 161
quint32
AudioClipWorkflow::getMaxComputedBuffers() const
162 163 164
{
    return AudioClipWorkflow::nbBuffers;
}
165

166 167
void
AudioClipWorkflow::releaseBuffer( AudioSample *sample )
168 169 170 171
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( sample );
}
172

173 174
void
AudioClipWorkflow::flushComputedBuffers()
175
{
176 177
    QMutexLocker    lock( m_availableBuffersMutex );
    QMutexLocker    lock2( m_computedBuffersMutex );
178 179 180 181 182 183

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

AudioClipWorkflow::StackedBuffer::StackedBuffer( AudioClipWorkflow::AudioSample *as,
186 187
                                                AudioClipWorkflow *poolHandler,
                                                bool mustBeReleased) :
188 189 190 191 192
    ::StackedBuffer<AudioClipWorkflow::AudioSample*>( as, mustBeReleased ),
    m_poolHandler( poolHandler )
{
}

193 194
void
AudioClipWorkflow::StackedBuffer::release()
195
{
196
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
197 198 199
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}