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/synchronization/lock.h"
12 #include "base/threading/thread_checker.h"
13 #include "content/renderer/media/webrtc_audio_device_impl.h"
14 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
15 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
16 #include "third_party/libjingle/source/talk/app/webrtc/mediastreamtrack.h"
17 #include "third_party/libjingle/source/talk/media/base/audiorenderer.h"
21 } // namespace cricket
29 class WebAudioCapturerSource;
30 class WebRtcAudioCapturer;
31 class WebRtcAudioCapturerSinkOwner;
33 // A WebRtcLocalAudioTrack instance contains the implementations of
34 // MediaStreamTrack and WebRtcAudioCapturerSink.
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 cricket::AudioRenderer),
41 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) {
43 static scoped_refptr<WebRtcLocalAudioTrack> Create(
44 const std::string& id,
45 const scoped_refptr<WebRtcAudioCapturer>& capturer,
46 WebAudioCapturerSource* webaudio_source,
47 webrtc::AudioSourceInterface* track_source,
48 const webrtc::MediaConstraintsInterface* constraints);
50 // Add a sink to the track. This function will trigger a SetCaptureFormat()
51 // call on the |sink|.
52 // Called on the main render thread.
53 void AddSink(WebRtcAudioCapturerSink* sink);
55 // Remove a sink from the track.
56 // Called on the main render thread.
57 void RemoveSink(WebRtcAudioCapturerSink* sink);
59 // Starts the local audio track. Called on the main render thread and
60 // should be called only once when audio track is created.
63 // Stops the local audio track. Called on the main render thread and
64 // should be called only once when audio track going away.
67 // Method called by the capturer to deliver the capture data.
68 void Capture(media::AudioBus* audio_source,
69 int audio_delay_milliseconds,
73 // Method called by the capturer to set the audio parameters used by source
74 // of the capture data..
75 // Can be called on different user threads.
76 void SetCaptureFormat(const media::AudioParameters& params);
79 WebRtcLocalAudioTrack(
80 const std::string& label,
81 const scoped_refptr<WebRtcAudioCapturer>& capturer,
82 WebAudioCapturerSource* webaudio_source,
83 webrtc::AudioSourceInterface* track_source,
84 const webrtc::MediaConstraintsInterface* constraints);
86 virtual ~WebRtcLocalAudioTrack();
89 typedef std::list<scoped_refptr<WebRtcAudioCapturerSinkOwner> > SinkList;
91 // cricket::AudioCapturer implementation.
92 virtual void AddChannel(int channel_id) OVERRIDE;
93 virtual void RemoveChannel(int channel_id) OVERRIDE;
95 // webrtc::AudioTrackInterface implementation.
96 virtual webrtc::AudioSourceInterface* GetSource() const OVERRIDE;
97 virtual cricket::AudioRenderer* GetRenderer() OVERRIDE;
99 // webrtc::MediaStreamTrack implementation.
100 virtual std::string kind() const OVERRIDE;
102 // The provider of captured data to render.
103 // The WebRtcAudioCapturer is today created by WebRtcAudioDeviceImpl.
104 scoped_refptr<WebRtcAudioCapturer> capturer_;
106 // The source of the audio track which is used by WebAudio, which provides
107 // data to the audio track when hooking up with WebAudio.
108 scoped_refptr<WebAudioCapturerSource> webaudio_source_;
110 // The source of the audio track which handles the audio constraints.
111 // TODO(xians): merge |track_source_| to |capturer_|.
112 talk_base::scoped_refptr<webrtc::AudioSourceInterface> track_source_;
114 // A list of sinks that the audio data is fed to.
117 // Used to DCHECK that we are called on the correct thread.
118 base::ThreadChecker thread_checker_;
120 // Protects |params_| and |sinks_|.
121 mutable base::Lock lock_;
123 // A vector of WebRtc VoE channels that the capturer sends datat to.
124 std::vector<int> voe_channels_;
126 bool need_audio_processing_;
128 // Buffers used for temporary storage during capture callbacks.
129 // Allocated during initialization.
130 class ConfiguredBuffer;
131 scoped_refptr<ConfiguredBuffer> buffer_;
133 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioTrack);
136 } // namespace content
138 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_