Copy Smart Lock user preferences to local state so we can access them on the sign...
[chromium-blink-merge.git] / remoting / host / linux / audio_pipe_reader.h
blob33b9b41418cbbf2b66916d30334b1291f83b26d5
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/files/file.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_path_watcher.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/observer_list_threadsafe.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "remoting/proto/audio.pb.h"
19 namespace remoting {
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.
26 class AudioPipeReader
27 : public base::RefCountedThreadSafe<AudioPipeReader, AudioPipeReaderTraits>,
28 public base::MessageLoopForIO::Watcher {
29 public:
30 // PulseAudio's module-pipe-sink must be configured to use the following
31 // parameters for the sink we read from.
32 static const AudioPacket_SamplingRate kSamplingRate =
33 AudioPacket::SAMPLING_RATE_48000;
34 static const AudioPacket_BytesPerSample kBytesPerSample =
35 AudioPacket::BYTES_PER_SAMPLE_2;
36 static const AudioPacket_Channels kChannels = AudioPacket::CHANNELS_STEREO;
38 class StreamObserver {
39 public:
40 virtual void OnDataRead(scoped_refptr<base::RefCountedString> data) = 0;
43 // |task_runner| specifies the IO thread to use to read data from the pipe.
44 static scoped_refptr<AudioPipeReader> Create(
45 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
46 const base::FilePath& pipe_path);
48 // Register or unregister an observer. Each observer receives data on the
49 // thread on which it was registered and guaranteed not to be called after
50 // RemoveObserver().
51 void AddObserver(StreamObserver* observer);
52 void RemoveObserver(StreamObserver* observer);
54 // MessageLoopForIO::Watcher interface.
55 void OnFileCanReadWithoutBlocking(int fd) override;
56 void OnFileCanWriteWithoutBlocking(int fd) override;
58 private:
59 friend class base::DeleteHelper<AudioPipeReader>;
60 friend class base::RefCountedThreadSafe<AudioPipeReader>;
61 friend struct AudioPipeReaderTraits;
63 AudioPipeReader(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
64 const base::FilePath& pipe_path);
65 ~AudioPipeReader() override;
67 void StartOnAudioThread();
68 void OnDirectoryChanged(const base::FilePath& path, bool error);
69 void TryOpenPipe();
70 void StartTimer();
71 void DoCapture();
72 void WaitForPipeReadable();
74 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
75 base::FilePath pipe_path_;
77 // Watcher for the directory that contains audio pipe we are reading from, to
78 // monitor when pulseaudio creates or deletes it.
79 base::FilePathWatcher file_watcher_;
81 base::File pipe_;
82 base::RepeatingTimer<AudioPipeReader> timer_;
83 scoped_refptr<ObserverListThreadSafe<StreamObserver> > observers_;
85 // Size of the pipe buffer.
86 int pipe_buffer_size_;
88 // Period between pipe reads.
89 base::TimeDelta capture_period_;
91 // Time when capturing was started.
92 base::TimeTicks started_time_;
94 // Stream position of the last capture in bytes with zero position
95 // corresponding to |started_time_|. Must always be a multiple of the sample
96 // size.
97 int64 last_capture_position_;
99 // Bytes left from the previous read.
100 std::string left_over_bytes_;
102 base::MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_;
104 DISALLOW_COPY_AND_ASSIGN(AudioPipeReader);
107 // Destroys |audio_pipe_reader| on the audio thread.
108 struct AudioPipeReaderTraits {
109 static void Destruct(const AudioPipeReader* audio_pipe_reader);
112 } // namespace remoting
114 #endif // REMOTING_HOST_LINUX_AUDIO_PIPE_READER_H_