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.
5 #ifndef REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_
6 #define REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_
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"
21 struct AudioPipeReaderTraits
;
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.
27 : public base::RefCountedThreadSafe
<AudioPipeReader
, AudioPipeReaderTraits
>,
28 public base::MessageLoopForIO::Watcher
{
30 class StreamObserver
{
32 virtual void OnDataRead(scoped_refptr
<base::RefCountedString
> data
) = 0;
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
);
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
43 void AddObserver(StreamObserver
* observer
);
44 void RemoveObserver(StreamObserver
* observer
);
46 // MessageLoopForIO::Watcher interface.
47 virtual void OnFileCanReadWithoutBlocking(int fd
) OVERRIDE
;
48 virtual void OnFileCanWriteWithoutBlocking(int fd
) OVERRIDE
;
51 friend class base::DeleteHelper
<AudioPipeReader
>;
52 friend class base::RefCountedThreadSafe
<AudioPipeReader
>;
53 friend struct AudioPipeReaderTraits
;
55 AudioPipeReader(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
56 virtual ~AudioPipeReader();
58 void StartOnAudioThread(const base::FilePath
& pipe_name
);
61 void WaitForPipeReadable();
63 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
66 base::RepeatingTimer
<AudioPipeReader
> timer_
;
67 scoped_refptr
<ObserverListThreadSafe
<StreamObserver
> > observers_
;
69 // Time when capturing was started.
70 base::TimeTicks started_time_
;
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
75 int64 last_capture_position_
;
77 // Bytes left from the previous read.
78 std::string left_over_bytes_
;
80 base::MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_
;
82 DISALLOW_COPY_AND_ASSIGN(AudioPipeReader
);
85 // Destroys |audio_pipe_reader| on the audio thread.
86 struct AudioPipeReaderTraits
{
87 static void Destruct(const AudioPipeReader
* audio_pipe_reader
);
90 } // namespace remoting
92 #endif // REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_