AudioClipWorkflow.cpp 5.71 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 35 36
    for ( unsigned int i = 0; i < AudioClipWorkflow::nbBuffers; ++i )
    {
        AudioSample* as = new AudioSample;
        as->buff = NULL;
        m_availableBuffers.push_back( as );
    }
    debugType = 1;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
37 38 39 40
}

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

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

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

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
63
//    qWarning() << "Audio. Available:" << m_availableBuffers.count() << "Computed:" << m_computedBuffers.count();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64 65
    if ( isEndReached() == true )
        return NULL;
66 67
    if ( mode == ClipWorkflow::Get )
        qCritical() << "A sound buffer should never be asked with 'Get' mode";
68
    StackedBuffer<AudioSample*, AudioClipWorkflow>* buff = new StackedBuffer<AudioSample*, AudioClipWorkflow>( m_computedBuffers.dequeue(), this, true );
69
    postGetOutput();
70
    return buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
71 72 73 74
}

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

88 89 90 91 92 93 94 95
AudioClipWorkflow::AudioSample*    AudioClipWorkflow::createBuffer( size_t size )
{
    AudioSample* as = new AudioSample;
    as->buff = new uchar[size];
    as->size = size;
    return as;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
96 97
void        AudioClipWorkflow::lock( AudioClipWorkflow* cw, uint8_t** pcm_buffer , unsigned int size )
{
98
    QMutexLocker    lock( cw->m_availableBuffersMutex );
99 100 101
    cw->m_computedBuffersMutex->lock();
    cw->m_renderLock->lock();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
102
//    qWarning() << ">>>AudioGeneration. Available:" << cw->m_availableBuffers.count() << "Computed:" << cw->m_computedBuffers.count();
103 104 105
    AudioSample* as = NULL;
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
106
        qCritical() << cw << "Late buffer generation. Spawning new audio buffer.";
107 108 109
        as = cw->createBuffer( size );
    }
    else
110
    {
111
        as = cw->m_availableBuffers.dequeue();
112 113 114 115 116 117 118
        if ( as->buff == NULL )
        {
            as->buff = new uchar[size];
            as->size = size;
        }
    }
    cw->m_computedBuffers.enqueue( as );
119
    *pcm_buffer = as->buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
120 121 122 123 124
}

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
125
                                      unsigned int size, qint64 pts )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
126
{
127 128
//    qDebug() << "pts:" << pts << "nb channels" << channels << "rate:" << rate <<
//            "size:" << size << "nb_samples:" << nb_samples;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
129 130 131 132
    Q_UNUSED( pcm_buffer );
    Q_UNUSED( rate );
    Q_UNUSED( bits_per_sample );
    Q_UNUSED( size );
133

134
    cw->computePtsDiff( pts );
135
    AudioSample* as = cw->m_computedBuffers.last();
136
//    qWarning() << "Computing audio PTS: pts:" << pts << "m_currentPts:" << cw->m_currentPts << "m_previousPts:" << cw->m_previousPts << "for buffer#" << as->debugId;
137
    if ( as->buff != NULL )
138
    {
139 140 141
        as->nbSample = nb_samples;
        as->nbChannels = channels;
        as->ptsDiff = cw->m_currentPts - cw->m_previousPts;
142
    }
143
    cw->commonUnlock();
144
    cw->m_renderLock->unlock();
145
    cw->m_computedBuffersMutex->unlock();
146 147 148 149 150 151
}

uint32_t    AudioClipWorkflow::getComputedBuffers() const
{
    return m_computedBuffers.count();
}
152 153 154 155 156

uint32_t    AudioClipWorkflow::getMaxComputedBuffers() const
{
    return AudioClipWorkflow::nbBuffers;
}
157 158 159 160 161 162

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