1// Copyright 2014 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 CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_
6#define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_
7
8#include <vector>
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_vector.h"
12#include "base/synchronization/lock.h"
13#include "content/common/content_export.h"
14#include "third_party/libjingle/source/talk/app/webrtc/mediastreamtrack.h"
15#include "third_party/libjingle/source/talk/media/base/audiorenderer.h"
16
17namespace cricket {
18class AudioRenderer;
19}
20
21namespace webrtc {
22class AudioSourceInterface;
23class AudioProcessorInterface;
24}
25
26namespace content {
27
28class MediaStreamAudioProcessor;
29class WebRtcAudioSinkAdapter;
30class WebRtcLocalAudioTrack;
31
32class CONTENT_EXPORT WebRtcLocalAudioTrackAdapter
33    : NON_EXPORTED_BASE(public cricket::AudioRenderer),
34      NON_EXPORTED_BASE(
35          public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) {
36 public:
37  static scoped_refptr<WebRtcLocalAudioTrackAdapter> Create(
38      const std::string& label,
39      webrtc::AudioSourceInterface* track_source);
40
41  WebRtcLocalAudioTrackAdapter(
42      const std::string& label,
43      webrtc::AudioSourceInterface* track_source);
44
45  virtual ~WebRtcLocalAudioTrackAdapter();
46
47  void Initialize(WebRtcLocalAudioTrack* owner);
48
49  std::vector<int> VoeChannels() const;
50
51  // Called on the audio thread by the WebRtcLocalAudioTrack to set the signal
52  // level of the audio data.
53  void SetSignalLevel(int signal_level);
54
55  // Method called by the WebRtcLocalAudioTrack to set the processor that
56  // applies signal processing on the data of the track.
57  // This class will keep a reference of the |processor|.
58  // Called on the main render thread.
59  void SetAudioProcessor(
60      const scoped_refptr<MediaStreamAudioProcessor>& processor);
61
62 private:
63  // webrtc::MediaStreamTrack implementation.
64  virtual std::string kind() const OVERRIDE;
65
66  // webrtc::AudioTrackInterface implementation.
67  virtual void AddSink(webrtc::AudioTrackSinkInterface* sink) OVERRIDE;
68  virtual void RemoveSink(webrtc::AudioTrackSinkInterface* sink) OVERRIDE;
69  virtual bool GetSignalLevel(int* level) OVERRIDE;
70  virtual rtc::scoped_refptr<webrtc::AudioProcessorInterface>
71      GetAudioProcessor() OVERRIDE;
72
73  // cricket::AudioCapturer implementation.
74  virtual void AddChannel(int channel_id) OVERRIDE;
75  virtual void RemoveChannel(int channel_id) OVERRIDE;
76
77  // webrtc::AudioTrackInterface implementation.
78  virtual webrtc::AudioSourceInterface* GetSource() const OVERRIDE;
79  virtual cricket::AudioRenderer* GetRenderer() OVERRIDE;
80
81  // Weak reference.
82  WebRtcLocalAudioTrack* owner_;
83
84  // The source of the audio track which handles the audio constraints.
85  // TODO(xians): merge |track_source_| to |capturer_| in WebRtcLocalAudioTrack.
86  rtc::scoped_refptr<webrtc::AudioSourceInterface> track_source_;
87
88  // The audio processsor that applies audio processing on the data of audio
89  // track.
90  scoped_refptr<MediaStreamAudioProcessor> audio_processor_;
91
92  // A vector of WebRtc VoE channels that the capturer sends data to.
93  std::vector<int> voe_channels_;
94
95  // A vector of the peer connection sink adapters which receive the audio data
96  // from the audio track.
97  ScopedVector<WebRtcAudioSinkAdapter> sink_adapters_;
98
99  // The amplitude of the signal.
100  int signal_level_;
101
102  // Protects |voe_channels_|, |audio_processor_| and |signal_level_|.
103  mutable base::Lock lock_;
104};
105
106}  // namespace content
107
108#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_
109