OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_ | |
6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/gtest_prod_util.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "components/copresence/mediums/audio/audio_recorder.h" | |
14 #include "media/audio/audio_io.h" | |
15 #include "media/audio/audio_parameters.h" | |
16 #include "media/base/audio_converter.h" | |
17 | |
18 namespace base { | |
19 class MessageLoop; | |
20 } | |
21 | |
22 namespace media { | |
23 class AudioBus; | |
24 } | |
25 | |
26 namespace copresence { | |
27 | |
28 // The AudioRecorder class will record audio until told to stop. | |
29 class AudioRecorderImpl final | |
30 : public AudioRecorder, | |
31 public media::AudioInputStream::AudioInputCallback, | |
32 public media::AudioConverter::InputCallback { | |
33 public: | |
34 using RecordedSamplesCallback = base::Callback<void(const std::string&)>; | |
35 | |
36 AudioRecorderImpl(); | |
37 | |
38 // AudioRecorder overrides: | |
39 void Initialize(const RecordedSamplesCallback& decode_callback) override; | |
40 void Record() override; | |
41 void Stop() override; | |
42 void Finalize() override; | |
43 | |
44 // Takes ownership of the stream. | |
45 void set_input_stream_for_testing( | |
46 media::AudioInputStream* input_stream_for_testing) { | |
47 input_stream_for_testing_.reset(input_stream_for_testing); | |
48 } | |
49 | |
50 // Takes ownership of the params. | |
51 void set_params_for_testing(media::AudioParameters* params_for_testing) { | |
52 params_for_testing_.reset(params_for_testing); | |
53 } | |
54 | |
55 protected: | |
56 ~AudioRecorderImpl() override; | |
57 void set_is_recording(bool is_recording) { is_recording_ = is_recording; } | |
58 | |
59 private: | |
60 friend class AudioRecorderTest; | |
61 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, BasicRecordAndStop); | |
62 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, OutOfOrderRecordAndStopMultiple); | |
63 | |
64 // Methods to do our various operations; all of these need to be run on the | |
65 // audio thread. | |
66 void InitializeOnAudioThread(); | |
67 void RecordOnAudioThread(); | |
68 void StopOnAudioThread(); | |
69 void StopAndCloseOnAudioThread(); | |
70 void FinalizeOnAudioThread(); | |
71 | |
72 // AudioInputStream::AudioInputCallback overrides: | |
73 // Called by the audio recorder when a full packet of audio data is | |
74 // available. This is called from a special audio thread and the | |
75 // implementation should return as soon as possible. | |
76 void OnData(media::AudioInputStream* stream, | |
77 const media::AudioBus* source, | |
78 uint32 hardware_delay_bytes, | |
79 double volume) override; | |
80 void OnError(media::AudioInputStream* stream) override; | |
81 | |
82 // AudioConverter::InputCallback overrides: | |
83 double ProvideInput(media::AudioBus* dest, | |
84 base::TimeDelta buffer_delay) override; | |
85 | |
86 // Flushes the audio loop, making sure that any queued operations are | |
87 // performed. | |
88 void FlushAudioLoopForTesting(); | |
89 | |
90 bool is_recording_; | |
91 | |
92 media::AudioInputStream* stream_; | |
93 | |
94 RecordedSamplesCallback decode_callback_; | |
95 | |
96 // ProvideInput will use this buffer as its source. | |
97 const media::AudioBus* temp_conversion_buffer_; | |
98 | |
99 // Outside of the ctor/Initialize method, only access the next variables on | |
100 // the recording thread. | |
101 scoped_ptr<media::AudioBus> buffer_; | |
102 int total_buffer_frames_; | |
103 int buffer_frame_index_; | |
104 | |
105 scoped_ptr<media::AudioConverter> converter_; | |
106 | |
107 scoped_ptr<media::AudioInputStream> input_stream_for_testing_; | |
108 scoped_ptr<media::AudioParameters> params_for_testing_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(AudioRecorderImpl); | |
111 }; | |
112 | |
113 } // namespace copresence | |
114 | |
115 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_ | |
OLD | NEW |