MediaX v1.0.0rc7 [7e6cb74]
Video streaming for military vehicles
Loading...
Searching...
No Matches
rtp_sap_wrapper.h
Go to the documentation of this file.
1//
2// Copyright (c) 2025, Astute Systems PTY LTD
3//
4// This file is part of the VivoeX project developed by Astute Systems.
5//
6// Licensed under the Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
7// License. See the LICENSE file in the project root for full license details.
8//
13
14#ifndef WRAPPERS_RTP_SAP_WRAPPER_H_
15#define WRAPPERS_RTP_SAP_WRAPPER_H_
16
17#include <chrono>
18#include <string>
19#include <vector>
20
21#include "rtp/rtp.h"
22#include "sap/sap.h"
24
25namespace mediax {
26
28template <typename T>
30 public:
50 RtpSapTransmit(std::string_view hostname, uint16_t port, std::string_view session_name, uint16_t height,
51 uint16_t width, uint16_t framerate, std::string_view encoding) {
52 stream_info_ = {.session_name = std::string(session_name),
53 .hostname = std::string(hostname),
54 .port = port,
55 .height = height,
56 .width = width,
57 .framerate = framerate,
58 .encoding = ::mediax::ColourspaceTypeFromString(encoding)};
59 if (!mediax::IsRtpInitialised()) mediax::InitRtp(0, nullptr);
63 rtp_payloader_.SetStreamInfo(stream_info_);
64 rtp_payloader_.Open();
65 rtp_payloader_.Start();
66 last_transmit_timestamp_ = std::chrono::system_clock::now();
67 }
68
74 std::vector<uint8_t>& GetBuffer() {
76 (BitsPerPixel(stream_info_.encoding) / static_cast<double>(8)));
77 return data_buffer_;
78 }
79
85 std::vector<uint8_t>& GetBuffer(uint32_t width, uint32_t height, ::mediax::rtp::ColourspaceType encoding) {
86 uint32_t size = width * height * (BitsPerPixel(encoding) / static_cast<double>(8));
87 data_buffer_.resize(size);
88 return data_buffer_;
89 }
90
110 std::vector<uint8_t>& GetBufferTestPattern(uint32_t pattern = 0) {
112 }
113
122 std::vector<uint8_t>& GetBufferTestPattern(uint32_t height, uint32_t width, ::mediax::rtp::ColourspaceType encoding,
123 uint32_t pattern = 0) {
124 std::vector<uint8_t>& buffer = GetBuffer(width, height, encoding);
125
126 switch (pattern) {
127 case 0:
128 CreateColourBarEbuTestCard(buffer.data(), width, height, encoding);
129 break;
130 case 1:
131 CreateColourBarTestCard(buffer.data(), width, height, encoding);
132 break;
133 case 2:
134 CreateGreyScaleBarTestCard(buffer.data(), width, height, encoding);
135 break;
136 case 3:
137 CreateCheckeredTestCard(buffer.data(), width, height, encoding);
138 break;
139 case 4:
140 // red
141 CreateSolidTestCard(buffer.data(), width, height, 0xff, 0xff, 0xff, encoding);
142 break;
143 case 5:
144 // green
145 CreateSolidTestCard(buffer.data(), width, height, 0x00, 0xff, 0x00, encoding);
146 break;
147 case 6:
148 // blue
149 CreateSolidTestCard(buffer.data(), width, height, 0x00, 0x00, 0xff, encoding);
150 break;
151 case 7:
152 // black
153 CreateSolidTestCard(buffer.data(), width, height, 0x00, 0x00, 0x00, encoding);
154 break;
155 case 8:
156 // white
157 CreateSolidTestCard(buffer.data(), width, height, 0xff, 0xff, 0xff, encoding);
158 break;
159 case 9:
160 // noise
161 CreateWhiteNoiseTestCard(buffer.data(), width, height, encoding);
162 break;
163 case 10:
164 // bouncing ball
165 CreateBouncingBallTestCard(buffer.data(), width, height, encoding);
166 break;
167 default:
168 // black
169 CreateSolidTestCard(buffer.data(), width, height, 0x00, 0x00, 0x00, encoding);
170 break;
171 }
172 return GetBuffer(width, height, encoding);
173 }
174
185
192 void Transmit(uint8_t* data, size_t size) {
193 // Check if we need to transmit yet
194 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() -
196 .count();
197 int interval = 1000 / stream_info_.framerate;
198 if (elapsed < 1000 / stream_info_.framerate) {
199 // Delay the transmit by the elapsed time minus the required delay
200 std::this_thread::sleep_for(std::chrono::milliseconds(interval - elapsed));
201 }
202 rtp_payloader_.Transmit(data, size);
203 // update timestamp
204 last_transmit_timestamp_ = std::chrono::system_clock::now();
205 }
206
207 private:
215 std::vector<uint8_t> data_buffer_;
217 std::chrono::system_clock::time_point last_transmit_timestamp_;
218};
219
221template <typename T>
223 public:
235 RtpSapRecieve(std::string_view hostname, uint16_t port, std::string_view session_name, uint16_t height,
236 uint16_t width, uint16_t framerate, std::string_view encoding) {
237 stream_info_ = {.session_name = std::string(session_name),
238 .hostname = std::string(hostname),
239 .port = port,
240 .height = height,
241 .width = width,
242 .framerate = framerate,
243 .encoding = ::mediax::ColourspaceTypeFromString(encoding),
244 .deleted = true}; // MArk as deleted as its not valid yet
245
246 if (!mediax::IsRtpInitialised()) mediax::InitRtp(0, nullptr);
249 }
250
261
266 static void Callback(const sap::SdpMessage* sdp, void* data) {
267 auto rtp = static_cast<RtpSapRecieve*>(data);
268 std::cout << "Address this2 " << rtp << std::endl;
269
270 if (rtp->stream_info_.deleted == true) {
271 rtp->stream_info_ = SapToStreamInformation(*sdp);
272 rtp->stream_info_.deleted = false;
273 rtp->rtp_depayloader_.SetStreamInfo(rtp->stream_info_);
274 rtp->rtp_depayloader_.Open();
275 rtp->rtp_depayloader_.Start();
276 }
277 }
278
284 std::vector<uint8_t>& GetBuffer() {
286 return data_buffer_;
287 }
288
295 bool Receive(::mediax::rtp::RtpFrameData* data, size_t size) {
296 if (CheckSapOk()) {
297 rtp_depayloader_.Receive(data, 80);
298 return true;
299 } else {
300 // Sleep for one second and try again
301 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
302 return false;
303 }
304 }
305
306 private:
313 bool CheckSapOk() const { return !stream_info_.deleted; }
314
322 std::vector<uint8_t> data_buffer_;
323};
324
325} // namespace mediax
326
327#endif // WRAPPERS_RTP_SAP_WRAPPER_H_
A RTP SAP receiver helper class.
Definition rtp_sap_wrapper.h:222
bool CheckSapOk() const
Check that the SAP announcement has been received and is OK.
Definition rtp_sap_wrapper.h:313
bool Receive(::mediax::rtp::RtpFrameData *data, size_t size)
The frame receive function, this will receive the frame over RTP.
Definition rtp_sap_wrapper.h:295
RtpSapRecieve(std::string_view hostname, uint16_t port, std::string_view session_name, uint16_t height, uint16_t width, uint16_t framerate, std::string_view encoding)
Construct a new Rtp Sap Recieve object.
Definition rtp_sap_wrapper.h:235
std::vector< uint8_t > data_buffer_
The vectored data buffer.
Definition rtp_sap_wrapper.h:322
sap::SapListener & sap_listener_
The SAP listener.
Definition rtp_sap_wrapper.h:318
std::vector< uint8_t > & GetBuffer()
Get the frame buffer, resized and ready to use.
Definition rtp_sap_wrapper.h:284
static void Callback(const sap::SdpMessage *sdp, void *data)
The SAP callback.
Definition rtp_sap_wrapper.h:266
T rtp_depayloader_
The RTP depayloader.
Definition rtp_sap_wrapper.h:316
~RtpSapRecieve()
Destroy the Rtp Sap Receive object.
Definition rtp_sap_wrapper.h:255
::mediax::rtp::StreamInformation stream_info_
The stream information.
Definition rtp_sap_wrapper.h:320
A RTP SAP transmitter helper class.
Definition rtp_sap_wrapper.h:29
std::vector< uint8_t > data_buffer_
The vectored data buffer.
Definition rtp_sap_wrapper.h:215
std::chrono::system_clock::time_point last_transmit_timestamp_
Last transmit timestamp.
Definition rtp_sap_wrapper.h:217
RtpSapTransmit(std::string_view hostname, uint16_t port, std::string_view session_name, uint16_t height, uint16_t width, uint16_t framerate, std::string_view encoding)
Construct a new Rtp Sap Transmit object.
Definition rtp_sap_wrapper.h:50
~RtpSapTransmit()
Destroy the Rtp Sap Transmit object.
Definition rtp_sap_wrapper.h:179
std::vector< uint8_t > & GetBufferTestPattern(uint32_t pattern=0)
Get the frame buffer containing a pre-defined test pattern.
Definition rtp_sap_wrapper.h:110
sap::SapAnnouncer & sap_announcer_
The SAP announcer.
Definition rtp_sap_wrapper.h:211
::mediax::rtp::StreamInformation stream_info_
The stream information.
Definition rtp_sap_wrapper.h:213
T rtp_payloader_
The RTP payloader.
Definition rtp_sap_wrapper.h:209
void Transmit(uint8_t *data, size_t size)
The frame transmit function, this will transmit the frame over RTP at the required frequency.
Definition rtp_sap_wrapper.h:192
std::vector< uint8_t > & GetBufferTestPattern(uint32_t height, uint32_t width, ::mediax::rtp::ColourspaceType encoding, uint32_t pattern=0)
This function can be called directly with explicit arguments.
Definition rtp_sap_wrapper.h:122
std::vector< uint8_t > & GetBuffer(uint32_t width, uint32_t height, ::mediax::rtp::ColourspaceType encoding)
Get the frame buffer, resized and ready to use.
Definition rtp_sap_wrapper.h:85
std::vector< uint8_t > & GetBuffer()
Get the frame buffer, resized and ready to use.
Definition rtp_sap_wrapper.h:74
Class to announce the stream details using the SAP protocol.
Definition sap_announcer.h:65
void AddSapAnnouncement(const ::mediax::rtp::StreamInformation &stream_information)
Add Sap stream announcement information.
Definition sap_announcer.cc:96
void Stop()
Stop the SAP/SDP announcement thread.
Definition sap_announcer.cc:70
void Start()
Start the SAP/SDP announcements thread. Ensure you have set the source interface using SetSourceInter...
Definition sap_announcer.cc:55
static SapAnnouncer & GetInstance()
A Singleton get method.
Definition sap_announcer.cc:53
void SetSourceInterface(uint32_t select=0)
Set the Source Interface object.
Definition sap_announcer.cc:316
Class definition of the SAPListener.
Definition sap_listener.h:124
static SapListener & GetInstance()
A Singleton get method.
Definition sap_listener.cc:100
void RegisterSapListener(std::string_view session_name, const ::mediax::sap::SapCallback &callback, void *data)
Register a callback for our session_name.
Definition sap_listener.cc:124
void Start()
Start the SAP/SDP announcements thread.
Definition sap_listener.cc:143
void Stop()
Stop the SAP/SDP announcement thread.
Definition sap_listener.cc:149
Functions to convert between different colour spaces.
ColourspaceType
Supported colour spaces.
Definition rtp_types.h:102
The Astute Systems (MediaX) library for video streaming.
Definition rtp_av1_depayloader.cc:34
uint8_t BitsPerPixel(rtp::ColourspaceType mode)
Get the number of bits per pixel for a given colour space.
Definition rtp_utils.cc:115
rtp::ColourspaceType ColourspaceTypeFromString(std::string_view str)
Convert string to enum.
Definition rtp_utils.cc:87
void InitRtp(int argc, char *argv[])
Initialize the RTP library, mainly needed for GStreamer support.
Definition rtp_utils.cc:33
void RtpCleanup()
Finalise the RTP library, mainly needed for GStreamer support.
Definition rtp_utils.cc:48
bool IsRtpInitialised()
Check if the RTP library has been initialized.
Definition rtp_utils.cc:40
RTP streaming video types.
void CreateWhiteNoiseTestCard(uint8_t *data, uint32_t width, uint32_t height, mediax::rtp::ColourspaceType colourspace)
Create a White Noise Test Card object.
Definition rtp_utils.cc:472
void CreateColourBarTestCard(uint8_t *data, uint32_t width, uint32_t height, mediax::rtp::ColourspaceType colourspace)
Create a Colour Bar Test Card object.
Definition rtp_utils.cc:340
void CreateCheckeredTestCard(uint8_t *data, uint32_t width, uint32_t height, mediax::rtp::ColourspaceType colourspace)
Create a Checkerd Test Card object.
Definition rtp_utils.cc:439
void CreateColourBarEbuTestCard(uint8_t *data, uint32_t width, uint32_t height, mediax::rtp::ColourspaceType colourspace)
Create a Colour Bar Ebu Test Card object.
Definition rtp_utils.cc:281
void CreateSolidTestCard(uint8_t *data, uint32_t width, uint32_t height, uint8_t red, uint8_t green, uint8_t blue, mediax::rtp::ColourspaceType colourspace)
Create a Solid Test Card object.
Definition rtp_utils.cc:462
void CreateBouncingBallTestCard(uint8_t *data, uint32_t width, uint32_t height, mediax::rtp::ColourspaceType colourspace)
Create a Bouncing Ball Test Card object.
Definition rtp_utils.cc:500
void CreateGreyScaleBarTestCard(uint8_t *data, uint32_t width, uint32_t height, mediax::rtp::ColourspaceType colourspace)
Create a Grey Scale Bar Test Card object.
Definition rtp_utils.cc:390
RTP streaming video types.
The buffer structure.
Definition capture_yuyv.c:81
The RTP callback data.
Definition rtp_types.h:201
Struct capturing all stream information.
Definition rtp_types.h:223
uint32_t height
The stream height in pixels.
Definition rtp_types.h:231
::mediax::rtp::ColourspaceType encoding
Colourspace.
Definition rtp_types.h:237
bool deleted
Flag indicating the stream was deleted.
Definition rtp_types.h:239
std::string session_name
The SDP session name.
Definition rtp_types.h:225
uint32_t width
The stream width in pixels.
Definition rtp_types.h:233
uint32_t framerate
The stream framerate in frames / second.
Definition rtp_types.h:235
A simplified SDP message structure.
Definition sap_listener.h:91