1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93

media / audio / fake_audio_input_stream.h [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// A fake implementation of AudioInputStream, useful for testing purpose.

#ifndef MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_
#define MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_

#include <memory>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "media/audio/audio_io.h"
#include "media/base/audio_parameters.h"
#include "media/base/fake_audio_worker.h"

namespace media {

class AudioBus;
class AudioManagerBase;

// This class acts as a fake audio input stream. The default is to generate a
// beeping sound unless --use-file-for-fake-audio-capture=<file> is specified,
// in which case the indicated .wav file will be read and played into the
// stream.
class MEDIA_EXPORT FakeAudioInputStream : public AudioInputStream {
 public:
  static AudioInputStream* MakeFakeStream(AudioManagerBase* manager,
                                          const AudioParameters& params);

  FakeAudioInputStream(const FakeAudioInputStream&) = delete;
  FakeAudioInputStream& operator=(const FakeAudioInputStream&) = delete;

  OpenOutcome Open() override;
  void Start(AudioInputCallback* callback) override;
  void Stop() override;
  void Close() override;
  double GetMaxVolume() override;
  void SetVolume(double volume) override;
  double GetVolume() override;
  bool IsMuted() override;
  bool SetAutomaticGainControl(bool enabled) override;
  bool GetAutomaticGainControl() override;
  void SetOutputDeviceForAec(const std::string& output_device_id) override;

  // Generate one beep sound. This method is called by FakeVideoCaptureDevice to
  // test audio/video synchronization. This is a static method because
  // FakeVideoCaptureDevice is disconnected from an audio device. This means
  // only one instance of this class gets to respond, which is okay because we
  // assume there's only one stream for this testing purpose. Furthermore this
  // method will do nothing if --use-file-for-fake-audio-capture is specified
  // since the input stream will be playing from a file instead of beeping.
  // TODO(hclam): Make this non-static. To do this we'll need to fix
  // crbug.com/159053 such that video capture device is aware of audio
  // input stream.
  static void BeepOnce();

  // Set the muted state for _all_ FakeAudioInputStreams. The value is global,
  // so it can be set before any FakeAudioInputStreams have been created.
  static void SetGlobalMutedState(bool is_muted);

 private:
  FakeAudioInputStream(AudioManagerBase* manager,
                       const AudioParameters& params);
  ~FakeAudioInputStream() override;

  std::unique_ptr<AudioOutputStream::AudioSourceCallback> ChooseSource();
  void ReadAudioFromSource(base::TimeTicks ideal_time, base::TimeTicks now);

  raw_ptr<AudioManagerBase> audio_manager_;
  // |callback_| needs the lock as ReadAudioFromSource reads callback_
  // on the capture thread, while callback_ is set on the audio thread.
  base::Lock callback_lock_;
  raw_ptr<AudioInputCallback> callback_ GUARDED_BY(callback_lock_);
  AudioParameters params_;

  std::unique_ptr<FakeAudioWorker> fake_audio_worker_;
  std::unique_ptr<AudioOutputStream::AudioSourceCallback> audio_source_;
  std::unique_ptr<media::AudioBus> audio_bus_;
  // We will delete the capture thread on the AudioManager worker task runner
  // since the audio thread is the main UI thread on Mac.
  std::unique_ptr<base::Thread, base::OnTaskRunnerDeleter> capture_thread_;
};

}  // namespace media

#endif  // MEDIA_AUDIO_FAKE_AUDIO_INPUT_STREAM_H_