Point Cloud Library (PCL)  1.14.0-dev
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  /** This function discovers devices that are capable of streaming
120  * depth data. */
121  void
122  populateDeviceList ();
123 
124  std::shared_ptr<PXCSession> session_;
125  std::shared_ptr<PXCCaptureManager> capture_manager_;
126 
127  std::vector<DeviceInfo> device_list_;
128 
129  static std::mutex mutex_;
130 
131  };
132 
133  class PCL_EXPORTS RealSenseDevice : boost::noncopyable
134  {
135 
136  public:
137  using Ptr = pcl::shared_ptr<RealSenseDevice>;
138 
139  inline const std::string&
140  getSerialNumber () { return (device_id_); }
141 
142  inline PXCCapture::Device&
143  getPXCDevice () { return (*device_); }
144 
145  /** Reset the state of given device by releasing and capturing again. */
146  static void
148  {
149  std::string id = device->getSerialNumber ();
150  device.reset ();
151  device = RealSenseDeviceManager::getInstance ()->captureDevice (id);
152  }
153 
154  private:
155 
157 
158  std::string device_id_;
159  std::shared_ptr<PXCCapture> capture_;
160  std::shared_ptr<PXCCapture::Device> device_;
161 
162  RealSenseDevice (const std::string& id) : device_id_ (id) { };
163 
164  };
165 
166  } // namespace real_sense
167 
168  } // namespace io
169 
170 } // 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