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.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/synchronization/lock.h"
13 #include "base/threading/thread_checker.h"
14 #include "content/renderer/media/media_stream_track.h"
15 #include "content/renderer/media/tagged_list.h"
16 #include "content/renderer/media/webrtc_audio_device_impl.h"
17 #include "content/renderer/media/webrtc_local_audio_source_provider.h"
21 class MediaStreamAudioLevelCalculator;
22 class MediaStreamAudioProcessor;
23 class MediaStreamAudioSink;
24 class MediaStreamAudioSinkOwner;
25 class MediaStreamAudioTrackSink;
26 class PeerConnectionAudioSink;
27 class WebAudioCapturerSource;
28 class WebRtcAudioCapturer;
29 class WebRtcLocalAudioTrackAdapter;
31 // A WebRtcLocalAudioTrack instance contains the implementations of
32 // MediaStreamTrackExtraData.
33 // When an instance is created, it will register itself as a track to the
34 // WebRtcAudioCapturer to get the captured data, and forward the data to
35 // its |sinks_|. The data flow can be stopped by disabling the audio track.
36 class CONTENT_EXPORT WebRtcLocalAudioTrack
37 : NON_EXPORTED_BASE(public MediaStreamTrack) {
39 WebRtcLocalAudioTrack(WebRtcLocalAudioTrackAdapter* adapter,
40 const scoped_refptr<WebRtcAudioCapturer>& capturer,
41 WebAudioCapturerSource* webaudio_source);
43 virtual ~WebRtcLocalAudioTrack();
45 // Add a sink to the track. This function will trigger a OnSetFormat()
46 // call on the |sink|.
47 // Called on the main render thread.
48 void AddSink(MediaStreamAudioSink* sink);
50 // Remove a sink from the track.
51 // Called on the main render thread.
52 void RemoveSink(MediaStreamAudioSink* sink);
54 // Add/remove PeerConnection sink to/from the track.
55 // TODO(xians): Remove these two methods after PeerConnection can use the
56 // same sink interface as MediaStreamAudioSink.
57 void AddSink(PeerConnectionAudioSink* sink);
58 void RemoveSink(PeerConnectionAudioSink* sink);
60 // Starts the local audio track. Called on the main render thread and
61 // should be called only once when audio track is created.
64 // Stops the local audio track. Called on the main render thread and
65 // should be called only once when audio track going away.
68 // Method called by the capturer to deliver the capture data.
69 // Called on the capture audio thread.
70 void Capture(const int16* audio_data,
71 base::TimeDelta delay,
74 bool need_audio_processing);
76 // Method called by the capturer to set the audio parameters used by source
77 // of the capture data..
78 // Called on the capture audio thread.
79 void OnSetFormat(const media::AudioParameters& params);
81 // Method called by the capturer to set the processor that applies signal
82 // processing on the data of the track.
83 // Called on the capture audio thread.
84 void SetAudioProcessor(
85 const scoped_refptr<MediaStreamAudioProcessor>& processor);
87 blink::WebAudioSourceProvider* audio_source_provider() const {
88 return source_provider_.get();
92 typedef TaggedList<MediaStreamAudioTrackSink> SinkList;
94 // All usage of libjingle is through this adapter. The adapter holds
95 // a reference on this object, but not vice versa.
96 WebRtcLocalAudioTrackAdapter* adapter_;
98 // The provider of captured data to render.
99 scoped_refptr<WebRtcAudioCapturer> capturer_;
101 // The source of the audio track which is used by WebAudio, which provides
102 // data to the audio track when hooking up with WebAudio.
103 scoped_refptr<WebAudioCapturerSource> webaudio_source_;
105 // A tagged list of sinks that the audio data is fed to. Tags
106 // indicate tracks that need to be notified that the audio format
110 // Used to DCHECK that some methods are called on the main render thread.
111 base::ThreadChecker main_render_thread_checker_;
113 // Used to DCHECK that some methods are called on the capture audio thread.
114 base::ThreadChecker capture_thread_checker_;
116 // Protects |params_| and |sinks_|.
117 mutable base::Lock lock_;
119 // Audio parameters of the audio capture stream.
120 // Accessed on only the audio capture thread.
121 media::AudioParameters audio_parameters_;
123 // The source provider to feed the track data to other clients like
125 scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_;
127 // Used to calculate the signal level that shows in the UI.
128 // Accessed on only the audio thread.
129 scoped_ptr<MediaStreamAudioLevelCalculator> level_calculator_;
131 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioTrack);
134 } // namespace content
136 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_