CARLA
Loading...
Searching...
No Matches
AsyncDataStream.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
10#include <carla/Buffer.h>
11#include <carla/Logging.h>
15#include <carla/rpc/Transform.h>
17
18
19template <typename T>
20class FDataStreamTmpl;
21
22// =============================================================================
23// -- FAsyncDataStreamTmpl -----------------------------------------------------
24// =============================================================================
25
26/// A streaming channel for sending sensor data to clients, supports sending
27/// data asynchronously. Data sent by the "Send" functions is passed to the
28/// serializer registered with the sensor at carla::sensor:SensorRegistry before
29/// being sent down the stream.
30///
31/// @warning This is a single-use object, a new one needs to be created for each
32/// new message.
33///
34/// FAsyncDataStream also has a pool of carla::Buffer that allows reusing the
35/// allocated memory, use it whenever possible.
36template <typename T>
38{
39public:
40
41 using StreamType = T;
42
44
45 /// Return the token that allows subscribing to this stream.
46 auto GetToken() const
47 {
48 return Stream.GetToken();
49 }
50
51 /// Pop a Buffer from the pool. Buffers in the pool can reuse the memory
52 /// allocated by previous messages, significantly improving performance for
53 /// big messages.
55 {
56 return Stream.MakeBuffer();
57 }
58
59 /// Send some data down the stream.
60 template <typename SensorT, typename... ArgsT>
61 void Send(SensorT &Sensor, ArgsT &&... Args);
62
63 template <typename SensorT, typename... ArgsT>
64 void SerializeAndSend(SensorT &Sensor, ArgsT &&... Args);
65
66 /// allow to change the frame number of the header
67 void SetFrameNumber(uint64_t FrameNumber)
68 {
71 if (HeaderStr)
72 {
73 if (HeaderStr->frame != FrameNumber)
74 {
75 carla::log_info("Re-framing sensor type ", HeaderStr->sensor_type, " from ", HeaderStr->frame, " to ", FrameNumber);
76 HeaderStr->frame = FrameNumber;
77 }
78 }
79 }
80
81 /// allow to change the timestamp of the header
82 void SetTimestamp(double Timestamp)
83 {
86 if (HeaderStr)
87 {
88 if (HeaderStr->timestamp != Timestamp)
89 {
90 carla::log_info("Re-timestamping sensor type ", HeaderStr->sensor_type, " from ", HeaderStr->timestamp, " to ", Timestamp);
91 HeaderStr->timestamp = Timestamp;
92 }
93 }
94 }
95
96 /// allow to change the transform of the header
98 {
101 if (HeaderStr)
102 {
103 if (HeaderStr->sensor_transform != Transform)
104 {
105 carla::log_info("Re-transforming sensor type ", HeaderStr->sensor_type, " from ", HeaderStr->sensor_transform, " to ", Transform);
106 HeaderStr->sensor_transform = Transform;
107 }
108 }
109 }
110
111 /// return the type of sensor of this stream
112 uint64_t GetSensorType()
113 {
116 if (HeaderStr)
117 {
118 return HeaderStr->sensor_type;
119 }
120 return 0u;
121 }
122
123 /// return the transform of the sensor
125 {
128 if (HeaderStr)
129 {
130 return FTransform(HeaderStr->sensor_transform);
131 }
132 return FTransform();
133 }
134
135 /// return the timestamp of the sensor
137 {
140 if (HeaderStr)
141 {
142 return HeaderStr->timestamp;
143 }
144 return 0.0;
145 }
146
147private:
148
149 friend class FDataStreamTmpl<T>;
150
151 /// @pre This functions needs to be called in the game-thread.
152 template <typename SensorT>
153 explicit FAsyncDataStreamTmpl(
154 const SensorT &InSensor,
155 double Timestamp,
156 StreamType InStream);
157
159
161};
162
163// =============================================================================
164// -- FAsyncDataStream and FAsyncDataMultiStream -------------------------------
165// =============================================================================
166
168
170
171// =============================================================================
172// -- FAsyncDataStreamTmpl implementation --------------------------------------
173// =============================================================================
174
175template <typename T>
176template <typename SensorT, typename... ArgsT>
177inline void FAsyncDataStreamTmpl<T>::SerializeAndSend(SensorT &Sensor, ArgsT &&... Args)
178{
179 // serialize data
180 carla::Buffer Data(carla::sensor::SensorRegistry::Serialize(Sensor, std::forward<ArgsT>(Args)...));
181
182 // create views of buffers
183 auto ViewHeader = carla::BufferView::CreateFrom(std::move(Header));
184 auto ViewData = carla::BufferView::CreateFrom(std::move(Data));
185
186 // send views
187 Stream.Write(ViewHeader, ViewData);
188}
189
190template <typename T>
191template <typename SensorT, typename... ArgsT>
192inline void FAsyncDataStreamTmpl<T>::Send(SensorT &Sensor, ArgsT &&... Args)
193{
194 // create views of buffers
195 auto ViewHeader = carla::BufferView::CreateFrom(std::move(Header));
196
197 // send views
198 Stream.Write(ViewHeader, std::forward<ArgsT>(Args)...);
199}
A streaming channel for sending sensor data to clients, supports sending data asynchronously.
FAsyncDataStreamTmpl(FAsyncDataStreamTmpl &&)=default
FTransform GetSensorTransform()
return the transform of the sensor
void SetFrameNumber(uint64_t FrameNumber)
allow to change the frame number of the header
void SerializeAndSend(SensorT &Sensor, ArgsT &&... Args)
void SetTimestamp(double Timestamp)
allow to change the timestamp of the header
void SetTransform(carla::rpc::Transform Transform)
allow to change the transform of the header
carla::Buffer PopBufferFromPool()
Pop a Buffer from the pool.
uint64_t GetSensorType()
return the type of sensor of this stream
double GetSensorTimestamp()
return the timestamp of the sensor
auto GetToken() const
Return the token that allows subscribing to this stream.
void Send(SensorT &Sensor, ArgsT &&... Args)
Send some data down the stream.
A streaming channel for sending sensor data to clients.
Definition DataStream.h:28
std::optional< StreamType > Stream
Definition DataStream.h:73
static std::shared_ptr< BufferView > CreateFrom(Buffer &&buffer)
Definition BufferView.h:56
A piece of raw data.
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
static Buffer Serialize(Sensor &sensor, Args &&... args)
Serialize the arguments provided into a Buffer by calling to the serializer registered for the given ...
static void log_info(Args &&... args)
Definition Logging.h:82