1 // Copyright (c) 2012 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 MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_
6 #define MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "media/audio/audio_io.h"
14 #include "media/audio/audio_parameters.h"
15 #include "media/audio/fake_audio_consumer.h"
16 #include "media/base/audio_converter.h"
19 class MessageLoopProxy
;
24 class LoopbackAudioConverter
;
25 class VirtualAudioOutputStream
;
27 // VirtualAudioInputStream converts and mixes audio from attached
28 // VirtualAudioOutputStreams into a single stream. It will continuously render
29 // audio until this VirtualAudioInputStream is stopped and closed.
30 class MEDIA_EXPORT VirtualAudioInputStream
: public AudioInputStream
{
32 // Callback invoked just after VirtualAudioInputStream is closed.
33 typedef base::Callback
<void(VirtualAudioInputStream
* vais
)>
36 // Construct a target for audio loopback which mixes multiple data streams
37 // into a single stream having the given |params|.
38 VirtualAudioInputStream(
39 const AudioParameters
& params
,
40 const scoped_refptr
<base::MessageLoopProxy
>& message_loop
,
41 const AfterCloseCallback
& after_close_cb
);
43 virtual ~VirtualAudioInputStream();
46 virtual bool Open() OVERRIDE
;
47 virtual void Start(AudioInputCallback
* callback
) OVERRIDE
;
48 virtual void Stop() OVERRIDE
;
49 virtual void Close() OVERRIDE
;
50 virtual double GetMaxVolume() OVERRIDE
;
51 virtual void SetVolume(double volume
) OVERRIDE
;
52 virtual double GetVolume() OVERRIDE
;
53 virtual void SetAutomaticGainControl(bool enabled
) OVERRIDE
;
54 virtual bool GetAutomaticGainControl() OVERRIDE
;
56 // Attaches a VirtualAudioOutputStream to be used as input. This
57 // VirtualAudioInputStream must outlive all attached streams, so any attached
58 // stream must be closed (which causes a detach) before
59 // VirtualAudioInputStream is destroyed.
60 virtual void AddOutputStream(VirtualAudioOutputStream
* stream
,
61 const AudioParameters
& output_params
);
63 // Detaches a VirtualAudioOutputStream and removes it as input.
64 virtual void RemoveOutputStream(VirtualAudioOutputStream
* stream
,
65 const AudioParameters
& output_params
);
68 friend class VirtualAudioInputStreamTest
;
70 typedef std::map
<AudioParameters
, LoopbackAudioConverter
*> AudioConvertersMap
;
72 // When Start() is called on this class, we continuously schedule this
73 // callback to render audio using any attached VirtualAudioOutputStreams until
75 void ReadAudio(AudioBus
* audio_bus
);
77 const scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
79 AfterCloseCallback after_close_cb_
;
81 AudioInputCallback
* callback_
;
83 // Non-const for testing.
84 scoped_ptr
<uint8
[]> buffer_
;
85 AudioParameters params_
;
87 // AudioConverters associated with the attached VirtualAudioOutputStreams,
88 // partitioned by common AudioParameters.
89 AudioConvertersMap converters_
;
91 // AudioConverter that takes all the audio converters and mixes them into one
92 // final audio stream.
93 AudioConverter mixer_
;
95 // Number of currently attached VirtualAudioOutputStreams.
96 int num_attached_output_streams_
;
98 // Handles callback timing for consumption of audio data.
99 FakeAudioConsumer fake_consumer_
;
101 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStream
);
106 #endif // MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_