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
media / audio / pulse / pulse_loopback.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pulse_loopback.h"
#include "audio_manager_pulse.h"
#include "pulse_input.h"
#include "pulse_util.h"
namespace media {
PulseLoopbackAudioStream::PulseLoopbackAudioStream(
ReleaseStreamCallback release_stream_callback,
const std::string& source_name,
const AudioParameters& params,
pa_threaded_mainloop* mainloop,
pa_context* context,
AudioManager::LogCallback log_callback,
bool mute_system_audio)
: release_stream_callback_(std::move(release_stream_callback)),
params_(params),
mainloop_(mainloop),
context_(context),
log_callback_(std::move(log_callback)),
mute_system_audio_(mute_system_audio),
sink_(nullptr),
stream_(new PulseAudioInputStream(nullptr,
source_name,
params,
mainloop,
context,
log_callback_)) {
CHECK(stream_);
}
PulseLoopbackAudioStream::~PulseLoopbackAudioStream() {
CHECK(!stream_);
}
AudioInputStream::OpenOutcome PulseLoopbackAudioStream::Open() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
OpenOutcome open_outcome = stream_->Open();
if (open_outcome == OpenOutcome::kSuccess) {
stream_opened_ = true;
if (mute_system_audio_) {
std::string default_sink_name = media::pulse::GetRealDefaultDeviceId(
mainloop_, context_, pulse::RequestType::OUTPUT);
std::string monitor_source_name =
media::pulse::GetMonitorSourceNameForSink(mainloop_, context_,
default_sink_name);
pulse::MuteAllSinksExcept(mainloop_, context_, monitor_source_name);
}
}
return open_outcome;
}
void PulseLoopbackAudioStream::Start(AudioInputCallback* callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!sink_);
sink_ = callback;
stream_->Start(callback);
}
void PulseLoopbackAudioStream::Stop() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
stream_->Stop();
sink_ = nullptr;
}
void PulseLoopbackAudioStream::Close() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!sink_);
if (mute_system_audio_) {
pulse::UnmuteAllSinks(mainloop_, context_);
}
CloseWrappedStream();
std::move(release_stream_callback_).Run(this);
}
double PulseLoopbackAudioStream::GetMaxVolume() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return stream_->GetMaxVolume();
}
void PulseLoopbackAudioStream::SetVolume(double volume) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
stream_->SetVolume(volume);
}
double PulseLoopbackAudioStream::GetVolume() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return stream_->GetVolume();
}
bool PulseLoopbackAudioStream::IsMuted() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return stream_->IsMuted();
}
void PulseLoopbackAudioStream::SetOutputDeviceForAec(
const std::string& output_device_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
stream_->SetOutputDeviceForAec(output_device_id);
}
void PulseLoopbackAudioStream::ChangeStreamSource(
const std::string& source_name) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
stream_->Stop();
CloseWrappedStream();
stream_ = new PulseAudioInputStream(nullptr, source_name, params_, mainloop_,
context_, log_callback_);
CHECK(stream_);
// Open the new stream iff the old one was open.
if (!stream_opened_) {
return;
}
if (stream_->Open() != OpenOutcome::kSuccess) {
stream_opened_ = false;
if (sink_) {
sink_->OnError();
}
return;
}
// Start the new stream iff the old one was started.
if (sink_) {
stream_->Start(sink_);
}
}
void PulseLoopbackAudioStream::CloseWrappedStream() {
// Avoid dangling pointers.
auto* stream = stream_.get();
stream_ = nullptr;
stream->Close();
delete stream;
}
} // namespace media