Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mfstream.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "mfstream_p.h"
5
6#include <QtCore/qcoreapplication.h>
7
9//MFStream is added for supporting QIODevice type of media source.
10//It is used to delegate invocations from media foundation(through IMFByteStream) to QIODevice.
11
12MFStream::MFStream(QIODevice *stream, bool ownStream)
14 , m_ownStream(ownStream)
15{
16 //Move to the thread of the stream object
17 //to make sure invocations on stream
18 //are happened in the same thread of stream object
19 this->moveToThread(stream->thread());
20}
21
22MFStream::~MFStream()
23{
24 if (m_ownStream)
25 m_stream->deleteLater();
26}
27
28//from IMFByteStream
29STDMETHODIMP MFStream::GetCapabilities(DWORD *pdwCapabilities)
30{
31 if (!pdwCapabilities)
32 return E_INVALIDARG;
33 *pdwCapabilities = MFBYTESTREAM_IS_READABLE;
34 if (!m_stream->isSequential())
35 *pdwCapabilities |= MFBYTESTREAM_IS_SEEKABLE;
36 return S_OK;
37}
38
39STDMETHODIMP MFStream::GetLength(QWORD *pqwLength)
40{
41 if (!pqwLength)
42 return E_INVALIDARG;
43 QMutexLocker locker(&m_mutex);
44 *pqwLength = QWORD(m_stream->size());
45 return S_OK;
46}
47
48STDMETHODIMP MFStream::SetLength(QWORD)
49{
50 return E_NOTIMPL;
51}
52
53STDMETHODIMP MFStream::GetCurrentPosition(QWORD *pqwPosition)
54{
55 if (!pqwPosition)
56 return E_INVALIDARG;
57 QMutexLocker locker(&m_mutex);
58 *pqwPosition = m_stream->pos();
59 return S_OK;
60}
61
62STDMETHODIMP MFStream::SetCurrentPosition(QWORD qwPosition)
63{
64 QMutexLocker locker(&m_mutex);
65 //SetCurrentPosition may happend during the BeginRead/EndRead pair,
66 //refusing to execute SetCurrentPosition during that time seems to be
67 //the simplest workable solution
68 if (m_currentReadResult)
69 return S_FALSE;
70
71 bool seekOK = m_stream->seek(qint64(qwPosition));
72 if (seekOK)
73 return S_OK;
74 else
75 return S_FALSE;
76}
77
78STDMETHODIMP MFStream::IsEndOfStream(BOOL *pfEndOfStream)
79{
80 if (!pfEndOfStream)
81 return E_INVALIDARG;
82 QMutexLocker locker(&m_mutex);
83 *pfEndOfStream = m_stream->atEnd() ? TRUE : FALSE;
84 return S_OK;
85}
86
87STDMETHODIMP MFStream::Read(BYTE *pb, ULONG cb, ULONG *pcbRead)
88{
89 QMutexLocker locker(&m_mutex);
90 qint64 read = m_stream->read((char*)(pb), qint64(cb));
91 if (pcbRead)
92 *pcbRead = ULONG(read);
93 return S_OK;
94}
95
96STDMETHODIMP MFStream::BeginRead(BYTE *pb, ULONG cb, IMFAsyncCallback *pCallback,
97 IUnknown *punkState)
98{
99 if (!pCallback || !pb)
100 return E_INVALIDARG;
101
102 Q_ASSERT(m_currentReadResult == nullptr);
103
104 ComPtr<AsyncReadState> state = makeComObject<AsyncReadState>(pb, cb);
105
106 HRESULT hr = MFCreateAsyncResult(state.Get(), pCallback, punkState, &m_currentReadResult);
107 if (FAILED(hr))
108 return hr;
109
110 QCoreApplication::postEvent(this, new QEvent(QEvent::User));
111 return hr;
112}
113
114STDMETHODIMP MFStream::EndRead(IMFAsyncResult* pResult, ULONG *pcbRead)
115{
116 if (!pcbRead)
117 return E_INVALIDARG;
118
119 ComPtr<IUnknown> pUnk;
120 pResult->GetObject(&pUnk);
121 auto *state = static_cast<AsyncReadState *>(pUnk.Get());
122 *pcbRead = state->bytesRead();
123
124 m_currentReadResult = nullptr;
125
126 return S_OK;
127}
128
129STDMETHODIMP MFStream::Write(const BYTE *, ULONG, ULONG *)
130{
131 return E_NOTIMPL;
132}
133
134STDMETHODIMP MFStream::BeginWrite(const BYTE *, ULONG ,
135 IMFAsyncCallback *,
136 IUnknown *)
137{
138 return E_NOTIMPL;
139}
140
141STDMETHODIMP MFStream::EndWrite(IMFAsyncResult *,
142 ULONG *)
143{
144 return E_NOTIMPL;
145}
146
147STDMETHODIMP MFStream::Seek(
148 MFBYTESTREAM_SEEK_ORIGIN SeekOrigin,
149 LONGLONG llSeekOffset,
150 DWORD,
151 QWORD *pqwCurrentPosition)
152{
153 QMutexLocker locker(&m_mutex);
154 if (m_currentReadResult)
155 return S_FALSE;
156
157 qint64 pos = qint64(llSeekOffset);
158 switch (SeekOrigin) {
159 case msoBegin:
160 break;
161 case msoCurrent:
162 pos += m_stream->pos();
163 break;
164 }
165 bool seekOK = m_stream->seek(pos);
166 if (pqwCurrentPosition)
167 *pqwCurrentPosition = pos;
168 if (seekOK)
169 return S_OK;
170 else
171 return S_FALSE;
172}
173
174STDMETHODIMP MFStream::Flush()
175{
176 return E_NOTIMPL;
177}
178
179STDMETHODIMP MFStream::Close()
180{
181 QMutexLocker locker(&m_mutex);
182 if (m_ownStream)
183 m_stream->close();
184 return S_OK;
185}
186
187void MFStream::doRead()
188{
189 if (!m_stream)
190 return;
191
192 bool readDone = true;
193 ComPtr<IUnknown> pUnk;
194 HRESULT hr = m_currentReadResult->GetObject(&pUnk);
195 if (SUCCEEDED(hr)) {
196 //do actual read
197 auto *state = static_cast<AsyncReadState *>(pUnk.Get());
198 ULONG cbRead;
199 Read(state->pb(), state->cb() - state->bytesRead(), &cbRead);
200
201 state->setBytesRead(cbRead + state->bytesRead());
202 if (state->cb() > state->bytesRead() && !m_stream->atEnd()) {
203 readDone = false;
204 }
205 }
206
207 if (readDone) {
208 //now inform the original caller
209 m_currentReadResult->SetStatus(hr);
210 MFInvokeCallback(m_currentReadResult.Get());
211 }
212}
213
214void MFStream::customEvent(QEvent *event)
215{
216 if (event->type() != QEvent::User) {
217 QObject::customEvent(event);
218 return;
219 }
220 doRead();
221}
222
223//AsyncReadState is a helper class used in BeginRead for asynchronous operation
224//to record some BeginRead parameters, so these parameters could be
225//used later when actually executing the read operation in another thread.
226MFStream::AsyncReadState::AsyncReadState(BYTE *pb, ULONG cb)
227 : m_pb(pb)
228 , m_cb(cb)
229 , m_cbRead(0)
230{
231}
232
233
234BYTE* MFStream::AsyncReadState::pb() const
235{
236 return m_pb;
237}
238
239ULONG MFStream::AsyncReadState::cb() const
240{
241 return m_cb;
242}
243
244ULONG MFStream::AsyncReadState::bytesRead() const
245{
246 return m_cbRead;
247}
248
249void MFStream::AsyncReadState::setBytesRead(ULONG cbRead)
250{
251 m_cbRead = cbRead;
252}
253
254QT_END_NAMESPACE
255
256#include "moc_mfstream_p.cpp"
\inmodule QtCore \reentrant
Definition qiodevice.h:38
Combined button and popup list for selecting options.