UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
hd-rum-recompress.cpp File Reference
#include <cinttypes>
#include <chrono>
#include <memory>
#include <thread>
#include <string>
#include "hd-rum-translator/hd-rum-recompress.h"
#include "debug.h"
#include "host.h"
#include "utils/misc.h"
#include "rtp/rtp.h"
#include "tv.h"
#include "video_compress.h"
#include "video_rxtx/ultragrid_rtp.hpp"
#include "utils/profile_timer.hpp"
Include dependency graph for hd-rum-recompress.cpp:

Go to the source code of this file.

Classes

struct  recompress_output_port
 
struct  recompress_worker_ctx
 
struct  state_recompress
 

Functions

static void extract_port (struct state_recompress *s, const std::string &compress_cfg, int i, recompress_output_port *move_to=nullptr)
 
static int move_port_to_worker (struct state_recompress *s, const char *compress, recompress_output_port &&port)
 
int recompress_add_port (struct state_recompress *s, const char *host, const char *compress, unsigned short rx_port, unsigned short tx_port, const struct common_opts *common, const char *fec, long long bitrate)
 
void recompress_done (struct state_recompress *s)
 
int recompress_get_num_active_ports (struct state_recompress *s)
 
uint32_t recompress_get_port_ssrc (struct state_recompress *s, int idx)
 
struct state_recompress * recompress_init (struct module *parent)
 
bool recompress_port_change_compress (struct state_recompress *s, int index, const char *new_compress)
 
void recompress_port_set_active (struct state_recompress *s, int index, bool active)
 
static void recompress_port_write (recompress_output_port &port, shared_ptr< video_frame > frame)
 
void recompress_process_async (state_recompress *s, std::shared_ptr< video_frame > frame)
 
void recompress_remove_port (struct state_recompress *s, int index)
 
static void recompress_worker (struct recompress_worker_ctx *ctx)
 
static int worker_get_num_active_ports (const recompress_worker_ctx &worker)
 

Detailed Description

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z
Martin Piatka piatk.nosp@m.a@ce.nosp@m.snet..nosp@m.cz

Component of the transcoding reflector that takes an uncompressed frame, recompresses it to another compression and sends it to destination (therefore it wraps the whole sending part of UltraGrid).

Definition in file hd-rum-recompress.cpp.

Function Documentation

◆ extract_port()

static void extract_port ( struct state_recompress * s,
const std::string & compress_cfg,
int i,
recompress_output_port * move_to = nullptr )
static

Definition at line 230 of file hd-rum-recompress.cpp.

233{
234 auto& worker = s->workers[compress_cfg];
235 {
236 std::unique_lock<std::mutex> lock(worker.ports_mut);
237 if(move_to)
238 *move_to = std::move(worker.ports[i]);
239 worker.ports.erase(worker.ports.begin() + i);
240
241 if(worker.ports.empty()){
242 //poison compress
243 compress_frame(worker.compress.get(), nullptr);
244 worker.thread.join();
245 s->workers.erase(compress_cfg);
246 }
247 }
248
249 for(auto& p : s->index_to_port){
250 if(p.first == compress_cfg && p.second > i)
251 p.second--;
252 }
253}
void compress_frame(struct compress_state *proxy, shared_ptr< video_frame > frame)
static void * worker(void *args)
Definition alsa.c:190
std::vector< std::pair< std::string, int > > index_to_port
std::map< std::string, recompress_worker_ctx > workers

References compress_frame(), state_recompress::index_to_port, module::lock, worker(), and state_recompress::workers.

Referenced by recompress_port_change_compress(), and recompress_remove_port().

Here is the call graph for this function:

◆ move_port_to_worker()

static int move_port_to_worker ( struct state_recompress * s,
const char * compress,
recompress_output_port && port )
static

Definition at line 183 of file hd-rum-recompress.cpp.

185{
186 auto& worker = s->workers[compress];
187 if(!worker.compress){
188 worker.compress_cfg = compress;
189 int ret = compress_init(s->parent, compress, out_ptr(worker.compress));
190 if (ret != 0) {
191 s->workers.erase(compress);
192 return -1;
193 }
194
195 worker.thread = std::thread(recompress_worker, &worker);
196 }
197
198 std::lock_guard<std::mutex> lock(worker.ports_mut);
199 int index_in_worker = worker.ports.size();
200 worker.ports.push_back(std::move(port));
201
202 return index_in_worker;
203}
Definition misc.h:94
int compress_init(struct module *parent, const char *config_string, struct compress_state **state)
This function initializes video compression.
static void recompress_worker(struct recompress_worker_ctx *ctx)
struct module * parent

References compress_init(), module::lock, state_recompress::parent, recompress_worker(), worker(), and state_recompress::workers.

Referenced by recompress_add_port(), and recompress_port_change_compress().

Here is the call graph for this function:

◆ recompress_add_port()

int recompress_add_port ( struct state_recompress * s,
const char * host,
const char * compress,
unsigned short rx_port,
unsigned short tx_port,
const struct common_opts * common,
const char * fec,
long long bitrate )

Definition at line 205 of file hd-rum-recompress.cpp.

209{
211
212 try{
213 port = recompress_output_port(host, rx_port, tx_port,
214 common, fec, bitrate);
215 } catch(...) {
216 return -1;
217 }
218
219 std::lock_guard<std::mutex> lock(s->mut);
220 int index_in_worker = move_port_to_worker(s, compress, std::move(port));
221 if(index_in_worker < 0)
222 return -1;
223
224 int index_of_port = s->index_to_port.size();
225 s->index_to_port.emplace_back(compress, index_in_worker);
226
227 return index_of_port;
228}
static int move_port_to_worker(struct state_recompress *s, const char *compress, recompress_output_port &&port)
Definition fec.h:51

References state_recompress::index_to_port, module::lock, move_port_to_worker(), and state_recompress::mut.

Referenced by create_output_port().

Here is the call graph for this function:

◆ recompress_done()

void recompress_done ( struct state_recompress * s)

Definition at line 342 of file hd-rum-recompress.cpp.

342 {
343 {
344 std::lock_guard<std::mutex> lock(s->mut);
345 for(auto& worker : s->workers){
346 //poison compress
347 compress_frame(worker.second.compress.get(), nullptr);
348
349 worker.second.thread.join();
350 compress_done(worker.second.compress.get());
351 }
352 }
353 delete s;
354}
void compress_done(struct compress_state *proxy)
Video compression cleanup function.

References compress_done(), compress_frame(), state_recompress::mut, worker(), and state_recompress::workers.

Referenced by hd_rum_translator_deinit().

Here is the call graph for this function:

◆ recompress_get_num_active_ports()

int recompress_get_num_active_ports ( struct state_recompress * s)

Definition at line 313 of file hd-rum-recompress.cpp.

313 {
314 std::lock_guard<std::mutex> lock(s->mut);
315 int ret = 0;
316 for(const auto& worker : s->workers){
317 ret += worker_get_num_active_ports(worker.second);
318 }
319
320 return ret;
321}
static int worker_get_num_active_ports(const recompress_worker_ctx &worker)

References module::lock, state_recompress::mut, worker(), worker_get_num_active_ports(), and state_recompress::workers.

Referenced by writer().

Here is the call graph for this function:

◆ recompress_get_port_ssrc()

uint32_t recompress_get_port_ssrc ( struct state_recompress * s,
int idx )

Definition at line 263 of file hd-rum-recompress.cpp.

263 {
264 std::lock_guard<std::mutex> lock(s->mut);
265 auto [compress_cfg, i] = s->index_to_port[idx];
266
267 std::lock_guard<std::mutex> work_lock(s->workers[compress_cfg].ports_mut);
268 return s->workers[compress_cfg].ports[i].video_rxtx->get_ssrc();
269}

References state_recompress::index_to_port, module::lock, state_recompress::mut, and state_recompress::workers.

Referenced by writer().

◆ recompress_init()

struct state_recompress * recompress_init ( struct module * parent)

Definition at line 323 of file hd-rum-recompress.cpp.

323 {
324 auto state = new state_recompress();
325 if(!state)
326 return nullptr;
327
328 state->parent = parent;
329
330 return state;
331}
void ** state
const char * parent
Definition settings.cpp:198

References parent, and state.

Referenced by main().

◆ recompress_port_change_compress()

bool recompress_port_change_compress ( struct state_recompress * s,
int index,
const char * new_compress )

Definition at line 281 of file hd-rum-recompress.cpp.

283{
284 std::lock_guard<std::mutex> lock(s->mut);
285 auto [old_compress, i] = s->index_to_port[index];
286
287 if(old_compress == new_compress)
288 return true;
289
291 extract_port(s, old_compress, i, &port);
292 int index_in_worker = move_port_to_worker(s, new_compress, std::move(port));
293
294 if(index_in_worker < 0){
295 s->index_to_port.erase(s->index_to_port.begin() + index);
296 return false;
297 }
298
299 s->index_to_port[index] = {new_compress, index_in_worker};
300
301 return true;
302}
static void extract_port(struct state_recompress *s, const std::string &compress_cfg, int i, recompress_output_port *move_to=nullptr)

References extract_port(), state_recompress::index_to_port, module::lock, move_port_to_worker(), and state_recompress::mut.

Referenced by change_replica_type().

Here is the call graph for this function:

◆ recompress_port_set_active()

void recompress_port_set_active ( struct state_recompress * s,
int index,
bool active )

Definition at line 271 of file hd-rum-recompress.cpp.

273{
274 std::lock_guard<std::mutex> lock(s->mut);
275 auto [compress_cfg, i] = s->index_to_port[index];
276
277 std::unique_lock<std::mutex> worker_lock(s->workers[compress_cfg].ports_mut);
278 s->workers[compress_cfg].ports[i].active = active;
279}

References state_recompress::index_to_port, module::lock, state_recompress::mut, and state_recompress::workers.

Referenced by change_replica_type(), and create_output_port().

◆ recompress_port_write()

static void recompress_port_write ( recompress_output_port & port,
shared_ptr< video_frame > frame )
static

Definition at line 146 of file hd-rum-recompress.cpp.

147{
149
150 port.frames += 1;
151
152 auto now = chrono::steady_clock::now();
153
154 double seconds = chrono::duration_cast<chrono::duration<double>>(now - port.t0).count();
155 if(seconds > 5) {
156 double fps = port.frames / seconds;
157 log_msg(LOG_LEVEL_INFO, "[0x%08" PRIx32 "->%s:%d:0x%08" PRIx32 "] %d frames in %g seconds = %g FPS\n",
158 frame->ssrc,
159 port.host.c_str(), port.tx_port,
160 port.video_rxtx->get_ssrc(),
161 port.frames, seconds, fps);
162 port.t0 = now;
163 port.frames = 0;
164 }
165
166 port.video_rxtx->send(std::move(frame));
167}
struct video_frame * frame
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_INFO
normal reporting
Definition debug.h:62
int count
Definition hd-rum.c:44
#define PROFILE_FUNC
std::unique_ptr< ultragrid_rtp_video_rxtx > video_rxtx
std::chrono::steady_clock::time_point t0
uint32_t ssrc
Definition types.h:330

References count, frame, recompress_output_port::frames, recompress_output_port::host, LOG_LEVEL_INFO, log_msg(), PROFILE_FUNC, video_frame::ssrc, recompress_output_port::t0, recompress_output_port::tx_port, and recompress_output_port::video_rxtx.

Referenced by recompress_worker().

Here is the call graph for this function:

◆ recompress_process_async()

void recompress_process_async ( state_recompress * s,
std::shared_ptr< video_frame > frame )

Definition at line 333 of file hd-rum-recompress.cpp.

333 {
335 std::lock_guard<std::mutex> lock(s->mut);
336 for(const auto& worker : s->workers){
337 if(worker_get_num_active_ports(worker.second) > 0)
338 compress_frame(worker.second.compress.get(), frame);
339 }
340}

References compress_frame(), frame, state_recompress::mut, PROFILE_FUNC, worker(), worker_get_num_active_ports(), and state_recompress::workers.

Referenced by hd_rum_decompress::state_transcoder_decompress::worker().

Here is the call graph for this function:

◆ recompress_remove_port()

void recompress_remove_port ( struct state_recompress * s,
int index )

Definition at line 255 of file hd-rum-recompress.cpp.

255 {
256 std::lock_guard<std::mutex> lock(s->mut);
257 auto [compress_cfg, i] = s->index_to_port[index];
258
259 extract_port(s, compress_cfg, i);
260 s->index_to_port.erase(s->index_to_port.begin() + index);
261}

References extract_port(), state_recompress::index_to_port, module::lock, and state_recompress::mut.

Referenced by writer().

Here is the call graph for this function:

◆ recompress_worker()

static void recompress_worker ( struct recompress_worker_ctx * ctx)
static

Definition at line 169 of file hd-rum-recompress.cpp.

169 {
171 assert(ctx->compress);
172
173 while(auto frame = compress_pop(ctx->compress.get())){
174 std::lock_guard<std::mutex> lock(ctx->ports_mut);
175 for(auto& port : ctx->ports){
176 if(port.active)
178 }
179 PROFILE_DETAIL("compress_pop");
180 }
181}
shared_ptr< video_frame > compress_pop(struct compress_state *proxy)
static void recompress_port_write(recompress_output_port &port, shared_ptr< video_frame > frame)
#define PROFILE_DETAIL(name)
std::unique_ptr< compress_state, compress_state_deleter > compress
std::vector< recompress_output_port > ports

References recompress_worker_ctx::compress, compress_pop(), frame, module::lock, recompress_worker_ctx::ports, recompress_worker_ctx::ports_mut, PROFILE_DETAIL, PROFILE_FUNC, and recompress_port_write().

Referenced by move_port_to_worker().

Here is the call graph for this function:

◆ worker_get_num_active_ports()

static int worker_get_num_active_ports ( const recompress_worker_ctx & worker)
static

Definition at line 304 of file hd-rum-recompress.cpp.

304 {
305 int ret = 0;
306 for(const auto& port : worker.ports){
307 if(port.active)
308 ret++;
309 }
310 return ret;
311}

References worker().

Referenced by recompress_get_num_active_ports(), and recompress_process_async().

Here is the call graph for this function: