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 using RecordedSamplesCallback
= base::Callback
<void(const std::string
&)>;
38 // AudioRecorder overrides:
39 void Initialize(const RecordedSamplesCallback
& decode_callback
) override
;
40 void Record() override
;
42 void Finalize() override
;
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
);
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
);
56 ~AudioRecorderImpl() override
;
57 void set_is_recording(bool is_recording
) { is_recording_
= is_recording
; }
60 friend class AudioRecorderTest
;
61 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest
, BasicRecordAndStop
);
62 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest
, OutOfOrderRecordAndStopMultiple
);
64 // Methods to do our various operations; all of these need to be run on the
66 void InitializeOnAudioThread();
67 void RecordOnAudioThread();
68 void StopOnAudioThread();
69 void StopAndCloseOnAudioThread();
70 void FinalizeOnAudioThread();
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
;
82 // AudioConverter::InputCallback overrides:
83 double ProvideInput(media::AudioBus
* dest
,
84 base::TimeDelta buffer_delay
) override
;
86 // Flushes the audio loop, making sure that any queued operations are
88 void FlushAudioLoopForTesting();
92 media::AudioInputStream
* stream_
;
94 RecordedSamplesCallback decode_callback_
;
96 // ProvideInput will use this buffer as its source.
97 const media::AudioBus
* temp_conversion_buffer_
;
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_
;
105 scoped_ptr
<media::AudioConverter
> converter_
;
107 scoped_ptr
<media::AudioInputStream
> input_stream_for_testing_
;
108 scoped_ptr
<media::AudioParameters
> params_for_testing_
;
110 DISALLOW_COPY_AND_ASSIGN(AudioRecorderImpl
);
113 } // namespace copresence
115 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_