help_functions_ds.h 3.5 KB

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