AudioClipWorkflow.cpp 4.96 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
/*****************************************************************************
 * 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"
26
#include "StackedBuffer.hpp"
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 32
    m_availableBuffersLock = new QReadWriteLock;
    m_computedBuffersLock = new QReadWriteLock;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
33 34 35 36
}

AudioClipWorkflow::~AudioClipWorkflow()
{
37 38 39 40 41 42
    while ( m_availableBuffers.isEmpty() == false )
        delete m_availableBuffers.pop();
    while ( m_computedBuffers.isEmpty() == false )
        delete m_computedBuffers.pop();
    delete m_availableBuffersLock;
    delete m_computedBuffersLock;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
43 44 45 46 47 48 49 50 51 52 53 54
}

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

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

55
void*       AudioClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56 57
{
    QMutexLocker    lock( m_renderLock );
58
    preGetOutput();
59 60

    qWarning() << "Audio. Available:" << m_availableBuffers.count() << "Computed:" << m_computedBuffers.count();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61
    if ( isEndReached() == true )
62 63
    {
        qDebug() << "Audio end reached";
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64
        return NULL;
65
    }
66 67 68
    if ( mode == ClipWorkflow::Get )
        qCritical() << "A sound buffer should never be asked with 'Get' mode";
    StackedBuffer<AudioSample*>* buff = new StackedBuffer<AudioSample*>( m_computedBuffers.pop(), &m_availableBuffers, true );
69
    postGetOutput();
70
    return buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
71 72 73 74 75 76 77 78 79 80
}

void        AudioClipWorkflow::initVlcOutput()
{
    m_vlcMedia->addOption( ":no-sout-video" );
    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" );
81 82
    m_vlcMedia->addOption( ":sout-transcode-samplerate=48000" );
    m_vlcMedia->addOption( ":sout-transcode-channels=2" );
83
    m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
84 85 86 87
}

void        AudioClipWorkflow::lock( AudioClipWorkflow* cw, uint8_t** pcm_buffer , unsigned int size )
{
88 89
    //If there's no buffer at all, it must be the first render
    if ( cw->m_availableBuffers.count() == 0 && cw->m_computedBuffers.count() == 0 )
90
    {
91 92 93 94 95 96 97
        for ( unsigned int i = 0; i < AudioClipWorkflow::nbBuffers; ++i )
        {
            AudioSample* as = new AudioSample;
            as->buff = new uchar[size];
            as->size = size;
            cw->m_availableBuffers.push_back( as );
        }
98
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
99
    cw->m_renderLock->lock();
100 101 102
    AudioSample* as = cw->m_availableBuffers.pop();
    cw->m_computedBuffers.push_back( as );
    *pcm_buffer = as->buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
103 104 105 106 107
}

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
108
                                      unsigned int size, qint64 pts )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
109
{
110 111
//    qDebug() << "pts:" << pts << "nb channels" << channels << "rate:" << rate <<
//            "size:" << size << "nb_samples:" << nb_samples;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
112 113 114 115
    Q_UNUSED( pcm_buffer );
    Q_UNUSED( rate );
    Q_UNUSED( bits_per_sample );
    Q_UNUSED( size );
116

117
    cw->computePtsDiff( pts );
118 119
    AudioSample* as = cw->m_computedBuffers.head();
    if ( as->buff != NULL )
120
    {
121 122 123
        as->nbSample = nb_samples;
        as->nbChannels = channels;
        as->ptsDiff = cw->m_currentPts - cw->m_previousPts;
124
    }
125
    cw->m_renderLock->unlock();
126

127
    cw->commonUnlock();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
128
}
129 130 131 132 133 134 135 136 137 138

uint32_t    AudioClipWorkflow::getAvailableBuffers() const
{
    return m_availableBuffers.count();
}

uint32_t    AudioClipWorkflow::getComputedBuffers() const
{
    return m_computedBuffers.count();
}
139 140 141 142 143

uint32_t    AudioClipWorkflow::getMaxComputedBuffers() const
{
    return AudioClipWorkflow::nbBuffers;
}