Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
pulseaudio_sink.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_sndio/target_pulseaudio/roc_sndio/pulseaudio_sink.h
10//! @brief PulseAudio sink.
11
12#ifndef ROC_SNDIO_PULSEAUDIO_SINK_H_
13#define ROC_SNDIO_PULSEAUDIO_SINK_H_
14
15#include <pulse/pulseaudio.h>
16
19#include "roc_core/stddefs.h"
20#include "roc_packet/units.h"
21#include "roc_sndio/config.h"
22#include "roc_sndio/isink.h"
23
24namespace roc {
25namespace sndio {
26
27//! PulseAudio sink,
28class PulseaudioSink : public ISink, public core::NonCopyable<> {
29public:
30 //! Initialize.
31 PulseaudioSink(const Config& config);
32
34
35 //! Open output device.
36 bool open(const char* device);
37
38 //! Get sample specification of the sink.
40
41 //! Get latency of the sink.
43
44 //! Check if the sink has own clock.
45 virtual bool has_clock() const;
46
47 //! Write audio frame.
48 virtual void write(audio::Frame& frame);
49
50private:
51 static void context_state_cb_(pa_context* context, void* userdata);
52
53 static void
54 sink_info_cb_(pa_context* context, const pa_sink_info* info, int eol, void* userdata);
55
56 static void stream_state_cb_(pa_stream* stream, void* userdata);
57 static void stream_write_cb_(pa_stream* stream, size_t length, void* userdata);
58 static void stream_latency_cb_(pa_stream* stream, void* userdata);
59
60 static void timer_cb_(pa_mainloop_api* mainloop,
61 pa_time_event* timer,
62 const struct timeval* tv,
63 void* userdata);
64
65 bool write_frame_(audio::Frame& frame);
66
67 void ensure_started_() const;
68 void ensure_opened_() const;
69
70 bool start_mainloop_();
71 void stop_mainloop_();
72
73 bool open_();
74 void close_();
75 void set_opened_(bool opened);
76
77 bool open_context_();
78 void close_context_();
79
80 bool start_sink_info_op_();
81 void cancel_sink_info_op_();
82
83 void init_stream_params_(const pa_sink_info& info);
84 bool check_stream_params_() const;
85 bool open_stream_();
86 void close_stream_();
87 ssize_t write_stream_(const audio::sample_t* data, size_t size);
88 ssize_t wait_stream_();
89
90 void start_timer_(core::nanoseconds_t timeout);
91 bool stop_timer_();
92
93 const char* device_;
94
95 Config config_;
96 size_t frame_size_;
97
98 core::nanoseconds_t latency_;
99 core::nanoseconds_t timeout_;
100
101 bool open_done_;
102 bool opened_;
103
104 pa_threaded_mainloop* mainloop_;
105 pa_context* context_;
106 pa_operation* sink_info_op_;
107 pa_stream* stream_;
108 pa_time_event* timer_;
109
110 core::nanoseconds_t timer_deadline_;
111
112 pa_sample_spec sample_spec_;
113 pa_buffer_attr buffer_attrs_;
114
115 core::RateLimiter rate_limiter_;
116};
117
118} // namespace sndio
119} // namespace roc
120
121#endif // ROC_SNDIO_PULSEAUDIO_SINK_H_
Audio frame.
Definition: frame.h:22
Sample stream specification. Defines sample rate and channel layout.
Definition: sample_spec.h:24
Base class for non-copyable objects.
Definition: noncopyable.h:23
Sink interface.
Definition: isink.h:22
virtual core::nanoseconds_t latency() const
Get latency of the sink.
virtual bool has_clock() const
Check if the sink has own clock.
PulseaudioSink(const Config &config)
Initialize.
bool open(const char *device)
Open output device.
virtual void write(audio::Frame &frame)
Write audio frame.
virtual audio::SampleSpec sample_spec() const
Get sample specification of the sink.
Sink interface.
float sample_t
Audio sample.
Definition: sample.h:22
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Rate limiter.
Sink and source config.
Commonly used types and functions.
Sink and source config.
Definition: config.h:23
Various units used in packets.