MediaX v1.0.0rc7 [7e6cb74]
Video streaming for military vehicles
Loading...
Searching...
No Matches
sap_listener.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//
14
15#ifndef SAP_SAP_LISTENER_H_
16#define SAP_SAP_LISTENER_H_
17
18#ifdef _WIN32
19#include <winsock2.h>
20#include <ws2tcpip.h>
21#else
22#include <arpa/inet.h>
23#include <ifaddrs.h>
24#include <netinet/in.h>
25#include <sys/socket.h>
26#endif
27
28#include <stdint.h>
29#include <unistd.h>
30
31#include <functional>
32#include <map>
33#include <memory>
34#include <string>
35#include <string_view>
36#include <thread>
37
38#include "rtp/rtp_types.h"
39
40namespace mediax::sap {
41
58
59// Time description
60// t= (time the session is active)
61// r=* (zero or more repeat times)
62// z=* (optional time zone offset line)
63
64// Media description, if present
65// m= (media name and transport address)
66// i=* (media title)
67// c=* (connection information -- optional if included at
68// session level)
69// b=* (zero or more bandwidth information lines)
70// k=* (obsolete)
71// a=* (zero or more media attribute lines)
72enum class SdpTypeEnum {
73 kProtocolVersion,
74 kOriginatorSessionIdentifier,
75 kSessionName,
76 kSessionInformation,
77 kUriOfDescription,
78 kEmailAddress,
79 kPhoneNumber,
80 kConnectionInformation,
81 kBandwidthInformation,
82 kSessionAttribute,
83 kTimeSessionActive,
84 kMediaNameAndTransportAddress,
85 kMediaTitle,
86 KConnectionInformation,
87 kUnknown
88};
89
91struct SdpMessage {
95 uint16_t hash;
97 std::string session_name;
99 std::string ip_address_source;
101 std::string ip_address;
103 uint32_t port;
105 uint32_t height;
107 uint32_t width;
109 std::string sampling;
111 uint32_t framerate;
115 bool deleted = false;
117 std::string sdp_text;
118};
119
121using SapCallback = std::function<void(sap::SdpMessage *sdp, void *data)>;
122
125 public:
130 SapListener();
131
136 ~SapListener();
137
142 static SapListener &GetInstance();
143
149 const std::map<std::string, ::mediax::sap::SdpMessage, std::less<>> &GetSapAnnouncements() const;
150
159 bool GetSapAnnouncement(std::string_view session_name, SdpMessage **sdp_message);
160
168 void RegisterSapListener(std::string_view session_name, const ::mediax::sap::SapCallback &callback, void *data);
169
177 bool GetStreamInformation(std::string_view session_name, ::mediax::rtp::StreamInformation *stream_information) const;
182 void Start();
183
188 void Stop();
189
190 private:
192 static std::unique_ptr<SapListener> singleton_;
193
201 static void SapListenerThread(SapListener *sap);
202
208 SdpTypeEnum GetType(const std::string_view &line) const;
209
216 std::map<std::string, std::string, std::less<>> ParseAttributes1(const std::string_view &line) const;
217
224 std::map<std::string, std::string, std::less<>> ParseAttributes(const std::string_view &line) const;
225
232 std::map<std::string, std::string, std::less<>> ParseAttributesEqual(const std::string &line) const;
233
240 bool SapStore(std::array<uint8_t, mediax::rtp::kMaxUdpData> *udpdata, uint32_t size);
241
243 std::map<std::string, SapCallback, std::less<>> callbacks_;
245 std::map<std::string, SdpMessage, std::less<>> announcements_;
247 std::array<uint8_t, mediax::rtp::kMaxUdpData> udpdata_;
249 std::thread thread_;
253 struct sockaddr_in multicast_addr_;
255 static bool running_;
257 void *data_;
258};
259
260} // namespace mediax::sap
261
262#endif // SAP_SAP_LISTENER_H_
Class definition of the SAPListener.
Definition sap_listener.h:124
SapListener()
Construct a new SAPListener::SAPListener object.
Definition sap_listener.cc:38
static SapListener & GetInstance()
A Singleton get method.
Definition sap_listener.cc:100
void * data_
Callback data.
Definition sap_listener.h:257
bool GetStreamInformation(std::string_view session_name, ::mediax::rtp::StreamInformation *stream_information) const
Get the stream information for a given session name.
Definition sap_listener.cc:129
std::thread thread_
The thread for the SAP/SDP announcements.
Definition sap_listener.h:249
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
SdpTypeEnum GetType(const std::string_view &line) const
Get the SDP attribute type doe a single SDP line of text.
Definition sap_listener.cc:154
std::map< std::string, std::string, std::less<> > ParseAttributes(const std::string_view &line) const
Parse any attributes.
Definition sap_listener.cc:182
std::map< std::string, std::string, std::less<> > ParseAttributes1(const std::string_view &line) const
Parse any attributes.
Definition sap_listener.cc:165
void Start()
Start the SAP/SDP announcements thread.
Definition sap_listener.cc:143
std::map< std::string, std::string, std::less<> > ParseAttributesEqual(const std::string &line) const
Parse attributes with equals.
int sockfd_
The socket for the SAP/SDP announcements.
Definition sap_listener.h:251
std::map< std::string, SapCallback, std::less<> > callbacks_
The list of SAP/SDP announcement callbacks.
Definition sap_listener.h:243
static bool running_
The SAP/SDP announcements thread running flag.
Definition sap_listener.h:255
std::map< std::string, SdpMessage, std::less<> > announcements_
The list of SAP/SDP announcements.
Definition sap_listener.h:245
std::array< uint8_t, mediax::rtp::kMaxUdpData > udpdata_
The buffer for the UDP data.
Definition sap_listener.h:247
static void SapListenerThread(SapListener *sap)
Function to broadcast SAP announcements for a list of streams.
Definition sap_listener.cc:105
const std::map< std::string, ::mediax::sap::SdpMessage, std::less<> > & GetSapAnnouncements() const
Get a list of SAP/SDP streams seen on the network.
Definition sap_listener.cc:402
bool SapStore(std::array< uint8_t, mediax::rtp::kMaxUdpData > *udpdata, uint32_t size)
Store or update the SAP data.
Definition sap_listener.cc:222
~SapListener()
Destroy the SAPListener::SAPListener object.
Definition sap_listener.cc:97
bool GetSapAnnouncement(std::string_view session_name, SdpMessage **sdp_message)
Get the Sap Announcement object.
Definition sap_listener.cc:406
static std::unique_ptr< SapListener > singleton_
The pointer to the SAPListener singleton.
Definition sap_listener.h:192
void Stop()
Stop the SAP/SDP announcement thread.
Definition sap_listener.cc:149
struct sockaddr_in multicast_addr_
The multicast address for the SAP/SDP announcements.
Definition sap_listener.h:253
The Session Announcment Protocol (SAP)/ Session Description Protocol (SDP) namespace.
Definition sap_announcer.cc:35
SdpTypeEnum
Definition sap_listener.h:72
std::function< void(sap::SdpMessage *sdp, void *data)> SapCallback
The SAPListener class is a singleton that listens for SAP/SDP announcements on the network.
Definition sap_listener.h:121
RTP streaming video types.
Struct capturing all stream information.
Definition rtp_types.h:223
A simplified SDP message structure.
Definition sap_listener.h:91
std::string ip_address_source
Session IPV4 source address.
Definition sap_listener.h:99
std::string session_name
Originator and session identifier.
Definition sap_listener.h:97
std::string sampling
The sampling rate of the video stream.
Definition sap_listener.h:109
uint32_t framerate
The framerate of the video stream.
Definition sap_listener.h:111
std::string ip_address
Session IPV4 destination address.
Definition sap_listener.h:101
std::string sdp_text
The complete SDP announcement.
Definition sap_listener.h:117
uint32_t width
The width of the video stream.
Definition sap_listener.h:107
uint32_t protocol_version
Protocol version.
Definition sap_listener.h:93
uint32_t port
The IPV4 source port.
Definition sap_listener.h:103
bool deleted
Set to true if the SDP message has been deleted.
Definition sap_listener.h:115
uint32_t bits_per_pixel
This bits per pixel of the video stream.
Definition sap_listener.h:113
uint16_t hash
Message hash.
Definition sap_listener.h:95
uint32_t height
The height of the video stream.
Definition sap_listener.h:105