1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/audio_capturer_linux.h"
7 #include "base/files/file_path.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "remoting/proto/audio.pb.h"
16 base::LazyInstance
<scoped_refptr
<AudioPipeReader
> >::Leaky
17 g_pulseaudio_pipe_sink_reader
= LAZY_INSTANCE_INITIALIZER
;
21 // TODO(wez): Remove this and have the DesktopEnvironmentFactory own the
22 // AudioPipeReader rather than having it process-global.
23 // See crbug.com/161373 and crbug.com/104544.
24 void AudioCapturerLinux::InitializePipeReader(
25 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
26 const base::FilePath
& pipe_name
) {
27 scoped_refptr
<AudioPipeReader
> pipe_reader
;
28 if (!pipe_name
.empty())
29 pipe_reader
= AudioPipeReader::Create(task_runner
, pipe_name
);
30 g_pulseaudio_pipe_sink_reader
.Get() = pipe_reader
;
33 AudioCapturerLinux::AudioCapturerLinux(
34 scoped_refptr
<AudioPipeReader
> pipe_reader
)
35 : pipe_reader_(pipe_reader
),
36 silence_detector_(0) {
39 AudioCapturerLinux::~AudioCapturerLinux() {
40 pipe_reader_
->RemoveObserver(this);
43 bool AudioCapturerLinux::Start(const PacketCapturedCallback
& callback
) {
45 silence_detector_
.Reset(AudioPipeReader::kSamplingRate
,
46 AudioPipeReader::kChannels
);
47 pipe_reader_
->AddObserver(this);
51 void AudioCapturerLinux::OnDataRead(
52 scoped_refptr
<base::RefCountedString
> data
) {
53 DCHECK(!callback_
.is_null());
55 if (silence_detector_
.IsSilence(
56 reinterpret_cast<const int16
*>(data
->data().data()),
57 data
->data().size() / sizeof(int16
))) {
61 scoped_ptr
<AudioPacket
> packet(new AudioPacket());
62 packet
->add_data(data
->data());
63 packet
->set_encoding(AudioPacket::ENCODING_RAW
);
64 packet
->set_sampling_rate(AudioPipeReader::kSamplingRate
);
65 packet
->set_bytes_per_sample(AudioPipeReader::kBytesPerSample
);
66 packet
->set_channels(AudioPipeReader::kChannels
);
67 callback_
.Run(packet
.Pass());
70 bool AudioCapturer::IsSupported() {
71 return g_pulseaudio_pipe_sink_reader
.Get().get() != nullptr;
74 scoped_ptr
<AudioCapturer
> AudioCapturer::Create() {
75 scoped_refptr
<AudioPipeReader
> reader
=
76 g_pulseaudio_pipe_sink_reader
.Get();
79 return make_scoped_ptr(new AudioCapturerLinux(reader
));
82 } // namespace remoting