| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ | 5 #ifndef REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ |
| 6 #define REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ | 6 #define REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 11 |
| 10 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 13 #include "base/files/file_descriptor_watcher_posix.h" |
| 11 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 12 #include "base/files/file_path_watcher.h" | 15 #include "base/files/file_path_watcher.h" |
| 13 #include "base/macros.h" | 16 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/ref_counted_memory.h" | 18 #include "base/memory/ref_counted_memory.h" |
| 16 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 17 #include "base/observer_list_threadsafe.h" | 20 #include "base/observer_list_threadsafe.h" |
| 18 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 19 #include "base/timer/timer.h" | 22 #include "base/timer/timer.h" |
| 20 #include "remoting/proto/audio.pb.h" | 23 #include "remoting/proto/audio.pb.h" |
| 21 | 24 |
| 22 namespace remoting { | 25 namespace remoting { |
| 23 | 26 |
| 24 struct AudioPipeReaderTraits; | 27 struct AudioPipeReaderTraits; |
| 25 | 28 |
| 26 // AudioPipeReader class reads from a named pipe to which an audio server (e.g. | 29 // AudioPipeReader class reads from a named pipe to which an audio server (e.g. |
| 27 // pulseaudio) writes the sound that's being played back and then sends data to | 30 // pulseaudio) writes the sound that's being played back and then sends data to |
| 28 // all registered observers. | 31 // all registered observers. |
| 29 class AudioPipeReader | 32 class AudioPipeReader |
| 30 : public base::RefCountedThreadSafe<AudioPipeReader, AudioPipeReaderTraits>, | 33 : public base::RefCountedThreadSafe<AudioPipeReader, |
| 31 public base::MessageLoopForIO::Watcher { | 34 AudioPipeReaderTraits> { |
| 32 public: | 35 public: |
| 33 // PulseAudio's module-pipe-sink must be configured to use the following | 36 // PulseAudio's module-pipe-sink must be configured to use the following |
| 34 // parameters for the sink we read from. | 37 // parameters for the sink we read from. |
| 35 static const AudioPacket_SamplingRate kSamplingRate = | 38 static const AudioPacket_SamplingRate kSamplingRate = |
| 36 AudioPacket::SAMPLING_RATE_48000; | 39 AudioPacket::SAMPLING_RATE_48000; |
| 37 static const AudioPacket_BytesPerSample kBytesPerSample = | 40 static const AudioPacket_BytesPerSample kBytesPerSample = |
| 38 AudioPacket::BYTES_PER_SAMPLE_2; | 41 AudioPacket::BYTES_PER_SAMPLE_2; |
| 39 static const AudioPacket_Channels kChannels = AudioPacket::CHANNELS_STEREO; | 42 static const AudioPacket_Channels kChannels = AudioPacket::CHANNELS_STEREO; |
| 40 | 43 |
| 41 class StreamObserver { | 44 class StreamObserver { |
| 42 public: | 45 public: |
| 43 virtual void OnDataRead(scoped_refptr<base::RefCountedString> data) = 0; | 46 virtual void OnDataRead(scoped_refptr<base::RefCountedString> data) = 0; |
| 44 }; | 47 }; |
| 45 | 48 |
| 46 // |task_runner| specifies the IO thread to use to read data from the pipe. | 49 // |task_runner| specifies the IO thread to use to read data from the pipe. |
| 47 static scoped_refptr<AudioPipeReader> Create( | 50 static scoped_refptr<AudioPipeReader> Create( |
| 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 51 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 49 const base::FilePath& pipe_path); | 52 const base::FilePath& pipe_path); |
| 50 | 53 |
| 51 // Register or unregister an observer. Each observer receives data on the | 54 // Register or unregister an observer. Each observer receives data on the |
| 52 // thread on which it was registered and guaranteed not to be called after | 55 // thread on which it was registered and guaranteed not to be called after |
| 53 // RemoveObserver(). | 56 // RemoveObserver(). |
| 54 void AddObserver(StreamObserver* observer); | 57 void AddObserver(StreamObserver* observer); |
| 55 void RemoveObserver(StreamObserver* observer); | 58 void RemoveObserver(StreamObserver* observer); |
| 56 | 59 |
| 57 // MessageLoopForIO::Watcher interface. | |
| 58 void OnFileCanReadWithoutBlocking(int fd) override; | |
| 59 void OnFileCanWriteWithoutBlocking(int fd) override; | |
| 60 | |
| 61 private: | 60 private: |
| 62 friend class base::DeleteHelper<AudioPipeReader>; | 61 friend class base::DeleteHelper<AudioPipeReader>; |
| 63 friend class base::RefCountedThreadSafe<AudioPipeReader>; | 62 friend class base::RefCountedThreadSafe<AudioPipeReader>; |
| 64 friend struct AudioPipeReaderTraits; | 63 friend struct AudioPipeReaderTraits; |
| 65 | 64 |
| 66 AudioPipeReader(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 65 AudioPipeReader(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 67 const base::FilePath& pipe_path); | 66 const base::FilePath& pipe_path); |
| 68 ~AudioPipeReader() override; | 67 ~AudioPipeReader(); |
| 69 | 68 |
| 70 void StartOnAudioThread(); | 69 void StartOnAudioThread(); |
| 71 void OnDirectoryChanged(const base::FilePath& path, bool error); | 70 void OnDirectoryChanged(const base::FilePath& path, bool error); |
| 72 void TryOpenPipe(); | 71 void TryOpenPipe(); |
| 73 void StartTimer(); | 72 void StartTimer(); |
| 74 void DoCapture(); | 73 void DoCapture(); |
| 75 void WaitForPipeReadable(); | 74 void WaitForPipeReadable(); |
| 76 | 75 |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 76 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 78 base::FilePath pipe_path_; | 77 base::FilePath pipe_path_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 95 base::TimeTicks started_time_; | 94 base::TimeTicks started_time_; |
| 96 | 95 |
| 97 // Stream position of the last capture in bytes with zero position | 96 // Stream position of the last capture in bytes with zero position |
| 98 // corresponding to |started_time_|. Must always be a multiple of the sample | 97 // corresponding to |started_time_|. Must always be a multiple of the sample |
| 99 // size. | 98 // size. |
| 100 int64_t last_capture_position_; | 99 int64_t last_capture_position_; |
| 101 | 100 |
| 102 // Bytes left from the previous read. | 101 // Bytes left from the previous read. |
| 103 std::string left_over_bytes_; | 102 std::string left_over_bytes_; |
| 104 | 103 |
| 105 base::MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_; | 104 std::unique_ptr<base::FileDescriptorWatcher::Controller> |
| 105 pipe_watch_controller_; |
| 106 | 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(AudioPipeReader); | 107 DISALLOW_COPY_AND_ASSIGN(AudioPipeReader); |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 // Destroys |audio_pipe_reader| on the audio thread. | 110 // Destroys |audio_pipe_reader| on the audio thread. |
| 111 struct AudioPipeReaderTraits { | 111 struct AudioPipeReaderTraits { |
| 112 static void Destruct(const AudioPipeReader* audio_pipe_reader); | 112 static void Destruct(const AudioPipeReader* audio_pipe_reader); |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 } // namespace remoting | 115 } // namespace remoting |
| 116 | 116 |
| 117 #endif // REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ | 117 #endif // REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ |
| OLD | NEW |