File: pulse_manager.hpp

package info (click to toggle)
pulseeffects 4.8.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,952 kB
  • sloc: cpp: 18,501; xml: 3,821; sh: 228; python: 56; makefile: 4
file content (337 lines) | stat: -rw-r--r-- 10,215 bytes parent folder | download | duplicates (4)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 *  Copyright © 2017-2020 Wellington Wallace
 *
 *  This file is part of PulseEffects.
 *
 *  PulseEffects 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.
 *
 *  PulseEffects 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 PulseEffects.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef PULSE_MANAGER_HPP
#define PULSE_MANAGER_HPP

#include <glibmm.h>
#include <pulse/pulseaudio.h>
#include <pulse/thread-mainloop.h>
#include <sigc++/sigc++.h>
#include <algorithm>
#include <array>
#include <cstring>
#include <iostream>
#include <memory>

struct myServerInfo {
  std::string server_name;
  std::string server_version;
  std::string default_sink_name;
  std::string default_source_name;
  std::string protocol;
  std::string format;
  std::string channel_map;
  uint rate;
  uint8_t channels;
};

struct mySinkInfo {
  std::string name;
  uint index;
  std::string description;
  uint owner_module;
  uint monitor_source;
  std::string monitor_source_name;
  uint rate;
  std::string format;
  std::string active_port;
};

struct mySourceInfo {
  std::string name;
  uint index;
  std::string description;
  uint rate;
  std::string format;
  std::string active_port;
};

struct myModuleInfo {
  std::string name;
  uint index;
  std::string argument;
};

struct myClientInfo {
  std::string name;
  uint index;
  std::string binary;
};

struct AppInfo {
  std::string app_type;
  uint index;
  std::string name;
  std::string icon_name;
  std::string media_name;
  uint8_t channels;
  double volume;
  uint rate;
  std::string resampler;
  std::string format;
  int mute;
  bool connected;
  bool visible;
  uint buffer;
  uint latency;
  int corked;
  bool wants_to_play;
};

class ParseAppInfo;

class PulseManager {
 public:
  PulseManager();
  PulseManager(const PulseManager&) = delete;
  auto operator=(const PulseManager&) -> PulseManager& = delete;
  PulseManager(const PulseManager&&) = delete;
  auto operator=(const PulseManager &&) -> PulseManager& = delete;
  ~PulseManager();

  pa_threaded_mainloop* main_loop = nullptr;

  myServerInfo server_info;
  std::shared_ptr<mySinkInfo> apps_sink_info;
  std::shared_ptr<mySinkInfo> mic_sink_info;

  auto get_sink_info(const std::string& name) -> std::shared_ptr<mySinkInfo>;
  auto get_source_info(const std::string& name) -> std::shared_ptr<mySourceInfo>;

  std::vector<std::string> blocklist_in;   // for input effects
  std::vector<std::string> blocklist_out;  // for output effects

  void find_sink_inputs();
  void find_source_outputs();
  void find_sinks();
  void find_sources();
  auto move_sink_input_to_pulseeffects(const std::string& name, uint idx) -> bool;
  auto remove_sink_input_from_pulseeffects(const std::string& name, uint idx) -> bool;
  auto move_source_output_to_pulseeffects(const std::string& name, uint idx) -> bool;
  auto remove_source_output_from_pulseeffects(const std::string& name, uint idx) -> bool;
  void set_sink_input_volume(const std::string& name, uint idx, uint8_t channels, uint value);
  void set_sink_input_mute(const std::string& name, uint idx, bool state);
  void set_source_output_volume(const std::string& name, uint idx, uint8_t channels, uint value);
  void set_source_output_mute(const std::string& name, uint idx, bool state);
  void get_sink_input_info(uint idx);
  void update_server_info(const pa_server_info* info);
  void get_modules_info();
  void get_clients_info();
  void set_sink_volume_by_name(const std::string& name, uint8_t channels, uint value);

  sigc::signal<void, std::shared_ptr<mySourceInfo>> source_added;
  sigc::signal<void, std::shared_ptr<mySourceInfo>> source_changed;
  sigc::signal<void, uint> source_removed;
  sigc::signal<void, std::shared_ptr<mySinkInfo>> sink_added;
  sigc::signal<void, std::shared_ptr<mySinkInfo>> sink_changed;
  sigc::signal<void, uint> sink_removed;
  sigc::signal<void, std::string> new_default_sink;
  sigc::signal<void, std::string> new_default_source;
  sigc::signal<void, std::shared_ptr<AppInfo>> sink_input_added;
  sigc::signal<void, std::shared_ptr<AppInfo>> sink_input_changed;
  sigc::signal<void, uint> sink_input_removed;
  sigc::signal<void, std::shared_ptr<AppInfo>> source_output_added;
  sigc::signal<void, std::shared_ptr<AppInfo>> source_output_changed;
  sigc::signal<void, uint> source_output_removed;
  sigc::signal<void> server_changed;
  sigc::signal<void, std::shared_ptr<myModuleInfo>> module_info;
  sigc::signal<void, std::shared_ptr<myClientInfo>> client_info;

 private:
  std::string log_tag = "pulse_manager: ";

  bool context_ready = false;

  pa_mainloop_api* main_loop_api = nullptr;
  pa_context* context = nullptr;

  std::array<std::string, 7> blocklist_apps = {
      "PulseEffectsWebrtcProbe", "gsd-media-keys", "GNOME Shell", "libcanberra", "Screenshot", "speech-dispatcher"};

  std::array<std::string, 4> blocklist_media_name = {"pulsesink probe", "bell-window-system", "audio-volume-change",
                                                     "screen-capture"};

  std::array<std::string, 1> blocklist_media_role = {"event"};

  std::array<std::string, 4> blocklist_app_id = {"com.github.wwmm.pulseeffects.sinkinputs",
                                                 "com.github.wwmm.pulseeffects.sourceoutputs",
                                                 "org.PulseAudio.pavucontrol", "org.gnome.VolumeControl"};

  static void context_state_cb(pa_context* ctx, void* data);

  void subscribe_to_events();

  void get_server_info();

  auto get_default_sink_info() -> std::shared_ptr<mySinkInfo>;

  auto get_default_source_info() -> std::shared_ptr<mySourceInfo>;

  auto load_sink(const std::string& name, const std::string& description, uint rate) -> std::shared_ptr<mySinkInfo>;

  void load_apps_sink();

  void load_mic_sink();

  auto load_module(const std::string& name, const std::string& argument) -> bool;

  void unload_module(uint idx);

  void unload_sinks();

  void drain_context();

  void new_app(const pa_sink_input_info* info);

  void new_app(const pa_source_output_info* info);

  void changed_app(const pa_sink_input_info* info);

  void changed_app(const pa_source_output_info* info);

  static void print_app_info(const std::shared_ptr<AppInfo>& info);

  auto app_is_connected(const pa_sink_input_info* info) -> bool;

  auto app_is_connected(const pa_source_output_info* info) -> bool;

  static auto get_latency(const pa_sink_input_info* info) -> uint { return info->sink_usec; }

  static auto get_latency(const pa_source_output_info* info) -> uint { return info->source_usec; }

  template <typename T>
  auto parse_app_info(const T& info) -> std::shared_ptr<AppInfo> {
    std::string app_name;
    std::string media_name;
    std::string media_role;
    std::string app_id;
    auto ai = std::make_shared<AppInfo>();
    bool forbidden_app = false;

    auto prop = pa_proplist_gets(info->proplist, "application.name");

    if (prop != nullptr) {
      app_name = prop;

      forbidden_app =
          std::find(std::begin(blocklist_apps), std::end(blocklist_apps), app_name) != std::end(blocklist_apps);

      if (forbidden_app) {
        return nullptr;
      }
    }

    prop = pa_proplist_gets(info->proplist, "media.name");

    if (prop != nullptr) {
      media_name = prop;

      if (app_name.empty()) {
        app_name = media_name;
      }

      forbidden_app = std::find(std::begin(blocklist_media_name), std::end(blocklist_media_name), media_name) !=
                      std::end(blocklist_media_name);

      if (forbidden_app) {
        return nullptr;
      }
    }

    prop = pa_proplist_gets(info->proplist, "media.role");

    if (prop != nullptr) {
      media_role = prop;

      forbidden_app = std::find(std::begin(blocklist_media_role), std::end(blocklist_media_role), media_role) !=
                      std::end(blocklist_media_role);

      if (forbidden_app) {
        return nullptr;
      }
    }

    prop = pa_proplist_gets(info->proplist, "application.id");

    if (prop != nullptr) {
      app_id = prop;

      forbidden_app =
          std::find(std::begin(blocklist_app_id), std::end(blocklist_app_id), app_id) != std::end(blocklist_app_id);

      if (forbidden_app) {
        return nullptr;
      }
    }

    prop = pa_proplist_gets(info->proplist, "application.icon_name");

    std::string icon_name;

    if (prop != nullptr) {
      icon_name = prop;
    } else {
      prop = pa_proplist_gets(info->proplist, "media.icon_name");

      if (prop != nullptr) {
        if (std::strcmp(prop, "audio-card-bluetooth") ==
            0) {  // there is no GTK icon with this name given by Pulseaudio =/
        } else {
          icon_name = "bluetooth-symbolic";
        }
      } else {
        icon_name = "audio-x-generic-symbolic";
      }
    }

    // Connection flag: it specifies only the primary state that can be enabled/disabled by the user
    ai->connected = app_is_connected(info);

    // Initialize visibility to true, it will be properly updated forward
    ai->visible = true;

    // linear volume
    ai->volume = 100.0 * (static_cast<double>(pa_cvolume_max(&info->volume)) / PA_VOLUME_NORM);

    if (info->resample_method) {
      ai->resampler = info->resample_method;
    } else {
      ai->resampler = "none";
    }

    ai->format = pa_sample_format_to_string(info->sample_spec.format);

    ai->index = info->index;
    ai->name = app_name;
    ai->media_name = media_name;
    ai->icon_name = icon_name;
    ai->channels = info->volume.channels;
    ai->rate = info->sample_spec.rate;
    ai->mute = info->mute;
    ai->buffer = info->buffer_usec;
    ai->latency = get_latency(info);
    ai->corked = info->corked;
    ai->wants_to_play = ai->connected && !ai->corked;

    return ai;
  }
};

#endif