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. DirectShow is used for
6 // capturing. DirectShow provide its own threads for capturing.
8 #ifndef MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_
9 #define MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_
11 // Avoid including strsafe.h via dshow as it will cause build warnings.
12 #define NO_DSHOW_STRSAFE
18 #include "base/threading/thread_checker.h"
19 #include "base/win/scoped_comptr.h"
20 #include "media/base/video_capture_types.h"
21 #include "media/capture/video/video_capture_device.h"
22 #include "media/capture/video/win/capability_list_win.h"
23 #include "media/capture/video/win/sink_filter_win.h"
24 #include "media/capture/video/win/sink_input_pin_win.h"
28 // All the methods in the class can only be run on a COM initialized thread.
29 class VideoCaptureDeviceWin
: public VideoCaptureDevice
,
30 public SinkFilterObserver
{
32 // A utility class that wraps the AM_MEDIA_TYPE type and guarantees that
33 // we free the structure when exiting the scope. DCHECKing is also done to
34 // avoid memory leaks.
35 class ScopedMediaType
{
37 ScopedMediaType() : media_type_(NULL
) {}
38 ~ScopedMediaType() { Free(); }
40 AM_MEDIA_TYPE
* operator->() { return media_type_
; }
41 AM_MEDIA_TYPE
* get() { return media_type_
; }
43 AM_MEDIA_TYPE
** Receive();
46 void FreeMediaType(AM_MEDIA_TYPE
* mt
);
47 void DeleteMediaType(AM_MEDIA_TYPE
* mt
);
49 AM_MEDIA_TYPE
* media_type_
;
52 static HRESULT
GetDeviceFilter(const std::string
& device_id
,
53 IBaseFilter
** filter
);
54 static base::win::ScopedComPtr
<IPin
> GetPin(IBaseFilter
* filter
,
55 PIN_DIRECTION pin_dir
,
58 static VideoPixelFormat
TranslateMediaSubtypeToPixelFormat(
59 const GUID
& sub_type
);
61 explicit VideoCaptureDeviceWin(const Name
& device_name
);
62 ~VideoCaptureDeviceWin() override
;
63 // Opens the device driver for this device.
66 // VideoCaptureDevice implementation.
67 void AllocateAndStart(const VideoCaptureParams
& params
,
68 scoped_ptr
<VideoCaptureDevice::Client
> client
) override
;
69 void StopAndDeAllocate() override
;
73 kIdle
, // The device driver is opened but camera is not in use.
74 kCapturing
, // Video is being captured.
75 kError
// Error accessing HW functions.
76 // User needs to recover by destroying the object.
79 // Implements SinkFilterObserver.
80 void FrameReceived(const uint8
* buffer
, int length
,
81 base::TimeTicks timestamp
) override
;
83 bool CreateCapabilityMap();
84 void SetAntiFlickerInCaptureFilter(const VideoCaptureParams
& params
);
85 void SetErrorState(const std::string
& reason
);
87 const Name device_name_
;
89 scoped_ptr
<VideoCaptureDevice::Client
> client_
;
91 base::win::ScopedComPtr
<IBaseFilter
> capture_filter_
;
93 base::win::ScopedComPtr
<IGraphBuilder
> graph_builder_
;
94 base::win::ScopedComPtr
<ICaptureGraphBuilder2
> capture_graph_builder_
;
96 base::win::ScopedComPtr
<IMediaControl
> media_control_
;
97 base::win::ScopedComPtr
<IPin
> input_sink_pin_
;
98 base::win::ScopedComPtr
<IPin
> output_capture_pin_
;
100 scoped_refptr
<SinkFilter
> sink_filter_
;
102 // Map of all capabilities this device support.
103 CapabilityList capabilities_
;
104 VideoCaptureFormat capture_format_
;
106 base::ThreadChecker thread_checker_
;
108 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceWin
);
113 #endif // MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_