-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
asyncrdr.cpp
394 lines (326 loc) · 8.4 KB
/
asyncrdr.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//------------------------------------------------------------------------------
// File: AsyncRdr.cpp
//
// Desc: DirectShow sample code - base library with I/O functionality.
// This file implements I/O source filter methods and output pin
// methods for CAsyncReader and CAsyncOutputPin.
//
// Copyright (c) 1992 - 2000, Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
#include <streams.h>
#include "asyncio.h"
#include "asyncrdr.h"
// --- CAsyncOutputPin implementation ---
CAsyncOutputPin::CAsyncOutputPin(
HRESULT * phr,
CAsyncReader *pReader,
CAsyncIo *pIo,
CCritSec * pLock)
: CBasePin(
NAME("Async output pin"),
pReader,
pLock,
phr,
L"Output",
PINDIR_OUTPUT),
m_pReader(pReader),
m_pIo(pIo)
{
}
CAsyncOutputPin::~CAsyncOutputPin()
{
}
STDMETHODIMP
CAsyncOutputPin::NonDelegatingQueryInterface(REFIID riid, void** ppv)
{
CheckPointer(ppv,E_POINTER);
if (riid == IID_IAsyncReader) {
m_bQueriedForAsyncReader = TRUE;
return GetInterface((IAsyncReader*) this, ppv);
} else {
return CBasePin::NonDelegatingQueryInterface(riid, ppv);
}
}
HRESULT
CAsyncOutputPin::GetMediaType(int iPosition, CMediaType *pMediaType)
{
if (iPosition < 0) {
return E_INVALIDARG;
}
if (iPosition > 0) {
return VFW_S_NO_MORE_ITEMS;
}
*pMediaType = *m_pReader->LoadType();
return S_OK;
}
HRESULT
CAsyncOutputPin::CheckMediaType(const CMediaType* pType)
{
CAutoLock lck(m_pLock);
/* We treat MEDIASUBTYPE_NULL subtype as a wild card */
if ((m_pReader->LoadType()->majortype == pType->majortype) &&
(m_pReader->LoadType()->subtype == MEDIASUBTYPE_NULL ||
m_pReader->LoadType()->subtype == pType->subtype)) {
return S_OK;
}
return S_FALSE;
}
HRESULT
CAsyncOutputPin::InitAllocator(IMemAllocator **ppAlloc)
{
HRESULT hr = NOERROR;
*ppAlloc = NULL;
CMemAllocator *pMemObject = NULL;
/* Create a default memory allocator */
pMemObject = new CMemAllocator(NAME("Base memory allocator"),NULL, &hr);
if (pMemObject == NULL) {
return E_OUTOFMEMORY;
}
if (FAILED(hr)) {
delete pMemObject;
return hr;
}
/* Get a reference counted IID_IMemAllocator interface */
hr = pMemObject->QueryInterface(IID_IMemAllocator,(void **)ppAlloc);
if (FAILED(hr)) {
delete pMemObject;
return E_NOINTERFACE;
}
ASSERT(*ppAlloc != NULL);
return NOERROR;
}
// we need to return an addrefed allocator, even if it is the preferred
// one, since he doesn't know whether it is the preferred one or not.
STDMETHODIMP
CAsyncOutputPin::RequestAllocator(
IMemAllocator* pPreferred,
ALLOCATOR_PROPERTIES* pProps,
IMemAllocator ** ppActual)
{
// we care about alignment but nothing else
if (!pProps->cbAlign || !m_pIo->IsAligned(pProps->cbAlign)) {
m_pIo->Alignment(&pProps->cbAlign);
}
ALLOCATOR_PROPERTIES Actual;
HRESULT hr;
if (pPreferred) {
hr = pPreferred->SetProperties(pProps, &Actual);
if (SUCCEEDED(hr) && m_pIo->IsAligned(Actual.cbAlign)) {
pPreferred->AddRef();
*ppActual = pPreferred;
return S_OK;
}
}
// create our own allocator
IMemAllocator* pAlloc;
hr = InitAllocator(&pAlloc);
if (FAILED(hr)) {
return hr;
}
//...and see if we can make it suitable
hr = pAlloc->SetProperties(pProps, &Actual);
if (SUCCEEDED(hr) && m_pIo->IsAligned(Actual.cbAlign)) {
// we need to release our refcount on pAlloc, and addref
// it to pass a refcount to the caller - this is a net nothing.
*ppActual = pAlloc;
return S_OK;
}
// failed to find a suitable allocator
pAlloc->Release();
// if we failed because of the IsAligned test, the error code will
// not be failure
if (SUCCEEDED(hr)) {
hr = VFW_E_BADALIGN;
}
return hr;
}
// queue an aligned read request. call WaitForNext to get
// completion.
STDMETHODIMP
CAsyncOutputPin::Request(
IMediaSample* pSample,
DWORD_PTR dwUser) // user context
{
REFERENCE_TIME tStart, tStop;
HRESULT hr = pSample->GetTime(&tStart, &tStop);
if (FAILED(hr)) {
return hr;
}
LONGLONG llPos = tStart / UNITS;
LONG lLength = (LONG) ((tStop - tStart) / UNITS);
LONGLONG llTotal;
LONGLONG llAvailable;
hr = m_pIo->Length(&llTotal, &llAvailable);
if (llPos + lLength > llTotal) {
// the end needs to be aligned, but may have been aligned
// on a coarser alignment.
LONG lAlign;
m_pIo->Alignment(&lAlign);
llTotal = (llTotal + lAlign -1) & ~(lAlign-1);
if (llPos + lLength > llTotal) {
lLength = (LONG) (llTotal - llPos);
// must be reducing this!
ASSERT((llTotal * UNITS) <= tStop);
tStop = llTotal * UNITS;
pSample->SetTime(&tStart, &tStop);
}
}
BYTE* pBuffer;
hr = pSample->GetPointer(&pBuffer);
if (FAILED(hr)) {
return hr;
}
return m_pIo->Request(
llPos,
lLength,
TRUE,
pBuffer,
(LPVOID)pSample,
dwUser);
}
// sync-aligned request. just like a request/waitfornext pair.
STDMETHODIMP
CAsyncOutputPin::SyncReadAligned(
IMediaSample* pSample)
{
REFERENCE_TIME tStart, tStop;
HRESULT hr = pSample->GetTime(&tStart, &tStop);
if (FAILED(hr)) {
return hr;
}
LONGLONG llPos = tStart / UNITS;
LONG lLength = (LONG) ((tStop - tStart) / UNITS);
LONGLONG llTotal;
LONGLONG llAvailable;
hr = m_pIo->Length(&llTotal, &llAvailable);
if (llPos + lLength > llTotal) {
// the end needs to be aligned, but may have been aligned
// on a coarser alignment.
LONG lAlign;
m_pIo->Alignment(&lAlign);
llTotal = (llTotal + lAlign -1) & ~(lAlign-1);
if (llPos + lLength > llTotal) {
lLength = (LONG) (llTotal - llPos);
// must be reducing this!
ASSERT((llTotal * UNITS) <= tStop);
tStop = llTotal * UNITS;
pSample->SetTime(&tStart, &tStop);
}
}
BYTE* pBuffer;
hr = pSample->GetPointer(&pBuffer);
if (FAILED(hr)) {
return hr;
}
LONG cbActual;
hr = m_pIo->SyncReadAligned(
llPos,
lLength,
pBuffer,
&cbActual,
pSample
);
pSample->SetActualDataLength(cbActual);
return hr;
}
//
// collect the next ready sample
STDMETHODIMP
CAsyncOutputPin::WaitForNext(
DWORD dwTimeout,
IMediaSample** ppSample, // completed sample
DWORD_PTR * pdwUser) // user context
{
LONG cbActual;
IMediaSample* pSample;
HRESULT hr = m_pIo->WaitForNext(
dwTimeout,
(LPVOID*) &pSample,
pdwUser,
&cbActual
);
if (SUCCEEDED(hr)) {
pSample->SetActualDataLength(cbActual);
}
*ppSample = pSample;
return hr;
}
//
// synchronous read that need not be aligned.
STDMETHODIMP
CAsyncOutputPin::SyncRead(
LONGLONG llPosition, // absolute Io position
LONG lLength, // nr bytes required
BYTE* pBuffer) // write data here
{
return m_pIo->SyncRead(llPosition, lLength, pBuffer);
}
// return the length of the file, and the length currently
// available locally. We only support locally accessible files,
// so they are always the same
STDMETHODIMP
CAsyncOutputPin::Length(
LONGLONG* pTotal,
LONGLONG* pAvailable)
{
HRESULT hr = m_pIo->Length(pTotal, pAvailable);
return hr;
}
STDMETHODIMP
CAsyncOutputPin::BeginFlush(void)
{
return m_pIo->BeginFlush();
}
STDMETHODIMP
CAsyncOutputPin::EndFlush(void)
{
return m_pIo->EndFlush();
}
STDMETHODIMP
CAsyncOutputPin::Connect(
IPin * pReceivePin,
const AM_MEDIA_TYPE *pmt // optional media type
)
{
return m_pReader->Connect(pReceivePin, pmt);
}
// --- CAsyncReader implementation ---
#pragma warning(disable:4355)
CAsyncReader::CAsyncReader(
TCHAR *pName,
LPUNKNOWN pUnk,
CAsyncStream *pStream,
HRESULT *phr)
: CBaseFilter(
pName,
pUnk,
&m_csFilter,
CLSID_AsyncReader,
NULL
),
m_OutputPin(
phr,
this,
&m_Io,
&m_csFilter),
m_Io(pStream)
{
}
CAsyncReader::~CAsyncReader()
{
}
int
CAsyncReader::GetPinCount()
{
return 1;
}
CBasePin *
CAsyncReader::GetPin(int n)
{
if ((GetPinCount() > 0) &&
(n == 0)) {
return &m_OutputPin;
} else {
return NULL;
}
}