sACN  HEAD (unstable)
Implementation of ANSI E1.31 (Streaming ACN)
View other versions:
Loading...
Searching...
No Matches
source_detector.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2024 ETC Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************
16 * This file is a part of sACN. For more information, go to:
17 * https://github.com/ETCLabs/sACN
18 *****************************************************************************/
19
20#ifndef SACN_CPP_SOURCE_DETECTOR_H_
21#define SACN_CPP_SOURCE_DETECTOR_H_
22
28#include "sacn/cpp/common.h"
29
30#include <vector>
31
33#include "etcpal/cpp/inet.h"
34#include "etcpal/cpp/uuid.h"
35
42namespace sacn
43{
122{
123public:
129 {
130 public:
131 NotifyHandler() = default;
132 virtual ~NotifyHandler() = default;
133
138 NotifyHandler(const NotifyHandler& other) = default;
139
145 NotifyHandler& operator=(const NotifyHandler& other) = default;
146
151 NotifyHandler(NotifyHandler&& other) = default;
152
159
176 const etcpal::Uuid& cid,
177 const std::string& name,
178 const std::vector<uint16_t>& sourced_universes) = 0;
179
187 virtual void HandleSourceExpired(RemoteSourceHandle handle, const etcpal::Uuid& cid, const std::string& name) = 0;
188
207 };
208
213 struct Settings
214 {
215 /********* Required values **********/
216
217 /********* Optional values **********/
218
223
228
231
233 Settings() = default;
234 };
235
236 SourceDetector() = delete;
237
238 static etcpal::Error Startup(NotifyHandler& notify_handler, McastMode mcast_mode);
239 static etcpal::Error Startup(NotifyHandler& notify_handler, std::vector<SacnMcastInterface>& netints);
240 static etcpal::Error Startup(const Settings& settings, NotifyHandler& notify_handler, McastMode mcast_mode);
241 static etcpal::Error Startup(const Settings& settings,
242 NotifyHandler& notify_handler,
243 std::vector<SacnMcastInterface>& netints);
244 static void Shutdown();
245 static etcpal::Error ResetNetworking(McastMode mcast_mode);
246 static etcpal::Error ResetNetworking(std::vector<SacnMcastInterface>& netints);
247 static std::vector<EtcPalMcastNetintId> GetNetworkInterfaces();
248
249private:
250 static SacnSourceDetectorConfig TranslateConfig(const Settings& settings, NotifyHandler& notify_handler);
251};
252
257namespace internal
258{
259extern "C" inline void SourceDetectorCbSourceUpdated(sacn_remote_source_t handle,
260 const EtcPalUuid* cid,
261 const char* name,
262 const uint16_t* sourced_universes,
263 size_t num_sourced_universes,
264 void* context)
265{
266 if (context && cid && name)
267 {
268 std::vector<uint16_t> sourced_vec;
269 if (sourced_universes && (num_sourced_universes > 0))
270 sourced_vec.assign(sourced_universes, sourced_universes + num_sourced_universes);
271 static_cast<SourceDetector::NotifyHandler*>(context)->HandleSourceUpdated(handle, *cid, name, sourced_vec);
272 }
273}
274
275extern "C" inline void SourceDetectorCbSourceExpired(sacn_remote_source_t handle,
276 const EtcPalUuid* cid,
277 const char* name,
278 void* context)
279{
280 if (context && cid && name)
281 {
282 static_cast<SourceDetector::NotifyHandler*>(context)->HandleSourceExpired(handle, *cid, name);
283 }
284}
285
286extern "C" inline void SourceDetectorCbMemoryLimitExceeded(void* context)
287{
288 if (context)
289 {
290 static_cast<SourceDetector::NotifyHandler*>(context)->HandleMemoryLimitExceeded();
291 }
292}
293
294}; // namespace internal
295
320 McastMode mcast_mode = McastMode::kEnabledOnAllInterfaces)
321{
322 SacnSourceDetectorConfig config = TranslateConfig(Settings(), notify_handler);
323
325 if (mcast_mode == McastMode::kDisabledOnAllInterfaces)
326 netint_config.no_netints = true;
327
328 return sacn_source_detector_create(&config, &netint_config);
329}
330
351inline etcpal::Error SourceDetector::Startup(NotifyHandler& notify_handler, std::vector<SacnMcastInterface>& netints)
352{
353 return Startup(Settings(), notify_handler, netints);
354}
355
377 NotifyHandler& notify_handler,
378 McastMode mcast_mode = McastMode::kEnabledOnAllInterfaces)
379{
380 SacnSourceDetectorConfig config = TranslateConfig(settings, notify_handler);
381
383 if (mcast_mode == McastMode::kDisabledOnAllInterfaces)
384 netint_config.no_netints = true;
385
386 return sacn_source_detector_create(&config, &netint_config);
387}
388
408 NotifyHandler& notify_handler,
409 std::vector<SacnMcastInterface>& netints)
410{
411 SacnSourceDetectorConfig config = TranslateConfig(settings, notify_handler);
412
413 if (netints.empty())
414 return sacn_source_detector_create(&config, nullptr);
415
417 netint_config.netints = netints.data();
418 netint_config.num_netints = netints.size();
419
420 return sacn_source_detector_create(&config, &netint_config);
421}
422
434
460inline etcpal::Error SourceDetector::ResetNetworking(McastMode mcast_mode = McastMode::kEnabledOnAllInterfaces)
461{
463 if (mcast_mode == McastMode::kDisabledOnAllInterfaces)
464 netint_config.no_netints = true;
465
466 return sacn_source_detector_reset_networking(&netint_config);
467}
468
493inline etcpal::Error SourceDetector::ResetNetworking(std::vector<SacnMcastInterface>& sys_netints)
494{
495 if (sys_netints.empty())
496 {
498 }
499
501 netint_config.netints = sys_netints.data();
502 netint_config.num_netints = sys_netints.size();
503
504 return sacn_source_detector_reset_networking(&netint_config);
505}
506
512inline std::vector<EtcPalMcastNetintId> SourceDetector::GetNetworkInterfaces()
513{
514 // This uses a guessing algorithm with a while loop to avoid race conditions.
515 size_t size_guess = 4u;
516 std::vector<EtcPalMcastNetintId> netints(size_guess);
517 size_t num_netints = sacn_source_detector_get_network_interfaces(netints.data(), netints.size());
518
519 while (num_netints > netints.size())
520 {
521 size_guess = num_netints + 4u;
522 netints.resize(size_guess);
523 num_netints = sacn_source_detector_get_network_interfaces(netints.data(), netints.size());
524 }
525
526 netints.resize(num_netints);
527 return netints;
528}
529
530inline SacnSourceDetectorConfig SourceDetector::TranslateConfig(const Settings& settings, NotifyHandler& notify_handler)
531{
532 // clang-format off
533 SacnSourceDetectorConfig config = {
534 {
535 internal::SourceDetectorCbSourceUpdated,
536 internal::SourceDetectorCbSourceExpired,
537 internal::SourceDetectorCbMemoryLimitExceeded,
538 &notify_handler
539 },
540 settings.source_count_max,
541 settings.universes_per_source_max,
542 settings.ip_supported
543 };
544 // clang-format on
545
546 return config;
547}
548
549}; // namespace sacn
550
551#endif // SACN_CPP_SOURCE_DETECTOR_H_
A base class for a class that receives common notification callbacks from sACN.
Definition common.h:73
A base class for a class that receives notification callbacks from a sACN Source Detector.
Definition source_detector.h:129
NotifyHandler & operator=(const NotifyHandler &other)=default
Defaulted copy assignment operator.
virtual void HandleMemoryLimitExceeded()
Notify that the module has run out of memory to track universes or sources.
Definition source_detector.h:206
NotifyHandler(const NotifyHandler &other)=default
Defaulted copy constructor.
virtual void HandleSourceUpdated(RemoteSourceHandle handle, const etcpal::Uuid &cid, const std::string &name, const std::vector< uint16_t > &sourced_universes)=0
Notify that a source is new or has changed.
NotifyHandler(NotifyHandler &&other)=default
Defaulted move constructor.
virtual void HandleSourceExpired(RemoteSourceHandle handle, const etcpal::Uuid &cid, const std::string &name)=0
Notify that a source is no longer transmitting Universe Discovery messages.
NotifyHandler & operator=(NotifyHandler &&other)=default
Defaulted move assignment operator.
An instance of sACN Source Detector functionality.
Definition source_detector.h:122
static void Shutdown()
Destroy the sACN Source Detector.
Definition source_detector.h:430
static std::vector< EtcPalMcastNetintId > GetNetworkInterfaces()
Obtain the source detector's network interfaces.
Definition source_detector.h:512
static etcpal::Error ResetNetworking(McastMode mcast_mode)
Resets the underlying network sockets and packet receipt state for the sACN Source Detector.
Definition source_detector.h:460
static etcpal::Error Startup(NotifyHandler &notify_handler, McastMode mcast_mode)
Start the sACN Source Detector with default settings.
Definition source_detector.h:319
C++ wrapper for the sACN init/deinit functions.
sacn_ip_support_t
This enum defines how the API module will use IPv4 and IPv6 networking.
Definition common.h:75
uint16_t sacn_remote_source_t
A source discovered on an sACN network that has a CID - used by Receiver and Merge Receiver.
Definition common.h:47
#define SACN_NETINT_CONFIG_DEFAULT_INIT
Initializes the members of a SacnNetintConfig to defaults.
Definition common.h:187
@ kSacnIpV4AndIpV6
Use both IPv4 and IPv6.
Definition common.h:81
void sacn_source_detector_destroy()
Destroy the sACN Source Detector.
Definition source_detector.c:129
etcpal_error_t sacn_source_detector_reset_networking(const SacnNetintConfig *sys_netint_config)
Updates the source detector system network interfaces.
Definition source_detector.c:178
etcpal_error_t sacn_source_detector_create(const SacnSourceDetectorConfig *config, const SacnNetintConfig *netint_config)
Create the sACN Source Detector.
Definition source_detector.c:85
size_t sacn_source_detector_get_network_interfaces(EtcPalMcastNetintId *netints, size_t netints_size)
Obtain the source detector's network interfaces.
Definition source_detector.c:225
@ kSacnSourceDetectorInfinite
Constant for "infinite" when listening for sources or universes on a source.
Definition source_detector.h:148
A namespace which contains all C++ language definitions in the sACN library.
Definition common.h:50
sacn_remote_source_t RemoteSourceHandle
A source discovered on an sACN network that has a CID - used by Receiver and Merge Receiver.
Definition common.h:59
McastMode
Determines whether multicast traffic is allowed through all interfaces or none.
Definition common.h:53
sACN Source Detector API definitions
Network interface configuration information to give the sACN library.
Definition common.h:114
SacnMcastInterface * netints
An array of network interface IDs to which to restrict multicast traffic.
Definition common.h:117
size_t num_netints
Size of netints array.
Definition common.h:119
bool no_netints
If this is true, no network interfaces will be used for multicast.
Definition common.h:122
A set of configuration information for the sACN Source Detector.
Definition source_detector.h:218
A set of configuration settings that a source detector needs to initialize.
Definition source_detector.h:214
int source_count_max
The maximum number of sources the detector will record.
Definition source_detector.h:222
Settings()=default
Create default data structure.
sacn_ip_support_t ip_supported
What IP networking the source detector will support.
Definition source_detector.h:230
int universes_per_source_max
The maximum number of universes the detector will record for a source.
Definition source_detector.h:227