Home | History | Annotate | Download | only in audio
      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.
      4 
      5 #ifndef MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_
      6 #define MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_
      7 
      8 #include <map>
      9 #include <set>
     10 
     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"
     19 
     20 namespace base {
     21 class SingleThreadTaskRunner;
     22 }
     23 
     24 namespace media {
     25 
     26 class LoopbackAudioConverter;
     27 class VirtualAudioOutputStream;
     28 
     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 {
     33  public:
     34   // Callback invoked just after VirtualAudioInputStream is closed.
     35   typedef base::Callback<void(VirtualAudioInputStream* vais)>
     36       AfterCloseCallback;
     37 
     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);
     46 
     47   virtual ~VirtualAudioInputStream();
     48 
     49   // AudioInputStream:
     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;
     59   virtual bool IsMuted() OVERRIDE;
     60 
     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);
     67 
     68   // Detaches a VirtualAudioOutputStream and removes it as input.
     69   virtual void RemoveOutputStream(VirtualAudioOutputStream* stream,
     70                                   const AudioParameters& output_params);
     71 
     72  private:
     73   friend class VirtualAudioInputStreamTest;
     74 
     75   typedef std::map<AudioParameters, LoopbackAudioConverter*> AudioConvertersMap;
     76 
     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.
     80   void PumpAudio(AudioBus* audio_bus);
     81 
     82   const scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_;
     83 
     84   AfterCloseCallback after_close_cb_;
     85 
     86   AudioInputCallback* callback_;
     87 
     88   // Non-const for testing.
     89   scoped_ptr<uint8[]> buffer_;
     90   AudioParameters params_;
     91 
     92   // Guards concurrent access to the converter network: converters_, mixer_, and
     93   // num_attached_output_streams_.
     94   base::Lock converter_network_lock_;
     95 
     96   // AudioConverters associated with the attached VirtualAudioOutputStreams,
     97   // partitioned by common AudioParameters.
     98   AudioConvertersMap converters_;
     99 
    100   // AudioConverter that takes all the audio converters and mixes them into one
    101   // final audio stream.
    102   AudioConverter mixer_;
    103 
    104   // Number of currently attached VirtualAudioOutputStreams.
    105   int num_attached_output_streams_;
    106 
    107   // Handles callback timing for consumption of audio data.
    108   FakeAudioConsumer fake_consumer_;
    109 
    110   base::ThreadChecker thread_checker_;
    111 
    112   DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStream);
    113 };
    114 
    115 }  // namespace media
    116 
    117 #endif  // MEDIA_AUDIO_VIRTUAL_AUDIO_INPUT_STREAM_H_
    118