Astra SDK  v2.1.3
StreamSet.hpp
1 // This file is part of the Orbbec Astra SDK [https://orbbec3d.com]
2 // Copyright (c) 2015-2017 Orbbec 3D
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // Be excellent to each other.
17 #ifndef ASTRA_STREAMSET_HPP
18 #define ASTRA_STREAMSET_HPP
19 
20 #include <string>
21 #include "capi/astra_core.h"
22 #include "StreamReader.hpp"
23 #include <stdexcept>
24 
25 namespace astra {
26 
27  static const char* ASTRA_DEFAULT_DEVICE_URI = "device/default";
28 
35  class StreamSet
36  {
37  public:
38  StreamSet()
39  : StreamSet(ASTRA_DEFAULT_DEVICE_URI)
40  {}
41 
42  StreamSet(const char* uri)
43  {
44  setRef_ = std::make_shared<StreamSetRef>(uri);
45  setRef_->connect();
46  }
47 
48  StreamSet(const StreamSet& other)
49  : setRef_(other.setRef_)
50  {}
51 
52  StreamSet& operator=(const StreamSet& rhs)
53  {
54  this->setRef_ = rhs.setRef_;
55  return *this;
56  }
57 
58  std::string uri()
59  {
60  char uri[ASTRA_STREAMSET_URI_MAX_LENGTH];
61  astra_streamset_get_uri(setRef_->connection_handle(), uri, ASTRA_STREAMSET_URI_MAX_LENGTH);
62  return std::string(uri);
63  }
64 
65  bool is_valid() { return setRef_ != nullptr; }
66 
67  bool is_available()
68  {
69  bool isAvailable = false;
70  if (setRef_ == nullptr || setRef_->connection_handle() == nullptr)
71  {
72  return isAvailable;
73  }
74  astra_streamset_is_available(setRef_->connection_handle(), &isAvailable);
75  return isAvailable;
76  }
77 
78  inline StreamReader create_reader();
79  astra_streamsetconnection_t get_handle() const { return setRef_->connection_handle(); }
80 
81  private:
82  class StreamSetRef;
83  using StreamSetRefPtr = std::shared_ptr<StreamSetRef>;
84 
85  class StreamSetRef :
86  public std::enable_shared_from_this<StreamSetRef>
87  {
88  public:
89  StreamSetRef(std::string uri)
90  : uri_(uri)
91  { }
92 
93  void connect()
94  {
95  if (!is_connected())
96  astra_streamset_open(uri_.c_str(), &connection_);
97  }
98 
99  bool is_connected() { return connection_ != nullptr; }
100 
101  ~StreamSetRef()
102  {
103  if (is_connected())
104  astra_streamset_close(&connection_);
105  }
106 
107  astra_streamsetconnection_t connection_handle() const { return connection_; }
108 
109  private:
110  astra_streamsetconnection_t connection_{nullptr};
111  std::string uri_;
112  };
113 
114  StreamSetRefPtr setRef_;
115 
116  friend bool operator==(const StreamSet& lhs, const StreamSet& rhs);
117  friend bool operator!=(const StreamSet& lhs, const StreamSet& rhs);
118  };
119 
120  inline bool operator==(const StreamSet& lhs, const StreamSet& rhs)
121  {
122  return lhs.setRef_ == rhs.setRef_;
123  }
124 
125  inline bool operator!=(const StreamSet& lhs, const StreamSet& rhs)
126  {
127  return !(lhs == rhs);
128  }
129 
130  StreamReader StreamSet::create_reader()
131  {
132  if (!setRef_->is_connected())
133  setRef_->connect();
134 
135  astra_reader_t reader;
136  astra_reader_create(get_handle(), &reader);
137 
138  return StreamReader(reader);
139  }
140 }
141 
142 #endif // ASTRA_STREAMSET_HPP
Stream Reader class
Definition: StreamReader.hpp:39
Stream Set Class
Definition: StreamSet.hpp:36
bool operator!=(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode not equal
Definition: Image.hpp:247
bool operator==(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode equal
Definition: Image.hpp:230