1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*****************************************************************************
* vlc_pipewire.c: common PipeWire code
*****************************************************************************
* Copyright (C) 2022 Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <locale.h>
#include <pwd.h>
#include <unistd.h>
#include <pipewire/pipewire.h>
#include <vlc_common.h>
#include <vlc_fs.h>
#include "vlc_pipewire.h"
struct vlc_pw_context {
struct pw_thread_loop *loop;
struct pw_context *context;
struct pw_core *core;
struct pw_registry *registry;
vlc_object_t *obj;
const char *type;
};
static
void vlc_pw_vlog(struct vlc_pw_context *ctx, int prio,
const char *file, unsigned int line, const char *func,
const char *fmt, va_list ap)
{
vlc_vaLog(ctx->obj, prio, "pipewire", file, line, func, fmt, ap);
}
void (vlc_pw_log)(struct vlc_pw_context *ctx, int prio,
const char *file, unsigned int line, const char *func,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vlc_pw_vlog(ctx, prio, file, line, func, fmt, ap);
va_end(ap);
}
int (vlc_pw_perror)(struct vlc_pw_context *ctx, const char *file,
unsigned int line, const char *func, const char *desc)
{
int err = errno;
(vlc_pw_log)(ctx, VLC_MSG_ERR, file, line, func,
"PipeWire %s error: %s", desc, vlc_strerror_c(err));
errno = err;
return err;
}
void vlc_pw_lock(struct vlc_pw_context *ctx)
{
pw_thread_loop_lock(ctx->loop);
}
void vlc_pw_unlock(struct vlc_pw_context *ctx)
{
pw_thread_loop_unlock(ctx->loop);
}
void vlc_pw_signal(struct vlc_pw_context *ctx)
{
pw_thread_loop_signal(ctx->loop, false);
}
void vlc_pw_wait(struct vlc_pw_context *ctx)
{
pw_thread_loop_wait(ctx->loop);
}
struct pw_stream *vlc_pw_stream_new(struct vlc_pw_context *ctx,
const char *name,
struct pw_properties *props)
{
return pw_stream_new(ctx->core, name, props);
}
struct vlc_pw_rt {
struct vlc_pw_context *context;
int seq;
bool done;
};
static void roundtrip_done(void *data, uint32_t id, int seq)
{
struct vlc_pw_rt *rt = data;
if (id == PW_ID_CORE && seq == rt->seq) {
rt->done = true;
vlc_pw_signal(rt->context);
}
}
void vlc_pw_roundtrip_unlocked(struct vlc_pw_context *ctx)
{
static const struct pw_core_events events = {
PW_VERSION_CORE_EVENTS,
.done = roundtrip_done,
};
struct spa_hook listener = { };
struct vlc_pw_rt rt;
rt.context = ctx;
rt.done = false;
pw_core_add_listener(ctx->core, &listener, &events, &rt);
rt.seq = pw_core_sync(ctx->core, PW_ID_CORE, 0);
while (!rt.done)
vlc_pw_wait(ctx);
spa_hook_remove(&listener);
}
int vlc_pw_registry_listen(struct vlc_pw_context *ctx, struct spa_hook *hook,
const struct pw_registry_events *evts, void *data)
{
if (ctx->registry == NULL) {
ctx->registry = pw_core_get_registry(ctx->core, PW_VERSION_REGISTRY,
0);
if (unlikely(ctx->registry == NULL))
return -errno;
}
*hook = (struct spa_hook){ };
pw_registry_add_listener(ctx->registry, hook, evts, data);
return 0;
}
void vlc_pw_disconnect(struct vlc_pw_context *ctx)
{
pw_thread_loop_stop(ctx->loop);
if (ctx->registry != NULL)
pw_proxy_destroy((struct pw_proxy *)ctx->registry);
pw_core_disconnect(ctx->core);
pw_context_destroy(ctx->context);
pw_thread_loop_destroy(ctx->loop);
pw_deinit();
free(ctx);
}
static int vlc_pw_properties_set_var(struct pw_properties *props,
const char *name,
vlc_object_t *obj, const char *varname)
{
char *str = var_InheritString(obj, varname);
int ret = -1;
if (str != NULL) {
ret = pw_properties_set(props, name, str);
free(str);
}
return ret;
}
static int vlc_pw_properties_set_env(struct pw_properties *props,
const char *name, const char *varname)
{
const char *str = getenv(varname);
int ret = -1;
if (str != NULL)
ret = pw_properties_set(props, name, str);
return ret;
}
static int getusername(uid_t uid, char *restrict buf, size_t buflen)
{
struct passwd pwbuf, *pw;
if (getpwuid_r(uid, &pwbuf, buf, buflen, &pw))
return -1;
memmove(buf, pw->pw_name, strlen(pw->pw_name) + 1);
return 0;
}
static int getmachineid(char *restrict buf, size_t buflen)
{
if (buflen <= 32) {
errno = ENAMETOOLONG;
return -1;
}
FILE *stream = vlc_fopen("/var/lib/dbus/machine-id", "rt");
if (stream == NULL)
return -1;
int ret;
if (fread(buf, 1, 32, stream) == 32) {
buf[32] = '\0';
ret = 0;
} else {
errno = ENXIO;
ret = -1;
}
fclose(stream);
return ret;
}
struct vlc_pw_context *vlc_pw_connect(vlc_object_t *obj, const char *name)
{
const char *version = pw_get_library_version();
int err;
msg_Dbg(obj, "using PipeWire run-time v%s (built v%s)", version,
pw_get_headers_version());
/* Safe use of pw_deinit() requires 0.3.49 */
if (strverscmp(version, "0.3.49") < 0) {
msg_Err(obj, "PipeWire version %s required, %s detected",
"0.3.49", version);
errno = ENOSYS;
return NULL;
}
struct vlc_pw_context *ctx = malloc(sizeof (*ctx));
if (unlikely(ctx == NULL))
return NULL;
pw_init(NULL, NULL);
ctx->obj = obj;
ctx->type = name;
ctx->loop = pw_thread_loop_new(name, NULL);
ctx->registry = NULL;
if (likely(ctx->loop != NULL)) {
struct spa_dict empty = SPA_DICT_INIT(NULL, 0);
struct pw_properties *props = pw_properties_new_dict(&empty);
if (likely(props != NULL)) {
char buf[256];
vlc_pw_properties_set_var(props, PW_KEY_APP_NAME, obj,
"user-agent");
vlc_pw_properties_set_var(props, PW_KEY_APP_ID, obj, "app-id");
vlc_pw_properties_set_var(props, PW_KEY_APP_VERSION, obj,
"app-version");
vlc_pw_properties_set_var(props, PW_KEY_APP_ICON_NAME, obj,
"app-icon-name");
pw_properties_set(props, PW_KEY_APP_LANGUAGE,
setlocale(LC_MESSAGES, NULL));
pw_properties_setf(props, PW_KEY_APP_PROCESS_ID, "%d",
(int)getpid());
/*PW_KEY_APP_PROCESS_BINARY*/
if (getusername(getuid(), buf, sizeof (buf)) == 0)
pw_properties_set(props, PW_KEY_APP_PROCESS_USER, buf);
if (gethostname(buf, sizeof (buf)) == 0)
pw_properties_set(props, PW_KEY_APP_PROCESS_HOST, buf);
if (getmachineid(buf, sizeof (buf)) == 0)
pw_properties_set(props, PW_KEY_APP_PROCESS_MACHINE_ID, buf);
vlc_pw_properties_set_env(props, PW_KEY_APP_PROCESS_SESSION_ID,
"XDG_SESSION_ID");
vlc_pw_properties_set_env(props, PW_KEY_WINDOW_X11_DISPLAY,
"DISPLAY");
}
ctx->context = pw_context_new(pw_thread_loop_get_loop(ctx->loop),
props, 0);
if (likely(ctx->context != NULL)) {
ctx->core = pw_context_connect(ctx->context, NULL, 0);
if (ctx->core != NULL) {
if (likely(pw_thread_loop_start(ctx->loop) == 0))
return ctx;
err = errno;
pw_core_disconnect(ctx->core);
} else {
err = errno;
vlc_pw_perror(ctx, "context connection");
}
pw_context_destroy(ctx->context);
} else
err = errno;
pw_thread_loop_destroy(ctx->loop);
} else
err = errno;
pw_deinit();
errno = err;
free(ctx);
return NULL;
}
|