1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "mfdecodersourcereader.h"
41 
MFDecoderSourceReader(QObject * parent)42 MFDecoderSourceReader::MFDecoderSourceReader(QObject *parent)
43     : m_cRef(1)
44     , m_sourceReader(0)
45     , m_source(0)
46 {
47     Q_UNUSED(parent)
48 }
49 
shutdown()50 void MFDecoderSourceReader::shutdown()
51 {
52     if (m_source) {
53         m_source->Release();
54         m_source = NULL;
55     }
56     if (m_sourceReader) {
57         m_sourceReader->Release();
58         m_sourceReader = NULL;
59     }
60 }
61 
mediaSource()62 IMFMediaSource* MFDecoderSourceReader::mediaSource()
63 {
64     return m_source;
65 }
66 
setSource(IMFMediaSource * source,const QAudioFormat & audioFormat)67 IMFMediaType* MFDecoderSourceReader::setSource(IMFMediaSource *source, const QAudioFormat &audioFormat)
68 {
69     IMFMediaType *mediaType = NULL;
70     if (m_source == source)
71         return mediaType;
72     if (m_source) {
73         m_source->Release();
74         m_source = NULL;
75     }
76     if (m_sourceReader) {
77         m_sourceReader->Release();
78         m_sourceReader = NULL;
79     }
80     if (!source)
81         return mediaType;
82     IMFAttributes *attr = NULL;
83     MFCreateAttributes(&attr, 1);
84     if (SUCCEEDED(attr->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, this))) {
85         if (SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attr, &m_sourceReader))) {
86             m_source = source;
87             m_source->AddRef();
88             m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_ALL_STREAMS), FALSE);
89             m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), TRUE);
90             IMFMediaType *pPartialType = NULL;
91             MFCreateMediaType(&pPartialType);
92             pPartialType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
93 
94             if (audioFormat.sampleType() == QAudioFormat::Float) {
95                 pPartialType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float);
96             } else {
97                 pPartialType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
98             }
99 
100             m_sourceReader->SetCurrentMediaType(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), NULL, pPartialType);
101             pPartialType->Release();
102             m_sourceReader->GetCurrentMediaType(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), &mediaType);
103             // Ensure the stream is selected.
104             m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), TRUE);
105         }
106         attr->Release();
107     }
108     return mediaType;
109 }
110 
reset()111 void MFDecoderSourceReader::reset()
112 {
113     if (!m_sourceReader)
114         return;
115     PROPVARIANT vPos;
116     PropVariantInit(&vPos);
117     vPos.vt = VT_I8;
118     vPos.uhVal.QuadPart = 0;
119     m_sourceReader->SetCurrentPosition(GUID_NULL, vPos);
120 }
121 
readNextSample()122 void MFDecoderSourceReader::readNextSample()
123 {
124     if (!m_sourceReader)
125         return;
126     m_sourceReader->ReadSample(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, NULL, NULL, NULL, NULL);
127 }
128 
takeSamples()129 QList<IMFSample*> MFDecoderSourceReader::takeSamples() //internal samples will be cleared after this
130 {
131     QList<IMFSample*> samples;
132     m_samplesMutex.lock();
133     samples = m_cachedSamples;
134     m_cachedSamples.clear();
135     m_samplesMutex.unlock();
136     return samples;
137 }
138 
139 //from IUnknown
QueryInterface(REFIID riid,LPVOID * ppvObject)140 STDMETHODIMP MFDecoderSourceReader::QueryInterface(REFIID riid, LPVOID *ppvObject)
141 {
142     if (!ppvObject)
143         return E_POINTER;
144     if (riid == IID_IMFSourceReaderCallback) {
145         *ppvObject = static_cast<IMFSourceReaderCallback*>(this);
146     } else if (riid == IID_IUnknown) {
147         *ppvObject = static_cast<IUnknown*>(this);
148     } else {
149         *ppvObject =  NULL;
150         return E_NOINTERFACE;
151     }
152     AddRef();
153     return S_OK;
154 }
155 
STDMETHODIMP_(ULONG)156 STDMETHODIMP_(ULONG) MFDecoderSourceReader::AddRef(void)
157 {
158     return InterlockedIncrement(&m_cRef);
159 }
160 
STDMETHODIMP_(ULONG)161 STDMETHODIMP_(ULONG) MFDecoderSourceReader::Release(void)
162 {
163     LONG cRef = InterlockedDecrement(&m_cRef);
164     if (cRef == 0) {
165         this->deleteLater();
166     }
167     return cRef;
168 }
169 
170 //from IMFSourceReaderCallback
OnReadSample(HRESULT hrStatus,DWORD dwStreamIndex,DWORD dwStreamFlags,LONGLONG llTimestamp,IMFSample * pSample)171 STDMETHODIMP MFDecoderSourceReader::OnReadSample(HRESULT hrStatus, DWORD dwStreamIndex,
172     DWORD dwStreamFlags, LONGLONG llTimestamp, IMFSample *pSample)
173 {
174     Q_UNUSED(hrStatus);
175     Q_UNUSED(dwStreamIndex);
176     Q_UNUSED(llTimestamp);
177     if (pSample) {
178         pSample->AddRef();
179         m_samplesMutex.lock();
180         m_cachedSamples.push_back(pSample);
181         m_samplesMutex.unlock();
182         emit sampleAdded();
183     } else if ((dwStreamFlags & MF_SOURCE_READERF_ENDOFSTREAM) == MF_SOURCE_READERF_ENDOFSTREAM) {
184         emit finished();
185     }
186     return S_OK;
187 }
188 
OnFlush(DWORD)189 STDMETHODIMP MFDecoderSourceReader::OnFlush(DWORD)
190 {
191     return S_OK;
192 }
193 
OnEvent(DWORD,IMFMediaEvent *)194 STDMETHODIMP MFDecoderSourceReader::OnEvent(DWORD, IMFMediaEvent*)
195 {
196     return S_OK;
197 }
198