Point Cloud Library (PCL)  1.10.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 <pcl/pcl_exports.h>
41 
42 #include <pxcsession.h>
43 #include <pxccapture.h>
44 #include <pxccapturemanager.h>
45 
46 #include <boost/utility.hpp>
47 
48 #include <memory>
49 
50 namespace pcl
51 {
52 
53  class RealSenseGrabber;
54 
55  namespace io
56  {
57 
58  namespace real_sense
59  {
60 
61  class RealSenseDevice;
62 
63  class PCL_EXPORTS RealSenseDeviceManager : boost::noncopyable
64  {
65 
66  public:
67 
68  using Ptr = std::shared_ptr<RealSenseDeviceManager>;
69 
70  static Ptr&
72  {
73  static Ptr instance;
74  if (!instance)
75  {
76  std::lock_guard<std::mutex> lock (mutex_);
77  if (!instance)
78  instance.reset (new RealSenseDeviceManager);
79  }
80  return (instance);
81  }
82 
83  inline std::size_t
85  {
86  return (device_list_.size ());
87  }
88 
89  std::shared_ptr<RealSenseDevice>
90  captureDevice ();
91 
92  std::shared_ptr<RealSenseDevice>
93  captureDevice (std::size_t index);
94 
95  std::shared_ptr<RealSenseDevice>
96  captureDevice (const std::string& sn);
97 
99 
100  private:
101 
102  struct DeviceInfo
103  {
104  pxcUID iuid;
105  pxcI32 didx;
106  std::string serial;
107  std::weak_ptr<RealSenseDevice> device_ptr;
108  inline bool isCaptured () { return (!device_ptr.expired ()); }
109  };
110 
111  /** If the device is already captured returns a pointer. */
112  std::shared_ptr<RealSenseDevice>
113  capture (DeviceInfo& device_info);
114 
116 
117  /** This function discovers devices that are capable of streaming
118  * depth data. */
119  void
120  populateDeviceList ();
121 
122  std::shared_ptr<PXCSession> session_;
123  std::shared_ptr<PXCCaptureManager> capture_manager_;
124 
125  std::vector<DeviceInfo> device_list_;
126 
127  static std::mutex mutex_;
128 
129  };
130 
131  class PCL_EXPORTS RealSenseDevice : boost::noncopyable
132  {
133 
134  public:
135 
136  using Ptr = std::shared_ptr<RealSenseDevice>;
137 
138  inline const std::string&
139  getSerialNumber () { return (device_id_); }
140 
141  inline PXCCapture::Device&
142  getPXCDevice () { return (*device_); }
143 
144  /** Reset the state of given device by releasing and capturing again. */
145  static void
147  {
148  std::string id = device->getSerialNumber ();
149  device.reset ();
150  device = RealSenseDeviceManager::getInstance ()->captureDevice (id);
151  }
152 
153  private:
154 
156 
157  std::string device_id_;
158  std::shared_ptr<PXCCapture> capture_;
159  std::shared_ptr<PXCCapture::Device> device_;
160 
161  RealSenseDevice (const std::string& id) : device_id_ (id) { };
162 
163  };
164 
165  } // namespace real_sense
166 
167  } // namespace io
168 
169 } // namespace pcl
std::shared_ptr< RealSenseDeviceManager > Ptr
std::shared_ptr< RealSenseDevice > Ptr
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
static void reset(RealSenseDevice::Ptr &device)
Reset the state of given device by releasing and capturing again.
#define PCL_EXPORTS
Definition: pcl_macros.h:253