summaryrefslogtreecommitdiff
path: root/chromium/media/audio/audio_output_resampler.h
blob: 96aab4a95a649c78fe88c7886b1058b4f9335814 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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.

#ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
#define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_

#include "base/containers/flat_map.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "media/audio/audio_debug_recording_helper.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_output_dispatcher.h"
#include "media/base/audio_parameters.h"

namespace media {

class AudioManager;
class AudioOutputDispatcherImpl;
class OnMoreDataConverter;

// AudioOutputResampler is a browser-side resampling and buffering solution
// which ensures audio data is always output at given parameters.  See the
// AudioConverter class for details on the conversion process.
//
// AOR works by intercepting the AudioSourceCallback provided to StartStream()
// and redirecting it through an AudioConverter instance.
//
// AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to
// AUDIO_PCM_LINEAR if the output device fails to open at the requested output
// parameters. If opening still fails, it will fallback to AUDIO_FAKE.
class MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher {
 public:
  // Callback type to register an AudioDebugRecorder.
  using RegisterDebugRecordingSourceCallback =
      base::RepeatingCallback<std::unique_ptr<AudioDebugRecorder>(
          const AudioParameters&)>;

  AudioOutputResampler(AudioManager* audio_manager,
                       const AudioParameters& input_params,
                       const AudioParameters& output_params,
                       const std::string& output_device_id,
                       base::TimeDelta close_delay,
                       const RegisterDebugRecordingSourceCallback&
                           register_debug_recording_source_callback);

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

  ~AudioOutputResampler() override;

  // AudioOutputDispatcher interface.
  AudioOutputProxy* CreateStreamProxy() override;
  bool OpenStream() override;
  bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
                   AudioOutputProxy* stream_proxy) override;
  void StopStream(AudioOutputProxy* stream_proxy) override;
  void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override;
  void CloseStream(AudioOutputProxy* stream_proxy) override;
  void FlushStream(AudioOutputProxy* stream_proxy) override;

 private:
  using CallbackMap =
      base::flat_map<AudioOutputProxy*, std::unique_ptr<OnMoreDataConverter>>;

  // Used to reinitialize |dispatcher_| upon timeout if there are no open
  // streams.
  void Reinitialize();

  // Used to initialize |dispatcher_|.
  std::unique_ptr<AudioOutputDispatcherImpl> MakeDispatcher(
      const std::string& output_device_id,
      const AudioParameters& params);

  // Stops the stream corresponding to the |item| in |callbacks_|.
  void StopStreamInternal(const CallbackMap::value_type& item);

  // Dispatcher to proxy all AudioOutputDispatcher calls too.
  // Lazily initialized on a first stream open request.
  std::unique_ptr<AudioOutputDispatcherImpl> dispatcher_;

  // Map of outstanding OnMoreDataConverter objects.  A new object is created
  // on every StartStream() call and destroyed on CloseStream().
  CallbackMap callbacks_;

  // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly.
  const base::TimeDelta close_delay_;

  // Source AudioParameters.
  const AudioParameters input_params_;

  // AudioParameters used to setup the output stream; changed upon fallback.
  AudioParameters output_params_;

  // The original AudioParameters we were constructed with.
  const AudioParameters original_output_params_;

  // Output device id.
  const std::string device_id_;

  // The reinitialization timer provides a way to recover from temporary failure
  // states by clearing the dispatcher if all proxies have been closed and none
  // have been created within |close_delay_|.  Without this, audio may be lost
  // to a fake stream indefinitely for transient errors.
  base::RetainingOneShotTimer reinitialize_timer_;

  // Callback for registering a debug recording source.
  RegisterDebugRecordingSourceCallback
      register_debug_recording_source_callback_;

  base::WeakPtrFactory<AudioOutputResampler> weak_factory_{this};
};

}  // namespace media

#endif  // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_