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);
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 
82  private:
83  friend class source_reader_callback;
84 
85  void play_profile(stream_profile profile, frame_callback callback);
86  void stop_stream_cleanup(const stream_profile& profile, std::vector<profile_and_callback>::iterator& elem);
87  void flush(int sIndex);
88  void check_connection() const;
89  IKsControl* get_ks_control(const extension_unit& xu) const;
90  std::vector<std::pair<stream_profile, int>> get_stream_profiles_and_indexes() const;
91  int get_stream_index_by_profile(const stream_profile& profile) const;
92 
93  const uvc_device_info _info;
94  power_state _power_state = D3;
95 
96  CComPtr<source_reader_callback> _callback = nullptr;
97  CComPtr<IMFSourceReader> _reader = nullptr;
98  CComPtr<IMFMediaSource> _source = nullptr;
99  CComPtr<IMFActivate> _activate = nullptr;
100  CComPtr<IMFAttributes> _device_attrs = nullptr;
101  CComPtr<IMFAttributes> _reader_attrs = nullptr;
102 
103  CComPtr<IAMCameraControl> _camera_control = nullptr;
104  CComPtr<IAMVideoProcAmp> _video_proc = nullptr;
105  std::unordered_map<int, CComPtr<IKsControl>> _ks_controls;
106 
107  manual_reset_event _is_flushed;
108  manual_reset_event _has_started;
109  HRESULT _readsample_result = S_OK;
110 
111  std::vector<profile_and_callback> _streams;
112  std::mutex _streams_mutex;
113 
114  std::shared_ptr<const wmf_backend> _backend;
115 
116  named_mutex _systemwide_lock;
117  std::string _location;
118  std::vector<stream_profile> _profiles;
119  std::vector<frame_callback> _frame_callbacks;
120  bool _streaming = false;
121  std::atomic<bool> _is_started = false;
122  };
123 
124  class source_reader_callback : public IMFSourceReaderCallback
125  {
126  public:
127  explicit source_reader_callback(std::weak_ptr<wmf_uvc_device> owner) : _owner(owner)
128  {
129  };
131  STDMETHODIMP QueryInterface(REFIID iid, void** ppv) override;
132  STDMETHODIMP_(ULONG) AddRef() override;
133  STDMETHODIMP_(ULONG) Release() override;
134  STDMETHODIMP OnReadSample(HRESULT /*hrStatus*/,
135  DWORD dwStreamIndex,
136  DWORD /*dwStreamFlags*/,
137  LONGLONG /*llTimestamp*/,
138  IMFSample *sample) override;
139  STDMETHODIMP OnEvent(DWORD /*sidx*/, IMFMediaEvent* /*event*/) override;
140  STDMETHODIMP OnFlush(DWORD) override;
141  private:
142  std::weak_ptr<wmf_uvc_device> _owner;
143  long _refCount = 0;
144  };
145 
146  }
147 }
wmf_uvc_device(const uvc_device_info &info, std::shared_ptr< const wmf_backend > backend)
static bool is_connected(const uvc_device_info &info)
Definition: win-helpers.h:66
frame_callback callback
Definition: win-uvc.h:43
void lock()
Definition: backend-v4l2.h:57
Definition: backend.h:351
std::vector< stream_profile > get_profiles() const override
virtual ~source_reader_callback()
Definition: win-uvc.h:130
Definition: backend.h:621
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
bool set_xu(const extension_unit &xu, uint8_t ctrl, const uint8_t *data, int len) override
control_range get_xu_range(const extension_unit &xu, uint8_t ctrl, int len) const override
#define KSCATEGORY_SENSOR_CAMERA
Definition: win-uvc.h:20
power_state get_power_state() const override
Definition: win-uvc.h:62
void set_power_state(power_state state) override
void unlock() const override
Definition: win-uvc.h:78
static void foreach_uvc_device(enumeration_callback action)
std::string get_device_location() const override
Definition: win-uvc.h:80
Definition: algo.h:16
void stream_on(std::function< void(const notification &n)> error_handler=[](const notification &n){}) override
Definition: backend-v4l2.h:50
power_state
Definition: backend.h:123
bool set_pu(rs2_option opt, int value) override
void init_xu(const extension_unit &xu) override
STDMETHODIMP QueryInterface(REFIID iid, void **ppv) override
void unlock()
Definition: backend-v4l2.h:58
Definition: backend.h:126
STDMETHODIMP OnEvent(DWORD, IMFMediaEvent *) override
std::function< void(stream_profile, frame_object, std::function< void()>)> frame_callback
Definition: backend.h:177
control_range get_pu_range(rs2_option opt) const override
bool get_xu(const extension_unit &xu, uint8_t ctrl, uint8_t *data, int len) const override
stream_profile profile
Definition: win-uvc.h:42
bool get_pu(rs2_option opt, int32_t &value) const override
Definition: backend.h:387
Definition: types.h:846
DEFINE_GUIDSTRUCT("24E552D7-6523-47F7-A647-D3465BF1F5CA", KSCATEGORY_SENSOR_CAMERA)
void lock() const override
Definition: win-uvc.h:77
STDMETHODIMP OnReadSample(HRESULT, DWORD dwStreamIndex, DWORD, LONGLONG, IMFSample *sample) override
source_reader_callback(std::weak_ptr< wmf_uvc_device > owner)
Definition: win-uvc.h:127
void probe_and_commit(stream_profile profile, frame_callback callback, int buffers) override
void close(stream_profile profile) override
std::function< void(const uvc_device_info &, IMFActivate *)> enumeration_callback
Definition: win-uvc.h:47