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 #ifndef DIRECTSHOWIOREADER_H
41 #define DIRECTSHOWIOREADER_H
42 
43 #include <dshow.h>
44 
45 #include <QtCore/qmutex.h>
46 #include <QtCore/qobject.h>
47 #include <QtCore/qwaitcondition.h>
48 
49 QT_BEGIN_NAMESPACE
50 class QIODevice;
51 
52 class DirectShowEventLoop;
53 class DirectShowIOSource;
54 class DirectShowSampleRequest;
55 
56 class DirectShowIOReader : public QObject, public IAsyncReader
57 {
58     Q_OBJECT
59 public:
60     DirectShowIOReader(QIODevice *device, DirectShowIOSource *source, DirectShowEventLoop *loop);
61     ~DirectShowIOReader() override;
62 
63     // IUnknown
64     HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) override;
65     ULONG STDMETHODCALLTYPE AddRef() override;
66     ULONG STDMETHODCALLTYPE Release() override;
67 
68     // IAsyncReader
69     HRESULT STDMETHODCALLTYPE RequestAllocator(
70             IMemAllocator *pPreferred, ALLOCATOR_PROPERTIES *pProps,
71             IMemAllocator **ppActual) override;
72 
73     HRESULT STDMETHODCALLTYPE Request(IMediaSample *pSample, DWORD_PTR dwUser) override;
74 
75     HRESULT STDMETHODCALLTYPE WaitForNext(
76             DWORD dwTimeout, IMediaSample **ppSample, DWORD_PTR *pdwUser) override;
77 
78     HRESULT STDMETHODCALLTYPE SyncReadAligned(IMediaSample *pSample) override;
79 
80     HRESULT STDMETHODCALLTYPE SyncRead(LONGLONG llPosition, LONG lLength, BYTE *pBuffer) override;
81 
82     HRESULT STDMETHODCALLTYPE Length(LONGLONG *pTotal, LONGLONG *pAvailable) override;
83 
84     HRESULT STDMETHODCALLTYPE BeginFlush() override;
85     HRESULT STDMETHODCALLTYPE EndFlush() override;
86 
87 protected:
88     void customEvent(QEvent *event) override;
89 
90 private Q_SLOTS:
91     void readyRead();
92 
93 private:
94     HRESULT blockingRead(LONGLONG position, LONG length, BYTE *buffer, qint64 *bytesRead);
95     bool nonBlockingRead(
96             LONGLONG position, LONG length, BYTE *buffer, qint64 *bytesRead, HRESULT *result);
97     void flushRequests();
98 
99     DirectShowIOSource *m_source;
100     QIODevice *m_device;
101     DirectShowEventLoop *m_loop;
102     DirectShowSampleRequest *m_pendingHead = nullptr;
103     DirectShowSampleRequest *m_pendingTail = nullptr;
104     DirectShowSampleRequest *m_readyHead = nullptr;
105     DirectShowSampleRequest *m_readyTail = nullptr;
106     LONGLONG m_synchronousPosition = 0;
107     LONG m_synchronousLength = 0;
108     qint64 m_synchronousBytesRead = 0;
109     BYTE *m_synchronousBuffer = nullptr;
110     HRESULT m_synchronousResult = S_OK;
111     LONGLONG m_totalLength = 0;
112     LONGLONG m_availableLength = 0;
113     bool m_flushing = false;
114     QMutex m_mutex;
115     QWaitCondition m_wait;
116 };
117 
118 QT_END_NAMESPACE
119 
120 #endif
121