| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_ | 5 #ifndef MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_ |
| 6 #define MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_ | 6 #define MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/thread_checker.h" |
| 13 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
| 14 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
| 15 #include "media/audio/fake_audio_consumer.h" | 17 #include "media/audio/fake_audio_consumer.h" |
| 16 #include "media/base/audio_converter.h" | 18 #include "media/base/audio_converter.h" |
| 17 | 19 |
| 18 namespace base { | 20 namespace base { |
| 19 class MessageLoopProxy; | 21 class MessageLoopProxy; |
| 20 } | 22 } |
| 21 | 23 |
| 22 namespace media { | 24 namespace media { |
| 23 | 25 |
| 24 class LoopbackAudioConverter; | 26 class LoopbackAudioConverter; |
| 25 class VirtualAudioOutputStream; | 27 class VirtualAudioOutputStream; |
| 26 | 28 |
| 27 // VirtualAudioInputStream converts and mixes audio from attached | 29 // VirtualAudioInputStream converts and mixes audio from attached |
| 28 // VirtualAudioOutputStreams into a single stream. It will continuously render | 30 // VirtualAudioOutputStreams into a single stream. It will continuously render |
| 29 // audio until this VirtualAudioInputStream is stopped and closed. | 31 // audio until this VirtualAudioInputStream is stopped and closed. |
| 30 class MEDIA_EXPORT VirtualAudioInputStream : public AudioInputStream { | 32 class MEDIA_EXPORT VirtualAudioInputStream : public AudioInputStream { |
| 31 public: | 33 public: |
| 32 // Callback invoked just after VirtualAudioInputStream is closed. | 34 // Callback invoked just after VirtualAudioInputStream is closed. |
| 33 typedef base::Callback<void(VirtualAudioInputStream* vais)> | 35 typedef base::Callback<void(VirtualAudioInputStream* vais)> |
| 34 AfterCloseCallback; | 36 AfterCloseCallback; |
| 35 | 37 |
| 36 // Construct a target for audio loopback which mixes multiple data streams | 38 // Construct a target for audio loopback which mixes multiple data streams |
| 37 // into a single stream having the given |params|. | 39 // into a single stream having the given |params|. |worker_loop| is the loop |
| 40 // on which AudioInputCallback methods are called and may or may not be the |
| 41 // single thread that invokes the AudioInputStream methods. |
| 38 VirtualAudioInputStream( | 42 VirtualAudioInputStream( |
| 39 const AudioParameters& params, | 43 const AudioParameters& params, |
| 40 const scoped_refptr<base::MessageLoopProxy>& message_loop, | 44 const scoped_refptr<base::MessageLoopProxy>& worker_loop, |
| 41 const AfterCloseCallback& after_close_cb); | 45 const AfterCloseCallback& after_close_cb); |
| 42 | 46 |
| 43 virtual ~VirtualAudioInputStream(); | 47 virtual ~VirtualAudioInputStream(); |
| 44 | 48 |
| 45 // AudioInputStream: | 49 // AudioInputStream: |
| 46 virtual bool Open() OVERRIDE; | 50 virtual bool Open() OVERRIDE; |
| 47 virtual void Start(AudioInputCallback* callback) OVERRIDE; | 51 virtual void Start(AudioInputCallback* callback) OVERRIDE; |
| 48 virtual void Stop() OVERRIDE; | 52 virtual void Stop() OVERRIDE; |
| 49 virtual void Close() OVERRIDE; | 53 virtual void Close() OVERRIDE; |
| 50 virtual double GetMaxVolume() OVERRIDE; | 54 virtual double GetMaxVolume() OVERRIDE; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 62 | 66 |
| 63 // Detaches a VirtualAudioOutputStream and removes it as input. | 67 // Detaches a VirtualAudioOutputStream and removes it as input. |
| 64 virtual void RemoveOutputStream(VirtualAudioOutputStream* stream, | 68 virtual void RemoveOutputStream(VirtualAudioOutputStream* stream, |
| 65 const AudioParameters& output_params); | 69 const AudioParameters& output_params); |
| 66 | 70 |
| 67 private: | 71 private: |
| 68 friend class VirtualAudioInputStreamTest; | 72 friend class VirtualAudioInputStreamTest; |
| 69 | 73 |
| 70 typedef std::map<AudioParameters, LoopbackAudioConverter*> AudioConvertersMap; | 74 typedef std::map<AudioParameters, LoopbackAudioConverter*> AudioConvertersMap; |
| 71 | 75 |
| 72 // When Start() is called on this class, we continuously schedule this | 76 // Pulls audio data from all attached VirtualAudioOutputStreams, mixes and |
| 73 // callback to render audio using any attached VirtualAudioOutputStreams until | 77 // converts the streams into one, and pushes the result to |callback_|. |
| 74 // Stop() is called. | 78 // Invoked on the worker thread. |
| 75 void ReadAudio(AudioBus* audio_bus); | 79 void PumpAudio(AudioBus* audio_bus); |
| 76 | 80 |
| 77 const scoped_refptr<base::MessageLoopProxy> message_loop_; | 81 const scoped_refptr<base::MessageLoopProxy> worker_loop_; |
| 78 | 82 |
| 79 AfterCloseCallback after_close_cb_; | 83 AfterCloseCallback after_close_cb_; |
| 80 | 84 |
| 81 AudioInputCallback* callback_; | 85 AudioInputCallback* callback_; |
| 82 | 86 |
| 83 // Non-const for testing. | 87 // Non-const for testing. |
| 84 scoped_ptr<uint8[]> buffer_; | 88 scoped_ptr<uint8[]> buffer_; |
| 85 AudioParameters params_; | 89 AudioParameters params_; |
| 86 | 90 |
| 91 // Guards concurrent access to the converter network: converters_, mixer_, and |
| 92 // num_attached_output_streams_. |
| 93 base::Lock converter_network_lock_; |
| 94 |
| 87 // AudioConverters associated with the attached VirtualAudioOutputStreams, | 95 // AudioConverters associated with the attached VirtualAudioOutputStreams, |
| 88 // partitioned by common AudioParameters. | 96 // partitioned by common AudioParameters. |
| 89 AudioConvertersMap converters_; | 97 AudioConvertersMap converters_; |
| 90 | 98 |
| 91 // AudioConverter that takes all the audio converters and mixes them into one | 99 // AudioConverter that takes all the audio converters and mixes them into one |
| 92 // final audio stream. | 100 // final audio stream. |
| 93 AudioConverter mixer_; | 101 AudioConverter mixer_; |
| 94 | 102 |
| 95 // Number of currently attached VirtualAudioOutputStreams. | 103 // Number of currently attached VirtualAudioOutputStreams. |
| 96 int num_attached_output_streams_; | 104 int num_attached_output_streams_; |
| 97 | 105 |
| 98 // Handles callback timing for consumption of audio data. | 106 // Handles callback timing for consumption of audio data. |
| 99 FakeAudioConsumer fake_consumer_; | 107 FakeAudioConsumer fake_consumer_; |
| 100 | 108 |
| 109 base::ThreadChecker thread_checker_; |
| 110 |
| 101 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStream); | 111 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStream); |
| 102 }; | 112 }; |
| 103 | 113 |
| 104 } // namespace media | 114 } // namespace media |
| 105 | 115 |
| 106 #endif // MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_ | 116 #endif // MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_ |
| OLD | NEW |