Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mfdecodersourcereader.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <decoder/mfdecodersourcereadercallback_p.h>
7
8#include <QtCore/qdebug.h>
9#include <QtCore/qlogging.h>
10
11#include <mfapi.h>
12#include <mferror.h>
13
14#include <system_error>
15
16QT_BEGIN_NAMESPACE
17
18ComPtr<IMFMediaType> MFDecoderSourceReader::setSource(const ComPtr<IMFMediaSource> &source, QAudioFormat::SampleFormat sampleFormat)
19{
20 ComPtr<IMFMediaType> mediaType;
21 m_sourceReader.Reset();
22
23 if (m_sourceReaderCallback) {
24 disconnect(m_sourceReaderCallback.Get());
25
26 m_sourceReaderCallback.Reset();
27 }
28
29 if (!source)
30 return mediaType;
31
32 m_sourceReaderCallback = makeComObject<MFSourceReaderCallback>();
33
34 connect(m_sourceReaderCallback.Get(), &MFSourceReaderCallback::finished, this,
35 &MFDecoderSourceReader::finished);
36 connect(m_sourceReaderCallback.Get(), &MFSourceReaderCallback::newSample, this,
37 &MFDecoderSourceReader::newSample);
38
39 ComPtr<IMFAttributes> attr;
40 MFCreateAttributes(attr.GetAddressOf(), 1);
41 if (FAILED(attr->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, m_sourceReaderCallback.Get())))
42 return mediaType;
43 if (FAILED(attr->SetUINT32(MF_SOURCE_READER_DISCONNECT_MEDIASOURCE_ON_SHUTDOWN, TRUE)))
44 return mediaType;
45
46 HRESULT hr = MFCreateSourceReaderFromMediaSource(source.Get(), attr.Get(), m_sourceReader.GetAddressOf());
47 if (FAILED(hr)) {
48 qWarning() << "MFDecoderSourceReader: failed to set up source reader: "
49 << std::system_category().message(hr).c_str();
50 return mediaType;
51 }
52
53 m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_ALL_STREAMS), FALSE);
54 m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), TRUE);
55
56 ComPtr<IMFMediaType> pPartialType;
57 MFCreateMediaType(pPartialType.GetAddressOf());
58 pPartialType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
59 pPartialType->SetGUID(MF_MT_SUBTYPE, sampleFormat == QAudioFormat::Float ? MFAudioFormat_Float : MFAudioFormat_PCM);
60 m_sourceReader->SetCurrentMediaType(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), nullptr, pPartialType.Get());
61 m_sourceReader->GetCurrentMediaType(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), mediaType.GetAddressOf());
62 // Ensure the stream is selected.
63 m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), TRUE);
64
65 return mediaType;
66}
67
69{
70 if (m_sourceReader)
71 m_sourceReader->ReadSample(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, NULL, NULL, NULL, NULL);
72}
73
74QT_END_NAMESPACE
75
76#include "moc_mfdecodersourcereader_p.cpp"