Point Cloud Library (PCL)  1.12.0
real_sense_device_manager.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2015-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pxcsession.h>
41 #include <pxccapture.h>
42 #include <pxccapturemanager.h>
43 
44 #include <pcl/memory.h> // for pcl::shared_ptr, pcl::weak_ptr
45 #include <pcl/pcl_exports.h> // for PCL_EXPORTS
46 
47 #include <boost/core/noncopyable.hpp> // for boost::noncopyable
48 
49 #include <cstddef> // for std::size_t
50 #include <mutex> // for std::lock_guard, std::mutex
51 #include <string> // for std::string
52 #include <vector> // for std::vector
53 
54 namespace pcl
55 {
56 
57  class RealSenseGrabber;
58 
59  namespace io
60  {
61 
62  namespace real_sense
63  {
64 
65  class RealSenseDevice;
66 
67  class PCL_EXPORTS RealSenseDeviceManager : boost::noncopyable
68  {
69 
70  public:
71 
72  using Ptr = std::shared_ptr<RealSenseDeviceManager>;
73 
74  static Ptr&
76  {
77  static Ptr instance;
78  if (!instance)
79  {
80  std::lock_guard<std::mutex> lock (mutex_);
81  if (!instance)
82  instance.reset (new RealSenseDeviceManager);
83  }
84  return (instance);
85  }
86 
87  inline std::size_t
89  {
90  return (device_list_.size ());
91  }
92 
93  std::shared_ptr<RealSenseDevice>
95 
96  std::shared_ptr<RealSenseDevice>
97  captureDevice (std::size_t index);
98 
99  std::shared_ptr<RealSenseDevice>
100  captureDevice (const std::string& sn);
101 
103 
104  private:
105 
106  struct DeviceInfo
107  {
108  pxcUID iuid;
109  pxcI32 didx;
110  std::string serial;
111  weak_ptr<RealSenseDevice> device_ptr;
112  inline bool isCaptured () { return (!device_ptr.expired ()); }
113  };
114 
115  /** If the device is already captured returns a pointer. */
116  std::shared_ptr<RealSenseDevice>
117  capture (DeviceInfo& device_info);
118 
119  RealSenseDeviceManager ();
120 
121  /** This function discovers devices that are capable of streaming
122  * depth data. */
123  void
124  populateDeviceList ();
125 
126  std::shared_ptr<PXCSession> session_;
127  std::shared_ptr<PXCCaptureManager> capture_manager_;
128 
129  std::vector<DeviceInfo> device_list_;
130 
131  static std::mutex mutex_;
132 
133  };
134 
135  class PCL_EXPORTS RealSenseDevice : boost::noncopyable
136  {
137 
138  public:
139  using Ptr = pcl::shared_ptr<RealSenseDevice>;
140 
141  inline const std::string&
142  getSerialNumber () { return (device_id_); }
143 
144  inline PXCCapture::Device&
145  getPXCDevice () { return (*device_); }
146 
147  /** Reset the state of given device by releasing and capturing again. */
148  static void
150  {
151  std::string id = device->getSerialNumber ();
152  device.reset ();
153  device = RealSenseDeviceManager::getInstance ()->captureDevice (id);
154  }
155 
156  private:
157 
159 
160  std::string device_id_;
161  std::shared_ptr<PXCCapture> capture_;
162  std::shared_ptr<PXCCapture::Device> device_;
163 
164  RealSenseDevice (const std::string& id) : device_id_ (id) { };
165 
166  };
167 
168  } // namespace real_sense
169 
170  } // namespace io
171 
172 } // namespace pcl
static void reset(RealSenseDevice::Ptr &device)
Reset the state of given device by releasing and capturing again.
pcl::shared_ptr< RealSenseDevice > Ptr
std::shared_ptr< RealSenseDevice > captureDevice(const std::string &sn)
std::shared_ptr< RealSenseDevice > captureDevice(std::size_t index)
std::shared_ptr< RealSenseDevice > captureDevice()
std::shared_ptr< RealSenseDeviceManager > Ptr
Defines functions, macros and traits for allocating and using memory.
#define PCL_EXPORTS
Definition: pcl_macros.h:323