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 "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h"
15 #include "media/audio/audio_io.h"
16 #include "media/audio/audio_parameters.h"
17 #include "media/audio/fake_audio_consumer.h"
18 #include "media/base/audio_converter.h"
21 class SingleThreadTaskRunner
;
26 class LoopbackAudioConverter
;
27 class VirtualAudioOutputStream
;
29 // VirtualAudioInputStream converts and mixes audio from attached
30 // VirtualAudioOutputStreams into a single stream. It will continuously render
31 // audio until this VirtualAudioInputStream is stopped and closed.
32 class MEDIA_EXPORT VirtualAudioInputStream
: public AudioInputStream
{
34 // Callback invoked just after VirtualAudioInputStream is closed.
35 typedef base::Callback
<void(VirtualAudioInputStream
* vais
)>
38 // Construct a target for audio loopback which mixes multiple data streams
39 // into a single stream having the given |params|. |worker_task_runner| is
40 // the task runner on which AudioInputCallback methods are called and may or
41 // may not be the single thread that invokes the AudioInputStream methods.
42 VirtualAudioInputStream(
43 const AudioParameters
& params
,
44 const scoped_refptr
<base::SingleThreadTaskRunner
>& worker_task_runner
,
45 const AfterCloseCallback
& after_close_cb
);
47 virtual ~VirtualAudioInputStream();
50 virtual bool Open() OVERRIDE
;
51 virtual void Start(AudioInputCallback
* callback
) OVERRIDE
;
52 virtual void Stop() OVERRIDE
;
53 virtual void Close() OVERRIDE
;
54 virtual double GetMaxVolume() OVERRIDE
;
55 virtual void SetVolume(double volume
) OVERRIDE
;
56 virtual double GetVolume() OVERRIDE
;
57 virtual void SetAutomaticGainControl(bool enabled
) OVERRIDE
;
58 virtual bool GetAutomaticGainControl() OVERRIDE
;
60 // Attaches a VirtualAudioOutputStream to be used as input. This
61 // VirtualAudioInputStream must outlive all attached streams, so any attached
62 // stream must be closed (which causes a detach) before
63 // VirtualAudioInputStream is destroyed.
64 virtual void AddOutputStream(VirtualAudioOutputStream
* stream
,
65 const AudioParameters
& output_params
);
67 // Detaches a VirtualAudioOutputStream and removes it as input.
68 virtual void RemoveOutputStream(VirtualAudioOutputStream
* stream
,
69 const AudioParameters
& output_params
);
72 friend class VirtualAudioInputStreamTest
;
74 typedef std::map
<AudioParameters
, LoopbackAudioConverter
*> AudioConvertersMap
;
76 // Pulls audio data from all attached VirtualAudioOutputStreams, mixes and
77 // converts the streams into one, and pushes the result to |callback_|.
78 // Invoked on the worker thread.
79 void PumpAudio(AudioBus
* audio_bus
);
81 const scoped_refptr
<base::SingleThreadTaskRunner
> worker_task_runner_
;
83 AfterCloseCallback after_close_cb_
;
85 AudioInputCallback
* callback_
;
87 // Non-const for testing.
88 scoped_ptr
<uint8
[]> buffer_
;
89 AudioParameters params_
;
91 // Guards concurrent access to the converter network: converters_, mixer_, and
92 // num_attached_output_streams_.
93 base::Lock converter_network_lock_
;
95 // AudioConverters associated with the attached VirtualAudioOutputStreams,
96 // partitioned by common AudioParameters.
97 AudioConvertersMap converters_
;
99 // AudioConverter that takes all the audio converters and mixes them into one
100 // final audio stream.
101 AudioConverter mixer_
;
103 // Number of currently attached VirtualAudioOutputStreams.
104 int num_attached_output_streams_
;
106 // Handles callback timing for consumption of audio data.
107 FakeAudioConsumer fake_consumer_
;
109 base::ThreadChecker thread_checker_
;
111 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStream
);
116 #endif // MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_