-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathpipeline.cpp
321 lines (284 loc) · 11.8 KB
/
pipeline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
#include <algorithm>
#include "pipeline.h"
#include "stream.h"
#include "media/playback/playback_device.h"
#include "media/record/record_device.h"
#include "media/ros/ros_writer.h"
#include <src/proc/syncer-processing-block.h>
#include <src/core/frame-callback.h>
#include <rsutils/string/from.h>
namespace librealsense
{
namespace pipeline
{
pipeline::pipeline(std::shared_ptr<librealsense::context> ctx) :
_ctx(ctx),
_dispatcher(10),
_hub( device_hub::make( ctx, RS2_PRODUCT_LINE_ANY_INTEL )),
_synced_streams({ RS2_STREAM_COLOR, RS2_STREAM_DEPTH, RS2_STREAM_INFRARED, RS2_STREAM_FISHEYE })
{}
pipeline::~pipeline()
{
if (_active_profile) {
try {
unsafe_stop();
}
catch (...) {}
}
}
std::shared_ptr<profile> pipeline::start(std::shared_ptr<config> conf, rs2_frame_callback_sptr callback)
{
std::lock_guard<std::mutex> lock(_mtx);
if (_active_profile)
{
throw librealsense::wrong_api_call_sequence_exception("start() cannot be called before stop()");
}
_streams_callback = callback;
unsafe_start(conf);
return unsafe_get_active_profile();
}
std::shared_ptr<profile> pipeline::get_active_profile() const
{
std::lock_guard<std::mutex> lock(_mtx);
return unsafe_get_active_profile();
}
std::shared_ptr<profile> pipeline::unsafe_get_active_profile() const
{
if (!_active_profile)
throw librealsense::wrong_api_call_sequence_exception("get_active_profile() can only be called between a start() and a following stop()");
return _active_profile;
}
void pipeline::unsafe_start(std::shared_ptr<config> conf)
{
std::shared_ptr<profile> profile = nullptr;
//first try to get the previously resolved profile (if exists)
auto cached_profile = conf->get_cached_resolved_profile();
if (cached_profile)
{
profile = cached_profile;
}
else
{
const int NUM_TIMES_TO_RETRY = 3;
for (int i = 1; i <= NUM_TIMES_TO_RETRY; i++)
{
try
{
profile = conf->resolve(shared_from_this(), std::chrono::seconds(5));
break;
}
catch (...)
{
if (i == NUM_TIMES_TO_RETRY)
throw;
}
}
}
assert(profile);
if (!profile->_multistream.get_profiles().size())
throw librealsense::wrong_api_call_sequence_exception("No streams are selected!");
auto synced_streams_ids = on_start(profile);
rs2_frame_callback_sptr callbacks = get_callback(synced_streams_ids);
auto dev = profile->get_device();
if (auto playback = As<librealsense::playback_device>(dev))
{
_playback_stopped_token = playback->playback_status_changed.subscribe( [this, callbacks](rs2_playback_status status)
{
if (status == RS2_PLAYBACK_STATUS_STOPPED)
{
_dispatcher.invoke([this, callbacks](dispatcher::cancellable_timer t)
{
//If the pipeline holds a playback device, and it reached the end of file (stopped)
//Then we restart it
if (_active_profile && _prev_conf->get_repeat_playback())
{
_active_profile->_multistream.open();
_active_profile->_multistream.start(callbacks);
}
});
}
} );
}
_dispatcher.start();
profile->_multistream.open();
profile->_multistream.start(callbacks);
_active_profile = profile;
_prev_conf = std::make_shared<config>(*conf);
}
void pipeline::stop()
{
std::lock_guard<std::mutex> lock(_mtx);
if (!_active_profile)
{
throw librealsense::wrong_api_call_sequence_exception("stop() cannot be called before start()");
}
unsafe_stop();
}
void pipeline::unsafe_stop()
{
if (_active_profile)
{
try
{
_syncer->stop();
_aggregator->stop();
auto dev = _active_profile->get_device();
if (auto playback = As<librealsense::playback_device>(dev))
{
_playback_stopped_token.cancel();
}
_active_profile->_multistream.stop();
_active_profile->_multistream.close();
_dispatcher.stop();
}
catch (...)
{
} // Stop will throw if device was disconnected. TODO - refactoring anticipated
// shared pointers initialized when pipeline running with _active_profile
// should be reset with _active_profile too
_active_profile.reset();
_prev_conf.reset();
_streams_callback.reset();
}
}
std::shared_ptr<device_interface> pipeline::wait_for_device(const std::chrono::milliseconds& timeout, const std::string& serial)
{
// pipeline's device selection shall be deterministic
return _hub->wait_for_device(timeout, false, serial);
}
std::shared_ptr<librealsense::context> pipeline::get_context() const
{
return _ctx;
}
std::vector<int> pipeline::on_start(std::shared_ptr<profile> profile)
{
std::vector<int> _streams_to_aggregate_ids;
std::vector<int> _streams_to_sync_ids;
bool sync_any = false;
if (std::find(_synced_streams.begin(), _synced_streams.end(), RS2_STREAM_ANY) != _synced_streams.end())
sync_any = true;
// check wich of the active profiles should be synced and update the sync list accordinglly
for (auto&& s : profile->get_active_streams())
{
_streams_to_aggregate_ids.push_back(s->get_unique_id());
bool sync_current = sync_any;
if (!sync_any && std::find(_synced_streams.begin(), _synced_streams.end(), s->get_stream_type()) != _synced_streams.end())
sync_current = true;
if(sync_current)
_streams_to_sync_ids.push_back(s->get_unique_id());
}
_syncer = std::unique_ptr<syncer_process_unit>(new syncer_process_unit());
_aggregator = std::unique_ptr<aggregator>(new aggregator(_streams_to_aggregate_ids, _streams_to_sync_ids));
if (_streams_callback)
_aggregator->set_output_callback(_streams_callback);
return _streams_to_sync_ids;
}
rs2_frame_callback_sptr pipeline::get_callback(std::vector<int> synced_streams_ids)
{
_syncer->set_output_callback(
make_frame_callback( [&]( frame_holder fref ) { _aggregator->invoke( std::move( fref ) ); } ) );
return make_frame_callback(
[&, synced_streams_ids]( frame_holder fref )
{
// if the user requested to sync the frame push it to the syncer, otherwise push it to the
// aggregator
if( std::find( synced_streams_ids.begin(),
synced_streams_ids.end(),
fref->get_stream()->get_unique_id() )
!= synced_streams_ids.end() )
_syncer->invoke( std::move( fref ) );
else
_aggregator->invoke( std::move( fref ) );
} );
}
frame_holder pipeline::wait_for_frames(unsigned int timeout_ms)
{
std::lock_guard<std::mutex> lock(_mtx);
if (!_active_profile)
{
throw librealsense::wrong_api_call_sequence_exception("wait_for_frames cannot be called before start()");
}
if (_streams_callback)
{
throw librealsense::wrong_api_call_sequence_exception("wait_for_frames cannot be called if a callback was provided");
}
frame_holder f;
if (_aggregator->dequeue(&f, timeout_ms))
{
return f;
}
//hub returns true even if device already reconnected
if (!_hub->is_connected(*_active_profile->get_device()))
{
try
{
auto prev_conf = _prev_conf;
unsafe_stop();
unsafe_start(prev_conf);
if (_aggregator->dequeue(&f, timeout_ms))
{
return f;
}
}
catch (const std::exception& e)
{
throw std::runtime_error( rsutils::string::from() << "Device disconnected. Failed to reconnect: "
<< e.what() << timeout_ms );
}
}
throw std::runtime_error( rsutils::string::from() << "Frame didn't arrive within " << timeout_ms );
}
bool pipeline::poll_for_frames(frame_holder* frame)
{
std::lock_guard<std::mutex> lock(_mtx);
if (!_active_profile)
{
throw librealsense::wrong_api_call_sequence_exception("poll_for_frames cannot be called before start()");
}
if (_streams_callback)
{
throw librealsense::wrong_api_call_sequence_exception("poll_for_frames cannot be called if a callback was provided");
}
if (_aggregator->try_dequeue(frame))
{
return true;
}
return false;
}
bool pipeline::try_wait_for_frames(frame_holder* frame, unsigned int timeout_ms)
{
std::lock_guard<std::mutex> lock(_mtx);
if (!_active_profile)
{
throw librealsense::wrong_api_call_sequence_exception("try_wait_for_frames cannot be called before start()");
}
if (_streams_callback)
{
throw librealsense::wrong_api_call_sequence_exception("try_wait_for_frames cannot be called if a callback was provided");
}
if (_aggregator->dequeue(frame, timeout_ms))
{
return true;
}
//hub returns true even if device already reconnected
if (!_hub->is_connected(*_active_profile->get_device()))
{
try
{
auto prev_conf = _prev_conf;
unsafe_stop();
unsafe_start(prev_conf);
return _aggregator->dequeue(frame, timeout_ms);
}
catch (const std::exception& e)
{
LOG_INFO(e.what());
return false;
}
}
return false;
}
}
}