summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/ffmpeg/qffmpegaudiodecoder.cpp
blob: 803d11fc3f3c884835bf8bb5195f72bb20fdbeb6 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qffmpegaudiodecoder_p.h"
#include "qffmpegresampler_p.h"
#include "qaudiobuffer.h"

#include "qffmpegplaybackengine_p.h"
#include "playbackengine/qffmpegrenderer_p.h"

#include <qloggingcategory.h>

Q_LOGGING_CATEGORY(qLcAudioDecoder, "qt.multimedia.ffmpeg.audioDecoder")

QT_BEGIN_NAMESPACE

namespace QFFmpeg
{

class SteppingAudioRenderer : public Renderer
{
    Q_OBJECT
public:
    SteppingAudioRenderer(const QAudioFormat &format) : Renderer({}), m_format(format) { }

    RenderingResult renderInternal(Frame frame) override
    {
        if (!frame.isValid())
            return {};

        if (!m_resampler)
            m_resampler = std::make_unique<Resampler>(frame.codec(), m_format);

        emit newAudioBuffer(m_resampler->resample(frame.avFrame()));

        return {};
    }

signals:
    void newAudioBuffer(QAudioBuffer);

private:
    QAudioFormat m_format;
    std::unique_ptr<Resampler> m_resampler;
};

class AudioDecoder : public PlaybackEngine
{
    Q_OBJECT
public:
    explicit AudioDecoder(const QAudioFormat &format) : m_format(format) { }

    RendererPtr createRenderer(QPlatformMediaPlayer::TrackType trackType) override
    {
        if (trackType != QPlatformMediaPlayer::AudioStream)
            return RendererPtr{ {}, {} };

        auto result = createPlaybackEngineObject<SteppingAudioRenderer>(m_format);
        m_audioRenderer = result.get();

        connect(result.get(), &SteppingAudioRenderer::newAudioBuffer, this,
                &AudioDecoder::newAudioBuffer);

        return result;
    }

    void nextBuffer()
    {
        Q_ASSERT(m_audioRenderer);
        Q_ASSERT(!m_audioRenderer->isStepForced());

        m_audioRenderer->doForceStep();
        // updateObjectsPausedState();
    }

signals:
    void newAudioBuffer(QAudioBuffer);

private:
    QPointer<Renderer> m_audioRenderer;
    QAudioFormat m_format;
};
}


QFFmpegAudioDecoder::QFFmpegAudioDecoder(QAudioDecoder *parent)
    : QPlatformAudioDecoder(parent)
{
}

QFFmpegAudioDecoder::~QFFmpegAudioDecoder() = default;

QUrl QFFmpegAudioDecoder::source() const
{
    return m_url;
}

void QFFmpegAudioDecoder::setSource(const QUrl &fileName)
{
    stop();
    m_sourceDevice = nullptr;

    if (std::exchange(m_url, fileName) != fileName)
        sourceChanged();
}

QIODevice *QFFmpegAudioDecoder::sourceDevice() const
{
    return m_sourceDevice;
}

void QFFmpegAudioDecoder::setSourceDevice(QIODevice *device)
{
    stop();
    m_url.clear();
    if (std::exchange(m_sourceDevice, device) != device)
        sourceChanged();
}

void QFFmpegAudioDecoder::start()
{
    qCDebug(qLcAudioDecoder) << "start";
    auto checkNoError = [this]() {
        if (error() == QAudioDecoder::NoError)
            return true;

        durationChanged(-1);
        positionChanged(-1);

        m_decoder.reset();

        return false;
    };

    m_decoder = std::make_unique<AudioDecoder>(m_audioFormat);
    connect(m_decoder.get(), &AudioDecoder::errorOccured, this, &QFFmpegAudioDecoder::errorSignal);
    connect(m_decoder.get(), &AudioDecoder::endOfStream, this, &QFFmpegAudioDecoder::done);
    connect(m_decoder.get(), &AudioDecoder::newAudioBuffer, this,
            &QFFmpegAudioDecoder::newAudioBuffer);

    m_decoder->setMedia(m_url, m_sourceDevice);
    if (!checkNoError())
        return;

    m_decoder->setState(QMediaPlayer::PausedState);
    if (!checkNoError())
        return;

    m_decoder->nextBuffer();
    if (!checkNoError())
        return;

    durationChanged(m_decoder->m_duration / 1000);
    setIsDecoding(true);
}

void QFFmpegAudioDecoder::stop()
{
    qCDebug(qLcAudioDecoder) << ">>>>> stop";
    if (m_decoder) {
        m_decoder.reset();
        done();
    }
}

QAudioFormat QFFmpegAudioDecoder::audioFormat() const
{
    return m_audioFormat;
}

void QFFmpegAudioDecoder::setAudioFormat(const QAudioFormat &format)
{
    if (std::exchange(m_audioFormat, format) != format)
        formatChanged(m_audioFormat);
}

QAudioBuffer QFFmpegAudioDecoder::read()
{
    auto buffer = std::exchange(m_audioBuffer, QAudioBuffer{});
    if (!buffer.isValid())
        return buffer;
    qCDebug(qLcAudioDecoder) << "reading buffer" << buffer.startTime();
    bufferAvailableChanged(false);
    if (m_decoder)
        m_decoder->nextBuffer();
    return buffer;
}

void QFFmpegAudioDecoder::newAudioBuffer(const QAudioBuffer &b)
{
    Q_ASSERT(b.isValid());
    Q_ASSERT(!m_audioBuffer.isValid());
    Q_ASSERT(!bufferAvailable());

    qCDebug(qLcAudioDecoder) << "new audio buffer" << b.startTime();
    m_audioBuffer = b;
    const qint64 pos = b.startTime();
    positionChanged(pos/1000);
    bufferAvailableChanged(b.isValid());
    bufferReady();
}

void QFFmpegAudioDecoder::done()
{
    qCDebug(qLcAudioDecoder) << ">>>>> DONE!";
    finished();
}

void QFFmpegAudioDecoder::errorSignal(int err, const QString &errorString)
{
    // unfortunately the error enums for QAudioDecoder and QMediaPlayer aren't identical.
    // Map them.
    switch (QMediaPlayer::Error(err)) {
    case QMediaPlayer::NoError:
        error(QAudioDecoder::NoError, errorString);
        break;
    case QMediaPlayer::ResourceError:
        error(QAudioDecoder::ResourceError, errorString);
        break;
    case QMediaPlayer::FormatError:
        error(QAudioDecoder::FormatError, errorString);
        break;
    case QMediaPlayer::NetworkError:
        // fall through, Network error doesn't exist in QAudioDecoder
    case QMediaPlayer::AccessDeniedError:
        error(QAudioDecoder::AccessDeniedError, errorString);
        break;
    }
}

QT_END_NAMESPACE

#include "moc_qffmpegaudiodecoder_p.cpp"

#include "qffmpegaudiodecoder.moc"