1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Windows specific implementation of VideoCaptureDevice.
6 // DirectShow is used for capturing. DirectShow provide its own threads
9 #ifndef MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_
10 #define MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_
12 // Avoid including strsafe.h via dshow as it will cause build warnings.
13 #define NO_DSHOW_STRSAFE
19 #include "base/threading/non_thread_safe.h"
20 #include "base/threading/thread.h"
21 #include "base/win/scoped_comptr.h"
22 #include "media/video/capture/video_capture_device.h"
23 #include "media/video/capture/video_capture_types.h"
24 #include "media/video/capture/win/capability_list_win.h"
25 #include "media/video/capture/win/sink_filter_win.h"
26 #include "media/video/capture/win/sink_input_pin_win.h"
30 // All the methods in the class can only be run on a COM initialized thread.
31 class VideoCaptureDeviceWin
32 : public base::NonThreadSafe
,
33 public VideoCaptureDevice
,
34 public SinkFilterObserver
{
36 // A utility class that wraps the AM_MEDIA_TYPE type and guarantees that
37 // we free the structure when exiting the scope. DCHECKing is also done to
38 // avoid memory leaks.
39 class ScopedMediaType
{
41 ScopedMediaType() : media_type_(NULL
) {}
42 ~ScopedMediaType() { Free(); }
44 AM_MEDIA_TYPE
* operator->() { return media_type_
; }
45 AM_MEDIA_TYPE
* get() { return media_type_
; }
47 AM_MEDIA_TYPE
** Receive();
50 void FreeMediaType(AM_MEDIA_TYPE
* mt
);
51 void DeleteMediaType(AM_MEDIA_TYPE
* mt
);
53 AM_MEDIA_TYPE
* media_type_
;
56 static HRESULT
GetDeviceFilter(const std::string
& device_id
,
57 const CLSID device_class_id
,
58 IBaseFilter
** filter
);
59 static base::win::ScopedComPtr
<IPin
> GetPin(IBaseFilter
* filter
,
60 PIN_DIRECTION pin_dir
,
63 static VideoPixelFormat
TranslateMediaSubtypeToPixelFormat(
64 const GUID
& sub_type
);
66 explicit VideoCaptureDeviceWin(const Name
& device_name
);
67 virtual ~VideoCaptureDeviceWin();
68 // Opens the device driver for this device.
71 // VideoCaptureDevice implementation.
72 virtual void AllocateAndStart(
73 const VideoCaptureParams
& params
,
74 scoped_ptr
<VideoCaptureDevice::Client
> client
) override
;
75 virtual void StopAndDeAllocate() override
;
79 kIdle
, // The device driver is opened but camera is not in use.
80 kCapturing
, // Video is being captured.
81 kError
// Error accessing HW functions.
82 // User needs to recover by destroying the object.
85 // Implements SinkFilterObserver.
86 virtual void FrameReceived(const uint8
* buffer
, int length
);
88 bool CreateCapabilityMap();
89 void SetAntiFlickerInCaptureFilter();
90 HRESULT
InstantiateWDMFiltersAndPins();
91 HRESULT
AddWDMCrossbarFilterToGraphAndConnect();
92 void SetErrorState(const std::string
& reason
);
96 scoped_ptr
<VideoCaptureDevice::Client
> client_
;
98 base::win::ScopedComPtr
<IBaseFilter
> capture_filter_
;
99 base::win::ScopedComPtr
<IGraphBuilder
> graph_builder_
;
100 base::win::ScopedComPtr
<IMediaControl
> media_control_
;
101 base::win::ScopedComPtr
<IPin
> input_sink_pin_
;
102 base::win::ScopedComPtr
<IPin
> output_capture_pin_
;
104 // Used for WDM devices as specified by |device_name_|. These devices need a
105 // WDM Crossbar Filter upstream from the Capture filter.
106 base::win::ScopedComPtr
<IBaseFilter
> crossbar_filter_
;
107 base::win::ScopedComPtr
<IPin
> crossbar_video_output_pin_
;
108 base::win::ScopedComPtr
<IPin
> analog_video_input_pin_
;
110 scoped_refptr
<SinkFilter
> sink_filter_
;
112 // Map of all capabilities this device support.
113 CapabilityList capabilities_
;
114 VideoCaptureFormat capture_format_
;
116 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceWin
);
121 #endif // MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_