niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | e713fd0 | 2012-04-10 07:13:46 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | 8187877 | 2012-11-20 13:35:33 +0000 | [diff] [blame] | 11 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/video_capture/windows/help_functions_ds.h" |
tommi@webrtc.org | 8187877 | 2012-11-20 13:35:33 +0000 | [diff] [blame] | 16 | |
Tommi | 30e60d6 | 2019-03-12 09:59:05 +0100 | [diff] [blame] | 17 | #include "rtc_base/logging.h" |
| 18 | |
tommi@webrtc.org | 8187877 | 2012-11-20 13:35:33 +0000 | [diff] [blame] | 19 | #include <cguid.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | namespace videocapturemodule { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | // This returns minimum :), which will give max frame rate... |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | LONGLONG 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | IPin* GetInputPin(IBaseFilter* filter) { |
| 34 | HRESULT hr; |
| 35 | IPin* pin = NULL; |
| 36 | IEnumPins* pPinEnum = NULL; |
| 37 | filter->EnumPins(&pPinEnum); |
| 38 | if (pPinEnum == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | return NULL; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 40 | } |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 63 | IPin* 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | return NULL; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | } |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 90 | BOOL 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | pKs->Release(); |
| 103 | } |
| 104 | return bFound; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | } |
Tommi | 30e60d6 | 2019-03-12 09:59:05 +0100 | [diff] [blame] | 106 | |
| 107 | void 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 | |
| 121 | void FreeMediaType(AM_MEDIA_TYPE* media_type) { |
| 122 | if (!media_type) |
| 123 | return; |
| 124 | ResetMediaType(media_type); |
| 125 | CoTaskMemFree(media_type); |
| 126 | } |
| 127 | |
| 128 | HRESULT 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 | |
| 149 | wchar_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.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 157 | } // namespace videocapturemodule |
| 158 | } // namespace webrtc |