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