Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / capture / video / win / video_capture_device_win.h
blobdd7485ec38b5c30f5aa976c5c5517513cd62d8ab
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
7 // for capturing.
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
14 #include <dshow.h>
16 #include <map>
17 #include <string>
19 #include "base/threading/non_thread_safe.h"
20 #include "base/threading/thread.h"
21 #include "base/win/scoped_comptr.h"
22 #include "media/base/video_capture_types.h"
23 #include "media/capture/video/video_capture_device.h"
24 #include "media/capture/video/win/capability_list_win.h"
25 #include "media/capture/video/win/sink_filter_win.h"
26 #include "media/capture/video/win/sink_input_pin_win.h"
28 namespace media {
30 // All the methods in the class can only be run on a COM initialized thread.
31 class VideoCaptureDeviceWin : public base::NonThreadSafe,
32 public VideoCaptureDevice,
33 public SinkFilterObserver {
34 public:
35 // A utility class that wraps the AM_MEDIA_TYPE type and guarantees that
36 // we free the structure when exiting the scope. DCHECKing is also done to
37 // avoid memory leaks.
38 class ScopedMediaType {
39 public:
40 ScopedMediaType() : media_type_(NULL) {}
41 ~ScopedMediaType() { Free(); }
43 AM_MEDIA_TYPE* operator->() { return media_type_; }
44 AM_MEDIA_TYPE* get() { return media_type_; }
45 void Free();
46 AM_MEDIA_TYPE** Receive();
48 private:
49 void FreeMediaType(AM_MEDIA_TYPE* mt);
50 void DeleteMediaType(AM_MEDIA_TYPE* mt);
52 AM_MEDIA_TYPE* media_type_;
55 static HRESULT GetDeviceFilter(const std::string& device_id,
56 const CLSID device_class_id,
57 IBaseFilter** filter);
58 static base::win::ScopedComPtr<IPin> GetPin(IBaseFilter* filter,
59 PIN_DIRECTION pin_dir,
60 REFGUID category,
61 REFGUID major_type);
62 static VideoCapturePixelFormat TranslateMediaSubtypeToPixelFormat(
63 const GUID& sub_type);
65 explicit VideoCaptureDeviceWin(const Name& device_name);
66 ~VideoCaptureDeviceWin() override;
67 // Opens the device driver for this device.
68 bool Init();
70 // VideoCaptureDevice implementation.
71 void AllocateAndStart(const VideoCaptureParams& params,
72 scoped_ptr<VideoCaptureDevice::Client> client) override;
73 void StopAndDeAllocate() override;
75 private:
76 enum InternalState {
77 kIdle, // The device driver is opened but camera is not in use.
78 kCapturing, // Video is being captured.
79 kError // Error accessing HW functions.
80 // User needs to recover by destroying the object.
83 // Implements SinkFilterObserver.
84 void FrameReceived(const uint8* buffer, int length) override;
86 bool CreateCapabilityMap();
87 void SetAntiFlickerInCaptureFilter();
88 void SetErrorState(const std::string& reason);
90 Name device_name_;
91 InternalState state_;
92 scoped_ptr<VideoCaptureDevice::Client> client_;
94 base::win::ScopedComPtr<IBaseFilter> capture_filter_;
96 base::win::ScopedComPtr<IGraphBuilder> graph_builder_;
97 base::win::ScopedComPtr<ICaptureGraphBuilder2> capture_graph_builder_;
99 base::win::ScopedComPtr<IMediaControl> media_control_;
100 base::win::ScopedComPtr<IPin> input_sink_pin_;
101 base::win::ScopedComPtr<IPin> output_capture_pin_;
103 scoped_refptr<SinkFilter> sink_filter_;
105 // Map of all capabilities this device support.
106 CapabilityList capabilities_;
107 VideoCaptureFormat capture_format_;
109 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceWin);
112 } // namespace media
114 #endif // MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_