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 <atomic>
21 #include <chrono>
22 #include <queue>
23 #include <string>
24 #include <map>
25 #include <mutex>
26 #include <optional>
27 //#include "File.h"
28 #include "AlcPacket.h"
29 #include "FileDeliveryTable.h"
30 
31 namespace LibFlute {
32 
33  class File;
34 
41  class Transmitter {
42  public:
47 
48 
53  public:
54  using date_time_type = std::chrono::system_clock::time_point;
55 
57  COMPRESSION_NONE = 0, //< No compression
58  COMPRESSION_GZIP, //< Use gzip compression when encoding the file
59  COMPRESSION_DEFLATE //< Use deflate compression when encoding the file
60  };
61 
62  FileDescription() = delete;
69  FileDescription(const std::string &content_location, const std::string &filename);
70 
82  FileDescription(const std::string &content_location, const std::vector<char> &data);
83  FileDescription(const std::string &content_location, const std::vector<unsigned char> &data);
97  FileDescription(const std::string &content_location, const char *data, size_t length);
98 
108  FileDescription(const std::string &content_location);
109 
117  FileDescription(const FileDescription &other);
118 
127 
133  virtual ~FileDescription();
134 
145 
156 
162  bool operator==(const FileDescription &other) const;
163 
169  bool has_tsi() const { return _tsi.has_value(); };
170 
177  uint64_t tsi() const { return _tsi?_tsi.value():0; };
178 
186  uint32_t toi() const { return _file_entry.toi; };
187 
194  return _file_entry;
195  };
196 
206  const char *data();
207 
217  size_t data_length();
218 
229 
235  FileDescription &set_content_location(const std::string &location);
236 
246  FileDescription &set_content(const std::string &filename);
247 
260  FileDescription &set_content(const char *data, size_t data_length);
261 
274  FileDescription &set_content(const std::vector<char> &data);
275  FileDescription &set_content(const std::vector<unsigned char> &data);
286  FileDescription &set_content_type(const std::string &content_type);
287 
294  FileDescription &set_expiry_time(const date_time_type &expiry_time);
295 
302 
311  FileDescription &set_etag(const std::string &etag);
312 
318  const std::string &get_etag() const;
319 
320  protected:
321  friend class Transmitter;
328  FileDescription &tsi(uint64_t val) { _tsi = val; return *this; };
335  FileDescription &toi(uint32_t val) { _file_entry.toi = val; return *this; };
336 
342  uint32_t previous_toi() const { return _previous_toi; };
343 
349  FileDescription &reset_previous_toi() { _previous_toi = 0; return *this; };
350 
358  FileDescription &merge_fec_oti(const FecOti &fec_oti);
359 
360  private:
361  void _attach_file(const std::string &filename);
362  void _free_file_data();
363  void _calculate_file_entry();
364  void _reset_toi(); //< Zero the TOI for reassignment, remembering the old value in _previous_toi
365 
366  std::optional<uint64_t> _tsi; //< The last TSI this file was associated with
367  FileDeliveryTable::FileEntry _file_entry; //< The FDT File entry values to use
368  CompressionAlgorithm _compression_type; //< The compression to apply to _data
369  std::string _filename; //< The filename that _data was loaded from, empty for no local file
370  int _file_handle; //< The file handle of the open _filename
371  const char *_data; //< The uncompressed file contents (may be mapped file)
372  size_t _data_length; //< The length of the uncompressed file contents
373  uint32_t _previous_toi = 0; //< TOI this description held before a content/compression
374  //< change zeroed it for reassignment, so the Transmitter can
375  //< remove that now-orphaned FDT entry; 0 once consumed
376  };
377 
384  typedef std::function<void(uint32_t)> completion_callback_t;
385 
416  Transmitter( const std::string& destination_address,
417  short port, uint64_t tsi, unsigned short mtu,
418  uint32_t rate_limit,
419  boost::asio::io_context& io_context,
420  const std::optional<boost::asio::ip::udp::endpoint>& tunnel_endpoint = std::nullopt,
422  bool active = true,
423  const std::optional<std::string>& source_address = std::nullopt);
424 
428  virtual ~Transmitter();
429 
435  const std::optional<boost::asio::ip::udp::endpoint> &udp_tunnel_address() const { return _tunnel_endpoint; };
436 
445  Transmitter &udp_tunnel_address(const boost::asio::ip::udp::endpoint &new_tunnel_endpoint);
446 
455  Transmitter &udp_tunnel_address(boost::asio::ip::udp::endpoint &&new_tunnel_endpoint);
456 
465  Transmitter &udp_tunnel_address(const std::optional<boost::asio::ip::udp::endpoint> &new_tunnel_endpoint);
466 
475  Transmitter &udp_tunnel_address(std::optional<boost::asio::ip::udp::endpoint> &&new_tunnel_endpoint);
476 
484  Transmitter &udp_tunnel_address(const std::nullopt_t&);
485 
493  uint32_t rate_limit() const { return _rate_limit; };
494 
503  Transmitter &rate_limit(uint32_t limit) { _rate_limit = limit; return *this; };
504 
513  const boost::asio::ip::udp::endpoint &endpoint() const { return _endpoint; };
514 
527  Transmitter &endpoint(const std::string &address, uint32_t port);
528 
541  Transmitter &endpoint(const boost::asio::ip::udp::endpoint &destination);
542  Transmitter &endpoint(boost::asio::ip::udp::endpoint &&destination);
550  const std::optional<boost::asio::ip::address> &source_address() const { return _source_address; };
551 
566  Transmitter &source_address(const std::optional<boost::asio::ip::address> &source);
567  Transmitter &source_address(std::optional<boost::asio::ip::address> &&source);
576  void enable_ipsec( uint32_t spi, const std::string& aes_key);
577 
594  uint16_t send(const std::string& content_location,
595  const std::string& content_type,
596  uint32_t expires,
597  char* data,
598  size_t length);
599 
614  uint16_t send(const std::shared_ptr<FileDescription> &file_description);
615 
621  uint64_t seconds_since_epoch();
622 
628  void register_completion_callback(completion_callback_t cb) { _completion_cb = cb; };
629 
636  void activate();
637 
652  void deactivate(bool finish_file_transmissions = false);
653 
659  size_t number_of_files() { std::lock_guard<std::mutex> guard(_files_mutex); return _files.size(); };
660 
661  private:
662  void send_fdt();
663  void send_next_packet();
664  void fdt_send_tick(const boost::system::error_code& error);
665  void start_fdt_repeat_timer();
666 
667  void _complete_deactivation();
668 
669  void file_transmitted(uint32_t toi);
670 
671  void handle_send_to(const boost::system::error_code& error);
672  boost::asio::ip::udp::endpoint _endpoint;
673  std::optional<boost::asio::ip::address> _source_address;
674  boost::asio::ip::udp::socket _socket;
675  boost::asio::io_context& _io_context;
676  boost::asio::steady_timer _send_timer;
677  boost::asio::steady_timer _fdt_timer;
678 
679  uint64_t _tsi;
680  uint16_t _mtu;
681 
682  std::unique_ptr<FileDeliveryTable> _fdt;
683  std::map<uint32_t, std::shared_ptr<File>> _files;
684  std::mutex _files_mutex;
685 
686  unsigned _fdt_repeat_interval = 5;
687  uint16_t _toi = 1;
688 
689  uint32_t _max_payload;
690  FecOti _fec_oti;
691 
692  completion_callback_t _completion_cb = nullptr;
693  std::string _mcast_address;
694 
695  uint32_t _rate_limit = 0;
696  std::optional<boost::asio::ip::udp::endpoint> _tunnel_endpoint = std::nullopt;
697  boost::asio::ip::address _tunnel_local_address;
698 
699  std::atomic<bool> _active;
700  std::atomic<bool> _deactivate_when_all_files_sent = false;
701  };
702 
703 } // end namespace LibFlute
FdtNamespace
FDT namespace enumeration.
File Description object.
Definition: Transmitter.h:52
FileDescription & set_content_location(const std::string &location)
Set Content-Location.
uint64_t tsi() const
Get the associated TSI value.
Definition: Transmitter.h:177
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:54
FileDescription & toi(uint32_t val)
Set the TOI (used by the Transmitter class)
Definition: Transmitter.h:335
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:186
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:328
FileDescription & reset_previous_toi()
Reset the previous TOI value.
Definition: Transmitter.h:349
bool has_tsi() const
Has a Transmitter associated a TSI with this file?
Definition: Transmitter.h:169
const FileDeliveryTable::FileEntry & file_entry() const
Get the FDT file entry.
Definition: Transmitter.h:193
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.
uint32_t previous_toi() const
Get the TOI before reset.
Definition: Transmitter.h:342
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:41
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:659
const std::optional< boost::asio::ip::udp::endpoint > & udp_tunnel_address() const
Get UDP Tunnel Address.
Definition: Transmitter.h:435
Transmitter & rate_limit(uint32_t limit)
Set Maximum Bit Rate.
Definition: Transmitter.h:503
const std::optional< boost::asio::ip::address > & source_address() const
Get the optional source address for the FLUTE session.
Definition: Transmitter.h:550
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:384
const boost::asio::ip::udp::endpoint & endpoint() const
Get UDP Address for FLUTE session.
Definition: Transmitter.h:513
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:628
uint32_t rate_limit() const
Get Maximum Bit Rate.
Definition: Transmitter.h:493
void deactivate(bool finish_file_transmissions=false)
Deactivate the FLUTE session.
void activate()
Activate the FLUTE session.
OTI values struct.
Definition: flute_types.h:51
An entry for a file in the FDT.