Debian Codegraph @

xbmc/cores/AudioEngine/Sinks/pipewire/PipewireRegistry.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2010-2021 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "PipewireRegistry.h"
10
11 #include "PipewireCore.h"
12 #include "PipewireGlobal.h"
13 #include "PipewireNode.h"
14 #include "utils/log.h"
15
16 #include <stdexcept>
17
18 #include <pipewire/keys.h>
19 #include <pipewire/node.h>
20 #include <pipewire/type.h>
21
22 using namespace KODI;
23 using namespace PIPEWIRE;
24
25 CPipewireRegistry::CPipewireRegistry(CPipewireCore& core)
26 : m_core(core), m_registryEvents(CreateRegistryEvents())
27 {
28 m_registry.reset(pw_core_get_registry(core.Get(), PW_VERSION_REGISTRY, 0));
29 if (!m_registry)
30 {
31 CLog::Log(LOGERROR, "CPipewireRegistry: failed to create registry: {}", strerror(errno));
32 throw std::runtime_error("CPipewireRegistry: failed to create registry");
33 }
34
35 pw_registry_add_listener(m_registry.get(), &m_registryListener, &m_registryEvents, this);
36 }
37
38 CPipewireRegistry::~CPipewireRegistry() = default;
39
40 void CPipewireRegistry::OnGlobalAdded(void* userdata,
41 uint32_t id,
42 uint32_t permissions,
43 const char* type,
44 uint32_t version,
45 const struct spa_dict* props)
46 {
47 if (strcmp(type, PW_TYPE_INTERFACE_Node) != 0)
48 return;
49
50 const char* mediaClass = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS);
51 if (!mediaClass)
52 return;
53
54 if (strcmp(mediaClass, "Audio/Sink") != 0)
55 return;
56
57 const char* name = spa_dict_lookup(props, PW_KEY_NODE_NAME);
58 if (!name)
59 return;
60
61 const char* desc = spa_dict_lookup(props, PW_KEY_NODE_DESCRIPTION);
62 if (!desc)
63 return;
64
65 auto properties =
66 std::unique_ptr<pw_properties, PipewirePropertiesDeleter>(pw_properties_new_dict(props));
67
68 auto& registry = *reinterpret_cast<CPipewireRegistry*>(userdata);
69 std::lock_guard lg(registry);
70 auto node = std::make_unique<CPipewireNode>(registry, id, type);
71
72 auto& globals = registry.GetGlobals();
73
74 globals[id] = std::make_unique<CPipewireGlobal>();
75 globals[id]
76 ->SetName(name)
77 .SetDescription(desc)
78 .SetID(id)
79 .SetPermissions(permissions)
80 .SetType(type)
81 .SetVersion(version)
82 .SetProperties(std::move(properties))
83 .SetNode(std::move(node));
84 }
85
86 void CPipewireRegistry::OnGlobalRemoved(void* userdata, uint32_t id)
87 {
88 auto& registry = *reinterpret_cast<CPipewireRegistry*>(userdata);
89 std::lock_guard lg(registry);
90 auto& globals = registry.GetGlobals();
91
92 auto it = globals.find(id);
93 if (it != globals.end())
94 {
95 const auto& [globalId, global] = *it;
96 CLog::Log(LOGDEBUG, "CPipewireRegistry::{} - id={} type={}", __FUNCTION__, id,
97 global->GetType());
98
99 globals.erase(it);
100 }
101 }
102
103 pw_registry_events CPipewireRegistry::CreateRegistryEvents()
104 {
105 pw_registry_events registryEvents = {};
106 registryEvents.version = PW_VERSION_REGISTRY_EVENTS;
107 registryEvents.global = OnGlobalAdded;
108 registryEvents.global_remove = OnGlobalRemoved;
109
110 return registryEvents;
111 }