audiodataoutput.cpp 6.62 KB
Newer Older
1
/*
2
3
    Copyright (C) 2006 Matthias Kretz <kretz@kde.org>
    Copyright (C) 2009 Martin Sandsmark <sandsmark@samfundet.no>
4
    Copyright (C) 2011 Harald Sitter <sitter@kde.org>
5
    Copyright (C) 2011 Alessandro Siniscalchi <asiniscalchi@gmail.com>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), Nokia Corporation
    (or its successors, if any) and the KDE Free Qt Foundation, which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "audiodataoutput.h"
#include "gsthelper.h"
#include "medianode.h"
#include <QtCore/QVector>
#include <QtCore/QMap>
#include <phonon/audiooutput.h>

32
#include <gst/gstghostpad.h>
Harald Sitter's avatar
Harald Sitter committed
33
34
#include <gst/gstutils.h>

35
36
37
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE

38
39
40
41
namespace Phonon
{
namespace Gstreamer
{
42

43
AudioDataOutput::AudioDataOutput(Backend *backend, QObject *parent)
44
45
    : QObject(parent)
    , MediaNode(backend, AudioSink)
46
47
48
49
{
    static int count = 0;
    m_name = "AudioDataOutput" + QString::number(count++);

50
51
52
53
54
    m_queue = gst_bin_new(NULL);
    gst_object_ref(GST_OBJECT(m_queue));
    gst_object_sink(GST_OBJECT(m_queue));
    GstElement* sink = gst_element_factory_make("fakesink", NULL);
    GstElement* queue = gst_element_factory_make("queue", NULL);
55
    GstElement* convert = gst_element_factory_make("audioconvert", NULL);
56
57

    g_signal_connect(sink, "handoff", G_CALLBACK(processBuffer), this);
Trever Fischer's avatar
Trever Fischer committed
58
    g_object_set(G_OBJECT(sink), "signal-handoffs", true, NULL);
59

60
61
62
    //G_BYTE_ORDER is the host machine's endianess
    GstCaps *caps = gst_caps_new_simple("audio/x-raw-int",
                                        "endianess", G_TYPE_INT, G_BYTE_ORDER,
63
                                        "width", G_TYPE_INT, 16,
64
65
66
                                        "depth", G_TYPE_INT, 16,
                                        NULL);

67
    gst_bin_add_many(GST_BIN(m_queue), sink, convert, queue, NULL);
68
69
    gst_element_link(queue, convert);
    gst_element_link_filtered(convert, sink, caps);
70
    gst_caps_unref(caps);
71

72
    GstPad *inputpad = gst_element_get_static_pad(queue, "sink");
73
74
75
    gst_element_add_pad(m_queue, gst_ghost_pad_new("sink", inputpad));
    gst_object_unref(inputpad);

Trever Fischer's avatar
Trever Fischer committed
76
    g_object_set(G_OBJECT(sink), "sync", true, NULL);
77

78
79
80
81
82
83
84
85
86
    m_isValid = true;
}

AudioDataOutput::~AudioDataOutput()
{
    gst_element_set_state(m_queue, GST_STATE_NULL);
    gst_object_unref(m_queue);
}

87
88
89
90
91
void AudioDataOutput::setDataSize(int size)
{
    m_dataSize = size;
}

92
93
94
95
96
97
98
99
100
101
int AudioDataOutput::dataSize() const
{
    return m_dataSize;
}

int AudioDataOutput::sampleRate() const
{
    return 44100;
}

102
inline void AudioDataOutput::convertAndEmit()
103
{
104
    QMap<Phonon::AudioDataOutput::Channel, QVector<qint16> > map;
105

106
107
108
    for (int i = 0 ; i < m_channels ; ++i) {
        map.insert(static_cast<Phonon::AudioDataOutput::Channel>(i), m_channelBuffers[i]);
    }
109
110
111
112

    emit dataReady(map);
}

113
void AudioDataOutput::processBuffer(GstElement*, GstBuffer* buffer, GstPad*, gpointer gThat)
114
115
{
    // TODO emit endOfMedia
116
117
118
119
120
121
    AudioDataOutput *that = static_cast<AudioDataOutput *>(gThat);

    // Copiend locally to avoid multithead problems
    qint32 dataSize = that->m_dataSize;
    if (dataSize == 0)
        return;
122
123

    // determine the number of channels
124
125
    GstStructure *structure = gst_caps_get_structure(GST_BUFFER_CAPS(buffer), 0);
    gst_structure_get_int(structure, "channels", &that->m_channels);
126

127
128
129
130
131
132
    // Let's get the buffers
    gint16 *gstBufferData = reinterpret_cast<gint16 *>(GST_BUFFER_DATA(buffer));
    guint gstBufferSize = GST_BUFFER_SIZE(buffer) / sizeof(gint16);

    if (gstBufferSize == 0) {
        qWarning() << Q_FUNC_INFO << ": received a buffer of 0 size ... doing nothing";
133
134
135
        return;
    }

136
137
138
139
    if ((gstBufferSize % that->m_channels) != 0) {
        qWarning() << Q_FUNC_INFO << ": corrupted data";
        return;
    }
140

Alessandro Siniscalchi's avatar
Alessandro Siniscalchi committed
141
142
143
144
    // I set the number of channels
    if (that->m_channelBuffers.size() != that->m_channels)
        that->m_channelBuffers.resize(that->m_channels);

145
146
    // check how many emits I will perform
    int nBlockToSend = (that->m_pendingData.size() + gstBufferSize) / (dataSize * that->m_channels);
147

148
149
150
151
152
    if (nBlockToSend == 0) { // add data to pending buffer
        for (quint32 i = 0; i < gstBufferSize ; ++i) {
            that->m_pendingData.append(gstBufferData[i]);
            return;
        }
153
154
    }

155
156
157
158
159
    // SENDING DATA

    // 1) I empty the stored data
    if (that->m_pendingData.size() != 0) {
        for (int i = 0; i < that->m_pendingData.size(); i += that->m_channels) {
160
161
            //TODO: Figure out why it crashes without the second test (bug #279791)
            for (int j = 0; (j < that->m_channels) && (i+j < that->m_pendingData.size()); ++j) {
162
                that->m_channelBuffers[j].append(that->m_pendingData[i+j]);
163
164
            }
        }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

        if (that->m_pendingData.capacity() != dataSize)
            that->m_pendingData.reserve(dataSize);

        that->m_pendingData.resize(0);
    }

    // 2) I fill with fresh data and send
    for (int i = 0 ; i < that->m_channels ; ++i)
    {
        if (that->m_channelBuffers[i].capacity() != dataSize)
            that->m_channelBuffers.reserve(dataSize);
    }

    quint32 bufferPosition = 0;
    for (int i = 0 ; i < nBlockToSend ; ++i) {
        for ( ; (that->m_channelBuffers[0].size() < dataSize) && (bufferPosition < gstBufferSize) ; bufferPosition += that->m_channels ) {
            for (int j = 0 ; j < that->m_channels ; ++j) {
                that->m_channelBuffers[j].append(gstBufferData[bufferPosition+j]);
            }
        }

        that->convertAndEmit();

        for (int j = 0 ; j < that->m_channels ; ++j) {
            // QVector::resize doesn't reallocate the buffer
            that->m_channelBuffers[j].resize(0);
        }
    }

    // 3) I store the rest of data
    while (bufferPosition < gstBufferSize) {
        that->m_pendingData.append(gstBufferData[bufferPosition]);
        ++bufferPosition;
199
200
201
202
203
    }
}

}} //namespace Phonon::Gstreamer

204
205
206
QT_END_NAMESPACE
QT_END_HEADER

207
#include "moc_audiodataoutput.cpp"