File: pw_metadata_manager.cpp

package info (click to toggle)
easyeffects 8.0.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,752 kB
  • sloc: cpp: 22,118; sh: 691; python: 448; javascript: 64; makefile: 8
file content (145 lines) | stat: -rw-r--r-- 4,281 bytes parent folder | download
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
/**
 * Copyright © 2017-2025 Wellington Wallace
 *
 * This file is part of Easy Effects.
 *
 * Easy Effects 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.
 *
 * Easy Effects 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Easy Effects. If not, see <https://www.gnu.org/licenses/>.
 */

#include "pw_metadata_manager.hpp"

#include <pipewire/context.h>
#include <pipewire/core.h>
#include <pipewire/extensions/metadata.h>
#include <pipewire/keys.h>
#include <pipewire/properties.h>
#include <pipewire/proxy.h>
#include <pipewire/thread-loop.h>
#include <qobject.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include <spa/utils/dict.h>
#include <spa/utils/hook.h>
#include <cstdint>
#include <cstring>
#include <format>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <string>
#include "tags_pipewire.hpp"
#include "util.hpp"

namespace pw {

auto MetadataManager::register_metadata(pw_registry* registry, uint32_t id, const char* type, const spa_dict* props)
    -> bool {
  if (const auto* name = spa_dict_lookup(props, PW_KEY_METADATA_NAME)) {
    util::debug(std::format("Found metadata: {}", name));

    if (std::strcmp(name, "default") == 0) {
      if (metadata != nullptr) {
        util::debug("A new default metadata is available. We will use it");

        spa_hook_remove(&metadata_listener);
      }

      metadata = static_cast<pw_metadata*>(pw_registry_bind(registry, id, type, PW_VERSION_METADATA, 0));

      if (metadata != nullptr) {
        pw_metadata_add_listener(metadata, &metadata_listener, &metadata_events, this);  // NOLINT

        util::debug("Successfully registered default metadata");

        return true;
      } else {
        util::warning("pw_registry_bind returned a null metadata object");

        return false;
      }
    }
  }

  return false;
}

auto MetadataManager::on_metadata_property(void* data,
                                           uint32_t id,
                                           const char* key,
                                           const char* type,
                                           const char* value) -> int {
  auto* const mm = static_cast<MetadataManager*>(data);

  const std::string str_key = (key != nullptr) ? key : "";
  const std::string str_value = (value != nullptr) ? value : "";
  const std::string str_type = (type != nullptr) ? type : "";

  util::debug(std::format("New metadata property: {}, {}, {}, {}", id, str_key, str_type, str_value));

  if (str_value.empty()) {
    return 0;
  }

  // Handle specific metadata properties
  if (str_key == "default.audio.sink") {
    auto v = nlohmann::json::parse(str_value).value("name", "");

    if (v == tags::pipewire::ee_sink_name) {
      return 0;
    }

    util::debug(std::format("New default output device: {}", v));

    Q_EMIT mm->defaultSinkChanged(QString::fromStdString(v));
  }

  if (str_key == "default.audio.source") {
    auto v = nlohmann::json::parse(str_value).value("name", "");

    if (v == tags::pipewire::ee_source_name) {
      return 0;
    }

    util::debug(std::format("New default input device: {}", v));

    Q_EMIT mm->defaultSourceChanged(QString::fromStdString(v));
  }

  return 0;
}

void MetadataManager::set_property(uint32_t id, const char* key, const char* type, const char* value) const {
  if (metadata == nullptr) {
    return;
  }

  pw_metadata_set_property(metadata, id, key, type, value);  // NOLINT
}

void MetadataManager::clear_property(uint32_t id, const char* key) const {
  if (metadata == nullptr) {
    return;
  }

  pw_metadata_set_property(metadata, id, key, nullptr, nullptr);  // NOLINT
}

void MetadataManager::destroy_metadata() {
  if (metadata != nullptr) {
    util::debug("Destroying PipeWire metadata...");

    pw_proxy_destroy((struct pw_proxy*)metadata);
  }
}

}  // namespace pw