Skip to content

Commit 94c59be

Browse files
committed
Move common implementation from ST2110Tx/ST2110Rx to ST2110
1 parent b154f4b commit 94c59be

File tree

5 files changed

+175
-243
lines changed

5 files changed

+175
-243
lines changed

media-proxy/include/mesh/st2110.h

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,125 @@ namespace mesh::connection {
1717
#define ST_APP_PAYLOAD_TYPE_ST20 (112)
1818
#define ST_APP_PAYLOAD_TYPE_ST22 (114)
1919

20+
int mesh_video_format_to_st_format(int fmt, st_frame_fmt& st_fmt);
21+
int mesh_audio_format_to_st_format(int fmt, st30_fmt& st_fmt);
22+
int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling);
23+
int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime);
24+
25+
void *get_frame_data_ptr(st_frame *src);
26+
void *get_frame_data_ptr(st30_frame *src);
27+
28+
void get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port,
29+
mtl_log_level log_level, const char local_ip_addr[MESH_IP_ADDRESS_SIZE]);
30+
mtl_handle get_mtl_device(const std::string& dev_port, mtl_log_level log_level,
31+
const char local_ip_addr[MESH_IP_ADDRESS_SIZE], int& session_id);
32+
2033
/**
2134
* ST2110
2235
*
2336
* Base abstract class of SPMTE ST2110-xx bridge. ST2110Rx/ST2110Tx
2437
* inherit this class.
2538
*/
26-
class ST2110 : public Connection {
39+
template <typename FRAME, typename HANDLE, typename OPS> class ST2110 : public Connection {
2740
public:
28-
static int mesh_video_format_to_st_format(int fmt, st_frame_fmt& st_fmt);
29-
static int mesh_audio_format_to_st_format(int fmt, st30_fmt& st_fmt);
30-
static int mesh_audio_sampling_to_st_sampling(int sampling, st30_sampling& st_sampling);
31-
static int mesh_audio_ptime_to_st_ptime(int ptime, st30_ptime& st_ptime);
41+
virtual ~ST2110() {
42+
shutdown(_ctx);
43+
if (ops.name)
44+
free((void *)ops.name);
45+
};
3246

33-
static void *get_frame_data_ptr(st_frame *src);
34-
static void *get_frame_data_ptr(st30_frame *src);
47+
protected:
48+
mtl_handle mtl_device = nullptr;
49+
HANDLE mtl_session = nullptr;
50+
OPS ops = {0};
51+
size_t transfer_size = 0;
52+
std::atomic<bool> frame_available;
53+
context::Context _ctx = context::WithCancel(context::Background());
3554

36-
static void get_mtl_dev_params(mtl_init_params& st_param, const std::string& dev_port,
37-
mtl_log_level log_level,
38-
const char local_ip_addr[MESH_IP_ADDRESS_SIZE]);
39-
static mtl_handle get_mtl_device(const std::string& dev_port, mtl_log_level log_level,
40-
const char local_ip_addr[MESH_IP_ADDRESS_SIZE],
41-
int& session_id);
55+
virtual FRAME *get_frame(HANDLE) = 0;
56+
virtual int put_frame(HANDLE, FRAME *) = 0;
57+
virtual HANDLE create_session(mtl_handle, OPS *) = 0;
58+
virtual int close_session(HANDLE) = 0;
4259

43-
virtual ~ST2110() {};
60+
static int frame_available_cb(void *ptr) {
61+
auto _this = static_cast<ST2110 *>(ptr);
62+
if (!_this) {
63+
return -1;
64+
}
4465

45-
protected:
46-
static int frame_available_cb(void *ptr);
47-
void init_frame_available();
48-
void notify_frame_available();
49-
void wait_frame_available();
66+
_this->notify_frame_available();
5067

51-
mtl_handle mtl_device = nullptr;
52-
std::atomic<bool> frame_available;
68+
return 0;
69+
}
70+
71+
void notify_frame_available() {
72+
frame_available.store(true, std::memory_order_release);
73+
frame_available.notify_one();
74+
}
75+
76+
void wait_frame_available() {
77+
frame_available.wait(false, std::memory_order_acquire);
78+
frame_available = false;
79+
}
80+
81+
virtual int configure_common(context::Context& ctx, const std::string& dev_port,
82+
const MeshConfig_ST2110& cfg_st2110) {
83+
int session_id = 0;
84+
mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id);
85+
if (!mtl_device) {
86+
log::error("Failed to get MTL device");
87+
return -1;
88+
}
89+
90+
strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN);
91+
ops.port.num_port = 1;
92+
93+
char session_name[NAME_MAX] = "";
94+
snprintf(session_name, NAME_MAX, "mcm_mtl_%d", session_id);
95+
if (ops.name)
96+
free((void *)ops.name);
97+
ops.name = strdup(session_name);
98+
ops.framebuff_cnt = 4;
99+
100+
ops.priv = this; // app handle register to lib
101+
ops.notify_frame_available = frame_available_cb;
102+
103+
log::info("ST2110: configure")
104+
("port", ops.port.port[MTL_PORT_P])
105+
("num_port", (int)ops.port.num_port)
106+
("udp_port", ops.port.udp_port[MTL_PORT_P])
107+
("name", ops.name)
108+
("framebuff_cnt", ops.framebuff_cnt);
109+
110+
return 0;
111+
}
112+
113+
Result on_establish(context::Context& ctx) override {
114+
_ctx = context::WithCancel(ctx);
115+
frame_available = false;
116+
117+
mtl_session = create_session(mtl_device, &ops);
118+
if (!mtl_session) {
119+
log::error("Failed to create session");
120+
set_state(ctx, State::closed);
121+
return set_result(Result::error_general_failure);
122+
}
123+
124+
set_state(ctx, State::active);
125+
return set_result(Result::success);
126+
}
127+
128+
Result on_shutdown(context::Context& ctx) override {
129+
_ctx.cancel();
130+
notify_frame_available();
131+
132+
if (mtl_session) {
133+
close_session(mtl_session);
134+
mtl_session = nullptr;
135+
}
136+
set_state(ctx, State::closed);
137+
return set_result(Result::success);
138+
};
53139
};
54140

55141
} // namespace mesh::connection

media-proxy/include/mesh/st2110rx.h

Lines changed: 34 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,124 +11,77 @@ namespace mesh::connection {
1111
* Base abstract class of ST2110Rx. ST2110_20Rx/ST2110_22Rx/ST2110_30Rx
1212
* inherit this class.
1313
*/
14-
template <typename FRAME, typename HANDLE, typename OPS> class ST2110Rx : public ST2110 {
14+
template <typename FRAME, typename HANDLE, typename OPS>
15+
class ST2110Rx : public ST2110<FRAME, HANDLE, OPS> {
1516
public:
16-
ST2110Rx() { _kind = Kind::receiver; }
17-
18-
~ST2110Rx() {
19-
shutdown(_ctx);
20-
if (ops.name)
21-
free((void *)ops.name);
22-
}
17+
ST2110Rx() { this->_kind = Kind::receiver; }
2318

2419
protected:
25-
HANDLE mtl_session = nullptr;
26-
OPS ops = {0};
27-
size_t transfer_size = 0;
2820
std::jthread frame_thread_handle;
29-
context::Context _ctx = context::WithCancel(context::Background());
30-
31-
virtual FRAME *get_frame(HANDLE) = 0;
32-
virtual int put_frame(HANDLE, FRAME *) = 0;
33-
virtual HANDLE create_session(mtl_handle, OPS *) = 0;
34-
virtual int close_session(HANDLE) = 0;
3521

3622
int configure_common(context::Context& ctx, const std::string& dev_port,
37-
const MeshConfig_ST2110& cfg_st2110) {
38-
int session_id = 0;
39-
mtl_device = get_mtl_device(dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr, session_id);
40-
if (!mtl_device) {
41-
log::error("Failed to get MTL device");
42-
return -1;
43-
}
44-
45-
inet_pton(AF_INET, cfg_st2110.remote_ip_addr, ops.port.ip_addr[MTL_PORT_P]);
46-
inet_pton(AF_INET, cfg_st2110.local_ip_addr, ops.port.mcast_sip_addr[MTL_PORT_P]);
47-
ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port;
48-
49-
strlcpy(ops.port.port[MTL_PORT_P], dev_port.c_str(), MTL_PORT_MAX_LEN);
50-
ops.port.num_port = 1;
23+
const MeshConfig_ST2110& cfg_st2110) override{
24+
ST2110<FRAME, HANDLE, OPS>::configure_common(ctx, dev_port, cfg_st2110);
5125

52-
char session_name[NAME_MAX] = "";
53-
snprintf(session_name, NAME_MAX, "mcm_mtl_rx_%d", session_id);
54-
if (ops.name)
55-
free((void *)ops.name);
56-
ops.name = strdup(session_name);
57-
ops.framebuff_cnt = 4;
58-
59-
ops.priv = this; // app handle register to lib
60-
ops.notify_frame_available = frame_available_cb;
26+
inet_pton(AF_INET, cfg_st2110.remote_ip_addr, this->ops.port.ip_addr[MTL_PORT_P]);
27+
inet_pton(AF_INET, cfg_st2110.local_ip_addr, this->ops.port.mcast_sip_addr[MTL_PORT_P]);
28+
this->ops.port.udp_port[MTL_PORT_P] = cfg_st2110.local_port;
6129

6230
log::info("ST2110Rx: configure")
63-
("port", ops.port.port[MTL_PORT_P])
64-
("ip_addr", std::to_string(ops.port.ip_addr[MTL_PORT_P][0]) + " " +
65-
std::to_string(ops.port.ip_addr[MTL_PORT_P][1]) + " " +
66-
std::to_string(ops.port.ip_addr[MTL_PORT_P][2]) + " " +
67-
std::to_string(ops.port.ip_addr[MTL_PORT_P][3]))
68-
("mcast_sip_addr", std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " +
69-
std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " +
70-
std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " +
71-
std::to_string(ops.port.mcast_sip_addr[MTL_PORT_P][3]))
72-
("num_port", ops.port.num_port)
73-
("udp_port", ops.port.udp_port[MTL_PORT_P])
74-
("name", ops.name)
75-
("framebuff_cnt", ops.framebuff_cnt);
76-
31+
("ip_addr", std::to_string(this->ops.port.ip_addr[MTL_PORT_P][0]) + " " +
32+
std::to_string(this->ops.port.ip_addr[MTL_PORT_P][1]) + " " +
33+
std::to_string(this->ops.port.ip_addr[MTL_PORT_P][2]) + " " +
34+
std::to_string(this->ops.port.ip_addr[MTL_PORT_P][3]))
35+
("mcast_sip_addr", std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][0]) + " " +
36+
std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][1]) + " " +
37+
std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][2]) + " " +
38+
std::to_string(this->ops.port.mcast_sip_addr[MTL_PORT_P][3]));
7739
return 0;
7840
}
7941

8042
Result on_establish(context::Context& ctx) override {
81-
_ctx = context::WithCancel(ctx);
82-
init_frame_available();
83-
84-
mtl_session = create_session(mtl_device, &ops);
85-
if (!mtl_session) {
86-
log::error("Failed to create session");
87-
set_state(ctx, State::closed);
88-
return set_result(Result::error_general_failure);
43+
Result res = ST2110<FRAME, HANDLE, OPS>::on_establish(ctx);
44+
if (res != Result::success) {
45+
return res;
8946
}
9047

9148
/* Start MTL session thread. */
9249
try {
9350
frame_thread_handle = std::jthread(&ST2110Rx::frame_thread, this);
9451
} catch (const std::system_error& e) {
9552
log::error("Failed to create thread");
96-
set_state(ctx, State::closed);
97-
return set_result(Result::error_out_of_memory);
53+
this->set_state(ctx, State::closed);
54+
return this->set_result(Result::error_out_of_memory);
9855
}
9956

100-
set_state(ctx, State::active);
101-
return set_result(Result::success);
57+
this->set_state(ctx, State::active);
58+
return this->set_result(Result::success);
10259
}
10360

10461
Result on_shutdown(context::Context& ctx) override {
105-
_ctx.cancel();
106-
notify_frame_available();
62+
Result res = ST2110<FRAME, HANDLE, OPS>::on_shutdown(ctx);
63+
if (res != Result::success) {
64+
return res;
65+
}
10766

10867
frame_thread_handle.join();
10968

110-
if (mtl_session) {
111-
close_session(mtl_session);
112-
mtl_session = nullptr;
113-
}
114-
set_state(ctx, State::closed);
115-
return set_result(Result::success);
69+
this->set_state(ctx, State::closed);
70+
return this->set_result(Result::success);
11671
};
11772

118-
virtual void on_delete(context::Context& ctx) override {}
119-
12073
private:
12174
void frame_thread() {
122-
while (!_ctx.cancelled()) {
75+
while (!this->_ctx.cancelled()) {
12376
// Get full buffer from MTL
124-
FRAME *frame_ptr = get_frame(mtl_session);
77+
FRAME *frame_ptr = this->get_frame(this->mtl_session);
12578
if (frame_ptr) {
12679
// Forward buffer to emulated receiver
127-
transmit(_ctx, get_frame_data_ptr(frame_ptr), transfer_size);
80+
this->transmit(this->_ctx, get_frame_data_ptr(frame_ptr), this->transfer_size);
12881
// Return used buffer to MTL
129-
put_frame(mtl_session, frame_ptr);
82+
this->put_frame(this->mtl_session, frame_ptr);
13083
} else {
131-
wait_frame_available();
84+
this->wait_frame_available();
13285
}
13386
}
13487
}

0 commit comments

Comments
 (0)