libflute
Transmitter.h
Go to the documentation of this file.
1 // libflute - FLUTE/ALC library
2 //
3 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
4 // 2025 British Broadcasting Corporation (David Waring <david.waring2@bbc.co.uk>)
5 //
6 // Licensed under the License terms and conditions for use, reproduction, and
7 // distribution of 5G-MAG software (the “License”). You may not use this file
8 // except in compliance with the License. You may obtain a copy of the License at
9 // https://www.5g-mag.com/reference-tools. Unless required by applicable law or
10 // agreed to in writing, software distributed under the License is distributed on
11 // an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 // or implied.
13 //
14 // See the License for the specific language governing permissions and limitations
15 // under the License.
16 //
17 #pragma once
18 #include <boost/asio.hpp>
19 #include <boost/bind/bind.hpp>
20 #include <chrono>
21 #include <queue>
22 #include <string>
23 #include <map>
24 #include <mutex>
25 #include <optional>
26 //#include "File.h"
27 #include "AlcPacket.h"
28 #include "FileDeliveryTable.h"
29 
30 namespace LibFlute {
31 
32  class File;
33 
40  class Transmitter {
41  public:
46 
47 
52  public:
53  using date_time_type = std::chrono::system_clock::time_point;
54 
56  COMPRESSION_NONE = 0, //< No compression
57  COMPRESSION_GZIP, //< Use gzip compression when encoding the file
58  COMPRESSION_DEFLATE //< Use deflate compression when encoding the file
59  };
60 
61  FileDescription() = delete;
68  FileDescription(const std::string &content_location, const std::string &filename);
69 
81  FileDescription(const std::string &content_location, const std::vector<char> &data);
82  FileDescription(const std::string &content_location, const std::vector<unsigned char> &data);
96  FileDescription(const std::string &content_location, const char *data, size_t length);
97 
107  FileDescription(const std::string &content_location);
108 
116  FileDescription(const FileDescription &other);
117 
126 
132  virtual ~FileDescription();
133 
144 
155 
161  bool operator==(const FileDescription &other) const;
162 
168  bool has_tsi() const { return _tsi.has_value(); };
169 
176  uint64_t tsi() const { return _tsi?_tsi.value():0; };
177 
185  uint32_t toi() const { return _file_entry.toi; };
186 
193  return _file_entry;
194  };
195 
205  const char *data();
206 
216  size_t data_length();
217 
228 
234  FileDescription &set_content_location(const std::string &location);
235 
245  FileDescription &set_content(const std::string &filename);
246 
259  FileDescription &set_content(const char *data, size_t data_length);
260 
273  FileDescription &set_content(const std::vector<char> &data);
274  FileDescription &set_content(const std::vector<unsigned char> &data);
285  FileDescription &set_content_type(const std::string &content_type);
286 
293  FileDescription &set_expiry_time(const date_time_type &expiry_time);
294 
301 
310  FileDescription &set_etag(const std::string &etag);
311 
317  const std::string &get_etag() const;
318 
319  protected:
320  friend class Transmitter;
327  FileDescription &tsi(uint64_t val) { _tsi = val; return *this; };
334  FileDescription &toi(uint32_t val) { _file_entry.toi = val; return *this; };
335 
343  FileDescription &merge_fec_oti(const FecOti &fec_oti);
344 
345  private:
346  void _attach_file(const std::string &filename);
347  void _free_file_data();
348  void _calculate_file_entry();
349 
350  std::optional<uint64_t> _tsi; //< The last TSI this file was associated with
351  FileDeliveryTable::FileEntry _file_entry; //< The FDT File entry values to use
352  CompressionAlgorithm _compression_type; //< The compression to apply to _data
353  std::string _filename; //< The filename that _data was loaded from, empty for no local file
354  int _file_handle; //< The file handle of the open _filename
355  const char *_data; //< The uncompressed file contents (may be mapped file)
356  size_t _data_length; //< The length of the uncompressed file contents
357  };
358 
365  typedef std::function<void(uint32_t)> completion_callback_t;
366 
397  Transmitter( const std::string& destination_address,
398  short port, uint64_t tsi, unsigned short mtu,
399  uint32_t rate_limit,
400  boost::asio::io_context& io_context,
401  const std::optional<boost::asio::ip::udp::endpoint>& tunnel_endpoint = std::nullopt,
403  bool active = true,
404  const std::optional<std::string>& source_address = std::nullopt);
405 
409  virtual ~Transmitter();
410 
416  const std::optional<boost::asio::ip::udp::endpoint> &udp_tunnel_address() const { return _tunnel_endpoint; };
417 
426  Transmitter &udp_tunnel_address(const boost::asio::ip::udp::endpoint &new_tunnel_endpoint);
427 
436  Transmitter &udp_tunnel_address(boost::asio::ip::udp::endpoint &&new_tunnel_endpoint);
437 
446  Transmitter &udp_tunnel_address(const std::optional<boost::asio::ip::udp::endpoint> &new_tunnel_endpoint);
447 
456  Transmitter &udp_tunnel_address(std::optional<boost::asio::ip::udp::endpoint> &&new_tunnel_endpoint);
457 
465  Transmitter &udp_tunnel_address(const std::nullopt_t&);
466 
474  uint32_t rate_limit() const { return _rate_limit; };
475 
484  Transmitter &rate_limit(uint32_t limit) { _rate_limit = limit; return *this; };
485 
494  const boost::asio::ip::udp::endpoint &endpoint() const { return _endpoint; };
495 
508  Transmitter &endpoint(const std::string &address, uint32_t port);
509 
522  Transmitter &endpoint(const boost::asio::ip::udp::endpoint &destination);
523  Transmitter &endpoint(boost::asio::ip::udp::endpoint &&destination);
531  const std::optional<boost::asio::ip::address> &source_address() const { return _source_address; };
532 
547  Transmitter &source_address(const std::optional<boost::asio::ip::address> &source);
548  Transmitter &source_address(std::optional<boost::asio::ip::address> &&source);
557  void enable_ipsec( uint32_t spi, const std::string& aes_key);
558 
575  uint16_t send(const std::string& content_location,
576  const std::string& content_type,
577  uint32_t expires,
578  char* data,
579  size_t length);
580 
595  uint16_t send(const std::shared_ptr<FileDescription> &file_description);
596 
602  uint64_t seconds_since_epoch();
603 
609  void register_completion_callback(completion_callback_t cb) { _completion_cb = cb; };
610 
617  void activate();
618 
628  void deactivate();
629 
635  size_t number_of_files() { std::lock_guard<std::mutex> guard(_files_mutex); return _files.size(); };
636 
637  private:
638  void send_fdt();
639  void send_next_packet();
640  void fdt_send_tick(const boost::system::error_code& error);
641  void start_fdt_repeat_timer();
642 
643  void file_transmitted(uint32_t toi);
644 
645  void handle_send_to(const boost::system::error_code& error);
646  boost::asio::ip::udp::endpoint _endpoint;
647  std::optional<boost::asio::ip::address> _source_address;
648  boost::asio::ip::udp::socket _socket;
649  boost::asio::io_context& _io_context;
650  boost::asio::deadline_timer _send_timer;
651  boost::asio::deadline_timer _fdt_timer;
652 
653  uint64_t _tsi;
654  uint16_t _mtu;
655 
656  std::unique_ptr<FileDeliveryTable> _fdt;
657  std::map<uint32_t, std::shared_ptr<File>> _files;
658  std::mutex _files_mutex;
659 
660  unsigned _fdt_repeat_interval = 5;
661  uint16_t _toi = 1;
662 
663  uint32_t _max_payload;
664  FecOti _fec_oti;
665 
666  completion_callback_t _completion_cb = nullptr;
667  std::string _mcast_address;
668 
669  uint32_t _rate_limit = 0;
670  std::optional<boost::asio::ip::udp::endpoint> _tunnel_endpoint = std::nullopt;
671  boost::asio::ip::address _tunnel_local_address;
672 
673  bool _active;
674  };
675 
676 } // end namespace LibFlute
FdtNamespace
FDT namespace enumeration.
File Description object.
Definition: Transmitter.h:51
FileDescription & set_content_location(const std::string &location)
Set Content-Location.
uint64_t tsi() const
Get the associated TSI value.
Definition: Transmitter.h:176
size_t data_length()
Get the length in bytes of the data to be transmitted.
FileDescription & set_expiry_time(const date_time_type &expiry_time)
Change the file expiry time.
const char * data()
Get the data to be transmitted.
FileDescription & set_etag(const std::string &etag)
Set the ETag value for the file.
std::chrono::system_clock::time_point date_time_type
Definition: Transmitter.h:53
FileDescription & toi(uint32_t val)
Set the TOI (used by the Transmitter class)
Definition: Transmitter.h:334
FileDescription & operator=(const FileDescription &other)
Copy operator.
FileDescription & set_content_type(const std::string &content_type)
Change the file content type.
uint32_t toi() const
Get the TOI associated with this file description.
Definition: Transmitter.h:185
FileDescription & set_content(const std::string &filename)
Change the file contents using a local file.
FileDescription & tsi(uint64_t val)
Set the TSI (used by the Transmitter class)
Definition: Transmitter.h:327
bool has_tsi() const
Has a Transmitter associated a TSI with this file?
Definition: Transmitter.h:168
const FileDeliveryTable::FileEntry & file_entry() const
Get the FDT file entry.
Definition: Transmitter.h:192
FileDescription & merge_fec_oti(const FecOti &fec_oti)
Merge the FecOti values.
FileDescription & set_compression(CompressionAlgorithm compression)
Set the compression algorithm.
bool operator==(const FileDescription &other) const
Equality operator.
date_time_type get_expiry_time() const
Get the currently set expiry time.
const std::string & get_etag() const
Get the current ETag value.
FLUTE transmitter class.
Definition: Transmitter.h:40
uint64_t seconds_since_epoch()
Convenience function to get the current timestamp for expiry calculation.
virtual ~Transmitter()
Default destructor.
size_t number_of_files()
Get number of files currently in queue for sending.
Definition: Transmitter.h:635
const std::optional< boost::asio::ip::udp::endpoint > & udp_tunnel_address() const
Get UDP Tunnel Address.
Definition: Transmitter.h:416
void deactivate()
Deactivate the FLUTE session.
Transmitter & rate_limit(uint32_t limit)
Set Maximum Bit Rate.
Definition: Transmitter.h:484
const std::optional< boost::asio::ip::address > & source_address() const
Get the optional source address for the FLUTE session.
Definition: Transmitter.h:531
std::function< void(uint32_t)> completion_callback_t
Definition of a file transmission completion callback function that can be registered through ::regis...
Definition: Transmitter.h:365
const boost::asio::ip::udp::endpoint & endpoint() const
Get UDP Address for FLUTE session.
Definition: Transmitter.h:494
uint16_t send(const std::string &content_location, const std::string &content_type, uint32_t expires, char *data, size_t length)
Transmit a file (deprecated).
void enable_ipsec(uint32_t spi, const std::string &aes_key)
Enable IPSEC ESP encryption of FLUTE payloads.
uint16_t send(const std::shared_ptr< FileDescription > &file_description)
Transmit a file.
Transmitter(const std::string &destination_address, short port, uint64_t tsi, unsigned short mtu, uint32_t rate_limit, boost::asio::io_context &io_context, const std::optional< boost::asio::ip::udp::endpoint > &tunnel_endpoint=std::nullopt, FdtNamespace fdt_namespace=FileDeliveryTable::FDT_NS_NONE, bool active=true, const std::optional< std::string > &source_address=std::nullopt)
Constructor.
void register_completion_callback(completion_callback_t cb)
Register a callback for file transmission completion notifications.
Definition: Transmitter.h:609
uint32_t rate_limit() const
Get Maximum Bit Rate.
Definition: Transmitter.h:474
void activate()
Activate the FLUTE session.
OTI values struct.
Definition: flute_types.h:51
An entry for a file in the FDT.