1
0
Fork 0
OpenLCP/protocol/esta/sacn/source.cpp

241 lines
6.9 KiB
C++

/*
sender.cpp
Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "extended.h"
#include "source.h"
namespace sACN {
/**
* @brief Source::Source
* @param cid
* @param fctn
*/
Source::Source(UUID::uuid cid, std::string fctn)
: Component(cid, fctn)
, discovery_worker_(&Source::discovery_loop_, this)
{
}
Source::~Source()
{
discovery_exitSignal_.set_value(); // set the exit signal for the discovery worker thread
discovery_worker_.join(); // wait for discovery to terminate normally
}
/**
* @brief Source::create
* @param num
*/
void Source::create(const uint16_t num)
{
if (universes_.count(num))
return;
auto metadata = std::make_shared<DATA::data_header>();
metadata->source_name = this->name();
metadata->universe = num;
universes_mutext_.lock();
universes_.emplace(num, std::make_shared<Universe>(this));
universes_[num]->setProvenance(metadata);
universes_mutext_.unlock();
}
/**
* @brief Source::terminate
* @param num
*/
void Source::terminate(const uint16_t num)
{
if (!universes_.count(num))
return;
universes_mutext_.lock();
auto metadata = universes_.at(num)->metadata();
metadata->options.stream_terminated = true;
universes_.at(num)->setProvenance(metadata);
universes_.erase(num);
universes_mutext_.unlock();
}
/**
* @brief Source::sendExtendedFrame
* @param vector
* @param header
* @param data
* @param ip
*/
void Source::sendExtendedFrame(const uint16_t vector,
std::shared_ptr<ACN::PDU::pdu_header> header,
std::shared_ptr<ACN::PDU::pdu_data> data,
const ACN::SDT::UDP::ipAddress& ip)
{
if (!(vector == VECTOR_E131_EXTENDED_DISCOVERY ||
vector == VECTOR_E131_EXTENDED_SYNCHRONIZATION))
return;
auto framepdu = std::make_shared<EXTENDED::Pdu>();
framepdu->setVector(vector);
framepdu->setHeader(header);
framepdu->setData(data);
rlpSendUdp(VECTOR_ROOT_E131_EXTENDED, framepdu, ip);
}
/**
* @brief Source::universe
* @param num
* @return
*/
std::shared_ptr<Universe> Source::universe(const uint16_t num)
{
if (!universes_.count(num))
return nullptr;
return universes_.at(num);
}
void Source::assignUserName(const std::string s)
{
ACN::Component::assignUserName(s);
for( auto & [_, universe] : universes_ )
universe->metadata()->source_name = name();
}
/**
* @brief Source::discovery_loop_
*/
void Source::discovery_loop_()
{
/// After terminating all universes, at least one empty universe
/// discovery message should be sent.
bool send_null_discovery = false;
auto future = discovery_exitSignal_.get_future();
while (future.wait_for(std::chrono::milliseconds(E131_UNIVERSE_DISCOVER_INTERVAL))
!= std::future_status::ready)
{
/// > \cite sACN 12.1 Universe Discovery and Termination
/// >
/// > Any source that is no longer sending any universe data may stop
/// > sending E1.31 Universe Discovery Packets until such time that it
/// > resumes transmission of E1.31 Data and/or Synchronization information.
if (universes_.empty())
{
if (send_null_discovery)
send_null_discovery = false;
else
continue;
}
else
send_null_discovery = true;
discovery_send_();
}
}
/**
* @brief Source::discovery_send_
*/
void Source::discovery_send_()
{
// framing layer header
auto frmheader = std::make_shared<EXTENDED::discovery_header>();
frmheader->source_name = name();
// universe discovery layer
auto header = std::make_shared<EXTENDED::DISCOVERY::discovery_list_header>();
// known universes
std::vector<std::shared_ptr<EXTENDED::DISCOVERY::discoveredUniverse>> list;
universes_mutext_.lock();
for (const auto & [num, univ] : universes_)
{
if (!univ->activeSlots())
continue;
if (univ->metadata()->options.stream_terminated)
continue;
if (univ->destination.type != ACN::SDT::SDT_ADDR_NULL)
continue;
list.emplace_back(std::make_shared<EXTENDED::DISCOVERY::discoveredUniverse>());
list.back()->universe = num;
}
universes_mutext_.unlock();
/// > \cite sACN 8.3 Page
/// >
/// > A single source may be transmitting on so many universes that the
/// > total number of universes it must include in its List of Universes
/// > will span multiple packets. Each one of these packets acts as a
/// > “page” of those universes.
///
/// > \cite sACN 8.4 Last Page
/// >
/// > The Universe Discovery Layer's Last Page field is an 8-bit field
/// > indicating the number of the final page being to be transmitted. ...
/// > Page numbers are indexed starting at 0.
header->last_page = list.size() / 512;
for (size_t i = 0; i <= header->last_page; i++)
{
/// > The Universe Discovery Layer's Page field is an 8-bit field
/// > indicating the page number of this E1.31 Universe Discovery Packet.
/// > Page numbers are indexed, starting at 0.
header->page = i;
// universe discover layer data
auto data = std::make_shared<EXTENDED::DISCOVERY::discovery_list_data>();
for (size_t n = i * 512; n < (i * 512) + 512; n++)
{
if (n >= list.size())
break;
data->found.push_back(list.at(n));
}
// framing layer data
auto dlistpdu = std::make_shared<EXTENDED::DISCOVERY::Pdu>();
dlistpdu->setVector(VECTOR_UNIVERSE_DISCOVERY_UNIVERSE_LIST);
dlistpdu->setHeader(header);
dlistpdu->setData(data);
// framing layer
if (enable_IPv4)
sendExtendedFrame(VECTOR_E131_EXTENDED_DISCOVERY, frmheader, dlistpdu,
sACN::IPv4MulticastAddress(E131_DISCOVERY_UNIVERSE));
if (enable_IPv6)
sendExtendedFrame(VECTOR_E131_EXTENDED_DISCOVERY, frmheader, dlistpdu,
sACN::IPv6MulticastAddress(E131_DISCOVERY_UNIVERSE));
}
}
} // SACN