From 631b89ddde44dfc8b72b904d8c41368b2c02d037 Mon Sep 17 00:00:00 2001 From: Yoann Lopes Date: Fri, 29 Jan 2016 15:18:33 +0100 Subject: DirectShow: refactor custom renderer filter Refactor out some of the filter and pin logic into the DirectShowBaseFilter and DirectShowPin abstract classes. This will avoid code duplication when implementing the probe filter. The existing source filter (for qrc files) can also be refactored to use these abstract classes. Change-Id: Iaf141472dea82579ad085b3318624f9a57aa46d8 Reviewed-by: Christian Stromme --- .../directshow/helpers/directshowbasefilter.cpp | 267 ++++++++ .../directshow/helpers/directshowbasefilter.h | 93 +++ .../directshow/helpers/directshoweventloop.cpp | 148 +++++ .../directshow/helpers/directshoweventloop.h | 76 +++ src/plugins/directshow/helpers/directshowglobal.h | 150 +++++ .../directshow/helpers/directshowmediatype.cpp | 253 +++++++ .../directshow/helpers/directshowmediatype.h | 85 +++ .../directshow/helpers/directshowmediatypeenum.cpp | 111 ++++ .../directshow/helpers/directshowmediatypeenum.h | 72 ++ .../directshow/helpers/directshowobject.cpp | 83 +++ src/plugins/directshow/helpers/directshowobject.h | 74 +++ src/plugins/directshow/helpers/directshowpin.cpp | 733 +++++++++++++++++++++ src/plugins/directshow/helpers/directshowpin.h | 184 ++++++ .../directshow/helpers/directshowpinenum.cpp | 126 ++++ src/plugins/directshow/helpers/directshowpinenum.h | 79 +++ .../directshow/helpers/directshowvideobuffer.cpp | 84 +++ .../directshow/helpers/directshowvideobuffer.h | 67 ++ src/plugins/directshow/helpers/helpers.pri | 22 + 18 files changed, 2707 insertions(+) create mode 100644 src/plugins/directshow/helpers/directshowbasefilter.cpp create mode 100644 src/plugins/directshow/helpers/directshowbasefilter.h create mode 100644 src/plugins/directshow/helpers/directshoweventloop.cpp create mode 100644 src/plugins/directshow/helpers/directshoweventloop.h create mode 100644 src/plugins/directshow/helpers/directshowglobal.h create mode 100644 src/plugins/directshow/helpers/directshowmediatype.cpp create mode 100644 src/plugins/directshow/helpers/directshowmediatype.h create mode 100644 src/plugins/directshow/helpers/directshowmediatypeenum.cpp create mode 100644 src/plugins/directshow/helpers/directshowmediatypeenum.h create mode 100644 src/plugins/directshow/helpers/directshowobject.cpp create mode 100644 src/plugins/directshow/helpers/directshowobject.h create mode 100644 src/plugins/directshow/helpers/directshowpin.cpp create mode 100644 src/plugins/directshow/helpers/directshowpin.h create mode 100644 src/plugins/directshow/helpers/directshowpinenum.cpp create mode 100644 src/plugins/directshow/helpers/directshowpinenum.h create mode 100644 src/plugins/directshow/helpers/directshowvideobuffer.cpp create mode 100644 src/plugins/directshow/helpers/directshowvideobuffer.h create mode 100644 src/plugins/directshow/helpers/helpers.pri (limited to 'src/plugins/directshow/helpers') diff --git a/src/plugins/directshow/helpers/directshowbasefilter.cpp b/src/plugins/directshow/helpers/directshowbasefilter.cpp new file mode 100644 index 000000000..fbf0f6204 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowbasefilter.cpp @@ -0,0 +1,267 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowbasefilter.h" + +#include "directshowpinenum.h" + +DirectShowBaseFilter::DirectShowBaseFilter() + : m_mutex(QMutex::Recursive) + , m_state(State_Stopped) + , m_graph(NULL) + , m_clock(NULL) + , m_sink(NULL) +{ + +} + +DirectShowBaseFilter::~DirectShowBaseFilter() +{ + if (m_clock) { + m_clock->Release(); + m_clock = NULL; + } +} + +HRESULT DirectShowBaseFilter::getInterface(REFIID riid, void **ppvObject) +{ + if (riid == IID_IPersist + || riid == IID_IMediaFilter + || riid == IID_IBaseFilter) { + return GetInterface(static_cast(this), ppvObject); + } else { + return DirectShowObject::getInterface(riid, ppvObject); + } +} + +HRESULT DirectShowBaseFilter::GetClassID(CLSID *pClassID) +{ + *pClassID = CLSID_NULL; + return S_OK; +} + +HRESULT DirectShowBaseFilter::NotifyEvent(long eventCode, LONG_PTR eventParam1, LONG_PTR eventParam2) +{ + IMediaEventSink *sink = m_sink; + if (sink) { + if (eventCode == EC_COMPLETE) + eventParam2 = (LONG_PTR)(IBaseFilter*)this; + + return sink->Notify(eventCode, eventParam1, eventParam2); + } else { + return E_NOTIMPL; + } +} + +HRESULT DirectShowBaseFilter::Run(REFERENCE_TIME tStart) +{ + Q_UNUSED(tStart) + QMutexLocker locker(&m_mutex); + + m_startTime = tStart; + + if (m_state == State_Stopped){ + HRESULT hr = Pause(); + if (FAILED(hr)) + return hr; + } + + m_state = State_Running; + + return S_OK; +} + +HRESULT DirectShowBaseFilter::Pause() +{ + QMutexLocker locker(&m_mutex); + + if (m_state == State_Stopped) { + const QList pinList = pins(); + for (DirectShowPin *pin : pinList) { + if (pin->isConnected()) { + HRESULT hr = pin->setActive(true); + if (FAILED(hr)) + return hr; + } + } + } + + m_state = State_Paused; + + return S_OK; +} + +HRESULT DirectShowBaseFilter::Stop() +{ + QMutexLocker locker(&m_mutex); + + HRESULT hr = S_OK; + + if (m_state != State_Stopped) { + const QList pinList = pins(); + for (DirectShowPin *pin : pinList) { + if (pin->isConnected()) { + HRESULT hrTmp = pin->setActive(false); + if (FAILED(hrTmp) && SUCCEEDED(hr)) + hr = hrTmp; + } + } + } + + m_state = State_Stopped; + + return hr; +} + +HRESULT DirectShowBaseFilter::GetState(DWORD dwMilliSecsTimeout, FILTER_STATE *pState) +{ + Q_UNUSED(dwMilliSecsTimeout); + + if (!pState) { + return E_POINTER; + } else { + QMutexLocker locker(&m_mutex); + + *pState = m_state; + + return S_OK; + } +} + +HRESULT DirectShowBaseFilter::SetSyncSource(IReferenceClock *pClock) +{ + QMutexLocker locker(&m_mutex); + + if (m_clock) + m_clock->Release(); + + m_clock = pClock; + + if (m_clock) + m_clock->AddRef(); + + return S_OK; +} + +HRESULT DirectShowBaseFilter::GetSyncSource(IReferenceClock **ppClock) +{ + if (!ppClock) { + return E_POINTER; + } else { + if (!m_clock) { + *ppClock = 0; + + return S_FALSE; + } else { + m_clock->AddRef(); + + *ppClock = m_clock; + + return S_OK; + } + } +} + +HRESULT DirectShowBaseFilter::EnumPins(IEnumPins **ppEnum) +{ + if (!ppEnum) { + return E_POINTER; + } else { + *ppEnum = new DirectShowPinEnum(this); + return S_OK; + } +} + +HRESULT DirectShowBaseFilter::FindPin(LPCWSTR Id, IPin **ppPin) +{ + if (!ppPin || !Id) { + return E_POINTER; + } else { + QMutexLocker locker(&m_mutex); + const QList pinList = pins(); + for (DirectShowPin *pin : pinList) { + if (QString::fromWCharArray(Id) == pin->name()) { + pin->AddRef(); + *ppPin = pin; + return S_OK; + } + } + + *ppPin = 0; + return VFW_E_NOT_FOUND; + } +} + +HRESULT DirectShowBaseFilter::JoinFilterGraph(IFilterGraph *pGraph, LPCWSTR pName) +{ + QMutexLocker locker(&m_mutex); + + m_filterName = QString::fromWCharArray(pName); + m_graph = pGraph; + m_sink = NULL; + + if (m_graph) { + if (SUCCEEDED(m_graph->QueryInterface(IID_PPV_ARGS(&m_sink)))) + m_sink->Release(); // we don't keep a reference on it + } + + return S_OK; +} + +HRESULT DirectShowBaseFilter::QueryFilterInfo(FILTER_INFO *pInfo) +{ + if (!pInfo) { + return E_POINTER; + } else { + QString name = m_filterName; + + if (name.length() >= MAX_FILTER_NAME) + name.truncate(MAX_FILTER_NAME - 1); + + int length = name.toWCharArray(pInfo->achName); + pInfo->achName[length] = '\0'; + + if (m_graph) + m_graph->AddRef(); + + pInfo->pGraph = m_graph; + + return S_OK; + } +} + +HRESULT DirectShowBaseFilter::QueryVendorInfo(LPWSTR *pVendorInfo) +{ + Q_UNUSED(pVendorInfo); + return E_NOTIMPL; +} diff --git a/src/plugins/directshow/helpers/directshowbasefilter.h b/src/plugins/directshow/helpers/directshowbasefilter.h new file mode 100644 index 000000000..cc8588044 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowbasefilter.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWBASEFILTER_H +#define DIRECTSHOWBASEFILTER_H + +#include "directshowpin.h" + +QT_USE_NAMESPACE + +class DirectShowBaseFilter : public DirectShowObject + , public IBaseFilter +{ + DIRECTSHOW_OBJECT + +public: + DirectShowBaseFilter(); + virtual ~DirectShowBaseFilter(); + + FILTER_STATE state() const { return m_state; } + HRESULT NotifyEvent(long eventCode, LONG_PTR eventParam1, LONG_PTR eventParam2); + + virtual QList pins() = 0; + + // DirectShowObject + HRESULT getInterface(const IID &riid, void **ppvObject); + + // IPersist + STDMETHODIMP GetClassID(CLSID *pClassID); + + // IMediaFilter + STDMETHODIMP Run(REFERENCE_TIME tStart); + STDMETHODIMP Pause(); + STDMETHODIMP Stop(); + + STDMETHODIMP GetState(DWORD dwMilliSecsTimeout, FILTER_STATE *pState); + + STDMETHODIMP SetSyncSource(IReferenceClock *pClock); + STDMETHODIMP GetSyncSource(IReferenceClock **ppClock); + + // IBaseFilter + STDMETHODIMP EnumPins(IEnumPins **ppEnum); + STDMETHODIMP FindPin(LPCWSTR Id, IPin **ppPin); + + STDMETHODIMP JoinFilterGraph(IFilterGraph *pGraph, LPCWSTR pName); + + STDMETHODIMP QueryFilterInfo(FILTER_INFO *pInfo); + STDMETHODIMP QueryVendorInfo(LPWSTR *pVendorInfo); + +protected: + QMutex m_mutex; + FILTER_STATE m_state; + IFilterGraph *m_graph; + IReferenceClock *m_clock; + IMediaEventSink *m_sink; + QString m_filterName; + REFERENCE_TIME m_startTime; + +private: + Q_DISABLE_COPY(DirectShowBaseFilter) +}; + +#endif // DIRECTSHOWBASEFILTER_H diff --git a/src/plugins/directshow/helpers/directshoweventloop.cpp b/src/plugins/directshow/helpers/directshoweventloop.cpp new file mode 100644 index 000000000..87f969e42 --- /dev/null +++ b/src/plugins/directshow/helpers/directshoweventloop.cpp @@ -0,0 +1,148 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include +#include + +class DirectShowPostedEvent +{ +public: + DirectShowPostedEvent(QObject *receiver, QEvent *event) + : receiver(receiver) + , event(event) + , next(0) + { + } + + ~DirectShowPostedEvent() + { + delete event; + } + + QObject *receiver; + QEvent *event; + DirectShowPostedEvent *next; +}; + +DirectShowEventLoop::DirectShowEventLoop(QObject *parent) + : QObject(parent) + , m_postsHead(0) + , m_postsTail(0) + , m_eventHandle(::CreateEvent(0, 0, 0, 0)) + , m_waitHandle(::CreateEvent(0, 0, 0, 0)) +{ +} + +DirectShowEventLoop::~DirectShowEventLoop() +{ + ::CloseHandle(m_eventHandle); + ::CloseHandle(m_waitHandle); + + for (DirectShowPostedEvent *post = m_postsHead; post; post = m_postsHead) { + m_postsHead = m_postsHead->next; + + delete post; + } +} + +void DirectShowEventLoop::wait(QMutex *mutex) +{ + ::ResetEvent(m_waitHandle); + + mutex->unlock(); + + HANDLE handles[] = { m_eventHandle, m_waitHandle }; + while (::WaitForMultipleObjects(2, handles, false, INFINITE) == WAIT_OBJECT_0) + processEvents(); + + mutex->lock(); +} + +void DirectShowEventLoop::wake() +{ + ::SetEvent(m_waitHandle); +} + +void DirectShowEventLoop::postEvent(QObject *receiver, QEvent *event) +{ + QMutexLocker locker(&m_mutex); + + DirectShowPostedEvent *post = new DirectShowPostedEvent(receiver, event); + + if (m_postsTail) + m_postsTail->next = post; + else + m_postsHead = post; + + m_postsTail = post; + + QCoreApplication::postEvent(this, new QEvent(QEvent::User)); + ::SetEvent(m_eventHandle); +} + +void DirectShowEventLoop::customEvent(QEvent *event) +{ + if (event->type() == QEvent::User) { + processEvents(); + } else { + QObject::customEvent(event); + } +} + +void DirectShowEventLoop::processEvents() +{ + QMutexLocker locker(&m_mutex); + + ::ResetEvent(m_eventHandle); + + while(m_postsHead) { + DirectShowPostedEvent *post = m_postsHead; + m_postsHead = m_postsHead->next; + + if (!m_postsHead) + m_postsTail = 0; + + locker.unlock(); + QCoreApplication::sendEvent(post->receiver, post->event); + delete post; + locker.relock(); + } +} diff --git a/src/plugins/directshow/helpers/directshoweventloop.h b/src/plugins/directshow/helpers/directshoweventloop.h new file mode 100644 index 000000000..09d986de7 --- /dev/null +++ b/src/plugins/directshow/helpers/directshoweventloop.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWEVENTLOOP_H +#define DIRECTSHOWEVENTLOOP_H + +#include +#include +#include + +#include + +class DirectShowPostedEvent; + +class DirectShowEventLoop : public QObject +{ + Q_OBJECT +public: + DirectShowEventLoop(QObject *parent = 0); + ~DirectShowEventLoop(); + + void wait(QMutex *mutex); + void wake(); + + void postEvent(QObject *object, QEvent *event); + +protected: + void customEvent(QEvent *event); + +private: + void processEvents(); + + DirectShowPostedEvent *m_postsHead; + DirectShowPostedEvent *m_postsTail; + HANDLE m_eventHandle; + HANDLE m_waitHandle; + QMutex m_mutex; +}; + +#endif diff --git a/src/plugins/directshow/helpers/directshowglobal.h b/src/plugins/directshow/helpers/directshowglobal.h new file mode 100644 index 000000000..f7890c52b --- /dev/null +++ b/src/plugins/directshow/helpers/directshowglobal.h @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWGLOBAL_H +#define DIRECTSHOWGLOBAL_H + +#include + +#include + +template T *com_cast(IUnknown *unknown, const IID &iid) +{ + T *iface = 0; + return unknown && unknown->QueryInterface(iid, reinterpret_cast(&iface)) == S_OK + ? iface + : 0; +} + +template T *com_new(const IID &clsid) +{ + T *object = 0; + return CoCreateInstance( + clsid, + NULL, + CLSCTX_INPROC_SERVER, + IID_PPV_ARGS(&object)) == S_OK + ? object + : 0; +} + +template T *com_new(const IID &clsid, const IID &iid) +{ + T *object = 0; + return CoCreateInstance( + clsid, + NULL, + CLSCTX_INPROC_SERVER, + iid, + reinterpret_cast(&object)) == S_OK + ? object + : 0; +} + +#ifndef __IFilterGraph2_INTERFACE_DEFINED__ +#define __IFilterGraph2_INTERFACE_DEFINED__ +#define INTERFACE IFilterGraph2 +DECLARE_INTERFACE_(IFilterGraph2 ,IGraphBuilder) +{ + STDMETHOD(AddSourceFilterForMoniker)(THIS_ IMoniker *, IBindCtx *, LPCWSTR,IBaseFilter **) PURE; + STDMETHOD(ReconnectEx)(THIS_ IPin *, const AM_MEDIA_TYPE *) PURE; + STDMETHOD(RenderEx)(IPin *, DWORD, DWORD *) PURE; +}; +#undef INTERFACE +#endif + +#ifndef __IAMFilterMiscFlags_INTERFACE_DEFINED__ +#define __IAMFilterMiscFlags_INTERFACE_DEFINED__ +#define INTERFACE IAMFilterMiscFlags +DECLARE_INTERFACE_(IAMFilterMiscFlags ,IUnknown) +{ + STDMETHOD(QueryInterface)(THIS_ REFIID,PVOID*) PURE; + STDMETHOD_(ULONG,AddRef)(THIS) PURE; + STDMETHOD_(ULONG,Release)(THIS) PURE; + STDMETHOD_(ULONG,GetMiscFlags)(THIS) PURE; +}; +#undef INTERFACE +#endif + +#ifndef __IFileSourceFilter_INTERFACE_DEFINED__ +#define __IFileSourceFilter_INTERFACE_DEFINED__ +#define INTERFACE IFileSourceFilter +DECLARE_INTERFACE_(IFileSourceFilter ,IUnknown) +{ + STDMETHOD(QueryInterface)(THIS_ REFIID,PVOID*) PURE; + STDMETHOD_(ULONG,AddRef)(THIS) PURE; + STDMETHOD_(ULONG,Release)(THIS) PURE; + STDMETHOD(Load)(THIS_ LPCOLESTR, const AM_MEDIA_TYPE *) PURE; + STDMETHOD(GetCurFile)(THIS_ LPOLESTR *ppszFileName, AM_MEDIA_TYPE *) PURE; +}; +#undef INTERFACE +#endif + +#ifndef __IAMOpenProgress_INTERFACE_DEFINED__ +#define __IAMOpenProgress_INTERFACE_DEFINED__ +#undef INTERFACE +#define INTERFACE IAMOpenProgress +DECLARE_INTERFACE_(IAMOpenProgress ,IUnknown) +{ + STDMETHOD(QueryInterface)(THIS_ REFIID,PVOID*) PURE; + STDMETHOD_(ULONG,AddRef)(THIS) PURE; + STDMETHOD_(ULONG,Release)(THIS) PURE; + STDMETHOD(QueryProgress)(THIS_ LONGLONG *, LONGLONG *) PURE; + STDMETHOD(AbortOperation)(THIS) PURE; +}; +#undef INTERFACE +#endif + +#ifndef __IFilterChain_INTERFACE_DEFINED__ +#define __IFilterChain_INTERFACE_DEFINED__ +#define INTERFACE IFilterChain +DECLARE_INTERFACE_(IFilterChain ,IUnknown) +{ + STDMETHOD(QueryInterface)(THIS_ REFIID,PVOID*) PURE; + STDMETHOD_(ULONG,AddRef)(THIS) PURE; + STDMETHOD_(ULONG,Release)(THIS) PURE; + STDMETHOD(StartChain)(IBaseFilter *, IBaseFilter *) PURE; + STDMETHOD(PauseChain)(IBaseFilter *, IBaseFilter *) PURE; + STDMETHOD(StopChain)(IBaseFilter *, IBaseFilter *) PURE; + STDMETHOD(RemoveChain)(IBaseFilter *, IBaseFilter *) PURE; +}; +#undef INTERFACE +#endif + +#endif diff --git a/src/plugins/directshow/helpers/directshowmediatype.cpp b/src/plugins/directshow/helpers/directshowmediatype.cpp new file mode 100644 index 000000000..60c0ee040 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowmediatype.cpp @@ -0,0 +1,253 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowmediatype.h" + +namespace +{ + struct TypeLookup + { + QVideoFrame::PixelFormat pixelFormat; + GUID mediaType; + }; + + static const TypeLookup qt_typeLookup[] = + { + { QVideoFrame::Format_RGB32, /*MEDIASUBTYPE_RGB32*/ {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}} }, + { QVideoFrame::Format_BGR24, /*MEDIASUBTYPE_RGB24*/ {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}} }, + { QVideoFrame::Format_RGB565, /*MEDIASUBTYPE_RGB565*/ {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}} }, + { QVideoFrame::Format_RGB555, /*MEDIASUBTYPE_RGB555*/ {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}} }, + { QVideoFrame::Format_AYUV444, /*MEDIASUBTYPE_AYUV*/ {0x56555941, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_YUYV, /*MEDIASUBTYPE_YUY2*/ {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_UYVY, /*MEDIASUBTYPE_UYVY*/ {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_IMC1, /*MEDIASUBTYPE_IMC1*/ {0x31434D49, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_IMC2, /*MEDIASUBTYPE_IMC2*/ {0x32434D49, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_IMC3, /*MEDIASUBTYPE_IMC3*/ {0x33434D49, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_IMC4, /*MEDIASUBTYPE_IMC4*/ {0x34434D49, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_YV12, /*MEDIASUBTYPE_YV12*/ {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_NV12, /*MEDIASUBTYPE_NV12*/ {0x3231564E, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_YUV420P, /*MEDIASUBTYPE_IYUV*/ {0x56555949, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} }, + { QVideoFrame::Format_YUV420P, /*MEDIASUBTYPE_I420*/ {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} } + }; +} + +bool DirectShowMediaType::isPartiallySpecified() const +{ + return majortype == GUID_NULL || formattype == GUID_NULL; +} + +bool DirectShowMediaType::isCompatibleWith(const DirectShowMediaType *type) const +{ + if (type->majortype != GUID_NULL && majortype != type->majortype) + return false; + + if (type->subtype != GUID_NULL && subtype != type->subtype) + return false; + + if (type->formattype != GUID_NULL) { + if (formattype != type->formattype) + return false; + if (cbFormat != type->cbFormat) + return false; + if (cbFormat != 0 && memcmp(pbFormat, type->pbFormat, cbFormat) != 0) + return false; + } + + return true; +} + +void DirectShowMediaType::init(AM_MEDIA_TYPE *type) +{ + ZeroMemory((PVOID)type, sizeof(*type)); + type->lSampleSize = 1; + type->bFixedSizeSamples = TRUE; +} + +void DirectShowMediaType::copy(AM_MEDIA_TYPE *target, const AM_MEDIA_TYPE &source) +{ + if (!target) + return; + + *target = source; + + if (source.cbFormat > 0) { + target->pbFormat = reinterpret_cast(CoTaskMemAlloc(source.cbFormat)); + memcpy(target->pbFormat, source.pbFormat, source.cbFormat); + } + if (target->pUnk) + target->pUnk->AddRef(); +} + +void DirectShowMediaType::deleteType(AM_MEDIA_TYPE *type) +{ + freeData(type); + + CoTaskMemFree(type); +} + +void DirectShowMediaType::freeData(AM_MEDIA_TYPE *type) +{ + if (type->cbFormat > 0) + CoTaskMemFree(type->pbFormat); + + if (type->pUnk) + type->pUnk->Release(); +} + + +GUID DirectShowMediaType::convertPixelFormat(QVideoFrame::PixelFormat format) +{ + const int count = sizeof(qt_typeLookup) / sizeof(TypeLookup); + + for (int i = 0; i < count; ++i) + if (qt_typeLookup[i].pixelFormat == format) + return qt_typeLookup[i].mediaType; + + return MEDIASUBTYPE_None; +} + +QVideoSurfaceFormat DirectShowMediaType::formatFromType(const AM_MEDIA_TYPE &type) +{ + const int count = sizeof(qt_typeLookup) / sizeof(TypeLookup); + + for (int i = 0; i < count; ++i) { + if (IsEqualGUID(qt_typeLookup[i].mediaType, type.subtype) && type.cbFormat > 0) { + if (IsEqualGUID(type.formattype, FORMAT_VideoInfo)) { + VIDEOINFOHEADER *header = reinterpret_cast(type.pbFormat); + + QVideoSurfaceFormat format( + QSize(header->bmiHeader.biWidth, qAbs(header->bmiHeader.biHeight)), + qt_typeLookup[i].pixelFormat); + + if (header->AvgTimePerFrame > 0) + format.setFrameRate(10000 /header->AvgTimePerFrame); + + format.setScanLineDirection(scanLineDirection(format.pixelFormat(), header->bmiHeader)); + + return format; + } else if (IsEqualGUID(type.formattype, FORMAT_VideoInfo2)) { + VIDEOINFOHEADER2 *header = reinterpret_cast(type.pbFormat); + + QVideoSurfaceFormat format( + QSize(header->bmiHeader.biWidth, qAbs(header->bmiHeader.biHeight)), + qt_typeLookup[i].pixelFormat); + + if (header->AvgTimePerFrame > 0) + format.setFrameRate(10000 / header->AvgTimePerFrame); + + format.setScanLineDirection(scanLineDirection(format.pixelFormat(), header->bmiHeader)); + + return format; + } + } + } + return QVideoSurfaceFormat(); +} + +QVideoFrame::PixelFormat DirectShowMediaType::pixelFormatFromType(const AM_MEDIA_TYPE &type) +{ + const int count = sizeof(qt_typeLookup) / sizeof(TypeLookup); + + for (int i = 0; i < count; ++i) { + if (IsEqualGUID(qt_typeLookup[i].mediaType, type.subtype)) { + return qt_typeLookup[i].pixelFormat; + } + } + + return QVideoFrame::Format_Invalid; +} + +#define PAD_TO_DWORD(x) (((x) + 3) & ~3) +int DirectShowMediaType::bytesPerLine(const QVideoSurfaceFormat &format) +{ + switch (format.pixelFormat()) { + // 32 bpp packed formats. + case QVideoFrame::Format_RGB32: + case QVideoFrame::Format_AYUV444: + return format.frameWidth() * 4; + // 24 bpp packed formats. + case QVideoFrame::Format_RGB24: + return PAD_TO_DWORD(format.frameWidth() * 3); + // 16 bpp packed formats. + case QVideoFrame::Format_RGB565: + case QVideoFrame::Format_RGB555: + case QVideoFrame::Format_YUYV: + case QVideoFrame::Format_UYVY: + return PAD_TO_DWORD(format.frameWidth() * 2); + // Planar formats. + case QVideoFrame::Format_YV12: + case QVideoFrame::Format_YUV420P: + case QVideoFrame::Format_IMC1: + case QVideoFrame::Format_IMC2: + case QVideoFrame::Format_IMC3: + case QVideoFrame::Format_IMC4: + case QVideoFrame::Format_NV12: + return format.frameWidth(); + default: + return 0; + } +} + +QVideoSurfaceFormat::Direction DirectShowMediaType::scanLineDirection(QVideoFrame::PixelFormat pixelFormat, const BITMAPINFOHEADER &bmiHeader) +{ + /* MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/dd318229(v=vs.85).aspx */ + /* For uncompressed RGB bitmaps: + * if biHeight is positive, the bitmap is a bottom-up DIB with the origin at the lower left corner. + * If biHeight is negative, the bitmap is a top-down DIB with the origin at the upper left corner. + * + * For YUV bitmaps: + * the bitmap is always top-down, regardless of the sign of biHeight. + * Decoders should offer YUV formats with postive biHeight, but for backward compatibility they should accept YUV formats with either positive or negative biHeight. + * + * For compressed formats: + * biHeight must be positive, regardless of image orientation. + */ + switch (pixelFormat) + { + case QVideoFrame::Format_RGB32: + case QVideoFrame::Format_BGR24: + case QVideoFrame::Format_RGB565: + case QVideoFrame::Format_RGB555: + return bmiHeader.biHeight < 0 + ? QVideoSurfaceFormat::TopToBottom + : QVideoSurfaceFormat::BottomToTop; + default: + return QVideoSurfaceFormat::TopToBottom; + } +} diff --git a/src/plugins/directshow/helpers/directshowmediatype.h b/src/plugins/directshow/helpers/directshowmediatype.h new file mode 100644 index 000000000..b2b074ccc --- /dev/null +++ b/src/plugins/directshow/helpers/directshowmediatype.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWMEDIATYPE_H +#define DIRECTSHOWMEDIATYPE_H + +#include + +#include + +#include + +QT_USE_NAMESPACE + +class DirectShowMediaType : public AM_MEDIA_TYPE +{ +public: + DirectShowMediaType() { init(this); } + DirectShowMediaType(const AM_MEDIA_TYPE &type) { copy(this, type); } + DirectShowMediaType(const DirectShowMediaType &other) { copy(this, other); } + DirectShowMediaType &operator =(const AM_MEDIA_TYPE &type) { + freeData(this); copy(this, type); return *this; } + DirectShowMediaType &operator =(const DirectShowMediaType &other) { + freeData(this); copy(this, other); return *this; } + ~DirectShowMediaType() { freeData(this); } + + void clear() { freeData(this); init(this); } + + bool isPartiallySpecified() const; + bool isCompatibleWith(const DirectShowMediaType *type) const; + + static void init(AM_MEDIA_TYPE *type); + static void copy(AM_MEDIA_TYPE *target, const AM_MEDIA_TYPE &source); + static void freeData(AM_MEDIA_TYPE *type); + static void deleteType(AM_MEDIA_TYPE *type); + + static GUID convertPixelFormat(QVideoFrame::PixelFormat format); + static QVideoSurfaceFormat formatFromType(const AM_MEDIA_TYPE &type); + static QVideoFrame::PixelFormat pixelFormatFromType(const AM_MEDIA_TYPE &type); + + static int bytesPerLine(const QVideoSurfaceFormat &format); + +private: + static QVideoSurfaceFormat::Direction scanLineDirection(QVideoFrame::PixelFormat pixelFormat, const BITMAPINFOHEADER &bmiHeader); +}; + +Q_DECLARE_TYPEINFO(DirectShowMediaType, Q_MOVABLE_TYPE); + +#endif diff --git a/src/plugins/directshow/helpers/directshowmediatypeenum.cpp b/src/plugins/directshow/helpers/directshowmediatypeenum.cpp new file mode 100644 index 000000000..a1c8b2306 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowmediatypeenum.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowmediatypeenum.h" + +#include "directshowpin.h" + +DirectShowMediaTypeEnum::DirectShowMediaTypeEnum(DirectShowPin *pin) + : m_pin(pin) + , m_mediaTypes(pin->supportedMediaTypes()) + , m_index(0) +{ + m_pin->AddRef(); +} + +DirectShowMediaTypeEnum::DirectShowMediaTypeEnum(const QList &types) + : m_pin(NULL) + , m_mediaTypes(types) + , m_index(0) +{ +} + +DirectShowMediaTypeEnum::~DirectShowMediaTypeEnum() +{ + if (m_pin) + m_pin->Release(); +} + +HRESULT DirectShowMediaTypeEnum::getInterface(REFIID riid, void **ppvObject) +{ + if (riid == IID_IEnumMediaTypes) { + return GetInterface(static_cast(this), ppvObject); + } else { + return DirectShowObject::getInterface(riid, ppvObject); + } +} + +HRESULT DirectShowMediaTypeEnum::Next(ULONG cMediaTypes, AM_MEDIA_TYPE **ppMediaTypes, ULONG *pcFetched) +{ + if (ppMediaTypes && (pcFetched || cMediaTypes == 1)) { + ULONG count = qBound(0, cMediaTypes, m_mediaTypes.count() - m_index); + + for (ULONG i = 0; i < count; ++i, ++m_index) { + ppMediaTypes[i] = reinterpret_cast(CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE))); + DirectShowMediaType::copy(ppMediaTypes[i], m_mediaTypes.at(m_index)); + } + + if (pcFetched) + *pcFetched = count; + + return count == cMediaTypes ? S_OK : S_FALSE; + } else { + return E_POINTER; + } +} + +HRESULT DirectShowMediaTypeEnum::Skip(ULONG cMediaTypes) +{ + m_index = qMin(int(m_index + cMediaTypes), m_mediaTypes.count()); + return m_index < m_mediaTypes.count() ? S_OK : S_FALSE; +} + +HRESULT DirectShowMediaTypeEnum::Reset() +{ + m_index = 0; + return S_OK; +} + +HRESULT DirectShowMediaTypeEnum::Clone(IEnumMediaTypes **ppEnum) +{ + if (ppEnum) { + if (m_pin) + *ppEnum = new DirectShowMediaTypeEnum(m_pin); + else + *ppEnum = new DirectShowMediaTypeEnum(m_mediaTypes); + return S_OK; + } else { + return E_POINTER; + } +} + diff --git a/src/plugins/directshow/helpers/directshowmediatypeenum.h b/src/plugins/directshow/helpers/directshowmediatypeenum.h new file mode 100644 index 000000000..050df0881 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowmediatypeenum.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWMEDIATYPEENUM_H +#define DIRECTSHOWMEDIATYPEENUM_H + +#include "directshowobject.h" +#include + +QT_USE_NAMESPACE + +class DirectShowPin; +class DirectShowMediaType; + +class DirectShowMediaTypeEnum : public DirectShowObject + , public IEnumMediaTypes +{ + DIRECTSHOW_OBJECT + +public: + DirectShowMediaTypeEnum(DirectShowPin *pin); + DirectShowMediaTypeEnum(const QList &types); + ~DirectShowMediaTypeEnum(); + + // DirectShowObject + HRESULT getInterface(REFIID riid, void **ppvObject); + + // IEnumMediaTypes + STDMETHODIMP Next(ULONG cMediaTypes, AM_MEDIA_TYPE **ppMediaTypes, ULONG *pcFetched); + STDMETHODIMP Skip(ULONG cMediaTypes); + STDMETHODIMP Reset(); + STDMETHODIMP Clone(IEnumMediaTypes **ppEnum); + +private: + Q_DISABLE_COPY(DirectShowMediaTypeEnum) + + DirectShowPin *m_pin; + QList m_mediaTypes; + int m_index; +}; + +#endif // DIRECTSHOWMEDIATYPEENUM_H diff --git a/src/plugins/directshow/helpers/directshowobject.cpp b/src/plugins/directshow/helpers/directshowobject.cpp new file mode 100644 index 000000000..b9d989f6e --- /dev/null +++ b/src/plugins/directshow/helpers/directshowobject.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowobject.h" + +DirectShowObject::DirectShowObject() + : m_ref(1) +{ +} + +DirectShowObject::~DirectShowObject() +{ + Q_ASSERT(m_ref == 0); +} + +HRESULT DirectShowObject::getInterface(const IID &riid, void **ppvObject) +{ + Q_UNUSED(riid) + *ppvObject = NULL; + return E_NOINTERFACE; +} + +ULONG DirectShowObject::ref() +{ + return InterlockedIncrement(&m_ref); +} + +ULONG DirectShowObject::unref() +{ + ULONG ref = InterlockedDecrement(&m_ref); + if (ref == 0) + delete this; + + return ref; +} + +HRESULT GetInterface(IUnknown *pUnk, void **ppv) +{ + if (!ppv) + return E_POINTER; + + *ppv = pUnk; + pUnk->AddRef(); + + return S_OK; +} + diff --git a/src/plugins/directshow/helpers/directshowobject.h b/src/plugins/directshow/helpers/directshowobject.h new file mode 100644 index 000000000..3aba06f46 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowobject.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWOBJECT_H +#define DIRECTSHOWOBJECT_H + +#include "directshowglobal.h" + +QT_USE_NAMESPACE + +class DirectShowObject +{ +public: + DirectShowObject(); + virtual ~DirectShowObject(); + + virtual HRESULT getInterface(REFIID riid, void **ppvObject); + ULONG ref(); + ULONG unref(); + +private: + Q_DISABLE_COPY(DirectShowObject) + + volatile LONG m_ref; +}; + +HRESULT GetInterface(IUnknown *pUnk, void **ppv); + +#define DIRECTSHOW_OBJECT \ +public: \ + STDMETHODIMP QueryInterface(REFIID riid, void **ppv) { \ + if (riid == IID_IUnknown) \ + return GetInterface(reinterpret_cast(this), ppv); \ + else \ + return getInterface(riid, ppv); \ + }; \ + STDMETHODIMP_(ULONG) AddRef() { \ + return ref(); \ + }; \ + STDMETHODIMP_(ULONG) Release() { \ + return unref(); \ + }; + +#endif // DIRECTSHOWOBJECT_H diff --git a/src/plugins/directshow/helpers/directshowpin.cpp b/src/plugins/directshow/helpers/directshowpin.cpp new file mode 100644 index 000000000..6cf4da321 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowpin.cpp @@ -0,0 +1,733 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowpin.h" + +#include "directshowmediatype.h" +#include "directshowbasefilter.h" +#include "directshowmediatypeenum.h" + +#include + +DirectShowPin::DirectShowPin(DirectShowBaseFilter *filter, const QString &name, PIN_DIRECTION direction) + : m_mutex(QMutex::Recursive) + , m_filter(filter) + , m_name(name) + , m_direction(direction) + , m_peerPin(NULL) +{ +} + +DirectShowPin::~DirectShowPin() +{ + +} + +HRESULT DirectShowPin::getInterface(const IID &riid, void **ppvObject) +{ + if (riid == IID_IPin) + return GetInterface(static_cast(this), ppvObject); + else + return DirectShowObject::getInterface(riid, ppvObject); +} + + + +HRESULT DirectShowPin::Connect(IPin *pReceivePin, const AM_MEDIA_TYPE *pmt) +{ + if (!pReceivePin) + return E_POINTER; + + HRESULT hr = E_FAIL; + QMutexLocker locker(&m_mutex); + + if (m_peerPin) + return VFW_E_ALREADY_CONNECTED; + if (m_filter->state() != State_Stopped) + return VFW_E_NOT_STOPPED; + + PIN_DIRECTION pd; + pReceivePin->QueryDirection(&pd); + if (pd == m_direction) + return VFW_E_INVALID_DIRECTION; + + const DirectShowMediaType *type = reinterpret_cast(pmt); + + if (type != NULL && !type->isPartiallySpecified()) { + // If the type is fully specified, use it + hr = tryConnect(pReceivePin, type); + } else { + IEnumMediaTypes *enumMediaTypes = NULL; + + // First, try the receiving pin's preferred types + if (SUCCEEDED(pReceivePin->EnumMediaTypes(&enumMediaTypes))) { + hr = tryMediaTypes(pReceivePin, type, enumMediaTypes); + enumMediaTypes->Release(); + } + // Then, try this pin's preferred types + if (FAILED(hr) && SUCCEEDED(EnumMediaTypes(&enumMediaTypes))) { + hr = tryMediaTypes(pReceivePin, type, enumMediaTypes); + enumMediaTypes->Release(); + } + } + + if (FAILED(hr)) { + return ((hr != E_FAIL) && (hr != E_INVALIDARG) && (hr != VFW_E_TYPE_NOT_ACCEPTED)) + ? hr : VFW_E_NO_ACCEPTABLE_TYPES; + } + + return S_OK; +} + +HRESULT DirectShowPin::tryMediaTypes(IPin *pin, const DirectShowMediaType *partialType, IEnumMediaTypes *enumMediaTypes) +{ + HRESULT hr = enumMediaTypes->Reset(); + if (FAILED(hr)) + return hr; + + DirectShowMediaType *mediaType = NULL; + ULONG mediaCount = 0; + HRESULT hrFailure = VFW_E_NO_ACCEPTABLE_TYPES; + + for (; enumMediaTypes->Next(1, reinterpret_cast(&mediaType), &mediaCount) == S_OK;) { + + if (mediaType && (partialType == NULL || mediaType->isCompatibleWith(partialType))) { + hr = tryConnect(pin, mediaType); + + if (FAILED(hr) && (hr != E_FAIL) + && (hr != E_INVALIDARG) + && (hr != VFW_E_TYPE_NOT_ACCEPTED)) { + hrFailure = hr; + } + } + + if (mediaType) + DirectShowMediaType::deleteType(mediaType); + + if (SUCCEEDED(hr)) + return S_OK; + } + + return hrFailure; +} + +HRESULT DirectShowPin::tryConnect(IPin *pin, const DirectShowMediaType *type) +{ + if (!isMediaTypeSupported(type)) + return VFW_E_TYPE_NOT_ACCEPTED; + + m_peerPin = pin; + m_peerPin->AddRef(); + + HRESULT hr; + if (!setMediaType(type)) { + hr = VFW_E_TYPE_NOT_ACCEPTED; + } else { + hr = pin->ReceiveConnection(this, type); + if (SUCCEEDED(hr)) { + hr = completeConnection(pin); + if (FAILED(hr)) + pin->Disconnect(); + } + } + + if (FAILED(hr)) { + connectionEnded(); + m_peerPin->Release(); + m_peerPin = NULL; + setMediaType(NULL); + return hr; + } + + return S_OK; +} + +HRESULT DirectShowPin::ReceiveConnection(IPin *pConnector, const AM_MEDIA_TYPE *pmt) +{ + if (!pConnector || !pmt) + return E_POINTER; + + QMutexLocker locker(&m_mutex); + + if (m_peerPin) + return VFW_E_ALREADY_CONNECTED; + if (m_filter->state() != State_Stopped) + return VFW_E_NOT_STOPPED; + + PIN_DIRECTION pd; + pConnector->QueryDirection(&pd); + if (pd == m_direction) + return VFW_E_INVALID_DIRECTION; + + const DirectShowMediaType *type = reinterpret_cast(pmt); + if (!isMediaTypeSupported(type)) + return VFW_E_TYPE_NOT_ACCEPTED; + + m_peerPin = pConnector; + m_peerPin->AddRef(); + + HRESULT hr; + if (!setMediaType(type)) + hr = VFW_E_TYPE_NOT_ACCEPTED; + else + hr = completeConnection(pConnector); + + if (FAILED(hr)) { + connectionEnded(); + m_peerPin->Release(); + m_peerPin = NULL; + setMediaType(NULL); + return hr; + } + + return S_OK; +} + +HRESULT DirectShowPin::Disconnect() +{ + QMutexLocker locker(&m_mutex); + + if (m_filter->state() != State_Stopped) + return VFW_E_NOT_STOPPED; + + if (m_peerPin) { + HRESULT hr = connectionEnded(); + if (FAILED(hr)) + return hr; + + m_peerPin->Release(); + m_peerPin = NULL; + + setMediaType(NULL); + + return S_OK; + } + + return S_FALSE; +} + +HRESULT DirectShowPin::ConnectedTo(IPin **ppPin) +{ + if (!ppPin) { + return E_POINTER; + } else { + QMutexLocker locker(&m_mutex); + + if (!m_peerPin) { + *ppPin = 0; + return VFW_E_NOT_CONNECTED; + } else { + m_peerPin->AddRef(); + *ppPin = m_peerPin; + return S_OK; + } + } +} + +HRESULT DirectShowPin::ConnectionMediaType(AM_MEDIA_TYPE *pmt) +{ + if (!pmt) { + return E_POINTER; + } else { + QMutexLocker locker(&m_mutex); + + if (!m_peerPin) { + DirectShowMediaType::init(pmt); + return VFW_E_NOT_CONNECTED; + } else { + DirectShowMediaType::copy(pmt, m_mediaType); + return S_OK; + } + } +} + +HRESULT DirectShowPin::QueryPinInfo(PIN_INFO *pInfo) +{ + if (!pInfo) { + return E_POINTER; + } else { + pInfo->pFilter = m_filter; + if (m_filter) { + m_filter->AddRef(); + } + pInfo->dir = m_direction; + + QString name = m_name; + if (name.length() >= MAX_PIN_NAME) + name.truncate(MAX_PIN_NAME - 1); + int length = name.toWCharArray(pInfo->achName); + pInfo->achName[length] = '\0'; + + return S_OK; + } +} + +HRESULT DirectShowPin::QueryId(LPWSTR *Id) +{ + if (!Id) { + return E_POINTER; + } else { + const int bytes = (m_name.length() + 1) * 2; + *Id = static_cast(::CoTaskMemAlloc(bytes)); + ::memcpy(*Id, m_name.utf16(), bytes); + return S_OK; + } +} + +HRESULT DirectShowPin::QueryAccept(const AM_MEDIA_TYPE *pmt) +{ + if (!pmt) + return E_POINTER; + + if (!isMediaTypeSupported(reinterpret_cast(pmt))) + return S_FALSE; + + return S_OK; +} + +HRESULT DirectShowPin::EnumMediaTypes(IEnumMediaTypes **ppEnum) +{ + if (!ppEnum) { + return E_POINTER; + } else { + *ppEnum = new DirectShowMediaTypeEnum(this); + return S_OK; + } +} + +HRESULT DirectShowPin::QueryInternalConnections(IPin **apPin, ULONG *nPin) +{ + Q_UNUSED(apPin); + Q_UNUSED(nPin); + return E_NOTIMPL; +} + +HRESULT DirectShowPin::EndOfStream() +{ + return S_OK; +} + +HRESULT DirectShowPin::BeginFlush() +{ + return E_UNEXPECTED; +} + +HRESULT DirectShowPin::EndFlush() +{ + return E_UNEXPECTED; +} + +HRESULT DirectShowPin::NewSegment(REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate) +{ + Q_UNUSED(tStart); + Q_UNUSED(tStop); + Q_UNUSED(dRate); + return S_OK; +} + +HRESULT DirectShowPin::QueryDirection(PIN_DIRECTION *pPinDir) +{ + if (!pPinDir) { + return E_POINTER; + } else { + *pPinDir = m_direction; + return S_OK; + } +} + +QList DirectShowPin::supportedMediaTypes() +{ + return QList(); +} + +bool DirectShowPin::setMediaType(const DirectShowMediaType *type) +{ + if (!type) + m_mediaType.clear(); + else + m_mediaType = *type; + + return true; +} + +HRESULT DirectShowPin::completeConnection(IPin *pin) +{ + Q_UNUSED(pin) + return S_OK; +} + +HRESULT DirectShowPin::connectionEnded() +{ + return S_OK; +} + +HRESULT DirectShowPin::setActive(bool active) +{ + Q_UNUSED(active) + return S_OK; +} + + +/* DirectShowOutputPin */ + +DirectShowOutputPin::DirectShowOutputPin(DirectShowBaseFilter *filter, const QString &name) + : DirectShowPin(filter, name, PINDIR_OUTPUT) + , m_allocator(NULL) + , m_inputPin(NULL) +{ + +} + +DirectShowOutputPin::~DirectShowOutputPin() +{ +} + +HRESULT DirectShowOutputPin::completeConnection(IPin *pin) +{ + if (!pin) + return E_POINTER; + + Q_ASSERT(m_inputPin == NULL); + Q_ASSERT(m_allocator == NULL); + + HRESULT hr = pin->QueryInterface(IID_PPV_ARGS(&m_inputPin)); + if (FAILED(hr)) + return hr; + + ALLOCATOR_PROPERTIES prop; + ZeroMemory(&prop, sizeof(prop)); + m_inputPin->GetAllocatorRequirements(&prop); + if (prop.cBuffers <= 0) + prop.cBuffers = 1; + if (prop.cbBuffer <= 0) + prop.cbBuffer = 1; + if (prop.cbAlign <= 0) + prop.cbAlign = 1; + + // Use the connected input pin's allocator if it has one + hr = m_inputPin->GetAllocator(&m_allocator); + if (SUCCEEDED(hr)) { + ALLOCATOR_PROPERTIES actualProperties; + hr = m_allocator->SetProperties(&prop, &actualProperties); + + if (SUCCEEDED(hr)) { + hr = m_inputPin->NotifyAllocator(m_allocator, FALSE); + if (SUCCEEDED(hr)) + return S_OK; + } + + m_allocator->Release(); + m_allocator = NULL; + } + + // Otherwise, allocate its own allocator + m_allocator = com_new(CLSID_MemoryAllocator); + if (!m_allocator) { + hr = E_OUTOFMEMORY; + } else { + ALLOCATOR_PROPERTIES actualProperties; + hr = m_allocator->SetProperties(&prop, &actualProperties); + + if (SUCCEEDED(hr)) { + hr = m_inputPin->NotifyAllocator(m_allocator, FALSE); + if (SUCCEEDED(hr)) + return S_OK; + } + + m_allocator->Release(); + m_allocator = NULL; + } + + return hr; +} + +HRESULT DirectShowOutputPin::connectionEnded() +{ + if (m_allocator) { + HRESULT hr = m_allocator->Decommit(); + if (FAILED(hr)) + return hr; + + m_allocator->Release(); + m_allocator = NULL; + } + + if (m_inputPin) { + m_inputPin->Release(); + m_inputPin = NULL; + } + + return S_OK; +} + +HRESULT DirectShowOutputPin::setActive(bool active) +{ + if (!m_allocator) + return VFW_E_NO_ALLOCATOR; + + return active ? m_allocator->Commit() + : m_allocator->Decommit(); +} + +HRESULT DirectShowOutputPin::EndOfStream() +{ + return E_UNEXPECTED; +} + + +/* DirectShowInputPin */ + +DirectShowInputPin::DirectShowInputPin(DirectShowBaseFilter *filter, const QString &name) + : DirectShowPin(filter, name, PINDIR_INPUT) + , m_allocator(NULL) + , m_flushing(false) + , m_inErrorState(false) +{ + ZeroMemory(&m_sampleProperties, sizeof(m_sampleProperties)); +} + +DirectShowInputPin::~DirectShowInputPin() +{ + +} + +HRESULT DirectShowInputPin::getInterface(const IID &riid, void **ppvObject) +{ + if (riid == IID_IMemInputPin) + return GetInterface(static_cast(this), ppvObject); + else + return DirectShowPin::getInterface(riid, ppvObject); +} + +HRESULT DirectShowInputPin::connectionEnded() +{ + if (m_allocator) { + HRESULT hr = m_allocator->Decommit(); + if (FAILED(hr)) + return hr; + + m_allocator->Release(); + m_allocator = NULL; + } + + return S_OK; +} + +HRESULT DirectShowInputPin::setActive(bool active) +{ + if (!active) { + m_inErrorState = false; + + if (!m_allocator) + return VFW_E_NO_ALLOCATOR; + + m_flushing = false; + return m_allocator->Decommit(); + } + + return S_OK; +} + +HRESULT DirectShowInputPin::EndOfStream() +{ + if (m_filter->state() == State_Stopped) + return VFW_E_WRONG_STATE; + if (m_flushing) + return S_FALSE; + if (m_inErrorState) + return VFW_E_RUNTIME_ERROR; + + return S_OK; +} + +HRESULT DirectShowInputPin::BeginFlush() +{ + QMutexLocker locker(&m_mutex); + m_flushing = true; + return S_OK; +} + +HRESULT DirectShowInputPin::EndFlush() +{ + QMutexLocker locker(&m_mutex); + m_flushing = false; + m_inErrorState = false; + return S_OK; +} + +HRESULT DirectShowInputPin::GetAllocator(IMemAllocator **ppAllocator) +{ + if (!ppAllocator) + return E_POINTER; + + QMutexLocker locker(&m_mutex); + + if (!m_allocator) { + m_allocator = com_new(CLSID_MemoryAllocator);; + if (!m_allocator) + return E_OUTOFMEMORY; + } + + *ppAllocator = m_allocator; + m_allocator->AddRef(); + + return S_OK; +} + +HRESULT DirectShowInputPin::NotifyAllocator(IMemAllocator *pAllocator, BOOL bReadOnly) +{ + Q_UNUSED(bReadOnly) + + if (!pAllocator) + return E_POINTER; + + QMutexLocker locker(&m_mutex); + + if (m_allocator) + m_allocator->Release(); + + m_allocator = pAllocator; + m_allocator->AddRef(); + + return S_OK; +} + +HRESULT DirectShowInputPin::GetAllocatorRequirements(ALLOCATOR_PROPERTIES *pProps) +{ + Q_UNUSED(pProps) + return E_NOTIMPL; +} + +HRESULT DirectShowInputPin::Receive(IMediaSample *pSample) +{ + if (!pSample) + return E_POINTER; + if (m_filter->state() == State_Stopped) + return VFW_E_WRONG_STATE; + if (m_flushing) + return S_FALSE; + if (m_inErrorState) + return VFW_E_RUNTIME_ERROR; + + HRESULT hr = S_OK; + + IMediaSample2 *sample2; + if (SUCCEEDED(pSample->QueryInterface(IID_PPV_ARGS(&sample2)))) { + hr = sample2->GetProperties(sizeof(m_sampleProperties), (PBYTE)&m_sampleProperties); + sample2->Release(); + if (FAILED(hr)) + return hr; + } else { + m_sampleProperties.cbData = sizeof(m_sampleProperties); + m_sampleProperties.dwTypeSpecificFlags = 0; + m_sampleProperties.dwStreamId = AM_STREAM_MEDIA; + m_sampleProperties.dwSampleFlags = 0; + if (pSample->IsDiscontinuity() == S_OK) + m_sampleProperties.dwSampleFlags |= AM_SAMPLE_DATADISCONTINUITY; + if (pSample->IsPreroll() == S_OK) + m_sampleProperties.dwSampleFlags |= AM_SAMPLE_PREROLL; + if (pSample->IsSyncPoint() == S_OK) + m_sampleProperties.dwSampleFlags |= AM_SAMPLE_SPLICEPOINT; + if (SUCCEEDED(pSample->GetTime(&m_sampleProperties.tStart, + &m_sampleProperties.tStop))) { + m_sampleProperties.dwSampleFlags |= AM_SAMPLE_TIMEVALID | AM_SAMPLE_STOPVALID; + } + if (pSample->GetMediaType(&m_sampleProperties.pMediaType) == S_OK) + m_sampleProperties.dwSampleFlags |= AM_SAMPLE_TYPECHANGED; + pSample->GetPointer(&m_sampleProperties.pbBuffer); + m_sampleProperties.lActual = pSample->GetActualDataLength(); + m_sampleProperties.cbBuffer = pSample->GetSize(); + } + + + if (!(m_sampleProperties.dwSampleFlags & AM_SAMPLE_TYPECHANGED)) + return S_OK; + + if (isMediaTypeSupported(reinterpret_cast(m_sampleProperties.pMediaType))) + return S_OK; + + m_inErrorState = true; + EndOfStream(); + m_filter->NotifyEvent(EC_ERRORABORT, VFW_E_TYPE_NOT_ACCEPTED, 0); + return VFW_E_INVALIDMEDIATYPE; +} + +HRESULT DirectShowInputPin::ReceiveMultiple(IMediaSample **pSamples, long nSamples, long *nSamplesProcessed) +{ + if (!pSamples || !nSamplesProcessed) + return E_POINTER; + + HRESULT hr = S_OK; + *nSamplesProcessed = 0; + while (nSamples-- > 0) { + hr = Receive(pSamples[*nSamplesProcessed]); + if (hr != S_OK) + break; + (*nSamplesProcessed)++; + } + return hr; +} + +HRESULT DirectShowInputPin::ReceiveCanBlock() +{ + int outputPins = 0; + + const QList pinList = m_filter->pins(); + for (DirectShowPin *pin : pinList) { + PIN_DIRECTION pd; + HRESULT hr = pin->QueryDirection(&pd); + if (FAILED(hr)) + return hr; + + if (pd == PINDIR_OUTPUT) { + IPin *connected; + hr = pin->ConnectedTo(&connected); + if (SUCCEEDED(hr)) { + ++outputPins; + IMemInputPin *inputPin; + hr = connected->QueryInterface(IID_PPV_ARGS(&inputPin)); + connected->Release(); + if (SUCCEEDED(hr)) { + hr = inputPin->ReceiveCanBlock(); + inputPin->Release(); + if (hr != S_FALSE) + return S_OK; + } else { + return S_OK; + } + } + } + } + + return outputPins == 0 ? S_OK : S_FALSE; +} diff --git a/src/plugins/directshow/helpers/directshowpin.h b/src/plugins/directshow/helpers/directshowpin.h new file mode 100644 index 000000000..823223956 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowpin.h @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWPIN_H +#define DIRECTSHOWPIN_H + +#include "directshowobject.h" + +#include "directshowmediatype.h" +#include +#include + +QT_USE_NAMESPACE + +class DirectShowBaseFilter; + +class DirectShowPin : public DirectShowObject + , public IPin +{ + DIRECTSHOW_OBJECT + +public: + virtual ~DirectShowPin(); + + QString name() const { return m_name; } + bool isConnected() const { return m_peerPin != NULL; } + + virtual bool isMediaTypeSupported(const DirectShowMediaType *type) = 0; + virtual QList supportedMediaTypes(); + virtual bool setMediaType(const DirectShowMediaType *type); + + virtual HRESULT completeConnection(IPin *pin); + virtual HRESULT connectionEnded(); + + virtual HRESULT setActive(bool active); + + // DirectShowObject + HRESULT getInterface(REFIID riid, void **ppvObject); + + // IPin + STDMETHODIMP Connect(IPin *pReceivePin, const AM_MEDIA_TYPE *pmt); + STDMETHODIMP ReceiveConnection(IPin *pConnector, const AM_MEDIA_TYPE *pmt); + STDMETHODIMP Disconnect(); + STDMETHODIMP ConnectedTo(IPin **ppPin); + + STDMETHODIMP ConnectionMediaType(AM_MEDIA_TYPE *pmt); + + STDMETHODIMP QueryPinInfo(PIN_INFO *pInfo); + STDMETHODIMP QueryId(LPWSTR *Id); + + STDMETHODIMP QueryAccept(const AM_MEDIA_TYPE *pmt); + + STDMETHODIMP EnumMediaTypes(IEnumMediaTypes **ppEnum); + + STDMETHODIMP QueryInternalConnections(IPin **apPin, ULONG *nPin); + + STDMETHODIMP EndOfStream(); + + STDMETHODIMP BeginFlush(); + STDMETHODIMP EndFlush(); + + STDMETHODIMP NewSegment(REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate); + + STDMETHODIMP QueryDirection(PIN_DIRECTION *pPinDir); + +protected: + DirectShowPin(DirectShowBaseFilter *filter, const QString &name, PIN_DIRECTION direction); + + QMutex m_mutex; + + DirectShowBaseFilter *m_filter; + QString m_name; + PIN_DIRECTION m_direction; + + IPin *m_peerPin; + DirectShowMediaType m_mediaType; + +private: + Q_DISABLE_COPY(DirectShowPin) + HRESULT tryMediaTypes(IPin *pin, const DirectShowMediaType *type, IEnumMediaTypes *enumMediaTypes); + HRESULT tryConnect(IPin *pin, const DirectShowMediaType *type); +}; + + +class DirectShowOutputPin : public DirectShowPin +{ + DIRECTSHOW_OBJECT + +public: + virtual ~DirectShowOutputPin(); + + // DirectShowPin + virtual HRESULT completeConnection(IPin *pin); + virtual HRESULT connectionEnded(); + virtual HRESULT setActive(bool active); + + // IPin + STDMETHODIMP EndOfStream(); + +protected: + DirectShowOutputPin(DirectShowBaseFilter *filter, const QString &name); + + IMemAllocator *m_allocator; + IMemInputPin *m_inputPin; + +private: + Q_DISABLE_COPY(DirectShowOutputPin) +}; + + +class DirectShowInputPin : public DirectShowPin + , public IMemInputPin +{ + DIRECTSHOW_OBJECT + +public: + virtual ~DirectShowInputPin(); + + const AM_SAMPLE2_PROPERTIES *currentSampleProperties() const { return &m_sampleProperties; } + + // DirectShowObject + HRESULT getInterface(REFIID riid, void **ppvObject); + + // DirectShowPin + HRESULT connectionEnded(); + HRESULT setActive(bool active); + + // IPin + STDMETHODIMP EndOfStream(); + STDMETHODIMP BeginFlush(); + STDMETHODIMP EndFlush(); + + // IMemInputPin + STDMETHODIMP GetAllocator(IMemAllocator **ppAllocator); + STDMETHODIMP NotifyAllocator(IMemAllocator *pAllocator, BOOL bReadOnly); + STDMETHODIMP GetAllocatorRequirements(ALLOCATOR_PROPERTIES *pProps); + + STDMETHODIMP Receive(IMediaSample *pSample); + STDMETHODIMP ReceiveMultiple(IMediaSample **pSamples, long nSamples, long *nSamplesProcessed); + STDMETHODIMP ReceiveCanBlock(); + +protected: + DirectShowInputPin(DirectShowBaseFilter *filter, const QString &name); + + IMemAllocator *m_allocator; + bool m_flushing; + bool m_inErrorState; + AM_SAMPLE2_PROPERTIES m_sampleProperties; + +private: + Q_DISABLE_COPY(DirectShowInputPin) +}; + +#endif // DIRECTSHOWPIN_H diff --git a/src/plugins/directshow/helpers/directshowpinenum.cpp b/src/plugins/directshow/helpers/directshowpinenum.cpp new file mode 100644 index 000000000..61aca8b9c --- /dev/null +++ b/src/plugins/directshow/helpers/directshowpinenum.cpp @@ -0,0 +1,126 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowpinenum.h" +#include "directshowbasefilter.h" + +DirectShowPinEnum::DirectShowPinEnum(DirectShowBaseFilter *filter) + : m_filter(filter) + , m_index(0) +{ + m_filter->AddRef(); + const QList pinList = filter->pins(); + for (DirectShowPin *pin : pinList) { + pin->AddRef(); + m_pins.append(pin); + } +} + +DirectShowPinEnum::DirectShowPinEnum(const QList &pins) + : m_filter(NULL) + , m_pins(pins) + , m_index(0) +{ + for (IPin *pin : qAsConst(m_pins)) + pin->AddRef(); +} + +DirectShowPinEnum::~DirectShowPinEnum() +{ + for (IPin *pin : qAsConst(m_pins)) + pin->Release(); + if (m_filter) + m_filter->Release(); +} + +HRESULT DirectShowPinEnum::getInterface(REFIID riid, void **ppvObject) +{ + if (riid == IID_IEnumPins) { + return GetInterface(static_cast(this), ppvObject); + } else { + return DirectShowObject::getInterface(riid, ppvObject); + } +} + +HRESULT DirectShowPinEnum::Next(ULONG cPins, IPin **ppPins, ULONG *pcFetched) +{ + if (ppPins && (pcFetched || cPins == 1)) { + ULONG count = qBound(0, cPins, m_pins.count() - m_index); + + for (ULONG i = 0; i < count; ++i, ++m_index) { + ppPins[i] = m_pins.at(m_index); + ppPins[i]->AddRef(); + } + + if (pcFetched) + *pcFetched = count; + + return count == cPins ? S_OK : S_FALSE; + } else { + return E_POINTER; + } +} + +HRESULT DirectShowPinEnum::Skip(ULONG cPins) +{ + m_index = qMin(int(m_index + cPins), m_pins.count()); + + return m_index < m_pins.count() ? S_OK : S_FALSE; +} + +HRESULT DirectShowPinEnum::Reset() +{ + m_index = 0; + + return S_OK; +} + +HRESULT DirectShowPinEnum::Clone(IEnumPins **ppEnum) +{ + if (ppEnum) { + if (m_filter) + *ppEnum = new DirectShowPinEnum(m_filter); + else + *ppEnum = new DirectShowPinEnum(m_pins); + + return S_OK; + } else { + return E_POINTER; + } +} diff --git a/src/plugins/directshow/helpers/directshowpinenum.h b/src/plugins/directshow/helpers/directshowpinenum.h new file mode 100644 index 000000000..84c2a7579 --- /dev/null +++ b/src/plugins/directshow/helpers/directshowpinenum.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWPINENUM_H +#define DIRECTSHOWPINENUM_H + +#include + +#include +#include "directshowpin.h" + +QT_USE_NAMESPACE + +class DirectShowBaseFilter; + +class DirectShowPinEnum : public DirectShowObject + , public IEnumPins +{ + DIRECTSHOW_OBJECT + +public: + DirectShowPinEnum(DirectShowBaseFilter *filter); + DirectShowPinEnum(const QList &pins); + ~DirectShowPinEnum(); + + // DirectShowObject + HRESULT getInterface(REFIID riid, void **ppvObject); + + // IEnumPins + STDMETHODIMP Next(ULONG cPins, IPin **ppPins, ULONG *pcFetched); + STDMETHODIMP Skip(ULONG cPins); + STDMETHODIMP Reset(); + STDMETHODIMP Clone(IEnumPins **ppEnum); + +private: + Q_DISABLE_COPY(DirectShowPinEnum) + + DirectShowBaseFilter *m_filter; + QList m_pins; + int m_index; +}; + +#endif diff --git a/src/plugins/directshow/helpers/directshowvideobuffer.cpp b/src/plugins/directshow/helpers/directshowvideobuffer.cpp new file mode 100644 index 000000000..3204e9f7e --- /dev/null +++ b/src/plugins/directshow/helpers/directshowvideobuffer.cpp @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowvideobuffer.h" + +DirectShowVideoBuffer::DirectShowVideoBuffer(IMediaSample *sample, int bytesPerLine) + : QAbstractVideoBuffer(NoHandle) + , m_sample(sample) + , m_bytesPerLine(bytesPerLine) + , m_mapMode(NotMapped) +{ + m_sample->AddRef(); +} + +DirectShowVideoBuffer::~DirectShowVideoBuffer() +{ + m_sample->Release(); +} + +uchar *DirectShowVideoBuffer::map(MapMode mode, int *numBytes, int *bytesPerLine) +{ + if (m_mapMode == NotMapped && mode != NotMapped) { + if (numBytes) + *numBytes = m_sample->GetActualDataLength(); + + if (bytesPerLine) + *bytesPerLine = m_bytesPerLine; + + BYTE *bytes = 0; + + if (m_sample->GetPointer(&bytes) == S_OK) { + m_mapMode = mode; + + return reinterpret_cast(bytes); + } + } + return 0; +} + +void DirectShowVideoBuffer::unmap() +{ + m_mapMode = NotMapped; +} + +QAbstractVideoBuffer::MapMode DirectShowVideoBuffer::mapMode() const +{ + return m_mapMode; +} diff --git a/src/plugins/directshow/helpers/directshowvideobuffer.h b/src/plugins/directshow/helpers/directshowvideobuffer.h new file mode 100644 index 000000000..10089c75a --- /dev/null +++ b/src/plugins/directshow/helpers/directshowvideobuffer.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWVIDEOBUFFER_H +#define DIRECTSHOWVIDEOBUFFER_H + +#include + +#include + +class DirectShowVideoBuffer : public QAbstractVideoBuffer +{ +public: + DirectShowVideoBuffer(IMediaSample *sample, int bytesPerLine); + ~DirectShowVideoBuffer(); + + IMediaSample *sample() { return m_sample; } + + uchar *map(MapMode mode, int *numBytes, int *bytesPerLine); + void unmap(); + + MapMode mapMode() const; + +private: + IMediaSample *m_sample; + int m_bytesPerLine; + MapMode m_mapMode; +}; + + +#endif diff --git a/src/plugins/directshow/helpers/helpers.pri b/src/plugins/directshow/helpers/helpers.pri new file mode 100644 index 000000000..b3743a680 --- /dev/null +++ b/src/plugins/directshow/helpers/helpers.pri @@ -0,0 +1,22 @@ +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/directshowbasefilter.h \ + $$PWD/directshoweventloop.h \ + $$PWD/directshowglobal.h \ + $$PWD/directshowmediatype.h \ + $$PWD/directshowmediatypeenum.h \ + $$PWD/directshowobject.h \ + $$PWD/directshowpin.h \ + $$PWD/directshowpinenum.h \ + $$PWD/directshowvideobuffer.h + +SOURCES += \ + $$PWD/directshowbasefilter.cpp \ + $$PWD/directshoweventloop.cpp \ + $$PWD/directshowmediatype.cpp \ + $$PWD/directshowmediatypeenum.cpp \ + $$PWD/directshowobject.cpp \ + $$PWD/directshowpin.cpp \ + $$PWD/directshowpinenum.cpp \ + $$PWD/directshowvideobuffer.cpp -- cgit v1.2.3