Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / copresence / mediums / audio / audio_recorder_impl.h
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   typedef base::Callback<void(const std::string&)> RecordedSamplesCallback;
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   bool IsRecording() override;
44
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);
49   }
50
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);
54   }
55
56  protected:
57   ~AudioRecorderImpl() override;
58   void set_is_recording(bool is_recording) { is_recording_ = is_recording; }
59
60  private:
61   friend class AudioRecorderTest;
62   FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, BasicRecordAndStop);
63   FRIEND_TEST_ALL_PREFIXES(AudioRecorderTest, OutOfOrderRecordAndStopMultiple);
64
65   // Methods to do our various operations; all of these need to be run on the
66   // audio thread.
67   void InitializeOnAudioThread();
68   void RecordOnAudioThread();
69   void StopOnAudioThread();
70   void StopAndCloseOnAudioThread();
71   void FinalizeOnAudioThread();
72
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;
82
83   // AudioConverter::InputCallback overrides:
84   double ProvideInput(media::AudioBus* dest,
85                       base::TimeDelta buffer_delay) override;
86
87   // Flushes the audio loop, making sure that any queued operations are
88   // performed.
89   void FlushAudioLoopForTesting();
90
91   bool is_recording_;
92
93   media::AudioInputStream* stream_;
94
95   RecordedSamplesCallback decode_callback_;
96
97   // ProvideInput will use this buffer as its source.
98   const media::AudioBus* temp_conversion_buffer_;
99
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_;
105
106   scoped_ptr<media::AudioConverter> converter_;
107
108   scoped_ptr<media::AudioInputStream> input_stream_for_testing_;
109   scoped_ptr<media::AudioParameters> params_for_testing_;
110
111   DISALLOW_COPY_AND_ASSIGN(AudioRecorderImpl);
112 };
113
114 }  // namespace copresence
115
116 #endif  // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_RECORDER_IMPL_H_