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 // Initializes the object. Do not use this object before calling this method. |
| 34 void Initialize(); |
| 35 |
| 36 void Record(); |
| 37 void Stop(); |
| 38 |
| 39 // Cleans up and deletes this object. Do not use object after this call. |
| 40 void Finalize(); |
| 41 |
| 42 private: |
| 43 friend class AudioRecorderTest; |
| 44 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, BasicRecordAndStop); |
| 45 FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, OutOfOrderRecordAndStopMultiple); |
| 46 |
| 47 virtual ~AudioRecorder(); |
| 48 |
| 49 // Methods to do our various operations; all of these need to be run on the |
| 50 // audio thread. |
| 51 void InitializeOnAudioThread(); |
| 52 void RecordOnAudioThread(); |
| 53 void StopOnAudioThread(); |
| 54 void StopAndCloseOnAudioThread(); |
| 55 void FinalizeOnAudioThread(); |
| 56 |
| 57 // AudioInputStream::AudioInputCallback overrides: |
| 58 // Called by the audio recorder when a full packet of audio data is |
| 59 // available. This is called from a special audio thread and the |
| 60 // implementation should return as soon as possible. |
| 61 virtual void OnData(media::AudioInputStream* stream, |
| 62 const media::AudioBus* source, |
| 63 uint32 hardware_delay_bytes, |
| 64 double volume) OVERRIDE; |
| 65 virtual void OnError(media::AudioInputStream* stream) OVERRIDE; |
| 66 |
| 67 // AudioConverter::InputCallback overrides: |
| 68 virtual double ProvideInput(media::AudioBus* dest, |
| 69 base::TimeDelta buffer_delay) OVERRIDE; |
| 70 |
| 71 // Flushes the audio loop, making sure that any queued operations are |
| 72 // performed. |
| 73 void FlushAudioLoopForTesting(); |
| 74 |
| 75 // Takes ownership of the stream. |
| 76 void set_input_stream_for_testing( |
| 77 media::AudioInputStream* input_stream_for_testing) { |
| 78 input_stream_for_testing_ = input_stream_for_testing; |
| 79 } |
| 80 |
| 81 // Takes ownership of the params. |
| 82 void set_params_for_testing(media::AudioParameters* params_for_testing) { |
| 83 params_for_testing_.reset(params_for_testing); |
| 84 } |
| 85 |
| 86 media::AudioInputStream* stream_; |
| 87 bool is_recording_; |
| 88 DecodeSamplesCallback decode_callback_; |
| 89 |
| 90 // ProvideInput will use this buffer as its source. |
| 91 const media::AudioBus* temp_conversion_buffer_; |
| 92 |
| 93 // Outside of the ctor/Initialize method, only access the next variables on |
| 94 // the recording thread. |
| 95 scoped_ptr<media::AudioBus> buffer_; |
| 96 int total_buffer_frames_; |
| 97 int buffer_frame_index_; |
| 98 |
| 99 scoped_ptr<media::AudioConverter> converter_; |
| 100 |
| 101 media::AudioInputStream* input_stream_for_testing_; |
| 102 scoped_ptr<media::AudioParameters> params_for_testing_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(AudioRecorder); |
| 105 }; |
| 106 |
| 107 } // namespace copresence |
| 108 |
| 109 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_ |
OLD | NEW |