Skip to content

Commit

Permalink
Replace std::function, with virtual fn in ST2110 derrived classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tszumski committed Nov 22, 2024
1 parent 577f662 commit 47ca913
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 106 deletions.
34 changes: 26 additions & 8 deletions media-proxy/include/mesh/st2110rx.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ template <typename FRAME, typename HANDLE, typename OPS> class ST2110Rx : public
std::jthread _frame_thread_handle;
context::Context _ctx;

std::function<FRAME *(HANDLE)> _get_frame_fn;
std::function<int(HANDLE, FRAME *)> _put_frame_fn;
std::function<HANDLE(mtl_handle, OPS *)> _create_session_fn;
std::function<int(HANDLE)> _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);
Expand All @@ -67,7 +67,7 @@ template <typename FRAME, typename HANDLE, typename OPS> class ST2110Rx : public
_frame_thread_handle.join();

if (_handle) {
_close_session_fn(_handle);
close_session(_handle);
_handle = nullptr;
}
set_state(ctx, State::closed);
Expand All @@ -81,7 +81,7 @@ template <typename FRAME, typename HANDLE, typename OPS> 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);
Expand All @@ -95,7 +95,7 @@ template <typename FRAME, typename HANDLE, typename OPS> 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);
}
}
};
Expand All @@ -108,6 +108,12 @@ class ST2110_20Rx : public ST2110Rx<st_frame, st20p_rx_handle, st20p_rx_ops> {
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:
};

Expand All @@ -119,6 +125,12 @@ class ST2110_22Rx : public ST2110Rx<st_frame, st22p_rx_handle, st22p_rx_ops> {
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:
};

Expand All @@ -130,6 +142,12 @@ class ST2110_30Rx : public ST2110Rx<st30_frame, st30p_rx_handle, st30p_rx_ops> {
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:
};

Expand Down
34 changes: 26 additions & 8 deletions media-proxy/include/mesh/st2110tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ template <typename FRAME, typename HANDLE, typename OPS> class ST2110Tx : public
uint32_t _transfer_size;
context::Context _ctx;

std::function<FRAME *(HANDLE)> _get_frame_fn;
std::function<int(HANDLE, FRAME *)> _put_frame_fn;
std::function<HANDLE(mtl_handle, OPS *)> _create_session_fn;
std::function<int(HANDLE)> _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);
Expand All @@ -56,7 +56,7 @@ template <typename FRAME, typename HANDLE, typename OPS> class ST2110Tx : public
_ctx.cancel();

if (_handle) {
_close_session_fn(_handle);
close_session(_handle);
_handle = nullptr;
}
set_state(ctx, State::closed);
Expand All @@ -71,7 +71,7 @@ template <typename FRAME, typename HANDLE, typename OPS> 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);
Expand All @@ -87,7 +87,7 @@ template <typename FRAME, typename HANDLE, typename OPS> 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);
Expand All @@ -106,6 +106,12 @@ class ST2110_20Tx : public ST2110Tx<st_frame, st20p_tx_handle, st20p_tx_ops> {
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:
};

Expand All @@ -117,6 +123,12 @@ class ST2110_22Tx : public ST2110Tx<st_frame, st22p_tx_handle, st22p_tx_ops> {
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:
};

Expand All @@ -128,6 +140,12 @@ class ST2110_30Tx : public ST2110Tx<st30_frame, st30p_tx_handle, st30p_tx_ops> {
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:
};

Expand Down
19 changes: 12 additions & 7 deletions media-proxy/src/mesh/st2110_20rx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions media-proxy/src/mesh/st2110_20tx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions media-proxy/src/mesh/st2110_22rx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions media-proxy/src/mesh/st2110_22tx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions media-proxy/src/mesh/st2110_30rx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions media-proxy/src/mesh/st2110_30tx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 47ca913

Please sign in to comment.