Point Cloud Library (PCL)  1.14.1-dev
depth_sense_device_manager.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2014-, 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 <boost/utility.hpp>
41 
42 #include <pcl/pcl_exports.h>
43 
44 #include <DepthSense.hxx>
45 
46 #include <memory>
47 #include <thread>
48 
49 namespace pcl
50 {
51 
52  namespace io
53  {
54 
55  namespace depth_sense
56  {
57 
58  struct DepthSenseGrabberImpl;
59 
60  /** A helper class for enumerating and managing access to DepthSense
61  * devices. */
62  class PCL_EXPORTS DepthSenseDeviceManager : boost::noncopyable
63  {
64 
65  public:
66 
67  using Ptr = std::shared_ptr<DepthSenseDeviceManager>;
68 
69  static Ptr&
71  {
72  static Ptr instance;
73  if (!instance)
74  {
75  std::lock_guard<std::mutex> lock (mutex_);
76  if (!instance)
77  instance.reset (new DepthSenseDeviceManager);
78  }
79  return (instance);
80  }
81 
82  /** Get the number of connected DepthSense devices. */
83  inline std::size_t
85  {
86  return (context_.getDevices ().size ());
87  }
88 
89  /** Capture first available device and associate it with a given
90  * grabber instance. */
91  std::string
93 
94  /** Capture the device with given index and associate it with a given
95  * grabber instance. */
96  std::string
97  captureDevice (DepthSenseGrabberImpl* grabber, std::size_t index);
98 
99  /** Capture the device with given serial number and associate it with
100  * a given grabber instance. */
101  std::string
102  captureDevice (DepthSenseGrabberImpl* grabber, const std::string& sn);
103 
104  /** Release DepthSense device with given serial number. */
105  void
106  releaseDevice (const std::string& sn);
107 
108  /** Reconfigure DepthSense device with given serial number. */
109  void
110  reconfigureDevice (const std::string& sn);
111 
112  /** Start data capturing for a given device. */
113  void
114  startDevice (const std::string& sn);
115 
116  /** Stop data capturing for a given device. */
117  void
118  stopDevice (const std::string& sn);
119 
121 
122  private:
123 
125 
126  std::string
127  captureDevice (DepthSenseGrabberImpl* grabber, DepthSense::Device device);
128 
129  inline bool
130  isCaptured (const std::string& sn) const
131  {
132  return (captured_devices_.count (sn) != 0);
133  }
134 
135  DepthSense::Context context_;
136 
137  static std::mutex mutex_;
138 
139  /// Thread where the grabbing takes place.
140  std::thread depth_sense_thread_;
141 
142  struct CapturedDevice
143  {
144  DepthSenseGrabberImpl* grabber;
145  DepthSense::DepthNode depth_node;
146  DepthSense::ColorNode color_node;
147  };
148 
149  std::map<std::string, CapturedDevice> captured_devices_;
150 
151  };
152 
153  } // namespace depth_sense
154 
155  } // namespace io
156 
157 } // namespace pcl
A helper class for enumerating and managing access to DepthSense devices.
void releaseDevice(const std::string &sn)
Release DepthSense device with given serial number.
void reconfigureDevice(const std::string &sn)
Reconfigure DepthSense device with given serial number.
void startDevice(const std::string &sn)
Start data capturing for a given device.
std::string captureDevice(DepthSenseGrabberImpl *grabber)
Capture first available device and associate it with a given grabber instance.
void stopDevice(const std::string &sn)
Stop data capturing for a given device.
std::string captureDevice(DepthSenseGrabberImpl *grabber, const std::string &sn)
Capture the device with given serial number and associate it with a given grabber instance.
std::size_t getNumDevices()
Get the number of connected DepthSense devices.
std::string captureDevice(DepthSenseGrabberImpl *grabber, std::size_t index)
Capture the device with given index and associate it with a given grabber instance.
std::shared_ptr< DepthSenseDeviceManager > Ptr
#define PCL_EXPORTS
Definition: pcl_macros.h:323