summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/windows/decoder/mfaudiodecodercontrol.cpp
blob: 93ad6267cfe8a8a2768b998615dfcb75605bcfcd (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
// Copyright (C) 2016 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 <system_error>
#include <mferror.h>
#include <qglobal.h>
#include "Wmcodecdsp.h"
#include "mfaudiodecodercontrol_p.h"
#include <private/qwindowsaudioutils_p.h>

MFAudioDecoderControl::MFAudioDecoderControl(QAudioDecoder *parent)
    : QPlatformAudioDecoder(parent)
    , m_sourceResolver(new SourceResolver)
{
    connect(m_sourceResolver, &SourceResolver::mediaSourceReady, this, &MFAudioDecoderControl::handleMediaSourceReady);
    connect(m_sourceResolver, &SourceResolver::error, this, &MFAudioDecoderControl::handleMediaSourceError);
}

MFAudioDecoderControl::~MFAudioDecoderControl()
{
    m_sourceResolver->shutdown();
    m_sourceResolver->Release();
}

void MFAudioDecoderControl::setSource(const QUrl &fileName)
{
    if (!m_device && m_source == fileName)
        return;
    stop();
    m_sourceResolver->cancel();
    m_sourceResolver->shutdown();
    m_device = nullptr;
    m_source = fileName;
    sourceChanged();

    if (!m_source.isEmpty()) {
        m_sourceResolver->load(m_source, 0);
        m_loadingSource = true;
    }
}

void MFAudioDecoderControl::setSourceDevice(QIODevice *device)
{
    if (m_device == device && m_source.isEmpty())
        return;
    stop();
    m_sourceResolver->cancel();
    m_sourceResolver->shutdown();
    m_source.clear();
    m_device = device;
    sourceChanged();

    if (m_device) {
        if (m_device->isOpen() && m_device->isReadable()) {
            m_sourceResolver->load(QUrl(), m_device);
            m_loadingSource = true;
        }
    }
}

void MFAudioDecoderControl::handleMediaSourceReady()
{
    m_loadingSource = false;
    if (m_deferredStart) {
        m_deferredStart = false;
        startReadingSource(m_sourceResolver->mediaSource());
    }
}

void MFAudioDecoderControl::handleMediaSourceError(long hr)
{
    m_loadingSource = false;
    m_deferredStart = false;
    if (hr == MF_E_UNSUPPORTED_BYTESTREAM_TYPE) {
        error(QAudioDecoder::FormatError, tr("Unsupported media type"));
    } else if (hr == ERROR_FILE_NOT_FOUND) {
        error(QAudioDecoder::ResourceError, tr("Media not found"));
    } else {
        error(QAudioDecoder::ResourceError, tr("Unable to load specified URL")
                      + QString::fromStdString(std::system_category().message(hr)));
    }
}

void MFAudioDecoderControl::startReadingSource(IMFMediaSource *source)
{
    Q_ASSERT(source);

    m_decoderSourceReader.reset(new MFDecoderSourceReader());
    if (!m_decoderSourceReader) {
        error(QAudioDecoder::ResourceError, tr("Could not instantiate MFDecoderSourceReader"));
        return;
    }

    auto mediaType = m_decoderSourceReader->setSource(source, m_outputFormat.sampleFormat());
    QAudioFormat mediaFormat = QWindowsAudioUtils::mediaTypeToFormat(mediaType.get());
    if (!mediaFormat.isValid()) {
        error(QAudioDecoder::FormatError, tr("Invalid media format"));
        m_decoderSourceReader.reset();
        return;
    }

    QWindowsIUPointer<IMFPresentationDescriptor> pd;
    if (SUCCEEDED(source->CreatePresentationDescriptor(pd.address()))) {
        UINT64 duration = 0;
        pd->GetUINT64(MF_PD_DURATION, &duration);
        duration /= 10000;
        m_duration = qint64(duration);
        durationChanged(m_duration);
    }

    if (!m_resampler.setup(mediaFormat, m_outputFormat.isValid() ? m_outputFormat : mediaFormat)) {
        qWarning() << "Failed to set up resampler";
        return;
    }

    connect(m_decoderSourceReader.get(), &MFDecoderSourceReader::finished, this, &MFAudioDecoderControl::handleSourceFinished);
    connect(m_decoderSourceReader.get(), &MFDecoderSourceReader::newSample, this, &MFAudioDecoderControl::handleNewSample);

    setIsDecoding(true);

    m_decoderSourceReader->readNextSample();
}

void MFAudioDecoderControl::start()
{
    if (isDecoding())
        return;

    if (m_loadingSource) {
        m_deferredStart = true;
    } else {
        IMFMediaSource *source = m_sourceResolver->mediaSource();
        if (!source) {
            if (m_device)
                error(QAudioDecoder::ResourceError, tr("Unable to read from specified device"));
            else if (m_source.isValid())
                error(QAudioDecoder::ResourceError, tr("Unable to load specified URL"));
            else
                error(QAudioDecoder::ResourceError, tr("No media source specified"));
            return;
        } else {
            startReadingSource(source);
        }
    }
}

void MFAudioDecoderControl::stop()
{
    m_deferredStart = false;
    if (!isDecoding())
        return;

    disconnect(m_decoderSourceReader.get());
    m_decoderSourceReader->clearSource();
    m_decoderSourceReader.reset();

    if (bufferAvailable()) {
        QAudioBuffer buffer;
        m_audioBuffer.swap(buffer);
        emit bufferAvailableChanged(false);
    }
    setIsDecoding(false);

    if (m_position != -1) {
        m_position = -1;
        positionChanged(m_position);
    }
    if (m_duration != -1) {
        m_duration = -1;
        durationChanged(m_duration);
    }
}

void MFAudioDecoderControl::handleNewSample(QWindowsIUPointer<IMFSample> sample)
{
    Q_ASSERT(sample);

    qint64 sampleStartTimeUs = m_resampler.outputFormat().durationForBytes(m_resampler.totalOutputBytes());
    QByteArray out = m_resampler.resample(sample.get());

    if (out.isEmpty()) {
        error(QAudioDecoder::Error::ResourceError, tr("Failed processing a sample"));

    } else {
        m_audioBuffer = QAudioBuffer(out, m_resampler.outputFormat(), sampleStartTimeUs);

        emit bufferAvailableChanged(true);
        emit bufferReady();
    }
}

void MFAudioDecoderControl::handleSourceFinished()
{
    stop();
    emit finished();
}

void MFAudioDecoderControl::setAudioFormat(const QAudioFormat &format)
{
    if (m_outputFormat == format)
        return;
    m_outputFormat = format;
    emit formatChanged(m_outputFormat);
}

QAudioBuffer MFAudioDecoderControl::read()
{
    QAudioBuffer buffer;

    if (bufferAvailable()) {
        buffer.swap(m_audioBuffer);
        m_position = buffer.startTime() / 1000;
        emit positionChanged(m_position);
        emit bufferAvailableChanged(false);
        m_decoderSourceReader->readNextSample();
    }

    return buffer;
}