audio_capturer_linux.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.
  4. #include "remoting/host/audio_capturer_linux.h"
  5. #include <stdint.h>
  6. #include <utility>
  7. #include "base/check.h"
  8. #include "base/files/file_path.h"
  9. #include "base/lazy_instance.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "remoting/proto/audio.pb.h"
  12. namespace remoting {
  13. namespace {
  14. base::LazyInstance<scoped_refptr<AudioPipeReader>>::Leaky
  15. g_pulseaudio_pipe_sink_reader = LAZY_INSTANCE_INITIALIZER;
  16. } // namespace
  17. // TODO(wez): Remove this and have the DesktopEnvironmentFactory own the
  18. // AudioPipeReader rather than having it process-global.
  19. // See crbug.com/161373 and crbug.com/104544.
  20. void AudioCapturerLinux::InitializePipeReader(
  21. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  22. const base::FilePath& pipe_name) {
  23. scoped_refptr<AudioPipeReader> pipe_reader;
  24. if (!pipe_name.empty())
  25. pipe_reader = AudioPipeReader::Create(task_runner, pipe_name);
  26. g_pulseaudio_pipe_sink_reader.Get() = pipe_reader;
  27. }
  28. AudioCapturerLinux::AudioCapturerLinux(
  29. scoped_refptr<AudioPipeReader> pipe_reader)
  30. : pipe_reader_(pipe_reader),
  31. silence_detector_(0) {
  32. }
  33. AudioCapturerLinux::~AudioCapturerLinux() {
  34. pipe_reader_->RemoveObserver(this);
  35. }
  36. bool AudioCapturerLinux::Start(const PacketCapturedCallback& callback) {
  37. callback_ = callback;
  38. silence_detector_.Reset(AudioPipeReader::kSamplingRate,
  39. AudioPipeReader::kChannels);
  40. pipe_reader_->AddObserver(this);
  41. return true;
  42. }
  43. void AudioCapturerLinux::OnDataRead(
  44. scoped_refptr<base::RefCountedString> data) {
  45. DCHECK(!callback_.is_null());
  46. if (silence_detector_.IsSilence(
  47. reinterpret_cast<const int16_t*>(data->data().data()),
  48. data->data().size() / sizeof(int16_t) / AudioPipeReader::kChannels)) {
  49. return;
  50. }
  51. std::unique_ptr<AudioPacket> packet(new AudioPacket());
  52. packet->add_data(data->data());
  53. packet->set_encoding(AudioPacket::ENCODING_RAW);
  54. packet->set_sampling_rate(AudioPipeReader::kSamplingRate);
  55. packet->set_bytes_per_sample(AudioPipeReader::kBytesPerSample);
  56. packet->set_channels(AudioPipeReader::kChannels);
  57. callback_.Run(std::move(packet));
  58. }
  59. bool AudioCapturer::IsSupported() {
  60. return g_pulseaudio_pipe_sink_reader.Get().get() != nullptr;
  61. }
  62. std::unique_ptr<AudioCapturer> AudioCapturer::Create() {
  63. scoped_refptr<AudioPipeReader> reader =
  64. g_pulseaudio_pipe_sink_reader.Get();
  65. if (!reader.get())
  66. return nullptr;
  67. return base::WrapUnique(new AudioCapturerLinux(reader));
  68. }
  69. } // namespace remoting