blob: 29479157a87b468ea64b15b3d24f8f75618dde59 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CAPTURE_MAIN_SOURCE_WINDOWS_HELP_FUNCTIONS_DS_H_
12#define MODULES_VIDEO_CAPTURE_MAIN_SOURCE_WINDOWS_HELP_FUNCTIONS_DS_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
14#include <dshow.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
Tommi30e60d62019-03-12 09:59:05 +010016#include <type_traits>
17#include <utility>
18
19#include "api/scoped_refptr.h"
20#include "rtc_base/ref_counter.h"
21
Yves Gerey665174f2018-06-19 15:03:05 +020022DEFINE_GUID(MEDIASUBTYPE_I420,
23 0x30323449,
24 0x0000,
25 0x0010,
26 0x80,
27 0x00,
28 0x00,
29 0xAA,
30 0x00,
31 0x38,
32 0x9B,
33 0x71);
34DEFINE_GUID(MEDIASUBTYPE_HDYC,
35 0x43594448,
36 0x0000,
37 0x0010,
38 0x80,
39 0x00,
40 0x00,
41 0xAA,
42 0x00,
43 0x38,
44 0x9B,
45 0x71);
niklase@google.com470e71d2011-07-07 08:21:25 +000046
Yves Gerey665174f2018-06-19 15:03:05 +020047#define RELEASE_AND_CLEAR(p) \
48 if (p) { \
49 (p)->Release(); \
50 (p) = NULL; \
51 }
niklase@google.com470e71d2011-07-07 08:21:25 +000052
Yves Gerey665174f2018-06-19 15:03:05 +020053namespace webrtc {
54namespace videocapturemodule {
55LONGLONG GetMaxOfFrameArray(LONGLONG* maxFps, long size);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
57IPin* GetInputPin(IBaseFilter* filter);
tommi@webrtc.org81878772012-11-20 13:35:33 +000058IPin* GetOutputPin(IBaseFilter* filter, REFGUID Category);
Yves Gerey665174f2018-06-19 15:03:05 +020059BOOL PinMatchesCategory(IPin* pPin, REFGUID Category);
Tommi30e60d62019-03-12 09:59:05 +010060void ResetMediaType(AM_MEDIA_TYPE* media_type);
61void FreeMediaType(AM_MEDIA_TYPE* media_type);
62HRESULT CopyMediaType(AM_MEDIA_TYPE* target, const AM_MEDIA_TYPE* source);
63
64// Helper function to make using scoped_refptr with COM interface pointers
65// a little less awkward. rtc::scoped_refptr doesn't support the & operator
66// or a way to receive values via an out ptr.
67// The function is intentionally not called QueryInterface to make things less
68// confusing for the compiler to figure out what the caller wants to do when
69// called from within the context of a class that also implements COM
70// interfaces.
71template <class T>
72HRESULT GetComInterface(IUnknown* object, rtc::scoped_refptr<T>* ptr) {
73 // This helper function is not meant to magically free ptr. If we do that
74 // we add code bloat to most places where it's not needed and make the code
75 // less readable since it's not clear at the call site that the pointer
76 // would get freed even inf QI() fails.
77 RTC_DCHECK(!ptr->get());
78 void* new_ptr = nullptr;
79 HRESULT hr = object->QueryInterface(__uuidof(T), &new_ptr);
80 if (SUCCEEDED(hr))
81 ptr->swap(reinterpret_cast<T**>(&new_ptr));
82 return hr;
83}
84
85// Provides a reference count implementation for COM (IUnknown derived) classes.
86// The implementation uses atomics for managing the ref count.
87template <class T>
88class ComRefCount : public T {
89 public:
90 ComRefCount() {}
91
92 template <class P0>
93 explicit ComRefCount(P0&& p0) : T(std::forward<P0>(p0)) {}
94
95 STDMETHOD_(ULONG, AddRef)() override {
96 ref_count_.IncRef();
97 return 1;
98 }
99
100 STDMETHOD_(ULONG, Release)() override {
101 const auto status = ref_count_.DecRef();
102 if (status == rtc::RefCountReleaseStatus::kDroppedLastRef) {
103 delete this;
104 return 0;
105 }
106 return 1;
107 }
108
109 protected:
110 ~ComRefCount() {}
111
112 private:
113 webrtc::webrtc_impl::RefCounter ref_count_{0};
114};
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000116} // namespace videocapturemodule
117} // namespace webrtc
Yves Gerey665174f2018-06-19 15:03:05 +0200118#endif // MODULES_VIDEO_CAPTURE_MAIN_SOURCE_WINDOWS_HELP_FUNCTIONS_DS_H_