UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
unix_sock.cpp File Reference
#include "config.h"
#include "config_unix.h"
#include "config_win32.h"
#include "debug.h"
#include "lib_common.h"
#include <condition_variable>
#include <list>
#include <map>
#include <vector>
#include <memory>
#include <mutex>
#include <queue>
#include <cmath>
#include <iostream>
#include <thread>
#include "host.h"
#include "video.h"
#include "video_display.h"
#include "video_codec.h"
#include "utils/fs.h"
#include "utils/misc.h"
#include "utils/macros.h"
#include "utils/color_out.h"
#include "utils/string_view_utils.hpp"
#include "tools/ipc_frame.h"
#include "tools/ipc_frame_ug.h"
#include "tools/ipc_frame_unix.h"
Include dependency graph for unix_sock.cpp:

Go to the source code of this file.

Classes

struct  state_unix_sock
 

Macros

#define DEFAULT_DISP_FILENAME   "ug_unix"
 
#define DEFAULT_PREVIEW_FILENAME   "ug_preview_disp_unix"
 
#define DEFAULT_SCALE_H   540
 
#define DEFAULT_SCALE_W   960
 
#define MOD_NAME   "[unix sock disp] "
 

Typedefs

using ipc_frame_conv_func_t
 

Functions

static void display_unix_sock_done (void *state)
 
static bool display_unix_sock_get_property (void *state, int property, void *val, size_t *len)
 
static struct video_frame * display_unix_sock_getf (void *state)
 
static void * display_unix_sock_init (struct module *parent, const char *fmt, unsigned int flags, bool is_preview)
 
static void * display_unix_sock_init_no_preview (struct module *parent, const char *fmt, unsigned int flags)
 
static void * display_unix_sock_init_preview (struct module *parent, const char *fmt, unsigned int flags)
 
static void display_unix_sock_probe (struct device_info **available_cards, int *count, void(**deleter)(void *))
 
static void display_unix_sock_put_audio_frame (void *state, const struct audio_frame *frame)
 
static bool display_unix_sock_putf (void *state, struct video_frame *frame, long long flags)
 
static bool display_unix_sock_reconfigure (void *state, struct video_desc desc)
 
static bool display_unix_sock_reconfigure_audio (void *state, int quant_samples, int channels, int sample_rate)
 
static void display_unix_sock_run (void *state)
 
 REGISTER_HIDDEN_MODULE (preview, &display_preview_info, LIBRARY_CLASS_VIDEO_DISPLAY, VIDEO_DISPLAY_ABI_VERSION)
 
 REGISTER_HIDDEN_MODULE (unix_sock, &display_unix_sock_info, LIBRARY_CLASS_VIDEO_DISPLAY, VIDEO_DISPLAY_ABI_VERSION)
 
static void show_help ()
 

Variables

static const struct video_display_info display_preview_info
 
static const struct video_display_info display_unix_sock_info
 
static constexpr unsigned int IN_QUEUE_MAX_BUFFER_LEN = 5
 
static constexpr int SKIP_FIRST_N_FRAMES_IN_STREAM = 5
 

Detailed Description

Author
Martin Piatka piatk.nosp@m.a@ce.nosp@m.snet..nosp@m.cz

Definition in file unix_sock.cpp.

Macro Definition Documentation

◆ DEFAULT_DISP_FILENAME

#define DEFAULT_DISP_FILENAME   "ug_unix"

Definition at line 71 of file unix_sock.cpp.

Referenced by display_unix_sock_init(), and show_help().

◆ DEFAULT_PREVIEW_FILENAME

#define DEFAULT_PREVIEW_FILENAME   "ug_preview_disp_unix"

Definition at line 70 of file unix_sock.cpp.

Referenced by display_unix_sock_init(), and show_help().

◆ DEFAULT_SCALE_H

#define DEFAULT_SCALE_H   540

Definition at line 74 of file unix_sock.cpp.

Referenced by display_unix_sock_init(), and show_help().

◆ DEFAULT_SCALE_W

#define DEFAULT_SCALE_W   960

Definition at line 73 of file unix_sock.cpp.

Referenced by display_unix_sock_init(), and show_help().

◆ MOD_NAME

#define MOD_NAME   "[unix sock disp] "

Typedef Documentation

◆ ipc_frame_conv_func_t

Initial value:
bool (*)(struct Ipc_frame *dst,
const struct video_frame *src,
unsigned scale_factor)
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
codec_t
Definition types.h:65

Definition at line 76 of file unix_sock.cpp.

Function Documentation

◆ display_unix_sock_done()

static void display_unix_sock_done ( void * state)
static

Definition at line 232 of file unix_sock.cpp.

233{
234 auto s = static_cast<state_unix_sock *>(state);
235 s->thread_id.join();
236
237 delete s;
238}
void ** state
std::thread thread_id

References state, and state_unix_sock::thread_id.

◆ display_unix_sock_get_property()

static bool display_unix_sock_get_property ( void * state,
int property,
void * val,
size_t * len )
static

Definition at line 274 of file unix_sock.cpp.

275{
276 UNUSED(state);
277 codec_t codecs[] = {UYVY, RGBA, RGB};
278 enum interlacing_t supported_il_modes[] = {PROGRESSIVE, INTERLACED_MERGED, SEGMENTED_FRAME};
279 int rgb_shift[] = {0, 8, 16};
280
281 switch (property) {
283 if(sizeof(codecs) <= *len) {
284 memcpy(val, codecs, sizeof(codecs));
285 } else {
286 return false;
287 }
288
289 *len = sizeof(codecs);
290 break;
292 if(sizeof(rgb_shift) > *len) {
293 return false;
294 }
295 memcpy(val, rgb_shift, sizeof(rgb_shift));
296 *len = sizeof(rgb_shift);
297 break;
299 *(int *) val = PITCH_DEFAULT;
300 *len = sizeof(int);
301 break;
303 if(sizeof(supported_il_modes) <= *len) {
304 memcpy(val, supported_il_modes, sizeof(supported_il_modes));
305 } else {
306 return false;
307 }
308 *len = sizeof(supported_il_modes);
309 break;
310 default:
311 return false;
312 }
313 return true;
314}
#define UNUSED(x)
int void * val
int void size_t * len
int property
#define PITCH_DEFAULT
default pitch, i. e. respective linesize
@ DISPLAY_PROPERTY_BUF_PITCH
requested framebuffer pitch - int (bytes), may be PITCH_DEFAULT
@ DISPLAY_PROPERTY_RGB_SHIFT
red,green,blue shift - int[3] (bits)
@ DISPLAY_PROPERTY_SUPPORTED_IL_MODES
display supported interlacing modes - enum interlacing_t[]
@ DISPLAY_PROPERTY_CODECS
list of natively supported codecs - codec_t[]
interlacing_t
Definition types.h:163
@ PROGRESSIVE
progressive frame
Definition types.h:164
@ SEGMENTED_FRAME
Segmented frame. Contains the same data as progressive frame.
Definition types.h:168
@ INTERLACED_MERGED
Columngs of both fields are interlaced together.
Definition types.h:167
@ RGB
RGB 8-bit (packed into 24-bit word)
Definition types.h:80
@ UYVY
YCbCr 422 8-bit - Cb Y0 Cr Y1.
Definition types.h:71
@ RGBA
RGBA 8-bit, big-endian.
Definition types.h:70
static struct @76 codecs[]

References codecs, DISPLAY_PROPERTY_BUF_PITCH, DISPLAY_PROPERTY_CODECS, DISPLAY_PROPERTY_RGB_SHIFT, DISPLAY_PROPERTY_SUPPORTED_IL_MODES, INTERLACED_MERGED, len, PITCH_DEFAULT, PROGRESSIVE, property, RGB, RGBA, SEGMENTED_FRAME, state, UNUSED, UYVY, and val.

◆ display_unix_sock_getf()

static struct video_frame * display_unix_sock_getf ( void * state)
static

Definition at line 240 of file unix_sock.cpp.

241{
242 auto s = static_cast<state_unix_sock *>(state);
243
244 return vf_alloc_desc_data(s->desc);
245}
struct video_frame * vf_alloc_desc_data(struct video_desc desc)

References state, and vf_alloc_desc_data().

Here is the call graph for this function:

◆ display_unix_sock_init()

static void * display_unix_sock_init ( struct module * parent,
const char * fmt,
unsigned int flags,
bool is_preview )
static

Definition at line 128 of file unix_sock.cpp.

132{
133 UNUSED(flags);
134 UNUSED(fmt);
135
136 auto s = std::make_unique<state_unix_sock>();
137
138 std::string_view fmt_sv = fmt ? fmt : "";
139
140 std::string socket_path = get_temp_dir();
141 socket_path += DEFAULT_DISP_FILENAME;
142
143 if(is_preview){
144 socket_path = get_temp_dir();
145 socket_path += DEFAULT_PREVIEW_FILENAME;
146 s->target_width = DEFAULT_SCALE_W;
147 s->target_height = DEFAULT_SCALE_H;
148 s->ignore_putf_blocking = true;
149 }
150
151 while(!fmt_sv.empty()){
152 auto tok = tokenize(fmt_sv, ':', '"');
153 auto key = tokenize(tok, '=');
154
155 if(key == "help"){
156 show_help();
157 return INIT_NOERR;
158 } else if(key == "path"){
159 socket_path = tokenize(tok, '=');
160 } else if(key == "key"){
161 socket_path += tokenize(tok, '=');
162 } else if(key == "hq"){
163 s->ipc_conv = ipc_frame_from_ug_frame_hq;
164 } else if(key == "target_size"){
165 auto val = tokenize(tok, '=');
166 if(!parse_num(tokenize(val, 'x'), s->target_width)
167 || !parse_num(tokenize(val, 'x'), s->target_height))
168 {
169 log_msg(LOG_LEVEL_ERROR, MOD_NAME "Failed to parse resolution\n");
170 return nullptr;
171 }
172 }
173 }
174
175 s->parent = parent;
176 s->ipc_frame.reset(ipc_frame_new());
177 s->frame_writer.reset(ipc_frame_writer_new(socket_path.c_str()));
178 if(!s->frame_writer){
179 log_msg(LOG_LEVEL_ERROR, MOD_NAME "Unable to init ipc writer for path %s\n", socket_path.c_str());
180 return nullptr;
181 }
182
183 s->thread_id = std::thread(display_unix_sock_run, s.get());
184 return s.release();
185}
const char * fmt
const char unsigned int flags
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define INIT_NOERR
Definition host.h:73
Ipc_frame * ipc_frame_new()
Definition ipc_frame.cpp:48
bool ipc_frame_from_ug_frame_hq(struct Ipc_frame *dst, const struct video_frame *src, codec_t codec, unsigned scale_factor)
Same as ipc_frame_from_ug_frame, but convert pixfmt first and scale later.
Ipc_frame_writer * ipc_frame_writer_new(const char *path)
static const char * get_temp_dir(void)
const char * parent
Definition settings.cpp:198
std::string_view tokenize(std::string_view &str, char delim, char quot)
Tokenizer for string_view.
bool parse_num(std::string_view sv, T &res, int base=10)
#define DEFAULT_SCALE_H
Definition unix_sock.cpp:74
#define DEFAULT_PREVIEW_FILENAME
Definition unix_sock.cpp:70
static void show_help()
static void display_unix_sock_run(void *state)
#define MOD_NAME
Definition unix_sock.cpp:68
#define DEFAULT_SCALE_W
Definition unix_sock.cpp:73
#define DEFAULT_DISP_FILENAME
Definition unix_sock.cpp:71
const char * key
Definition cineform.cpp:125

References DEFAULT_DISP_FILENAME, DEFAULT_PREVIEW_FILENAME, DEFAULT_SCALE_H, DEFAULT_SCALE_W, display_unix_sock_run(), flags, fmt, get_temp_dir(), INIT_NOERR, ipc_frame_from_ug_frame_hq(), ipc_frame_new(), ipc_frame_writer_new(), key, LOG_LEVEL_ERROR, log_msg(), MOD_NAME, parent, parse_num(), show_help(), tokenize(), UNUSED, and val.

Referenced by display_unix_sock_init_no_preview(), and display_unix_sock_init_preview().

Here is the call graph for this function:

◆ display_unix_sock_init_no_preview()

static void * display_unix_sock_init_no_preview ( struct module * parent,
const char * fmt,
unsigned int flags )
static

Definition at line 352 of file unix_sock.cpp.

352 {
353 return display_unix_sock_init(parent, fmt, flags, false);
354}
static void * display_unix_sock_init(struct module *parent, const char *fmt, unsigned int flags, bool is_preview)

References display_unix_sock_init(), flags, fmt, and parent.

Here is the call graph for this function:

◆ display_unix_sock_init_preview()

static void * display_unix_sock_init_preview ( struct module * parent,
const char * fmt,
unsigned int flags )
static

Definition at line 348 of file unix_sock.cpp.

348 {
349 return display_unix_sock_init(parent, fmt, flags, true);
350}

References display_unix_sock_init(), flags, fmt, and parent.

Here is the call graph for this function:

◆ display_unix_sock_probe()

static void display_unix_sock_probe ( struct device_info ** available_cards,
int * count,
void(** deleter )(void *) )
static

Definition at line 342 of file unix_sock.cpp.

342 {
344 *available_cards = nullptr;
345 *count = 0;
346}
int void(** deleter)(void *))
int count
Definition hd-rum.c:44

References count, deleter, and UNUSED.

◆ display_unix_sock_put_audio_frame()

static void display_unix_sock_put_audio_frame ( void * state,
const struct audio_frame * frame )
static

Definition at line 325 of file unix_sock.cpp.

326{
327 UNUSED(state);
328 UNUSED(frame);
329}
struct video_frame * frame

References frame, state, and UNUSED.

◆ display_unix_sock_putf()

static bool display_unix_sock_putf ( void * state,
struct video_frame * frame,
long long flags )
static

Definition at line 247 of file unix_sock.cpp.

248{
249 auto s = static_cast<state_unix_sock *>(state);
250 auto f = unique_frame(frame);
251
252 if (flags == PUTF_DISCARD)
253 return true;
254
255 std::unique_lock<std::mutex> lg(s->lock);
256 if (s->incoming_queue.size() >= IN_QUEUE_MAX_BUFFER_LEN){
257 if(flags != PUTF_BLOCKING){
258 log_msg(LOG_LEVEL_WARNING, MOD_NAME "queue full!\n");
259 return false;
260 }
261 if(s->ignore_putf_blocking){
262 return false;
263 }
264 }
265
266 s->frame_consumed_cv.wait(lg, [s]{return s->incoming_queue.size() < IN_QUEUE_MAX_BUFFER_LEN;});
267 s->incoming_queue.push(std::move(f));
268 lg.unlock();
269 s->frame_available_cv.notify_one();
270
271 return true;
272}
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
#define PUTF_DISCARD
#define PUTF_BLOCKING
static constexpr unsigned int IN_QUEUE_MAX_BUFFER_LEN
Definition unix_sock.cpp:81
std::unique_ptr< video_frame, frame_deleter > unique_frame
Definition pipewire.cpp:78

References flags, frame, IN_QUEUE_MAX_BUFFER_LEN, LOG_LEVEL_WARNING, log_msg(), MOD_NAME, PUTF_BLOCKING, PUTF_DISCARD, and state.

Here is the call graph for this function:

◆ display_unix_sock_reconfigure()

static bool display_unix_sock_reconfigure ( void * state,
struct video_desc desc )
static

Definition at line 316 of file unix_sock.cpp.

317{
318 auto s = static_cast<state_unix_sock *>(state);
319
320 s->desc = desc;
321
322 return true;
323}
struct video_desc desc
struct video_desc desc
Definition unix_sock.cpp:97

References desc, state_unix_sock::desc, and state.

◆ display_unix_sock_reconfigure_audio()

static bool display_unix_sock_reconfigure_audio ( void * state,
int quant_samples,
int channels,
int sample_rate )
static

Definition at line 331 of file unix_sock.cpp.

333{
334 UNUSED(state);
338
339 return false;
340}
int int int sample_rate
int int channels
int quant_samples

References channels, quant_samples, sample_rate, state, and UNUSED.

◆ display_unix_sock_run()

static void display_unix_sock_run ( void * state)
static

Definition at line 187 of file unix_sock.cpp.

188{
189 auto s = static_cast<state_unix_sock *>(state);
190 int skipped = 0;
191
192 while (1) {
193 auto frame = [&]{
194 std::unique_lock<std::mutex> l(s->lock);
195 s->frame_available_cv.wait(l,
196 [s]{return s->incoming_queue.size() > 0;});
197 auto frame = std::move(s->incoming_queue.front());
198 s->incoming_queue.pop();
199 s->frame_consumed_cv.notify_one();
200 return frame;
201 }();
202
203 if (!frame) {
204 break;
205 }
206
207 if (skipped < SKIP_FIRST_N_FRAMES_IN_STREAM){
208 skipped++;
209 continue;
210 }
211
212 const tile *tile = &frame->tiles[0];
213
215 s->target_width, s->target_height);
216
217 if(!s->ipc_conv(s->ipc_frame.get(), frame.get(),
218 RGB, scale))
219 {
220 log_msg(LOG_LEVEL_WARNING, MOD_NAME "Unable to convert\n");
221 continue;
222 }
223
224 errno = 0;
225 if(!ipc_frame_writer_write(s->frame_writer.get(), s->ipc_frame.get())){
226 perror(MOD_NAME "Unable to send frame");
227 continue;
228 }
229 }
230}
int ipc_frame_get_scale_factor(int src_w, int src_h, int target_w, int target_h)
bool ipc_frame_writer_write(struct Ipc_frame_writer *writer, const struct Ipc_frame *f)
Struct tile is an area of video_frame.
Definition types.h:279
unsigned int width
Definition types.h:280
unsigned int height
Definition types.h:281
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
Definition types.h:345
static constexpr int SKIP_FIRST_N_FRAMES_IN_STREAM
Definition unix_sock.cpp:82

References frame, tile::height, ipc_frame_get_scale_factor(), ipc_frame_writer_write(), LOG_LEVEL_WARNING, log_msg(), MOD_NAME, RGB, SKIP_FIRST_N_FRAMES_IN_STREAM, state, video_frame::tiles, and tile::width.

Referenced by display_unix_sock_init().

Here is the call graph for this function:

◆ REGISTER_HIDDEN_MODULE() [1/2]

REGISTER_HIDDEN_MODULE ( preview ,
& display_preview_info,
LIBRARY_CLASS_VIDEO_DISPLAY ,
VIDEO_DISPLAY_ABI_VERSION  )

◆ REGISTER_HIDDEN_MODULE() [2/2]

REGISTER_HIDDEN_MODULE ( unix_sock ,
& display_unix_sock_info,
LIBRARY_CLASS_VIDEO_DISPLAY ,
VIDEO_DISPLAY_ABI_VERSION  )

◆ show_help()

static void show_help ( void )
static

Definition at line 114 of file unix_sock.cpp.

114 {
115 col() << "unix_socket/preview display. The two display are identical apart from their defaults and the fact that preview never blocks on putf().\n";
116 col() << "usage:\n";
117 col() << TBOLD(TRED("\t-d (unix_socket|preview)") << "[:path=<path>][:target_size=<w>x<h>]")
118 << "\n\n";
119 col() << "options:\n";
120 col() << TBOLD("\tpath=<path>") << "\tpath to unix socket to connect to. Defaults are \""
121 << get_temp_dir() << DEFAULT_PREVIEW_FILENAME "\" for preview and \""
122 << get_temp_dir() << DEFAULT_DISP_FILENAME "\" for unix_sock\n";
123 col() << TBOLD("\ttarget_size=<w>x<h>")<< "\tScales the video frame so that the total number of pixel is around <w>x<h>. If -1x-1 is passed, no scaling takes place."
124 << "Defaults are -1x-1 for unix_sock and " TOSTRING(DEFAULT_SCALE_W) "x" TOSTRING(DEFAULT_SCALE_H) " for preview.\n";
125 col() << TBOLD("\thq") << "\tUse higher quality downscale\n";
126}
#define TRED(x)
Definition color_out.h:75
#define TBOLD(x)
Definition color_out.h:70
#define TOSTRING(A)
Definition macros.h:49

References DEFAULT_DISP_FILENAME, DEFAULT_PREVIEW_FILENAME, DEFAULT_SCALE_H, DEFAULT_SCALE_W, get_temp_dir(), TBOLD, TOSTRING, and TRED.

Referenced by display_unix_sock_init().

Here is the call graph for this function:

Variable Documentation

◆ display_preview_info

const struct video_display_info display_preview_info
static
Initial value:
= {
NULL,
"[unix sock preview] ",
}
static bool display_unix_sock_reconfigure_audio(void *state, int quant_samples, int channels, int sample_rate)
static void * display_unix_sock_init_preview(struct module *parent, const char *fmt, unsigned int flags)
static void display_unix_sock_probe(struct device_info **available_cards, int *count, void(**deleter)(void *))
static void display_unix_sock_done(void *state)
static bool display_unix_sock_reconfigure(void *state, struct video_desc desc)
static bool display_unix_sock_putf(void *state, struct video_frame *frame, long long flags)
static bool display_unix_sock_get_property(void *state, int property, void *val, size_t *len)
static struct video_frame * display_unix_sock_getf(void *state)
static void display_unix_sock_put_audio_frame(void *state, const struct audio_frame *frame)

Definition at line 370 of file unix_sock.cpp.

◆ display_unix_sock_info

◆ IN_QUEUE_MAX_BUFFER_LEN

unsigned int IN_QUEUE_MAX_BUFFER_LEN = 5
staticconstexpr

Definition at line 81 of file unix_sock.cpp.

Referenced by display_unix_sock_putf().

◆ SKIP_FIRST_N_FRAMES_IN_STREAM

int SKIP_FIRST_N_FRAMES_IN_STREAM = 5
staticconstexpr

Definition at line 82 of file unix_sock.cpp.

Referenced by display_unix_sock_run().