AudioClipWorkflow.cpp 7.13 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
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
/*****************************************************************************
 * AudioClipWorkflow.cpp : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * 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"

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

AudioClipWorkflow::~AudioClipWorkflow()
{
42
    while ( m_availableBuffers.isEmpty() == false )
43
        delete m_availableBuffers.dequeue();
44
    while ( m_computedBuffers.isEmpty() == false )
45
        delete m_computedBuffers.dequeue();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
46 47 48 49 50 51 52 53 54 55 56 57
}

void*       AudioClipWorkflow::getLockCallback()
{
    return reinterpret_cast<void*>(&AudioClipWorkflow::lock);
}

void*       AudioClipWorkflow::getUnlockCallback()
{
    return reinterpret_cast<void*>(&AudioClipWorkflow::unlock);
}

58
void*       AudioClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
59
{
60
//    qDebug() << "entering audio get output";
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61
    QMutexLocker    lock( m_renderLock );
62
//    qDebug() << "got audio render lock";
63
    QMutexLocker    lock2( m_computedBuffersMutex );
64
//    qDebug() << "got computed buffers mutex";
65 66

    if ( preGetOutput() == false )
67
    {
68
//        qDebug() << "audio preGetOutput() returned false";
69
        return NULL;
70
    }
71

72
//    qWarning() << "Audio. Available:" << m_availableBuffers.count() << "Computed:" << m_computedBuffers.count();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
73
    if ( isEndReached() == true )
74
    {
75
//        qDebug() << "audio end is reached";
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
76
        return NULL;
77
    }
78 79
    if ( mode == ClipWorkflow::Get )
        qCritical() << "A sound buffer should never be asked with 'Get' mode";
80 81
    ::StackedBuffer<AudioSample*>* buff = new StackedBuffer(
            m_computedBuffers.dequeue(), this, true );
82
//    qDebug() << "calling audio postGetOutput();";
83
    postGetOutput();
84
//    qDebug() << "returning audio buffer";
85
    return buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
86 87 88 89
}

void        AudioClipWorkflow::initVlcOutput()
{
90 91
//    m_vlcMedia->addOption( ":verbose 3" );
//    m_vlcMedia->addOption( ":vvv" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
92
    m_vlcMedia->addOption( ":no-sout-video" );
93
    m_vlcMedia->addOption( ":no-video" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
94 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() ) );
    m_vlcMedia->addOption( ":sout-transcode-acodec=s16l" );
99 100
    m_vlcMedia->addOption( ":sout-transcode-samplerate=48000" );
    m_vlcMedia->addOption( ":sout-transcode-channels=2" );
101
    m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
102 103
}

104 105
AudioClipWorkflow::AudioSample*    AudioClipWorkflow::createBuffer( size_t size )
{
106
    qDebug() << "Creating new buffer of size:" << size;
107 108 109
    AudioSample* as = new AudioSample;
    as->buff = new uchar[size];
    as->size = size;
110
    as->debugId = -1;
111 112 113
    return as;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
114 115
void        AudioClipWorkflow::lock( AudioClipWorkflow* cw, uint8_t** pcm_buffer , unsigned int size )
{
116
    QMutexLocker    lock( cw->m_availableBuffersMutex );
117
    cw->m_renderLock->lock();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
118
    cw->m_computedBuffersMutex->lock();
119

120
    qWarning() << ">>>AudioGeneration. Available:" << cw->m_availableBuffers.count() << "Computed:" << cw->m_computedBuffers.count() << "position" << cw->m_mediaPlayer->getPosition();
121 122 123
    AudioSample* as = NULL;
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
124
//        qCritical() << cw << "Late buffer generation. Spawning new audio buffer.";
125 126 127
        as = cw->createBuffer( size );
    }
    else
128
    {
129
        as = cw->m_availableBuffers.dequeue();
130 131 132 133 134 135 136
        if ( as->buff == NULL )
        {
            as->buff = new uchar[size];
            as->size = size;
        }
    }
    cw->m_computedBuffers.enqueue( as );
137
    *pcm_buffer = as->buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
138 139 140 141 142
}

void        AudioClipWorkflow::unlock( AudioClipWorkflow* cw, uint8_t* pcm_buffer,
                                      unsigned int channels, unsigned int rate,
                                      unsigned int nb_samples, unsigned int bits_per_sample,
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
143
                                      unsigned int size, qint64 pts )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
144
{
145 146
//    qDebug() << "pts:" << pts << "nb channels" << channels << "rate:" << rate <<
//            "size:" << size << "nb_samples:" << nb_samples;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
147 148 149 150
    Q_UNUSED( pcm_buffer );
    Q_UNUSED( rate );
    Q_UNUSED( bits_per_sample );
    Q_UNUSED( size );
151

152
    cw->computePtsDiff( pts );
153
    AudioSample* as = cw->m_computedBuffers.last();
154
    if ( as->buff != NULL )
155
    {
156 157 158
        as->nbSample = nb_samples;
        as->nbChannels = channels;
        as->ptsDiff = cw->m_currentPts - cw->m_previousPts;
159
    }
160 161 162 163
//    if ( as->ptsDiff == 0 )
//    {
//        qCritical() << "PTS DIFF IS 0 !!!! ";
//    }
164
//    qWarning() << "::::Computing audio PTS: debugId:" << as->debugId << "ptsdiff:" << as->ptsDiff;
165
    cw->commonUnlock();
166
    cw->m_renderLock->unlock();
167
    cw->m_computedBuffersMutex->unlock();
168 169
}

170
uint32_t    AudioClipWorkflow::getNbComputedBuffers() const
171 172 173
{
    return m_computedBuffers.count();
}
174 175 176 177 178

uint32_t    AudioClipWorkflow::getMaxComputedBuffers() const
{
    return AudioClipWorkflow::nbBuffers;
}
179 180 181 182 183 184

void        AudioClipWorkflow::releaseBuffer( AudioSample *sample )
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( sample );
}
185 186 187

void        AudioClipWorkflow::flushComputedBuffers()
{
188 189 190
    qDebug() << "Flushing computed buffers in audio";
    QMutexLocker    lock( m_availableBuffersMutex );
    QMutexLocker    lock2( m_computedBuffersMutex );
191 192 193 194 195 196

    while ( m_computedBuffers.isEmpty() == false )
    {
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
    }
}
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

AudioClipWorkflow::StackedBuffer::StackedBuffer( AudioClipWorkflow::AudioSample *as,
                                                        AudioClipWorkflow *poolHandler,
                                                        bool mustBeReleased) :
    ::StackedBuffer<AudioClipWorkflow::AudioSample*>( as, mustBeReleased ),
    m_poolHandler( poolHandler )
{
}

void    AudioClipWorkflow::StackedBuffer::release()
{
    if ( m_mustRelease == true )
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}