1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/video/capture/win/pin_base_win.h"
7 #include "base/logging.h"
11 // Implement IEnumPins.
12 class TypeEnumerator final
13 : public IEnumMediaTypes,
14 public base::RefCounted<TypeEnumerator> {
16 explicit TypeEnumerator(PinBase* pin)
24 // Implement from IUnknown.
25 STDMETHOD(QueryInterface)(REFIID iid, void** object_ptr) {
26 if (iid == IID_IEnumMediaTypes || iid == IID_IUnknown) {
28 *object_ptr = static_cast<IEnumMediaTypes*>(this);
34 STDMETHOD_(ULONG, AddRef)() {
35 base::RefCounted<TypeEnumerator>::AddRef();
39 STDMETHOD_(ULONG, Release)() {
40 base::RefCounted<TypeEnumerator>::Release();
44 // Implement IEnumMediaTypes.
45 STDMETHOD(Next)(ULONG count, AM_MEDIA_TYPE** types, ULONG* fetched) {
46 ULONG types_fetched = 0;
48 while (types_fetched < count) {
49 // Allocate AM_MEDIA_TYPE that we will store the media type in.
50 AM_MEDIA_TYPE* type = reinterpret_cast<AM_MEDIA_TYPE*>(CoTaskMemAlloc(
51 sizeof(AM_MEDIA_TYPE)));
53 FreeAllocatedMediaTypes(types_fetched, types);
56 ZeroMemory(type, sizeof(AM_MEDIA_TYPE));
58 // Allocate a VIDEOINFOHEADER and connect it to the AM_MEDIA_TYPE.
59 type->cbFormat = sizeof(VIDEOINFOHEADER);
60 BYTE *format = reinterpret_cast<BYTE*>(CoTaskMemAlloc(
61 sizeof(VIDEOINFOHEADER)));
64 FreeAllocatedMediaTypes(types_fetched, types);
67 type->pbFormat = format;
68 // Get the media type from the pin.
69 if (pin_->GetValidMediaType(index_++, type)) {
70 types[types_fetched++] = type;
72 CoTaskMemFree(format);
79 *fetched = types_fetched;
81 return types_fetched == count ? S_OK : S_FALSE;
84 STDMETHOD(Skip)(ULONG count) {
94 STDMETHOD(Clone)(IEnumMediaTypes** clone) {
95 TypeEnumerator* type_enum = new TypeEnumerator(pin_);
97 type_enum->index_ = index_;
103 void FreeAllocatedMediaTypes(ULONG allocated, AM_MEDIA_TYPE** types) {
104 for (ULONG i = 0; i < allocated; ++i) {
105 CoTaskMemFree(types[i]->pbFormat);
106 CoTaskMemFree(types[i]);
110 scoped_refptr<PinBase> pin_;
114 PinBase::PinBase(IBaseFilter* owner)
116 memset(¤t_media_type_, 0, sizeof(current_media_type_));
119 PinBase::~PinBase() {
122 void PinBase::SetOwner(IBaseFilter* owner) {
126 // Called on an output pin to and establish a
128 STDMETHODIMP PinBase::Connect(IPin* receive_pin,
129 const AM_MEDIA_TYPE* media_type) {
130 if (!receive_pin || !media_type)
133 current_media_type_ = *media_type;
134 receive_pin->AddRef();
135 connected_pin_.Attach(receive_pin);
136 HRESULT hr = receive_pin->ReceiveConnection(this, media_type);
141 // Called from an output pin on an input pin to and establish a
143 STDMETHODIMP PinBase::ReceiveConnection(IPin* connector,
144 const AM_MEDIA_TYPE* media_type) {
145 if (!IsMediaTypeValid(media_type))
146 return VFW_E_TYPE_NOT_ACCEPTED;
148 current_media_type_ = *media_type;
150 connected_pin_.Attach(connector);
154 STDMETHODIMP PinBase::Disconnect() {
158 connected_pin_.Release();
162 STDMETHODIMP PinBase::ConnectedTo(IPin** pin) {
163 *pin = connected_pin_;
165 return VFW_E_NOT_CONNECTED;
167 connected_pin_.get()->AddRef();
171 STDMETHODIMP PinBase::ConnectionMediaType(AM_MEDIA_TYPE* media_type) {
173 return VFW_E_NOT_CONNECTED;
174 *media_type = current_media_type_;
178 STDMETHODIMP PinBase::QueryPinInfo(PIN_INFO* info) {
179 info->dir = PINDIR_INPUT;
180 info->pFilter = owner_;
183 info->achName[0] = L'\0';
188 STDMETHODIMP PinBase::QueryDirection(PIN_DIRECTION* pin_dir) {
189 *pin_dir = PINDIR_INPUT;
193 STDMETHODIMP PinBase::QueryId(LPWSTR* id) {
195 return E_OUTOFMEMORY;
198 STDMETHODIMP PinBase::QueryAccept(const AM_MEDIA_TYPE* media_type) {
202 STDMETHODIMP PinBase::EnumMediaTypes(IEnumMediaTypes** types) {
203 *types = new TypeEnumerator(this);
208 STDMETHODIMP PinBase::QueryInternalConnections(IPin** pins, ULONG* no_pins) {
212 STDMETHODIMP PinBase::EndOfStream() {
216 STDMETHODIMP PinBase::BeginFlush() {
220 STDMETHODIMP PinBase::EndFlush() {
224 STDMETHODIMP PinBase::NewSegment(REFERENCE_TIME start,
231 // Inherited from IMemInputPin.
232 STDMETHODIMP PinBase::GetAllocator(IMemAllocator** allocator) {
233 return VFW_E_NO_ALLOCATOR;
236 STDMETHODIMP PinBase::NotifyAllocator(IMemAllocator* allocator,
241 STDMETHODIMP PinBase::GetAllocatorRequirements(
242 ALLOCATOR_PROPERTIES* properties) {
246 STDMETHODIMP PinBase::ReceiveMultiple(IMediaSample** samples,
253 while (sample_count--) {
254 hr = Receive(samples[*processed]);
255 // S_FALSE means don't send any more.
263 STDMETHODIMP PinBase::ReceiveCanBlock() {
267 // Inherited from IUnknown.
268 STDMETHODIMP PinBase::QueryInterface(REFIID id, void** object_ptr) {
269 if (id == IID_IPin || id == IID_IUnknown) {
270 *object_ptr = static_cast<IPin*>(this);
271 } else if (id == IID_IMemInputPin) {
272 *object_ptr = static_cast<IMemInputPin*>(this);
274 return E_NOINTERFACE;
280 STDMETHODIMP_(ULONG) PinBase::AddRef() {
281 base::RefCounted<PinBase>::AddRef();
285 STDMETHODIMP_(ULONG) PinBase::Release() {
286 base::RefCounted<PinBase>::Release();