Index: remoting/host/audio_capturer_linux.cc |
diff --git a/remoting/host/audio_capturer_linux.cc b/remoting/host/audio_capturer_linux.cc |
index 1901dfcd96862a69dee3574b7cfe9caaa5900fe8..419f8c8ec6dd7fb184c6f9e19cf61eedc48ee1ef 100644 |
--- a/remoting/host/audio_capturer_linux.cc |
+++ b/remoting/host/audio_capturer_linux.cc |
@@ -2,14 +2,151 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include "remoting/host/audio_capturer_linux.h" |
+ |
+#include <fcntl.h> |
+#include <sys/stat.h> |
+#include <sys/types.h> |
+#include <unistd.h> |
+ |
+#include "base/command_line.h" |
+#include "base/eintr_wrapper.h" |
+#include "base/file_path.h" |
#include "base/logging.h" |
-#include "remoting/host/audio_capturer.h" |
+#include "base/stl_util.h" |
+#include "remoting/proto/audio.pb.h" |
namespace remoting { |
+namespace { |
+ |
+const char kAudioPipeOptionName[] = "audio-pipe-name"; |
+ |
+// PulseAudio's module-pipe-sink must be configured to use the following |
+// parameters for the sink we read from. |
+const AudioPacket_SamplingRate kSamplingRate = AudioPacket::SAMPLING_RATE_44100; |
+const int kChannels = 2; |
+const int kBytesPerSample = 2; |
+ |
+// Read data from the pipe every 40ms. |
+const int kCapturingPeriodMs = 40; |
+ |
+} // namespace |
+ |
+AudioCapturerLinux::AudioCapturerLinux(const FilePath& pipe_name) { |
+ pipe_fd_ = HANDLE_EINTR(open( |
+ pipe_name.value().c_str(), O_RDONLY | O_NONBLOCK)); |
+ if (pipe_fd_ < 0) { |
+ LOG(ERROR) << "Failed to open " << pipe_name.value(); |
+ return; |
+ } |
+ |
+ // Set buffer size for the pipe to the double of what's required for samples |
+ // of each capturing period. |
+ int pipe_buffer_size = 2 * kCapturingPeriodMs * kSamplingRate * kChannels * |
+ kBytesPerSample / base::Time::kMillisecondsPerSecond; |
+ int result = HANDLE_EINTR(fcntl(pipe_fd_, F_SETPIPE_SZ, pipe_buffer_size)); |
+ if (result < 0) { |
+ PLOG(ERROR) << "fcntl"; |
+ } |
+ |
+ Sleep(); |
Wez
2012/09/06 21:47:29
Why do this here, rather than in Start?
Sergey Ulanov
2012/09/06 23:18:53
Capturer is started only when a user is connected,
Wez
2012/09/06 23:32:39
Do we actually need to do that, given that there w
Sergey Ulanov
2012/09/07 00:22:03
Yes, we do need to do it. Otherwise we may break s
|
+} |
+ |
+AudioCapturerLinux::~AudioCapturerLinux() { |
+} |
+ |
+bool AudioCapturerLinux::Start(const PacketCapturedCallback& callback) { |
+ if (pipe_fd_ < 0) |
+ return false; |
+ |
+ callback_ = callback; |
+ |
+ return true; |
+} |
+ |
+void AudioCapturerLinux::Stop() { |
+ callback_.Reset(); |
+} |
+ |
+bool AudioCapturerLinux::IsRunning() { |
+ return !callback_.is_null(); |
+} |
+ |
+void AudioCapturerLinux::OnFileCanReadWithoutBlocking(int fd) { |
+ DCHECK_EQ(fd, pipe_fd_); |
+ StartTimer(); |
+} |
+ |
+void AudioCapturerLinux::OnFileCanWriteWithoutBlocking(int fd) { |
+ NOTREACHED(); |
+} |
+ |
+void AudioCapturerLinux::StartTimer() { |
+ started_time_ = base::TimeTicks::Now(); |
+ last_capture_time_samples_ = 0; |
+ timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kCapturingPeriodMs), |
+ this, &AudioCapturerLinux::DoCapture); |
+} |
+ |
+void AudioCapturerLinux::DoCapture() { |
+ DCHECK_GT(pipe_fd_, 0); |
+ |
+ base::TimeDelta stream_position = base::TimeTicks::Now() - started_time_; |
Wez
2012/09/06 21:47:29
Add a comment explaining what this calculation is
Sergey Ulanov
2012/09/06 23:18:53
Done.
|
+ int64 stream_position_samples = stream_position.InMilliseconds() * |
+ kSamplingRate / base::Time::kMillisecondsPerSecond; |
+ int64 samples_to_capture = |
+ stream_position_samples - last_capture_time_samples_; |
+ last_capture_time_samples_ = stream_position_samples; |
+ int64 read_size = |
+ samples_to_capture * kChannels * kBytesPerSample; |
+ |
+ std::string data; |
+ data.resize(read_size); |
+ int pos = 0; |
+ while (pos < read_size) { |
+ int read_result = HANDLE_EINTR( |
+ read(pipe_fd_, string_as_array(&data) + pos, read_size - pos)); |
+ if (read_result >= 0) { |
+ pos += read_result; |
+ } else { |
+ if (errno != EWOULDBLOCK && errno != EAGAIN) |
+ PLOG(ERROR) << "read"; |
+ break; |
+ } |
+ } |
+ |
+ if (pos == 0) { |
+ Sleep(); |
+ return; |
+ } |
+ |
+ if (!callback_.is_null()) { |
+ data.resize(pos); |
+ |
+ scoped_ptr<AudioPacket> packet(new AudioPacket()); |
+ packet->add_data(data); |
+ packet->set_encoding(AudioPacket::ENCODING_RAW); |
+ packet->set_sampling_rate(kSamplingRate); |
+ packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
+ packet->set_channels(AudioPacket::CHANNELS_STEREO); |
+ callback_.Run(packet.Pass()); |
+ } |
+} |
+ |
+void AudioCapturerLinux::Sleep() { |
Wez
2012/09/06 21:47:29
Sleep -> e.g. WaitForDataReady / WaitForPipe / Wai
Sergey Ulanov
2012/09/06 23:18:53
Done.
|
+ timer_.Stop(); |
+ MessageLoopForIO::current()->WatchFileDescriptor( |
+ pipe_fd_, false, MessageLoopForIO::WATCH_READ, |
+ &file_descriptor_watcher_, this); |
+} |
+ |
scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
- NOTIMPLEMENTED(); |
- return scoped_ptr<AudioCapturer>(NULL); |
+ CommandLine* cl = CommandLine::ForCurrentProcess(); |
+ FilePath path = cl->GetSwitchValuePath(kAudioPipeOptionName); |
+ if (path.empty()) |
+ return scoped_ptr<AudioCapturer>(); |
Wez
2012/09/06 21:47:29
You'll need to update this to provide a SupportsAu
Sergey Ulanov
2012/09/06 23:18:53
Done.
|
+ return scoped_ptr<AudioCapturer>(new AudioCapturerLinux(path)); |
} |
} // namespace remoting |