Skip to content

Commit 6d4f16d

Browse files
committed
video_rxtx: hide implementation
Make the API C-compatible (except of `send` that uses shared_ptr). This is perhaps better than creating a PIMPL or so, that could not be used from C code, anyways.
1 parent b2e37e9 commit 6d4f16d

File tree

16 files changed

+138
-88
lines changed

16 files changed

+138
-88
lines changed

src/audio/audio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
#include "utils/string_view_utils.hpp"
9999
#include "utils/thread.h"
100100
#include "utils/worker.h"
101-
#include "video_rxtx.hpp" // for video_rxtx
101+
#include "video_rxtx.h" // for video_rxtx
102102

103103
using std::array;
104104
using std::fixed;
@@ -1076,7 +1076,7 @@ set_audio_spec_to_vrxtx(struct video_rxtx *vrxtx, audio_frame2 *compressed_frm,
10761076
audio_desc_to_cstring(desc), rx_port);
10771077

10781078
assert(vrxtx != nullptr);
1079-
vrxtx->set_audio_spec(&desc, rx_port, tx_port, rtp_is_ipv6(netdev));
1079+
vrxtx_set_audio_spec(vrxtx, &desc, rx_port, tx_port, rtp_is_ipv6(netdev));
10801080
}
10811081

10821082
static void *audio_sender_thread(void *arg)

src/hd-rum-translator/hd-rum-decompress.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#include "video.h"
5959
#include "video_display.h"
6060
#include "video_display/pipe.h" // for pipe_frame_recv_del...
61-
#include "video_rxtx.hpp" // for video_rxtx, vrxtx_pa...
61+
#include "video_rxtx.h" // for video_rxtx, vrxtx_pa...
6262
#include "video_rxtx/ultragrid_rtp.hpp"
6363

6464
#include "utils/profile_timer.hpp"
@@ -137,7 +137,7 @@ ssize_t hd_rum_decompress_write(void *state, void *buf, size_t count)
137137
auto *s = static_cast<state_transcoder_decompress *>(state);
138138

139139
auto *ultragrid_rtp = static_cast<ultragrid_rtp_video_rxtx *>(
140-
s->video_rxtx->m_impl_state);
140+
vrxtx_get_impl_state(s->video_rxtx));
141141
assert(ultragrid_rtp != nullptr);
142142
return rtp_send_raw_rtp_data(ultragrid_rtp->m_network_device,
143143
(char *) buf, count);
@@ -255,9 +255,8 @@ void hd_rum_decompress_done(void *state) {
255255
s->worker_thread.join();
256256

257257
display_put_frame(s->display, nullptr, 0);
258-
s->video_rxtx->join();
259-
260-
delete s->video_rxtx;
258+
vrxtx_join(s->video_rxtx);
259+
vrxtx_destroy(s->video_rxtx);
261260

262261
display_join(s->display);
263262
display_done(s->display);

src/hd-rum-translator/hd-rum-recompress.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ struct recompress_output_port {
7474
unsigned short tx_port, const struct common_opts *common,
7575
const char *fec, long long bitrate);
7676

77-
std::unique_ptr<struct video_rxtx> video_rxtx;
77+
std::unique_ptr<struct video_rxtx, decltype(&vrxtx_destroy)> video_rxtx{
78+
nullptr, vrxtx_destroy
79+
};
7880
std::string host;
7981
int tx_port = 0;
8082

@@ -127,7 +129,11 @@ recompress_output_port::recompress_output_port(
127129
// params["decoder_mode"].l = VIDEO_NORMAL;
128130
// params["display_device"].ptr = nullptr;
129131

130-
auto rxtx = video_rxtx::create("ultragrid_rtp", &params, common);
132+
struct video_rxtx *rxtx = nullptr;
133+
int rc = vrxtx_init("ultragrid_rtp", &params, common, &rxtx);
134+
if (rc != 0) {
135+
throw rc;
136+
}
131137

132138
video_rxtx.reset(rxtx);
133139
}
@@ -143,16 +149,17 @@ static void recompress_port_write(recompress_output_port& port, shared_ptr<video
143149
double seconds = chrono::duration_cast<chrono::duration<double>>(now - port.t0).count();
144150
if(seconds > 5) {
145151
double fps = port.frames / seconds;
152+
void *impl = vrxtx_get_impl_state(port.video_rxtx.get());
146153
log_msg(LOG_LEVEL_INFO, "[0x%08" PRIx32 "->%s:%d:0x%08" PRIx32 "] %d frames in %g seconds = %g FPS\n",
147154
frame->ssrc,
148155
port.host.c_str(), port.tx_port,
149-
ultragrid_rtp_get_ssrc(port.video_rxtx->m_impl_state),
156+
ultragrid_rtp_get_ssrc(impl),
150157
port.frames, seconds, fps);
151158
port.t0 = now;
152159
port.frames = 0;
153160
}
154161

155-
port.video_rxtx->send(std::move(frame));
162+
vrxtx_send(port.video_rxtx.get(), std::move(frame));
156163
}
157164

158165
static void recompress_worker(struct recompress_worker_ctx *ctx){
@@ -254,8 +261,9 @@ uint32_t recompress_get_port_ssrc(struct state_recompress *s, int idx){
254261
auto [compress_cfg, i] = s->index_to_port[idx];
255262

256263
std::lock_guard<std::mutex> work_lock(s->workers[compress_cfg].ports_mut);
257-
return ultragrid_rtp_get_ssrc(
258-
s->workers[compress_cfg].ports[i].video_rxtx->m_impl_state);
264+
void *impl = vrxtx_get_impl_state(
265+
s->workers[compress_cfg].ports[i].video_rxtx.get());
266+
return ultragrid_rtp_get_ssrc(impl);
259267
}
260268

261269
void recompress_port_set_active(struct state_recompress *s,

src/main.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
#include "video_capture.h"
122122
#include "video_capture_params.h" // for vidcap_params_get_driver, vid...
123123
#include "video_display.h"
124-
#include "video_rxtx.hpp"
124+
#include "video_rxtx.h"
125125

126126
constexpr char MOD_NAME[] = "[main] ";
127127

@@ -379,7 +379,7 @@ static void *capture_thread(void *arg)
379379
tx_frame, tx_frame->callbacks.dispose);
380380
}
381381

382-
uv->state_video_rxtx->send(
382+
vrxtx_send(uv->state_video_rxtx,
383383
std::move(frame)); // std::move really important here (!)
384384

385385
// wait for frame frame to be processed, eg. by compress
@@ -669,7 +669,7 @@ static bool parse_protocol(int ch, char *optarg, struct ug_options *opt) {
669669
col() << SBOLD("\t-x A:proto") " - use specified audio protocol\n";
670670
col() << SBOLD("\t-x V:proto") " - use specified audio protocol\n";
671671
col() << "\nAudio protocol can be one of: " << TBOLD(AUDIO_PROTOCOLS) " (not all must be available)\n";
672-
video_rxtx::list(strcmp(optarg, "fullhelp") == 0);
672+
vrxtx_list(strcmp(optarg, "fullhelp") == 0);
673673
return false;
674674
}
675675
if (set_audio) {
@@ -1390,7 +1390,7 @@ int main(int argc, char *argv[])
13901390
<< "\n";
13911391
col() << TBOLD("Audio codec : ")
13921392
<< get_name_to_audio_codec(ac_params.codec) << "\n";
1393-
col() << TBOLD("Network protocol : ") << video_rxtx::get_long_name(opt.video_protocol) << "\n";
1393+
col() << TBOLD("Network protocol : ") << vrxtx_get_long_name(opt.video_protocol) << "\n";
13941394
col() << TBOLD("Audio FEC : ") << opt.audio.fec_cfg << "\n";
13951395
col() << TBOLD("Video FEC : ") << opt.video.fec << "\n";
13961396
col() << "\n";
@@ -1518,8 +1518,9 @@ int main(int argc, char *argv[])
15181518

15191519
/* also wait for audio threads */
15201520
audio_join(uv.audio);
1521-
if (uv.state_video_rxtx)
1522-
uv.state_video_rxtx->join();
1521+
if (uv.state_video_rxtx) {
1522+
vrxtx_join(uv.state_video_rxtx);
1523+
}
15231524

15241525
export_destroy(opt.common.exporter);
15251526

@@ -1533,7 +1534,7 @@ int main(int argc, char *argv[])
15331534

15341535
if(uv.audio)
15351536
audio_done(uv.audio);
1536-
delete uv.state_video_rxtx;
1537+
vrxtx_destroy(uv.state_video_rxtx);
15371538

15381539
if (uv.capture_device)
15391540
vidcap_done(uv.capture_device);

src/video_capture/ug_input.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#include "video_display.h" // for display_done, display_join, displa...
6060
#include "video_display/pipe.h" // for pipe_frame_recv_delegate
6161
#include "video_frame.h" // for VIDEO_FRAME_DISPOSE, vf_free
62-
#include "video_rxtx.hpp" // for video_rxtx, vrxtx_params, VRXTX_INIT
62+
#include "video_rxtx.h" // for video_rxtx, vrxtx_params, VRXTX_INIT
6363

6464
struct vidcap_params;
6565

src/video_rxtx.cpp

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
*/
3737

38+
#include <atomic>
3839
#include <cassert> // for assert
3940
#include <memory>
4041
#include <sstream>
@@ -43,6 +44,8 @@
4344
#include <string>
4445
#include <utility>
4546

47+
#define WANT_PTHREAD_NULL
48+
#include "compat/misc.h" // for PTHREAD_NULL
4649
#include "debug.h"
4750
#include "export.h"
4851
#include "host.h"
@@ -62,7 +65,7 @@
6265
#include "video_compress.h"
6366
#include "video_decompress.h"
6467
#include "video_display.h"
65-
#include "video_rxtx.hpp"
68+
#include "video_rxtx.h"
6669

6770
constexpr char DEFAULT_VIDEO_COMPRESSION[] = "none";
6871

@@ -73,6 +76,52 @@ using std::shared_ptr;
7376
using std::ostringstream;
7477
using std::string;
7578

79+
struct video_rxtx {
80+
public:
81+
virtual ~video_rxtx() noexcept;
82+
void send(std::shared_ptr<struct video_frame>) noexcept;
83+
static const char *get_long_name(std::string const &short_name) noexcept;
84+
/**
85+
* If overridden, children must call also video_rxtx::join()
86+
*/
87+
virtual void join() noexcept;
88+
static video_rxtx *create(std::string const &name,
89+
const struct vrxtx_params *params,
90+
const struct common_opts *opts) noexcept(false);
91+
static void list(bool full) noexcept;
92+
void set_audio_spec(const struct audio_desc *desc, int audio_rx_port,
93+
int audio_tx_port, bool ipv6) noexcept;
94+
95+
const struct video_rxtx_info *m_impl_funcs = nullptr;
96+
void *m_impl_state = nullptr;
97+
98+
protected:
99+
video_rxtx(const char *protocol_name,
100+
const struct vrxtx_params *params,
101+
const struct common_opts *opts) noexcept(false);
102+
void check_sender_messages();
103+
104+
struct module m_sender_mod;
105+
struct module m_receiver_mod;
106+
unsigned long long int m_frames_sent = 0ull;
107+
struct exporter *m_exporter;
108+
109+
private:
110+
static void *sender_thread(void *args);
111+
void *sender_loop();
112+
113+
struct compress_state *m_compression = nullptr;
114+
pthread_mutex_t m_lock;
115+
116+
pthread_t m_sender_thread_id = PTHREAD_NULL;
117+
bool m_sender_poisoned = false;
118+
bool m_sender_joined = true;
119+
pthread_t m_receiver_thread_id = PTHREAD_NULL;
120+
121+
video_desc m_video_desc{};
122+
std::atomic<codec_t> m_input_codec{};
123+
};
124+
76125
const char *
77126
vrxtx_get_compression(const char *video_protocol, const char *req_compression)
78127
{
@@ -340,6 +389,18 @@ int vrxtx_init(const char *proto_name, const struct vrxtx_params *params,
340389
return 0;
341390
}
342391

392+
void
393+
vrxtx_list(bool full)
394+
{
395+
video_rxtx::list(full);
396+
}
397+
398+
const char *
399+
vrxtx_get_long_name(const char *short_name)
400+
{
401+
return video_rxtx::get_long_name(short_name);
402+
}
403+
343404
void
344405
vrxtx_join(struct video_rxtx *state)
345406
{
@@ -351,3 +412,21 @@ vrxtx_destroy(struct video_rxtx *state)
351412
{
352413
delete state;
353414
}
415+
416+
void vrxtx_set_audio_spec(struct video_rxtx *state,
417+
const struct audio_desc *desc, int audio_rx_port,
418+
int audio_tx_port, bool ipv6) {
419+
state->set_audio_spec(desc, audio_rx_port, audio_tx_port, ipv6);
420+
}
421+
422+
void *
423+
vrxtx_get_impl_state(struct video_rxtx *state)
424+
{
425+
return state->m_impl_state;
426+
}
427+
428+
void
429+
vrxtx_send(struct video_rxtx *state, std::shared_ptr<struct video_frame> f)
430+
{
431+
state->send(std::move(f));
432+
}
Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file video_rxtx.hpp
2+
* @file video_rxtx.h
33
* @author Martin Pulec <pulec@cesnet.cz>
44
*/
55
/*
@@ -39,14 +39,10 @@
3939
#define VIDEO_RXTX_H_
4040

4141
#ifdef __cplusplus
42-
#include <atomic>
43-
#include <memory>
44-
#include <string>
45-
#endif // defined __cplusplus
42+
#include <memory> // for std::shared_ptr
43+
#endif
4644

4745
#include "host.h"
48-
#define WANT_PTHREAD_NULL
49-
#include "compat/misc.h" // for PTHREAD_NULL
5046
#include "module.h"
5147
#include "types.h" // for codec_t, video_desc, video_frame (ptr only)
5248

@@ -114,68 +110,35 @@ struct video_rxtx_info {
114110
bool ipv6);
115111
void *(*receiver_routine)(void *state);
116112
};
117-
118-
struct video_rxtx {
119-
public:
120-
virtual ~video_rxtx() noexcept;
121-
void send(std::shared_ptr<struct video_frame>) noexcept;
122-
static const char *get_long_name(std::string const &short_name) noexcept;
123-
/**
124-
* If overridden, children must call also video_rxtx::join()
125-
*/
126-
virtual void join() noexcept;
127-
static video_rxtx *create(std::string const &name,
128-
const struct vrxtx_params *params,
129-
const struct common_opts *opts) noexcept(false);
130-
static void list(bool full) noexcept;
131-
void set_audio_spec(const struct audio_desc *desc, int audio_rx_port,
132-
int audio_tx_port, bool ipv6) noexcept;
133-
134-
const struct video_rxtx_info *m_impl_funcs = nullptr;
135-
void *m_impl_state = nullptr;
136-
137-
protected:
138-
video_rxtx(const char *protocol_name,
139-
const struct vrxtx_params *params,
140-
const struct common_opts *opts) noexcept(false);
141-
void check_sender_messages();
142-
143-
struct module m_sender_mod;
144-
struct module m_receiver_mod;
145-
unsigned long long int m_frames_sent = 0ull;
146-
struct exporter *m_exporter;
147-
148-
private:
149-
static void *sender_thread(void *args);
150-
void *sender_loop();
151-
152-
struct compress_state *m_compression = nullptr;
153-
pthread_mutex_t m_lock;
154-
155-
pthread_t m_sender_thread_id = PTHREAD_NULL;
156-
bool m_sender_poisoned = false;
157-
bool m_sender_joined = true;
158-
pthread_t m_receiver_thread_id = PTHREAD_NULL;
159-
160-
video_desc m_video_desc{};
161-
std::atomic<codec_t> m_input_codec{};
162-
};
163113
#endif // defined __cplusplus
164114

165115
#ifdef __cplusplus
166116
extern "C" {
167117
#endif
168118

169-
int vrxtx_init(const char *proto_name, const struct vrxtx_params *params,
170-
const struct common_opts *opts, struct video_rxtx **state);
119+
struct video_rxtx;
120+
121+
int vrxtx_init(const char *proto_name, const struct vrxtx_params *params,
122+
const struct common_opts *opts, struct video_rxtx **state);
123+
void vrxtx_destroy(struct video_rxtx *state);
124+
void vrxtx_list(bool full);
125+
const char *vrxtx_get_long_name(const char *short_name);
171126
const char *vrxtx_get_compression(const char *video_protocol,
172127
const char *req_compression);
173-
void vrxtx_join(struct video_rxtx *state);
174-
void vrxtx_destroy(struct video_rxtx *state);
128+
void vrxtx_join(struct video_rxtx *state);
129+
void vrxtx_set_audio_spec(struct video_rxtx *state,
130+
const struct audio_desc *desc, int audio_rx_port,
131+
int audio_tx_port, bool ipv6);
132+
void *vrxtx_get_impl_state(struct video_rxtx *state);
175133

176134
#ifdef __cplusplus
177135
} // extern "C"
178136
#endif
179137

138+
#ifdef __cplusplus
139+
#include <memory>
140+
void vrxtx_send(struct video_rxtx *state, std::shared_ptr<struct video_frame>);
141+
#endif
142+
180143
#endif // VIDEO_RXTX_H_
181144

0 commit comments

Comments
 (0)