UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
loopback.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2018-2023 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
43#ifdef HAVE_CONFIG_H
44#include "config.h"
45#include "config_unix.h"
46#include "config_win32.h"
47#endif // HAVE_CONFIG_H
48
50
51#include <chrono>
52
53#include "debug.h"
54#include "host.h"
55#include "lib_common.h"
56#include "utils/thread.h"
57#include "video_display.h"
58#include "video_frame.h"
59
60using std::chrono::milliseconds;
61using std::condition_variable;
62using std::mutex;
63using std::unique_lock;
64
65static const int BUFF_MAX_LEN = 2;
66static const char *MODULE_NAME = "[loopback] ";
67
68loopback_video_rxtx::loopback_video_rxtx(std::map<std::string, param_u> const &params)
70{
71 m_display_device = static_cast<struct display *>(params.at("display_device").ptr);
72}
73
77
79{
80 auto s = static_cast<loopback_video_rxtx *>(arg);
81 return s->receiver_loop();
82}
83
84void
85loopback_video_rxtx::send_frame(std::shared_ptr<video_frame> f) noexcept
86{
87 unique_lock<mutex> lk(m_lock);
88 if (m_frames.size() >= BUFF_MAX_LEN) {
89 LOG(LOG_LEVEL_WARNING) << MODULE_NAME << "Max buffer len " <<
90 BUFF_MAX_LEN << " exceeded.\n";
91 }
92 m_frames.push(f);
93 lk.unlock();
94 m_frame_ready.notify_one();
95}
96
98{
99 set_thread_name(__func__);
100 while (!m_should_exit) {
101 unique_lock<mutex> lk(m_lock);
102 m_frame_ready.wait_for(lk, milliseconds(100), [this]{return m_frames.size() > 0;});
103 if (m_frames.size() == 0) {
104 continue;
105 }
106
107 auto frame = m_frames.front();
108 m_frames.pop();
109 lk.unlock();
110 auto new_desc = video_desc_from_frame(frame.get());
111 if (m_configure_desc != new_desc) {
113 LOG(LOG_LEVEL_ERROR) << "Unable to reconfigure display!\n";
114 continue;
115 }
116 m_configure_desc = new_desc;
117 }
118 auto display_f = display_get_frame(m_display_device);
119 memcpy(display_f->tiles[0].data, frame->tiles[0].data, frame->tiles[0].data_len);
121 }
123 return nullptr;
124}
125
126void *(*loopback_video_rxtx::get_receiver_thread() noexcept)(void *arg)
127{
128 return receiver_thread;
129}
130
131static video_rxtx *create_video_rxtx_loopback(std::map<std::string, param_u> const &params)
132{
133 return new loopback_video_rxtx(params);
134}
135
137 "loopback dummy transport",
139};
140
142
struct video_frame * frame
struct video_desc video_desc_from_frame(const struct video_frame *frame)
Returns struct video_desc from video frame.
virtual void send_frame(std::shared_ptr< video_frame >) noexcept override
Definition loopback.cpp:85
void * receiver_loop()
Definition loopback.cpp:97
loopback_video_rxtx(std::map< std::string, param_u > const &)
Definition loopback.cpp:68
std::queue< std::shared_ptr< video_frame > > m_frames
Definition loopback.hpp:64
virtual void *(*)(void *arg) get_receiver_thread() noexcept
Definition loopback.hpp:126
virtual ~loopback_video_rxtx()
Definition loopback.cpp:74
std::condition_variable m_frame_ready
Definition loopback.hpp:65
std::mutex m_lock
Definition loopback.hpp:66
static void * receiver_thread(void *arg)
Definition loopback.cpp:78
struct display * m_display_device
Definition loopback.hpp:62
#define FALSE
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define LOG(level)
Definition debug.h:313
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
bool display_put_frame(struct display *d, struct video_frame *frame, long long timeout_ns)
Puts filled video frame. After calling this function, video frame cannot be used.
#define PUTF_BLOCKING
struct video_frame * display_get_frame(struct display *d)
Returns video framebuffer which will be written to.
@ LIBRARY_CLASS_VIDEO_RXTX
Definition lib_common.h:84
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
Definition lib_common.h:144
static const int BUFF_MAX_LEN
Definition loopback.cpp:65
static const char * MODULE_NAME
Definition loopback.cpp:66
static video_rxtx * create_video_rxtx_loopback(std::map< std::string, param_u > const &params)
Definition loopback.cpp:131
static const struct video_rxtx_info loopback_video_rxtx_info
Definition loopback.cpp:136
This struct represents initialized video display state.
char * data
Raw tile data. Pointer must be at least 4B aligned.
Definition types.h:287
unsigned int data_len
length of the data
Definition types.h:288
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
Definition types.h:345
bool m_should_exit
void set_thread_name(const char *name)
Definition thread.c:72
@ VIDEO_NORMAL
normal video (one tile)
Definition types.h:356
bool display_reconfigure(struct display *d, struct video_desc desc, enum video_mode video_mode)
Reconfigure display to new video format.
#define VIDEO_RXTX_ABI_VERSION