summaryrefslogtreecommitdiff
path: root/chromium/media/capture/video/win/video_capture_device_factory_win.h
blob: f62dfdb5c95d1fac58edef7b55797c8cb9f0a509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Implementation of a VideoCaptureDeviceFactory class for Windows platforms.

#ifndef MEDIA_CAPTURE_VIDEO_WIN_VIDEO_CAPTURE_DEVICE_FACTORY_WIN_H_
#define MEDIA_CAPTURE_VIDEO_WIN_VIDEO_CAPTURE_DEVICE_FACTORY_WIN_H_

#include <d3d11.h>
// Avoid including strsafe.h via dshow as it will cause build warnings.
#define NO_DSHOW_STRSAFE
#include <dshow.h>
#include <mfidl.h>
#include <windows.devices.enumeration.h>
#include <wrl.h>

#include "base/macros.h"
#include "base/threading/thread.h"
#include "media/base/win/dxgi_device_manager.h"
#include "media/capture/video/video_capture_device_factory.h"

namespace media {

using ABI::Windows::Foundation::IAsyncOperation;
using ABI::Windows::Devices::Enumeration::DeviceInformationCollection;

// Extension of VideoCaptureDeviceFactory to create and manipulate Windows
// devices, via either DirectShow or MediaFoundation APIs.
class CAPTURE_EXPORT VideoCaptureDeviceFactoryWin
    : public VideoCaptureDeviceFactory {
 public:
  static bool PlatformSupportsMediaFoundation();

  VideoCaptureDeviceFactoryWin();

  VideoCaptureDeviceFactoryWin(const VideoCaptureDeviceFactoryWin&) = delete;
  VideoCaptureDeviceFactoryWin& operator=(const VideoCaptureDeviceFactoryWin&) =
      delete;

  ~VideoCaptureDeviceFactoryWin() override;

  std::unique_ptr<VideoCaptureDevice> CreateDevice(
      const VideoCaptureDeviceDescriptor& device_descriptor) override;
  void GetDevicesInfo(GetDevicesInfoCallback callback) override;

  void set_use_media_foundation_for_testing(bool use) {
    use_media_foundation_ = use;
  }

  void set_use_d3d11_with_media_foundation_for_testing(bool use) {
    use_d3d11_with_media_foundation_ = use;
  }

 protected:
  // Protected and virtual for testing.
  virtual bool CreateDeviceEnumMonikerDirectShow(IEnumMoniker** enum_moniker);
  virtual bool CreateDeviceFilterDirectShow(const std::string& device_id,
                                            IBaseFilter** capture_filter);
  virtual bool CreateDeviceFilterDirectShow(
      Microsoft::WRL::ComPtr<IMoniker> moniker,
      IBaseFilter** capture_filter);
  virtual bool CreateDeviceSourceMediaFoundation(const std::string& device_id,
                                                 VideoCaptureApi capture_api,
                                                 IMFMediaSource** source_out);
  virtual bool CreateDeviceSourceMediaFoundation(
      Microsoft::WRL::ComPtr<IMFAttributes> attributes,
      IMFMediaSource** source);
  virtual bool EnumerateDeviceSourcesMediaFoundation(
      Microsoft::WRL::ComPtr<IMFAttributes> attributes,
      IMFActivate*** devices,
      UINT32* count);
  virtual VideoCaptureFormats GetSupportedFormatsDirectShow(
      Microsoft::WRL::ComPtr<IBaseFilter> capture_filter,
      const std::string& display_name);
  virtual VideoCaptureFormats GetSupportedFormatsMediaFoundation(
      Microsoft::WRL::ComPtr<IMFMediaSource> source,
      const std::string& display_name);

  bool use_d3d11_with_media_foundation_for_testing() {
    return use_d3d11_with_media_foundation_;
  }

  scoped_refptr<DXGIDeviceManager> dxgi_device_manager_for_testing() {
    return dxgi_device_manager_;
  }

 private:
  void EnumerateDevicesUWP(std::vector<VideoCaptureDeviceInfo> devices_info,
                           GetDevicesInfoCallback result_callback);
  void FoundAllDevicesUWP(
      std::vector<VideoCaptureDeviceInfo> devices_info,
      GetDevicesInfoCallback result_callback,
      IAsyncOperation<DeviceInformationCollection*>* operation);
  void DeviceInfoReady(std::vector<VideoCaptureDeviceInfo> devices_info,
                       GetDevicesInfoCallback result_callback);
  std::vector<VideoCaptureDeviceInfo> GetDevicesInfoMediaFoundation();
  void AugmentDevicesListWithDirectShowOnlyDevices(
      std::vector<VideoCaptureDeviceInfo>* devices_info);
  std::vector<VideoCaptureDeviceInfo> GetDevicesInfoDirectShow();

  bool use_media_foundation_;
  bool use_d3d11_with_media_foundation_;

  // For calling WinRT methods on a COM initiated thread.
  base::Thread com_thread_;
  scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
  std::unordered_set<IAsyncOperation<DeviceInformationCollection*>*> async_ops_;
  // For hardware acceleration in MediaFoundation capture engine
  scoped_refptr<DXGIDeviceManager> dxgi_device_manager_;
  base::WeakPtrFactory<VideoCaptureDeviceFactoryWin> weak_ptr_factory_{this};
};

}  // namespace media

#endif  // MEDIA_CAPTURE_VIDEO_WIN_VIDEO_CAPTURE_DEVICE_FACTORY_WIN_H_