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.
5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_
6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_
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"
26 namespace copresence {
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 {
34 typedef base::Callback<void(const std::string&)> RecordedSamplesCallback;
38 // AudioRecorder overrides:
39 void Initialize(const RecordedSamplesCallback& decode_callback) override;
40 void Record() override;
42 void Finalize() override;
43 bool IsRecording() override;
45 // Takes ownership of the stream.
46 void set_input_stream_for_testing(
47 media::AudioInputStream* input_stream_for_testing) {
48 input_stream_for_testing_.reset(input_stream_for_testing);
51 // Takes ownership of the params.
52 void set_params_for_testing(media::AudioParameters* params_for_testing) {
53 params_for_testing_.reset(params_for_testing);
57 ~AudioRecorderImpl() override;
58 void set_is_recording(bool is_recording) { is_recording_ = is_recording; }
61 friend class AudioRecorderTest;
62 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, BasicRecordAndStop);
63 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, OutOfOrderRecordAndStopMultiple);
65 // Methods to do our various operations; all of these need to be run on the
67 void InitializeOnAudioThread();
68 void RecordOnAudioThread();
69 void StopOnAudioThread();
70 void StopAndCloseOnAudioThread();
71 void FinalizeOnAudioThread();
73 // AudioInputStream::AudioInputCallback overrides:
74 // Called by the audio recorder when a full packet of audio data is
75 // available. This is called from a special audio thread and the
76 // implementation should return as soon as possible.
77 void OnData(media::AudioInputStream* stream,
78 const media::AudioBus* source,
79 uint32 hardware_delay_bytes,
80 double volume) override;
81 void OnError(media::AudioInputStream* stream) override;
83 // AudioConverter::InputCallback overrides:
84 double ProvideInput(media::AudioBus* dest,
85 base::TimeDelta buffer_delay) override;
87 // Flushes the audio loop, making sure that any queued operations are
89 void FlushAudioLoopForTesting();
93 media::AudioInputStream* stream_;
95 RecordedSamplesCallback decode_callback_;
97 // ProvideInput will use this buffer as its source.
98 const media::AudioBus* temp_conversion_buffer_;
100 // Outside of the ctor/Initialize method, only access the next variables on
101 // the recording thread.
102 scoped_ptr<media::AudioBus> buffer_;
103 int total_buffer_frames_;
104 int buffer_frame_index_;
106 scoped_ptr<media::AudioConverter> converter_;
108 scoped_ptr<media::AudioInputStream> input_stream_for_testing_;
109 scoped_ptr<media::AudioParameters> params_for_testing_;
111 DISALLOW_COPY_AND_ASSIGN(AudioRecorderImpl);
114 } // namespace copresence
116 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_