| 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 "PipewireProxy.h" |
| 10 | |
| 11 | #include "PipewireRegistry.h" |
| 12 | #include "ServiceBroker.h" |
| 13 | #include "cores/AudioEngine/Interfaces/AE.h" |
| 14 | #include "utils/log.h" |
| 15 | |
| 16 | #include <stdexcept> |
| 17 | |
| 18 | using namespace KODI; |
| 19 | using namespace PIPEWIRE; |
| 20 | |
| 21 | CPipewireProxy::CPipewireProxy(CPipewireRegistry& registry, |
| 22 | uint32_t id, |
| 23 | const char* type, |
| 24 | uint32_t version) |
| 25 | : m_registry(registry), m_proxyEvents(CreateProxyEvents()) |
| 26 | { |
| 27 | m_proxy.reset( |
| 28 | reinterpret_cast<pw_proxy*>(pw_registry_bind(registry.Get(), id, type, version, 0))); |
| 29 | if (!m_proxy) |
| 30 | { |
| 31 | CLog::Log(LOGERROR, "CPipewireProxy: failed to create proxy: {}", strerror(errno)); |
| 32 | throw std::runtime_error("CPipewireProxy: failed to create proxy"); |
| 33 | } |
| 34 | |
| 35 | pw_proxy_add_listener(m_proxy.get(), &m_proxyListener, &m_proxyEvents, this); |
| 36 | } |
| 37 | |
| 38 | CPipewireProxy::~CPipewireProxy() |
| 39 | { |
| 40 | spa_hook_remove(&m_proxyListener); |
| 41 | } |
| 42 | |
| 43 | void CPipewireProxy::Bound(void* userdata, uint32_t id) |
| 44 | { |
| 45 | CLog::Log(LOGDEBUG, "CPipewireProxy::{} - id={}", __FUNCTION__, id); |
| 46 | |
| 47 | auto AE = CServiceBroker::GetActiveAE(); |
| 48 | if (AE) |
| 49 | AE->DeviceCountChange("PIPEWIRE"); |
| 50 | } |
| 51 | |
| 52 | void CPipewireProxy::Removed(void* userdata) |
| 53 | { |
| 54 | CLog::Log(LOGDEBUG, "CPipewireProxy::{}", __FUNCTION__); |
| 55 | |
| 56 | auto AE = CServiceBroker::GetActiveAE(); |
| 57 | if (AE) |
| 58 | AE->DeviceCountChange("PIPEWIRE"); |
| 59 | } |
| 60 | |
| 61 | pw_proxy_events CPipewireProxy::CreateProxyEvents() |
| 62 | { |
| 63 | pw_proxy_events proxyEvents = {}; |
| 64 | proxyEvents.version = PW_VERSION_PROXY_EVENTS; |
| 65 | proxyEvents.bound = Bound; |
| 66 | proxyEvents.removed = Removed; |
| 67 | |
| 68 | return proxyEvents; |
| 69 | } |