From 01373b04dcd7bf455c2a04bd7c2bc166a0f37dbf Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Wed, 20 Nov 2024 11:55:05 +0000 Subject: [PATCH 01/27] Add error code "error_shutdown", Fix use of uninitialize value _link, Fix clang compilation error - uuid.cc Signed-off-by: Tomasz Szumski --- media-proxy/include/mesh/conn.h | 2 +- media-proxy/src/mesh/conn.cc | 2 ++ media-proxy/src/mesh/uuid.cc | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/media-proxy/include/mesh/conn.h b/media-proxy/include/mesh/conn.h index ad5da1cd2..d2904263c 100644 --- a/media-proxy/include/mesh/conn.h +++ b/media-proxy/include/mesh/conn.h @@ -66,7 +66,7 @@ enum class Result { error_bad_argument, error_out_of_memory, error_general_failure, - + error_shutdown, // TODO: more error codes to be added... }; diff --git a/media-proxy/src/mesh/conn.cc b/media-proxy/src/mesh/conn.cc index 85a178ebf..308fab981 100644 --- a/media-proxy/src/mesh/conn.cc +++ b/media-proxy/src/mesh/conn.cc @@ -15,6 +15,7 @@ Connection::Connection() _status = Status::initial; setting_link = false; transmitting = false; + _link = nullptr; metrics.inbound_bytes = 0; metrics.outbound_bytes = 0; @@ -273,6 +274,7 @@ const char * result2str(Result res) case Result::error_bad_argument: return "bad argument"; case Result::error_out_of_memory: return "out of memory"; case Result::error_general_failure: return "general failure"; + case Result::error_shutdown: return "processing shutdown"; default: return str_unknown; } } diff --git a/media-proxy/src/mesh/uuid.cc b/media-proxy/src/mesh/uuid.cc index 52ca52f91..25b857d7f 100644 --- a/media-proxy/src/mesh/uuid.cc +++ b/media-proxy/src/mesh/uuid.cc @@ -8,6 +8,7 @@ #include #include #include +#include namespace mesh { From ba5c36af604edd62a007bf9c7c5c6a5477276a8e Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Thu, 21 Nov 2024 07:52:22 +0000 Subject: [PATCH 02/27] Add ST2110 Rx/Tx 20/22/30 connections with basic UT Signed-off-by: Tomasz Szumski --- media-proxy/include/mesh/st2110.h | 63 ++++ media-proxy/include/mesh/st2110rx.h | 148 ++++++++ media-proxy/include/mesh/st2110tx.h | 146 +++++++ media-proxy/src/mesh/st2110.cc | 159 ++++++++ media-proxy/src/mesh/st2110_20rx.cc | 89 +++++ media-proxy/src/mesh/st2110_20tx.cc | 86 +++++ media-proxy/src/mesh/st2110_22rx.cc | 91 +++++ media-proxy/src/mesh/st2110_22tx.cc | 89 +++++ media-proxy/src/mesh/st2110_30rx.cc | 86 +++++ media-proxy/src/mesh/st2110_30tx.cc | 83 ++++ media-proxy/tests/st2110_tests.cc | 565 ++++++++++++++++++++++++++++ 11 files changed, 1605 insertions(+) create mode 100644 media-proxy/include/mesh/st2110.h create mode 100644 media-proxy/include/mesh/st2110rx.h create mode 100644 media-proxy/include/mesh/st2110tx.h create mode 100644 media-proxy/src/mesh/st2110.cc create mode 100644 media-proxy/src/mesh/st2110_20rx.cc create mode 100644 media-proxy/src/mesh/st2110_20tx.cc create mode 100644 media-proxy/src/mesh/st2110_22rx.cc create mode 100644 media-proxy/src/mesh/st2110_22tx.cc create mode 100644 media-proxy/src/mesh/st2110_30rx.cc create mode 100644 media-proxy/src/mesh/st2110_30tx.cc create mode 100644 media-proxy/tests/st2110_tests.cc diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h new file mode 100644 index 000000000..b65342d3d --- /dev/null +++ b/media-proxy/include/mesh/st2110.h @@ -0,0 +1,63 @@ +#ifndef ST2110_H +#define ST2110_H + +#include +#include +#include +#include +#include + +#include "conn.h" +#include "mesh_dp.h" +#include "logger.h" + +namespace mesh +{ + +namespace connection +{ + +#define ST_APP_PAYLOAD_TYPE_ST30 (111) +#define ST_APP_PAYLOAD_TYPE_ST20 (112) +#define ST_APP_PAYLOAD_TYPE_ST22 (114) + +/** + * ST2110 + * + * Base abstract class of ST2110. ST2110Rx/ST2110Tx + * inherit this class. + */ +class ST2110 : public Connection +{ + public: + static st_frame_fmt mesh_video_format_to_st_format(int fmt); + static st30_fmt mesh_audio_format_to_st_format(int fmt); + static st30_sampling mesh_audio_sampling_to_st_sampling(int sampling); + static st30_ptime mesh_audio_ptime_to_st_ptime(int ptime); + static void *get_frame_data_ptr(st_frame *src); + static void *get_frame_data_ptr(st30_frame *src); + + static void get_mtl_dev_params(mtl_init_params &st_param, const std::string &dev_port, + mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); + static mtl_handle get_mtl_handle(const std::string &dev_port, mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); + + ST2110() : _st(0){}; + virtual ~ST2110(){}; + + protected: + static int frame_available_cb(void *ptr); + + mtl_handle _st; + std::atomic _stop; + std::condition_variable_any _cv; + + private: +}; + +} // namespace connection + +} // namespace mesh + +#endif // ST2110_H diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h new file mode 100644 index 000000000..23a37399a --- /dev/null +++ b/media-proxy/include/mesh/st2110rx.h @@ -0,0 +1,148 @@ +#ifndef ST2110RX_H +#define ST2110RX_H + +#include "st2110.h" + +namespace mesh +{ + +namespace connection +{ + +/** + * ST2110Rx + * + * Base abstract class of ST2110Rx. ST2110_20Rx/ST2110_22Rx/ST2110_30Rx + * inherit this class. + */ +template class ST2110Rx : public ST2110 +{ + public: + ST2110Rx() + { + _kind = Kind::receiver; + + _handle = nullptr; + _ops = {0}; + _transfer_size = 0; + } + ~ST2110Rx() {} + + protected: + HANDLE _handle; + OPS _ops; + size_t _transfer_size; + std::jthread _frame_thread_handle; + context::Context _ctx; + + std::function _get_frame_fn; + std::function _put_frame_fn; + std::function _create_session_fn; + std::function _close_session_fn; + + Result on_establish(context::Context &ctx) override + { + _ctx = context::WithCancel(ctx); + _stop = false; + + _handle = _create_session_fn(_st, &_ops); + if (!_handle) { + log::error("Failed to create session"); + set_state(ctx, State::closed); + return set_result(Result::error_general_failure); + } + + /* Start MTL session thread. */ + try { + _frame_thread_handle = std::jthread(&ST2110Rx::frame_thread, this); + } catch (const std::system_error &e) { + log::error("Failed to create thread"); + set_state(ctx, State::closed); + return set_result(Result::error_out_of_memory); + } + + set_state(ctx, State::active); + return set_result(Result::success); + } + + Result on_shutdown(context::Context &ctx) override + { + _ctx.cancel(); + + _frame_thread_handle.join(); + + if (_handle) { + _close_session_fn(_handle); + _handle = nullptr; + } + set_state(ctx, State::closed); + return set_result(Result::success); + }; + + virtual void on_delete(context::Context &ctx) override {} + + private: + void frame_thread() + { + while (!_ctx.cancelled()) { + // Get full buffer from MTL + FRAME *frame_ptr = _get_frame_fn(_handle); + if (!frame_ptr) { /* no frame */ + std::mutex mx; + std::unique_lock lk(mx); + _cv.wait(lk, _ctx.stop_token(), [this] { return _stop.load(); }); + _stop = false; + if (_ctx.cancelled()) { + return; + } + continue; + } + // Forward buffer to emulated receiver + transmit(_ctx, get_frame_data_ptr(frame_ptr), _transfer_size); + // Return used buffer to MTL + _put_frame_fn(_handle, frame_ptr); + } + } +}; + +class ST2110_20Rx : public ST2110Rx +{ + public: + ST2110_20Rx(); + ~ST2110_20Rx(); + + Result configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + + private: +}; + +class ST2110_22Rx : public ST2110Rx +{ + public: + ST2110_22Rx(); + ~ST2110_22Rx(); + + Result configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + + private: +}; + +class ST2110_30Rx : public ST2110Rx +{ + public: + ST2110_30Rx(); + ~ST2110_30Rx(); + + Result configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); + + private: +}; + +} // namespace connection + +} // namespace mesh + +#endif // ST2110RX_H diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h new file mode 100644 index 000000000..6cd457476 --- /dev/null +++ b/media-proxy/include/mesh/st2110tx.h @@ -0,0 +1,146 @@ +#ifndef ST2110TX_H +#define ST2110TX_H + +#include "st2110.h" +#include + +namespace mesh +{ + +namespace connection +{ + +/** + * ST2110 + * + * Base abstract class of ST2110. ST2110_20Tx/ST2110_22Tx/ST2110_30Tx + * inherit this class. + */ +template class ST2110Tx : public ST2110 +{ + public: + ST2110Tx() + { + _kind = Kind::transmitter; + + _handle = nullptr; + _ops = {0}; + _transfer_size = 0; + }; + ~ST2110Tx(){}; + + protected: + HANDLE _handle; + OPS _ops; + uint32_t _transfer_size; + context::Context _ctx; + + std::function _get_frame_fn; + std::function _put_frame_fn; + std::function _create_session_fn; + std::function _close_session_fn; + + Result on_establish(context::Context &ctx) override + { + _ctx = context::WithCancel(ctx); + _stop = false; + + _handle = _create_session_fn(_st, &_ops); + if (!_handle) { + log::error("Failed to create session"); + set_state(ctx, State::closed); + return set_result(Result::error_general_failure); + } + + set_state(ctx, State::active); + return set_result(Result::success); + }; + + Result on_shutdown(context::Context &ctx) override + { + _ctx.cancel(); + + if (_handle) { + _close_session_fn(_handle); + _handle = nullptr; + } + set_state(ctx, State::closed); + return set_result(Result::success); + }; + + Result on_receive(context::Context &ctx, void *ptr, uint32_t sz, uint32_t &sent) override + { + sent = std::min(_transfer_size, sz); + // TODO: add error/warning if sent is different than _transfer_size + + FRAME *frame = NULL; + do { + // Get empty buffer from MTL + frame = _get_frame_fn(_handle); + if (!frame) { + std::mutex mx; + std::unique_lock lk(mx); + _cv.wait(lk, _ctx.stop_token(), [this] { return _stop.load(); }); + _stop = false; + if (_ctx.cancelled()) { + break; + } + } + } while (!frame); + + if (frame) { + // Copy data from emulated transmitter to MTL empty buffer + mtl_memcpy(get_frame_data_ptr(frame), ptr, sent); + // Return full buffer to MTL + _put_frame_fn(_handle, frame); + } else { + sent = 0; + return set_result(Result::error_shutdown); + } + return set_result(Result::success); + }; + + private: +}; + +class ST2110_20Tx : public ST2110Tx +{ + public: + ST2110_20Tx(); + ~ST2110_20Tx(); + + Result configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + + private: +}; + +class ST2110_22Tx : public ST2110Tx +{ + public: + ST2110_22Tx(); + ~ST2110_22Tx(); + + Result configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + + private: +}; + +class ST2110_30Tx : public ST2110Tx +{ + public: + ST2110_30Tx(); + ~ST2110_30Tx(); + + Result configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); + + private: +}; + +} // namespace connection + +} // namespace mesh + +#endif // ST2110TX_H diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc new file mode 100644 index 000000000..3795dbb37 --- /dev/null +++ b/media-proxy/src/mesh/st2110.cc @@ -0,0 +1,159 @@ +#include "st2110.h" + +namespace mesh +{ + +namespace connection +{ + +st_frame_fmt ST2110::mesh_video_format_to_st_format(int fmt) +{ + switch (fmt) { + case MESH_VIDEO_PIXEL_FORMAT_NV12: + return ST_FRAME_FMT_YUV420CUSTOM8; + case MESH_VIDEO_PIXEL_FORMAT_YUV422P: + return ST_FRAME_FMT_YUV422PLANAR8; + case MESH_VIDEO_PIXEL_FORMAT_YUV422P10LE: + return ST_FRAME_FMT_YUV422PLANAR10LE; + case MESH_VIDEO_PIXEL_FORMAT_YUV444P10LE: + return ST_FRAME_FMT_YUV444PLANAR10LE; + case MESH_VIDEO_PIXEL_FORMAT_RGB8: + return ST_FRAME_FMT_RGB8; + default: + return ST_FRAME_FMT_YUV422PLANAR10LE; + } +} + +st30_fmt ST2110::mesh_audio_format_to_st_format(int fmt) +{ + switch (fmt) { + case MESH_AUDIO_FORMAT_PCM_S8: + return ST30_FMT_PCM8; + case MESH_AUDIO_FORMAT_PCM_S16BE: + return ST30_FMT_PCM16; + case MESH_AUDIO_FORMAT_PCM_S24BE: + return ST30_FMT_PCM24; + default: + return ST30_FMT_MAX; + } +} + +st30_sampling ST2110::mesh_audio_sampling_to_st_sampling(int sampling) +{ + switch (sampling) { + case MESH_AUDIO_SAMPLE_RATE_48000: + return ST30_SAMPLING_48K; + case MESH_AUDIO_SAMPLE_RATE_96000: + return ST30_SAMPLING_96K; + case MESH_AUDIO_SAMPLE_RATE_44100: + return ST31_SAMPLING_44K; + default: + return ST30_SAMPLING_MAX; + } +} + +st30_ptime ST2110::mesh_audio_ptime_to_st_ptime(int ptime) +{ + switch (ptime) { + case MESH_AUDIO_PACKET_TIME_1MS: + return ST30_PTIME_1MS; + case MESH_AUDIO_PACKET_TIME_125US: + return ST30_PTIME_125US; + case MESH_AUDIO_PACKET_TIME_250US: + return ST30_PTIME_250US; + case MESH_AUDIO_PACKET_TIME_333US: + return ST30_PTIME_333US; + case MESH_AUDIO_PACKET_TIME_4MS: + return ST30_PTIME_4MS; + case MESH_AUDIO_PACKET_TIME_80US: + return ST31_PTIME_80US; + case MESH_AUDIO_PACKET_TIME_1_09MS: + return ST31_PTIME_1_09MS; + case MESH_AUDIO_PACKET_TIME_0_14MS: + return ST31_PTIME_0_14MS; + case MESH_AUDIO_PACKET_TIME_0_09MS: + return ST31_PTIME_0_09MS; + default: + return ST30_PTIME_MAX; + } +} + +void *ST2110::get_frame_data_ptr(st_frame *src) { return src->addr[0]; } +void *ST2110::get_frame_data_ptr(st30_frame *src) { return src->addr; } + +int ST2110::frame_available_cb(void *ptr) +{ + auto _this = static_cast(ptr); + if (!_this) { + return -1; + } + + _this->_stop.store(true); + _this->_cv.notify_all(); + + return 0; +} + +void ST2110::get_mtl_dev_params(mtl_init_params &st_param, const std::string &dev_port, + mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE]) +{ + if (getenv("KAHAWAI_CFG_PATH") == NULL) { + setenv("KAHAWAI_CFG_PATH", "/usr/local/etc/imtl.json", 0); + } + strlcpy(st_param.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + inet_pton(AF_INET, local_ip_addr, st_param.sip_addr[MTL_PORT_P]); + st_param.pmd[MTL_PORT_P] = mtl_pmd_by_port_name(st_param.port[MTL_PORT_P]); + st_param.num_ports = 1; + st_param.flags = MTL_FLAG_BIND_NUMA; + st_param.flags |= MTL_FLAG_TX_VIDEO_MIGRATE; + st_param.flags |= MTL_FLAG_RX_VIDEO_MIGRATE; + st_param.flags |= MTL_FLAG_RX_UDP_PORT_ONLY; + st_param.pacing = ST21_TX_PACING_WAY_AUTO; + st_param.log_level = log_level; + st_param.priv = NULL; + st_param.ptp_get_time_fn = NULL; + // Native af_xdp have only 62 queues available + if (st_param.pmd[MTL_PORT_P] == MTL_PMD_NATIVE_AF_XDP) { + st_param.rx_queues_cnt[MTL_PORT_P] = 62; + st_param.tx_queues_cnt[MTL_PORT_P] = 62; + } else { + st_param.rx_queues_cnt[MTL_PORT_P] = 128; + st_param.tx_queues_cnt[MTL_PORT_P] = 128; + } + st_param.lcores = NULL; + st_param.memzone_max = 9000; +} + +mtl_handle ST2110::get_mtl_handle(const std::string &dev_port, mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE]) +{ + static mtl_handle dev_handle; + static std::mutex mtx; + std::lock_guard lock(mtx); + + if (dev_handle) { + return dev_handle; + } + + mtl_init_params st_param = {0}; + get_mtl_dev_params(st_param, dev_port, log_level, local_ip_addr); + // create device + dev_handle = mtl_init(&st_param); + if (!dev_handle) { + log::error("Failed to initialize MTL device"); + return nullptr; + } + + // start MTL device + if (mtl_start(dev_handle) != 0) { + log::error("Failed to start MTL device"); + return nullptr; + } + + return dev_handle; +} + +} // namespace connection + +} // namespace mesh diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc new file mode 100644 index 000000000..a83aad6fe --- /dev/null +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -0,0 +1,89 @@ +#include "st2110rx.h" + +namespace mesh +{ + +namespace connection +{ + +ST2110_20Rx::ST2110_20Rx() +{ + _get_frame_fn = st20p_rx_get_frame; + _put_frame_fn = st20p_rx_put_frame; + _create_session_fn = st20p_rx_create; + _close_session_fn = st20p_rx_free; +} + +ST2110_20Rx::~ST2110_20Rx() {} + +Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, + const MeshConfig_Video &cfg_video) +{ + static int session_id; + static std::mutex mtx; + std::lock_guard lock(mtx); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (_st == nullptr) { + log::error("Failed to get MTL device"); + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + char session_name[NAME_MAX] = ""; + + snprintf(session_name, NAME_MAX, "mcm_rx_st20_%d", session_id++); + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, _ops.port.mcast_sip_addr[MTL_PORT_P]); + _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; + + strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + _ops.port.num_port = 1; + _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; + _ops.name = strdup(session_name); + _ops.width = cfg_video.width; + _ops.height = cfg_video.height; + _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + _ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; + _ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + _ops.device = ST_PLUGIN_DEVICE_AUTO; + _ops.framebuff_cnt = 4; + + log::info("ProxyContext: %s...", __func__); + log::info("port : %s", _ops.port.port[MTL_PORT_P]); + log::info("ip_addr : %d %d %d %d", _ops.port.ip_addr[MTL_PORT_P][0], + _ops.port.ip_addr[MTL_PORT_P][2], _ops.port.ip_addr[MTL_PORT_P][2], + _ops.port.ip_addr[MTL_PORT_P][3]); + log::info("mcast_sip_addr: %d %d %d %d", _ops.port.mcast_sip_addr[MTL_PORT_P][0], + _ops.port.mcast_sip_addr[MTL_PORT_P][1], _ops.port.mcast_sip_addr[MTL_PORT_P][2], + _ops.port.mcast_sip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", _ops.port.num_port); + log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); + log::info("payload_type : %d", _ops.port.payload_type); + log::info("name : %s", _ops.name); + log::info("width : %d", _ops.width); + log::info("height : %d", _ops.height); + log::info("fps : %d", _ops.fps); + log::info("transport_fmt : %d", _ops.transport_fmt); + log::info("output_fmt : %d", _ops.output_fmt); + log::info("device : %d", _ops.device); + log::info("framebuff_cnt : %d", _ops.framebuff_cnt); + + _transfer_size = st_frame_size(_ops.output_fmt, _ops.width, _ops.height, false); + _ops.priv = this; // app handle register to lib + _ops.notify_frame_available = frame_available_cb; + + set_state(ctx, State::configured); + return set_result(Result::success); +} + +} // namespace connection + +} // namespace mesh diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc new file mode 100644 index 000000000..c1b944b9c --- /dev/null +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -0,0 +1,86 @@ +#include "st2110tx.h" + +namespace mesh +{ + +namespace connection +{ + +ST2110_20Tx::ST2110_20Tx() +{ + _get_frame_fn = st20p_tx_get_frame; + _put_frame_fn = st20p_tx_put_frame; + _create_session_fn = st20p_tx_create; + _close_session_fn = st20p_tx_free; +} + +ST2110_20Tx::~ST2110_20Tx() {} + +Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, + const MeshConfig_Video &cfg_video) +{ + static int session_id; + static std::mutex mtx; + std::lock_guard lock(mtx); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (_st == nullptr) { + log::error("Failed to get MTL device"); + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + char session_name[NAME_MAX] = ""; + + snprintf(session_name, NAME_MAX, "mcm_tx_st20_%d", session_id++); + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.dip_addr[MTL_PORT_P]); + _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; + _ops.port.num_port = 1; + _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; + _ops.name = strdup(session_name); + _ops.width = cfg_video.width; + _ops.height = cfg_video.height; + _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + _ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + _ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; + _ops.device = ST_PLUGIN_DEVICE_AUTO; + _ops.framebuff_cnt = 4; + + log::info("ProxyContext: %s...", __func__); + log::info("port : %s", _ops.port.port[MTL_PORT_P]); + log::info("dip_addr : %d %d %d %d", _ops.port.dip_addr[MTL_PORT_P][0], + _ops.port.dip_addr[MTL_PORT_P][2], _ops.port.dip_addr[MTL_PORT_P][2], + _ops.port.dip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", _ops.port.num_port); + log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); + log::info("udp_src_port : %d", _ops.port.udp_src_port[MTL_PORT_P]); + log::info("payload_type : %d", _ops.port.payload_type); + log::info("name : %s", _ops.name); + log::info("width : %d", _ops.width); + log::info("height : %d", _ops.height); + log::info("fps : %d", _ops.fps); + log::info("transport_fmt : %d", _ops.transport_fmt); + log::info("input_fmt : %d", _ops.input_fmt); + log::info("device : %d", _ops.device); + log::info("framebuff_cnt : %d", _ops.framebuff_cnt); + + _transfer_size = st_frame_size(_ops.input_fmt, _ops.width, _ops.height, false); + _ops.priv = this; // app handle register to lib + _ops.notify_frame_available = frame_available_cb; + + set_state(ctx, State::configured); + return set_result(Result::success); +} + +} // namespace connection + +} // namespace mesh diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc new file mode 100644 index 000000000..5d8c8eb17 --- /dev/null +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -0,0 +1,91 @@ +#include "st2110rx.h" + +namespace mesh +{ + +namespace connection +{ + +ST2110_22Rx::ST2110_22Rx() +{ + _get_frame_fn = st22p_rx_get_frame; + _put_frame_fn = st22p_rx_put_frame; + _create_session_fn = st22p_rx_create; + _close_session_fn = st22p_rx_free; +} + +ST2110_22Rx::~ST2110_22Rx() {} + +Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, + const MeshConfig_Video &cfg_video) +{ + static int session_id; + static std::mutex mtx; + std::lock_guard lock(mtx); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (_st == nullptr) { + log::error("Failed to get MTL device"); + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + char session_name[NAME_MAX] = ""; + + snprintf(session_name, NAME_MAX, "mcm_rx_st22_%d", session_id++); + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, _ops.port.mcast_sip_addr[MTL_PORT_P]); + _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; + + strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + _ops.port.num_port = 1; + _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; + _ops.name = strdup(session_name); + _ops.width = cfg_video.width; + _ops.height = cfg_video.height; + _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + _ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + _ops.device = ST_PLUGIN_DEVICE_AUTO; + _ops.framebuff_cnt = 4; + _ops.pack_type = ST22_PACK_CODESTREAM; + _ops.codec = ST22_CODEC_JPEGXS; + _ops.codec_thread_cnt = 0; + _ops.max_codestream_size = 0; + + log::info("ProxyContext: %s...", __func__); + log::info("port : %s", _ops.port.port[MTL_PORT_P]); + log::info("ip_addr : %d %d %d %d", _ops.port.ip_addr[MTL_PORT_P][0], + _ops.port.ip_addr[MTL_PORT_P][2], _ops.port.ip_addr[MTL_PORT_P][2], + _ops.port.ip_addr[MTL_PORT_P][3]); + log::info("mcast_sip_addr: %d %d %d %d", _ops.port.mcast_sip_addr[MTL_PORT_P][0], + _ops.port.mcast_sip_addr[MTL_PORT_P][1], _ops.port.mcast_sip_addr[MTL_PORT_P][2], + _ops.port.mcast_sip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", _ops.port.num_port); + log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); + log::info("payload_type : %d", _ops.port.payload_type); + log::info("name : %s", _ops.name); + log::info("width : %d", _ops.width); + log::info("height : %d", _ops.height); + log::info("fps : %d", _ops.fps); + log::info("output_fmt : %d", _ops.output_fmt); + log::info("device : %d", _ops.device); + log::info("framebuff_cnt : %d", _ops.framebuff_cnt); + + _transfer_size = st_frame_size(_ops.output_fmt, _ops.width, _ops.height, false); + _ops.priv = this; // app handle register to lib + _ops.notify_frame_available = frame_available_cb; + + set_state(ctx, State::configured); + return set_result(Result::success); +} + +} // namespace connection + +} // namespace mesh diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc new file mode 100644 index 000000000..560622bb0 --- /dev/null +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -0,0 +1,89 @@ +#include "st2110tx.h" + +namespace mesh +{ + +namespace connection +{ + +ST2110_22Tx::ST2110_22Tx() +{ + _get_frame_fn = st22p_tx_get_frame; + _put_frame_fn = st22p_tx_put_frame; + _create_session_fn = st22p_tx_create; + _close_session_fn = st22p_tx_free; +} + +ST2110_22Tx::~ST2110_22Tx() {} + +Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, + const MeshConfig_Video &cfg_video) +{ + static int session_id; + static std::mutex mtx; + std::lock_guard lock(mtx); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (_st == nullptr) { + log::error("Failed to get MTL device"); + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + char session_name[NAME_MAX] = ""; + + snprintf(session_name, NAME_MAX, "mcm_tx_st22_%d", session_id++); + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.dip_addr[MTL_PORT_P]); + _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; + _ops.port.num_port = 1; + _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; + _ops.name = strdup(session_name); + _ops.width = cfg_video.width; + _ops.height = cfg_video.height; + _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + _ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + _ops.device = ST_PLUGIN_DEVICE_AUTO; + _ops.framebuff_cnt = 4; + _ops.pack_type = ST22_PACK_CODESTREAM; + _ops.codec = ST22_CODEC_JPEGXS; + _ops.quality = ST22_QUALITY_MODE_SPEED; + _ops.codec_thread_cnt = 0; + _ops.codestream_size = _ops.width * _ops.height * 3 / 8; + + log::info("ProxyContext: %s...", __func__); + log::info("port : %s", _ops.port.port[MTL_PORT_P]); + log::info("dip_addr : %d %d %d %d", _ops.port.dip_addr[MTL_PORT_P][0], + _ops.port.dip_addr[MTL_PORT_P][2], _ops.port.dip_addr[MTL_PORT_P][2], + _ops.port.dip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", _ops.port.num_port); + log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); + log::info("udp_src_port : %d", _ops.port.udp_src_port[MTL_PORT_P]); + log::info("payload_type : %d", _ops.port.payload_type); + log::info("name : %s", _ops.name); + log::info("width : %d", _ops.width); + log::info("height : %d", _ops.height); + log::info("fps : %d", _ops.fps); + log::info("input_fmt : %d", _ops.input_fmt); + log::info("device : %d", _ops.device); + log::info("framebuff_cnt : %d", _ops.framebuff_cnt); + + _transfer_size = st_frame_size(_ops.input_fmt, _ops.width, _ops.height, false); + _ops.priv = this; // app handle register to lib + _ops.notify_frame_available = frame_available_cb; + + set_state(ctx, State::configured); + return set_result(Result::success); +} + +} // namespace connection + +} // namespace mesh diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc new file mode 100644 index 000000000..a1d0ea927 --- /dev/null +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -0,0 +1,86 @@ +#include "st2110rx.h" + +namespace mesh +{ + +namespace connection +{ + +ST2110_30Rx::ST2110_30Rx() +{ + _get_frame_fn = st30p_rx_get_frame; + _put_frame_fn = st30p_rx_put_frame; + _create_session_fn = st30p_rx_create; + _close_session_fn = st30p_rx_free; +} + +ST2110_30Rx::~ST2110_30Rx() {} + +Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, + const MeshConfig_Audio &cfg_audio) +{ + static int session_id; + static std::mutex mtx; + std::lock_guard lock(mtx); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (_st == nullptr) { + log::error("Failed to get MTL device"); + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + char session_name[NAME_MAX] = ""; + + snprintf(session_name, NAME_MAX, "mcm_rx_st30_%d", session_id++); + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, _ops.port.mcast_sip_addr[MTL_PORT_P]); + _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; + + strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + _ops.port.num_port = 1; + _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; + _ops.name = strdup(session_name); + _ops.framebuff_cnt = 4; + _ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); + _ops.channel = cfg_audio.channels; + _ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); + _ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); + + log::info("ProxyContext: %s...", __func__); + log::info("port : %s", _ops.port.port[MTL_PORT_P]); + log::info("ip_addr : %d %d %d %d", _ops.port.ip_addr[MTL_PORT_P][0], + _ops.port.ip_addr[MTL_PORT_P][2], _ops.port.ip_addr[MTL_PORT_P][2], + _ops.port.ip_addr[MTL_PORT_P][3]); + log::info("mcast_sip_addr: %d %d %d %d", _ops.port.mcast_sip_addr[MTL_PORT_P][0], + _ops.port.mcast_sip_addr[MTL_PORT_P][1], _ops.port.mcast_sip_addr[MTL_PORT_P][2], + _ops.port.mcast_sip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", _ops.port.num_port); + log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); + log::info("payload_type : %d", _ops.port.payload_type); + log::info("name : %s", _ops.name); + log::info("framebuff_cnt : %d", _ops.framebuff_cnt); + log::info("audio_fmt : %d", _ops.fmt); + log::info("audio_chan : %d", _ops.channel); + log::info("audio_sampl : %d", _ops.sampling); + log::info("audio_ptime : %d", _ops.ptime); + + _ops.framebuff_size = _transfer_size = + st30_get_packet_size(_ops.fmt, _ops.ptime, _ops.sampling, _ops.channel); + _ops.priv = this; // app handle register to lib + _ops.notify_frame_available = frame_available_cb; + + set_state(ctx, State::configured); + return set_result(Result::success); +} + +} // namespace connection + +} // namespace mesh diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc new file mode 100644 index 000000000..7f0f18c07 --- /dev/null +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -0,0 +1,83 @@ +#include "st2110tx.h" + +namespace mesh +{ + +namespace connection +{ + +ST2110_30Tx::ST2110_30Tx() +{ + _get_frame_fn = st30p_tx_get_frame; + _put_frame_fn = st30p_tx_put_frame; + _create_session_fn = st30p_tx_create; + _close_session_fn = st30p_tx_free; +} + +ST2110_30Tx::~ST2110_30Tx() {} + +Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110, + const MeshConfig_Audio &cfg_audio) +{ + static int session_id; + static std::mutex mtx; + std::lock_guard lock(mtx); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (_st == nullptr) { + log::error("Failed to get MTL device"); + set_state(ctx, State::not_configured); + return set_result(Result::error_bad_argument); + } + + char session_name[NAME_MAX] = ""; + + snprintf(session_name, NAME_MAX, "mcm_tx_st30_%d", session_id++); + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.dip_addr[MTL_PORT_P]); + _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; + _ops.port.num_port = 1; + _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; + _ops.name = strdup(session_name); + _ops.framebuff_cnt = 4; + _ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); + _ops.channel = cfg_audio.channels; + _ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); + _ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); + + log::info("ProxyContext: %s...", __func__); + log::info("port : %s", _ops.port.port[MTL_PORT_P]); + log::info("dip_addr : %d %d %d %d", _ops.port.dip_addr[MTL_PORT_P][0], + _ops.port.dip_addr[MTL_PORT_P][2], _ops.port.dip_addr[MTL_PORT_P][2], + _ops.port.dip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", _ops.port.num_port); + log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); + log::info("udp_src_port : %d", _ops.port.udp_src_port[MTL_PORT_P]); + log::info("payload_type : %d", _ops.port.payload_type); + log::info("name : %s", _ops.name); + log::info("framebuff_cnt : %d", _ops.framebuff_cnt); + log::info("audio_fmt : %d", _ops.fmt); + log::info("audio_chan : %d", _ops.channel); + log::info("audio_sampl : %d", _ops.sampling); + log::info("audio_ptime : %d", _ops.ptime); + + _ops.framebuff_size = _transfer_size = + st30_get_packet_size(_ops.fmt, _ops.ptime, _ops.sampling, _ops.channel); + _ops.priv = this; // app handle register to lib + _ops.notify_frame_available = frame_available_cb; + + set_state(ctx, State::configured); + return set_result(Result::success); +} + +} // namespace connection + +} // namespace mesh diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc new file mode 100644 index 000000000..abe10a13f --- /dev/null +++ b/media-proxy/tests/st2110_tests.cc @@ -0,0 +1,565 @@ +#include +#include "mesh/st2110rx.h" +#include "mesh/st2110tx.h" +#include "mesh/conn.h" +using namespace mesh; + +#define DUMMY_DATA1 "DUMMY_DATA1" +#define DUMMY_DATA2 "DUMMY_DATA2" + +struct wrapper { + static uint32_t received_packets_dummy1; + static uint32_t received_packets_dummy2; + + static st_frame *get_frame_dummy(int *h) + { + st_frame *f = new st_frame; + f->addr[0] = calloc(1000, 1); + memcpy(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)); + return f; + } + + static int put_frame_dummy(int *d, st_frame *f) + { + if (memcmp(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { + received_packets_dummy1++; + } else if (memcmp(f->addr[0], DUMMY_DATA2, sizeof(DUMMY_DATA2)) == 0) { + received_packets_dummy2++; + } + + free(f->addr[0]); + delete f; + return 0; + } + + static int *create_dummy(mtl_handle, int *o) { return (int *)malloc(1); } + + static int close_dummy(int *h) + { + free(h); + return 0; + } +}; +uint32_t wrapper::received_packets_dummy1; +uint32_t wrapper::received_packets_dummy2; + +class EmulatedTransmitter : public connection::Connection +{ + public: + EmulatedTransmitter(context::Context &ctx) + { + _kind = connection::Kind::transmitter; + set_state(ctx, connection::State::configured); + } + + connection::Result on_establish(context::Context &ctx) + { + set_state(ctx, connection::State::active); + return connection::Result::success; + } + + connection::Result on_shutdown(context::Context &ctx) { return connection::Result::success; } + + connection::Result transmit_wrapper(context::Context &ctx, void *ptr, uint32_t sz) + { + return transmit(ctx, ptr, sz); + } +}; + +class EmulatedReceiver : public connection::Connection +{ + public: + uint32_t received_packets_lossless; + uint32_t received_packets_lossy; + EmulatedReceiver(context::Context &ctx) + { + _kind = connection::Kind::receiver; + received_packets_lossless = 0; + received_packets_lossy = 0; + set_state(ctx, connection::State::configured); + } + + connection::Result on_establish(context::Context &ctx) + { + set_state(ctx, connection::State::active); + return connection::Result::success; + } + + connection::Result on_shutdown(context::Context &ctx) { return connection::Result::success; } + + connection::Result on_receive(context::Context &ctx, void *ptr, uint32_t sz, uint32_t &sent) + { + if (memcmp(ptr, DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { + received_packets_lossless++; + } else { + received_packets_lossy++; + } + return connection::Result::success; + } +}; + +class EmulatedST2110_Tx : public connection::ST2110Tx +{ + public: + EmulatedST2110_Tx() + { + _get_frame_fn = wrapper::get_frame_dummy; + _put_frame_fn = wrapper::put_frame_dummy; + _create_session_fn = wrapper::create_dummy; + _close_session_fn = wrapper::close_dummy; + _transfer_size = 10000; + }; + ~EmulatedST2110_Tx(){}; + + connection::Result configure(context::Context &ctx) + { + set_state(ctx, connection::State::configured); + return connection::Result::success; + } +}; + +class EmulatedST2110_Rx : public connection::ST2110Rx +{ + public: + EmulatedST2110_Rx() + { + _get_frame_fn = wrapper::get_frame_dummy; + _put_frame_fn = wrapper::put_frame_dummy; + _create_session_fn = wrapper::create_dummy; + _close_session_fn = wrapper::close_dummy; + _transfer_size = 10000; + }; + ~EmulatedST2110_Rx(){}; + + connection::Result configure(context::Context &ctx) + { + set_state(ctx, connection::State::configured); + return connection::Result::success; + } +}; + +static void validate_state_change(context::Context &ctx, connection::Connection *c) +{ + connection::Result res; + + // Change state Configured -> Active + res = c->establish(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(c->state(), connection::State::active); + + // Change state Active -> Suspended + res = c->suspend(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(c->state(), connection::State::suspended); + + // Change state Suspended -> Active + res = c->resume(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(c->state(), connection::State::active); + + // Change state Active -> Closed + res = c->shutdown(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(c->state(), connection::State::closed); +} + +static void get_ST2110_session_config(MeshConfig_ST2110 &cfg_st2110, int transport) +{ + memcpy(cfg_st2110.local_ip_addr, "127.0.0.1", sizeof("127.0.0.1")); + memcpy(cfg_st2110.remote_ip_addr, "127.0.0.1", sizeof("127.0.0.1")); + cfg_st2110.local_port = 9001; + cfg_st2110.remote_port = 9001; + cfg_st2110.transport = transport; +} + +static void get_ST2110_video_cfg(MeshConfig_Video &cfg_video) +{ + cfg_video.fps = 30; + cfg_video.width = 1920; + cfg_video.height = 1080; + cfg_video.pixel_format = MESH_VIDEO_PIXEL_FORMAT_YUV422P10LE; +} + +static void get_ST2110_audio_cfg(MeshConfig_Audio &cfg_audio) +{ + cfg_audio.channels = 2; + cfg_audio.format = MESH_AUDIO_FORMAT_PCM_S16BE; + cfg_audio.packet_time = MESH_AUDIO_PACKET_TIME_1MS; + cfg_audio.sample_rate = MESH_AUDIO_SAMPLE_RATE_48000; +} + +TEST(st2110_tx, state_change) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + + auto conn_tx = new EmulatedST2110_Tx; + ASSERT_EQ(conn_tx->kind(), connection::Kind::transmitter); + ASSERT_EQ(conn_tx->state(), connection::State::not_configured); + + // Change state: Not Configured -> Configured + connection::Result res = conn_tx->configure(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::configured); + + validate_state_change(ctx, dynamic_cast(conn_tx)); + + delete conn_tx; +} + +TEST(st2110_rx, state_change) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + + auto conn_rx = new EmulatedST2110_Rx; + ASSERT_EQ(conn_rx->kind(), connection::Kind::receiver); + ASSERT_EQ(conn_rx->state(), connection::State::not_configured); + + // Change state: Not Configured -> Configured + connection::Result res = conn_rx->configure(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + validate_state_change(ctx, dynamic_cast(conn_rx)); + + delete conn_rx; +} + +TEST(st2110_20tx, state_change) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + + auto conn_rx = new connection::ST2110_20Tx; + ASSERT_EQ(conn_rx->kind(), connection::Kind::transmitter); + ASSERT_EQ(conn_rx->state(), connection::State::not_configured); + + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_20); + + MeshConfig_Video cfg_video; + get_ST2110_video_cfg(cfg_video); + + // Change state: Not Configured -> Configured + std::string dev_port("kernel:lo"); + connection::Result res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + validate_state_change(ctx, dynamic_cast(conn_rx)); + validate_state_change(ctx, dynamic_cast(conn_rx)); + + delete conn_rx; +} + +TEST(st2110_22tx, state_change) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + + auto conn_rx = new connection::ST2110_22Tx; + ASSERT_EQ(conn_rx->kind(), connection::Kind::transmitter); + ASSERT_EQ(conn_rx->state(), connection::State::not_configured); + + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_22); + + MeshConfig_Video cfg_video; + get_ST2110_video_cfg(cfg_video); + + // Change state: Not Configured -> Configured + std::string dev_port("kernel:lo"); + connection::Result res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + validate_state_change(ctx, dynamic_cast(conn_rx)); + validate_state_change(ctx, dynamic_cast(conn_rx)); + + delete conn_rx; +} + +TEST(st2110_30tx, state_change) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + + auto conn_rx = new connection::ST2110_30Tx; + ASSERT_EQ(conn_rx->kind(), connection::Kind::transmitter); + ASSERT_EQ(conn_rx->state(), connection::State::not_configured); + + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_30); + + MeshConfig_Audio cfg_audio; + get_ST2110_audio_cfg(cfg_audio); + + // Change state: Not Configured -> Configured + std::string dev_port("kernel:lo"); + connection::Result res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_audio); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + validate_state_change(ctx, dynamic_cast(conn_rx)); + + // Change state: Closed -> Configured + res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_audio); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + validate_state_change(ctx, dynamic_cast(conn_rx)); + + delete conn_rx; +} + +TEST(st2110_tx, send_data) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + connection::Result res; + + auto conn_tx = new EmulatedST2110_Tx; + auto emulated_tx = new EmulatedTransmitter(ctx); + + // Setup Tx connection + res = conn_tx->configure(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::configured); + + // Change state Configured -> Active + res = conn_tx->establish(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::active); + + // Setup Emulated Transmitter + emulated_tx->establish(ctx); + + // Connect Emulated Transmitter to Tx connection + emulated_tx->set_link(ctx, conn_tx); + + for (int i = 0; i < 5; i++) { + res = emulated_tx->transmit_wrapper(ctx, (void *)DUMMY_DATA2, sizeof(DUMMY_DATA2)); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::active); + ASSERT_GT(wrapper::received_packets_dummy2, 0); + } + + // Shutdown Tx connection + res = conn_tx->shutdown(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::closed); + + // Destroy resources + delete emulated_tx; + delete conn_tx; +} + +TEST(st2110_rx, get_data) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + connection::Result res; + + // Setup Emulated Receiver + auto emulated_rx = new EmulatedReceiver(ctx); + emulated_rx->establish(ctx); + + // Setup Rx connection + auto conn_rx = new EmulatedST2110_Rx; + + res = conn_rx->configure(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + res = conn_rx->establish(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::active); + + // Connect Rx connection to Emulated Receiver + conn_rx->set_link(ctx, emulated_rx); + + // Sleep some sufficient time to allow receiving the data from transmitter + mesh::thread::Sleep(ctx, std::chrono::milliseconds(100)); + + // Shutdown Rx connection + res = conn_rx->shutdown(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::closed); + + ASSERT_GT(wrapper::received_packets_dummy1, 0); + // Destroy resources + delete conn_rx; + delete emulated_rx; +} + +/************************** */ +static void tx_thread(context::Context &ctx, connection::Connection *conn_tx) +{ + auto emulated_tx = new EmulatedTransmitter(ctx); + // Change state Configured -> Active + connection::Result res = conn_tx->establish(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::active); + + // Setup Emulated Transmitter + emulated_tx->establish(ctx); + + // Connect Emulated Transmitter to Tx connection + emulated_tx->set_link(ctx, conn_tx); + + for (int i = 0; i < 50; i++) { + res = emulated_tx->transmit_wrapper(ctx, (void *)DUMMY_DATA1, sizeof(DUMMY_DATA1)); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::active); + } + + // Shutdown Tx connection + res = conn_tx->shutdown(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::closed); + + // Destroy resources + delete emulated_tx; +} + +static void rx_thread(context::Context &ctx, connection::Connection *conn_rx, bool is_lossless) +{ + auto emulated_rx = new EmulatedReceiver(ctx); + emulated_rx->establish(ctx); + + conn_rx->set_link(ctx, emulated_rx); + // Connect Rx connection to Emulated Receiver + connection::Result res = conn_rx->establish(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::active); + + // Sleep some sufficient time to allow receiving the data from transmitter + mesh::thread::Sleep(ctx, std::chrono::milliseconds(500)); + + // Shutdown Rx connection + res = conn_rx->shutdown(ctx); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::closed); + + if (is_lossless) { + ASSERT_GT(emulated_rx->received_packets_lossless, 0); + ASSERT_EQ(emulated_rx->received_packets_lossy, 0); + } else { + ASSERT_GT(emulated_rx->received_packets_lossy, 0); + ASSERT_EQ(emulated_rx->received_packets_lossless, 0); + } + + delete emulated_rx; +} + +TEST(DISABLED_st2110_20, send_and_receive_data) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + connection::Result res; + + // Setup Tx connection + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_20); + + MeshConfig_Video cfg_video; + get_ST2110_video_cfg(cfg_video); + + // Configure Tx + std::string dev_port("kernel:lo"); + auto conn_tx = new connection::ST2110_20Tx; + res = conn_tx->configure(ctx, dev_port, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::configured); + + // Configure Rx + auto conn_rx = new connection::ST2110_20Rx; + res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + std::jthread rx_th, tx_th; + try { + rx_th = std::jthread(&rx_thread, std::ref(ctx), conn_rx, 1); + tx_th = std::jthread(&tx_thread, std::ref(ctx), conn_tx); + } catch (const std::system_error &e) { + ASSERT_TRUE(0) << e.what(); + } + + rx_th.join(); + tx_th.join(); + + delete conn_tx; + delete conn_rx; +} + +TEST(DISABLED_st2110_22, send_and_receive_data) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + connection::Result res; + + // Setup Tx connection + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_22); + + MeshConfig_Video cfg_video; + get_ST2110_video_cfg(cfg_video); + + // Configure Tx + std::string dev_port("kernel:lo"); + auto conn_tx = new connection::ST2110_22Tx; + res = conn_tx->configure(ctx, dev_port, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::configured); + + // Configure Rx + auto conn_rx = new connection::ST2110_22Rx; + res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + std::jthread rx_th, tx_th; + try { + rx_th = std::jthread(&rx_thread, std::ref(ctx), conn_rx, 0); + tx_th = std::jthread(&tx_thread, std::ref(ctx), conn_tx); + } catch (const std::system_error &e) { + ASSERT_TRUE(0) << e.what(); + } + + rx_th.join(); + tx_th.join(); + + delete conn_tx; + delete conn_rx; +} + +TEST(DISABLED_st2110_30, send_and_receive_data) +{ + auto ctx = context::WithCancel(mesh::context::Background()); + connection::Result res; + + // Setup Tx connection + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_30); + + MeshConfig_Audio cfg_audio; + get_ST2110_audio_cfg(cfg_audio); + + // Configure Tx + std::string dev_port("kernel:lo"); + auto conn_tx = new connection::ST2110_30Tx; + res = conn_tx->configure(ctx, dev_port, cfg_st2110, cfg_audio); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::configured); + + // Configure Rx + auto conn_rx = new connection::ST2110_30Rx; + res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_audio); + ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + std::jthread rx_th, tx_th; + try { + rx_th = std::jthread(&rx_thread, std::ref(ctx), conn_rx, 1); + tx_th = std::jthread(&tx_thread, std::ref(ctx), conn_tx); + } catch (const std::system_error &e) { + ASSERT_TRUE(0) << e.what(); + } + + rx_th.join(); + tx_th.join(); + + delete conn_tx; + delete conn_rx; +} From 030c4ef1d6b4d164d86916d4edc7091d1e118445 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Thu, 21 Nov 2024 09:05:26 +0000 Subject: [PATCH 03/27] Adjust clang-format and apply it to st2110 files Signed-off-by: Tomasz Szumski --- .clang-format | 6 +++++- media-proxy/include/mesh/st2110.h | 13 +++---------- media-proxy/include/mesh/st2110rx.h | 22 ++++++---------------- media-proxy/include/mesh/st2110tx.h | 22 ++++++---------------- media-proxy/src/mesh/st2110.cc | 10 ++-------- media-proxy/src/mesh/st2110_20rx.cc | 10 ++-------- media-proxy/src/mesh/st2110_20tx.cc | 10 ++-------- media-proxy/src/mesh/st2110_22rx.cc | 10 ++-------- media-proxy/src/mesh/st2110_22tx.cc | 10 ++-------- media-proxy/src/mesh/st2110_30rx.cc | 10 ++-------- media-proxy/src/mesh/st2110_30tx.cc | 10 ++-------- media-proxy/tests/st2110_tests.cc | 12 ++++-------- 12 files changed, 38 insertions(+), 107 deletions(-) diff --git a/.clang-format b/.clang-format index 331acefc6..46a72ccbf 100644 --- a/.clang-format +++ b/.clang-format @@ -3,7 +3,11 @@ BasedOnStyle: LLVM IndentWidth: 4 UseTab: Never ColumnLimit: 100 -BreakBeforeBraces: Linux +BreakBeforeBraces: Custom +BraceWrapping: + AfterClass: false + AfterNamespace: false + AfterFunction: true AllowShortIfStatementsOnASingleLine: false AllowShortEnumsOnASingleLine: false IndentCaseLabels: false diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index b65342d3d..3da6d64d7 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -11,11 +11,7 @@ #include "mesh_dp.h" #include "logger.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { #define ST_APP_PAYLOAD_TYPE_ST30 (111) #define ST_APP_PAYLOAD_TYPE_ST20 (112) @@ -27,8 +23,7 @@ namespace connection * Base abstract class of ST2110. ST2110Rx/ST2110Tx * inherit this class. */ -class ST2110 : public Connection -{ +class ST2110 : public Connection { public: static st_frame_fmt mesh_video_format_to_st_format(int fmt); static st30_fmt mesh_audio_format_to_st_format(int fmt); @@ -56,8 +51,6 @@ class ST2110 : public Connection private: }; -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection #endif // ST2110_H diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 23a37399a..f561defad 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -3,11 +3,7 @@ #include "st2110.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { /** * ST2110Rx @@ -15,8 +11,7 @@ namespace connection * Base abstract class of ST2110Rx. ST2110_20Rx/ST2110_22Rx/ST2110_30Rx * inherit this class. */ -template class ST2110Rx : public ST2110 -{ +template class ST2110Rx : public ST2110 { public: ST2110Rx() { @@ -105,8 +100,7 @@ template class ST2110Rx : public } }; -class ST2110_20Rx : public ST2110Rx -{ +class ST2110_20Rx : public ST2110Rx { public: ST2110_20Rx(); ~ST2110_20Rx(); @@ -117,8 +111,7 @@ class ST2110_20Rx : public ST2110Rx private: }; -class ST2110_22Rx : public ST2110Rx -{ +class ST2110_22Rx : public ST2110Rx { public: ST2110_22Rx(); ~ST2110_22Rx(); @@ -129,8 +122,7 @@ class ST2110_22Rx : public ST2110Rx private: }; -class ST2110_30Rx : public ST2110Rx -{ +class ST2110_30Rx : public ST2110Rx { public: ST2110_30Rx(); ~ST2110_30Rx(); @@ -141,8 +133,6 @@ class ST2110_30Rx : public ST2110Rx private: }; -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection #endif // ST2110RX_H diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 6cd457476..070e49285 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -4,11 +4,7 @@ #include "st2110.h" #include -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { /** * ST2110 @@ -16,8 +12,7 @@ namespace connection * Base abstract class of ST2110. ST2110_20Tx/ST2110_22Tx/ST2110_30Tx * inherit this class. */ -template class ST2110Tx : public ST2110 -{ +template class ST2110Tx : public ST2110 { public: ST2110Tx() { @@ -103,8 +98,7 @@ template class ST2110Tx : public private: }; -class ST2110_20Tx : public ST2110Tx -{ +class ST2110_20Tx : public ST2110Tx { public: ST2110_20Tx(); ~ST2110_20Tx(); @@ -115,8 +109,7 @@ class ST2110_20Tx : public ST2110Tx private: }; -class ST2110_22Tx : public ST2110Tx -{ +class ST2110_22Tx : public ST2110Tx { public: ST2110_22Tx(); ~ST2110_22Tx(); @@ -127,8 +120,7 @@ class ST2110_22Tx : public ST2110Tx private: }; -class ST2110_30Tx : public ST2110Tx -{ +class ST2110_30Tx : public ST2110Tx { public: ST2110_30Tx(); ~ST2110_30Tx(); @@ -139,8 +131,6 @@ class ST2110_30Tx : public ST2110Tx private: }; -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection #endif // ST2110TX_H diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index 3795dbb37..cf648cc3f 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -1,10 +1,6 @@ #include "st2110.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { st_frame_fmt ST2110::mesh_video_format_to_st_format(int fmt) { @@ -154,6 +150,4 @@ mtl_handle ST2110::get_mtl_handle(const std::string &dev_port, mtl_log_level log return dev_handle; } -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index a83aad6fe..b6f7f3b74 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -1,10 +1,6 @@ #include "st2110rx.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { ST2110_20Rx::ST2110_20Rx() { @@ -84,6 +80,4 @@ Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::success); } -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index c1b944b9c..7092e70d7 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -1,10 +1,6 @@ #include "st2110tx.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { ST2110_20Tx::ST2110_20Tx() { @@ -81,6 +77,4 @@ Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::success); } -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index 5d8c8eb17..50baccbe2 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -1,10 +1,6 @@ #include "st2110rx.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { ST2110_22Rx::ST2110_22Rx() { @@ -86,6 +82,4 @@ Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::success); } -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index 560622bb0..aa56bd86a 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -1,10 +1,6 @@ #include "st2110tx.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { ST2110_22Tx::ST2110_22Tx() { @@ -84,6 +80,4 @@ Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::success); } -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index a1d0ea927..496ae4fb3 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -1,10 +1,6 @@ #include "st2110rx.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { ST2110_30Rx::ST2110_30Rx() { @@ -81,6 +77,4 @@ Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::success); } -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index 7f0f18c07..21f109a35 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -1,10 +1,6 @@ #include "st2110tx.h" -namespace mesh -{ - -namespace connection -{ +namespace mesh::connection { ST2110_30Tx::ST2110_30Tx() { @@ -78,6 +74,4 @@ Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::success); } -} // namespace connection - -} // namespace mesh +} // namespace mesh::connection diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index abe10a13f..c316ba8a7 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -43,8 +43,7 @@ struct wrapper { uint32_t wrapper::received_packets_dummy1; uint32_t wrapper::received_packets_dummy2; -class EmulatedTransmitter : public connection::Connection -{ +class EmulatedTransmitter : public connection::Connection { public: EmulatedTransmitter(context::Context &ctx) { @@ -66,8 +65,7 @@ class EmulatedTransmitter : public connection::Connection } }; -class EmulatedReceiver : public connection::Connection -{ +class EmulatedReceiver : public connection::Connection { public: uint32_t received_packets_lossless; uint32_t received_packets_lossy; @@ -98,8 +96,7 @@ class EmulatedReceiver : public connection::Connection } }; -class EmulatedST2110_Tx : public connection::ST2110Tx -{ +class EmulatedST2110_Tx : public connection::ST2110Tx { public: EmulatedST2110_Tx() { @@ -118,8 +115,7 @@ class EmulatedST2110_Tx : public connection::ST2110Tx } }; -class EmulatedST2110_Rx : public connection::ST2110Rx -{ +class EmulatedST2110_Rx : public connection::ST2110Rx { public: EmulatedST2110_Rx() { From 8f3aa3931de415f15c31529b9b9399fa077c27e7 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Thu, 21 Nov 2024 10:17:38 +0000 Subject: [PATCH 04/27] Call shutdown() in destructor --- media-proxy/include/mesh/st2110rx.h | 2 +- media-proxy/include/mesh/st2110tx.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index f561defad..5f6802739 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -21,7 +21,7 @@ template class ST2110Rx : public _ops = {0}; _transfer_size = 0; } - ~ST2110Rx() {} + ~ST2110Rx() { shutdown(_ctx); } protected: HANDLE _handle; diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 070e49285..c4282d584 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -22,7 +22,7 @@ template class ST2110Tx : public _ops = {0}; _transfer_size = 0; }; - ~ST2110Tx(){}; + ~ST2110Tx() { shutdown(_ctx); }; protected: HANDLE _handle; From ae8e31d14c9df6d56af7f2aee82898e38baa29d8 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Fri, 22 Nov 2024 08:03:32 +0000 Subject: [PATCH 05/27] CI/CD quick fix. Disable MTL tests as hugepages are not set --- media-proxy/tests/st2110_tests.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index c316ba8a7..fe4be3d9b 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -220,7 +220,7 @@ TEST(st2110_rx, state_change) delete conn_rx; } -TEST(st2110_20tx, state_change) +TEST(DISABLED_st2110_20tx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); @@ -246,7 +246,7 @@ TEST(st2110_20tx, state_change) delete conn_rx; } -TEST(st2110_22tx, state_change) +TEST(DISABLED_st2110_22tx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); @@ -272,7 +272,7 @@ TEST(st2110_22tx, state_change) delete conn_rx; } -TEST(st2110_30tx, state_change) +TEST(DISABLED_st2110_30tx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); From 2954106d2c5a3dbb38411b5051f68c210f39f99b Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Fri, 22 Nov 2024 09:54:13 +0000 Subject: [PATCH 06/27] Replace std::function, with virtual fn in ST2110 derrived classes --- media-proxy/include/mesh/st2110rx.h | 34 ++++++-- media-proxy/include/mesh/st2110tx.h | 34 ++++++-- media-proxy/src/mesh/st2110_20rx.cc | 19 +++-- media-proxy/src/mesh/st2110_20tx.cc | 19 +++-- media-proxy/src/mesh/st2110_22rx.cc | 19 +++-- media-proxy/src/mesh/st2110_22tx.cc | 19 +++-- media-proxy/src/mesh/st2110_30rx.cc | 19 +++-- media-proxy/src/mesh/st2110_30tx.cc | 19 +++-- media-proxy/tests/st2110_tests.cc | 119 +++++++++++++++++----------- 9 files changed, 195 insertions(+), 106 deletions(-) diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 5f6802739..59ea5fefc 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -30,17 +30,17 @@ template class ST2110Rx : public std::jthread _frame_thread_handle; context::Context _ctx; - std::function _get_frame_fn; - std::function _put_frame_fn; - std::function _create_session_fn; - std::function _close_session_fn; + virtual FRAME *get_frame(HANDLE) = 0; + virtual int put_frame(HANDLE, FRAME *) = 0; + virtual HANDLE create_session(mtl_handle, OPS *) = 0; + virtual int close_session(HANDLE) = 0; Result on_establish(context::Context &ctx) override { _ctx = context::WithCancel(ctx); _stop = false; - _handle = _create_session_fn(_st, &_ops); + _handle = create_session(_st, &_ops); if (!_handle) { log::error("Failed to create session"); set_state(ctx, State::closed); @@ -67,7 +67,7 @@ template class ST2110Rx : public _frame_thread_handle.join(); if (_handle) { - _close_session_fn(_handle); + close_session(_handle); _handle = nullptr; } set_state(ctx, State::closed); @@ -81,7 +81,7 @@ template class ST2110Rx : public { while (!_ctx.cancelled()) { // Get full buffer from MTL - FRAME *frame_ptr = _get_frame_fn(_handle); + FRAME *frame_ptr = get_frame(_handle); if (!frame_ptr) { /* no frame */ std::mutex mx; std::unique_lock lk(mx); @@ -95,7 +95,7 @@ template class ST2110Rx : public // Forward buffer to emulated receiver transmit(_ctx, get_frame_data_ptr(frame_ptr), _transfer_size); // Return used buffer to MTL - _put_frame_fn(_handle, frame_ptr); + put_frame(_handle, frame_ptr); } } }; @@ -108,6 +108,12 @@ class ST2110_20Rx : public ST2110Rx { Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + protected: + st_frame *get_frame(st20p_rx_handle h) override; + int put_frame(st20p_rx_handle h, st_frame *f) override; + st20p_rx_handle create_session(mtl_handle h, st20p_rx_ops *o) override; + int close_session(st20p_rx_handle h) override; + private: }; @@ -119,6 +125,12 @@ class ST2110_22Rx : public ST2110Rx { Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + protected: + st_frame *get_frame(st22p_rx_handle h) override; + int put_frame(st22p_rx_handle h, st_frame *f) override; + st22p_rx_handle create_session(mtl_handle h, st22p_rx_ops *o) override; + int close_session(st22p_rx_handle h) override; + private: }; @@ -130,6 +142,12 @@ class ST2110_30Rx : public ST2110Rx { Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); + protected: + st30_frame *get_frame(st30p_rx_handle h) override; + int put_frame(st30p_rx_handle h, st30_frame *f) override; + st30p_rx_handle create_session(mtl_handle h, st30p_rx_ops *o) override; + int close_session(st30p_rx_handle h) override; + private: }; diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index c4282d584..e818038e4 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -30,17 +30,17 @@ template class ST2110Tx : public uint32_t _transfer_size; context::Context _ctx; - std::function _get_frame_fn; - std::function _put_frame_fn; - std::function _create_session_fn; - std::function _close_session_fn; + virtual FRAME *get_frame(HANDLE) = 0; + virtual int put_frame(HANDLE, FRAME *) = 0; + virtual HANDLE create_session(mtl_handle, OPS *) = 0; + virtual int close_session(HANDLE) = 0; Result on_establish(context::Context &ctx) override { _ctx = context::WithCancel(ctx); _stop = false; - _handle = _create_session_fn(_st, &_ops); + _handle = create_session(_st, &_ops); if (!_handle) { log::error("Failed to create session"); set_state(ctx, State::closed); @@ -56,7 +56,7 @@ template class ST2110Tx : public _ctx.cancel(); if (_handle) { - _close_session_fn(_handle); + close_session(_handle); _handle = nullptr; } set_state(ctx, State::closed); @@ -71,7 +71,7 @@ template class ST2110Tx : public FRAME *frame = NULL; do { // Get empty buffer from MTL - frame = _get_frame_fn(_handle); + frame = get_frame(_handle); if (!frame) { std::mutex mx; std::unique_lock lk(mx); @@ -87,7 +87,7 @@ template class ST2110Tx : public // Copy data from emulated transmitter to MTL empty buffer mtl_memcpy(get_frame_data_ptr(frame), ptr, sent); // Return full buffer to MTL - _put_frame_fn(_handle, frame); + put_frame(_handle, frame); } else { sent = 0; return set_result(Result::error_shutdown); @@ -106,6 +106,12 @@ class ST2110_20Tx : public ST2110Tx { Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + protected: + st_frame *get_frame(st20p_tx_handle h) override; + int put_frame(st20p_tx_handle h, st_frame *f) override; + st20p_tx_handle create_session(mtl_handle h, st20p_tx_ops *o) override; + int close_session(st20p_tx_handle h) override; + private: }; @@ -117,6 +123,12 @@ class ST2110_22Tx : public ST2110Tx { Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + protected: + st_frame *get_frame(st22p_tx_handle h) override; + int put_frame(st22p_tx_handle h, st_frame *f) override; + st22p_tx_handle create_session(mtl_handle h, st22p_tx_ops *o) override; + int close_session(st22p_tx_handle h) override; + private: }; @@ -128,6 +140,12 @@ class ST2110_30Tx : public ST2110Tx { Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); + protected: + st30_frame *get_frame(st30p_tx_handle h) override; + int put_frame(st30p_tx_handle h, st30_frame *f) override; + st30p_tx_handle create_session(mtl_handle h, st30p_tx_ops *o) override; + int close_session(st30p_tx_handle h) override; + private: }; diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index b6f7f3b74..247c002ce 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -2,16 +2,21 @@ namespace mesh::connection { -ST2110_20Rx::ST2110_20Rx() -{ - _get_frame_fn = st20p_rx_get_frame; - _put_frame_fn = st20p_rx_put_frame; - _create_session_fn = st20p_rx_create; - _close_session_fn = st20p_rx_free; -} +ST2110_20Rx::ST2110_20Rx() {} ST2110_20Rx::~ST2110_20Rx() {} +st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) { return st20p_rx_get_frame(h); }; + +int ST2110_20Rx::put_frame(st20p_rx_handle h, st_frame *f) { return st20p_rx_put_frame(h, f); }; + +st20p_rx_handle ST2110_20Rx::create_session(mtl_handle h, st20p_rx_ops *o) +{ + return st20p_rx_create(h, o); +}; + +int ST2110_20Rx::close_session(st20p_rx_handle h) { return st20p_rx_free(h); }; + Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index 7092e70d7..a7f0f19f1 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -2,16 +2,21 @@ namespace mesh::connection { -ST2110_20Tx::ST2110_20Tx() -{ - _get_frame_fn = st20p_tx_get_frame; - _put_frame_fn = st20p_tx_put_frame; - _create_session_fn = st20p_tx_create; - _close_session_fn = st20p_tx_free; -} +ST2110_20Tx::ST2110_20Tx() {} ST2110_20Tx::~ST2110_20Tx() {} +st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) { return st20p_tx_get_frame(h); }; + +int ST2110_20Tx::put_frame(st20p_tx_handle h, st_frame *f) { return st20p_tx_put_frame(h, f); }; + +st20p_tx_handle ST2110_20Tx::create_session(mtl_handle h, st20p_tx_ops *o) +{ + return st20p_tx_create(h, o); +}; + +int ST2110_20Tx::close_session(st20p_tx_handle h) { return st20p_tx_free(h); }; + Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index 50baccbe2..a2d82656f 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -2,16 +2,21 @@ namespace mesh::connection { -ST2110_22Rx::ST2110_22Rx() -{ - _get_frame_fn = st22p_rx_get_frame; - _put_frame_fn = st22p_rx_put_frame; - _create_session_fn = st22p_rx_create; - _close_session_fn = st22p_rx_free; -} +ST2110_22Rx::ST2110_22Rx() {} ST2110_22Rx::~ST2110_22Rx() {} +st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) { return st22p_rx_get_frame(h); }; + +int ST2110_22Rx::put_frame(st22p_rx_handle h, st_frame *f) { return st22p_rx_put_frame(h, f); }; + +st22p_rx_handle ST2110_22Rx::create_session(mtl_handle h, st22p_rx_ops *o) +{ + return st22p_rx_create(h, o); +}; + +int ST2110_22Rx::close_session(st22p_rx_handle h) { return st22p_rx_free(h); }; + Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index aa56bd86a..79a9a424a 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -2,16 +2,21 @@ namespace mesh::connection { -ST2110_22Tx::ST2110_22Tx() -{ - _get_frame_fn = st22p_tx_get_frame; - _put_frame_fn = st22p_tx_put_frame; - _create_session_fn = st22p_tx_create; - _close_session_fn = st22p_tx_free; -} +ST2110_22Tx::ST2110_22Tx() {} ST2110_22Tx::~ST2110_22Tx() {} +st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) { return st22p_tx_get_frame(h); }; + +int ST2110_22Tx::put_frame(st22p_tx_handle h, st_frame *f) { return st22p_tx_put_frame(h, f); }; + +st22p_tx_handle ST2110_22Tx::create_session(mtl_handle h, st22p_tx_ops *o) +{ + return st22p_tx_create(h, o); +}; + +int ST2110_22Tx::close_session(st22p_tx_handle h) { return st22p_tx_free(h); }; + Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 496ae4fb3..474d18e88 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -2,16 +2,21 @@ namespace mesh::connection { -ST2110_30Rx::ST2110_30Rx() -{ - _get_frame_fn = st30p_rx_get_frame; - _put_frame_fn = st30p_rx_put_frame; - _create_session_fn = st30p_rx_create; - _close_session_fn = st30p_rx_free; -} +ST2110_30Rx::ST2110_30Rx() {} ST2110_30Rx::~ST2110_30Rx() {} +st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) { return st30p_rx_get_frame(h); }; + +int ST2110_30Rx::put_frame(st30p_rx_handle h, st30_frame *f) { return st30p_rx_put_frame(h, f); }; + +st30p_rx_handle ST2110_30Rx::create_session(mtl_handle h, st30p_rx_ops *o) +{ + return st30p_rx_create(h, o); +}; + +int ST2110_30Rx::close_session(st30p_rx_handle h) { return st30p_rx_free(h); }; + Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio) diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index 21f109a35..e7cadc0ee 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -2,16 +2,21 @@ namespace mesh::connection { -ST2110_30Tx::ST2110_30Tx() -{ - _get_frame_fn = st30p_tx_get_frame; - _put_frame_fn = st30p_tx_put_frame; - _create_session_fn = st30p_tx_create; - _close_session_fn = st30p_tx_free; -} +ST2110_30Tx::ST2110_30Tx() {} ST2110_30Tx::~ST2110_30Tx() {} +st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) { return st30p_tx_get_frame(h); }; + +int ST2110_30Tx::put_frame(st30p_tx_handle h, st30_frame *f) { return st30p_tx_put_frame(h, f); }; + +st30p_tx_handle ST2110_30Tx::create_session(mtl_handle h, st30p_tx_ops *o) +{ + return st30p_tx_create(h, o); +}; + +int ST2110_30Tx::close_session(st30p_tx_handle h) { return st30p_tx_free(h); }; + Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio) diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index fe4be3d9b..cba426555 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -7,42 +7,6 @@ using namespace mesh; #define DUMMY_DATA1 "DUMMY_DATA1" #define DUMMY_DATA2 "DUMMY_DATA2" -struct wrapper { - static uint32_t received_packets_dummy1; - static uint32_t received_packets_dummy2; - - static st_frame *get_frame_dummy(int *h) - { - st_frame *f = new st_frame; - f->addr[0] = calloc(1000, 1); - memcpy(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)); - return f; - } - - static int put_frame_dummy(int *d, st_frame *f) - { - if (memcmp(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { - received_packets_dummy1++; - } else if (memcmp(f->addr[0], DUMMY_DATA2, sizeof(DUMMY_DATA2)) == 0) { - received_packets_dummy2++; - } - - free(f->addr[0]); - delete f; - return 0; - } - - static int *create_dummy(mtl_handle, int *o) { return (int *)malloc(1); } - - static int close_dummy(int *h) - { - free(h); - return 0; - } -}; -uint32_t wrapper::received_packets_dummy1; -uint32_t wrapper::received_packets_dummy2; - class EmulatedTransmitter : public connection::Connection { public: EmulatedTransmitter(context::Context &ctx) @@ -98,40 +62,99 @@ class EmulatedReceiver : public connection::Connection { class EmulatedST2110_Tx : public connection::ST2110Tx { public: + uint32_t received_packets_dummy1; + uint32_t received_packets_dummy2; + EmulatedST2110_Tx() { - _get_frame_fn = wrapper::get_frame_dummy; - _put_frame_fn = wrapper::put_frame_dummy; - _create_session_fn = wrapper::create_dummy; - _close_session_fn = wrapper::close_dummy; + received_packets_dummy1 = 0; + received_packets_dummy2 = 0; _transfer_size = 10000; }; - ~EmulatedST2110_Tx(){}; + ~EmulatedST2110_Tx() {}; connection::Result configure(context::Context &ctx) { set_state(ctx, connection::State::configured); return connection::Result::success; } + + st_frame *get_frame(int *h) override + { + st_frame *f = new st_frame; + f->addr[0] = calloc(1000, 1); + memcpy(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)); + return f; + } + + int put_frame(int *d, st_frame *f) override + { + if (memcmp(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { + received_packets_dummy1++; + } else if (memcmp(f->addr[0], DUMMY_DATA2, sizeof(DUMMY_DATA2)) == 0) { + received_packets_dummy2++; + } + + free(f->addr[0]); + delete f; + return 0; + } + + int *create_session(mtl_handle, int *o) override { return (int *)malloc(1); } + + int close_session(int *h) override + { + free(h); + return 0; + } }; class EmulatedST2110_Rx : public connection::ST2110Rx { public: + uint32_t received_packets_dummy1; + uint32_t received_packets_dummy2; EmulatedST2110_Rx() { - _get_frame_fn = wrapper::get_frame_dummy; - _put_frame_fn = wrapper::put_frame_dummy; - _create_session_fn = wrapper::create_dummy; - _close_session_fn = wrapper::close_dummy; + received_packets_dummy1 = 0; + received_packets_dummy2 = 0; _transfer_size = 10000; }; - ~EmulatedST2110_Rx(){}; + ~EmulatedST2110_Rx() {}; connection::Result configure(context::Context &ctx) { set_state(ctx, connection::State::configured); return connection::Result::success; } + + st_frame *get_frame(int *h) override + { + st_frame *f = new st_frame; + f->addr[0] = calloc(1000, 1); + memcpy(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)); + return f; + } + + int put_frame(int *d, st_frame *f) override + { + if (memcmp(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { + received_packets_dummy1++; + } else if (memcmp(f->addr[0], DUMMY_DATA2, sizeof(DUMMY_DATA2)) == 0) { + received_packets_dummy2++; + } + + free(f->addr[0]); + delete f; + return 0; + } + + int *create_session(mtl_handle, int *o) override { return (int *)malloc(1); } + + int close_session(int *h) override + { + free(h); + return 0; + } }; static void validate_state_change(context::Context &ctx, connection::Connection *c) @@ -331,7 +354,7 @@ TEST(st2110_tx, send_data) res = emulated_tx->transmit_wrapper(ctx, (void *)DUMMY_DATA2, sizeof(DUMMY_DATA2)); ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::active); - ASSERT_GT(wrapper::received_packets_dummy2, 0); + ASSERT_GT(conn_tx->received_packets_dummy2, 0); } // Shutdown Tx connection @@ -374,7 +397,7 @@ TEST(st2110_rx, get_data) ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::closed); - ASSERT_GT(wrapper::received_packets_dummy1, 0); + ASSERT_GT(conn_rx->received_packets_dummy1, 0); // Destroy resources delete conn_rx; delete emulated_rx; From 974051f5a9918ebb4a0f0edeb4acf68fd5a16626 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Fri, 22 Nov 2024 13:16:44 +0000 Subject: [PATCH 07/27] Fix mutex --- media-proxy/include/mesh/st2110.h | 1 + media-proxy/include/mesh/st2110rx.h | 3 +-- media-proxy/include/mesh/st2110tx.h | 3 +-- media-proxy/src/mesh/st2110.cc | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 3da6d64d7..5deebed98 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -47,6 +47,7 @@ class ST2110 : public Connection { mtl_handle _st; std::atomic _stop; std::condition_variable_any _cv; + std::mutex _mx; private: }; diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 59ea5fefc..d67997c78 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -83,8 +83,7 @@ template class ST2110Rx : public // Get full buffer from MTL FRAME *frame_ptr = get_frame(_handle); if (!frame_ptr) { /* no frame */ - std::mutex mx; - std::unique_lock lk(mx); + std::unique_lock lk(_mx); _cv.wait(lk, _ctx.stop_token(), [this] { return _stop.load(); }); _stop = false; if (_ctx.cancelled()) { diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index e818038e4..6de9d0797 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -73,8 +73,7 @@ template class ST2110Tx : public // Get empty buffer from MTL frame = get_frame(_handle); if (!frame) { - std::mutex mx; - std::unique_lock lk(mx); + std::unique_lock lk(_mx); _cv.wait(lk, _ctx.stop_token(), [this] { return _stop.load(); }); _stop = false; if (_ctx.cancelled()) { diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index cf648cc3f..e60a32560 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -83,7 +83,7 @@ int ST2110::frame_available_cb(void *ptr) if (!_this) { return -1; } - + std::unique_lock lk(_this->_mx); _this->_stop.store(true); _this->_cv.notify_all(); From 2db96fcf0909944b4b398078f1155c88d2035ea8 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 06:11:49 +0000 Subject: [PATCH 08/27] 1. Free memory allocated by "strdup()" 2. Rename _st to "mtl_device" --- media-proxy/include/mesh/st2110.h | 6 +++--- media-proxy/include/mesh/st2110rx.h | 2 +- media-proxy/include/mesh/st2110tx.h | 2 +- media-proxy/src/mesh/st2110_20rx.cc | 12 +++++++++--- media-proxy/src/mesh/st2110_20tx.cc | 12 +++++++++--- media-proxy/src/mesh/st2110_22rx.cc | 12 +++++++++--- media-proxy/src/mesh/st2110_22tx.cc | 12 +++++++++--- media-proxy/src/mesh/st2110_30rx.cc | 12 +++++++++--- media-proxy/src/mesh/st2110_30tx.cc | 12 +++++++++--- 9 files changed, 59 insertions(+), 23 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 5deebed98..838029487 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -20,7 +20,7 @@ namespace mesh::connection { /** * ST2110 * - * Base abstract class of ST2110. ST2110Rx/ST2110Tx + * Base abstract class of SPMTE ST2110-xx bridge. ST2110Rx/ST2110Tx * inherit this class. */ class ST2110 : public Connection { @@ -38,13 +38,13 @@ class ST2110 : public Connection { static mtl_handle get_mtl_handle(const std::string &dev_port, mtl_log_level log_level, const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); - ST2110() : _st(0){}; + ST2110() : mtl_device(nullptr) {}; virtual ~ST2110(){}; protected: static int frame_available_cb(void *ptr); - mtl_handle _st; + mtl_handle mtl_device; std::atomic _stop; std::condition_variable_any _cv; std::mutex _mx; diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index d67997c78..ee3babfd7 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -40,7 +40,7 @@ template class ST2110Rx : public _ctx = context::WithCancel(ctx); _stop = false; - _handle = create_session(_st, &_ops); + _handle = create_session(mtl_device, &_ops); if (!_handle) { log::error("Failed to create session"); set_state(ctx, State::closed); diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 6de9d0797..3be258078 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -40,7 +40,7 @@ template class ST2110Tx : public _ctx = context::WithCancel(ctx); _stop = false; - _handle = create_session(_st, &_ops); + _handle = create_session(mtl_device, &_ops); if (!_handle) { log::error("Failed to create session"); set_state(ctx, State::closed); diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index 247c002ce..97c884a18 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -4,7 +4,11 @@ namespace mesh::connection { ST2110_20Rx::ST2110_20Rx() {} -ST2110_20Rx::~ST2110_20Rx() {} +ST2110_20Rx::~ST2110_20Rx() +{ + if (_ops.name) + free((void *)_ops.name); +} st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) { return st20p_rx_get_frame(h); }; @@ -30,8 +34,8 @@ Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); } - _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (_st == nullptr) { + mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (mtl_device == nullptr) { log::error("Failed to get MTL device"); set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); @@ -48,6 +52,8 @@ Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); _ops.port.num_port = 1; _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; + if (_ops.name) + free((void *)_ops.name); _ops.name = strdup(session_name); _ops.width = cfg_video.width; _ops.height = cfg_video.height; diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index a7f0f19f1..9b6bb1f86 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -4,7 +4,11 @@ namespace mesh::connection { ST2110_20Tx::ST2110_20Tx() {} -ST2110_20Tx::~ST2110_20Tx() {} +ST2110_20Tx::~ST2110_20Tx() +{ + if (_ops.name) + free((void *)_ops.name); +} st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) { return st20p_tx_get_frame(h); }; @@ -30,8 +34,8 @@ Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); } - _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (_st == nullptr) { + mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (mtl_device == nullptr) { log::error("Failed to get MTL device"); set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); @@ -47,6 +51,8 @@ Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; _ops.port.num_port = 1; _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; + if (_ops.name) + free((void *)_ops.name); _ops.name = strdup(session_name); _ops.width = cfg_video.width; _ops.height = cfg_video.height; diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index a2d82656f..46065cd48 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -4,7 +4,11 @@ namespace mesh::connection { ST2110_22Rx::ST2110_22Rx() {} -ST2110_22Rx::~ST2110_22Rx() {} +ST2110_22Rx::~ST2110_22Rx() +{ + if (_ops.name) + free((void *)_ops.name); +} st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) { return st22p_rx_get_frame(h); }; @@ -30,8 +34,8 @@ Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); } - _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (_st == nullptr) { + mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (mtl_device == nullptr) { log::error("Failed to get MTL device"); set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); @@ -48,6 +52,8 @@ Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); _ops.port.num_port = 1; _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; + if (_ops.name) + free((void *)_ops.name); _ops.name = strdup(session_name); _ops.width = cfg_video.width; _ops.height = cfg_video.height; diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index 79a9a424a..e8b21e440 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -4,7 +4,11 @@ namespace mesh::connection { ST2110_22Tx::ST2110_22Tx() {} -ST2110_22Tx::~ST2110_22Tx() {} +ST2110_22Tx::~ST2110_22Tx() +{ + if (_ops.name) + free((void *)_ops.name); +} st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) { return st22p_tx_get_frame(h); }; @@ -30,8 +34,8 @@ Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); } - _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (_st == nullptr) { + mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (mtl_device == nullptr) { log::error("Failed to get MTL device"); set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); @@ -47,6 +51,8 @@ Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; _ops.port.num_port = 1; _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; + if (_ops.name) + free((void *)_ops.name); _ops.name = strdup(session_name); _ops.width = cfg_video.width; _ops.height = cfg_video.height; diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 474d18e88..644e74c68 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -4,7 +4,11 @@ namespace mesh::connection { ST2110_30Rx::ST2110_30Rx() {} -ST2110_30Rx::~ST2110_30Rx() {} +ST2110_30Rx::~ST2110_30Rx() +{ + if (_ops.name) + free((void *)_ops.name); +} st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) { return st30p_rx_get_frame(h); }; @@ -30,8 +34,8 @@ Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); } - _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (_st == nullptr) { + mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (mtl_device == nullptr) { log::error("Failed to get MTL device"); set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); @@ -48,6 +52,8 @@ Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); _ops.port.num_port = 1; _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; + if (_ops.name) + free((void *)_ops.name); _ops.name = strdup(session_name); _ops.framebuff_cnt = 4; _ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index e7cadc0ee..e8cbc7958 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -4,7 +4,11 @@ namespace mesh::connection { ST2110_30Tx::ST2110_30Tx() {} -ST2110_30Tx::~ST2110_30Tx() {} +ST2110_30Tx::~ST2110_30Tx() +{ + if (_ops.name) + free((void *)_ops.name); +} st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) { return st30p_tx_get_frame(h); }; @@ -30,8 +34,8 @@ Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); } - _st = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (_st == nullptr) { + mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); + if (mtl_device == nullptr) { log::error("Failed to get MTL device"); set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); @@ -47,6 +51,8 @@ Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; _ops.port.num_port = 1; _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; + if (_ops.name) + free((void *)_ops.name); _ops.name = strdup(session_name); _ops.framebuff_cnt = 4; _ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); From cfdbb2016c3c9df3bd88d737954e51c941667afc Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 06:25:56 +0000 Subject: [PATCH 09/27] 1. Remove prefix "_*" from class members name 2. Move class members to default initializer 3. Rename "handle" to "mtl_session" 4. Remove empty "private:" sections --- media-proxy/include/mesh/st2110.h | 8 +-- media-proxy/include/mesh/st2110rx.h | 50 +++++++---------- media-proxy/include/mesh/st2110tx.h | 47 ++++++---------- media-proxy/src/mesh/st2110.cc | 6 +- media-proxy/src/mesh/st2110_20rx.cc | 82 +++++++++++++-------------- media-proxy/src/mesh/st2110_20tx.cc | 76 ++++++++++++------------- media-proxy/src/mesh/st2110_22rx.cc | 86 ++++++++++++++--------------- media-proxy/src/mesh/st2110_22tx.cc | 82 +++++++++++++-------------- media-proxy/src/mesh/st2110_30rx.cc | 76 ++++++++++++------------- media-proxy/src/mesh/st2110_30tx.cc | 70 +++++++++++------------ media-proxy/tests/st2110_tests.cc | 4 +- 11 files changed, 279 insertions(+), 308 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 838029487..3e979ef53 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -45,11 +45,9 @@ class ST2110 : public Connection { static int frame_available_cb(void *ptr); mtl_handle mtl_device; - std::atomic _stop; - std::condition_variable_any _cv; - std::mutex _mx; - - private: + std::atomic stop; + std::condition_variable_any cv; + std::mutex mx; }; } // namespace mesh::connection diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index ee3babfd7..3329185e1 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -13,21 +13,15 @@ namespace mesh::connection { */ template class ST2110Rx : public ST2110 { public: - ST2110Rx() - { - _kind = Kind::receiver; + ST2110Rx() : mtl_session(nullptr), ops({0}), transfer_size(0) { _kind = Kind::receiver; } - _handle = nullptr; - _ops = {0}; - _transfer_size = 0; - } ~ST2110Rx() { shutdown(_ctx); } protected: - HANDLE _handle; - OPS _ops; - size_t _transfer_size; - std::jthread _frame_thread_handle; + HANDLE mtl_session; + OPS ops; + size_t transfer_size; + std::jthread frame_thread_handle; context::Context _ctx; virtual FRAME *get_frame(HANDLE) = 0; @@ -38,10 +32,10 @@ template class ST2110Rx : public Result on_establish(context::Context &ctx) override { _ctx = context::WithCancel(ctx); - _stop = false; + stop = false; - _handle = create_session(mtl_device, &_ops); - if (!_handle) { + mtl_session = create_session(mtl_device, &ops); + if (!mtl_session) { log::error("Failed to create session"); set_state(ctx, State::closed); return set_result(Result::error_general_failure); @@ -49,7 +43,7 @@ template class ST2110Rx : public /* Start MTL session thread. */ try { - _frame_thread_handle = std::jthread(&ST2110Rx::frame_thread, this); + frame_thread_handle = std::jthread(&ST2110Rx::frame_thread, this); } catch (const std::system_error &e) { log::error("Failed to create thread"); set_state(ctx, State::closed); @@ -64,11 +58,11 @@ template class ST2110Rx : public { _ctx.cancel(); - _frame_thread_handle.join(); + frame_thread_handle.join(); - if (_handle) { - close_session(_handle); - _handle = nullptr; + if (mtl_session) { + close_session(mtl_session); + mtl_session = nullptr; } set_state(ctx, State::closed); return set_result(Result::success); @@ -81,20 +75,20 @@ template class ST2110Rx : public { while (!_ctx.cancelled()) { // Get full buffer from MTL - FRAME *frame_ptr = get_frame(_handle); + FRAME *frame_ptr = get_frame(mtl_session); if (!frame_ptr) { /* no frame */ - std::unique_lock lk(_mx); - _cv.wait(lk, _ctx.stop_token(), [this] { return _stop.load(); }); - _stop = false; + std::unique_lock lk(mx); + cv.wait(lk, _ctx.stop_token(), [this] { return stop.load(); }); + stop = false; if (_ctx.cancelled()) { return; } continue; } // Forward buffer to emulated receiver - transmit(_ctx, get_frame_data_ptr(frame_ptr), _transfer_size); + transmit(_ctx, get_frame_data_ptr(frame_ptr), transfer_size); // Return used buffer to MTL - put_frame(_handle, frame_ptr); + put_frame(mtl_session, frame_ptr); } } }; @@ -112,8 +106,6 @@ class ST2110_20Rx : public ST2110Rx { int put_frame(st20p_rx_handle h, st_frame *f) override; st20p_rx_handle create_session(mtl_handle h, st20p_rx_ops *o) override; int close_session(st20p_rx_handle h) override; - - private: }; class ST2110_22Rx : public ST2110Rx { @@ -129,8 +121,6 @@ class ST2110_22Rx : public ST2110Rx { int put_frame(st22p_rx_handle h, st_frame *f) override; st22p_rx_handle create_session(mtl_handle h, st22p_rx_ops *o) override; int close_session(st22p_rx_handle h) override; - - private: }; class ST2110_30Rx : public ST2110Rx { @@ -146,8 +136,6 @@ class ST2110_30Rx : public ST2110Rx { int put_frame(st30p_rx_handle h, st30_frame *f) override; st30p_rx_handle create_session(mtl_handle h, st30p_rx_ops *o) override; int close_session(st30p_rx_handle h) override; - - private: }; } // namespace mesh::connection diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 3be258078..77bb96f9a 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -14,20 +14,13 @@ namespace mesh::connection { */ template class ST2110Tx : public ST2110 { public: - ST2110Tx() - { - _kind = Kind::transmitter; - - _handle = nullptr; - _ops = {0}; - _transfer_size = 0; - }; + ST2110Tx() : mtl_session(nullptr), ops({0}), transfer_size(0) { _kind = Kind::transmitter; }; ~ST2110Tx() { shutdown(_ctx); }; protected: - HANDLE _handle; - OPS _ops; - uint32_t _transfer_size; + HANDLE mtl_session; + OPS ops; + uint32_t transfer_size; context::Context _ctx; virtual FRAME *get_frame(HANDLE) = 0; @@ -38,10 +31,10 @@ template class ST2110Tx : public Result on_establish(context::Context &ctx) override { _ctx = context::WithCancel(ctx); - _stop = false; + stop = false; - _handle = create_session(mtl_device, &_ops); - if (!_handle) { + mtl_session = create_session(mtl_device, &ops); + if (!mtl_session) { log::error("Failed to create session"); set_state(ctx, State::closed); return set_result(Result::error_general_failure); @@ -55,9 +48,9 @@ template class ST2110Tx : public { _ctx.cancel(); - if (_handle) { - close_session(_handle); - _handle = nullptr; + if (mtl_session) { + close_session(mtl_session); + mtl_session = nullptr; } set_state(ctx, State::closed); return set_result(Result::success); @@ -65,17 +58,17 @@ template class ST2110Tx : public Result on_receive(context::Context &ctx, void *ptr, uint32_t sz, uint32_t &sent) override { - sent = std::min(_transfer_size, sz); + sent = std::min(transfer_size, sz); // TODO: add error/warning if sent is different than _transfer_size FRAME *frame = NULL; do { // Get empty buffer from MTL - frame = get_frame(_handle); + frame = get_frame(mtl_session); if (!frame) { - std::unique_lock lk(_mx); - _cv.wait(lk, _ctx.stop_token(), [this] { return _stop.load(); }); - _stop = false; + std::unique_lock lk(mx); + cv.wait(lk, _ctx.stop_token(), [this] { return stop.load(); }); + stop = false; if (_ctx.cancelled()) { break; } @@ -86,15 +79,13 @@ template class ST2110Tx : public // Copy data from emulated transmitter to MTL empty buffer mtl_memcpy(get_frame_data_ptr(frame), ptr, sent); // Return full buffer to MTL - put_frame(_handle, frame); + put_frame(mtl_session, frame); } else { sent = 0; return set_result(Result::error_shutdown); } return set_result(Result::success); }; - - private: }; class ST2110_20Tx : public ST2110Tx { @@ -110,8 +101,6 @@ class ST2110_20Tx : public ST2110Tx { int put_frame(st20p_tx_handle h, st_frame *f) override; st20p_tx_handle create_session(mtl_handle h, st20p_tx_ops *o) override; int close_session(st20p_tx_handle h) override; - - private: }; class ST2110_22Tx : public ST2110Tx { @@ -127,8 +116,6 @@ class ST2110_22Tx : public ST2110Tx { int put_frame(st22p_tx_handle h, st_frame *f) override; st22p_tx_handle create_session(mtl_handle h, st22p_tx_ops *o) override; int close_session(st22p_tx_handle h) override; - - private: }; class ST2110_30Tx : public ST2110Tx { @@ -144,8 +131,6 @@ class ST2110_30Tx : public ST2110Tx { int put_frame(st30p_tx_handle h, st30_frame *f) override; st30p_tx_handle create_session(mtl_handle h, st30p_tx_ops *o) override; int close_session(st30p_tx_handle h) override; - - private: }; } // namespace mesh::connection diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index e60a32560..10faa9fe6 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -83,9 +83,9 @@ int ST2110::frame_available_cb(void *ptr) if (!_this) { return -1; } - std::unique_lock lk(_this->_mx); - _this->_stop.store(true); - _this->_cv.notify_all(); + std::unique_lock lk(_this->mx); + _this->stop.store(true); + _this->cv.notify_all(); return 0; } diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index 97c884a18..811e078a4 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -6,8 +6,8 @@ ST2110_20Rx::ST2110_20Rx() {} ST2110_20Rx::~ST2110_20Rx() { - if (_ops.name) - free((void *)_ops.name); + if (ops.name) + free((void *)ops.name); } st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) { return st20p_rx_get_frame(h); }; @@ -45,47 +45,47 @@ Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port snprintf(session_name, NAME_MAX, "mcm_rx_st20_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.ip_addr[MTL_PORT_P]); - inet_pton(AF_INET, cfg_st2110.local_ip_addr, _ops.port.mcast_sip_addr[MTL_PORT_P]); - _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; - - strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - _ops.port.num_port = 1; - _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; - if (_ops.name) - free((void *)_ops.name); - _ops.name = strdup(session_name); - _ops.width = cfg_video.width; - _ops.height = cfg_video.height; - _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - _ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; - _ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); - _ops.device = ST_PLUGIN_DEVICE_AUTO; - _ops.framebuff_cnt = 4; + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; + + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.num_port = 1; + ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.width = cfg_video.width; + ops.height = cfg_video.height; + ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; + ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + ops.device = ST_PLUGIN_DEVICE_AUTO; + ops.framebuff_cnt = 4; log::info("ProxyContext: %s...", __func__); - log::info("port : %s", _ops.port.port[MTL_PORT_P]); - log::info("ip_addr : %d %d %d %d", _ops.port.ip_addr[MTL_PORT_P][0], - _ops.port.ip_addr[MTL_PORT_P][2], _ops.port.ip_addr[MTL_PORT_P][2], - _ops.port.ip_addr[MTL_PORT_P][3]); - log::info("mcast_sip_addr: %d %d %d %d", _ops.port.mcast_sip_addr[MTL_PORT_P][0], - _ops.port.mcast_sip_addr[MTL_PORT_P][1], _ops.port.mcast_sip_addr[MTL_PORT_P][2], - _ops.port.mcast_sip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", _ops.port.num_port); - log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); - log::info("payload_type : %d", _ops.port.payload_type); - log::info("name : %s", _ops.name); - log::info("width : %d", _ops.width); - log::info("height : %d", _ops.height); - log::info("fps : %d", _ops.fps); - log::info("transport_fmt : %d", _ops.transport_fmt); - log::info("output_fmt : %d", _ops.output_fmt); - log::info("device : %d", _ops.device); - log::info("framebuff_cnt : %d", _ops.framebuff_cnt); - - _transfer_size = st_frame_size(_ops.output_fmt, _ops.width, _ops.height, false); - _ops.priv = this; // app handle register to lib - _ops.notify_frame_available = frame_available_cb; + log::info("port : %s", ops.port.port[MTL_PORT_P]); + log::info("ip_addr : %d %d %d %d", ops.port.ip_addr[MTL_PORT_P][0], + ops.port.ip_addr[MTL_PORT_P][2], ops.port.ip_addr[MTL_PORT_P][2], + ops.port.ip_addr[MTL_PORT_P][3]); + log::info("mcast_sip_addr: %d %d %d %d", ops.port.mcast_sip_addr[MTL_PORT_P][0], + ops.port.mcast_sip_addr[MTL_PORT_P][1], ops.port.mcast_sip_addr[MTL_PORT_P][2], + ops.port.mcast_sip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", ops.port.num_port); + log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); + log::info("payload_type : %d", ops.port.payload_type); + log::info("name : %s", ops.name); + log::info("width : %d", ops.width); + log::info("height : %d", ops.height); + log::info("fps : %d", ops.fps); + log::info("transport_fmt : %d", ops.transport_fmt); + log::info("output_fmt : %d", ops.output_fmt); + log::info("device : %d", ops.device); + log::info("framebuff_cnt : %d", ops.framebuff_cnt); + + transfer_size = st_frame_size(ops.output_fmt, ops.width, ops.height, false); + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index 9b6bb1f86..4f70e33b5 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -6,8 +6,8 @@ ST2110_20Tx::ST2110_20Tx() {} ST2110_20Tx::~ST2110_20Tx() { - if (_ops.name) - free((void *)_ops.name); + if (ops.name) + free((void *)ops.name); } st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) { return st20p_tx_get_frame(h); }; @@ -45,44 +45,44 @@ Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port snprintf(session_name, NAME_MAX, "mcm_tx_st20_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.dip_addr[MTL_PORT_P]); - _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; - strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; - _ops.port.num_port = 1; - _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; - if (_ops.name) - free((void *)_ops.name); - _ops.name = strdup(session_name); - _ops.width = cfg_video.width; - _ops.height = cfg_video.height; - _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - _ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); - _ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; - _ops.device = ST_PLUGIN_DEVICE_AUTO; - _ops.framebuff_cnt = 4; + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; + ops.port.num_port = 1; + ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.width = cfg_video.width; + ops.height = cfg_video.height; + ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; + ops.device = ST_PLUGIN_DEVICE_AUTO; + ops.framebuff_cnt = 4; log::info("ProxyContext: %s...", __func__); - log::info("port : %s", _ops.port.port[MTL_PORT_P]); - log::info("dip_addr : %d %d %d %d", _ops.port.dip_addr[MTL_PORT_P][0], - _ops.port.dip_addr[MTL_PORT_P][2], _ops.port.dip_addr[MTL_PORT_P][2], - _ops.port.dip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", _ops.port.num_port); - log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); - log::info("udp_src_port : %d", _ops.port.udp_src_port[MTL_PORT_P]); - log::info("payload_type : %d", _ops.port.payload_type); - log::info("name : %s", _ops.name); - log::info("width : %d", _ops.width); - log::info("height : %d", _ops.height); - log::info("fps : %d", _ops.fps); - log::info("transport_fmt : %d", _ops.transport_fmt); - log::info("input_fmt : %d", _ops.input_fmt); - log::info("device : %d", _ops.device); - log::info("framebuff_cnt : %d", _ops.framebuff_cnt); - - _transfer_size = st_frame_size(_ops.input_fmt, _ops.width, _ops.height, false); - _ops.priv = this; // app handle register to lib - _ops.notify_frame_available = frame_available_cb; + log::info("port : %s", ops.port.port[MTL_PORT_P]); + log::info("dip_addr : %d %d %d %d", ops.port.dip_addr[MTL_PORT_P][0], + ops.port.dip_addr[MTL_PORT_P][2], ops.port.dip_addr[MTL_PORT_P][2], + ops.port.dip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", ops.port.num_port); + log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); + log::info("udp_src_port : %d", ops.port.udp_src_port[MTL_PORT_P]); + log::info("payload_type : %d", ops.port.payload_type); + log::info("name : %s", ops.name); + log::info("width : %d", ops.width); + log::info("height : %d", ops.height); + log::info("fps : %d", ops.fps); + log::info("transport_fmt : %d", ops.transport_fmt); + log::info("input_fmt : %d", ops.input_fmt); + log::info("device : %d", ops.device); + log::info("framebuff_cnt : %d", ops.framebuff_cnt); + + transfer_size = st_frame_size(ops.input_fmt, ops.width, ops.height, false); + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index 46065cd48..4d09505d7 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -6,8 +6,8 @@ ST2110_22Rx::ST2110_22Rx() {} ST2110_22Rx::~ST2110_22Rx() { - if (_ops.name) - free((void *)_ops.name); + if (ops.name) + free((void *)ops.name); } st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) { return st22p_rx_get_frame(h); }; @@ -45,49 +45,49 @@ Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port snprintf(session_name, NAME_MAX, "mcm_rx_st22_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.ip_addr[MTL_PORT_P]); - inet_pton(AF_INET, cfg_st2110.local_ip_addr, _ops.port.mcast_sip_addr[MTL_PORT_P]); - _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; - - strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - _ops.port.num_port = 1; - _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; - if (_ops.name) - free((void *)_ops.name); - _ops.name = strdup(session_name); - _ops.width = cfg_video.width; - _ops.height = cfg_video.height; - _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - _ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); - _ops.device = ST_PLUGIN_DEVICE_AUTO; - _ops.framebuff_cnt = 4; - _ops.pack_type = ST22_PACK_CODESTREAM; - _ops.codec = ST22_CODEC_JPEGXS; - _ops.codec_thread_cnt = 0; - _ops.max_codestream_size = 0; + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; + + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.num_port = 1; + ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.width = cfg_video.width; + ops.height = cfg_video.height; + ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + ops.device = ST_PLUGIN_DEVICE_AUTO; + ops.framebuff_cnt = 4; + ops.pack_type = ST22_PACK_CODESTREAM; + ops.codec = ST22_CODEC_JPEGXS; + ops.codec_thread_cnt = 0; + ops.max_codestream_size = 0; log::info("ProxyContext: %s...", __func__); - log::info("port : %s", _ops.port.port[MTL_PORT_P]); - log::info("ip_addr : %d %d %d %d", _ops.port.ip_addr[MTL_PORT_P][0], - _ops.port.ip_addr[MTL_PORT_P][2], _ops.port.ip_addr[MTL_PORT_P][2], - _ops.port.ip_addr[MTL_PORT_P][3]); - log::info("mcast_sip_addr: %d %d %d %d", _ops.port.mcast_sip_addr[MTL_PORT_P][0], - _ops.port.mcast_sip_addr[MTL_PORT_P][1], _ops.port.mcast_sip_addr[MTL_PORT_P][2], - _ops.port.mcast_sip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", _ops.port.num_port); - log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); - log::info("payload_type : %d", _ops.port.payload_type); - log::info("name : %s", _ops.name); - log::info("width : %d", _ops.width); - log::info("height : %d", _ops.height); - log::info("fps : %d", _ops.fps); - log::info("output_fmt : %d", _ops.output_fmt); - log::info("device : %d", _ops.device); - log::info("framebuff_cnt : %d", _ops.framebuff_cnt); - - _transfer_size = st_frame_size(_ops.output_fmt, _ops.width, _ops.height, false); - _ops.priv = this; // app handle register to lib - _ops.notify_frame_available = frame_available_cb; + log::info("port : %s", ops.port.port[MTL_PORT_P]); + log::info("ip_addr : %d %d %d %d", ops.port.ip_addr[MTL_PORT_P][0], + ops.port.ip_addr[MTL_PORT_P][2], ops.port.ip_addr[MTL_PORT_P][2], + ops.port.ip_addr[MTL_PORT_P][3]); + log::info("mcast_sip_addr: %d %d %d %d", ops.port.mcast_sip_addr[MTL_PORT_P][0], + ops.port.mcast_sip_addr[MTL_PORT_P][1], ops.port.mcast_sip_addr[MTL_PORT_P][2], + ops.port.mcast_sip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", ops.port.num_port); + log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); + log::info("payload_type : %d", ops.port.payload_type); + log::info("name : %s", ops.name); + log::info("width : %d", ops.width); + log::info("height : %d", ops.height); + log::info("fps : %d", ops.fps); + log::info("output_fmt : %d", ops.output_fmt); + log::info("device : %d", ops.device); + log::info("framebuff_cnt : %d", ops.framebuff_cnt); + + transfer_size = st_frame_size(ops.output_fmt, ops.width, ops.height, false); + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index e8b21e440..57168a600 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -6,8 +6,8 @@ ST2110_22Tx::ST2110_22Tx() {} ST2110_22Tx::~ST2110_22Tx() { - if (_ops.name) - free((void *)_ops.name); + if (ops.name) + free((void *)ops.name); } st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) { return st22p_tx_get_frame(h); }; @@ -45,47 +45,47 @@ Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port snprintf(session_name, NAME_MAX, "mcm_tx_st22_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.dip_addr[MTL_PORT_P]); - _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; - strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; - _ops.port.num_port = 1; - _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; - if (_ops.name) - free((void *)_ops.name); - _ops.name = strdup(session_name); - _ops.width = cfg_video.width; - _ops.height = cfg_video.height; - _ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - _ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); - _ops.device = ST_PLUGIN_DEVICE_AUTO; - _ops.framebuff_cnt = 4; - _ops.pack_type = ST22_PACK_CODESTREAM; - _ops.codec = ST22_CODEC_JPEGXS; - _ops.quality = ST22_QUALITY_MODE_SPEED; - _ops.codec_thread_cnt = 0; - _ops.codestream_size = _ops.width * _ops.height * 3 / 8; + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; + ops.port.num_port = 1; + ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.width = cfg_video.width; + ops.height = cfg_video.height; + ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + ops.device = ST_PLUGIN_DEVICE_AUTO; + ops.framebuff_cnt = 4; + ops.pack_type = ST22_PACK_CODESTREAM; + ops.codec = ST22_CODEC_JPEGXS; + ops.quality = ST22_QUALITY_MODE_SPEED; + ops.codec_thread_cnt = 0; + ops.codestream_size = ops.width * ops.height * 3 / 8; log::info("ProxyContext: %s...", __func__); - log::info("port : %s", _ops.port.port[MTL_PORT_P]); - log::info("dip_addr : %d %d %d %d", _ops.port.dip_addr[MTL_PORT_P][0], - _ops.port.dip_addr[MTL_PORT_P][2], _ops.port.dip_addr[MTL_PORT_P][2], - _ops.port.dip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", _ops.port.num_port); - log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); - log::info("udp_src_port : %d", _ops.port.udp_src_port[MTL_PORT_P]); - log::info("payload_type : %d", _ops.port.payload_type); - log::info("name : %s", _ops.name); - log::info("width : %d", _ops.width); - log::info("height : %d", _ops.height); - log::info("fps : %d", _ops.fps); - log::info("input_fmt : %d", _ops.input_fmt); - log::info("device : %d", _ops.device); - log::info("framebuff_cnt : %d", _ops.framebuff_cnt); - - _transfer_size = st_frame_size(_ops.input_fmt, _ops.width, _ops.height, false); - _ops.priv = this; // app handle register to lib - _ops.notify_frame_available = frame_available_cb; + log::info("port : %s", ops.port.port[MTL_PORT_P]); + log::info("dip_addr : %d %d %d %d", ops.port.dip_addr[MTL_PORT_P][0], + ops.port.dip_addr[MTL_PORT_P][2], ops.port.dip_addr[MTL_PORT_P][2], + ops.port.dip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", ops.port.num_port); + log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); + log::info("udp_src_port : %d", ops.port.udp_src_port[MTL_PORT_P]); + log::info("payload_type : %d", ops.port.payload_type); + log::info("name : %s", ops.name); + log::info("width : %d", ops.width); + log::info("height : %d", ops.height); + log::info("fps : %d", ops.fps); + log::info("input_fmt : %d", ops.input_fmt); + log::info("device : %d", ops.device); + log::info("framebuff_cnt : %d", ops.framebuff_cnt); + + transfer_size = st_frame_size(ops.input_fmt, ops.width, ops.height, false); + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 644e74c68..6957cac03 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -6,8 +6,8 @@ ST2110_30Rx::ST2110_30Rx() {} ST2110_30Rx::~ST2110_30Rx() { - if (_ops.name) - free((void *)_ops.name); + if (ops.name) + free((void *)ops.name); } st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) { return st30p_rx_get_frame(h); }; @@ -45,44 +45,44 @@ Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port snprintf(session_name, NAME_MAX, "mcm_rx_st30_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.ip_addr[MTL_PORT_P]); - inet_pton(AF_INET, cfg_st2110.local_ip_addr, _ops.port.mcast_sip_addr[MTL_PORT_P]); - _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; - - strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - _ops.port.num_port = 1; - _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; - if (_ops.name) - free((void *)_ops.name); - _ops.name = strdup(session_name); - _ops.framebuff_cnt = 4; - _ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); - _ops.channel = cfg_audio.channels; - _ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); - _ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; + + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.num_port = 1; + ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.framebuff_cnt = 4; + ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); + ops.channel = cfg_audio.channels; + ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); + ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); log::info("ProxyContext: %s...", __func__); - log::info("port : %s", _ops.port.port[MTL_PORT_P]); - log::info("ip_addr : %d %d %d %d", _ops.port.ip_addr[MTL_PORT_P][0], - _ops.port.ip_addr[MTL_PORT_P][2], _ops.port.ip_addr[MTL_PORT_P][2], - _ops.port.ip_addr[MTL_PORT_P][3]); - log::info("mcast_sip_addr: %d %d %d %d", _ops.port.mcast_sip_addr[MTL_PORT_P][0], - _ops.port.mcast_sip_addr[MTL_PORT_P][1], _ops.port.mcast_sip_addr[MTL_PORT_P][2], - _ops.port.mcast_sip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", _ops.port.num_port); - log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); - log::info("payload_type : %d", _ops.port.payload_type); - log::info("name : %s", _ops.name); - log::info("framebuff_cnt : %d", _ops.framebuff_cnt); - log::info("audio_fmt : %d", _ops.fmt); - log::info("audio_chan : %d", _ops.channel); - log::info("audio_sampl : %d", _ops.sampling); - log::info("audio_ptime : %d", _ops.ptime); - - _ops.framebuff_size = _transfer_size = - st30_get_packet_size(_ops.fmt, _ops.ptime, _ops.sampling, _ops.channel); - _ops.priv = this; // app handle register to lib - _ops.notify_frame_available = frame_available_cb; + log::info("port : %s", ops.port.port[MTL_PORT_P]); + log::info("ip_addr : %d %d %d %d", ops.port.ip_addr[MTL_PORT_P][0], + ops.port.ip_addr[MTL_PORT_P][2], ops.port.ip_addr[MTL_PORT_P][2], + ops.port.ip_addr[MTL_PORT_P][3]); + log::info("mcast_sip_addr: %d %d %d %d", ops.port.mcast_sip_addr[MTL_PORT_P][0], + ops.port.mcast_sip_addr[MTL_PORT_P][1], ops.port.mcast_sip_addr[MTL_PORT_P][2], + ops.port.mcast_sip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", ops.port.num_port); + log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); + log::info("payload_type : %d", ops.port.payload_type); + log::info("name : %s", ops.name); + log::info("framebuff_cnt : %d", ops.framebuff_cnt); + log::info("audio_fmt : %d", ops.fmt); + log::info("audio_chan : %d", ops.channel); + log::info("audio_sampl : %d", ops.sampling); + log::info("audio_ptime : %d", ops.ptime); + + ops.framebuff_size = transfer_size = + st30_get_packet_size(ops.fmt, ops.ptime, ops.sampling, ops.channel); + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index e8cbc7958..71c6ddae1 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -6,8 +6,8 @@ ST2110_30Tx::ST2110_30Tx() {} ST2110_30Tx::~ST2110_30Tx() { - if (_ops.name) - free((void *)_ops.name); + if (ops.name) + free((void *)ops.name); } st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) { return st30p_tx_get_frame(h); }; @@ -45,41 +45,41 @@ Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port snprintf(session_name, NAME_MAX, "mcm_tx_st30_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, _ops.port.dip_addr[MTL_PORT_P]); - _ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; - strlcpy(_ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - _ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; - _ops.port.num_port = 1; - _ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; - if (_ops.name) - free((void *)_ops.name); - _ops.name = strdup(session_name); - _ops.framebuff_cnt = 4; - _ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); - _ops.channel = cfg_audio.channels; - _ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); - _ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; + ops.port.num_port = 1; + ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.framebuff_cnt = 4; + ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); + ops.channel = cfg_audio.channels; + ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); + ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); log::info("ProxyContext: %s...", __func__); - log::info("port : %s", _ops.port.port[MTL_PORT_P]); - log::info("dip_addr : %d %d %d %d", _ops.port.dip_addr[MTL_PORT_P][0], - _ops.port.dip_addr[MTL_PORT_P][2], _ops.port.dip_addr[MTL_PORT_P][2], - _ops.port.dip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", _ops.port.num_port); - log::info("udp_port : %d", _ops.port.udp_port[MTL_PORT_P]); - log::info("udp_src_port : %d", _ops.port.udp_src_port[MTL_PORT_P]); - log::info("payload_type : %d", _ops.port.payload_type); - log::info("name : %s", _ops.name); - log::info("framebuff_cnt : %d", _ops.framebuff_cnt); - log::info("audio_fmt : %d", _ops.fmt); - log::info("audio_chan : %d", _ops.channel); - log::info("audio_sampl : %d", _ops.sampling); - log::info("audio_ptime : %d", _ops.ptime); - - _ops.framebuff_size = _transfer_size = - st30_get_packet_size(_ops.fmt, _ops.ptime, _ops.sampling, _ops.channel); - _ops.priv = this; // app handle register to lib - _ops.notify_frame_available = frame_available_cb; + log::info("port : %s", ops.port.port[MTL_PORT_P]); + log::info("dip_addr : %d %d %d %d", ops.port.dip_addr[MTL_PORT_P][0], + ops.port.dip_addr[MTL_PORT_P][2], ops.port.dip_addr[MTL_PORT_P][2], + ops.port.dip_addr[MTL_PORT_P][3]); + log::info("num_port : %d", ops.port.num_port); + log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); + log::info("udp_src_port : %d", ops.port.udp_src_port[MTL_PORT_P]); + log::info("payload_type : %d", ops.port.payload_type); + log::info("name : %s", ops.name); + log::info("framebuff_cnt : %d", ops.framebuff_cnt); + log::info("audio_fmt : %d", ops.fmt); + log::info("audio_chan : %d", ops.channel); + log::info("audio_sampl : %d", ops.sampling); + log::info("audio_ptime : %d", ops.ptime); + + ops.framebuff_size = transfer_size = + st30_get_packet_size(ops.fmt, ops.ptime, ops.sampling, ops.channel); + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index cba426555..bd95b12ca 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -69,7 +69,7 @@ class EmulatedST2110_Tx : public connection::ST2110Tx { { received_packets_dummy1 = 0; received_packets_dummy2 = 0; - _transfer_size = 10000; + transfer_size = 10000; }; ~EmulatedST2110_Tx() {}; @@ -117,7 +117,7 @@ class EmulatedST2110_Rx : public connection::ST2110Rx { { received_packets_dummy1 = 0; received_packets_dummy2 = 0; - _transfer_size = 10000; + transfer_size = 10000; }; ~EmulatedST2110_Rx() {}; From 0afaf08eba14b4503e07e76fd3ac1dff7ffddea4 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 06:50:57 +0000 Subject: [PATCH 10/27] 1. Move empty constructor to header 2. Do not inline functions in .cc files --- media-proxy/include/mesh/st2110rx.h | 8 ++++---- media-proxy/include/mesh/st2110tx.h | 8 ++++---- media-proxy/src/mesh/st2110.cc | 11 +++++++++-- media-proxy/src/mesh/st2110_20rx.cc | 12 ++++++++---- media-proxy/src/mesh/st2110_20tx.cc | 12 ++++++++---- media-proxy/src/mesh/st2110_22rx.cc | 12 ++++++++---- media-proxy/src/mesh/st2110_22tx.cc | 12 ++++++++---- media-proxy/src/mesh/st2110_30rx.cc | 12 ++++++++---- media-proxy/src/mesh/st2110_30tx.cc | 12 ++++++++---- 9 files changed, 65 insertions(+), 34 deletions(-) diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 3329185e1..a90f6c3d3 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -13,7 +13,7 @@ namespace mesh::connection { */ template class ST2110Rx : public ST2110 { public: - ST2110Rx() : mtl_session(nullptr), ops({0}), transfer_size(0) { _kind = Kind::receiver; } + ST2110Rx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::receiver; } ~ST2110Rx() { shutdown(_ctx); } @@ -95,7 +95,7 @@ template class ST2110Rx : public class ST2110_20Rx : public ST2110Rx { public: - ST2110_20Rx(); + ST2110_20Rx() {}; ~ST2110_20Rx(); Result configure(context::Context &ctx, const std::string &dev_port, @@ -110,7 +110,7 @@ class ST2110_20Rx : public ST2110Rx { class ST2110_22Rx : public ST2110Rx { public: - ST2110_22Rx(); + ST2110_22Rx() {}; ~ST2110_22Rx(); Result configure(context::Context &ctx, const std::string &dev_port, @@ -125,7 +125,7 @@ class ST2110_22Rx : public ST2110Rx { class ST2110_30Rx : public ST2110Rx { public: - ST2110_30Rx(); + ST2110_30Rx() {}; ~ST2110_30Rx(); Result configure(context::Context &ctx, const std::string &dev_port, diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 77bb96f9a..84279c049 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -14,7 +14,7 @@ namespace mesh::connection { */ template class ST2110Tx : public ST2110 { public: - ST2110Tx() : mtl_session(nullptr), ops({0}), transfer_size(0) { _kind = Kind::transmitter; }; + ST2110Tx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::transmitter; }; ~ST2110Tx() { shutdown(_ctx); }; protected: @@ -90,7 +90,7 @@ template class ST2110Tx : public class ST2110_20Tx : public ST2110Tx { public: - ST2110_20Tx(); + ST2110_20Tx() {}; ~ST2110_20Tx(); Result configure(context::Context &ctx, const std::string &dev_port, @@ -105,7 +105,7 @@ class ST2110_20Tx : public ST2110Tx { class ST2110_22Tx : public ST2110Tx { public: - ST2110_22Tx(); + ST2110_22Tx() {}; ~ST2110_22Tx(); Result configure(context::Context &ctx, const std::string &dev_port, @@ -120,7 +120,7 @@ class ST2110_22Tx : public ST2110Tx { class ST2110_30Tx : public ST2110Tx { public: - ST2110_30Tx(); + ST2110_30Tx() {}; ~ST2110_30Tx(); Result configure(context::Context &ctx, const std::string &dev_port, diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index 10faa9fe6..35df3e107 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -74,8 +74,15 @@ st30_ptime ST2110::mesh_audio_ptime_to_st_ptime(int ptime) } } -void *ST2110::get_frame_data_ptr(st_frame *src) { return src->addr[0]; } -void *ST2110::get_frame_data_ptr(st30_frame *src) { return src->addr; } +void *ST2110::get_frame_data_ptr(st_frame *src) +{ + return src->addr[0]; +} + +void *ST2110::get_frame_data_ptr(st30_frame *src) +{ + return src->addr; +} int ST2110::frame_available_cb(void *ptr) { diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index 811e078a4..7a68e5169 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -2,17 +2,21 @@ namespace mesh::connection { -ST2110_20Rx::ST2110_20Rx() {} - ST2110_20Rx::~ST2110_20Rx() { if (ops.name) free((void *)ops.name); } -st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) { return st20p_rx_get_frame(h); }; +st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) +{ + return st20p_rx_get_frame(h); +}; -int ST2110_20Rx::put_frame(st20p_rx_handle h, st_frame *f) { return st20p_rx_put_frame(h, f); }; +int ST2110_20Rx::put_frame(st20p_rx_handle h, st_frame *f) +{ + return st20p_rx_put_frame(h, f); +}; st20p_rx_handle ST2110_20Rx::create_session(mtl_handle h, st20p_rx_ops *o) { diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index 4f70e33b5..77f107d01 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -2,17 +2,21 @@ namespace mesh::connection { -ST2110_20Tx::ST2110_20Tx() {} - ST2110_20Tx::~ST2110_20Tx() { if (ops.name) free((void *)ops.name); } -st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) { return st20p_tx_get_frame(h); }; +st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) +{ + return st20p_tx_get_frame(h); +}; -int ST2110_20Tx::put_frame(st20p_tx_handle h, st_frame *f) { return st20p_tx_put_frame(h, f); }; +int ST2110_20Tx::put_frame(st20p_tx_handle h, st_frame *f) +{ + return st20p_tx_put_frame(h, f); +}; st20p_tx_handle ST2110_20Tx::create_session(mtl_handle h, st20p_tx_ops *o) { diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index 4d09505d7..53f3f8c4a 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -2,17 +2,21 @@ namespace mesh::connection { -ST2110_22Rx::ST2110_22Rx() {} - ST2110_22Rx::~ST2110_22Rx() { if (ops.name) free((void *)ops.name); } -st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) { return st22p_rx_get_frame(h); }; +st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) +{ + return st22p_rx_get_frame(h); +}; -int ST2110_22Rx::put_frame(st22p_rx_handle h, st_frame *f) { return st22p_rx_put_frame(h, f); }; +int ST2110_22Rx::put_frame(st22p_rx_handle h, st_frame *f) +{ + return st22p_rx_put_frame(h, f); +}; st22p_rx_handle ST2110_22Rx::create_session(mtl_handle h, st22p_rx_ops *o) { diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index 57168a600..d8d962ac5 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -2,17 +2,21 @@ namespace mesh::connection { -ST2110_22Tx::ST2110_22Tx() {} - ST2110_22Tx::~ST2110_22Tx() { if (ops.name) free((void *)ops.name); } -st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) { return st22p_tx_get_frame(h); }; +st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) +{ + return st22p_tx_get_frame(h); +}; -int ST2110_22Tx::put_frame(st22p_tx_handle h, st_frame *f) { return st22p_tx_put_frame(h, f); }; +int ST2110_22Tx::put_frame(st22p_tx_handle h, st_frame *f) +{ + return st22p_tx_put_frame(h, f); +}; st22p_tx_handle ST2110_22Tx::create_session(mtl_handle h, st22p_tx_ops *o) { diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 6957cac03..703553895 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -2,17 +2,21 @@ namespace mesh::connection { -ST2110_30Rx::ST2110_30Rx() {} - ST2110_30Rx::~ST2110_30Rx() { if (ops.name) free((void *)ops.name); } -st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) { return st30p_rx_get_frame(h); }; +st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) +{ + return st30p_rx_get_frame(h); +}; -int ST2110_30Rx::put_frame(st30p_rx_handle h, st30_frame *f) { return st30p_rx_put_frame(h, f); }; +int ST2110_30Rx::put_frame(st30p_rx_handle h, st30_frame *f) +{ + return st30p_rx_put_frame(h, f); +}; st30p_rx_handle ST2110_30Rx::create_session(mtl_handle h, st30p_rx_ops *o) { diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index 71c6ddae1..18bab26fe 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -2,17 +2,21 @@ namespace mesh::connection { -ST2110_30Tx::ST2110_30Tx() {} - ST2110_30Tx::~ST2110_30Tx() { if (ops.name) free((void *)ops.name); } -st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) { return st30p_tx_get_frame(h); }; +st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) +{ + return st30p_tx_get_frame(h); +}; -int ST2110_30Tx::put_frame(st30p_tx_handle h, st30_frame *f) { return st30p_tx_put_frame(h, f); }; +int ST2110_30Tx::put_frame(st30p_tx_handle h, st30_frame *f) +{ + return st30p_tx_put_frame(h, f); +}; st30p_tx_handle ST2110_30Tx::create_session(mtl_handle h, st30p_tx_ops *o) { From e25952d5c44bd1f080c59f812cf0d08cabe053f8 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 07:08:04 +0000 Subject: [PATCH 11/27] Rework functions to return an error if the format is unknown. --- media-proxy/include/mesh/st2110.h | 9 +-- media-proxy/src/mesh/st2110.cc | 88 +++++++++++++++++++---------- media-proxy/src/mesh/st2110_20rx.cc | 5 +- media-proxy/src/mesh/st2110_20tx.cc | 5 +- media-proxy/src/mesh/st2110_22rx.cc | 5 +- media-proxy/src/mesh/st2110_22tx.cc | 5 +- media-proxy/src/mesh/st2110_30rx.cc | 11 +++- media-proxy/src/mesh/st2110_30tx.cc | 12 +++- 8 files changed, 96 insertions(+), 44 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 3e979ef53..8d473fbfd 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -25,10 +25,11 @@ namespace mesh::connection { */ class ST2110 : public Connection { public: - static st_frame_fmt mesh_video_format_to_st_format(int fmt); - static st30_fmt mesh_audio_format_to_st_format(int fmt); - static st30_sampling mesh_audio_sampling_to_st_sampling(int sampling); - static st30_ptime mesh_audio_ptime_to_st_ptime(int ptime); + static int mesh_video_format_to_st_format(int fmt, st_frame_fmt &st_fmt); + static int mesh_audio_format_to_st_format(int fmt, st30_fmt &st_fmt); + static int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling &st_sampling); + static int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime &st_ptime); + static void *get_frame_data_ptr(st_frame *src); static void *get_frame_data_ptr(st30_frame *src); diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index 35df3e107..cec812cf0 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -2,76 +2,104 @@ namespace mesh::connection { -st_frame_fmt ST2110::mesh_video_format_to_st_format(int fmt) +int ST2110::mesh_video_format_to_st_format(int mesh_fmt, st_frame_fmt &st_fmt) { - switch (fmt) { + switch (mesh_fmt) { case MESH_VIDEO_PIXEL_FORMAT_NV12: - return ST_FRAME_FMT_YUV420CUSTOM8; + st_fmt = ST_FRAME_FMT_YUV420CUSTOM8; + break; case MESH_VIDEO_PIXEL_FORMAT_YUV422P: - return ST_FRAME_FMT_YUV422PLANAR8; + st_fmt = ST_FRAME_FMT_YUV422PLANAR8; + break; case MESH_VIDEO_PIXEL_FORMAT_YUV422P10LE: - return ST_FRAME_FMT_YUV422PLANAR10LE; + st_fmt = ST_FRAME_FMT_YUV422PLANAR10LE; + break; case MESH_VIDEO_PIXEL_FORMAT_YUV444P10LE: - return ST_FRAME_FMT_YUV444PLANAR10LE; + st_fmt = ST_FRAME_FMT_YUV444PLANAR10LE; + break; case MESH_VIDEO_PIXEL_FORMAT_RGB8: - return ST_FRAME_FMT_RGB8; + st_fmt = ST_FRAME_FMT_RGB8; + break; default: - return ST_FRAME_FMT_YUV422PLANAR10LE; + return -1; // Error: unknown format } + + return 0; // Success } -st30_fmt ST2110::mesh_audio_format_to_st_format(int fmt) +int ST2110::mesh_audio_format_to_st_format(int mesh_fmt, st30_fmt &st_fmt) { - switch (fmt) { + switch (mesh_fmt) { case MESH_AUDIO_FORMAT_PCM_S8: - return ST30_FMT_PCM8; + st_fmt = ST30_FMT_PCM8; + break; case MESH_AUDIO_FORMAT_PCM_S16BE: - return ST30_FMT_PCM16; + st_fmt = ST30_FMT_PCM16; + break; case MESH_AUDIO_FORMAT_PCM_S24BE: - return ST30_FMT_PCM24; + st_fmt = ST30_FMT_PCM24; + break; default: - return ST30_FMT_MAX; + return -1; // Error: unknown format } + + return 0; // Success } -st30_sampling ST2110::mesh_audio_sampling_to_st_sampling(int sampling) +int ST2110::mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling &st_sampling) { switch (sampling) { case MESH_AUDIO_SAMPLE_RATE_48000: - return ST30_SAMPLING_48K; + st_sampling = ST30_SAMPLING_48K; + break; case MESH_AUDIO_SAMPLE_RATE_96000: - return ST30_SAMPLING_96K; + st_sampling = ST30_SAMPLING_96K; + break; case MESH_AUDIO_SAMPLE_RATE_44100: - return ST31_SAMPLING_44K; + st_sampling = ST31_SAMPLING_44K; + break; default: - return ST30_SAMPLING_MAX; + return -1; // Error: unknown sampling rate } + + return 0; // Success } -st30_ptime ST2110::mesh_audio_ptime_to_st_ptime(int ptime) +int ST2110::mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime &st_ptime) { switch (ptime) { case MESH_AUDIO_PACKET_TIME_1MS: - return ST30_PTIME_1MS; + st_ptime = ST30_PTIME_1MS; + break; case MESH_AUDIO_PACKET_TIME_125US: - return ST30_PTIME_125US; + st_ptime = ST30_PTIME_125US; + break; case MESH_AUDIO_PACKET_TIME_250US: - return ST30_PTIME_250US; + st_ptime = ST30_PTIME_250US; + break; case MESH_AUDIO_PACKET_TIME_333US: - return ST30_PTIME_333US; + st_ptime = ST30_PTIME_333US; + break; case MESH_AUDIO_PACKET_TIME_4MS: - return ST30_PTIME_4MS; + st_ptime = ST30_PTIME_4MS; + break; case MESH_AUDIO_PACKET_TIME_80US: - return ST31_PTIME_80US; + st_ptime = ST31_PTIME_80US; + break; case MESH_AUDIO_PACKET_TIME_1_09MS: - return ST31_PTIME_1_09MS; + st_ptime = ST31_PTIME_1_09MS; + break; case MESH_AUDIO_PACKET_TIME_0_14MS: - return ST31_PTIME_0_14MS; + st_ptime = ST31_PTIME_0_14MS; + break; case MESH_AUDIO_PACKET_TIME_0_09MS: - return ST31_PTIME_0_09MS; + st_ptime = ST31_PTIME_0_09MS; + break; default: - return ST30_PTIME_MAX; + return -1; // Error: unknown packet time } + + return 0; // Success } void *ST2110::get_frame_data_ptr(st_frame *src) diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index 7a68e5169..0349c4775 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -63,7 +63,10 @@ Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; - ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.output_fmt)) + return set_result(Result::error_bad_argument); + ops.device = ST_PLUGIN_DEVICE_AUTO; ops.framebuff_cnt = 4; diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index 77f107d01..98f30d564 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -61,7 +61,10 @@ Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.input_fmt)) + return set_result(Result::error_bad_argument); + ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; ops.device = ST_PLUGIN_DEVICE_AUTO; ops.framebuff_cnt = 4; diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index 53f3f8c4a..e9fd0dd66 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -62,7 +62,10 @@ Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - ops.output_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.output_fmt)) + return set_result(Result::error_bad_argument); + ops.device = ST_PLUGIN_DEVICE_AUTO; ops.framebuff_cnt = 4; ops.pack_type = ST22_PACK_CODESTREAM; diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index d8d962ac5..20752e2a0 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -61,7 +61,10 @@ Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - ops.input_fmt = mesh_video_format_to_st_format(cfg_video.pixel_format); + + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.input_fmt)) + return set_result(Result::error_bad_argument); + ops.device = ST_PLUGIN_DEVICE_AUTO; ops.framebuff_cnt = 4; ops.pack_type = ST22_PACK_CODESTREAM; diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 703553895..3767a5ca5 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -60,10 +60,15 @@ Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port free((void *)ops.name); ops.name = strdup(session_name); ops.framebuff_cnt = 4; - ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); + if(mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) + return set_result(Result::error_bad_argument); + ops.channel = cfg_audio.channels; - ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); - ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); + if(mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) + return set_result(Result::error_bad_argument); + + if(mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) + return set_result(Result::error_bad_argument); log::info("ProxyContext: %s...", __func__); log::info("port : %s", ops.port.port[MTL_PORT_P]); diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index 18bab26fe..5ff9e8c5f 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -59,10 +59,16 @@ Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port free((void *)ops.name); ops.name = strdup(session_name); ops.framebuff_cnt = 4; - ops.fmt = mesh_audio_format_to_st_format(cfg_audio.format); + + if(mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) + return set_result(Result::error_bad_argument); + ops.channel = cfg_audio.channels; - ops.sampling = mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate); - ops.ptime = mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time); + if(mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) + return set_result(Result::error_bad_argument); + + if(mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) + return set_result(Result::error_bad_argument); log::info("ProxyContext: %s...", __func__); log::info("port : %s", ops.port.port[MTL_PORT_P]); From d194ef12560fe297299f4de17590a77c1b6dc861 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 07:49:05 +0000 Subject: [PATCH 12/27] 1. Initialze _ctx with context::WithCancel(context::Background()); 2. Fix ST2110Tx description 3. Add Check if st_frame_size() return error 4. Rework ST2110 configure() move as much as possible to base class 5. Fix session_id to be thread-safe 6. Rework configure() to use the structured logging feature. --- media-proxy/include/mesh/st2110.h | 5 ++- media-proxy/include/mesh/st2110rx.h | 63 +++++++++++++++++++++----- media-proxy/include/mesh/st2110tx.h | 63 ++++++++++++++++++++------ media-proxy/src/mesh/st2110.cc | 7 ++- media-proxy/src/mesh/st2110_20rx.cc | 68 +++++++--------------------- media-proxy/src/mesh/st2110_20tx.cc | 65 +++++++-------------------- media-proxy/src/mesh/st2110_22rx.cc | 64 ++++++-------------------- media-proxy/src/mesh/st2110_22tx.cc | 61 ++++++------------------- media-proxy/src/mesh/st2110_30rx.cc | 69 +++++++---------------------- media-proxy/src/mesh/st2110_30tx.cc | 65 +++++++-------------------- media-proxy/tests/st2110_tests.cc | 8 ++-- 11 files changed, 207 insertions(+), 331 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 8d473fbfd..d93d0ff6a 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -36,8 +36,9 @@ class ST2110 : public Connection { static void get_mtl_dev_params(mtl_init_params &st_param, const std::string &dev_port, mtl_log_level log_level, const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); - static mtl_handle get_mtl_handle(const std::string &dev_port, mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); + static mtl_handle get_mtl_device(const std::string &dev_port, mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE], + int &session_id); ST2110() : mtl_device(nullptr) {}; virtual ~ST2110(){}; diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index a90f6c3d3..f217b2b8b 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -15,20 +15,70 @@ template class ST2110Rx : public public: ST2110Rx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::receiver; } - ~ST2110Rx() { shutdown(_ctx); } + ~ST2110Rx() + { + shutdown(_ctx); + if (ops.name) + free((void *)ops.name); + } protected: HANDLE mtl_session; OPS ops; size_t transfer_size; std::jthread frame_thread_handle; - context::Context _ctx; + context::Context _ctx = context::WithCancel(context::Background()); virtual FRAME *get_frame(HANDLE) = 0; virtual int put_frame(HANDLE, FRAME *) = 0; virtual HANDLE create_session(mtl_handle, OPS *) = 0; virtual int close_session(HANDLE) = 0; + int configure_common(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110) + { + int session_id = 0; + mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); + if (mtl_device == nullptr) { + log::error("Failed to get MTL device"); + return -1; + } + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; + + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.num_port = 1; + + char session_name[NAME_MAX] = ""; + snprintf(session_name, NAME_MAX, "mcm_mtl_rx_%d", session_id); + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.framebuff_cnt = 4; + + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; + + log::info("ST2110Rx: configure") + ("port", ops.port.port[MTL_PORT_P]) + ("ip_addr", std::to_string(ops.port.ip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][3])) + ("mcast_sip_addr", std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][3])) + ("num_port", ops.port.num_port) + ("udp_port", ops.port.udp_port[MTL_PORT_P]) + ("name", ops.name) + ("framebuff_cnt", ops.framebuff_cnt); + + return 0; + } + Result on_establish(context::Context &ctx) override { _ctx = context::WithCancel(ctx); @@ -95,9 +145,6 @@ template class ST2110Rx : public class ST2110_20Rx : public ST2110Rx { public: - ST2110_20Rx() {}; - ~ST2110_20Rx(); - Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); @@ -110,9 +157,6 @@ class ST2110_20Rx : public ST2110Rx { class ST2110_22Rx : public ST2110Rx { public: - ST2110_22Rx() {}; - ~ST2110_22Rx(); - Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); @@ -125,9 +169,6 @@ class ST2110_22Rx : public ST2110Rx { class ST2110_30Rx : public ST2110Rx { public: - ST2110_30Rx() {}; - ~ST2110_30Rx(); - Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 84279c049..aa1ca017a 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -7,27 +7,73 @@ namespace mesh::connection { /** - * ST2110 + * ST2110Tx * - * Base abstract class of ST2110. ST2110_20Tx/ST2110_22Tx/ST2110_30Tx + * Base abstract class of ST2110Tx. ST2110_20Tx/ST2110_22Tx/ST2110_30Tx * inherit this class. */ template class ST2110Tx : public ST2110 { public: ST2110Tx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::transmitter; }; - ~ST2110Tx() { shutdown(_ctx); }; + ~ST2110Tx() + { + shutdown(_ctx); + if (ops.name) + free((void *)ops.name); + }; protected: HANDLE mtl_session; OPS ops; uint32_t transfer_size; - context::Context _ctx; + context::Context _ctx = context::WithCancel(context::Background()); virtual FRAME *get_frame(HANDLE) = 0; virtual int put_frame(HANDLE, FRAME *) = 0; virtual HANDLE create_session(mtl_handle, OPS *) = 0; virtual int close_session(HANDLE) = 0; + int configure_common(context::Context &ctx, const std::string &dev_port, + const MeshConfig_ST2110 &cfg_st2110) + { + int session_id = 0; + mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); + if (mtl_device == nullptr) { + log::error("Failed to get MTL device"); + return -1; + } + + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); + ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; + ops.port.num_port = 1; + + char session_name[NAME_MAX] = ""; + snprintf(session_name, NAME_MAX, "mcm_mtl_tx_%d", session_id); + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.framebuff_cnt = 4; + + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; + + log::info("ST2110Tx: configure") + ("port", ops.port.port[MTL_PORT_P]) + ("dip_addr", std::to_string(ops.port.dip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][3])) + ("num_port", ops.port.num_port) + ("udp_port", ops.port.udp_port[MTL_PORT_P]) + ("udp_src_port", ops.port.udp_src_port[MTL_PORT_P]) + ("name", ops.name) + ("framebuff_cnt", ops.framebuff_cnt); + + return 0; + } + Result on_establish(context::Context &ctx) override { _ctx = context::WithCancel(ctx); @@ -90,9 +136,6 @@ template class ST2110Tx : public class ST2110_20Tx : public ST2110Tx { public: - ST2110_20Tx() {}; - ~ST2110_20Tx(); - Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); @@ -105,9 +148,6 @@ class ST2110_20Tx : public ST2110Tx { class ST2110_22Tx : public ST2110Tx { public: - ST2110_22Tx() {}; - ~ST2110_22Tx(); - Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); @@ -120,9 +160,6 @@ class ST2110_22Tx : public ST2110Tx { class ST2110_30Tx : public ST2110Tx { public: - ST2110_30Tx() {}; - ~ST2110_30Tx(); - Result configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index cec812cf0..855cf93c3 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -156,13 +156,16 @@ void ST2110::get_mtl_dev_params(mtl_init_params &st_param, const std::string &de st_param.memzone_max = 9000; } -mtl_handle ST2110::get_mtl_handle(const std::string &dev_port, mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE]) +mtl_handle ST2110::get_mtl_device(const std::string &dev_port, mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int &session_id) { static mtl_handle dev_handle; + static int _session_id; static std::mutex mtx; std::lock_guard lock(mtx); + session_id = _session_id++; + if (dev_handle) { return dev_handle; } diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index 0349c4775..c91a16a6c 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -2,12 +2,6 @@ namespace mesh::connection { -ST2110_20Rx::~ST2110_20Rx() -{ - if (ops.name) - free((void *)ops.name); -} - st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) { return st20p_rx_get_frame(h); @@ -23,42 +17,24 @@ st20p_rx_handle ST2110_20Rx::create_session(mtl_handle h, st20p_rx_ops *o) return st20p_rx_create(h, o); }; -int ST2110_20Rx::close_session(st20p_rx_handle h) { return st20p_rx_free(h); }; +int ST2110_20Rx::close_session(st20p_rx_handle h) +{ + return st20p_rx_free(h); +}; Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - static int session_id; - static std::mutex mtx; - std::lock_guard lock(mtx); - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); } - mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (mtl_device == nullptr) { - log::error("Failed to get MTL device"); - set_state(ctx, State::not_configured); + if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); - } - - char session_name[NAME_MAX] = ""; - - snprintf(session_name, NAME_MAX, "mcm_rx_st20_%d", session_id++); - - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); - inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.num_port = 1; ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); @@ -68,31 +44,19 @@ Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); ops.device = ST_PLUGIN_DEVICE_AUTO; - ops.framebuff_cnt = 4; - - log::info("ProxyContext: %s...", __func__); - log::info("port : %s", ops.port.port[MTL_PORT_P]); - log::info("ip_addr : %d %d %d %d", ops.port.ip_addr[MTL_PORT_P][0], - ops.port.ip_addr[MTL_PORT_P][2], ops.port.ip_addr[MTL_PORT_P][2], - ops.port.ip_addr[MTL_PORT_P][3]); - log::info("mcast_sip_addr: %d %d %d %d", ops.port.mcast_sip_addr[MTL_PORT_P][0], - ops.port.mcast_sip_addr[MTL_PORT_P][1], ops.port.mcast_sip_addr[MTL_PORT_P][2], - ops.port.mcast_sip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", ops.port.num_port); - log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); - log::info("payload_type : %d", ops.port.payload_type); - log::info("name : %s", ops.name); - log::info("width : %d", ops.width); - log::info("height : %d", ops.height); - log::info("fps : %d", ops.fps); - log::info("transport_fmt : %d", ops.transport_fmt); - log::info("output_fmt : %d", ops.output_fmt); - log::info("device : %d", ops.device); - log::info("framebuff_cnt : %d", ops.framebuff_cnt); transfer_size = st_frame_size(ops.output_fmt, ops.width, ops.height, false); - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + if (transfer_size == 0) + return set_result(Result::error_bad_argument); + + log::info("ST2110_20Rx: configure") + ("payload_type", (int)ops.port.payload_type) + ("width", ops.width) + ("height", ops.height) + ("fps", ops.fps) + ("transport_fmt", ops.transport_fmt) + ("output_fmt", ops.output_fmt) + ("device", ops.device); set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index 98f30d564..691e4c6e0 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -2,12 +2,6 @@ namespace mesh::connection { -ST2110_20Tx::~ST2110_20Tx() -{ - if (ops.name) - free((void *)ops.name); -} - st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) { return st20p_tx_get_frame(h); @@ -23,73 +17,46 @@ st20p_tx_handle ST2110_20Tx::create_session(mtl_handle h, st20p_tx_ops *o) return st20p_tx_create(h, o); }; -int ST2110_20Tx::close_session(st20p_tx_handle h) { return st20p_tx_free(h); }; +int ST2110_20Tx::close_session(st20p_tx_handle h) +{ + return st20p_tx_free(h); +}; Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - static int session_id; - static std::mutex mtx; - std::lock_guard lock(mtx); - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); } - mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (mtl_device == nullptr) { - log::error("Failed to get MTL device"); - set_state(ctx, State::not_configured); + if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); - } - - char session_name[NAME_MAX] = ""; - - snprintf(session_name, NAME_MAX, "mcm_tx_st20_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; - ops.port.num_port = 1; ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); + ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.input_fmt)) return set_result(Result::error_bad_argument); - ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; ops.device = ST_PLUGIN_DEVICE_AUTO; - ops.framebuff_cnt = 4; - log::info("ProxyContext: %s...", __func__); - log::info("port : %s", ops.port.port[MTL_PORT_P]); - log::info("dip_addr : %d %d %d %d", ops.port.dip_addr[MTL_PORT_P][0], - ops.port.dip_addr[MTL_PORT_P][2], ops.port.dip_addr[MTL_PORT_P][2], - ops.port.dip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", ops.port.num_port); - log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); - log::info("udp_src_port : %d", ops.port.udp_src_port[MTL_PORT_P]); - log::info("payload_type : %d", ops.port.payload_type); - log::info("name : %s", ops.name); - log::info("width : %d", ops.width); - log::info("height : %d", ops.height); - log::info("fps : %d", ops.fps); - log::info("transport_fmt : %d", ops.transport_fmt); - log::info("input_fmt : %d", ops.input_fmt); - log::info("device : %d", ops.device); - log::info("framebuff_cnt : %d", ops.framebuff_cnt); + log::info("ST2110_20Tx: configure") + ("payload_type", (int)ops.port.payload_type) + ("width", ops.width) + ("height", ops.height) + ("fps", ops.fps) + ("transport_fmt", ops.transport_fmt) + ("input_fmt", ops.input_fmt) + ("device", ops.device); transfer_size = st_frame_size(ops.input_fmt, ops.width, ops.height, false); - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + if (transfer_size == 0) + return set_result(Result::error_bad_argument); set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index e9fd0dd66..ef2323e58 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -2,12 +2,6 @@ namespace mesh::connection { -ST2110_22Rx::~ST2110_22Rx() -{ - if (ops.name) - free((void *)ops.name); -} - st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) { return st22p_rx_get_frame(h); @@ -23,42 +17,24 @@ st22p_rx_handle ST2110_22Rx::create_session(mtl_handle h, st22p_rx_ops *o) return st22p_rx_create(h, o); }; -int ST2110_22Rx::close_session(st22p_rx_handle h) { return st22p_rx_free(h); }; +int ST2110_22Rx::close_session(st22p_rx_handle h) +{ + return st22p_rx_free(h); +}; Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - static int session_id; - static std::mutex mtx; - std::lock_guard lock(mtx); - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); } - mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (mtl_device == nullptr) { - log::error("Failed to get MTL device"); - set_state(ctx, State::not_configured); + if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); - } - - char session_name[NAME_MAX] = ""; - snprintf(session_name, NAME_MAX, "mcm_rx_st22_%d", session_id++); - - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); - inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; - - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.num_port = 1; ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); @@ -67,34 +43,22 @@ Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); ops.device = ST_PLUGIN_DEVICE_AUTO; - ops.framebuff_cnt = 4; ops.pack_type = ST22_PACK_CODESTREAM; ops.codec = ST22_CODEC_JPEGXS; ops.codec_thread_cnt = 0; ops.max_codestream_size = 0; - log::info("ProxyContext: %s...", __func__); - log::info("port : %s", ops.port.port[MTL_PORT_P]); - log::info("ip_addr : %d %d %d %d", ops.port.ip_addr[MTL_PORT_P][0], - ops.port.ip_addr[MTL_PORT_P][2], ops.port.ip_addr[MTL_PORT_P][2], - ops.port.ip_addr[MTL_PORT_P][3]); - log::info("mcast_sip_addr: %d %d %d %d", ops.port.mcast_sip_addr[MTL_PORT_P][0], - ops.port.mcast_sip_addr[MTL_PORT_P][1], ops.port.mcast_sip_addr[MTL_PORT_P][2], - ops.port.mcast_sip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", ops.port.num_port); - log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); - log::info("payload_type : %d", ops.port.payload_type); - log::info("name : %s", ops.name); - log::info("width : %d", ops.width); - log::info("height : %d", ops.height); - log::info("fps : %d", ops.fps); - log::info("output_fmt : %d", ops.output_fmt); - log::info("device : %d", ops.device); - log::info("framebuff_cnt : %d", ops.framebuff_cnt); + log::info("ST2110_22Rx: configure") + ("payload_type", (int)ops.port.payload_type) + ("width", ops.width) + ("height", ops.height) + ("fps", ops.fps) + ("output_fmt", ops.output_fmt) + ("device", ops.device); transfer_size = st_frame_size(ops.output_fmt, ops.width, ops.height, false); - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + if (transfer_size == 0) + return set_result(Result::error_bad_argument); set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index 20752e2a0..4eb1ec8f5 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -2,12 +2,6 @@ namespace mesh::connection { -ST2110_22Tx::~ST2110_22Tx() -{ - if (ops.name) - free((void *)ops.name); -} - st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) { return st22p_tx_get_frame(h); @@ -23,41 +17,24 @@ st22p_tx_handle ST2110_22Tx::create_session(mtl_handle h, st22p_tx_ops *o) return st22p_tx_create(h, o); }; -int ST2110_22Tx::close_session(st22p_tx_handle h) { return st22p_tx_free(h); }; +int ST2110_22Tx::close_session(st22p_tx_handle h) +{ + return st22p_tx_free(h); +}; Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - static int session_id; - static std::mutex mtx; - std::lock_guard lock(mtx); - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); } - mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (mtl_device == nullptr) { - log::error("Failed to get MTL device"); - set_state(ctx, State::not_configured); + if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); - } - - char session_name[NAME_MAX] = ""; - snprintf(session_name, NAME_MAX, "mcm_tx_st22_%d", session_id++); - - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; - ops.port.num_port = 1; ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); @@ -66,33 +43,23 @@ Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port return set_result(Result::error_bad_argument); ops.device = ST_PLUGIN_DEVICE_AUTO; - ops.framebuff_cnt = 4; ops.pack_type = ST22_PACK_CODESTREAM; ops.codec = ST22_CODEC_JPEGXS; ops.quality = ST22_QUALITY_MODE_SPEED; ops.codec_thread_cnt = 0; ops.codestream_size = ops.width * ops.height * 3 / 8; - log::info("ProxyContext: %s...", __func__); - log::info("port : %s", ops.port.port[MTL_PORT_P]); - log::info("dip_addr : %d %d %d %d", ops.port.dip_addr[MTL_PORT_P][0], - ops.port.dip_addr[MTL_PORT_P][2], ops.port.dip_addr[MTL_PORT_P][2], - ops.port.dip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", ops.port.num_port); - log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); - log::info("udp_src_port : %d", ops.port.udp_src_port[MTL_PORT_P]); - log::info("payload_type : %d", ops.port.payload_type); - log::info("name : %s", ops.name); - log::info("width : %d", ops.width); - log::info("height : %d", ops.height); - log::info("fps : %d", ops.fps); - log::info("input_fmt : %d", ops.input_fmt); - log::info("device : %d", ops.device); - log::info("framebuff_cnt : %d", ops.framebuff_cnt); + log::info("ST2110_22Tx: configure") + ("payload_type", (int)ops.port.payload_type) + ("width", ops.width) + ("height", ops.height) + ("fps", ops.fps) + ("input_fmt", ops.input_fmt) + ("device", ops.device); transfer_size = st_frame_size(ops.input_fmt, ops.width, ops.height, false); - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + if (transfer_size == 0) + return set_result(Result::error_bad_argument); set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 3767a5ca5..1fa416063 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -2,12 +2,6 @@ namespace mesh::connection { -ST2110_30Rx::~ST2110_30Rx() -{ - if (ops.name) - free((void *)ops.name); -} - st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) { return st30p_rx_get_frame(h); @@ -23,75 +17,46 @@ st30p_rx_handle ST2110_30Rx::create_session(mtl_handle h, st30p_rx_ops *o) return st30p_rx_create(h, o); }; -int ST2110_30Rx::close_session(st30p_rx_handle h) { return st30p_rx_free(h); }; +int ST2110_30Rx::close_session(st30p_rx_handle h) +{ + return st30p_rx_free(h); +}; Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio) { - static int session_id; - static std::mutex mtx; - std::lock_guard lock(mtx); - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); } - mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (mtl_device == nullptr) { - log::error("Failed to get MTL device"); - set_state(ctx, State::not_configured); + if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); - } - - char session_name[NAME_MAX] = ""; - - snprintf(session_name, NAME_MAX, "mcm_rx_st30_%d", session_id++); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); - inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; - - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.num_port = 1; ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); - ops.framebuff_cnt = 4; - if(mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) + + if (mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) return set_result(Result::error_bad_argument); ops.channel = cfg_audio.channels; - if(mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) + if (mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) return set_result(Result::error_bad_argument); - if(mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) + if (mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) return set_result(Result::error_bad_argument); - log::info("ProxyContext: %s...", __func__); - log::info("port : %s", ops.port.port[MTL_PORT_P]); - log::info("ip_addr : %d %d %d %d", ops.port.ip_addr[MTL_PORT_P][0], - ops.port.ip_addr[MTL_PORT_P][2], ops.port.ip_addr[MTL_PORT_P][2], - ops.port.ip_addr[MTL_PORT_P][3]); - log::info("mcast_sip_addr: %d %d %d %d", ops.port.mcast_sip_addr[MTL_PORT_P][0], - ops.port.mcast_sip_addr[MTL_PORT_P][1], ops.port.mcast_sip_addr[MTL_PORT_P][2], - ops.port.mcast_sip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", ops.port.num_port); - log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); - log::info("payload_type : %d", ops.port.payload_type); - log::info("name : %s", ops.name); - log::info("framebuff_cnt : %d", ops.framebuff_cnt); - log::info("audio_fmt : %d", ops.fmt); - log::info("audio_chan : %d", ops.channel); - log::info("audio_sampl : %d", ops.sampling); - log::info("audio_ptime : %d", ops.ptime); + log::info("ST2110_22Rx: configure") + ("payload_type", (int)ops.port.payload_type) + ("audio_fmt", ops.fmt) + ("audio_chan", ops.channel) + ("audio_sampl", ops.sampling) + ("audio_ptime", ops.ptime); ops.framebuff_size = transfer_size = st30_get_packet_size(ops.fmt, ops.ptime, ops.sampling, ops.channel); - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + if (transfer_size == 0) + return set_result(Result::error_bad_argument); set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index 5ff9e8c5f..fee869b95 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -2,12 +2,6 @@ namespace mesh::connection { -ST2110_30Tx::~ST2110_30Tx() -{ - if (ops.name) - free((void *)ops.name); -} - st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) { return st30p_tx_get_frame(h); @@ -23,73 +17,46 @@ st30p_tx_handle ST2110_30Tx::create_session(mtl_handle h, st30p_tx_ops *o) return st30p_tx_create(h, o); }; -int ST2110_30Tx::close_session(st30p_tx_handle h) { return st30p_tx_free(h); }; +int ST2110_30Tx::close_session(st30p_tx_handle h) +{ + return st30p_tx_free(h); +}; Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port, const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio) { - static int session_id; - static std::mutex mtx; - std::lock_guard lock(mtx); - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); } - mtl_device = get_mtl_handle(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr); - if (mtl_device == nullptr) { - log::error("Failed to get MTL device"); - set_state(ctx, State::not_configured); + if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); - } - - char session_name[NAME_MAX] = ""; - snprintf(session_name, NAME_MAX, "mcm_tx_st30_%d", session_id++); - - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; - ops.port.num_port = 1; ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); - ops.framebuff_cnt = 4; - if(mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) + if (mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) return set_result(Result::error_bad_argument); ops.channel = cfg_audio.channels; - if(mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) + if (mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) return set_result(Result::error_bad_argument); - if(mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) + if (mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) return set_result(Result::error_bad_argument); - log::info("ProxyContext: %s...", __func__); - log::info("port : %s", ops.port.port[MTL_PORT_P]); - log::info("dip_addr : %d %d %d %d", ops.port.dip_addr[MTL_PORT_P][0], - ops.port.dip_addr[MTL_PORT_P][2], ops.port.dip_addr[MTL_PORT_P][2], - ops.port.dip_addr[MTL_PORT_P][3]); - log::info("num_port : %d", ops.port.num_port); - log::info("udp_port : %d", ops.port.udp_port[MTL_PORT_P]); - log::info("udp_src_port : %d", ops.port.udp_src_port[MTL_PORT_P]); - log::info("payload_type : %d", ops.port.payload_type); - log::info("name : %s", ops.name); - log::info("framebuff_cnt : %d", ops.framebuff_cnt); - log::info("audio_fmt : %d", ops.fmt); - log::info("audio_chan : %d", ops.channel); - log::info("audio_sampl : %d", ops.sampling); - log::info("audio_ptime : %d", ops.ptime); + log::info("ST2110_30Tx: configure") + ("payload_type", (int)ops.port.payload_type) + ("audio_fmt", ops.fmt) + ("audio_chan", ops.channel) + ("audio_sampl", ops.sampling) + ("audio_ptime", ops.ptime); ops.framebuff_size = transfer_size = st30_get_packet_size(ops.fmt, ops.ptime, ops.sampling, ops.channel); - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + if (transfer_size == 0) + return set_result(Result::error_bad_argument); set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index bd95b12ca..a7537f4f3 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -60,7 +60,7 @@ class EmulatedReceiver : public connection::Connection { } }; -class EmulatedST2110_Tx : public connection::ST2110Tx { +class EmulatedST2110_Tx : public connection::ST2110Tx { public: uint32_t received_packets_dummy1; uint32_t received_packets_dummy2; @@ -100,7 +100,7 @@ class EmulatedST2110_Tx : public connection::ST2110Tx { return 0; } - int *create_session(mtl_handle, int *o) override { return (int *)malloc(1); } + int *create_session(mtl_handle, st20p_rx_ops *o) override { return (int *)malloc(1); } int close_session(int *h) override { @@ -109,7 +109,7 @@ class EmulatedST2110_Tx : public connection::ST2110Tx { } }; -class EmulatedST2110_Rx : public connection::ST2110Rx { +class EmulatedST2110_Rx : public connection::ST2110Rx { public: uint32_t received_packets_dummy1; uint32_t received_packets_dummy2; @@ -148,7 +148,7 @@ class EmulatedST2110_Rx : public connection::ST2110Rx { return 0; } - int *create_session(mtl_handle, int *o) override { return (int *)malloc(1); } + int *create_session(mtl_handle, st20p_rx_ops *o) override { return (int *)malloc(1); } int close_session(int *h) override { From 1f0e0daf0769a4632a9daba7a26993a06e2c4c3c Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 10:45:15 +0000 Subject: [PATCH 13/27] Set sent variable only when data is actually sent. --- media-proxy/include/mesh/st2110tx.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index aa1ca017a..ab4dc1211 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -104,7 +104,7 @@ template class ST2110Tx : public Result on_receive(context::Context &ctx, void *ptr, uint32_t sz, uint32_t &sent) override { - sent = std::min(transfer_size, sz); + int _sent = std::min(transfer_size, sz); // TODO: add error/warning if sent is different than _transfer_size FRAME *frame = NULL; @@ -116,20 +116,21 @@ template class ST2110Tx : public cv.wait(lk, _ctx.stop_token(), [this] { return stop.load(); }); stop = false; if (_ctx.cancelled()) { - break; + return set_result(Result::error_shutdown); } } } while (!frame); if (frame) { // Copy data from emulated transmitter to MTL empty buffer - mtl_memcpy(get_frame_data_ptr(frame), ptr, sent); + mtl_memcpy(get_frame_data_ptr(frame), ptr, _sent); // Return full buffer to MTL put_frame(mtl_session, frame); } else { - sent = 0; return set_result(Result::error_shutdown); } + + sent = _sent; return set_result(Result::success); }; }; From 3bf6fce63fb1d77805ecdab6a0176c6d61be820b Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 11:47:18 +0000 Subject: [PATCH 14/27] Set state Result::configured only if Result::success --- media-proxy/src/mesh/st2110_20rx.cc | 6 +++--- media-proxy/src/mesh/st2110_20tx.cc | 6 +++--- media-proxy/src/mesh/st2110_22rx.cc | 6 +++--- media-proxy/src/mesh/st2110_22tx.cc | 6 +++--- media-proxy/src/mesh/st2110_30rx.cc | 6 +++--- media-proxy/src/mesh/st2110_30tx.cc | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index c91a16a6c..9840aa70a 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -26,10 +26,10 @@ Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { - set_state(ctx, State::not_configured); + set_state(ctx, State::not_configured); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) return set_result(Result::error_bad_argument); - } if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index 691e4c6e0..d6c074982 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -26,10 +26,10 @@ Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { - set_state(ctx, State::not_configured); + set_state(ctx, State::not_configured); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) return set_result(Result::error_bad_argument); - } if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index ef2323e58..08eed2d6d 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -26,10 +26,10 @@ Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { - set_state(ctx, State::not_configured); + set_state(ctx, State::not_configured); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) return set_result(Result::error_bad_argument); - } if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index 4eb1ec8f5..73f22eeb9 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -26,10 +26,10 @@ Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video) { - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { - set_state(ctx, State::not_configured); + set_state(ctx, State::not_configured); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) return set_result(Result::error_bad_argument); - } if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 1fa416063..4ef7eb348 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -26,10 +26,10 @@ Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio) { - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { - set_state(ctx, State::not_configured); + set_state(ctx, State::not_configured); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) return set_result(Result::error_bad_argument); - } if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index fee869b95..f6b9e15f0 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -26,10 +26,10 @@ Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio) { - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { - set_state(ctx, State::not_configured); + set_state(ctx, State::not_configured); + + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) return set_result(Result::error_bad_argument); - } if (configure_common(ctx, dev_port, cfg_st2110)) return set_result(Result::error_bad_argument); From 2bfc5b4859fb0d5f63f4dc5b05cec6d067a514a9 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 12:15:51 +0000 Subject: [PATCH 15/27] Rename variable "_sent" to "to_be_sent" --- media-proxy/include/mesh/st2110tx.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index ab4dc1211..4a3c7f188 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -104,7 +104,7 @@ template class ST2110Tx : public Result on_receive(context::Context &ctx, void *ptr, uint32_t sz, uint32_t &sent) override { - int _sent = std::min(transfer_size, sz); + int to_be_sent = std::min(transfer_size, sz); // TODO: add error/warning if sent is different than _transfer_size FRAME *frame = NULL; @@ -123,14 +123,14 @@ template class ST2110Tx : public if (frame) { // Copy data from emulated transmitter to MTL empty buffer - mtl_memcpy(get_frame_data_ptr(frame), ptr, _sent); + mtl_memcpy(get_frame_data_ptr(frame), ptr, to_be_sent); // Return full buffer to MTL put_frame(mtl_session, frame); } else { return set_result(Result::error_shutdown); } - sent = _sent; + sent = to_be_sent; return set_result(Result::success); }; }; From b29cdf8c46c90a9243dbaf841dc734cf11d36fd3 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 12:47:34 +0000 Subject: [PATCH 16/27] Format-coding applied based on updated clang-format 1. referenceAlignment: Left 2. BraceWrapping AfterFunction false --- .clang-format | 3 +- media-proxy/include/mesh/st2110.h | 16 ++--- media-proxy/include/mesh/st2110rx.h | 58 +++++++-------- media-proxy/include/mesh/st2110tx.h | 48 ++++++------- media-proxy/src/mesh/st2110.cc | 31 +++----- media-proxy/src/mesh/st2110_20rx.cc | 19 ++--- media-proxy/src/mesh/st2110_20tx.cc | 19 ++--- media-proxy/src/mesh/st2110_22rx.cc | 19 ++--- media-proxy/src/mesh/st2110_22tx.cc | 19 ++--- media-proxy/src/mesh/st2110_30rx.cc | 19 ++--- media-proxy/src/mesh/st2110_30tx.cc | 19 ++--- media-proxy/tests/st2110_tests.cc | 106 ++++++++++------------------ 12 files changed, 145 insertions(+), 231 deletions(-) diff --git a/.clang-format b/.clang-format index 46a72ccbf..88b118d2d 100644 --- a/.clang-format +++ b/.clang-format @@ -7,10 +7,11 @@ BreakBeforeBraces: Custom BraceWrapping: AfterClass: false AfterNamespace: false - AfterFunction: true + AfterFunction: false AllowShortIfStatementsOnASingleLine: false AllowShortEnumsOnASingleLine: false IndentCaseLabels: false IncludeBlocks: Preserve SortIncludes: false +ReferenceAlignment: Left ... diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index d93d0ff6a..b8e08af69 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -25,23 +25,23 @@ namespace mesh::connection { */ class ST2110 : public Connection { public: - static int mesh_video_format_to_st_format(int fmt, st_frame_fmt &st_fmt); - static int mesh_audio_format_to_st_format(int fmt, st30_fmt &st_fmt); - static int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling &st_sampling); - static int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime &st_ptime); + static int mesh_video_format_to_st_format(int fmt, st_frame_fmt& st_fmt); + static int mesh_audio_format_to_st_format(int fmt, st30_fmt& st_fmt); + static int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling); + static int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime); static void *get_frame_data_ptr(st_frame *src); static void *get_frame_data_ptr(st30_frame *src); - static void get_mtl_dev_params(mtl_init_params &st_param, const std::string &dev_port, + static void get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port, mtl_log_level log_level, const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); - static mtl_handle get_mtl_device(const std::string &dev_port, mtl_log_level log_level, + static mtl_handle get_mtl_device(const std::string& dev_port, mtl_log_level log_level, const char local_ip_addr[MESH_IP_ADDRESS_SIZE], - int &session_id); + int& session_id); ST2110() : mtl_device(nullptr) {}; - virtual ~ST2110(){}; + virtual ~ST2110() {}; protected: static int frame_available_cb(void *ptr); diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index f217b2b8b..40f68aab9 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -15,8 +15,7 @@ template class ST2110Rx : public public: ST2110Rx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::receiver; } - ~ST2110Rx() - { + ~ST2110Rx() { shutdown(_ctx); if (ops.name) free((void *)ops.name); @@ -34,9 +33,8 @@ template class ST2110Rx : public virtual HANDLE create_session(mtl_handle, OPS *) = 0; virtual int close_session(HANDLE) = 0; - int configure_common(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110) - { + int configure_common(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110) { int session_id = 0; mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); if (mtl_device == nullptr) { @@ -61,26 +59,22 @@ template class ST2110Rx : public ops.priv = this; // app handle register to lib ops.notify_frame_available = frame_available_cb; - log::info("ST2110Rx: configure") - ("port", ops.port.port[MTL_PORT_P]) - ("ip_addr", std::to_string(ops.port.ip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][3])) - ("mcast_sip_addr", std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][3])) - ("num_port", ops.port.num_port) - ("udp_port", ops.port.udp_port[MTL_PORT_P]) - ("name", ops.name) - ("framebuff_cnt", ops.framebuff_cnt); + log::info("ST2110Rx: configure")("port", ops.port.port[MTL_PORT_P])( + "ip_addr", std::to_string(ops.port.ip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][3]))( + "mcast_sip_addr", std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][3]))( + "num_port", ops.port.num_port)("udp_port", ops.port.udp_port[MTL_PORT_P])( + "name", ops.name)("framebuff_cnt", ops.framebuff_cnt); return 0; } - Result on_establish(context::Context &ctx) override - { + Result on_establish(context::Context& ctx) override { _ctx = context::WithCancel(ctx); stop = false; @@ -94,7 +88,7 @@ template class ST2110Rx : public /* Start MTL session thread. */ try { frame_thread_handle = std::jthread(&ST2110Rx::frame_thread, this); - } catch (const std::system_error &e) { + } catch (const std::system_error& e) { log::error("Failed to create thread"); set_state(ctx, State::closed); return set_result(Result::error_out_of_memory); @@ -104,8 +98,7 @@ template class ST2110Rx : public return set_result(Result::success); } - Result on_shutdown(context::Context &ctx) override - { + Result on_shutdown(context::Context& ctx) override { _ctx.cancel(); frame_thread_handle.join(); @@ -118,11 +111,10 @@ template class ST2110Rx : public return set_result(Result::success); }; - virtual void on_delete(context::Context &ctx) override {} + virtual void on_delete(context::Context& ctx) override {} private: - void frame_thread() - { + void frame_thread() { while (!_ctx.cancelled()) { // Get full buffer from MTL FRAME *frame_ptr = get_frame(mtl_session); @@ -145,8 +137,8 @@ template class ST2110Rx : public class ST2110_20Rx : public ST2110Rx { public: - Result configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + Result configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video); protected: st_frame *get_frame(st20p_rx_handle h) override; @@ -157,8 +149,8 @@ class ST2110_20Rx : public ST2110Rx { class ST2110_22Rx : public ST2110Rx { public: - Result configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + Result configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video); protected: st_frame *get_frame(st22p_rx_handle h) override; @@ -169,8 +161,8 @@ class ST2110_22Rx : public ST2110Rx { class ST2110_30Rx : public ST2110Rx { public: - Result configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); + Result configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Audio& cfg_audio); protected: st30_frame *get_frame(st30p_rx_handle h) override; diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 4a3c7f188..021d794b1 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -15,8 +15,7 @@ namespace mesh::connection { template class ST2110Tx : public ST2110 { public: ST2110Tx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::transmitter; }; - ~ST2110Tx() - { + ~ST2110Tx() { shutdown(_ctx); if (ops.name) free((void *)ops.name); @@ -33,9 +32,8 @@ template class ST2110Tx : public virtual HANDLE create_session(mtl_handle, OPS *) = 0; virtual int close_session(HANDLE) = 0; - int configure_common(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110) - { + int configure_common(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110) { int session_id = 0; mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); if (mtl_device == nullptr) { @@ -59,23 +57,19 @@ template class ST2110Tx : public ops.priv = this; // app handle register to lib ops.notify_frame_available = frame_available_cb; - log::info("ST2110Tx: configure") - ("port", ops.port.port[MTL_PORT_P]) - ("dip_addr", std::to_string(ops.port.dip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][3])) - ("num_port", ops.port.num_port) - ("udp_port", ops.port.udp_port[MTL_PORT_P]) - ("udp_src_port", ops.port.udp_src_port[MTL_PORT_P]) - ("name", ops.name) - ("framebuff_cnt", ops.framebuff_cnt); + log::info("ST2110Tx: configure")("port", ops.port.port[MTL_PORT_P])( + "dip_addr", std::to_string(ops.port.dip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][3]))( + "num_port", ops.port.num_port)("udp_port", ops.port.udp_port[MTL_PORT_P])( + "udp_src_port", ops.port.udp_src_port[MTL_PORT_P])("name", ops.name)("framebuff_cnt", + ops.framebuff_cnt); return 0; } - Result on_establish(context::Context &ctx) override - { + Result on_establish(context::Context& ctx) override { _ctx = context::WithCancel(ctx); stop = false; @@ -90,8 +84,7 @@ template class ST2110Tx : public return set_result(Result::success); }; - Result on_shutdown(context::Context &ctx) override - { + Result on_shutdown(context::Context& ctx) override { _ctx.cancel(); if (mtl_session) { @@ -102,8 +95,7 @@ template class ST2110Tx : public return set_result(Result::success); }; - Result on_receive(context::Context &ctx, void *ptr, uint32_t sz, uint32_t &sent) override - { + Result on_receive(context::Context& ctx, void *ptr, uint32_t sz, uint32_t& sent) override { int to_be_sent = std::min(transfer_size, sz); // TODO: add error/warning if sent is different than _transfer_size @@ -137,8 +129,8 @@ template class ST2110Tx : public class ST2110_20Tx : public ST2110Tx { public: - Result configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + Result configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video); protected: st_frame *get_frame(st20p_tx_handle h) override; @@ -149,8 +141,8 @@ class ST2110_20Tx : public ST2110Tx { class ST2110_22Tx : public ST2110Tx { public: - Result configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Video &cfg_video); + Result configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video); protected: st_frame *get_frame(st22p_tx_handle h) override; @@ -161,8 +153,8 @@ class ST2110_22Tx : public ST2110Tx { class ST2110_30Tx : public ST2110Tx { public: - Result configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, const MeshConfig_Audio &cfg_audio); + Result configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Audio& cfg_audio); protected: st30_frame *get_frame(st30p_tx_handle h) override; diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index 855cf93c3..581e46ee0 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -2,8 +2,7 @@ namespace mesh::connection { -int ST2110::mesh_video_format_to_st_format(int mesh_fmt, st_frame_fmt &st_fmt) -{ +int ST2110::mesh_video_format_to_st_format(int mesh_fmt, st_frame_fmt& st_fmt) { switch (mesh_fmt) { case MESH_VIDEO_PIXEL_FORMAT_NV12: st_fmt = ST_FRAME_FMT_YUV420CUSTOM8; @@ -27,8 +26,7 @@ int ST2110::mesh_video_format_to_st_format(int mesh_fmt, st_frame_fmt &st_fmt) return 0; // Success } -int ST2110::mesh_audio_format_to_st_format(int mesh_fmt, st30_fmt &st_fmt) -{ +int ST2110::mesh_audio_format_to_st_format(int mesh_fmt, st30_fmt& st_fmt) { switch (mesh_fmt) { case MESH_AUDIO_FORMAT_PCM_S8: st_fmt = ST30_FMT_PCM8; @@ -46,8 +44,7 @@ int ST2110::mesh_audio_format_to_st_format(int mesh_fmt, st30_fmt &st_fmt) return 0; // Success } -int ST2110::mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling &st_sampling) -{ +int ST2110::mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling) { switch (sampling) { case MESH_AUDIO_SAMPLE_RATE_48000: st_sampling = ST30_SAMPLING_48K; @@ -65,8 +62,7 @@ int ST2110::mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling &st_s return 0; // Success } -int ST2110::mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime &st_ptime) -{ +int ST2110::mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime) { switch (ptime) { case MESH_AUDIO_PACKET_TIME_1MS: st_ptime = ST30_PTIME_1MS; @@ -102,18 +98,15 @@ int ST2110::mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime &st_ptime) return 0; // Success } -void *ST2110::get_frame_data_ptr(st_frame *src) -{ +void *ST2110::get_frame_data_ptr(st_frame *src) { return src->addr[0]; } -void *ST2110::get_frame_data_ptr(st30_frame *src) -{ +void *ST2110::get_frame_data_ptr(st30_frame *src) { return src->addr; } -int ST2110::frame_available_cb(void *ptr) -{ +int ST2110::frame_available_cb(void *ptr) { auto _this = static_cast(ptr); if (!_this) { return -1; @@ -125,10 +118,9 @@ int ST2110::frame_available_cb(void *ptr) return 0; } -void ST2110::get_mtl_dev_params(mtl_init_params &st_param, const std::string &dev_port, +void ST2110::get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port, mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE]) -{ + const char local_ip_addr[MESH_IP_ADDRESS_SIZE]) { if (getenv("KAHAWAI_CFG_PATH") == NULL) { setenv("KAHAWAI_CFG_PATH", "/usr/local/etc/imtl.json", 0); } @@ -156,9 +148,8 @@ void ST2110::get_mtl_dev_params(mtl_init_params &st_param, const std::string &de st_param.memzone_max = 9000; } -mtl_handle ST2110::get_mtl_device(const std::string &dev_port, mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int &session_id) -{ +mtl_handle ST2110::get_mtl_device(const std::string& dev_port, mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int& session_id) { static mtl_handle dev_handle; static int _session_id; static std::mutex mtx; diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index 9840aa70a..28e49f275 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -2,30 +2,25 @@ namespace mesh::connection { -st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) -{ +st_frame *ST2110_20Rx::get_frame(st20p_rx_handle h) { return st20p_rx_get_frame(h); }; -int ST2110_20Rx::put_frame(st20p_rx_handle h, st_frame *f) -{ +int ST2110_20Rx::put_frame(st20p_rx_handle h, st_frame *f) { return st20p_rx_put_frame(h, f); }; -st20p_rx_handle ST2110_20Rx::create_session(mtl_handle h, st20p_rx_ops *o) -{ +st20p_rx_handle ST2110_20Rx::create_session(mtl_handle h, st20p_rx_ops *o) { return st20p_rx_create(h, o); }; -int ST2110_20Rx::close_session(st20p_rx_handle h) -{ +int ST2110_20Rx::close_session(st20p_rx_handle h) { return st20p_rx_free(h); }; -Result ST2110_20Rx::configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, - const MeshConfig_Video &cfg_video) -{ +Result ST2110_20Rx::configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, + const MeshConfig_Video& cfg_video) { set_state(ctx, State::not_configured); if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index d6c074982..1370e9040 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -2,30 +2,25 @@ namespace mesh::connection { -st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) -{ +st_frame *ST2110_20Tx::get_frame(st20p_tx_handle h) { return st20p_tx_get_frame(h); }; -int ST2110_20Tx::put_frame(st20p_tx_handle h, st_frame *f) -{ +int ST2110_20Tx::put_frame(st20p_tx_handle h, st_frame *f) { return st20p_tx_put_frame(h, f); }; -st20p_tx_handle ST2110_20Tx::create_session(mtl_handle h, st20p_tx_ops *o) -{ +st20p_tx_handle ST2110_20Tx::create_session(mtl_handle h, st20p_tx_ops *o) { return st20p_tx_create(h, o); }; -int ST2110_20Tx::close_session(st20p_tx_handle h) -{ +int ST2110_20Tx::close_session(st20p_tx_handle h) { return st20p_tx_free(h); }; -Result ST2110_20Tx::configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, - const MeshConfig_Video &cfg_video) -{ +Result ST2110_20Tx::configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, + const MeshConfig_Video& cfg_video) { set_state(ctx, State::not_configured); if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index 08eed2d6d..f2e6ce023 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -2,30 +2,25 @@ namespace mesh::connection { -st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) -{ +st_frame *ST2110_22Rx::get_frame(st22p_rx_handle h) { return st22p_rx_get_frame(h); }; -int ST2110_22Rx::put_frame(st22p_rx_handle h, st_frame *f) -{ +int ST2110_22Rx::put_frame(st22p_rx_handle h, st_frame *f) { return st22p_rx_put_frame(h, f); }; -st22p_rx_handle ST2110_22Rx::create_session(mtl_handle h, st22p_rx_ops *o) -{ +st22p_rx_handle ST2110_22Rx::create_session(mtl_handle h, st22p_rx_ops *o) { return st22p_rx_create(h, o); }; -int ST2110_22Rx::close_session(st22p_rx_handle h) -{ +int ST2110_22Rx::close_session(st22p_rx_handle h) { return st22p_rx_free(h); }; -Result ST2110_22Rx::configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, - const MeshConfig_Video &cfg_video) -{ +Result ST2110_22Rx::configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, + const MeshConfig_Video& cfg_video) { set_state(ctx, State::not_configured); if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index 73f22eeb9..d9c2ed671 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -2,30 +2,25 @@ namespace mesh::connection { -st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) -{ +st_frame *ST2110_22Tx::get_frame(st22p_tx_handle h) { return st22p_tx_get_frame(h); }; -int ST2110_22Tx::put_frame(st22p_tx_handle h, st_frame *f) -{ +int ST2110_22Tx::put_frame(st22p_tx_handle h, st_frame *f) { return st22p_tx_put_frame(h, f); }; -st22p_tx_handle ST2110_22Tx::create_session(mtl_handle h, st22p_tx_ops *o) -{ +st22p_tx_handle ST2110_22Tx::create_session(mtl_handle h, st22p_tx_ops *o) { return st22p_tx_create(h, o); }; -int ST2110_22Tx::close_session(st22p_tx_handle h) -{ +int ST2110_22Tx::close_session(st22p_tx_handle h) { return st22p_tx_free(h); }; -Result ST2110_22Tx::configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, - const MeshConfig_Video &cfg_video) -{ +Result ST2110_22Tx::configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, + const MeshConfig_Video& cfg_video) { set_state(ctx, State::not_configured); if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 4ef7eb348..392572237 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -2,30 +2,25 @@ namespace mesh::connection { -st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) -{ +st30_frame *ST2110_30Rx::get_frame(st30p_rx_handle h) { return st30p_rx_get_frame(h); }; -int ST2110_30Rx::put_frame(st30p_rx_handle h, st30_frame *f) -{ +int ST2110_30Rx::put_frame(st30p_rx_handle h, st30_frame *f) { return st30p_rx_put_frame(h, f); }; -st30p_rx_handle ST2110_30Rx::create_session(mtl_handle h, st30p_rx_ops *o) -{ +st30p_rx_handle ST2110_30Rx::create_session(mtl_handle h, st30p_rx_ops *o) { return st30p_rx_create(h, o); }; -int ST2110_30Rx::close_session(st30p_rx_handle h) -{ +int ST2110_30Rx::close_session(st30p_rx_handle h) { return st30p_rx_free(h); }; -Result ST2110_30Rx::configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, - const MeshConfig_Audio &cfg_audio) -{ +Result ST2110_30Rx::configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, + const MeshConfig_Audio& cfg_audio) { set_state(ctx, State::not_configured); if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index f6b9e15f0..86b6d174d 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -2,30 +2,25 @@ namespace mesh::connection { -st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) -{ +st30_frame *ST2110_30Tx::get_frame(st30p_tx_handle h) { return st30p_tx_get_frame(h); }; -int ST2110_30Tx::put_frame(st30p_tx_handle h, st30_frame *f) -{ +int ST2110_30Tx::put_frame(st30p_tx_handle h, st30_frame *f) { return st30p_tx_put_frame(h, f); }; -st30p_tx_handle ST2110_30Tx::create_session(mtl_handle h, st30p_tx_ops *o) -{ +st30p_tx_handle ST2110_30Tx::create_session(mtl_handle h, st30p_tx_ops *o) { return st30p_tx_create(h, o); }; -int ST2110_30Tx::close_session(st30p_tx_handle h) -{ +int ST2110_30Tx::close_session(st30p_tx_handle h) { return st30p_tx_free(h); }; -Result ST2110_30Tx::configure(context::Context &ctx, const std::string &dev_port, - const MeshConfig_ST2110 &cfg_st2110, - const MeshConfig_Audio &cfg_audio) -{ +Result ST2110_30Tx::configure(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110, + const MeshConfig_Audio& cfg_audio) { set_state(ctx, State::not_configured); if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index a7537f4f3..41395cc33 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -9,22 +9,19 @@ using namespace mesh; class EmulatedTransmitter : public connection::Connection { public: - EmulatedTransmitter(context::Context &ctx) - { + EmulatedTransmitter(context::Context& ctx) { _kind = connection::Kind::transmitter; set_state(ctx, connection::State::configured); } - connection::Result on_establish(context::Context &ctx) - { + connection::Result on_establish(context::Context& ctx) { set_state(ctx, connection::State::active); return connection::Result::success; } - connection::Result on_shutdown(context::Context &ctx) { return connection::Result::success; } + connection::Result on_shutdown(context::Context& ctx) { return connection::Result::success; } - connection::Result transmit_wrapper(context::Context &ctx, void *ptr, uint32_t sz) - { + connection::Result transmit_wrapper(context::Context& ctx, void *ptr, uint32_t sz) { return transmit(ctx, ptr, sz); } }; @@ -33,24 +30,21 @@ class EmulatedReceiver : public connection::Connection { public: uint32_t received_packets_lossless; uint32_t received_packets_lossy; - EmulatedReceiver(context::Context &ctx) - { + EmulatedReceiver(context::Context& ctx) { _kind = connection::Kind::receiver; received_packets_lossless = 0; received_packets_lossy = 0; set_state(ctx, connection::State::configured); } - connection::Result on_establish(context::Context &ctx) - { + connection::Result on_establish(context::Context& ctx) { set_state(ctx, connection::State::active); return connection::Result::success; } - connection::Result on_shutdown(context::Context &ctx) { return connection::Result::success; } + connection::Result on_shutdown(context::Context& ctx) { return connection::Result::success; } - connection::Result on_receive(context::Context &ctx, void *ptr, uint32_t sz, uint32_t &sent) - { + connection::Result on_receive(context::Context& ctx, void *ptr, uint32_t sz, uint32_t& sent) { if (memcmp(ptr, DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { received_packets_lossless++; } else { @@ -65,30 +59,26 @@ class EmulatedST2110_Tx : public connection::ST2110Txaddr[0] = calloc(1000, 1); memcpy(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)); return f; } - int put_frame(int *d, st_frame *f) override - { + int put_frame(int *d, st_frame *f) override { if (memcmp(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { received_packets_dummy1++; } else if (memcmp(f->addr[0], DUMMY_DATA2, sizeof(DUMMY_DATA2)) == 0) { @@ -102,8 +92,7 @@ class EmulatedST2110_Tx : public connection::ST2110Txaddr[0] = calloc(1000, 1); memcpy(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)); return f; } - int put_frame(int *d, st_frame *f) override - { + int put_frame(int *d, st_frame *f) override { if (memcmp(f->addr[0], DUMMY_DATA1, sizeof(DUMMY_DATA1)) == 0) { received_packets_dummy1++; } else if (memcmp(f->addr[0], DUMMY_DATA2, sizeof(DUMMY_DATA2)) == 0) { @@ -150,15 +135,13 @@ class EmulatedST2110_Rx : public connection::ST2110Rx Active @@ -182,8 +165,7 @@ static void validate_state_change(context::Context &ctx, connection::Connection ASSERT_EQ(c->state(), connection::State::closed); } -static void get_ST2110_session_config(MeshConfig_ST2110 &cfg_st2110, int transport) -{ +static void get_ST2110_session_config(MeshConfig_ST2110& cfg_st2110, int transport) { memcpy(cfg_st2110.local_ip_addr, "127.0.0.1", sizeof("127.0.0.1")); memcpy(cfg_st2110.remote_ip_addr, "127.0.0.1", sizeof("127.0.0.1")); cfg_st2110.local_port = 9001; @@ -191,24 +173,21 @@ static void get_ST2110_session_config(MeshConfig_ST2110 &cfg_st2110, int transpo cfg_st2110.transport = transport; } -static void get_ST2110_video_cfg(MeshConfig_Video &cfg_video) -{ +static void get_ST2110_video_cfg(MeshConfig_Video& cfg_video) { cfg_video.fps = 30; cfg_video.width = 1920; cfg_video.height = 1080; cfg_video.pixel_format = MESH_VIDEO_PIXEL_FORMAT_YUV422P10LE; } -static void get_ST2110_audio_cfg(MeshConfig_Audio &cfg_audio) -{ +static void get_ST2110_audio_cfg(MeshConfig_Audio& cfg_audio) { cfg_audio.channels = 2; cfg_audio.format = MESH_AUDIO_FORMAT_PCM_S16BE; cfg_audio.packet_time = MESH_AUDIO_PACKET_TIME_1MS; cfg_audio.sample_rate = MESH_AUDIO_SAMPLE_RATE_48000; } -TEST(st2110_tx, state_change) -{ +TEST(st2110_tx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); auto conn_tx = new EmulatedST2110_Tx; @@ -225,8 +204,7 @@ TEST(st2110_tx, state_change) delete conn_tx; } -TEST(st2110_rx, state_change) -{ +TEST(st2110_rx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); auto conn_rx = new EmulatedST2110_Rx; @@ -243,8 +221,7 @@ TEST(st2110_rx, state_change) delete conn_rx; } -TEST(DISABLED_st2110_20tx, state_change) -{ +TEST(DISABLED_st2110_20tx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); auto conn_rx = new connection::ST2110_20Tx; @@ -269,8 +246,7 @@ TEST(DISABLED_st2110_20tx, state_change) delete conn_rx; } -TEST(DISABLED_st2110_22tx, state_change) -{ +TEST(DISABLED_st2110_22tx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); auto conn_rx = new connection::ST2110_22Tx; @@ -295,8 +271,7 @@ TEST(DISABLED_st2110_22tx, state_change) delete conn_rx; } -TEST(DISABLED_st2110_30tx, state_change) -{ +TEST(DISABLED_st2110_30tx, state_change) { auto ctx = context::WithCancel(mesh::context::Background()); auto conn_rx = new connection::ST2110_30Tx; @@ -326,8 +301,7 @@ TEST(DISABLED_st2110_30tx, state_change) delete conn_rx; } -TEST(st2110_tx, send_data) -{ +TEST(st2110_tx, send_data) { auto ctx = context::WithCancel(mesh::context::Background()); connection::Result res; @@ -367,8 +341,7 @@ TEST(st2110_tx, send_data) delete conn_tx; } -TEST(st2110_rx, get_data) -{ +TEST(st2110_rx, get_data) { auto ctx = context::WithCancel(mesh::context::Background()); connection::Result res; @@ -404,8 +377,7 @@ TEST(st2110_rx, get_data) } /************************** */ -static void tx_thread(context::Context &ctx, connection::Connection *conn_tx) -{ +static void tx_thread(context::Context& ctx, connection::Connection *conn_tx) { auto emulated_tx = new EmulatedTransmitter(ctx); // Change state Configured -> Active connection::Result res = conn_tx->establish(ctx); @@ -433,8 +405,7 @@ static void tx_thread(context::Context &ctx, connection::Connection *conn_tx) delete emulated_tx; } -static void rx_thread(context::Context &ctx, connection::Connection *conn_rx, bool is_lossless) -{ +static void rx_thread(context::Context& ctx, connection::Connection *conn_rx, bool is_lossless) { auto emulated_rx = new EmulatedReceiver(ctx); emulated_rx->establish(ctx); @@ -463,8 +434,7 @@ static void rx_thread(context::Context &ctx, connection::Connection *conn_rx, bo delete emulated_rx; } -TEST(DISABLED_st2110_20, send_and_receive_data) -{ +TEST(DISABLED_st2110_20, send_and_receive_data) { auto ctx = context::WithCancel(mesh::context::Background()); connection::Result res; @@ -492,7 +462,7 @@ TEST(DISABLED_st2110_20, send_and_receive_data) try { rx_th = std::jthread(&rx_thread, std::ref(ctx), conn_rx, 1); tx_th = std::jthread(&tx_thread, std::ref(ctx), conn_tx); - } catch (const std::system_error &e) { + } catch (const std::system_error& e) { ASSERT_TRUE(0) << e.what(); } @@ -503,8 +473,7 @@ TEST(DISABLED_st2110_20, send_and_receive_data) delete conn_rx; } -TEST(DISABLED_st2110_22, send_and_receive_data) -{ +TEST(DISABLED_st2110_22, send_and_receive_data) { auto ctx = context::WithCancel(mesh::context::Background()); connection::Result res; @@ -532,7 +501,7 @@ TEST(DISABLED_st2110_22, send_and_receive_data) try { rx_th = std::jthread(&rx_thread, std::ref(ctx), conn_rx, 0); tx_th = std::jthread(&tx_thread, std::ref(ctx), conn_tx); - } catch (const std::system_error &e) { + } catch (const std::system_error& e) { ASSERT_TRUE(0) << e.what(); } @@ -543,8 +512,7 @@ TEST(DISABLED_st2110_22, send_and_receive_data) delete conn_rx; } -TEST(DISABLED_st2110_30, send_and_receive_data) -{ +TEST(DISABLED_st2110_30, send_and_receive_data) { auto ctx = context::WithCancel(mesh::context::Background()); connection::Result res; @@ -572,7 +540,7 @@ TEST(DISABLED_st2110_30, send_and_receive_data) try { rx_th = std::jthread(&rx_thread, std::ref(ctx), conn_rx, 1); tx_th = std::jthread(&tx_thread, std::ref(ctx), conn_tx); - } catch (const std::system_error &e) { + } catch (const std::system_error& e) { ASSERT_TRUE(0) << e.what(); } From db06c5a111e73f5a2d057234e97375c6071e046e Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 19:45:43 +0000 Subject: [PATCH 17/27] Fix log message --- media-proxy/src/mesh/st2110_30rx.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index 392572237..fb6713bd6 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -41,7 +41,7 @@ Result ST2110_30Rx::configure(context::Context& ctx, const std::string& dev_port if (mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) return set_result(Result::error_bad_argument); - log::info("ST2110_22Rx: configure") + log::info("ST2110_30Rx: configure") ("payload_type", (int)ops.port.payload_type) ("audio_fmt", ops.fmt) ("audio_chan", ops.channel) From 2dd4cd38cdedb1a6c646718acb9119054f97b490 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 26 Nov 2024 19:46:40 +0000 Subject: [PATCH 18/27] Use only atomic for synchronization --- media-proxy/include/mesh/st2110.h | 2 -- media-proxy/include/mesh/st2110rx.h | 8 +++----- media-proxy/include/mesh/st2110tx.h | 17 +++++++---------- media-proxy/src/mesh/st2110.cc | 6 +++--- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index b8e08af69..3b742e182 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -48,8 +48,6 @@ class ST2110 : public Connection { mtl_handle mtl_device; std::atomic stop; - std::condition_variable_any cv; - std::mutex mx; }; } // namespace mesh::connection diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 40f68aab9..b2d19a970 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -100,6 +100,8 @@ template class ST2110Rx : public Result on_shutdown(context::Context& ctx) override { _ctx.cancel(); + stop = true; + stop.notify_all(); frame_thread_handle.join(); @@ -119,12 +121,8 @@ template class ST2110Rx : public // Get full buffer from MTL FRAME *frame_ptr = get_frame(mtl_session); if (!frame_ptr) { /* no frame */ - std::unique_lock lk(mx); - cv.wait(lk, _ctx.stop_token(), [this] { return stop.load(); }); + stop.wait(false); stop = false; - if (_ctx.cancelled()) { - return; - } continue; } // Forward buffer to emulated receiver diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 021d794b1..780ecdfa4 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -86,6 +86,8 @@ template class ST2110Tx : public Result on_shutdown(context::Context& ctx) override { _ctx.cancel(); + stop = true; + stop.notify_all(); if (mtl_session) { close_session(mtl_session); @@ -104,8 +106,7 @@ template class ST2110Tx : public // Get empty buffer from MTL frame = get_frame(mtl_session); if (!frame) { - std::unique_lock lk(mx); - cv.wait(lk, _ctx.stop_token(), [this] { return stop.load(); }); + stop.wait(false); stop = false; if (_ctx.cancelled()) { return set_result(Result::error_shutdown); @@ -113,14 +114,10 @@ template class ST2110Tx : public } } while (!frame); - if (frame) { - // Copy data from emulated transmitter to MTL empty buffer - mtl_memcpy(get_frame_data_ptr(frame), ptr, to_be_sent); - // Return full buffer to MTL - put_frame(mtl_session, frame); - } else { - return set_result(Result::error_shutdown); - } + // Copy data from emulated transmitter to MTL empty buffer + mtl_memcpy(get_frame_data_ptr(frame), ptr, to_be_sent); + // Return full buffer to MTL + put_frame(mtl_session, frame); sent = to_be_sent; return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index 581e46ee0..8bc7ae352 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -111,9 +111,9 @@ int ST2110::frame_available_cb(void *ptr) { if (!_this) { return -1; } - std::unique_lock lk(_this->mx); - _this->stop.store(true); - _this->cv.notify_all(); + + _this->stop = true; + _this->stop.notify_all(); return 0; } From 8d01718eff9cdc094a066a6ed81a6cac70722908 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Wed, 27 Nov 2024 11:39:01 +0000 Subject: [PATCH 19/27] Rename error message --- media-proxy/include/mesh/conn.h | 2 +- media-proxy/include/mesh/st2110tx.h | 2 +- media-proxy/src/mesh/conn.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/media-proxy/include/mesh/conn.h b/media-proxy/include/mesh/conn.h index d2904263c..29f863450 100644 --- a/media-proxy/include/mesh/conn.h +++ b/media-proxy/include/mesh/conn.h @@ -66,7 +66,7 @@ enum class Result { error_bad_argument, error_out_of_memory, error_general_failure, - error_shutdown, + error_context_cancelled, // TODO: more error codes to be added... }; diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 780ecdfa4..b1cad69b2 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -109,7 +109,7 @@ template class ST2110Tx : public stop.wait(false); stop = false; if (_ctx.cancelled()) { - return set_result(Result::error_shutdown); + return set_result(Result::error_context_cancelled); } } } while (!frame); diff --git a/media-proxy/src/mesh/conn.cc b/media-proxy/src/mesh/conn.cc index 308fab981..186e4e114 100644 --- a/media-proxy/src/mesh/conn.cc +++ b/media-proxy/src/mesh/conn.cc @@ -274,7 +274,7 @@ const char * result2str(Result res) case Result::error_bad_argument: return "bad argument"; case Result::error_out_of_memory: return "out of memory"; case Result::error_general_failure: return "general failure"; - case Result::error_shutdown: return "processing shutdown"; + case Result::error_context_cancelled: return "context cancelled"; default: return str_unknown; } } From 80abcb08296c33f75c6084e1ba81681c20efa633 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Wed, 27 Nov 2024 11:43:34 +0000 Subject: [PATCH 20/27] Move varaibles to default initializers --- media-proxy/include/mesh/st2110.h | 3 +-- media-proxy/include/mesh/st2110rx.h | 8 ++++---- media-proxy/include/mesh/st2110tx.h | 8 ++++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 3b742e182..0db460a74 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -40,13 +40,12 @@ class ST2110 : public Connection { const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int& session_id); - ST2110() : mtl_device(nullptr) {}; virtual ~ST2110() {}; protected: static int frame_available_cb(void *ptr); - mtl_handle mtl_device; + mtl_handle mtl_device = nullptr; std::atomic stop; }; diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index b2d19a970..f5a5a14ff 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -13,7 +13,7 @@ namespace mesh::connection { */ template class ST2110Rx : public ST2110 { public: - ST2110Rx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::receiver; } + ST2110Rx() { _kind = Kind::receiver; } ~ST2110Rx() { shutdown(_ctx); @@ -22,9 +22,9 @@ template class ST2110Rx : public } protected: - HANDLE mtl_session; - OPS ops; - size_t transfer_size; + HANDLE mtl_session = nullptr; + OPS ops = {0}; + size_t transfer_size = 0; std::jthread frame_thread_handle; context::Context _ctx = context::WithCancel(context::Background()); diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index b1cad69b2..5a5007f34 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -14,7 +14,7 @@ namespace mesh::connection { */ template class ST2110Tx : public ST2110 { public: - ST2110Tx() : mtl_session(nullptr), ops{0}, transfer_size(0) { _kind = Kind::transmitter; }; + ST2110Tx() { _kind = Kind::transmitter; }; ~ST2110Tx() { shutdown(_ctx); if (ops.name) @@ -22,9 +22,9 @@ template class ST2110Tx : public }; protected: - HANDLE mtl_session; - OPS ops; - uint32_t transfer_size; + HANDLE mtl_session = nullptr; + OPS ops = {0}; + uint32_t transfer_size = 0; context::Context _ctx = context::WithCancel(context::Background()); virtual FRAME *get_frame(HANDLE) = 0; From 565e3fe6e4623bfcdc40d46ea91544a7049aa8d4 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Wed, 27 Nov 2024 11:55:26 +0000 Subject: [PATCH 21/27] Adjust code formatting --- media-proxy/include/mesh/st2110rx.h | 27 +++++++++++++++------------ media-proxy/include/mesh/st2110tx.h | 21 ++++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index f5a5a14ff..565a905fb 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -37,7 +37,7 @@ template class ST2110Rx : public const MeshConfig_ST2110& cfg_st2110) { int session_id = 0; mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); - if (mtl_device == nullptr) { + if (!mtl_device) { log::error("Failed to get MTL device"); return -1; } @@ -59,17 +59,20 @@ template class ST2110Rx : public ops.priv = this; // app handle register to lib ops.notify_frame_available = frame_available_cb; - log::info("ST2110Rx: configure")("port", ops.port.port[MTL_PORT_P])( - "ip_addr", std::to_string(ops.port.ip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][3]))( - "mcast_sip_addr", std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][3]))( - "num_port", ops.port.num_port)("udp_port", ops.port.udp_port[MTL_PORT_P])( - "name", ops.name)("framebuff_cnt", ops.framebuff_cnt); + log::info("ST2110Rx: configure") + ("port", ops.port.port[MTL_PORT_P]) + ("ip_addr", std::to_string(ops.port.ip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.ip_addr[MTL_PORT_P][3])) + ("mcast_sip_addr", std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][3])) + ("num_port", ops.port.num_port) + ("udp_port", ops.port.udp_port[MTL_PORT_P]) + ("name", ops.name) + ("framebuff_cnt", ops.framebuff_cnt); return 0; } diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 5a5007f34..6c553513f 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -36,7 +36,7 @@ template class ST2110Tx : public const MeshConfig_ST2110& cfg_st2110) { int session_id = 0; mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); - if (mtl_device == nullptr) { + if (!mtl_device) { log::error("Failed to get MTL device"); return -1; } @@ -57,14 +57,17 @@ template class ST2110Tx : public ops.priv = this; // app handle register to lib ops.notify_frame_available = frame_available_cb; - log::info("ST2110Tx: configure")("port", ops.port.port[MTL_PORT_P])( - "dip_addr", std::to_string(ops.port.dip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][3]))( - "num_port", ops.port.num_port)("udp_port", ops.port.udp_port[MTL_PORT_P])( - "udp_src_port", ops.port.udp_src_port[MTL_PORT_P])("name", ops.name)("framebuff_cnt", - ops.framebuff_cnt); + log::info("ST2110Tx: configure") + ("port", ops.port.port[MTL_PORT_P]) + ("dip_addr", std::to_string(ops.port.dip_addr[MTL_PORT_P][0]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][1]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][2]) + " " + + std::to_string(ops.port.dip_addr[MTL_PORT_P][3])) + ("num_port", ops.port.num_port) + ("udp_port", ops.port.udp_port[MTL_PORT_P]) + ("udp_src_port", ops.port.udp_src_port[MTL_PORT_P]) + ("name", ops.name) + ("framebuff_cnt", ops.framebuff_cnt); return 0; } From e8a44f537c4a85be49fe9f96a04cb2539cf2eaf6 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Wed, 27 Nov 2024 12:49:10 +0000 Subject: [PATCH 22/27] Fix synchronization based on std::atomic --- media-proxy/include/mesh/st2110.h | 5 ++++- media-proxy/include/mesh/st2110rx.h | 20 +++++++++----------- media-proxy/include/mesh/st2110tx.h | 8 +++----- media-proxy/src/mesh/st2110.cc | 28 ++++++++++++++++++++++++++-- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 0db460a74..0990c9af1 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -44,9 +44,12 @@ class ST2110 : public Connection { protected: static int frame_available_cb(void *ptr); + void init_frame_available(); + void notify_frame_available(); + void wait_frame_available(); mtl_handle mtl_device = nullptr; - std::atomic stop; + std::atomic frame_available; }; } // namespace mesh::connection diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 565a905fb..909228777 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -79,7 +79,7 @@ template class ST2110Rx : public Result on_establish(context::Context& ctx) override { _ctx = context::WithCancel(ctx); - stop = false; + init_frame_available(); mtl_session = create_session(mtl_device, &ops); if (!mtl_session) { @@ -103,8 +103,7 @@ template class ST2110Rx : public Result on_shutdown(context::Context& ctx) override { _ctx.cancel(); - stop = true; - stop.notify_all(); + notify_frame_available(); frame_thread_handle.join(); @@ -123,15 +122,14 @@ template class ST2110Rx : public while (!_ctx.cancelled()) { // Get full buffer from MTL FRAME *frame_ptr = get_frame(mtl_session); - if (!frame_ptr) { /* no frame */ - stop.wait(false); - stop = false; - continue; + if (frame_ptr) { + // Forward buffer to emulated receiver + transmit(_ctx, get_frame_data_ptr(frame_ptr), transfer_size); + // Return used buffer to MTL + put_frame(mtl_session, frame_ptr); + } else { + wait_frame_available(); } - // Forward buffer to emulated receiver - transmit(_ctx, get_frame_data_ptr(frame_ptr), transfer_size); - // Return used buffer to MTL - put_frame(mtl_session, frame_ptr); } } }; diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 6c553513f..18090b9f2 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -74,7 +74,7 @@ template class ST2110Tx : public Result on_establish(context::Context& ctx) override { _ctx = context::WithCancel(ctx); - stop = false; + init_frame_available(); mtl_session = create_session(mtl_device, &ops); if (!mtl_session) { @@ -89,8 +89,7 @@ template class ST2110Tx : public Result on_shutdown(context::Context& ctx) override { _ctx.cancel(); - stop = true; - stop.notify_all(); + notify_frame_available(); if (mtl_session) { close_session(mtl_session); @@ -109,8 +108,7 @@ template class ST2110Tx : public // Get empty buffer from MTL frame = get_frame(mtl_session); if (!frame) { - stop.wait(false); - stop = false; + wait_frame_available(); if (_ctx.cancelled()) { return set_result(Result::error_context_cancelled); } diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index 8bc7ae352..c1ed34ea9 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -106,14 +106,38 @@ void *ST2110::get_frame_data_ptr(st30_frame *src) { return src->addr; } +/** + * Use this function in on_establish() to initialize frame_available flag. + */ +void ST2110::init_frame_available() { + frame_available = false; +} + +/** + * Use this function in on_shutdown() and frame_available_cb() to properly notify another thread + * + */ +void ST2110::notify_frame_available() { + frame_available.store(true, std::memory_order_release); + frame_available.notify_one(); +} + +/** + * Use this function in on_receive() to wait for notification + * + */ +void ST2110::wait_frame_available() { + frame_available.wait(false, std::memory_order_acquire); + frame_available = false; +} + int ST2110::frame_available_cb(void *ptr) { auto _this = static_cast(ptr); if (!_this) { return -1; } - _this->stop = true; - _this->stop.notify_all(); + _this->notify_frame_available(); return 0; } From c628077246c30c886ac7fb60ce319e8885e9e2ed Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Wed, 27 Nov 2024 13:36:13 +0000 Subject: [PATCH 23/27] Fix setting state prematurely --- media-proxy/src/mesh/st2110_20rx.cc | 18 ++++++++++++------ media-proxy/src/mesh/st2110_20tx.cc | 18 ++++++++++++------ media-proxy/src/mesh/st2110_22rx.cc | 18 ++++++++++++------ media-proxy/src/mesh/st2110_22tx.cc | 18 ++++++++++++------ media-proxy/src/mesh/st2110_30rx.cc | 26 ++++++++++++++++++-------- media-proxy/src/mesh/st2110_30tx.cc | 26 ++++++++++++++++++-------- 6 files changed, 84 insertions(+), 40 deletions(-) diff --git a/media-proxy/src/mesh/st2110_20rx.cc b/media-proxy/src/mesh/st2110_20rx.cc index 28e49f275..992786dd0 100644 --- a/media-proxy/src/mesh/st2110_20rx.cc +++ b/media-proxy/src/mesh/st2110_20rx.cc @@ -21,13 +21,15 @@ int ST2110_20Rx::close_session(st20p_rx_handle h) { Result ST2110_20Rx::configure(context::Context& ctx, const std::string& dev_port, const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video) { - set_state(ctx, State::not_configured); - - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (configure_common(ctx, dev_port, cfg_st2110)) + if (configure_common(ctx, dev_port, cfg_st2110)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; ops.width = cfg_video.width; @@ -35,14 +37,18 @@ Result ST2110_20Rx::configure(context::Context& ctx, const std::string& dev_port ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; - if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.output_fmt)) + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.output_fmt)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.device = ST_PLUGIN_DEVICE_AUTO; transfer_size = st_frame_size(ops.output_fmt, ops.width, ops.height, false); - if (transfer_size == 0) + if (transfer_size == 0) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } log::info("ST2110_20Rx: configure") ("payload_type", (int)ops.port.payload_type) diff --git a/media-proxy/src/mesh/st2110_20tx.cc b/media-proxy/src/mesh/st2110_20tx.cc index 1370e9040..44efac0c0 100644 --- a/media-proxy/src/mesh/st2110_20tx.cc +++ b/media-proxy/src/mesh/st2110_20tx.cc @@ -21,13 +21,15 @@ int ST2110_20Tx::close_session(st20p_tx_handle h) { Result ST2110_20Tx::configure(context::Context& ctx, const std::string& dev_port, const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video) { - set_state(ctx, State::not_configured); - - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_20) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (configure_common(ctx, dev_port, cfg_st2110)) + if (configure_common(ctx, dev_port, cfg_st2110)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST20; ops.width = cfg_video.width; @@ -35,8 +37,10 @@ Result ST2110_20Tx::configure(context::Context& ctx, const std::string& dev_port ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); ops.transport_fmt = ST20_FMT_YUV_422_PLANAR10LE; - if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.input_fmt)) + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.input_fmt)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.device = ST_PLUGIN_DEVICE_AUTO; @@ -50,8 +54,10 @@ Result ST2110_20Tx::configure(context::Context& ctx, const std::string& dev_port ("device", ops.device); transfer_size = st_frame_size(ops.input_fmt, ops.width, ops.height, false); - if (transfer_size == 0) + if (transfer_size == 0) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_22rx.cc b/media-proxy/src/mesh/st2110_22rx.cc index f2e6ce023..2eda00d25 100644 --- a/media-proxy/src/mesh/st2110_22rx.cc +++ b/media-proxy/src/mesh/st2110_22rx.cc @@ -21,21 +21,25 @@ int ST2110_22Rx::close_session(st22p_rx_handle h) { Result ST2110_22Rx::configure(context::Context& ctx, const std::string& dev_port, const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video) { - set_state(ctx, State::not_configured); - - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (configure_common(ctx, dev_port, cfg_st2110)) + if (configure_common(ctx, dev_port, cfg_st2110)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.output_fmt)) + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.output_fmt)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.device = ST_PLUGIN_DEVICE_AUTO; ops.pack_type = ST22_PACK_CODESTREAM; @@ -52,8 +56,10 @@ Result ST2110_22Rx::configure(context::Context& ctx, const std::string& dev_port ("device", ops.device); transfer_size = st_frame_size(ops.output_fmt, ops.width, ops.height, false); - if (transfer_size == 0) + if (transfer_size == 0) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_22tx.cc b/media-proxy/src/mesh/st2110_22tx.cc index d9c2ed671..1f34c8d59 100644 --- a/media-proxy/src/mesh/st2110_22tx.cc +++ b/media-proxy/src/mesh/st2110_22tx.cc @@ -21,21 +21,25 @@ int ST2110_22Tx::close_session(st22p_tx_handle h) { Result ST2110_22Tx::configure(context::Context& ctx, const std::string& dev_port, const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Video& cfg_video) { - set_state(ctx, State::not_configured); - - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_22) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (configure_common(ctx, dev_port, cfg_st2110)) + if (configure_common(ctx, dev_port, cfg_st2110)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST22; ops.width = cfg_video.width; ops.height = cfg_video.height; ops.fps = st_frame_rate_to_st_fps(cfg_video.fps); - if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.input_fmt)) + if (mesh_video_format_to_st_format(cfg_video.pixel_format, ops.input_fmt)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.device = ST_PLUGIN_DEVICE_AUTO; ops.pack_type = ST22_PACK_CODESTREAM; @@ -53,8 +57,10 @@ Result ST2110_22Tx::configure(context::Context& ctx, const std::string& dev_port ("device", ops.device); transfer_size = st_frame_size(ops.input_fmt, ops.width, ops.height, false); - if (transfer_size == 0) + if (transfer_size == 0) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_30rx.cc b/media-proxy/src/mesh/st2110_30rx.cc index fb6713bd6..1d8a6b591 100644 --- a/media-proxy/src/mesh/st2110_30rx.cc +++ b/media-proxy/src/mesh/st2110_30rx.cc @@ -21,25 +21,33 @@ int ST2110_30Rx::close_session(st30p_rx_handle h) { Result ST2110_30Rx::configure(context::Context& ctx, const std::string& dev_port, const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Audio& cfg_audio) { - set_state(ctx, State::not_configured); - - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (configure_common(ctx, dev_port, cfg_st2110)) + if (configure_common(ctx, dev_port, cfg_st2110)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; - if (mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) + if (mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.channel = cfg_audio.channels; - if (mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) + if (mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) + if (mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } log::info("ST2110_30Rx: configure") ("payload_type", (int)ops.port.payload_type) @@ -50,8 +58,10 @@ Result ST2110_30Rx::configure(context::Context& ctx, const std::string& dev_port ops.framebuff_size = transfer_size = st30_get_packet_size(ops.fmt, ops.ptime, ops.sampling, ops.channel); - if (transfer_size == 0) + if (transfer_size == 0) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } set_state(ctx, State::configured); return set_result(Result::success); diff --git a/media-proxy/src/mesh/st2110_30tx.cc b/media-proxy/src/mesh/st2110_30tx.cc index 86b6d174d..03debbb9a 100644 --- a/media-proxy/src/mesh/st2110_30tx.cc +++ b/media-proxy/src/mesh/st2110_30tx.cc @@ -21,25 +21,33 @@ int ST2110_30Tx::close_session(st30p_tx_handle h) { Result ST2110_30Tx::configure(context::Context& ctx, const std::string& dev_port, const MeshConfig_ST2110& cfg_st2110, const MeshConfig_Audio& cfg_audio) { - set_state(ctx, State::not_configured); - - if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) + if (cfg_st2110.transport != MESH_CONN_TRANSPORT_ST2110_30) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (configure_common(ctx, dev_port, cfg_st2110)) + if (configure_common(ctx, dev_port, cfg_st2110)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.port.payload_type = ST_APP_PAYLOAD_TYPE_ST30; - if (mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) + if (mesh_audio_format_to_st_format(cfg_audio.format, ops.fmt)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } ops.channel = cfg_audio.channels; - if (mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) + if (mesh_audio_sampling_to_st_sampling(cfg_audio.sample_rate, ops.sampling)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } - if (mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) + if (mesh_audio_ptime_to_st_ptime(cfg_audio.packet_time, ops.ptime)) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } log::info("ST2110_30Tx: configure") ("payload_type", (int)ops.port.payload_type) @@ -50,8 +58,10 @@ Result ST2110_30Tx::configure(context::Context& ctx, const std::string& dev_port ops.framebuff_size = transfer_size = st30_get_packet_size(ops.fmt, ops.ptime, ops.sampling, ops.channel); - if (transfer_size == 0) + if (transfer_size == 0) { + set_state(ctx, State::not_configured); return set_result(Result::error_bad_argument); + } set_state(ctx, State::configured); return set_result(Result::success); From fa712dc16befae50d4ec27da63bd3c6b5d66da05 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Wed, 27 Nov 2024 17:19:10 +0100 Subject: [PATCH 24/27] Reorder loop Co-authored-by: Konstantin Ilichev <2067613@gmail.com> Signed-off-by: Tomasz --- media-proxy/include/mesh/st2110tx.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index 18090b9f2..d3715ea9e 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -103,17 +103,18 @@ template class ST2110Tx : public int to_be_sent = std::min(transfer_size, sz); // TODO: add error/warning if sent is different than _transfer_size - FRAME *frame = NULL; - do { + FRAME *frame; + for (;;) { + if (ctx.cancelled() || _ctx.cancelled()) + return set_result(Result::error_context_cancelled); + // Get empty buffer from MTL frame = get_frame(mtl_session); - if (!frame) { - wait_frame_available(); - if (_ctx.cancelled()) { - return set_result(Result::error_context_cancelled); - } - } - } while (!frame); + if (frame) + break; + + wait_frame_available(); + } // Copy data from emulated transmitter to MTL empty buffer mtl_memcpy(get_frame_data_ptr(frame), ptr, to_be_sent); From ee85085c987db569faa2a3b093a2063e21c7b2c5 Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Fri, 29 Nov 2024 08:13:17 +0000 Subject: [PATCH 25/27] Remove redundant mesh:: namespace --- media-proxy/tests/st2110_tests.cc | 76 +++++++++++++++---------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index 41395cc33..cc20c45f8 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -146,22 +146,22 @@ static void validate_state_change(context::Context& ctx, connection::Connection // Change state Configured -> Active res = c->establish(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(c->state(), connection::State::active); // Change state Active -> Suspended res = c->suspend(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(c->state(), connection::State::suspended); // Change state Suspended -> Active res = c->resume(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(c->state(), connection::State::active); // Change state Active -> Closed res = c->shutdown(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(c->state(), connection::State::closed); } @@ -188,7 +188,7 @@ static void get_ST2110_audio_cfg(MeshConfig_Audio& cfg_audio) { } TEST(st2110_tx, state_change) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); auto conn_tx = new EmulatedST2110_Tx; ASSERT_EQ(conn_tx->kind(), connection::Kind::transmitter); @@ -196,7 +196,7 @@ TEST(st2110_tx, state_change) { // Change state: Not Configured -> Configured connection::Result res = conn_tx->configure(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::configured); validate_state_change(ctx, dynamic_cast(conn_tx)); @@ -205,7 +205,7 @@ TEST(st2110_tx, state_change) { } TEST(st2110_rx, state_change) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); auto conn_rx = new EmulatedST2110_Rx; ASSERT_EQ(conn_rx->kind(), connection::Kind::receiver); @@ -213,7 +213,7 @@ TEST(st2110_rx, state_change) { // Change state: Not Configured -> Configured connection::Result res = conn_rx->configure(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); validate_state_change(ctx, dynamic_cast(conn_rx)); @@ -222,7 +222,7 @@ TEST(st2110_rx, state_change) { } TEST(DISABLED_st2110_20tx, state_change) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); auto conn_rx = new connection::ST2110_20Tx; ASSERT_EQ(conn_rx->kind(), connection::Kind::transmitter); @@ -237,7 +237,7 @@ TEST(DISABLED_st2110_20tx, state_change) { // Change state: Not Configured -> Configured std::string dev_port("kernel:lo"); connection::Result res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); validate_state_change(ctx, dynamic_cast(conn_rx)); @@ -247,7 +247,7 @@ TEST(DISABLED_st2110_20tx, state_change) { } TEST(DISABLED_st2110_22tx, state_change) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); auto conn_rx = new connection::ST2110_22Tx; ASSERT_EQ(conn_rx->kind(), connection::Kind::transmitter); @@ -262,7 +262,7 @@ TEST(DISABLED_st2110_22tx, state_change) { // Change state: Not Configured -> Configured std::string dev_port("kernel:lo"); connection::Result res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); validate_state_change(ctx, dynamic_cast(conn_rx)); @@ -272,7 +272,7 @@ TEST(DISABLED_st2110_22tx, state_change) { } TEST(DISABLED_st2110_30tx, state_change) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); auto conn_rx = new connection::ST2110_30Tx; ASSERT_EQ(conn_rx->kind(), connection::Kind::transmitter); @@ -287,14 +287,14 @@ TEST(DISABLED_st2110_30tx, state_change) { // Change state: Not Configured -> Configured std::string dev_port("kernel:lo"); connection::Result res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_audio); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); validate_state_change(ctx, dynamic_cast(conn_rx)); // Change state: Closed -> Configured res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_audio); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); validate_state_change(ctx, dynamic_cast(conn_rx)); @@ -302,7 +302,7 @@ TEST(DISABLED_st2110_30tx, state_change) { } TEST(st2110_tx, send_data) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); connection::Result res; auto conn_tx = new EmulatedST2110_Tx; @@ -310,12 +310,12 @@ TEST(st2110_tx, send_data) { // Setup Tx connection res = conn_tx->configure(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::configured); // Change state Configured -> Active res = conn_tx->establish(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::active); // Setup Emulated Transmitter @@ -326,14 +326,14 @@ TEST(st2110_tx, send_data) { for (int i = 0; i < 5; i++) { res = emulated_tx->transmit_wrapper(ctx, (void *)DUMMY_DATA2, sizeof(DUMMY_DATA2)); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::active); ASSERT_GT(conn_tx->received_packets_dummy2, 0); } // Shutdown Tx connection res = conn_tx->shutdown(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::closed); // Destroy resources @@ -342,7 +342,7 @@ TEST(st2110_tx, send_data) { } TEST(st2110_rx, get_data) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); connection::Result res; // Setup Emulated Receiver @@ -353,10 +353,10 @@ TEST(st2110_rx, get_data) { auto conn_rx = new EmulatedST2110_Rx; res = conn_rx->configure(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); res = conn_rx->establish(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::active); // Connect Rx connection to Emulated Receiver @@ -367,7 +367,7 @@ TEST(st2110_rx, get_data) { // Shutdown Rx connection res = conn_rx->shutdown(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::closed); ASSERT_GT(conn_rx->received_packets_dummy1, 0); @@ -381,7 +381,7 @@ static void tx_thread(context::Context& ctx, connection::Connection *conn_tx) { auto emulated_tx = new EmulatedTransmitter(ctx); // Change state Configured -> Active connection::Result res = conn_tx->establish(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::active); // Setup Emulated Transmitter @@ -392,13 +392,13 @@ static void tx_thread(context::Context& ctx, connection::Connection *conn_tx) { for (int i = 0; i < 50; i++) { res = emulated_tx->transmit_wrapper(ctx, (void *)DUMMY_DATA1, sizeof(DUMMY_DATA1)); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::active); } // Shutdown Tx connection res = conn_tx->shutdown(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::closed); // Destroy resources @@ -412,7 +412,7 @@ static void rx_thread(context::Context& ctx, connection::Connection *conn_rx, bo conn_rx->set_link(ctx, emulated_rx); // Connect Rx connection to Emulated Receiver connection::Result res = conn_rx->establish(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::active); // Sleep some sufficient time to allow receiving the data from transmitter @@ -420,7 +420,7 @@ static void rx_thread(context::Context& ctx, connection::Connection *conn_rx, bo // Shutdown Rx connection res = conn_rx->shutdown(ctx); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::closed); if (is_lossless) { @@ -435,7 +435,7 @@ static void rx_thread(context::Context& ctx, connection::Connection *conn_rx, bo } TEST(DISABLED_st2110_20, send_and_receive_data) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); connection::Result res; // Setup Tx connection @@ -449,13 +449,13 @@ TEST(DISABLED_st2110_20, send_and_receive_data) { std::string dev_port("kernel:lo"); auto conn_tx = new connection::ST2110_20Tx; res = conn_tx->configure(ctx, dev_port, cfg_st2110, cfg_video); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::configured); // Configure Rx auto conn_rx = new connection::ST2110_20Rx; res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); std::jthread rx_th, tx_th; @@ -474,7 +474,7 @@ TEST(DISABLED_st2110_20, send_and_receive_data) { } TEST(DISABLED_st2110_22, send_and_receive_data) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); connection::Result res; // Setup Tx connection @@ -488,13 +488,13 @@ TEST(DISABLED_st2110_22, send_and_receive_data) { std::string dev_port("kernel:lo"); auto conn_tx = new connection::ST2110_22Tx; res = conn_tx->configure(ctx, dev_port, cfg_st2110, cfg_video); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::configured); // Configure Rx auto conn_rx = new connection::ST2110_22Rx; res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_video); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); std::jthread rx_th, tx_th; @@ -513,7 +513,7 @@ TEST(DISABLED_st2110_22, send_and_receive_data) { } TEST(DISABLED_st2110_30, send_and_receive_data) { - auto ctx = context::WithCancel(mesh::context::Background()); + auto ctx = context::WithCancel(context::Background()); connection::Result res; // Setup Tx connection @@ -527,13 +527,13 @@ TEST(DISABLED_st2110_30, send_and_receive_data) { std::string dev_port("kernel:lo"); auto conn_tx = new connection::ST2110_30Tx; res = conn_tx->configure(ctx, dev_port, cfg_st2110, cfg_audio); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_tx->state(), connection::State::configured); // Configure Rx auto conn_rx = new connection::ST2110_30Rx; res = conn_rx->configure(ctx, dev_port, cfg_st2110, cfg_audio); - ASSERT_EQ(res, connection::Result::success) << mesh::connection::result2str(res); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); ASSERT_EQ(conn_rx->state(), connection::State::configured); std::jthread rx_th, tx_th; From b154f4b2e9b4698e71bdd7a0bf04f561f16e47af Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Fri, 29 Nov 2024 09:05:01 +0000 Subject: [PATCH 26/27] Add test scenarios for ST2110 transmit and receive following Concept.md scenario --- media-proxy/tests/st2110_tests.cc | 112 ++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index cc20c45f8..8c429848d 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -550,3 +550,115 @@ TEST(DISABLED_st2110_30, send_and_receive_data) { delete conn_tx; delete conn_rx; } + +/********************************************CONCEPT.md scenario**************************************** */ +/** + * How to run the test: + * 1) Modify two variables port_card0 and port_card1 with the correct values of the network interfaces of the machine. + * 2) Open 2 consoles + * 2a) In the first console run the following command: + * ./media_proxy_unit_tests --gtest_also_run_disabled_tests --gtest_filter=*concept_scenarion.mtl_st20_rx + * 2b) In the second console run the following command: + * ./media_proxy_unit_tests --gtest_also_run_disabled_tests --gtest_filter=*concept_scenarion.mtl_st20_tx + * 3) Wait until test ends ~120s + * 4) Check the output of the first console, it should show the number of received packets ~2000 + * example: + * received_packets_lossless: 2000 + * received_packets_lossy: 0 + */ + +std::string port_card0("0000:4b:01.1"); +std::string port_card1("0000:4b:11.1"); + +TEST(DISABLED_concept_scenarion, mtl_st20_tx) { + auto ctx = context::WithCancel(context::Background()); + connection::Result res; + + MeshConfig_Video cfg_video; + get_ST2110_video_cfg(cfg_video); + + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_20); + memcpy(cfg_st2110.local_ip_addr, "192.168.96.2", sizeof("192.168.96.2")); + memcpy(cfg_st2110.remote_ip_addr, "192.168.96.1", sizeof("192.168.96.1")); + + // Configure Tx + auto conn_tx = new connection::ST2110_20Tx; + res = conn_tx->configure(ctx, port_card0, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::configured); + + auto emulated_tx = new EmulatedTransmitter(ctx); + // Change state Configured -> Active + res = conn_tx->establish(ctx); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::active); + + // Setup Emulated Transmitter + emulated_tx->establish(ctx); + + // Connect Emulated Transmitter to Tx connection + emulated_tx->set_link(ctx, conn_tx); + + uint32_t data_size = cfg_video.width * cfg_video.height * 4; + void *data = malloc(data_size); + memcpy(data, DUMMY_DATA1, sizeof(DUMMY_DATA1)); + + for (int i = 0; i < 2000; i++) { + res = emulated_tx->transmit_wrapper(ctx, data, data_size); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::active); + } + + // Shutdown Tx connection + res = conn_tx->shutdown(ctx); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); + ASSERT_EQ(conn_tx->state(), connection::State::closed); + + // Destroy resources + delete emulated_tx; + delete conn_tx; + free(data); +} + +TEST(DISABLED_concept_scenarion, mtl_st20_rx) { + auto ctx = context::WithCancel(context::Background()); + connection::Result res; + + MeshConfig_Video cfg_video; + get_ST2110_video_cfg(cfg_video); + + MeshConfig_ST2110 cfg_st2110; + get_ST2110_session_config(cfg_st2110, MESH_CONN_TRANSPORT_ST2110_20); + memcpy(cfg_st2110.local_ip_addr, "192.168.96.1", sizeof("192.168.96.1")); + memcpy(cfg_st2110.remote_ip_addr, "192.168.96.2", sizeof("192.168.96.2")); + + // Configure Rx + auto conn_rx = new connection::ST2110_20Rx; + res = conn_rx->configure(ctx, port_card1, cfg_st2110, cfg_video); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::configured); + + auto emulated_rx = new EmulatedReceiver(ctx); + emulated_rx->establish(ctx); + + conn_rx->set_link(ctx, emulated_rx); + // Connect Rx connection to Emulated Receiver + res = conn_rx->establish(ctx); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::active); + + // Sleep some sufficient time to allow receiving the data from transmitter + mesh::thread::Sleep(ctx, std::chrono::milliseconds(1000 * 120)); // 120s + + // Shutdown Rx connection + res = conn_rx->shutdown(ctx); + ASSERT_EQ(res, connection::Result::success) << connection::result2str(res); + ASSERT_EQ(conn_rx->state(), connection::State::closed); + + printf("received_packets_lossless: %d\n", emulated_rx->received_packets_lossless); + printf("received_packets_lossy: %d\n", emulated_rx->received_packets_lossy); + + delete emulated_rx; + delete conn_rx; +} From 2e7b58266ac69100a6bb3f6f7fc2940f265160da Mon Sep 17 00:00:00 2001 From: Tomasz Szumski Date: Tue, 3 Dec 2024 08:17:04 +0000 Subject: [PATCH 27/27] Move common implementation from ST2110Tx/ST2110Rx to ST2110 --- media-proxy/include/mesh/st2110.h | 127 +++++++++++++++++++++++----- media-proxy/include/mesh/st2110rx.h | 116 ++++++++----------------- media-proxy/include/mesh/st2110tx.h | 111 +++++------------------- media-proxy/src/mesh/st2110.cc | 61 +++---------- media-proxy/tests/st2110_tests.cc | 4 +- 5 files changed, 176 insertions(+), 243 deletions(-) diff --git a/media-proxy/include/mesh/st2110.h b/media-proxy/include/mesh/st2110.h index 0990c9af1..74e306e88 100644 --- a/media-proxy/include/mesh/st2110.h +++ b/media-proxy/include/mesh/st2110.h @@ -17,39 +17,124 @@ namespace mesh::connection { #define ST_APP_PAYLOAD_TYPE_ST20 (112) #define ST_APP_PAYLOAD_TYPE_ST22 (114) +int mesh_video_format_to_st_format(int fmt, st_frame_fmt& st_fmt); +int mesh_audio_format_to_st_format(int fmt, st30_fmt& st_fmt); +int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling); +int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime); + +void *get_frame_data_ptr(st_frame *src); +void *get_frame_data_ptr(st30_frame *src); + +void get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port, + mtl_log_level log_level, const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); +mtl_handle get_mtl_device(const std::string& dev_port, mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int& session_id); + /** * ST2110 * * Base abstract class of SPMTE ST2110-xx bridge. ST2110Rx/ST2110Tx * inherit this class. */ -class ST2110 : public Connection { +template class ST2110 : public Connection { public: - static int mesh_video_format_to_st_format(int fmt, st_frame_fmt& st_fmt); - static int mesh_audio_format_to_st_format(int fmt, st30_fmt& st_fmt); - static int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling); - static int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime); + virtual ~ST2110() { + shutdown(_ctx); + if (ops.name) + free((void *)ops.name); + }; - static void *get_frame_data_ptr(st_frame *src); - static void *get_frame_data_ptr(st30_frame *src); + protected: + mtl_handle mtl_device = nullptr; + HANDLE mtl_session = nullptr; + OPS ops = {0}; + size_t transfer_size = 0; + std::atomic frame_available; + context::Context _ctx = context::WithCancel(context::Background()); - static void get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port, - mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE]); - static mtl_handle get_mtl_device(const std::string& dev_port, mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE], - int& session_id); + virtual FRAME *get_frame(HANDLE) = 0; + virtual int put_frame(HANDLE, FRAME *) = 0; + virtual HANDLE create_session(mtl_handle, OPS *) = 0; + virtual int close_session(HANDLE) = 0; - virtual ~ST2110() {}; + static int frame_available_cb(void *ptr) { + auto _this = static_cast(ptr); + if (!_this) { + return -1; + } - protected: - static int frame_available_cb(void *ptr); - void init_frame_available(); - void notify_frame_available(); - void wait_frame_available(); + _this->notify_frame_available(); - mtl_handle mtl_device = nullptr; - std::atomic frame_available; + return 0; + } + + void notify_frame_available() { + frame_available.store(true, std::memory_order_release); + frame_available.notify_one(); + } + + void wait_frame_available() { + frame_available.wait(false, std::memory_order_acquire); + frame_available = false; + } + + virtual int configure_common(context::Context& ctx, const std::string& dev_port, + const MeshConfig_ST2110& cfg_st2110) { + int session_id = 0; + mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); + if (!mtl_device) { + log::error("Failed to get MTL device"); + return -1; + } + + strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); + ops.port.num_port = 1; + + char session_name[NAME_MAX] = ""; + snprintf(session_name, NAME_MAX, "mcm_mtl_%d", session_id); + if (ops.name) + free((void *)ops.name); + ops.name = strdup(session_name); + ops.framebuff_cnt = 4; + + ops.priv = this; // app handle register to lib + ops.notify_frame_available = frame_available_cb; + + log::info("ST2110: configure") + ("port", ops.port.port[MTL_PORT_P]) + ("num_port", (int)ops.port.num_port) + ("name", ops.name) + ("framebuff_cnt", ops.framebuff_cnt); + + return 0; + } + + Result on_establish(context::Context& ctx) override { + _ctx = context::WithCancel(ctx); + frame_available = false; + + mtl_session = create_session(mtl_device, &ops); + if (!mtl_session) { + log::error("Failed to create session"); + set_state(ctx, State::closed); + return set_result(Result::error_general_failure); + } + + set_state(ctx, State::active); + return set_result(Result::success); + } + + Result on_shutdown(context::Context& ctx) override { + _ctx.cancel(); + notify_frame_available(); + + if (mtl_session) { + close_session(mtl_session); + mtl_session = nullptr; + } + set_state(ctx, State::closed); + return set_result(Result::success); + }; }; } // namespace mesh::connection diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 909228777..692537936 100644 --- a/media-proxy/include/mesh/st2110rx.h +++ b/media-proxy/include/mesh/st2110rx.h @@ -11,81 +11,39 @@ namespace mesh::connection { * Base abstract class of ST2110Rx. ST2110_20Rx/ST2110_22Rx/ST2110_30Rx * inherit this class. */ -template class ST2110Rx : public ST2110 { +template +class ST2110Rx : public ST2110 { public: - ST2110Rx() { _kind = Kind::receiver; } - - ~ST2110Rx() { - shutdown(_ctx); - if (ops.name) - free((void *)ops.name); - } + ST2110Rx() { this->_kind = Kind::receiver; } protected: - HANDLE mtl_session = nullptr; - OPS ops = {0}; - size_t transfer_size = 0; std::jthread frame_thread_handle; - context::Context _ctx = context::WithCancel(context::Background()); - - virtual FRAME *get_frame(HANDLE) = 0; - virtual int put_frame(HANDLE, FRAME *) = 0; - virtual HANDLE create_session(mtl_handle, OPS *) = 0; - virtual int close_session(HANDLE) = 0; int configure_common(context::Context& ctx, const std::string& dev_port, - const MeshConfig_ST2110& cfg_st2110) { - int session_id = 0; - mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); - if (!mtl_device) { - log::error("Failed to get MTL device"); - return -1; - } - - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]); - inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; - - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.num_port = 1; + const MeshConfig_ST2110& cfg_st2110) override{ + ST2110::configure_common(ctx, dev_port, cfg_st2110); - char session_name[NAME_MAX] = ""; - snprintf(session_name, NAME_MAX, "mcm_mtl_rx_%d", session_id); - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); - ops.framebuff_cnt = 4; - - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, this->ops.port.ip_addr[MTL_PORT_P]); + inet_pton(AF_INET, cfg_st2110.local_ip_addr, this->ops.port.mcast_sip_addr[MTL_PORT_P]); + this->ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port; log::info("ST2110Rx: configure") - ("port", ops.port.port[MTL_PORT_P]) - ("ip_addr", std::to_string(ops.port.ip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.ip_addr[MTL_PORT_P][3])) - ("mcast_sip_addr", std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][3])) - ("num_port", ops.port.num_port) - ("udp_port", ops.port.udp_port[MTL_PORT_P]) - ("name", ops.name) - ("framebuff_cnt", ops.framebuff_cnt); - + ("ip_addr", std::to_string(this->ops.port.ip_addr[MTL_PORT_P][0]) + " " + + std::to_string(this->ops.port.ip_addr[MTL_PORT_P][1]) + " " + + std::to_string(this->ops.port.ip_addr[MTL_PORT_P][2]) + " " + + std::to_string(this->ops.port.ip_addr[MTL_PORT_P][3])) + ("mcast_sip_addr", std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " + + std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " + + std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " + + std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][3])) + ("udp_port", this->ops.port.udp_port[MTL_PORT_P]); return 0; } Result on_establish(context::Context& ctx) override { - _ctx = context::WithCancel(ctx); - init_frame_available(); - - mtl_session = create_session(mtl_device, &ops); - if (!mtl_session) { - log::error("Failed to create session"); - set_state(ctx, State::closed); - return set_result(Result::error_general_failure); + Result res = ST2110::on_establish(ctx); + if (res != Result::success) { + return res; } /* Start MTL session thread. */ @@ -93,42 +51,38 @@ template class ST2110Rx : public frame_thread_handle = std::jthread(&ST2110Rx::frame_thread, this); } catch (const std::system_error& e) { log::error("Failed to create thread"); - set_state(ctx, State::closed); - return set_result(Result::error_out_of_memory); + this->set_state(ctx, State::closed); + return this->set_result(Result::error_out_of_memory); } - set_state(ctx, State::active); - return set_result(Result::success); + this->set_state(ctx, State::active); + return this->set_result(Result::success); } Result on_shutdown(context::Context& ctx) override { - _ctx.cancel(); - notify_frame_available(); + Result res = ST2110::on_shutdown(ctx); + if (res != Result::success) { + return res; + } frame_thread_handle.join(); - if (mtl_session) { - close_session(mtl_session); - mtl_session = nullptr; - } - set_state(ctx, State::closed); - return set_result(Result::success); + this->set_state(ctx, State::closed); + return this->set_result(Result::success); }; - virtual void on_delete(context::Context& ctx) override {} - private: void frame_thread() { - while (!_ctx.cancelled()) { + while (!this->_ctx.cancelled()) { // Get full buffer from MTL - FRAME *frame_ptr = get_frame(mtl_session); + FRAME *frame_ptr = this->get_frame(this->mtl_session); if (frame_ptr) { // Forward buffer to emulated receiver - transmit(_ctx, get_frame_data_ptr(frame_ptr), transfer_size); + this->transmit(this->_ctx, get_frame_data_ptr(frame_ptr), this->transfer_size); // Return used buffer to MTL - put_frame(mtl_session, frame_ptr); + this->put_frame(this->mtl_session, frame_ptr); } else { - wait_frame_available(); + this->wait_frame_available(); } } } diff --git a/media-proxy/include/mesh/st2110tx.h b/media-proxy/include/mesh/st2110tx.h index d3715ea9e..79d11a819 100644 --- a/media-proxy/include/mesh/st2110tx.h +++ b/media-proxy/include/mesh/st2110tx.h @@ -2,7 +2,6 @@ #define ST2110TX_H #include "st2110.h" -#include namespace mesh::connection { @@ -12,117 +11,53 @@ namespace mesh::connection { * Base abstract class of ST2110Tx. ST2110_20Tx/ST2110_22Tx/ST2110_30Tx * inherit this class. */ -template class ST2110Tx : public ST2110 { +template +class ST2110Tx : public ST2110 { public: - ST2110Tx() { _kind = Kind::transmitter; }; - ~ST2110Tx() { - shutdown(_ctx); - if (ops.name) - free((void *)ops.name); - }; + ST2110Tx() { this->_kind = Kind::transmitter; }; protected: - HANDLE mtl_session = nullptr; - OPS ops = {0}; - uint32_t transfer_size = 0; - context::Context _ctx = context::WithCancel(context::Background()); - - virtual FRAME *get_frame(HANDLE) = 0; - virtual int put_frame(HANDLE, FRAME *) = 0; - virtual HANDLE create_session(mtl_handle, OPS *) = 0; - virtual int close_session(HANDLE) = 0; - int configure_common(context::Context& ctx, const std::string& dev_port, - const MeshConfig_ST2110& cfg_st2110) { - int session_id = 0; - mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id); - if (!mtl_device) { - log::error("Failed to get MTL device"); - return -1; - } + const MeshConfig_ST2110& cfg_st2110) override { + ST2110::configure_common(ctx, dev_port, cfg_st2110); - inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.dip_addr[MTL_PORT_P]); - ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; - strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN); - ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; - ops.port.num_port = 1; - - char session_name[NAME_MAX] = ""; - snprintf(session_name, NAME_MAX, "mcm_mtl_tx_%d", session_id); - if (ops.name) - free((void *)ops.name); - ops.name = strdup(session_name); - ops.framebuff_cnt = 4; - - ops.priv = this; // app handle register to lib - ops.notify_frame_available = frame_available_cb; + inet_pton(AF_INET, cfg_st2110.remote_ip_addr, this->ops.port.dip_addr[MTL_PORT_P]); + this->ops.port.udp_port[MTL_PORT_P] = cfg_st2110.remote_port; + this->ops.port.udp_src_port[MTL_PORT_P] = cfg_st2110.local_port; log::info("ST2110Tx: configure") - ("port", ops.port.port[MTL_PORT_P]) - ("dip_addr", std::to_string(ops.port.dip_addr[MTL_PORT_P][0]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][1]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][2]) + " " + - std::to_string(ops.port.dip_addr[MTL_PORT_P][3])) - ("num_port", ops.port.num_port) - ("udp_port", ops.port.udp_port[MTL_PORT_P]) - ("udp_src_port", ops.port.udp_src_port[MTL_PORT_P]) - ("name", ops.name) - ("framebuff_cnt", ops.framebuff_cnt); - + ("dip_addr", std::to_string(this->ops.port.dip_addr[MTL_PORT_P][0]) + " " + + std::to_string(this->ops.port.dip_addr[MTL_PORT_P][1]) + " " + + std::to_string(this->ops.port.dip_addr[MTL_PORT_P][2]) + " " + + std::to_string(this->ops.port.dip_addr[MTL_PORT_P][3])) + ("udp_port", this->ops.port.udp_port[MTL_PORT_P]) + ("udp_src_port", this->ops.port.udp_src_port[MTL_PORT_P]); return 0; } - Result on_establish(context::Context& ctx) override { - _ctx = context::WithCancel(ctx); - init_frame_available(); - - mtl_session = create_session(mtl_device, &ops); - if (!mtl_session) { - log::error("Failed to create session"); - set_state(ctx, State::closed); - return set_result(Result::error_general_failure); - } - - set_state(ctx, State::active); - return set_result(Result::success); - }; - - Result on_shutdown(context::Context& ctx) override { - _ctx.cancel(); - notify_frame_available(); - - if (mtl_session) { - close_session(mtl_session); - mtl_session = nullptr; - } - set_state(ctx, State::closed); - return set_result(Result::success); - }; - Result on_receive(context::Context& ctx, void *ptr, uint32_t sz, uint32_t& sent) override { - int to_be_sent = std::min(transfer_size, sz); - // TODO: add error/warning if sent is different than _transfer_size + int copy_size = this->transfer_size > sz ? sz : this->transfer_size; FRAME *frame; for (;;) { - if (ctx.cancelled() || _ctx.cancelled()) - return set_result(Result::error_context_cancelled); + if (ctx.cancelled() || this->_ctx.cancelled()) + return this->set_result(Result::error_context_cancelled); // Get empty buffer from MTL - frame = get_frame(mtl_session); + frame = this->get_frame(this->mtl_session); if (frame) break; - wait_frame_available(); + this->wait_frame_available(); } // Copy data from emulated transmitter to MTL empty buffer - mtl_memcpy(get_frame_data_ptr(frame), ptr, to_be_sent); + mtl_memcpy(get_frame_data_ptr(frame), ptr, copy_size); // Return full buffer to MTL - put_frame(mtl_session, frame); + this->put_frame(this->mtl_session, frame); - sent = to_be_sent; - return set_result(Result::success); + sent = this->transfer_size; + return this->set_result(Result::success); }; }; diff --git a/media-proxy/src/mesh/st2110.cc b/media-proxy/src/mesh/st2110.cc index c1ed34ea9..0dcf7f026 100644 --- a/media-proxy/src/mesh/st2110.cc +++ b/media-proxy/src/mesh/st2110.cc @@ -2,7 +2,7 @@ namespace mesh::connection { -int ST2110::mesh_video_format_to_st_format(int mesh_fmt, st_frame_fmt& st_fmt) { +int mesh_video_format_to_st_format(int mesh_fmt, st_frame_fmt& st_fmt) { switch (mesh_fmt) { case MESH_VIDEO_PIXEL_FORMAT_NV12: st_fmt = ST_FRAME_FMT_YUV420CUSTOM8; @@ -26,7 +26,7 @@ int ST2110::mesh_video_format_to_st_format(int mesh_fmt, st_frame_fmt& st_fmt) { return 0; // Success } -int ST2110::mesh_audio_format_to_st_format(int mesh_fmt, st30_fmt& st_fmt) { +int mesh_audio_format_to_st_format(int mesh_fmt, st30_fmt& st_fmt) { switch (mesh_fmt) { case MESH_AUDIO_FORMAT_PCM_S8: st_fmt = ST30_FMT_PCM8; @@ -44,7 +44,7 @@ int ST2110::mesh_audio_format_to_st_format(int mesh_fmt, st30_fmt& st_fmt) { return 0; // Success } -int ST2110::mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling) { +int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling) { switch (sampling) { case MESH_AUDIO_SAMPLE_RATE_48000: st_sampling = ST30_SAMPLING_48K; @@ -62,7 +62,7 @@ int ST2110::mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_s return 0; // Success } -int ST2110::mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime) { +int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime) { switch (ptime) { case MESH_AUDIO_PACKET_TIME_1MS: st_ptime = ST30_PTIME_1MS; @@ -98,53 +98,12 @@ int ST2110::mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime) { return 0; // Success } -void *ST2110::get_frame_data_ptr(st_frame *src) { - return src->addr[0]; -} - -void *ST2110::get_frame_data_ptr(st30_frame *src) { - return src->addr; -} - -/** - * Use this function in on_establish() to initialize frame_available flag. - */ -void ST2110::init_frame_available() { - frame_available = false; -} - -/** - * Use this function in on_shutdown() and frame_available_cb() to properly notify another thread - * - */ -void ST2110::notify_frame_available() { - frame_available.store(true, std::memory_order_release); - frame_available.notify_one(); -} +void *get_frame_data_ptr(st_frame *src) { return src->addr[0]; } -/** - * Use this function in on_receive() to wait for notification - * - */ -void ST2110::wait_frame_available() { - frame_available.wait(false, std::memory_order_acquire); - frame_available = false; -} - -int ST2110::frame_available_cb(void *ptr) { - auto _this = static_cast(ptr); - if (!_this) { - return -1; - } - - _this->notify_frame_available(); - - return 0; -} +void *get_frame_data_ptr(st30_frame *src) { return src->addr; } -void ST2110::get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port, - mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE]) { +void get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port, + mtl_log_level log_level, const char local_ip_addr[MESH_IP_ADDRESS_SIZE]) { if (getenv("KAHAWAI_CFG_PATH") == NULL) { setenv("KAHAWAI_CFG_PATH", "/usr/local/etc/imtl.json", 0); } @@ -172,8 +131,8 @@ void ST2110::get_mtl_dev_params(mtl_init_params& st_param, const std::string& de st_param.memzone_max = 9000; } -mtl_handle ST2110::get_mtl_device(const std::string& dev_port, mtl_log_level log_level, - const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int& session_id) { +mtl_handle get_mtl_device(const std::string& dev_port, mtl_log_level log_level, + const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int& session_id) { static mtl_handle dev_handle; static int _session_id; static std::mutex mtx; diff --git a/media-proxy/tests/st2110_tests.cc b/media-proxy/tests/st2110_tests.cc index 8c429848d..787da0db5 100644 --- a/media-proxy/tests/st2110_tests.cc +++ b/media-proxy/tests/st2110_tests.cc @@ -54,7 +54,7 @@ class EmulatedReceiver : public connection::Connection { } }; -class EmulatedST2110_Tx : public connection::ST2110Tx { +class EmulatedST2110_Tx : public connection::ST2110Tx { public: uint32_t received_packets_dummy1; uint32_t received_packets_dummy2; @@ -90,7 +90,7 @@ class EmulatedST2110_Tx : public connection::ST2110Tx