blob: b1bc7af8e8d3b2ea9dd85564dc3ef3246fd8a762 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/video_capture/windows/help_functions_ds.h"
tommi@webrtc.org81878772012-11-20 13:35:33 +000016
Tommi30e60d62019-03-12 09:59:05 +010017#include "rtc_base/logging.h"
18
tommi@webrtc.org81878772012-11-20 13:35:33 +000019#include <cguid.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000020
Yves Gerey665174f2018-06-19 15:03:05 +020021namespace webrtc {
22namespace videocapturemodule {
niklase@google.com470e71d2011-07-07 08:21:25 +000023// This returns minimum :), which will give max frame rate...
Yves Gerey665174f2018-06-19 15:03:05 +020024LONGLONG GetMaxOfFrameArray(LONGLONG* maxFps, long size) {
25 LONGLONG maxFPS = maxFps[0];
26 for (int i = 0; i < size; i++) {
27 if (maxFPS > maxFps[i])
28 maxFPS = maxFps[i];
29 }
30 return maxFPS;
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
Yves Gerey665174f2018-06-19 15:03:05 +020033IPin* GetInputPin(IBaseFilter* filter) {
34 HRESULT hr;
35 IPin* pin = NULL;
36 IEnumPins* pPinEnum = NULL;
37 filter->EnumPins(&pPinEnum);
38 if (pPinEnum == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000039 return NULL;
Yves Gerey665174f2018-06-19 15:03:05 +020040 }
41
42 // get first unconnected pin
43 hr = pPinEnum->Reset(); // set to first pin
44
45 while (S_OK == pPinEnum->Next(1, &pin, NULL)) {
46 PIN_DIRECTION pPinDir;
47 pin->QueryDirection(&pPinDir);
48 if (PINDIR_INPUT == pPinDir) // This is an input pin
49 {
50 IPin* tempPin = NULL;
51 if (S_OK != pin->ConnectedTo(&tempPin)) // The pint is not connected
52 {
53 pPinEnum->Release();
54 return pin;
55 }
56 }
57 pin->Release();
58 }
59 pPinEnum->Release();
60 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
62
Yves Gerey665174f2018-06-19 15:03:05 +020063IPin* GetOutputPin(IBaseFilter* filter, REFGUID Category) {
64 HRESULT hr;
65 IPin* pin = NULL;
66 IEnumPins* pPinEnum = NULL;
67 filter->EnumPins(&pPinEnum);
68 if (pPinEnum == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000069 return NULL;
Yves Gerey665174f2018-06-19 15:03:05 +020070 }
71 // get first unconnected pin
72 hr = pPinEnum->Reset(); // set to first pin
73 while (S_OK == pPinEnum->Next(1, &pin, NULL)) {
74 PIN_DIRECTION pPinDir;
75 pin->QueryDirection(&pPinDir);
76 if (PINDIR_OUTPUT == pPinDir) // This is an output pin
77 {
78 if (Category == GUID_NULL || PinMatchesCategory(pin, Category)) {
79 pPinEnum->Release();
80 return pin;
81 }
82 }
83 pin->Release();
84 pin = NULL;
85 }
86 pPinEnum->Release();
87 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000088}
89
Yves Gerey665174f2018-06-19 15:03:05 +020090BOOL PinMatchesCategory(IPin* pPin, REFGUID Category) {
91 BOOL bFound = FALSE;
92 IKsPropertySet* pKs = NULL;
93 HRESULT hr = pPin->QueryInterface(IID_PPV_ARGS(&pKs));
94 if (SUCCEEDED(hr)) {
95 GUID PinCategory;
96 DWORD cbReturned;
97 hr = pKs->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, NULL, 0,
98 &PinCategory, sizeof(GUID), &cbReturned);
99 if (SUCCEEDED(hr) && (cbReturned == sizeof(GUID))) {
100 bFound = (PinCategory == Category);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 }
Yves Gerey665174f2018-06-19 15:03:05 +0200102 pKs->Release();
103 }
104 return bFound;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
Tommi30e60d62019-03-12 09:59:05 +0100106
107void ResetMediaType(AM_MEDIA_TYPE* media_type) {
108 if (!media_type)
109 return;
110 if (media_type->cbFormat != 0) {
111 CoTaskMemFree(media_type->pbFormat);
112 media_type->cbFormat = 0;
113 media_type->pbFormat = nullptr;
114 }
115 if (media_type->pUnk) {
116 media_type->pUnk->Release();
117 media_type->pUnk = nullptr;
118 }
119}
120
121void FreeMediaType(AM_MEDIA_TYPE* media_type) {
122 if (!media_type)
123 return;
124 ResetMediaType(media_type);
125 CoTaskMemFree(media_type);
126}
127
128HRESULT CopyMediaType(AM_MEDIA_TYPE* target, const AM_MEDIA_TYPE* source) {
129 RTC_DCHECK_NE(source, target);
130 *target = *source;
131 if (source->cbFormat != 0) {
132 RTC_DCHECK(source->pbFormat);
133 target->pbFormat =
134 reinterpret_cast<BYTE*>(CoTaskMemAlloc(source->cbFormat));
135 if (target->pbFormat == nullptr) {
136 target->cbFormat = 0;
137 return E_OUTOFMEMORY;
138 } else {
139 CopyMemory(target->pbFormat, source->pbFormat, target->cbFormat);
140 }
141 }
142
143 if (target->pUnk != nullptr)
144 target->pUnk->AddRef();
145
146 return S_OK;
147}
148
149wchar_t* DuplicateWideString(const wchar_t* str) {
150 size_t len = lstrlenW(str);
151 wchar_t* ret =
152 reinterpret_cast<LPWSTR>(CoTaskMemAlloc((len + 1) * sizeof(wchar_t)));
153 lstrcpyW(ret, str);
154 return ret;
155}
156
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000157} // namespace videocapturemodule
158} // namespace webrtc