UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
pipewire_common.hpp
Go to the documentation of this file.
1#ifndef PIPEWIRE_COMMON_HPP_b6f55dc83d84
2#define PIPEWIRE_COMMON_HPP_b6f55dc83d84
3
4#include <memory>
5#include <string>
6#include <string_view>
7#include <vector>
8#include <mutex>
9#include <spa/param/audio/format-utils.h>
10#include <spa/debug/types.h> //For pw format to string
11#include <pipewire/pipewire.h>
12#include "types.h"
13
14template<typename T, auto delete_func>
15struct raii_deleter_helper { void operator()(T *p) { delete_func(p); } };
16template<typename T, auto delete_func>
17using raii_uniq_handle = std::unique_ptr<T, raii_deleter_helper<T, delete_func>>;
18
19template<typename T>
20struct raii_proxy_deleter_helper { void operator()(T *p) { pw_proxy_destroy(reinterpret_cast<pw_proxy *>(p)); } };
21template<typename T>
22using raii_uniq_proxy_handle = std::unique_ptr<T, raii_proxy_deleter_helper<T>>;
23
30
33 spa_zero(hook);
34 }
36 /*Check if hook is initialized.
37 * Needed only for old pw versions before commit 2394413e */
38 if(!!hook.link.prev)
39 spa_hook_remove(&hook);
40 }
43
44 spa_hook& get() { return hook; }
45
46 spa_hook hook;
47};
48
50public:
51 pipewire_thread_loop_lock_guard(pw_thread_loop *loop) : l(loop) {
52 pw_thread_loop_lock(l);
53 }
55 pw_thread_loop_unlock(l);
56 }
59
60private:
61 pw_thread_loop *l;
62};
63
65public:
69 pipewire_init_guard(pipewire_init_guard&& o) { std::swap(initialized, o.initialized);}
71 std::swap(initialized, o.initialized);
72 return *this;
73 }
74
76#if !PW_CHECK_VERSION(0, 3, 49)
77 std::lock_guard<std::mutex> l(mut);
78 if(initialized){
79 init_count--;
80 if(init_count > 0)
81 return;
82 }
83#endif
84 if(initialized)
85 pw_deinit();
86 }
87
88 void init(){
89#if !PW_CHECK_VERSION(0, 3, 49)
90 std::lock_guard<std::mutex> l(mut);
91 init_count++;
92#endif
93 pw_init(nullptr, nullptr);
94 initialized = true;
95 }
96
97private:
98 bool initialized = false;
99#if !PW_CHECK_VERSION(0, 3, 49)
100 /* After 0.3.49 deinit needs to be called as many times as init was called,
101 * but on previous versions it can be called only once.
102 */
103 inline static int init_count = 0;
104 std::mutex mut;
105#endif
106};
107
121
123 std::string name; //Internal name
124 std::string nick; //Display name
125 std::string description; //Longer display name
126 std::string serial;
127
128 std::string media_class; //"Audio/Sink", "Audio/Source"...
129};
130
131bool initialize_pw_common(pipewire_state_common& s, int fd = -1);
132
133std::vector<Pipewire_device> get_pw_device_list();
134void print_devices(std::string_view media_class);
135
136inline spa_audio_format get_pw_format_from_bps(unsigned bps){
137 spa_audio_format format_map[] = {
138 SPA_AUDIO_FORMAT_UNKNOWN,
139 SPA_AUDIO_FORMAT_S8,
140 SPA_AUDIO_FORMAT_S16,
141 SPA_AUDIO_FORMAT_S24,
142 SPA_AUDIO_FORMAT_S32,
143 };
144
145 return bps < std::size(format_map) ? format_map[bps] : SPA_AUDIO_FORMAT_UNKNOWN;
146}
147
148inline unsigned get_bps_from_pw_format(spa_audio_format fmt){
149 switch(fmt){
150 case SPA_AUDIO_FORMAT_S8: return 1;
151 case SPA_AUDIO_FORMAT_S16: return 2;
152 case SPA_AUDIO_FORMAT_S24: return 3;
153 case SPA_AUDIO_FORMAT_S32: return 4;
154 default: return 0;
155 }
156}
157
158inline codec_t uv_codec_from_pw_fmt(spa_video_format fmt){
159 switch(fmt){
160 case SPA_VIDEO_FORMAT_RGBA:
161 case SPA_VIDEO_FORMAT_RGBx:
162 case SPA_VIDEO_FORMAT_BGRA:
163 case SPA_VIDEO_FORMAT_BGRx:
164 return RGBA;
165 case SPA_VIDEO_FORMAT_RGB:
166 return RGB;
167 case SPA_VIDEO_FORMAT_BGR:
168 return BGR;
169 case SPA_VIDEO_FORMAT_UYVY:
170 return UYVY;
171 case SPA_VIDEO_FORMAT_YUY2:
172 return YUYV;
173 default:
174 return VIDEO_CODEC_NONE;
175 }
176}
177
178inline spa_video_format pw_fmt_from_uv_codec(codec_t codec){
179 switch(codec){
180 case RGBA:
181 return SPA_VIDEO_FORMAT_RGBA;
182 case RGB:
183 return SPA_VIDEO_FORMAT_RGB;
184 case BGR:
185 return SPA_VIDEO_FORMAT_BGR;
186 case UYVY:
187 return SPA_VIDEO_FORMAT_UYVY;
188 default:
189 return SPA_VIDEO_FORMAT_UNKNOWN;
190 }
191}
192
193#if !PW_CHECK_VERSION(0, 3, 64)
194# define STREAM_TARGET_PROPERTY_KEY PW_KEY_NODE_TARGET
195#else
196# define STREAM_TARGET_PROPERTY_KEY PW_KEY_TARGET_OBJECT
197#endif
198
199#ifndef PW_KEY_NODE_RATE
200# define PW_KEY_NODE_RATE "node.rate"
201#endif
202
203#endif
const char * fmt
pipewire_init_guard & operator=(pipewire_init_guard &)=delete
pipewire_init_guard(pipewire_init_guard &)=delete
pipewire_init_guard & operator=(pipewire_init_guard &&o)
pipewire_init_guard(pipewire_init_guard &&o)
pipewire_init_guard()=default
pipewire_thread_loop_lock_guard(pipewire_thread_loop_lock_guard &)=delete
pipewire_thread_loop_lock_guard & operator=(pipewire_thread_loop_lock_guard &)=delete
pipewire_thread_loop_lock_guard(pw_thread_loop *loop)
void print_devices(std::string_view media_class)
codec_t uv_codec_from_pw_fmt(spa_video_format fmt)
std::unique_ptr< T, raii_deleter_helper< T, delete_func > > raii_uniq_handle
raii_uniq_proxy_handle< pw_registry > pw_registry_uniq
raii_uniq_handle< pw_stream, pw_stream_destroy > pw_stream_uniq
spa_video_format pw_fmt_from_uv_codec(codec_t codec)
raii_uniq_handle< pw_main_loop, pw_main_loop_destroy > pw_main_loop_uniq
spa_audio_format get_pw_format_from_bps(unsigned bps)
std::vector< Pipewire_device > get_pw_device_list()
std::unique_ptr< T, raii_proxy_deleter_helper< T > > raii_uniq_proxy_handle
raii_uniq_handle< pw_context, pw_context_destroy > pw_context_uniq
bool initialize_pw_common(pipewire_state_common &s, int fd=-1)
unsigned get_bps_from_pw_format(spa_audio_format fmt)
raii_uniq_handle< pw_core, pw_core_disconnect > pw_core_uniq
raii_uniq_handle< pw_thread_loop, pw_thread_loop_destroy > pw_thread_loop_uniq
std::string description
std::string media_class
pw_context_uniq pipewire_context
pw_thread_loop_uniq pipewire_loop
pipewire_init_guard init_guard
spa_hook_uniq(spa_hook_uniq &)=delete
spa_hook_uniq & operator=(spa_hook_uniq &)=delete
spa_hook & get()
This file contains some common data types.
codec_t
Definition types.h:65
@ YUYV
YCbCr 422 8-bit - Y0 Cb Y1 Cr.
Definition types.h:72
@ 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
@ VIDEO_CODEC_NONE
dummy color spec
Definition types.h:66
@ BGR
8-bit BGR
Definition types.h:88
@ RGBA
RGBA 8-bit, big-endian.
Definition types.h:70