Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-videodev/dshowclasses.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include <pjmedia-videodev/config.h>
20
21
22 #if defined(PJMEDIA_VIDEO_DEV_HAS_DSHOW) && PJMEDIA_VIDEO_DEV_HAS_DSHOW != 0
23
24
25 #include <assert.h>
26 #include <streams.h>
27
28 typedef void (*input_callback)(void *user_data, IMediaSample *pMediaSample);
29
30 const GUID CLSID_NullRenderer = {0xF9168C5E, 0xCEB2, 0x4FAA, {0xB6, 0xBF,
31 0x32, 0x9B, 0xF3, 0x9F, 0xA1, 0xE4}};
32
33 const GUID CLSID_SourceFilter = {0xF9168C5E, 0xCEB2, 0x4FAA, {0xB6, 0xBF,
34 0x32, 0x9B, 0xF3, 0x9F, 0xA1, 0xE5}};
35
36 class NullRenderer: public CBaseRenderer
37 {
38 public:
39 NullRenderer(HRESULT *pHr);
40 virtual ~NullRenderer();
41
42 virtual HRESULT CheckMediaType(const CMediaType *pmt);
43 virtual HRESULT DoRenderSample(IMediaSample *pMediaSample);
44
45 input_callback input_cb;
46 void *user_data;
47 };
48
49 class OutputPin: public CBaseOutputPin
50 {
51 public:
52 OutputPin(CBaseFilter *pFilter, CCritSec *pLock, HRESULT *pHr);
53 ~OutputPin();
54
55 HRESULT Push(void *buf, long size);
56
57 virtual HRESULT CheckMediaType(const CMediaType *pmt);
58 virtual HRESULT DecideBufferSize(IMemAllocator *pAlloc,
59 ALLOCATOR_PROPERTIES *ppropInputRequest);
60
61 CMediaType mediaType;
62 long bufSize;
63 };
64
65 class SourceFilter: public CBaseFilter
66 {
67 public:
68 SourceFilter();
69 ~SourceFilter();
70
71 int GetPinCount();
72 CBasePin* GetPin(int n);
73
74 protected:
75 CCritSec lock;
76 OutputPin* outPin;
77 };
78
79 OutputPin::OutputPin(CBaseFilter *pFilter, CCritSec *pLock, HRESULT *pHr):
80 CBaseOutputPin("OutputPin", pFilter, pLock, pHr, L"OutputPin")
81 {
82 }
83
84 OutputPin::~OutputPin()
85 {
86 }
87
88 HRESULT OutputPin::CheckMediaType(const CMediaType *pmt)
89 {
90 return S_OK;
91 }
92
93 HRESULT OutputPin::DecideBufferSize(IMemAllocator *pAlloc,
94 ALLOCATOR_PROPERTIES *ppropInputRequest)
95 {
96 ALLOCATOR_PROPERTIES properties;
97
98 ppropInputRequest->cbBuffer = bufSize;
99 ppropInputRequest->cBuffers = 1;
100
101 /* First set the buffer descriptions we're interested in */
102 pAlloc->SetProperties(ppropInputRequest, &properties);
103
104 return S_OK;
105 }
106
107 HRESULT OutputPin::Push(void *buf, long size)
108 {
109 HRESULT hr;
110 IMediaSample *pSample;
111 VIDEOINFOHEADER *vi;
112 AM_MEDIA_TYPE *pmt;
113 BYTE *dst_buf;
114
115 /**
116 * Hold the critical section here as the pin might get disconnected
117 * during the Deliver() method call.
118 */
119 m_pLock->Lock();
120
121 hr = GetDeliveryBuffer(&pSample, NULL, NULL, 0);
122 if (FAILED(hr))
123 goto on_error;
124
125 pSample->GetMediaType(&pmt);
126 if (pmt) {
127 mediaType.Set(*pmt);
128 bufSize = pmt->lSampleSize;
129 }
130
131 pSample->GetPointer(&dst_buf);
132 vi = (VIDEOINFOHEADER *)mediaType.pbFormat;
133 if (vi->rcSource.right == vi->bmiHeader.biWidth) {
134 assert(pSample->GetSize() >= size);
135 memcpy(dst_buf, buf, size);
136 } else {
137 unsigned i, bpp;
138 unsigned dststride, srcstride;
139 BYTE *src_buf = (BYTE *)buf;
140
141 bpp = size / abs(vi->bmiHeader.biHeight) / vi->rcSource.right;
142 dststride = vi->bmiHeader.biWidth * bpp;
143 srcstride = vi->rcSource.right * bpp;
144 for (i = abs(vi->bmiHeader.biHeight); i > 0; i--) {
145 memcpy(dst_buf, src_buf, srcstride);
146 dst_buf += dststride;
147 src_buf += srcstride;
148 }
149 }
150 pSample->SetActualDataLength(size);
151
152 hr = Deliver(pSample);
153
154 pSample->Release();
155
156 on_error:
157 m_pLock->Unlock();
158 return hr;
159 }
160
161 SourceFilter::SourceFilter(): CBaseFilter("SourceFilter", NULL, &lock,
162 CLSID_SourceFilter)
163 {
164 HRESULT hr;
165 outPin = new OutputPin(this, &lock, &hr);
166 }
167
168 SourceFilter::~SourceFilter()
169 {
170 }
171
172 int SourceFilter::GetPinCount()
173 {
174 return 1;
175 }
176
177 CBasePin* SourceFilter::GetPin(int n)
178 {
179 return outPin;
180 }
181
182 NullRenderer::NullRenderer(HRESULT *pHr): CBaseRenderer(CLSID_NullRenderer,
183 "NullRenderer",
184 NULL, pHr)
185 {
186 input_cb = NULL;
187 }
188
189 NullRenderer::~NullRenderer()
190 {
191 }
192
193 HRESULT NullRenderer::CheckMediaType(const CMediaType *pmt)
194 {
195 return S_OK;
196 }
197
198 HRESULT NullRenderer::DoRenderSample(IMediaSample *pMediaSample)
199 {
200 if (input_cb)
201 input_cb(user_data, pMediaSample);
202
203 return S_OK;
204 }
205
206 extern "C" IBaseFilter* NullRenderer_Create(input_callback input_cb,
207 void *user_data)
208 {
209 HRESULT hr;
210 NullRenderer *renderer = new NullRenderer(&hr);
211 renderer->AddRef();
212 renderer->input_cb = input_cb;
213 renderer->user_data = user_data;
214
215 return (CBaseFilter *)renderer;
216 }
217
218 extern "C" IBaseFilter* SourceFilter_Create(SourceFilter **pSrc)
219 {
220 SourceFilter *src = new SourceFilter();
221 src->AddRef();
222 *pSrc = src;
223
224 return (CBaseFilter *)src;
225 }
226
227 extern "C" HRESULT SourceFilter_Deliver(SourceFilter *src,
228 void *buf, long size)
229 {
230 return ((OutputPin *)src->GetPin(0))->Push(buf, size);
231 }
232
233 extern "C" void SourceFilter_SetMediaType(SourceFilter *src,
234 AM_MEDIA_TYPE *pmt)
235 {
236 ((OutputPin *)src->GetPin(0))->mediaType.Set(*pmt);
237 ((OutputPin *)src->GetPin(0))->bufSize = pmt->lSampleSize;
238 }
239
240
241 #endif /* PJMEDIA_VIDEO_DEV_HAS_DSHOW */