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_worker.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 ~VirtualAudioInputStream() override
;
51 void Start(AudioInputCallback
* callback
) override
;
53 void Close() override
;
54 double GetMaxVolume() override
;
55 void SetVolume(double volume
) override
;
56 double GetVolume() override
;
57 bool SetAutomaticGainControl(bool enabled
) override
;
58 bool GetAutomaticGainControl() override
;
59 bool IsMuted() override
;
61 // Attaches a VirtualAudioOutputStream to be used as input. This
62 // VirtualAudioInputStream must outlive all attached streams, so any attached
63 // stream must be closed (which causes a detach) before
64 // VirtualAudioInputStream is destroyed.
65 virtual void AddOutputStream(VirtualAudioOutputStream
* stream
,
66 const AudioParameters
& output_params
);
68 // Detaches a VirtualAudioOutputStream and removes it as input.
69 virtual void RemoveOutputStream(VirtualAudioOutputStream
* stream
,
70 const AudioParameters
& output_params
);
73 friend class VirtualAudioInputStreamTest
;
75 typedef std::map
<AudioParameters
, LoopbackAudioConverter
*> AudioConvertersMap
;
77 // Pulls audio data from all attached VirtualAudioOutputStreams, mixes and
78 // converts the streams into one, and pushes the result to |callback_|.
79 // Invoked on the worker thread.
82 const scoped_refptr
<base::SingleThreadTaskRunner
> worker_task_runner_
;
84 AfterCloseCallback after_close_cb_
;
86 AudioInputCallback
* callback_
;
88 // Non-const for testing.
89 scoped_ptr
<uint8
[]> buffer_
;
90 AudioParameters params_
;
92 // Guards concurrent access to the converter network: converters_, mixer_, and
93 // num_attached_output_streams_.
94 base::Lock converter_network_lock_
;
96 // AudioConverters associated with the attached VirtualAudioOutputStreams,
97 // partitioned by common AudioParameters.
98 AudioConvertersMap converters_
;
100 // AudioConverter that takes all the audio converters and mixes them into one
101 // final audio stream.
102 AudioConverter mixer_
;
104 // Number of currently attached VirtualAudioOutputStreams.
105 int num_attached_output_streams_
;
107 // Handles callback timing for consumption of audio data.
108 FakeAudioWorker fake_worker_
;
110 scoped_ptr
<AudioBus
> audio_bus_
;
112 base::ThreadChecker thread_checker_
;
114 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStream
);
119 #endif // MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_