diff --git a/media-proxy/include/mesh/st2110rx.h b/media-proxy/include/mesh/st2110rx.h index 5f680273..59ea5fef 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 c4282d58..e818038e 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 b6f7f3b7..247c002c 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 7092e70d..5f3de79c 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) { 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 50baccbe..a2d82656 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 aa56bd86..79a9a424 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 496ae4fb..474d18e8 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 21f109a3..e7cadc0e 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 fe4be3d9..cba42655 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;