receiver_base.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef TINYPLAYER3_RECEIVER_BASE_H
  2. #define TINYPLAYER3_RECEIVER_BASE_H
  3. #include "codec/codec_base.hpp"
  4. #include "core/utility.hpp"
  5. #include <fmt/chrono.h>
  6. #include <fmt/format.h>
  7. #include <functional>
  8. enum receiver_type {
  9. RECEIVER_TCP,
  10. RECEIVER_UDP,
  11. RECEIVER_UDP_FEC
  12. };
  13. class receiver_base {
  14. public:
  15. using cb_func_type = std::function<void(frame_info)>;
  16. struct create_config {
  17. cb_func_type cb_func = nullptr;
  18. bool enable_log = false;
  19. };
  20. explicit receiver_base(const create_config &conf) {
  21. cb_func = conf.cb_func;
  22. // create log file if requested
  23. if (conf.enable_log) {
  24. auto file_name = fmt::format("log_{:%Y_%m_%d_%H_%M_%S}.csv",
  25. std::chrono::system_clock::now());
  26. log_file.open(file_name);
  27. }
  28. }
  29. protected:
  30. void commit_frame(const frame_info &frame) {
  31. log_frame_received(frame.frame_id);
  32. cb_func(frame);
  33. }
  34. private:
  35. cb_func_type cb_func;
  36. std::ofstream log_file;
  37. void log_frame_received(uint64_t frame_id) {
  38. if (!log_file.is_open()) return;
  39. log_file << fmt::format("{},{}\n", frame_id, current_timestamp());
  40. }
  41. };
  42. #endif //TINYPLAYER3_RECEIVER_BASE_H