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 "samplegrabber.h"
41 #include "mfaudioprobecontrol.h"
42
QueryInterface(REFIID riid,void ** ppv)43 STDMETHODIMP SampleGrabberCallback::QueryInterface(REFIID riid, void** ppv)
44 {
45 if (!ppv)
46 return E_POINTER;
47 if (riid == IID_IMFSampleGrabberSinkCallback) {
48 *ppv = static_cast<IMFSampleGrabberSinkCallback*>(this);
49 } else if (riid == IID_IMFClockStateSink) {
50 *ppv = static_cast<IMFClockStateSink*>(this);
51 } else if (riid == IID_IUnknown) {
52 *ppv = static_cast<IUnknown*>(this);
53 } else {
54 *ppv = NULL;
55 return E_NOINTERFACE;
56 }
57 AddRef();
58 return S_OK;
59 }
60
STDMETHODIMP_(ULONG)61 STDMETHODIMP_(ULONG) SampleGrabberCallback::AddRef()
62 {
63 return InterlockedIncrement(&m_cRef);
64 }
65
STDMETHODIMP_(ULONG)66 STDMETHODIMP_(ULONG) SampleGrabberCallback::Release()
67 {
68 ULONG cRef = InterlockedDecrement(&m_cRef);
69 if (cRef == 0) {
70 delete this;
71 }
72 return cRef;
73
74 }
75
76 // IMFClockStateSink methods.
77
OnClockStart(MFTIME hnsSystemTime,LONGLONG llClockStartOffset)78 STDMETHODIMP SampleGrabberCallback::OnClockStart(MFTIME hnsSystemTime, LONGLONG llClockStartOffset)
79 {
80 Q_UNUSED(hnsSystemTime);
81 Q_UNUSED(llClockStartOffset);
82 return S_OK;
83 }
84
OnClockStop(MFTIME hnsSystemTime)85 STDMETHODIMP SampleGrabberCallback::OnClockStop(MFTIME hnsSystemTime)
86 {
87 Q_UNUSED(hnsSystemTime);
88 return S_OK;
89 }
90
OnClockPause(MFTIME hnsSystemTime)91 STDMETHODIMP SampleGrabberCallback::OnClockPause(MFTIME hnsSystemTime)
92 {
93 Q_UNUSED(hnsSystemTime);
94 return S_OK;
95 }
96
OnClockRestart(MFTIME hnsSystemTime)97 STDMETHODIMP SampleGrabberCallback::OnClockRestart(MFTIME hnsSystemTime)
98 {
99 Q_UNUSED(hnsSystemTime);
100 return S_OK;
101 }
102
OnClockSetRate(MFTIME hnsSystemTime,float flRate)103 STDMETHODIMP SampleGrabberCallback::OnClockSetRate(MFTIME hnsSystemTime, float flRate)
104 {
105 Q_UNUSED(hnsSystemTime);
106 Q_UNUSED(flRate);
107 return S_OK;
108 }
109
110 // IMFSampleGrabberSink methods.
111
OnSetPresentationClock(IMFPresentationClock * pClock)112 STDMETHODIMP SampleGrabberCallback::OnSetPresentationClock(IMFPresentationClock* pClock)
113 {
114 Q_UNUSED(pClock);
115 return S_OK;
116 }
117
OnShutdown()118 STDMETHODIMP SampleGrabberCallback::OnShutdown()
119 {
120 return S_OK;
121 }
122
addProbe(MFAudioProbeControl * probe)123 void AudioSampleGrabberCallback::addProbe(MFAudioProbeControl* probe)
124 {
125 QMutexLocker locker(&m_audioProbeMutex);
126
127 if (m_audioProbes.contains(probe))
128 return;
129
130 m_audioProbes.append(probe);
131 }
132
removeProbe(MFAudioProbeControl * probe)133 void AudioSampleGrabberCallback::removeProbe(MFAudioProbeControl* probe)
134 {
135 QMutexLocker locker(&m_audioProbeMutex);
136 m_audioProbes.removeOne(probe);
137 }
138
setFormat(const QAudioFormat & format)139 void AudioSampleGrabberCallback::setFormat(const QAudioFormat& format)
140 {
141 m_format = format;
142 }
143
OnProcessSample(REFGUID guidMajorMediaType,DWORD dwSampleFlags,LONGLONG llSampleTime,LONGLONG llSampleDuration,const BYTE * pSampleBuffer,DWORD dwSampleSize)144 STDMETHODIMP AudioSampleGrabberCallback::OnProcessSample(REFGUID guidMajorMediaType, DWORD dwSampleFlags,
145 LONGLONG llSampleTime, LONGLONG llSampleDuration, const BYTE * pSampleBuffer,
146 DWORD dwSampleSize)
147 {
148 Q_UNUSED(dwSampleFlags);
149 Q_UNUSED(llSampleTime);
150 Q_UNUSED(llSampleDuration);
151
152 if (guidMajorMediaType != GUID_NULL && guidMajorMediaType != MFMediaType_Audio)
153 return S_OK;
154
155 QMutexLocker locker(&m_audioProbeMutex);
156
157 if (m_audioProbes.isEmpty())
158 return S_OK;
159
160 // Check if sample has a presentation time
161 if (llSampleTime == _I64_MAX) {
162 // Set default QAudioBuffer start time
163 llSampleTime = -1;
164 } else {
165 // WMF uses 100-nanosecond units, Qt uses microseconds
166 llSampleTime /= 10;
167 }
168
169 for (MFAudioProbeControl* probe : qAsConst(m_audioProbes))
170 probe->bufferProbed((const char*)pSampleBuffer, dwSampleSize, m_format, llSampleTime);
171
172 return S_OK;
173 }
174