summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/media/renderer_webaudiodevice_impl.h
blob: f6196ae82f284ba4fbf76a0a0870c54c1f504489 (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
117
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIODEVICE_IMPL_H_
#define CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIODEVICE_IMPL_H_

#include <stdint.h>

#include <memory>
#include <string>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/threading/thread_checker.h"
#include "content/common/content_export.h"
#include "media/base/audio_parameters.h"
#include "media/base/audio_renderer_sink.h"
#include "third_party/blink/public/platform/web_audio_device.h"
#include "third_party/blink/public/platform/web_audio_latency_hint.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace media {
class SilentSinkSuspender;
}

namespace content {
class CONTENT_EXPORT RendererWebAudioDeviceImpl
    : public blink::WebAudioDevice,
      public media::AudioRendererSink::RenderCallback {
 public:
  ~RendererWebAudioDeviceImpl() override;

  static std::unique_ptr<RendererWebAudioDeviceImpl> Create(
      media::ChannelLayout layout,
      int channels,
      const blink::WebAudioLatencyHint& latency_hint,
      blink::WebAudioDevice::RenderCallback* callback,
      int session_id);

  // blink::WebAudioDevice implementation.
  void Start() override;
  void Stop() override;
  double SampleRate() override;
  int FramesPerBuffer() override;

  // AudioRendererSink::RenderCallback implementation.
  int Render(base::TimeDelta delay,
             base::TimeTicks delay_timestamp,
             int prior_frames_skipped,
             media::AudioBus* dest) override;

  void OnRenderError() override;

  void SetMediaTaskRunnerForTesting(
      const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner);

  const media::AudioParameters& get_sink_params_for_testing() {
    return sink_params_;
  }

 protected:
  // Callback to get output device params (for tests).
  using OutputDeviceParamsCallback =
      base::Callback<media::AudioParameters(int frame_id,
                                            int session_id,
                                            const std::string& device_id)>;

  // Callback get render frame ID for current context (for tests).
  using RenderFrameIdCallback = base::Callback<int()>;

  RendererWebAudioDeviceImpl(media::ChannelLayout layout,
                             int channels,
                             const blink::WebAudioLatencyHint& latency_hint,
                             blink::WebAudioDevice::RenderCallback* callback,
                             int session_id,
                             const OutputDeviceParamsCallback& device_params_cb,
                             const RenderFrameIdCallback& render_frame_id_cb);

 private:
  const scoped_refptr<base::SingleThreadTaskRunner>& GetMediaTaskRunner();

  media::AudioParameters sink_params_;

  const blink::WebAudioLatencyHint latency_hint_;

  // Weak reference to the callback into WebKit code.
  blink::WebAudioDevice::RenderCallback* const client_callback_;

  // To avoid the need for locking, ensure the control methods of the
  // blink::WebAudioDevice implementation are called on the same thread.
  base::ThreadChecker thread_checker_;

  // When non-NULL, we are started.  When NULL, we are stopped.
  scoped_refptr<media::AudioRendererSink> sink_;

  // ID to allow browser to select the correct input device for unified IO.
  int session_id_;

  // Used to suspend |sink_| usage when silence has been detected for too long.
  std::unique_ptr<media::SilentSinkSuspender> webaudio_suspender_;

  // Render frame routing ID for the current context.
  int frame_id_;

  // Allow unit tests to set a custom MediaThreadTaskRunner.
  scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;

  DISALLOW_COPY_AND_ASSIGN(RendererWebAudioDeviceImpl);
};

}  // namespace content

#endif  // CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIODEVICE_IMPL_H_