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
|
/*
* Copyright (C) 2010-2013 Hendrik Leppkes
* http://www.1f0.de
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.h"
#include "InputPin.h"
#include "LAVSplitter.h"
#define READ_BUFFER_SIZE 131072
CLAVInputPin::CLAVInputPin(TCHAR *pName, CLAVSplitter *pFilter, CCritSec *pLock, HRESULT *phr)
: CBasePin(pName, pFilter, pLock, phr, L"Input", PINDIR_INPUT)
{
}
CLAVInputPin::~CLAVInputPin(void)
{
if (m_pAVIOContext) {
av_free(m_pAVIOContext->buffer);
av_free(m_pAVIOContext);
m_pAVIOContext = nullptr;
}
}
STDMETHODIMP CLAVInputPin::NonDelegatingQueryInterface(REFIID riid, void** ppv)
{
CheckPointer(ppv, E_POINTER);
return
__super::NonDelegatingQueryInterface(riid, ppv);
}
HRESULT CLAVInputPin::CheckMediaType(const CMediaType* pmt)
{
//return pmt->majortype == MEDIATYPE_Stream ? S_OK : VFW_E_TYPE_NOT_ACCEPTED;
return S_OK;
}
HRESULT CLAVInputPin::CheckConnect(IPin* pPin)
{
HRESULT hr;
if(FAILED(hr = __super::CheckConnect(pPin))) {
return hr;
}
IAsyncReader *pReader = nullptr;
if (FAILED(hr = pPin->QueryInterface(&pReader)) || pReader == nullptr) {
return E_FAIL;
}
SafeRelease(&pReader);
return S_OK;
}
HRESULT CLAVInputPin::BreakConnect()
{
HRESULT hr;
if(FAILED(hr = __super::BreakConnect())) {
return hr;
}
if(FAILED(hr = (static_cast<CLAVSplitter *>(m_pFilter))->BreakInputConnection())) {
return hr;
}
SafeRelease(&m_pAsyncReader);
if (m_pAVIOContext) {
av_free(m_pAVIOContext->buffer);
av_free(m_pAVIOContext);
m_pAVIOContext = nullptr;
}
return S_OK;
}
HRESULT CLAVInputPin::CompleteConnect(IPin* pPin)
{
HRESULT hr;
if(FAILED(hr = __super::CompleteConnect(pPin))) {
return hr;
}
CheckPointer(pPin, E_POINTER);
if (FAILED(hr = pPin->QueryInterface(&m_pAsyncReader)) || m_pAsyncReader == nullptr) {
return E_FAIL;
}
m_llPos = 0;
if(FAILED(hr = (static_cast<CLAVSplitter *>(m_pFilter))->CompleteInputConnection())) {
return hr;
}
return S_OK;
}
int CLAVInputPin::Read(void *opaque, uint8_t *buf, int buf_size)
{
CLAVInputPin *pin = static_cast<CLAVInputPin *>(opaque);
CAutoLock lock(pin);
HRESULT hr = pin->m_pAsyncReader->SyncRead(pin->m_llPos, buf_size, buf);
if (FAILED(hr)) {
return -1;
}
if (hr == S_FALSE) {
LONGLONG total = 0, available = 0;
int read = 0;
if (S_OK == pin->m_pAsyncReader->Length(&total, &available) && total >= pin->m_llPos && total <= (pin->m_llPos + buf_size)) {
read = (int)(total - pin->m_llPos);
DbgLog((LOG_TRACE, 10, L"At EOF, pos: %I64d, size: %I64d, remainder: %d", pin->m_llPos, total, read));
} else {
DbgLog((LOG_TRACE, 10, L"We're at EOF (pos: %I64d), but Length seems unreliable, trying reading manually", pin->m_llPos));
do {
hr = pin->m_pAsyncReader->SyncRead(pin->m_llPos, 1, buf+read);
} while(hr == S_OK && (++read) < buf_size);
DbgLog((LOG_TRACE, 10, L"-> Read %d bytes", read));
}
pin->m_llPos += read;
return read;
}
pin->m_llPos += buf_size;
return buf_size;
}
int64_t CLAVInputPin::Seek(void *opaque, int64_t offset, int whence)
{
CLAVInputPin *pin = static_cast<CLAVInputPin *>(opaque);
CAutoLock lock(pin);
int64_t pos = 0;
LONGLONG total = 0;
LONGLONG available = 0;
pin->m_pAsyncReader->Length(&total, &available);
if (whence == SEEK_SET) {
pin->m_llPos = offset;
} else if (whence == SEEK_CUR) {
pin->m_llPos += offset;
} else if (whence == SEEK_END) {
pin->m_llPos = total - offset;
} else if (whence == AVSEEK_SIZE) {
return total;
} else
return -1;
if (pin->m_llPos > available)
pin->m_llPos = available;
else if (pin->m_llPos < 0)
pin->m_llPos = 0;
return pin->m_llPos;
}
HRESULT CLAVInputPin::GetAVIOContext(AVIOContext** ppContext)
{
CheckPointer(m_pAsyncReader, E_UNEXPECTED);
CheckPointer(ppContext, E_POINTER);
if (!m_pAVIOContext) {
uint8_t *buffer = (uint8_t *)av_mallocz(READ_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
m_pAVIOContext = avio_alloc_context(buffer, READ_BUFFER_SIZE, 0, this, Read, nullptr, Seek);
LONGLONG total = 0;
LONGLONG available = 0;
HRESULT hr = m_pAsyncReader->Length(&total, &available);
if (FAILED(hr) || total == 0) {
DbgLog((LOG_TRACE, 10, L"CLAVInputPin::GetAVIOContext(): getting file length failed, disabling seeking"));
m_pAVIOContext->seekable = 0;
m_pAVIOContext->seek = nullptr;
m_pAVIOContext->buffer_size = READ_BUFFER_SIZE / 4;
}
}
*ppContext = m_pAVIOContext;
return S_OK;
}
STDMETHODIMP CLAVInputPin::BeginFlush()
{
return E_UNEXPECTED;
}
STDMETHODIMP CLAVInputPin::EndFlush()
{
return E_UNEXPECTED;
}
|