Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
win-uvc.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 #pragma once
4 
5 #include "../backend.h"
6 #include "win-helpers.h"
7 
8 #include <mfidl.h>
9 #include <mfreadwrite.h>
10 #include <atlcomcli.h>
11 #include <strmif.h>
12 #include <Ks.h>
13 #include <ksproxy.h>
14 #include <unordered_map>
15 #include <mutex>
16 #include <atomic>
17 
18 #ifndef KSCATEGORY_SENSOR_CAMERA
19 DEFINE_GUIDSTRUCT("24E552D7-6523-47F7-A647-D3465BF1F5CA", KSCATEGORY_SENSOR_CAMERA);
20 #define KSCATEGORY_SENSOR_CAMERA DEFINE_GUIDNAMED(KSCATEGORY_SENSOR_CAMERA)
21 #endif // !KSCATEGORY_SENSOR_CAMERA
22 
23 
24 static const std::vector<std::vector<std::pair<GUID, GUID>>> attributes_params = {
25  {
26  { MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID },
27  { MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_CATEGORY, KSCATEGORY_SENSOR_CAMERA }
28  },
29  {
30  { MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID }
31  },
32 };
33 
34 namespace librealsense
35 {
36  namespace platform
37  {
38  class wmf_backend;
39 
41  {
44  };
45 
46  typedef std::function<void(const uvc_device_info&, IMFActivate*)>
48 
49  class wmf_uvc_device : public std::enable_shared_from_this<wmf_uvc_device>,
50  public uvc_device
51  {
52  public:
53  wmf_uvc_device(const uvc_device_info& info, std::shared_ptr<const wmf_backend> backend);
54  ~wmf_uvc_device();
55 
56  void probe_and_commit(stream_profile profile, frame_callback callback, int buffers) override;
57  void stream_on(std::function<void(const notification& n)> error_handler = [](const notification& n){}) override;
58  void start_callbacks() override;
59  void stop_callbacks() override;
60  void close(stream_profile profile) override;
61  void set_power_state(power_state state) override;
62  power_state get_power_state() const override { return _power_state; }
63  std::vector<stream_profile> get_profiles() const override;
64 
65  static bool is_connected(const uvc_device_info& info);
66  static void foreach_uvc_device(enumeration_callback action);
67 
68  void init_xu(const extension_unit& xu) override;
69  bool set_xu(const extension_unit& xu, uint8_t ctrl, const uint8_t* data, int len) override;
70  bool get_xu(const extension_unit& xu, uint8_t ctrl, uint8_t* data, int len) const override;
71  control_range get_xu_range(const extension_unit& xu, uint8_t ctrl, int len) const override;
72 
73  bool get_pu(rs2_option opt, int32_t& value) const override;
74  bool set_pu(rs2_option opt, int value) override;
75  control_range get_pu_range(rs2_option opt) const override;
76 
77  void lock() const override { _systemwide_lock.lock(); }
78  void unlock() const override { _systemwide_lock.unlock(); }
79 
80  std::string get_device_location() const override { return _location; }
81  usb_spec get_usb_specification() const override { return _device_usb_spec; }
82 
83  IAMVideoProcAmp* get_video_proc() const
84  {
85  if (!_video_proc.p)
86  throw std::runtime_error("The device does not support adjusting the qualities of an incoming video signal, such as brightness, contrast, hue, saturation, gamma, and sharpness.");
87  return _video_proc.p;
88  }
89 
90  IAMCameraControl* get_camera_control() const
91  {
92  if (!_camera_control.p)
93  throw std::runtime_error("The device does not support camera settings such as zoom, pan, aperture adjustment, or shutter speed.");
94  return _camera_control.p;
95  }
96 
97  private:
98  friend class source_reader_callback;
99 
100  void play_profile(stream_profile profile, frame_callback callback);
101  void stop_stream_cleanup(const stream_profile& profile, std::vector<profile_and_callback>::iterator& elem);
102  void flush(int sIndex);
103  void check_connection() const;
104  IKsControl* get_ks_control(const extension_unit& xu) const;
105  std::vector<std::pair<stream_profile, int>> get_stream_profiles_and_indexes() const;
106  int get_stream_index_by_profile(const stream_profile& profile) const;
107 
108  const uvc_device_info _info;
109  power_state _power_state = D3;
110 
111  CComPtr<source_reader_callback> _callback = nullptr;
112  CComPtr<IMFSourceReader> _reader = nullptr;
113  CComPtr<IMFMediaSource> _source = nullptr;
114  CComPtr<IMFActivate> _activate = nullptr;
115  CComPtr<IMFAttributes> _device_attrs = nullptr;
116  CComPtr<IMFAttributes> _reader_attrs = nullptr;
117 
118  CComPtr<IAMCameraControl> _camera_control = nullptr;
119  CComPtr<IAMVideoProcAmp> _video_proc = nullptr;
120  std::unordered_map<int, CComPtr<IKsControl>> _ks_controls;
121 
122  manual_reset_event _is_flushed;
123  manual_reset_event _has_started;
124  HRESULT _readsample_result = S_OK;
125 
126  uint16_t _streamIndex;
127  std::vector<profile_and_callback> _streams;
128  std::mutex _streams_mutex;
129 
130  std::shared_ptr<const wmf_backend> _backend;
131 
132  named_mutex _systemwide_lock;
133  std::string _location;
134  usb_spec _device_usb_spec;
135  std::vector<stream_profile> _profiles;
136  std::vector<frame_callback> _frame_callbacks;
137  bool _streaming = false;
138  std::atomic<bool> _is_started = false;
139  };
140 
141  class source_reader_callback : public IMFSourceReaderCallback
142  {
143  public:
144  explicit source_reader_callback(std::weak_ptr<wmf_uvc_device> owner) : _owner(owner)
145  {
146  };
148  STDMETHODIMP QueryInterface(REFIID iid, void** ppv) override;
149  STDMETHODIMP_(ULONG) AddRef() override;
150  STDMETHODIMP_(ULONG) Release() override;
151  STDMETHODIMP OnReadSample(HRESULT /*hrStatus*/,
152  DWORD dwStreamIndex,
153  DWORD /*dwStreamFlags*/,
154  LONGLONG /*llTimestamp*/,
155  IMFSample *sample) override;
156  STDMETHODIMP OnEvent(DWORD /*sidx*/, IMFMediaEvent* /*event*/) override;
157  STDMETHODIMP OnFlush(DWORD) override;
158  private:
159  std::weak_ptr<wmf_uvc_device> _owner;
160  long _refCount = 0;
161  };
162 
163  }
164 }
Definition: win-helpers.h:67
frame_callback callback
Definition: win-uvc.h:43
Definition: backend.h:380
virtual ~source_reader_callback()
Definition: win-uvc.h:147
Definition: backend.h:652
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
#define KSCATEGORY_SENSOR_CAMERA
Definition: win-uvc.h:20
power_state get_power_state() const override
Definition: win-uvc.h:62
usb_spec
Definition: backend.h:180
void unlock() const override
Definition: win-uvc.h:78
std::string get_device_location() const override
Definition: win-uvc.h:80
Definition: algo.h:16
Definition: backend-v4l2.h:50
power_state
Definition: backend.h:123
Definition: backend.h:126
std::function< void(stream_profile, frame_object, std::function< void()>)> frame_callback
Definition: backend.h:177
IAMCameraControl * get_camera_control() const
Definition: win-uvc.h:90
stream_profile profile
Definition: win-uvc.h:42
Definition: backend.h:413
Definition: types.h:920
DEFINE_GUIDSTRUCT("24E552D7-6523-47F7-A647-D3465BF1F5CA", KSCATEGORY_SENSOR_CAMERA)
void lock() const override
Definition: win-uvc.h:77
source_reader_callback(std::weak_ptr< wmf_uvc_device > owner)
Definition: win-uvc.h:144
usb_spec get_usb_specification() const override
Definition: win-uvc.h:81
std::function< void(const uvc_device_info &, IMFActivate *)> enumeration_callback
Definition: win-uvc.h:47
IAMVideoProcAmp * get_video_proc() const
Definition: win-uvc.h:83