blob: b767726107675875e90ab356416f19ba03930c17 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.orge713fd02012-04-10 07:13:46 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
tommi@webrtc.org81878772012-11-20 13:35:33 +000011#include <initguid.h> // Must come before the help_functions_ds.h include so
12 // that DEFINE_GUID() entries will be defined in this
13 // object file.
14
tommi@webrtc.org81878772012-11-20 13:35:33 +000015#include <cguid.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
Jonas Olssona4d87372019-07-05 19:08:33 +020017#include "modules/video_capture/windows/help_functions_ds.h"
18#include "rtc_base/logging.h"
19
Yves Gerey665174f2018-06-19 15:03:05 +020020namespace webrtc {
21namespace videocapturemodule {
niklase@google.com470e71d2011-07-07 08:21:25 +000022// This returns minimum :), which will give max frame rate...
Yves Gerey665174f2018-06-19 15:03:05 +020023LONGLONG GetMaxOfFrameArray(LONGLONG* maxFps, long size) {
24 LONGLONG maxFPS = maxFps[0];
25 for (int i = 0; i < size; i++) {
26 if (maxFPS > maxFps[i])
27 maxFPS = maxFps[i];
28 }
29 return maxFPS;
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
Yves Gerey665174f2018-06-19 15:03:05 +020032IPin* GetInputPin(IBaseFilter* filter) {
Yves Gerey665174f2018-06-19 15:03:05 +020033 IPin* pin = NULL;
34 IEnumPins* pPinEnum = NULL;
35 filter->EnumPins(&pPinEnum);
36 if (pPinEnum == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000037 return NULL;
Yves Gerey665174f2018-06-19 15:03:05 +020038 }
39
40 // get first unconnected pin
Mirko Bonadeiff8caf12021-10-29 21:55:04 +020041 pPinEnum->Reset(); // set to first pin
Yves Gerey665174f2018-06-19 15:03:05 +020042
43 while (S_OK == pPinEnum->Next(1, &pin, NULL)) {
44 PIN_DIRECTION pPinDir;
45 pin->QueryDirection(&pPinDir);
46 if (PINDIR_INPUT == pPinDir) // This is an input pin
47 {
48 IPin* tempPin = NULL;
49 if (S_OK != pin->ConnectedTo(&tempPin)) // The pint is not connected
50 {
51 pPinEnum->Release();
52 return pin;
53 }
54 }
55 pin->Release();
56 }
57 pPinEnum->Release();
58 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
Yves Gerey665174f2018-06-19 15:03:05 +020061IPin* GetOutputPin(IBaseFilter* filter, REFGUID Category) {
Yves Gerey665174f2018-06-19 15:03:05 +020062 IPin* pin = NULL;
63 IEnumPins* pPinEnum = NULL;
64 filter->EnumPins(&pPinEnum);
65 if (pPinEnum == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000066 return NULL;
Yves Gerey665174f2018-06-19 15:03:05 +020067 }
68 // get first unconnected pin
Mirko Bonadei3cff1712021-10-28 21:14:00 +020069 pPinEnum->Reset(); // set to first pin
Yves Gerey665174f2018-06-19 15:03:05 +020070 while (S_OK == pPinEnum->Next(1, &pin, NULL)) {
71 PIN_DIRECTION pPinDir;
72 pin->QueryDirection(&pPinDir);
73 if (PINDIR_OUTPUT == pPinDir) // This is an output pin
74 {
75 if (Category == GUID_NULL || PinMatchesCategory(pin, Category)) {
76 pPinEnum->Release();
77 return pin;
78 }
79 }
80 pin->Release();
81 pin = NULL;
82 }
83 pPinEnum->Release();
84 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000085}
86
Yves Gerey665174f2018-06-19 15:03:05 +020087BOOL PinMatchesCategory(IPin* pPin, REFGUID Category) {
88 BOOL bFound = FALSE;
89 IKsPropertySet* pKs = NULL;
90 HRESULT hr = pPin->QueryInterface(IID_PPV_ARGS(&pKs));
91 if (SUCCEEDED(hr)) {
92 GUID PinCategory;
93 DWORD cbReturned;
94 hr = pKs->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, NULL, 0,
95 &PinCategory, sizeof(GUID), &cbReturned);
96 if (SUCCEEDED(hr) && (cbReturned == sizeof(GUID))) {
97 bFound = (PinCategory == Category);
niklase@google.com470e71d2011-07-07 08:21:25 +000098 }
Yves Gerey665174f2018-06-19 15:03:05 +020099 pKs->Release();
100 }
101 return bFound;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
Tommi30e60d62019-03-12 09:59:05 +0100103
104void ResetMediaType(AM_MEDIA_TYPE* media_type) {
105 if (!media_type)
106 return;
107 if (media_type->cbFormat != 0) {
108 CoTaskMemFree(media_type->pbFormat);
109 media_type->cbFormat = 0;
110 media_type->pbFormat = nullptr;
111 }
112 if (media_type->pUnk) {
113 media_type->pUnk->Release();
114 media_type->pUnk = nullptr;
115 }
116}
117
118void FreeMediaType(AM_MEDIA_TYPE* media_type) {
119 if (!media_type)
120 return;
121 ResetMediaType(media_type);
122 CoTaskMemFree(media_type);
123}
124
125HRESULT CopyMediaType(AM_MEDIA_TYPE* target, const AM_MEDIA_TYPE* source) {
126 RTC_DCHECK_NE(source, target);
127 *target = *source;
128 if (source->cbFormat != 0) {
129 RTC_DCHECK(source->pbFormat);
130 target->pbFormat =
131 reinterpret_cast<BYTE*>(CoTaskMemAlloc(source->cbFormat));
132 if (target->pbFormat == nullptr) {
133 target->cbFormat = 0;
134 return E_OUTOFMEMORY;
135 } else {
136 CopyMemory(target->pbFormat, source->pbFormat, target->cbFormat);
137 }
138 }
139
140 if (target->pUnk != nullptr)
141 target->pUnk->AddRef();
142
143 return S_OK;
144}
145
146wchar_t* DuplicateWideString(const wchar_t* str) {
147 size_t len = lstrlenW(str);
148 wchar_t* ret =
149 reinterpret_cast<LPWSTR>(CoTaskMemAlloc((len + 1) * sizeof(wchar_t)));
150 lstrcpyW(ret, str);
151 return ret;
152}
153
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000154} // namespace videocapturemodule
155} // namespace webrtc