Music Hub  ..
A session-wide music playback service
pulse_audio_output_observer.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 Canonical Ltd.
3  * Copyright © 2022 UBports Foundation.
4  *
5  * Contact: Alberto Mardegan <mardy@users.sourceforge.net>
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License version 3,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Authored by: Thomas Voß <thomas.voss@canonical.com>
20  * Ricardo Mendoza <ricardo.mendoza@canonical.com>
21  */
22 #ifndef LOMIRI_MEDIAHUBSERVICE_AUDIO_PULSE_AUDIO_OUTPUT_OBSERVER_H
23 #define LOMIRI_MEDIAHUBSERVICE_AUDIO_PULSE_AUDIO_OUTPUT_OBSERVER_H
24 
26 
27 #include <iosfwd>
28 #include <memory>
29 #include <set>
30 #include <string>
31 
32 class QStringList;
33 
34 namespace lomiri
35 {
36 namespace MediaHubService
37 {
38 namespace audio
39 {
40 // Implements the audio::OutputObserver interface
41 // relying on pulse to query the connected ports
42 // of the primary card of the system.
44 {
45 public:
46  // Reporter is responsible for surfacing events from the implementation
47  // that help in resolving/tracking down issues. Default implementation is empty.
48  struct Reporter
49  {
50  // To save us some typing.
51  typedef std::shared_ptr<Reporter> Ptr;
52 
53  // Simple type to help in reporting.
54  struct Port
55  {
56  // Returns true iff the name of both ports are equal.
57  bool operator==(const Port& rhs) const;
58  // Returns true iff the name of the ports differ.
59  bool operator<(const Port& rhs) const;
60 
61  std::string name; // The name of the port.
62  std::string description; // Human-readable description of the port.
63  bool is_available; // True if the port is available.
64  bool is_monitored; // True if the port is monitored by the observer.
65  };
66 
67  virtual ~Reporter();
68  // connected_to_pulse_audio is called when a connection with pulse has been established.
69  virtual void connected_to_pulse_audio();
70  // query_for_default_sink_failed is called when no default sink was returned.
71  virtual void query_for_default_sink_failed();
72  // query_for_default_sink_finished is called when the default sink query against pulse
73  // has finished, reporting the name of the sink to observers.
74  virtual void query_for_default_sink_finished(const std::string& sink_name);
75  // query_for_sink_info_finished is called when a query for information about a specific sink
76  // has finished, reporting the name, index of the sink as well as the set of ports known to the sink.
77  virtual void query_for_sink_info_finished(const std::string& name, std::uint32_t index, const std::set<Port>& known_ports);
78  // sink_event_with_index is called when something happened on a sink, reporing the index of the
79  // sink.
80  virtual void sink_event_with_index(std::uint32_t index);
81  };
82 
83  // Constructs a new instance, throws:
84  // * std::runtime_error if connection to pulseaudio fails.
85  // * std::runtime_error if reporter instance is null.
86  PulseAudioOutputObserver(const QString &sink,
87  const QStringList &outputPortPatterns,
88  Reporter::Ptr reporter,
89  OutputObserver *q);
90 
91  // We provide the name of the sink we are connecting to as a
92  // getable property. This is specifically meant for
93  // consumption by test code.
94  QString sink() const;
95  // The set of ports that have been identified on the configured sink.
96  // Specifically meant for consumption by test code.
97  std::set<Reporter::Port>& knownPorts() const;
98 
99 private:
100  struct Private;
101  std::shared_ptr<Private> d;
102 };
103 }
104 }
105 }
106 
107 #endif // LOMIRI_MEDIAHUBSERVICE_AUDIO_PULSE_AUDIO_OUTPUT_OBSERVER_H
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::connected_to_pulse_audio
virtual void connected_to_pulse_audio()
Definition: pulse_audio_output_observer.cpp:416
lomiri::MediaHubService::audio::OutputObserverPrivate
Definition: output_observer_p.h:28
lomiri::MediaHubService::audio::PulseAudioOutputObserver
Definition: pulse_audio_output_observer.h:43
lomiri::MediaHubService::audio::PulseAudioOutputObserver::PulseAudioOutputObserver
PulseAudioOutputObserver(const QString &sink, const QStringList &outputPortPatterns, Reporter::Ptr reporter, OutputObserver *q)
lomiri::MediaHubService::audio
Definition: ostream_reporter.h:33
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::query_for_sink_info_finished
virtual void query_for_sink_info_finished(const std::string &name, std::uint32_t index, const std::set< Port > &known_ports)
Definition: pulse_audio_output_observer.cpp:428
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Port::name
std::string name
Definition: pulse_audio_output_observer.h:61
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Port::operator==
bool operator==(const Port &rhs) const
Definition: pulse_audio_output_observer.cpp:402
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::query_for_default_sink_finished
virtual void query_for_default_sink_finished(const std::string &sink_name)
Definition: pulse_audio_output_observer.cpp:424
lomiri::MediaHubService::audio::OutputObserver
Definition: output_observer.h:50
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter
Definition: pulse_audio_output_observer.h:48
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::~Reporter
virtual ~Reporter()
Definition: pulse_audio_output_observer.cpp:412
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::sink_event_with_index
virtual void sink_event_with_index(std::uint32_t index)
Definition: pulse_audio_output_observer.cpp:432
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Port
Definition: pulse_audio_output_observer.h:54
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::query_for_default_sink_failed
virtual void query_for_default_sink_failed()
Definition: pulse_audio_output_observer.cpp:420
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Port::is_monitored
bool is_monitored
Definition: pulse_audio_output_observer.h:64
lomiri::MediaHubService::audio::PulseAudioOutputObserver::sink
QString sink() const
output_observer_p.h
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Port::is_available
bool is_available
Definition: pulse_audio_output_observer.h:63
lomiri::MediaHubService::audio::PulseAudioOutputObserver::knownPorts
std::set< Reporter::Port > & knownPorts() const
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Port::description
std::string description
Definition: pulse_audio_output_observer.h:62
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Ptr
std::shared_ptr< Reporter > Ptr
Definition: pulse_audio_output_observer.h:51
lomiri
Definition: dbus_utils.h:24
lomiri::MediaHubService::audio::PulseAudioOutputObserver::Reporter::Port::operator<
bool operator<(const Port &rhs) const
Definition: pulse_audio_output_observer.cpp:407