• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // Windows specific implementation of VideoCaptureDevice.
6 // DirectShow is used for capturing. DirectShow provide its own threads
7 // for capturing.
8 
9 #ifndef MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_
10 #define MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_
11 
12 // Avoid including strsafe.h via dshow as it will cause build warnings.
13 #define NO_DSHOW_STRSAFE
14 #include <dshow.h>
15 
16 #include <map>
17 #include <string>
18 
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"
27 
28 namespace media {
29 
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 {
35  public:
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 {
40    public:
ScopedMediaType()41     ScopedMediaType() : media_type_(NULL) {}
~ScopedMediaType()42     ~ScopedMediaType() { Free(); }
43 
44     AM_MEDIA_TYPE* operator->() { return media_type_; }
get()45     AM_MEDIA_TYPE* get() { return media_type_; }
46     void Free();
47     AM_MEDIA_TYPE** Receive();
48 
49    private:
50     void FreeMediaType(AM_MEDIA_TYPE* mt);
51     void DeleteMediaType(AM_MEDIA_TYPE* mt);
52 
53     AM_MEDIA_TYPE* media_type_;
54   };
55 
56   static HRESULT GetDeviceFilter(const Name& device_name,
57                                  IBaseFilter** filter);
58   static bool PinMatchesCategory(IPin* pin, REFGUID category);
59   static base::win::ScopedComPtr<IPin> GetPin(IBaseFilter* filter,
60                                               PIN_DIRECTION pin_dir,
61                                               REFGUID category);
62   static VideoPixelFormat TranslateMediaSubtypeToPixelFormat(
63       const GUID& sub_type);
64 
65   explicit VideoCaptureDeviceWin(const Name& device_name);
66   virtual ~VideoCaptureDeviceWin();
67   // Opens the device driver for this device.
68   bool Init();
69 
70   // VideoCaptureDevice implementation.
71   virtual void AllocateAndStart(
72       const VideoCaptureParams& params,
73       scoped_ptr<VideoCaptureDevice::Client> client) OVERRIDE;
74   virtual void StopAndDeAllocate() OVERRIDE;
75 
76  private:
77   enum InternalState {
78     kIdle,  // The device driver is opened but camera is not in use.
79     kCapturing,  // Video is being captured.
80     kError  // Error accessing HW functions.
81             // User needs to recover by destroying the object.
82   };
83 
84   // Implements SinkFilterObserver.
85   virtual void FrameReceived(const uint8* buffer, int length);
86 
87   bool CreateCapabilityMap();
88   void SetAntiFlickerInCaptureFilter();
89   void SetErrorState(const std::string& reason);
90 
91   Name device_name_;
92   InternalState state_;
93   scoped_ptr<VideoCaptureDevice::Client> client_;
94 
95   base::win::ScopedComPtr<IBaseFilter> capture_filter_;
96   base::win::ScopedComPtr<IGraphBuilder> graph_builder_;
97   base::win::ScopedComPtr<IMediaControl> media_control_;
98   base::win::ScopedComPtr<IPin> input_sink_pin_;
99   base::win::ScopedComPtr<IPin> output_capture_pin_;
100   // Used when using a MJPEG decoder.
101   base::win::ScopedComPtr<IBaseFilter> mjpg_filter_;
102   base::win::ScopedComPtr<IPin> input_mjpg_pin_;
103   base::win::ScopedComPtr<IPin> output_mjpg_pin_;
104 
105   scoped_refptr<SinkFilter> sink_filter_;
106 
107   // Map of all capabilities this device support.
108   CapabilityList capabilities_;
109   VideoCaptureFormat capture_format_;
110 
111   DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceWin);
112 };
113 
114 }  // namespace media
115 
116 #endif  // MEDIA_VIDEO_CAPTURE_WIN_VIDEO_CAPTURE_DEVICE_WIN_H_
117