| 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 "PipewireNode.h" |
| 10 | |
| 11 | #include "PipewireContext.h" |
| 12 | #include "PipewireCore.h" |
| 13 | #include "PipewireRegistry.h" |
| 14 | #include "PipewireThreadLoop.h" |
| 15 | #include "utils/StringUtils.h" |
| 16 | #include "utils/log.h" |
| 17 | |
| 18 | #include <spa/param/format.h> |
| 19 | #include <spa/pod/iter.h> |
| 20 | |
| 21 | using namespace KODI; |
| 22 | using namespace PIPEWIRE; |
| 23 | |
| 24 | CPipewireNode::CPipewireNode(CPipewireRegistry& registry, uint32_t id, const char* type) |
| 25 | : CPipewireProxy(registry, id, type, PW_VERSION_NODE), m_nodeEvents(CreateNodeEvents()) |
| 26 | { |
| 27 | pw_proxy_add_object_listener(m_proxy.get(), &m_objectListener, &m_nodeEvents, this); |
| 28 | } |
| 29 | |
| 30 | CPipewireNode::~CPipewireNode() |
| 31 | { |
| 32 | spa_hook_remove(&m_objectListener); |
| 33 | } |
| 34 | |
| 35 | void CPipewireNode::EnumerateFormats() |
| 36 | { |
| 37 | if (!m_info) |
| 38 | return; |
| 39 | |
| 40 | for (uint32_t param = 0; param < m_info->n_params; param++) |
| 41 | { |
| 42 | if (m_info->params[param].id == SPA_PARAM_EnumFormat) |
| 43 | pw_node_enum_params(reinterpret_cast<struct pw_node*>(m_proxy.get()), 0, |
| 44 | m_info->params[param].id, 0, 0, NULL); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void CPipewireNode::Info(void* userdata, const struct pw_node_info* info) |
| 49 | { |
| 50 | auto& node = *reinterpret_cast<CPipewireNode*>(userdata); |
| 51 | |
| 52 | if (node.m_info) |
| 53 | { |
| 54 | CLog::Log(LOGDEBUG, "CPipewireNode::{} - node {} changed", __FUNCTION__, info->id); |
| 55 | pw_node_info* m_info = node.m_info.get(); |
| 56 | m_info = pw_node_info_update(m_info, info); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | node.m_info.reset(pw_node_info_update(node.m_info.get(), info)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | template<typename T> |
| 65 | static T Parse(uint32_t type, void* body, uint32_t size) |
| 66 | { |
| 67 | switch (type) |
| 68 | { |
| 69 | case SPA_TYPE_Id: |
| 70 | case SPA_TYPE_Int: |
| 71 | return *reinterpret_cast<T*>(body); |
| 72 | |
| 73 | default: |
| 74 | throw std::runtime_error(StringUtils::Format("unhandled type: {}", type)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | template<typename T> |
| 79 | static std::set<T> ParseArray(uint32_t type, void* body, uint32_t size) |
| 80 | { |
| 81 | switch (type) |
| 82 | { |
| 83 | case SPA_TYPE_Id: |
| 84 | case SPA_TYPE_Int: |
| 85 | { |
| 86 | std::set<T> values; |
| 87 | values.emplace(Parse<T>(type, body, size)); |
| 88 | |
| 89 | return values; |
| 90 | } |
| 91 | case SPA_TYPE_Array: |
| 92 | { |
| 93 | auto array = reinterpret_cast<spa_pod_array_body*>(body); |
| 94 | void* p; |
| 95 | std::set<T> values; |
| 96 | SPA_POD_ARRAY_BODY_FOREACH(array, size, p) |
| 97 | values.emplace(Parse<T>(array->child.type, p, array->child.size)); |
| 98 | |
| 99 | return values; |
| 100 | } |
| 101 | case SPA_TYPE_Choice: |
| 102 | { |
| 103 | auto choice = reinterpret_cast<spa_pod_choice_body*>(body); |
| 104 | void* p; |
| 105 | std::set<T> values; |
| 106 | SPA_POD_CHOICE_BODY_FOREACH(choice, size, p) |
| 107 | values.emplace(Parse<T>(choice->child.type, p, choice->child.size)); |
| 108 | |
| 109 | return values; |
| 110 | } |
| 111 | default: |
| 112 | throw std::runtime_error(StringUtils::Format("unhandled array: {}", type)); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void CPipewireNode::Parse(uint32_t type, void* body, uint32_t size) |
| 117 | { |
| 118 | switch (type) |
| 119 | { |
| 120 | case SPA_TYPE_Object: |
| 121 | { |
| 122 | auto object = reinterpret_cast<spa_pod_object_body*>(body); |
| 123 | |
| 124 | switch (object->type) |
| 125 | { |
| 126 | case SPA_TYPE_OBJECT_Format: |
| 127 | { |
| 128 | spa_pod_prop* prop; |
| 129 | SPA_POD_OBJECT_BODY_FOREACH(object, size, prop) |
| 130 | { |
| 131 | spa_format format = static_cast<spa_format>(prop->key); |
| 132 | |
| 133 | switch (format) |
| 134 | { |
| 135 | case SPA_FORMAT_AUDIO_format: |
| 136 | { |
| 137 | m_formats = ParseArray<spa_audio_format>( |
| 138 | prop->value.type, SPA_POD_CONTENTS(spa_pod_prop, prop), prop->value.size); |
| 139 | break; |
| 140 | } |
| 141 | case SPA_FORMAT_AUDIO_rate: |
| 142 | { |
| 143 | m_rates = ParseArray<uint32_t>( |
| 144 | prop->value.type, SPA_POD_CONTENTS(spa_pod_prop, prop), prop->value.size); |
| 145 | break; |
| 146 | } |
| 147 | case SPA_FORMAT_AUDIO_position: |
| 148 | { |
| 149 | m_channels = ParseArray<spa_audio_channel>( |
| 150 | prop->value.type, SPA_POD_CONTENTS(spa_pod_prop, prop), prop->value.size); |
| 151 | break; |
| 152 | } |
| 153 | case SPA_FORMAT_AUDIO_iec958Codec: |
| 154 | { |
| 155 | m_iec958Codecs = ParseArray<spa_audio_iec958_codec>( |
| 156 | prop->value.type, SPA_POD_CONTENTS(spa_pod_prop, prop), prop->value.size); |
| 157 | break; |
| 158 | } |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | break; |
| 165 | } |
| 166 | default: |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | break; |
| 171 | } |
| 172 | default: |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void CPipewireNode::Param(void* userdata, |
| 178 | int seq, |
| 179 | uint32_t id, |
| 180 | uint32_t index, |
| 181 | uint32_t next, |
| 182 | const struct spa_pod* param) |
| 183 | { |
| 184 | auto& node = *reinterpret_cast<CPipewireNode*>(userdata); |
| 185 | auto& loop = node.GetRegistry().GetCore().GetContext().GetThreadLoop(); |
| 186 | |
| 187 | node.Parse(SPA_POD_TYPE(param), SPA_POD_BODY(param), SPA_POD_BODY_SIZE(param)); |
| 188 | |
| 189 | loop.Signal(false); |
| 190 | } |
| 191 | |
| 192 | pw_node_events CPipewireNode::CreateNodeEvents() |
| 193 | { |
| 194 | pw_node_events nodeEvents = {}; |
| 195 | nodeEvents.version = PW_VERSION_NODE_EVENTS; |
| 196 | nodeEvents.info = Info; |
| 197 | nodeEvents.param = Param; |
| 198 | |
| 199 | return nodeEvents; |
| 200 | } |