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>
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}
FAsyncDataStreamTmpl< carla::streaming::Stream > FAsyncDataMultiStream
FAsyncDataStreamTmpl< carla::streaming::Stream > FAsyncDataStream
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
auto GetToken() const
Return the token that allows subscribing to this stream.
void Send(SensorT &Sensor, ArgsT &&... Args)
Send some data down the stream.
FAsyncDataStreamTmpl(const SensorT &InSensor, double Timestamp, StreamType InStream)
A streaming channel for sending sensor data to clients.
Definition DataStream.h:28
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
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.
carla::sensor::CompositeSerializer< std::pair< ACollisionSensor *, s11n::CollisionEventSerializer >, std::pair< ADepthCamera *, s11n::ImageSerializer >, std::pair< ANormalsCamera *, s11n::NormalsImageSerializer >, std::pair< ADVSCamera *, s11n::DVSEventArraySerializer >, std::pair< AGnssSensor *, s11n::GnssSerializer >, std::pair< AInertialMeasurementUnit *, s11n::IMUSerializer >, std::pair< ALaneInvasionSensor *, s11n::NoopSerializer >, std::pair< AObstacleDetectionSensor *, s11n::ObstacleDetectionEventSerializer >, std::pair< AOpticalFlowCamera *, s11n::OpticalFlowImageSerializer >, std::pair< ARadar *, s11n::RadarSerializer >, std::pair< ARayCastSemanticLidar *, s11n::SemanticLidarSerializer >, std::pair< ARayCastLidar *, s11n::LidarSerializer >, std::pair< ARssSensor *, s11n::NoopSerializer >, std::pair< ASceneCaptureCamera *, s11n::ImageSerializer >, std::pair< ASceneCaptureCamera_RayTracedLens *, s11n::ImageSerializer >, std::pair< ASemanticSegmentationCamera *, s11n::ImageSerializer >, std::pair< AInstanceSegmentationCamera *, s11n::ImageSerializer >, std::pair< FWorldObserver *, s11n::EpisodeStateSerializer >, std::pair< ASceneCaptureCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< ADepthCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< AInstanceSegmentationCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< ASemanticSegmentationCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< FCameraGBufferUint8 *, s11n::GBufferUint8Serializer >, std::pair< FCameraGBufferFloat *, s11n::GBufferFloatSerializer >, std::pair< AHSSLidar *, s11n::LidarSerializer >, std::pair< AV2XSensor *, s11n::CAMDataSerializer >, std::pair< ACustomV2XSensor *, s11n::CustomV2XDataSerializer >, std::pair< AVehicleStatusSensor *, s11n::VehicleStatusSerializer >, std::pair< AAutowareGnssSensor *, s11n::GnssSerializer >, std::pair< ASceneCaptureCamera_RayTracedLensDistance *, s11n::DistanceImageSerializer >, std::pair< ASceneCaptureCamera_RayTracedLensInstance *, s11n::ImageSerializer > >::Serialize
static Buffer Serialize(Sensor &sensor, Args &&... args)
void Write(Buffers &&... buffers)
Flush buffers down the stream. No copies are made.
geom::Transform Transform