Remove dependency on DirectShow baseclasses (streams.h from the winsdk_samples directory).

This required a fairly extensive overhaul:

* Removed locks from the implementation.
  The filter/pin architecture does use multiple threads,
  but the state is controlled on one and synchronization
  is done via flags that don't require locking.
  Note though that the baseclasses used a lot of locking, it's unclear why,
  but perhaps there are things I'm not aware of. The locking was not done
  consistently though, which doesn't seem to have been a problem.
* Change the code to not mix AddRef/Release and use of explicit 'delete'.
* Removed implementations of interfaces we don't need/use.
* Similarly some methods now return E_NOTIMPL.
* Added some utilities to make use of COM interfaces and concepts, easier.

BUG=webrtc:10374
TBR=mbonadei@webrtc.org

Change-Id: Iaedb1157d37ef5d5c75f727dba3d7de75ce22cd8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/125086
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Christian Fremerey <chfremer@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27072}
This commit is contained in:
Tommi
2019-03-12 09:59:05 +01:00
committed by Commit Bot
parent 2594f27bad
commit 30e60d6fd9
8 changed files with 1085 additions and 456 deletions

View File

@ -107,8 +107,6 @@ if (!build_with_chromium) {
"windows/BasePin.cpp",
"windows/MediaType.cpp",
]
} else {
deps += [ "//third_party/winsdk_samples" ]
}
}

View File

@ -16,7 +16,6 @@
#include "rtc_base/string_utils.h"
#include <dvdmedia.h>
#include <streams.h>
namespace webrtc {
namespace videocapturemodule {
@ -519,7 +518,7 @@ int32_t DeviceInfoDS::CreateCapabilityMap(const char* deviceUniqueIdUTF8)
<< " type:" << static_cast<int>(capability.videoType)
<< " fps:" << capability.maxFPS;
}
DeleteMediaType(pmt);
FreeMediaType(pmt);
pmt = NULL;
}
RELEASE_AND_CLEAR(streamConfig);

View File

@ -14,6 +14,8 @@
#include "modules/video_capture/windows/help_functions_ds.h"
#include "rtc_base/logging.h"
#include <cguid.h>
namespace webrtc {
@ -101,5 +103,56 @@ BOOL PinMatchesCategory(IPin* pPin, REFGUID Category) {
}
return bFound;
}
void ResetMediaType(AM_MEDIA_TYPE* media_type) {
if (!media_type)
return;
if (media_type->cbFormat != 0) {
CoTaskMemFree(media_type->pbFormat);
media_type->cbFormat = 0;
media_type->pbFormat = nullptr;
}
if (media_type->pUnk) {
media_type->pUnk->Release();
media_type->pUnk = nullptr;
}
}
void FreeMediaType(AM_MEDIA_TYPE* media_type) {
if (!media_type)
return;
ResetMediaType(media_type);
CoTaskMemFree(media_type);
}
HRESULT CopyMediaType(AM_MEDIA_TYPE* target, const AM_MEDIA_TYPE* source) {
RTC_DCHECK_NE(source, target);
*target = *source;
if (source->cbFormat != 0) {
RTC_DCHECK(source->pbFormat);
target->pbFormat =
reinterpret_cast<BYTE*>(CoTaskMemAlloc(source->cbFormat));
if (target->pbFormat == nullptr) {
target->cbFormat = 0;
return E_OUTOFMEMORY;
} else {
CopyMemory(target->pbFormat, source->pbFormat, target->cbFormat);
}
}
if (target->pUnk != nullptr)
target->pUnk->AddRef();
return S_OK;
}
wchar_t* DuplicateWideString(const wchar_t* str) {
size_t len = lstrlenW(str);
wchar_t* ret =
reinterpret_cast<LPWSTR>(CoTaskMemAlloc((len + 1) * sizeof(wchar_t)));
lstrcpyW(ret, str);
return ret;
}
} // namespace videocapturemodule
} // namespace webrtc

View File

@ -13,6 +13,12 @@
#include <dshow.h>
#include <type_traits>
#include <utility>
#include "api/scoped_refptr.h"
#include "rtc_base/ref_counter.h"
DEFINE_GUID(MEDIASUBTYPE_I420,
0x30323449,
0x0000,
@ -51,6 +57,61 @@ LONGLONG GetMaxOfFrameArray(LONGLONG* maxFps, long size);
IPin* GetInputPin(IBaseFilter* filter);
IPin* GetOutputPin(IBaseFilter* filter, REFGUID Category);
BOOL PinMatchesCategory(IPin* pPin, REFGUID Category);
void ResetMediaType(AM_MEDIA_TYPE* media_type);
void FreeMediaType(AM_MEDIA_TYPE* media_type);
HRESULT CopyMediaType(AM_MEDIA_TYPE* target, const AM_MEDIA_TYPE* source);
// Helper function to make using scoped_refptr with COM interface pointers
// a little less awkward. rtc::scoped_refptr doesn't support the & operator
// or a way to receive values via an out ptr.
// The function is intentionally not called QueryInterface to make things less
// confusing for the compiler to figure out what the caller wants to do when
// called from within the context of a class that also implements COM
// interfaces.
template <class T>
HRESULT GetComInterface(IUnknown* object, rtc::scoped_refptr<T>* ptr) {
// This helper function is not meant to magically free ptr. If we do that
// we add code bloat to most places where it's not needed and make the code
// less readable since it's not clear at the call site that the pointer
// would get freed even inf QI() fails.
RTC_DCHECK(!ptr->get());
void* new_ptr = nullptr;
HRESULT hr = object->QueryInterface(__uuidof(T), &new_ptr);
if (SUCCEEDED(hr))
ptr->swap(reinterpret_cast<T**>(&new_ptr));
return hr;
}
// Provides a reference count implementation for COM (IUnknown derived) classes.
// The implementation uses atomics for managing the ref count.
template <class T>
class ComRefCount : public T {
public:
ComRefCount() {}
template <class P0>
explicit ComRefCount(P0&& p0) : T(std::forward<P0>(p0)) {}
STDMETHOD_(ULONG, AddRef)() override {
ref_count_.IncRef();
return 1;
}
STDMETHOD_(ULONG, Release)() override {
const auto status = ref_count_.DecRef();
if (status == rtc::RefCountReleaseStatus::kDroppedLastRef) {
delete this;
return 0;
}
return 1;
}
protected:
~ComRefCount() {}
private:
webrtc::webrtc_impl::RefCounter ref_count_{0};
};
} // namespace videocapturemodule
} // namespace webrtc

File diff suppressed because it is too large Load Diff

View File

@ -11,84 +11,151 @@
#ifndef MODULES_VIDEO_CAPTURE_MAIN_SOURCE_WINDOWS_SINK_FILTER_DS_H_
#define MODULES_VIDEO_CAPTURE_MAIN_SOURCE_WINDOWS_SINK_FILTER_DS_H_
#include <streams.h> // Include base DS filter header files
#include <dshow.h>
#include <atomic>
#include <memory>
#include <vector>
#include "modules/video_capture/video_capture_defines.h"
#include "modules/video_capture/windows/help_functions_ds.h"
#include "rtc_base/thread_annotations.h"
#include "rtc_base/thread_checker.h"
namespace webrtc {
namespace videocapturemodule {
// forward declaration
// forward declarations
class CaptureSinkFilter;
/**
* input pin for camera input
*
*/
class CaptureInputPin : public CBaseInputPin {
// Input pin for camera input
// Implements IMemInputPin, IPin.
class CaptureInputPin : public IMemInputPin, public IPin {
public:
VideoCaptureCapability _requestedCapability;
VideoCaptureCapability _resultingCapability;
HANDLE _threadHandle;
CaptureInputPin(CaptureSinkFilter* filter);
CaptureInputPin(IN TCHAR* szName,
IN CaptureSinkFilter* pFilter,
IN CCritSec* pLock,
OUT HRESULT* pHr,
IN LPCWSTR pszName);
~CaptureInputPin() override;
HRESULT SetRequestedCapability(const VideoCaptureCapability& capability);
HRESULT GetMediaType(IN int iPos, OUT CMediaType* pmt) override;
HRESULT CheckMediaType(IN const CMediaType* pmt) override;
STDMETHODIMP Receive(IN IMediaSample*) override;
HRESULT SetMatchingMediaType(const VideoCaptureCapability& capability);
};
// Notifications from the filter.
void OnFilterActivated();
void OnFilterDeactivated();
class CaptureSinkFilter : public CBaseFilter {
public:
CaptureSinkFilter(const IN TCHAR* tszName,
IN LPUNKNOWN punk,
OUT HRESULT* phr,
VideoCaptureExternal& captureObserver);
~CaptureSinkFilter() override;
// --------------------------------------------------------------------
// class methods
void ProcessCapturedFrame(unsigned char* pBuffer,
size_t length,
const VideoCaptureCapability& frameInfo);
// explicit receiver lock aquisition and release
void LockReceive() { m_crtRecv.Lock(); }
void UnlockReceive() { m_crtRecv.Unlock(); }
// explicit filter lock aquisition and release
void LockFilter() { m_crtFilter.Lock(); }
void UnlockFilter() { m_crtFilter.Unlock(); }
void SetFilterGraph(IGraphBuilder* graph); // Used if EVR
// --------------------------------------------------------------------
// COM interfaces
STDMETHOD(QueryInterface)(REFIID riid, void** ppv) override;
STDMETHOD_(ULONG, AddRef)() override;
STDMETHOD_(ULONG, Release)() override;
STDMETHOD(SetMatchingMediaType)(const VideoCaptureCapability& capability);
// --------------------------------------------------------------------
// CBaseFilter methods
int GetPinCount() override;
CBasePin* GetPin(IN int Index) override;
STDMETHOD(Pause)() override;
STDMETHOD(Stop)() override;
STDMETHOD(GetClassID)(OUT CLSID* pCLSID) override;
// --------------------------------------------------------------------
// class factory calls this
static CUnknown* CreateInstance(IN LPUNKNOWN punk, OUT HRESULT* phr);
protected:
virtual ~CaptureInputPin();
private:
CCritSec m_crtFilter; // filter lock
CCritSec m_crtRecv; // receiver lock; always acquire before filter lock
CaptureInputPin* m_pInput;
VideoCaptureExternal& _captureObserver;
CaptureSinkFilter* Filter() const;
HRESULT AttemptConnection(IPin* receive_pin, const AM_MEDIA_TYPE* media_type);
std::vector<AM_MEDIA_TYPE*> DetermineCandidateFormats(
IPin* receive_pin,
const AM_MEDIA_TYPE* media_type);
void ClearAllocator(bool decommit);
HRESULT CheckDirection(IPin* pin) const;
// IUnknown
STDMETHOD(QueryInterface)(REFIID riid, void** ppv) override;
// clang-format off
// clang isn't sure what to do with the longer STDMETHOD() function
// declarations.
// IPin
STDMETHOD(Connect)(IPin* receive_pin,
const AM_MEDIA_TYPE* media_type) override;
STDMETHOD(ReceiveConnection)(IPin* connector,
const AM_MEDIA_TYPE* media_type) override;
STDMETHOD(Disconnect)() override;
STDMETHOD(ConnectedTo)(IPin** pin) override;
STDMETHOD(ConnectionMediaType)(AM_MEDIA_TYPE* media_type) override;
STDMETHOD(QueryPinInfo)(PIN_INFO* info) override;
STDMETHOD(QueryDirection)(PIN_DIRECTION* pin_dir) override;
STDMETHOD(QueryId)(LPWSTR* id) override;
STDMETHOD(QueryAccept)(const AM_MEDIA_TYPE* media_type) override;
STDMETHOD(EnumMediaTypes)(IEnumMediaTypes** types) override;
STDMETHOD(QueryInternalConnections)(IPin** pins, ULONG* count) override;
STDMETHOD(EndOfStream)() override;
STDMETHOD(BeginFlush)() override;
STDMETHOD(EndFlush)() override;
STDMETHOD(NewSegment)(REFERENCE_TIME start, REFERENCE_TIME stop,
double rate) override;
// IMemInputPin
STDMETHOD(GetAllocator)(IMemAllocator** allocator) override;
STDMETHOD(NotifyAllocator)(IMemAllocator* allocator, BOOL read_only) override;
STDMETHOD(GetAllocatorRequirements)(ALLOCATOR_PROPERTIES* props) override;
STDMETHOD(Receive)(IMediaSample* sample) override;
STDMETHOD(ReceiveMultiple)(IMediaSample** samples, long count,
long* processed) override;
STDMETHOD(ReceiveCanBlock)() override;
// clang-format on
rtc::ThreadChecker main_checker_;
rtc::ThreadChecker capture_checker_;
VideoCaptureCapability requested_capability_ RTC_GUARDED_BY(main_checker_);
// Accessed on the main thread when Filter()->IsStopped() (capture thread not
// running), otherwise accessed on the capture thread.
VideoCaptureCapability resulting_capability_;
DWORD capture_thread_id_ = 0;
rtc::scoped_refptr<IMemAllocator> allocator_ RTC_GUARDED_BY(main_checker_);
rtc::scoped_refptr<IPin> receive_pin_ RTC_GUARDED_BY(main_checker_);
std::atomic_bool flushing_{false};
std::atomic_bool runtime_error_{false};
// Holds a referenceless pointer to the owning filter, the name and
// direction of the pin. The filter pointer can be considered const.
PIN_INFO info_ = {};
AM_MEDIA_TYPE media_type_ RTC_GUARDED_BY(main_checker_) = {};
};
// Implement IBaseFilter (including IPersist and IMediaFilter).
class CaptureSinkFilter : public IBaseFilter {
public:
CaptureSinkFilter(VideoCaptureExternal* capture_observer);
HRESULT SetRequestedCapability(const VideoCaptureCapability& capability);
// Called on the capture thread.
void ProcessCapturedFrame(unsigned char* buffer,
size_t length,
const VideoCaptureCapability& frame_info);
void NotifyEvent(long code, LONG_PTR param1, LONG_PTR param2);
bool IsStopped() const;
// IUnknown
STDMETHOD(QueryInterface)(REFIID riid, void** ppv) override;
// IPersist
STDMETHOD(GetClassID)(CLSID* clsid) override;
// IMediaFilter.
STDMETHOD(GetState)(DWORD msecs, FILTER_STATE* state) override;
STDMETHOD(SetSyncSource)(IReferenceClock* clock) override;
STDMETHOD(GetSyncSource)(IReferenceClock** clock) override;
STDMETHOD(Pause)() override;
STDMETHOD(Run)(REFERENCE_TIME start) override;
STDMETHOD(Stop)() override;
// IBaseFilter
STDMETHOD(EnumPins)(IEnumPins** pins) override;
STDMETHOD(FindPin)(LPCWSTR id, IPin** pin) override;
STDMETHOD(QueryFilterInfo)(FILTER_INFO* info) override;
STDMETHOD(JoinFilterGraph)(IFilterGraph* graph, LPCWSTR name) override;
STDMETHOD(QueryVendorInfo)(LPWSTR* vendor_info) override;
protected:
virtual ~CaptureSinkFilter();
private:
rtc::ThreadChecker main_checker_;
const rtc::scoped_refptr<ComRefCount<CaptureInputPin>> input_pin_;
VideoCaptureExternal* const capture_observer_;
FILTER_INFO info_ RTC_GUARDED_BY(main_checker_) = {};
// Set/cleared in JoinFilterGraph. The filter must be stopped (no capture)
// at that time, so no lock is required. While the state is not stopped,
// the sink will be used from the capture thread.
IMediaEventSink* sink_ = nullptr;
FILTER_STATE state_ RTC_GUARDED_BY(main_checker_) = State_Stopped;
};
} // namespace videocapturemodule
} // namespace webrtc

View File

@ -23,7 +23,6 @@ VideoCaptureDS::VideoCaptureDS()
: _captureFilter(NULL),
_graphBuilder(NULL),
_mediaControl(NULL),
_sinkFilter(NULL),
_inputSendPin(NULL),
_outputCapturePin(NULL),
_dvFilter(NULL),
@ -35,8 +34,8 @@ VideoCaptureDS::~VideoCaptureDS() {
_mediaControl->Stop();
}
if (_graphBuilder) {
if (_sinkFilter)
_graphBuilder->RemoveFilter(_sinkFilter);
if (sink_filter_)
_graphBuilder->RemoveFilter(sink_filter_);
if (_captureFilter)
_graphBuilder->RemoveFilter(_captureFilter);
if (_dvFilter)
@ -46,7 +45,6 @@ VideoCaptureDS::~VideoCaptureDS() {
RELEASE_AND_CLEAR(_outputCapturePin);
RELEASE_AND_CLEAR(_captureFilter); // release the capture device
RELEASE_AND_CLEAR(_sinkFilter);
RELEASE_AND_CLEAR(_dvFilter);
RELEASE_AND_CLEAR(_mediaControl);
@ -101,20 +99,15 @@ int32_t VideoCaptureDS::Init(const char* deviceUniqueIdUTF8) {
}
// Create the sink filte used for receiving Captured frames.
_sinkFilter = new CaptureSinkFilter(SINK_FILTER_NAME, NULL, &hr, *this);
if (hr != S_OK) {
RTC_LOG(LS_INFO) << "Failed to create send filter";
return -1;
}
_sinkFilter->AddRef();
sink_filter_ = new ComRefCount<CaptureSinkFilter>(this);
hr = _graphBuilder->AddFilter(_sinkFilter, SINK_FILTER_NAME);
hr = _graphBuilder->AddFilter(sink_filter_, SINK_FILTER_NAME);
if (FAILED(hr)) {
RTC_LOG(LS_INFO) << "Failed to add the send filter to the graph.";
return -1;
}
_inputSendPin = GetInputPin(_sinkFilter);
_inputSendPin = GetInputPin(sink_filter_);
if (!_inputSendPin) {
RTC_LOG(LS_INFO) << "Failed to get input send pin";
return -1;
@ -164,6 +157,7 @@ int32_t VideoCaptureDS::StopCapture() {
}
return 0;
}
bool VideoCaptureDS::CaptureStarted() {
OAFilterState state = 0;
HRESULT hr = _mediaControl->GetState(1000, &state);
@ -173,6 +167,7 @@ bool VideoCaptureDS::CaptureStarted() {
RTC_LOG(LS_INFO) << "CaptureStarted " << state;
return state == State_Running;
}
int32_t VideoCaptureDS::CaptureSettings(VideoCaptureCapability& settings) {
settings = _requestedCapability;
return 0;
@ -234,7 +229,7 @@ int32_t VideoCaptureDS::SetCameraOutput(
}
// Set the sink filter to request this capability
_sinkFilter->SetMatchingMediaType(capability);
sink_filter_->SetRequestedCapability(capability);
// Order the capture device to use this capability
hr += streamConfig->SetFormat(pmt);
@ -279,6 +274,7 @@ int32_t VideoCaptureDS::DisconnectGraph() {
}
return 0;
}
HRESULT VideoCaptureDS::ConnectDVCamera() {
HRESULT hr = S_OK;
@ -320,7 +316,6 @@ HRESULT VideoCaptureDS::ConnectDVCamera() {
RTC_LOG(LS_INFO) << "Failed to connect capture device to the send graph: "
<< hr;
}
return hr;
}
return hr;
}

View File

@ -11,6 +11,7 @@
#ifndef MODULES_VIDEO_CAPTURE_MAIN_SOURCE_WINDOWS_VIDEO_CAPTURE_DS_H_
#define MODULES_VIDEO_CAPTURE_MAIN_SOURCE_WINDOWS_VIDEO_CAPTURE_DS_H_
#include "api/scoped_refptr.h"
#include "modules/video_capture/video_capture_impl.h"
#include "modules/video_capture/windows/device_info_ds.h"
@ -59,7 +60,7 @@ class VideoCaptureDS : public VideoCaptureImpl {
IBaseFilter* _captureFilter;
IGraphBuilder* _graphBuilder;
IMediaControl* _mediaControl;
CaptureSinkFilter* _sinkFilter;
rtc::scoped_refptr<CaptureSinkFilter> sink_filter_;
IPin* _inputSendPin;
IPin* _outputCapturePin;