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 // A fake implementation of AudioInputStream, useful for testing purpose. | 5 // A fake implementation of AudioInputStream, useful for testing purpose. |
6 | 6 |
7 #ifndef MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_ | 7 #ifndef MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_ |
8 #define MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_ | 8 #define MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_ |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
| 12 #include "base/callback_forward.h" |
12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
15 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "media/audio/audio_io.h" | 18 #include "media/audio/audio_io.h" |
18 #include "media/audio/audio_parameters.h" | 19 #include "media/audio/audio_parameters.h" |
| 20 #include "media/audio/fake_audio_provider.h" |
19 #include "media/audio/sounds/wav_audio_handler.h" | 21 #include "media/audio/sounds/wav_audio_handler.h" |
20 #include "media/base/audio_converter.h" | |
21 | 22 |
22 namespace media { | 23 namespace media { |
23 | 24 |
24 class AudioBus; | 25 class AudioBus; |
25 class AudioManagerBase; | 26 class AudioManagerBase; |
26 | 27 |
27 // This class can either generate a beep sound or play audio from a file. | 28 // This class acts as a fake audio input stream. The default is to generate a |
| 29 // beeping sound unless --use-file-for-fake-audio-capture=<file> is specified, |
| 30 // in which case the indicated .wav file will be read and played into the |
| 31 // stream. |
28 class MEDIA_EXPORT FakeAudioInputStream | 32 class MEDIA_EXPORT FakeAudioInputStream |
29 : public AudioInputStream, AudioConverter::InputCallback { | 33 : public AudioInputStream { |
30 public: | 34 public: |
31 static AudioInputStream* MakeFakeStream( | 35 static AudioInputStream* MakeFakeStream( |
32 AudioManagerBase* manager, const AudioParameters& params); | 36 AudioManagerBase* manager, const AudioParameters& params); |
33 | 37 |
34 bool Open() override; | 38 bool Open() override; |
35 void Start(AudioInputCallback* callback) override; | 39 void Start(AudioInputCallback* callback) override; |
36 void Stop() override; | 40 void Stop() override; |
37 void Close() override; | 41 void Close() override; |
38 double GetMaxVolume() override; | 42 double GetMaxVolume() override; |
39 void SetVolume(double volume) override; | 43 void SetVolume(double volume) override; |
40 double GetVolume() override; | 44 double GetVolume() override; |
41 bool IsMuted() override; | 45 bool IsMuted() override; |
42 bool SetAutomaticGainControl(bool enabled) override; | 46 bool SetAutomaticGainControl(bool enabled) override; |
43 bool GetAutomaticGainControl() override; | 47 bool GetAutomaticGainControl() override; |
44 | 48 |
45 // Generate one beep sound. This method is called by | 49 // Generate one beep sound. This method is called by |
46 // FakeVideoCaptureDevice to test audio/video synchronization. | 50 // FakeVideoCaptureDevice to test audio/video synchronization. |
47 // This is a static method because FakeVideoCaptureDevice is | 51 // This is a static method because FakeVideoCaptureDevice is |
48 // disconnected from an audio device. This means only one instance of | 52 // disconnected from an audio device. This means only one instance of |
49 // this class gets to respond, which is okay because we assume there's | 53 // this class gets to respond, which is okay because we assume there's |
50 // only one stream for this testing purpose. | 54 // only one stream for this testing purpose. Furthermore this method will do |
| 55 // nothing if --use-file-for-fake-audio-capture is specified since the input |
| 56 // stream will be playing from a file instead of beeping. |
51 // TODO(hclam): Make this non-static. To do this we'll need to fix | 57 // TODO(hclam): Make this non-static. To do this we'll need to fix |
52 // crbug.com/159053 such that video capture device is aware of audio | 58 // crbug.com/159053 such that video capture device is aware of audio |
53 // input stream. | 59 // input stream. |
54 static void BeepOnce(); | 60 static void BeepOnce(); |
55 | 61 |
56 private: | 62 private: |
57 FakeAudioInputStream(AudioManagerBase* manager, | 63 FakeAudioInputStream(AudioManagerBase* manager, |
58 const AudioParameters& params); | 64 const AudioParameters& params); |
59 ~FakeAudioInputStream() override; | 65 ~FakeAudioInputStream() override; |
60 | 66 |
61 void DoCallback(); | 67 void OnMoreInputData(AudioBus* audio_bus, int buffer_size); |
62 | |
63 // Opens this stream reading from a |wav_filename| rather than beeping. | |
64 void OpenInFileMode(const base::FilePath& wav_filename); | |
65 | |
66 // Returns true if the device is playing from a file; false if we're beeping. | |
67 bool PlayingFromFile(); | |
68 | |
69 void PlayFile(); | |
70 void PlayBeep(); | |
71 | 68 |
72 AudioManagerBase* audio_manager_; | 69 AudioManagerBase* audio_manager_; |
73 AudioInputCallback* callback_; | 70 AudioInputCallback* callback_; |
74 scoped_ptr<uint8[]> buffer_; | 71 FakeAudioProvider fake_audio_provider_; |
75 int buffer_size_; | |
76 AudioParameters params_; | |
77 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
78 base::TimeTicks last_callback_time_; | |
79 base::TimeDelta callback_interval_; | |
80 base::TimeDelta interval_from_last_beep_; | |
81 int beep_duration_in_buffers_; | |
82 int beep_generated_in_buffers_; | |
83 int beep_period_in_frames_; | |
84 scoped_ptr<media::AudioBus> audio_bus_; | |
85 scoped_ptr<uint8[]> wav_file_data_; | |
86 scoped_ptr<media::WavAudioHandler> wav_audio_handler_; | |
87 scoped_ptr<media::AudioConverter> file_audio_converter_; | |
88 int wav_file_read_pos_; | |
89 | |
90 // Allows us to run tasks on the FakeAudioInputStream instance which are | |
91 // bound by its lifetime. | |
92 base::WeakPtrFactory<FakeAudioInputStream> weak_factory_; | |
93 | |
94 // If running in file mode, this provides audio data from wav_audio_handler_. | |
95 double ProvideInput(AudioBus* audio_bus, | |
96 base::TimeDelta buffer_delay) override; | |
97 | 72 |
98 DISALLOW_COPY_AND_ASSIGN(FakeAudioInputStream); | 73 DISALLOW_COPY_AND_ASSIGN(FakeAudioInputStream); |
99 }; | 74 }; |
100 | 75 |
101 } // namespace media | 76 } // namespace media |
102 | 77 |
103 #endif // MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_ | 78 #endif // MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_ |
OLD | NEW |