xref: /aosp_15_r20/external/webrtc/modules/video_capture/windows/help_functions_ds.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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...
GetMaxOfFrameArray(LONGLONG * maxFps,long size)23 LONGLONG 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;
30 }
31 
GetInputPin(IBaseFilter * filter)32 IPin* GetInputPin(IBaseFilter* filter) {
33   IPin* pin = NULL;
34   IEnumPins* pPinEnum = NULL;
35   filter->EnumPins(&pPinEnum);
36   if (pPinEnum == NULL) {
37     return NULL;
38   }
39 
40   // get first unconnected pin
41   pPinEnum->Reset();  // set to first pin
42 
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;
59 }
60 
GetOutputPin(IBaseFilter * filter,REFGUID Category)61 IPin* GetOutputPin(IBaseFilter* filter, REFGUID Category) {
62   IPin* pin = NULL;
63   IEnumPins* pPinEnum = NULL;
64   filter->EnumPins(&pPinEnum);
65   if (pPinEnum == NULL) {
66     return NULL;
67   }
68   // get first unconnected pin
69   pPinEnum->Reset();  // set to first pin
70   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;
85 }
86 
PinMatchesCategory(IPin * pPin,REFGUID Category)87 BOOL 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);
98     }
99     pKs->Release();
100   }
101   return bFound;
102 }
103 
ResetMediaType(AM_MEDIA_TYPE * media_type)104 void 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 
FreeMediaType(AM_MEDIA_TYPE * media_type)118 void FreeMediaType(AM_MEDIA_TYPE* media_type) {
119   if (!media_type)
120     return;
121   ResetMediaType(media_type);
122   CoTaskMemFree(media_type);
123 }
124 
CopyMediaType(AM_MEDIA_TYPE * target,const AM_MEDIA_TYPE * source)125 HRESULT 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 
DuplicateWideString(const wchar_t * str)146 wchar_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 
154 }  // namespace videocapturemodule
155 }  // namespace webrtc
156