• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_LOCAL_AUDIO_TRACK_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_
7 
8 #include <list>
9 #include <string>
10 
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_checker.h"
13 #include "content/renderer/media/media_stream_audio_track_sink.h"
14 #include "content/renderer/media/tagged_list.h"
15 #include "content/renderer/media/webrtc_audio_device_impl.h"
16 #include "content/renderer/media/webrtc_local_audio_source_provider.h"
17 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
18 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
19 #include "third_party/libjingle/source/talk/app/webrtc/mediastreamtrack.h"
20 #include "third_party/libjingle/source/talk/media/base/audiorenderer.h"
21 
22 namespace cricket {
23 class AudioRenderer;
24 }  // namespace cricket
25 
26 namespace media {
27 class AudioBus;
28 }  // namespace media
29 
30 namespace content {
31 
32 class MediaStreamAudioSink;
33 class MediaStreamAudioSinkOwner;
34 class PeerConnectionAudioSink;
35 class WebAudioCapturerSource;
36 class WebRtcAudioCapturer;
37 
38 // A WebRtcLocalAudioTrack instance contains the implementations of
39 // MediaStreamTrack and MediaStreamAudioSink.
40 // When an instance is created, it will register itself as a track to the
41 // WebRtcAudioCapturer to get the captured data, and forward the data to
42 // its |sinks_|. The data flow can be stopped by disabling the audio track.
43 class CONTENT_EXPORT WebRtcLocalAudioTrack
NON_EXPORTED_BASE(public cricket::AudioRenderer)44     : NON_EXPORTED_BASE(public cricket::AudioRenderer),
45       NON_EXPORTED_BASE(
46           public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) {
47  public:
48   static scoped_refptr<WebRtcLocalAudioTrack> Create(
49       const std::string& id,
50       const scoped_refptr<WebRtcAudioCapturer>& capturer,
51       WebAudioCapturerSource* webaudio_source,
52       webrtc::AudioSourceInterface* track_source,
53       const webrtc::MediaConstraintsInterface* constraints);
54 
55   // Add a sink to the track. This function will trigger a OnSetFormat()
56   // call on the |sink|.
57   // Called on the main render thread.
58   void AddSink(MediaStreamAudioSink* sink);
59 
60   // Remove a sink from the track.
61   // Called on the main render thread.
62   void RemoveSink(MediaStreamAudioSink* sink);
63 
64   // Add/remove PeerConnection sink to/from the track.
65   // TODO(xians): Remove these two methods after PeerConnection can use the
66   // same sink interface as MediaStreamAudioSink.
67   void AddSink(PeerConnectionAudioSink* sink);
68   void RemoveSink(PeerConnectionAudioSink* sink);
69 
70   // Starts the local audio track. Called on the main render thread and
71   // should be called only once when audio track is created.
72   void Start();
73 
74   // Stops the local audio track. Called on the main render thread and
75   // should be called only once when audio track going away.
76   void Stop();
77 
78   // Method called by the capturer to deliver the capture data.
79   // Call on the capture audio thread.
80   void Capture(media::AudioBus* audio_source,
81                int audio_delay_milliseconds,
82                int volume,
83                bool key_pressed);
84 
85   // Method called by the capturer to set the audio parameters used by source
86   // of the capture data..
87   // Call on the capture audio thread.
88   void OnSetFormat(const media::AudioParameters& params);
89 
90   blink::WebAudioSourceProvider* audio_source_provider() const {
91     return source_provider_.get();
92   }
93 
94  protected:
95   WebRtcLocalAudioTrack(
96       const std::string& label,
97       const scoped_refptr<WebRtcAudioCapturer>& capturer,
98       WebAudioCapturerSource* webaudio_source,
99       webrtc::AudioSourceInterface* track_source,
100       const webrtc::MediaConstraintsInterface* constraints);
101 
102   virtual ~WebRtcLocalAudioTrack();
103 
104  private:
105   typedef TaggedList<MediaStreamAudioTrackSink> SinkList;
106 
107   // cricket::AudioCapturer implementation.
108   virtual void AddChannel(int channel_id) OVERRIDE;
109   virtual void RemoveChannel(int channel_id) OVERRIDE;
110 
111   // webrtc::AudioTrackInterface implementation.
112   virtual webrtc::AudioSourceInterface* GetSource() const OVERRIDE;
113   virtual cricket::AudioRenderer* GetRenderer() OVERRIDE;
114 
115   // webrtc::MediaStreamTrack implementation.
116   virtual std::string kind() const OVERRIDE;
117 
118   // The provider of captured data to render.
119   // The WebRtcAudioCapturer is today created by WebRtcAudioDeviceImpl.
120   scoped_refptr<WebRtcAudioCapturer> capturer_;
121 
122   // The source of the audio track which is used by WebAudio, which provides
123   // data to the audio track when hooking up with WebAudio.
124   scoped_refptr<WebAudioCapturerSource> webaudio_source_;
125 
126   // The source of the audio track which handles the audio constraints.
127   // TODO(xians): merge |track_source_| to |capturer_|.
128   talk_base::scoped_refptr<webrtc::AudioSourceInterface> track_source_;
129 
130   // A tagged list of sinks that the audio data is fed to. Tags
131   // indicate tracks that need to be notified that the audio format
132   // has changed.
133   SinkList sinks_;
134 
135   // Used to DCHECK that some methods are called on the main render thread.
136   base::ThreadChecker main_render_thread_checker_;
137 
138   // Used to DCHECK that some methods are called on the capture audio thread.
139   base::ThreadChecker capture_thread_checker_;
140 
141   // Protects |params_| and |sinks_|.
142   mutable base::Lock lock_;
143 
144   // A vector of WebRtc VoE channels that the capturer sends data to.
145   std::vector<int> voe_channels_;
146 
147   bool need_audio_processing_;
148 
149   // Buffers used for temporary storage during capture callbacks.
150   // Allocated and accessed only on the capture audio thread.
151   class ConfiguredBuffer;
152   scoped_ptr<ConfiguredBuffer> buffer_;
153 
154   // The source provider to feed the track data to other clients like
155   // WebAudio.
156   scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_;
157 
158   DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioTrack);
159 };
160 
161 }  // namespace content
162 
163 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_
164