1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/* This file is part of the KDE project.
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 or 3 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "qasyncreader.h"
#include "qbasefilter.h"
QT_BEGIN_NAMESPACE
namespace Phonon
{
namespace DS9
{
QAsyncReader::QAsyncReader(QBaseFilter *parent, const QVector<AM_MEDIA_TYPE> &mediaTypes) : QPin(parent, PINDIR_OUTPUT, mediaTypes)
{
}
QAsyncReader::~QAsyncReader()
{
}
STDMETHODIMP QAsyncReader::QueryInterface(REFIID iid, void **out)
{
if (!out) {
return E_POINTER;
}
if (iid == IID_IAsyncReader) {
AddRef();
*out = static_cast<IAsyncReader*>(this);
return S_OK;
}
return QPin::QueryInterface(iid, out);
}
STDMETHODIMP_(ULONG) QAsyncReader::AddRef()
{
return QPin::AddRef();
}
STDMETHODIMP_(ULONG) QAsyncReader::Release()
{
return QPin::Release();
}
STDMETHODIMP QAsyncReader::RequestAllocator(IMemAllocator *preferred, ALLOCATOR_PROPERTIES *prop,IMemAllocator **actual)
{
ALLOCATOR_PROPERTIES prop2;
if (prop->cbAlign == 0) {
prop->cbAlign = 1; //align on 1 char
}
if (preferred && preferred->SetProperties(prop, &prop2) == S_OK) {
preferred->AddRef();
*actual = preferred;
return S_OK;
}
//we should try to create one memory allocator ourselves here
return E_FAIL;
}
STDMETHODIMP QAsyncReader::Request(IMediaSample *sample,DWORD_PTR user)
{
QMutexLocker locker(&m_mutex);
if (m_flushing) {
return VFW_E_WRONG_STATE;
}
m_requestQueue.enqueue(AsyncRequest(sample, user));
m_requestWait.wakeOne();
return S_OK;
}
STDMETHODIMP QAsyncReader::WaitForNext(DWORD timeout, IMediaSample **sample, DWORD_PTR *user)
{
QMutexLocker locker(&m_mutex);
if (!sample ||!user) {
return E_POINTER;
}
//msdn says to return immediately if we're flushing but that doesn't seem to be true
//since it triggers a dead-lock somewhere inside directshow (see task 258830)
*sample = 0;
*user = 0;
if (m_requestQueue.isEmpty()) {
if (m_requestWait.wait(&m_mutex, timeout) == false) {
return VFW_E_TIMEOUT;
}
if (m_requestQueue.isEmpty()) {
return VFW_E_WRONG_STATE;
}
}
AsyncRequest r = m_requestQueue.dequeue();
//at this point we're sure to have a request to proceed
if (r.sample == 0) {
return E_FAIL;
}
*sample = r.sample;
*user = r.user;
return syncReadAlignedUnlocked(r.sample);
}
STDMETHODIMP QAsyncReader::BeginFlush()
{
QMutexLocker locker(&m_mutex);
m_flushing = true;
m_requestWait.wakeOne();
return S_OK;
}
STDMETHODIMP QAsyncReader::EndFlush()
{
QMutexLocker locker(&m_mutex);
m_flushing = false;
return S_OK;
}
STDMETHODIMP QAsyncReader::SyncReadAligned(IMediaSample *sample)
{
QMutexLocker locker(&m_mutex);
return syncReadAlignedUnlocked(sample);
}
STDMETHODIMP QAsyncReader::SyncRead(LONGLONG pos, LONG length, BYTE *buffer)
{
QMutexLocker locker(&m_mutex);
return read(pos, length, buffer, 0);
}
STDMETHODIMP QAsyncReader::syncReadAlignedUnlocked(IMediaSample *sample)
{
Q_ASSERT(!m_mutex.tryLock());
if (!sample) {
return E_POINTER;
}
REFERENCE_TIME start = 0,
stop = 0;
HRESULT hr = sample->GetTime(&start, &stop);
if(FAILED(hr)) {
return hr;
}
LONGLONG startPos = start / 10000000;
LONG length = static_cast<LONG>((stop - start) / 10000000);
BYTE *buffer;
hr = sample->GetPointer(&buffer);
if(FAILED(hr)) {
return hr;
}
LONG actual = 0;
read(startPos, length, buffer, &actual);
return sample->SetActualDataLength(actual);
}
}
}
QT_END_NAMESPACE
|