help_functions_ds.cc (4201B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 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 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 15 #include <cguid.h> 16 17 #include "modules/video_capture/windows/help_functions_ds.h" 18 #include "rtc_base/logging.h" 19 20 namespace webrtc { 21 namespace videocapturemodule { 22 // This returns minimum :), which will give max frame rate... 23 LONGLONG GetMaxOfFrameArray(LONGLONG* maxFps, long size) { 24 if (!maxFps || size <= 0) { 25 return 0; 26 } 27 LONGLONG maxFPS = maxFps[0]; 28 for (int i = 0; i < size; i++) { 29 if (maxFPS > maxFps[i]) 30 maxFPS = maxFps[i]; 31 } 32 return maxFPS; 33 } 34 35 IPin* GetInputPin(IBaseFilter* filter) { 36 IPin* pin = NULL; 37 IEnumPins* pPinEnum = NULL; 38 filter->EnumPins(&pPinEnum); 39 if (pPinEnum == NULL) { 40 return NULL; 41 } 42 43 // get first unconnected pin 44 pPinEnum->Reset(); // set to first pin 45 46 while (S_OK == pPinEnum->Next(1, &pin, NULL)) { 47 PIN_DIRECTION pPinDir; 48 pin->QueryDirection(&pPinDir); 49 if (PINDIR_INPUT == pPinDir) // This is an input pin 50 { 51 IPin* tempPin = NULL; 52 if (S_OK != pin->ConnectedTo(&tempPin)) // The pint is not connected 53 { 54 pPinEnum->Release(); 55 return pin; 56 } 57 } 58 pin->Release(); 59 } 60 pPinEnum->Release(); 61 return NULL; 62 } 63 64 IPin* GetOutputPin(IBaseFilter* filter, REFGUID Category) { 65 IPin* pin = NULL; 66 IEnumPins* pPinEnum = NULL; 67 filter->EnumPins(&pPinEnum); 68 if (pPinEnum == NULL) { 69 return NULL; 70 } 71 // get first unconnected pin 72 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; 88 } 89 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); 101 } 102 pKs->Release(); 103 } 104 return bFound; 105 } 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 157 } // namespace videocapturemodule 158 } // namespace webrtc