summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/render_audiosourceprovider.h
blob: 6d842cb50e1e3fc65fcfdde255aaae8418b3e50f (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
// 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.
//
// RenderAudioSourceProvider provides a bridge between classes:
//     WebKit::WebAudioSourceProvider <---> media::AudioRendererSink
//
// RenderAudioSourceProvider is a "sink" of audio, and uses a default
// AudioDevice if a client has not explicitly been set.
//
// WebKit optionally sets a client, and then periodically calls provideInput()
// to render a certain number of audio sample-frames.  provideInput()
// uses the renderer to get this data, and then massages it into the form
// required by provideInput().  In this case, the default AudioDevice
// is no longer used.
//
// THREAD SAFETY:
// It is assumed that the callers to setClient() and provideInput()
// implement appropriate locking for thread safety when making
// these calls.  This happens in WebKit.

#ifndef CONTENT_RENDERER_MEDIA_RENDER_AUDIOSOURCEPROVIDER_H_
#define CONTENT_RENDERER_MEDIA_RENDER_AUDIOSOURCEPROVIDER_H_

#include <vector>

#include "content/renderer/media/audio_device.h"
#include "media/base/audio_renderer_sink.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebAudioSourceProvider.h"

namespace WebKit {
class WebAudioSourceProviderClient;
}

class RenderAudioSourceProvider
    : public WebKit::WebAudioSourceProvider,
      public media::AudioRendererSink {
 public:
  RenderAudioSourceProvider();
  virtual ~RenderAudioSourceProvider();

  // WebKit::WebAudioSourceProvider implementation.

  // WebKit calls setClient() if it desires to take control of the rendered
  // audio stream.  We call client's setFormat() when the audio stream format
  // is known.
  virtual void setClient(WebKit::WebAudioSourceProviderClient* client);

  // If setClient() has been called, then WebKit calls provideInput()
  // periodically to get the rendered audio stream.
  virtual void provideInput(const WebKit::WebVector<float*>& audio_data,
                            size_t number_of_frames);

  // AudioRendererSink implementation.
  virtual void Start() OVERRIDE;
  virtual void Stop() OVERRIDE;
  virtual void Play() OVERRIDE;
  virtual void Pause(bool flush) OVERRIDE;
  virtual bool SetVolume(double volume) OVERRIDE;
  virtual void GetVolume(double* volume) OVERRIDE;
  virtual void Initialize(
      const media::AudioParameters& params, RenderCallback* renderer) OVERRIDE;

 private:
  // Set to true when Initialize() is called.
  bool is_initialized_;
  int channels_;
  int sample_rate_;

  bool is_running_;
  double volume_;
  media::AudioRendererSink::RenderCallback* renderer_;
  WebKit::WebAudioSourceProviderClient* client_;

  // Protects access to sink_
  base::Lock sink_lock_;

  // default_sink_ is the default sink.
  scoped_refptr<media::AudioRendererSink> default_sink_;

  DISALLOW_COPY_AND_ASSIGN(RenderAudioSourceProvider);
};

#endif  // CONTENT_RENDERER_MEDIA_RENDER_AUDIOSOURCEPROVIDER_H_