blob: ad4c064d0659272c9aa5aaa578304fae7a421ed4 [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) {
33 HRESULT hr;
34 IPin* pin = NULL;
35 IEnumPins* pPinEnum = NULL;
36 filter->EnumPins(&pPinEnum);
37 if (pPinEnum == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000038 return NULL;
Yves Gerey665174f2018-06-19 15:03:05 +020039 }
40
41 // get first unconnected pin
42 hr = pPinEnum->Reset(); // set to first pin
43
44 while (S_OK == pPinEnum->Next(1, &pin, NULL)) {
45 PIN_DIRECTION pPinDir;
46 pin->QueryDirection(&pPinDir);
47 if (PINDIR_INPUT == pPinDir) // This is an input pin
48 {
49 IPin* tempPin = NULL;
50 if (S_OK != pin->ConnectedTo(&tempPin)) // The pint is not connected
51 {
52 pPinEnum->Release();
53 return pin;
54 }
55 }
56 pin->Release();
57 }
58 pPinEnum->Release();
59 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
61
Yves Gerey665174f2018-06-19 15:03:05 +020062IPin* GetOutputPin(IBaseFilter* filter, REFGUID Category) {
63 HRESULT hr;
64 IPin* pin = NULL;
65 IEnumPins* pPinEnum = NULL;
66 filter->EnumPins(&pPinEnum);
67 if (pPinEnum == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000068 return NULL;
Yves Gerey665174f2018-06-19 15:03:05 +020069 }
70 // get first unconnected pin
71 hr = pPinEnum->Reset(); // set to first pin
72 while (S_OK == pPinEnum->Next(1, &pin, NULL)) {
73 PIN_DIRECTION pPinDir;
74 pin->QueryDirection(&pPinDir);
75 if (PINDIR_OUTPUT == pPinDir) // This is an output pin
76 {
77 if (Category == GUID_NULL || PinMatchesCategory(pin, Category)) {
78 pPinEnum->Release();
79 return pin;
80 }
81 }
82 pin->Release();
83 pin = NULL;
84 }
85 pPinEnum->Release();
86 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
Yves Gerey665174f2018-06-19 15:03:05 +020089BOOL PinMatchesCategory(IPin* pPin, REFGUID Category) {
90 BOOL bFound = FALSE;
91 IKsPropertySet* pKs = NULL;
92 HRESULT hr = pPin->QueryInterface(IID_PPV_ARGS(&pKs));
93 if (SUCCEEDED(hr)) {
94 GUID PinCategory;
95 DWORD cbReturned;
96 hr = pKs->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, NULL, 0,
97 &PinCategory, sizeof(GUID), &cbReturned);
98 if (SUCCEEDED(hr) && (cbReturned == sizeof(GUID))) {
99 bFound = (PinCategory == Category);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 }
Yves Gerey665174f2018-06-19 15:03:05 +0200101 pKs->Release();
102 }
103 return bFound;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
Tommi30e60d62019-03-12 09:59:05 +0100105
106void ResetMediaType(AM_MEDIA_TYPE* media_type) {
107 if (!media_type)
108 return;
109 if (media_type->cbFormat != 0) {
110 CoTaskMemFree(media_type->pbFormat);
111 media_type->cbFormat = 0;
112 media_type->pbFormat = nullptr;
113 }
114 if (media_type->pUnk) {
115 media_type->pUnk->Release();
116 media_type->pUnk = nullptr;
117 }
118}
119
120void FreeMediaType(AM_MEDIA_TYPE* media_type) {
121 if (!media_type)
122 return;
123 ResetMediaType(media_type);
124 CoTaskMemFree(media_type);
125}
126
127HRESULT CopyMediaType(AM_MEDIA_TYPE* target, const AM_MEDIA_TYPE* source) {
128 RTC_DCHECK_NE(source, target);
129 *target = *source;
130 if (source->cbFormat != 0) {
131 RTC_DCHECK(source->pbFormat);
132 target->pbFormat =
133 reinterpret_cast<BYTE*>(CoTaskMemAlloc(source->cbFormat));
134 if (target->pbFormat == nullptr) {
135 target->cbFormat = 0;
136 return E_OUTOFMEMORY;
137 } else {
138 CopyMemory(target->pbFormat, source->pbFormat, target->cbFormat);
139 }
140 }
141
142 if (target->pUnk != nullptr)
143 target->pUnk->AddRef();
144
145 return S_OK;
146}
147
148wchar_t* DuplicateWideString(const wchar_t* str) {
149 size_t len = lstrlenW(str);
150 wchar_t* ret =
151 reinterpret_cast<LPWSTR>(CoTaskMemAlloc((len + 1) * sizeof(wchar_t)));
152 lstrcpyW(ret, str);
153 return ret;
154}
155
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000156} // namespace videocapturemodule
157} // namespace webrtc