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 "media/audio/audio_parameters.h"
24 class MediaStreamAudioLevelCalculator
;
25 class MediaStreamAudioProcessor
;
26 class MediaStreamAudioSink
;
27 class MediaStreamAudioSinkOwner
;
28 class MediaStreamAudioTrackSink
;
29 class WebAudioCapturerSource
;
30 class WebRtcAudioCapturer
;
31 class WebRtcLocalAudioTrackAdapter
;
33 // A WebRtcLocalAudioTrack instance contains the implementations of
34 // MediaStreamTrackExtraData.
35 // When an instance is created, it will register itself as a track to the
36 // WebRtcAudioCapturer to get the captured data, and forward the data to
37 // its |sinks_|. The data flow can be stopped by disabling the audio track.
38 class CONTENT_EXPORT WebRtcLocalAudioTrack
39 : NON_EXPORTED_BASE(public MediaStreamTrack
) {
41 WebRtcLocalAudioTrack(WebRtcLocalAudioTrackAdapter
* adapter
,
42 const scoped_refptr
<WebRtcAudioCapturer
>& capturer
,
43 WebAudioCapturerSource
* webaudio_source
);
45 virtual ~WebRtcLocalAudioTrack();
47 // Add a sink to the track. This function will trigger a OnSetFormat()
48 // call on the |sink|.
49 // Called on the main render thread.
50 void AddSink(MediaStreamAudioSink
* sink
);
52 // Remove a sink from the track.
53 // Called on the main render thread.
54 void RemoveSink(MediaStreamAudioSink
* sink
);
56 // Starts the local audio track. Called on the main render thread and
57 // should be called only once when audio track is created.
60 // Overrides for MediaStreamTrack.
62 void SetEnabled(bool enabled
) override
;
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 webrtc::AudioTrackInterface
* GetAudioAdapter() override
;
70 // Returns the output format of the capture source. May return an invalid
71 // AudioParameters if the format is not yet available.
72 // Called on the main render thread.
73 media::AudioParameters
GetOutputFormat() const;
75 // Method called by the capturer to deliver the capture data.
76 // Called on the capture audio thread.
77 void Capture(const media::AudioBus
& audio_bus
,
78 base::TimeTicks estimated_capture_time
,
79 bool force_report_nonzero_energy
);
81 // Method called by the capturer to set the audio parameters used by source
82 // of the capture data..
83 // Called on the capture audio thread.
84 void OnSetFormat(const media::AudioParameters
& params
);
86 // Method called by the capturer to set the processor that applies signal
87 // processing on the data of the track.
88 // Called on the capture audio thread.
89 void SetAudioProcessor(
90 const scoped_refptr
<MediaStreamAudioProcessor
>& processor
);
93 typedef TaggedList
<MediaStreamAudioTrackSink
> SinkList
;
95 // All usage of libjingle is through this adapter. The adapter holds
96 // a pointer to this object, but no reference.
97 const scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_
;
99 // The provider of captured data to render.
100 scoped_refptr
<WebRtcAudioCapturer
> capturer_
;
102 // The source of the audio track which is used by WebAudio, which provides
103 // data to the audio track when hooking up with WebAudio.
104 scoped_refptr
<WebAudioCapturerSource
> webaudio_source_
;
106 // A tagged list of sinks that the audio data is fed to. Tags
107 // indicate tracks that need to be notified that the audio format
111 // Tests that methods are called on libjingle's signaling thread.
112 base::ThreadChecker signal_thread_checker_
;
114 // Used to DCHECK that some methods are called on the capture audio thread.
115 base::ThreadChecker capture_thread_checker_
;
117 // Protects |params_| and |sinks_|.
118 mutable base::Lock lock_
;
120 // Audio parameters of the audio capture stream.
121 // Accessed on only the audio capture thread.
122 media::AudioParameters audio_parameters_
;
124 // Used to calculate the signal level that shows in the UI.
125 // Accessed on only the audio thread.
126 scoped_ptr
<MediaStreamAudioLevelCalculator
> level_calculator_
;
128 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioTrack
);
131 } // namespace content
133 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_