• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/message_loop.h"
11 #include "base/observer_list_threadsafe.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 
15 namespace base {
16 class FilePath;
17 }
18 
19 namespace remoting {
20 
21 struct AudioPipeReaderTraits;
22 
23 // AudioPipeReader class reads from a named pipe to which an audio server (e.g.
24 // pulseaudio) writes the sound that's being played back and then sends data to
25 // all registered observers.
26 class AudioPipeReader
27   : public base::RefCountedThreadSafe<AudioPipeReader, AudioPipeReaderTraits>,
28       public base::MessageLoopForIO::Watcher {
29  public:
30   class StreamObserver {
31    public:
32     virtual void OnDataRead(scoped_refptr<base::RefCountedString> data) = 0;
33   };
34 
35   // |task_runner| specifies the IO thread to use to read data from the pipe.
36   static scoped_refptr<AudioPipeReader> Create(
37       scoped_refptr<base::SingleThreadTaskRunner> task_runner,
38       const base::FilePath& pipe_name);
39 
40   // Register or unregister an observer. Each observer receives data on the
41   // thread on which it was registered and guaranteed not to be called after
42   // RemoveObserver().
43   void AddObserver(StreamObserver* observer);
44   void RemoveObserver(StreamObserver* observer);
45 
46   // MessageLoopForIO::Watcher interface.
47   virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
48   virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
49 
50  private:
51   friend class base::DeleteHelper<AudioPipeReader>;
52   friend class base::RefCountedThreadSafe<AudioPipeReader>;
53   friend struct AudioPipeReaderTraits;
54 
55   AudioPipeReader(scoped_refptr<base::SingleThreadTaskRunner> task_runner);
56   virtual ~AudioPipeReader();
57 
58   void StartOnAudioThread(const base::FilePath& pipe_name);
59   void StartTimer();
60   void DoCapture();
61   void WaitForPipeReadable();
62 
63   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
64 
65   int pipe_fd_;
66   base::RepeatingTimer<AudioPipeReader> timer_;
67   scoped_refptr<ObserverListThreadSafe<StreamObserver> > observers_;
68 
69   // Time when capturing was started.
70   base::TimeTicks started_time_;
71 
72   // Stream position of the last capture in bytes with zero position
73   // corresponding to |started_time_|. Must always be a multiple of the sample
74   // size.
75   int64 last_capture_position_;
76 
77   // Bytes left from the previous read.
78   std::string left_over_bytes_;
79 
80   base::MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_;
81 
82   DISALLOW_COPY_AND_ASSIGN(AudioPipeReader);
83 };
84 
85 // Destroys |audio_pipe_reader| on the audio thread.
86 struct AudioPipeReaderTraits {
87   static void Destruct(const AudioPipeReader* audio_pipe_reader);
88 };
89 
90 }  // namespace remoting
91 
92 #endif  // REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_
93