| OLD | NEW |
| (Empty) | |
| 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 |
| 5 #ifndef REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ |
| 6 #define REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/observer_list_threadsafe.h" |
| 12 #include "base/time.h" |
| 13 #include "base/timer.h" |
| 14 |
| 15 class FilePath; |
| 16 |
| 17 namespace remoting { |
| 18 |
| 19 // AudioPipeReader class reads from a named pipe to which an audio server (e.g. |
| 20 // pulseaudio) writes the sound that's being played back and then sends data to |
| 21 // all registered observers. |
| 22 class AudioPipeReader |
| 23 : public base::RefCountedThreadSafe<AudioPipeReader>, |
| 24 public MessageLoopForIO::Watcher { |
| 25 public: |
| 26 class StreamObserver { |
| 27 public: |
| 28 virtual void OnDataRead(scoped_refptr<base::RefCountedString> data) = 0; |
| 29 }; |
| 30 |
| 31 // |task_runner| defines the IO thread on which the object will be reading |
| 32 // data from the pipe. |
| 33 AudioPipeReader( |
| 34 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 35 const FilePath& pipe_name); |
| 36 |
| 37 // Register or unregister an observer. Each observer receives data on the |
| 38 // thread on which it was registered and guaranteed not to be called after |
| 39 // RemoveObserver(). |
| 40 void AddObserver(StreamObserver* observer); |
| 41 void RemoveObserver(StreamObserver* observer); |
| 42 |
| 43 // MessageLoopForIO::Watcher interface. |
| 44 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 45 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| 46 |
| 47 private: |
| 48 friend class base::RefCountedThreadSafe<AudioPipeReader>; |
| 49 virtual ~AudioPipeReader(); |
| 50 |
| 51 void StartOnAudioThread(const FilePath& pipe_name); |
| 52 void StartTimer(); |
| 53 void DoCapture(); |
| 54 void WaitForPipeReadable(); |
| 55 |
| 56 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 57 |
| 58 int pipe_fd_; |
| 59 base::RepeatingTimer<AudioPipeReader> timer_; |
| 60 scoped_refptr<ObserverListThreadSafe<StreamObserver> > observers_; |
| 61 |
| 62 // Time when capturing was started. |
| 63 base::TimeTicks started_time_; |
| 64 |
| 65 // Stream position of the last capture. |
| 66 int64 last_capture_samples_; |
| 67 |
| 68 // Bytes left from the previous read. |
| 69 std::string left_over_bytes_; |
| 70 |
| 71 MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(AudioPipeReader); |
| 74 }; |
| 75 |
| 76 } // namespace remoting |
| 77 |
| 78 #endif // REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_ |
| OLD | NEW |