- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webrtc_local_audio_track.h
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/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"
18
19 namespace cricket {
20 class AudioRenderer;
21 }  // namespace cricket
22
23 namespace media {
24 class AudioBus;
25 }  // namespace media
26
27 namespace content {
28
29 class WebAudioCapturerSource;
30 class WebRtcAudioCapturer;
31 class WebRtcAudioCapturerSinkOwner;
32
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),
40       NON_EXPORTED_BASE(
41           public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) {
42  public:
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);
49
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);
54
55   // Remove a sink from the track.
56   // Called on the main render thread.
57   void RemoveSink(WebRtcAudioCapturerSink* sink);
58
59   // Starts the local audio track. Called on the main render thread and
60   // should be called only once when audio track is created.
61   void Start();
62
63   // Stops the local audio track. Called on the main render thread and
64   // should be called only once when audio track going away.
65   void Stop();
66
67   // Method called by the capturer to deliver the capture data.
68   void Capture(media::AudioBus* audio_source,
69                int audio_delay_milliseconds,
70                int volume,
71                bool key_pressed);
72
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);
77
78  protected:
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);
85
86   virtual ~WebRtcLocalAudioTrack();
87
88  private:
89   typedef std::list<scoped_refptr<WebRtcAudioCapturerSinkOwner> > SinkList;
90
91   // cricket::AudioCapturer implementation.
92   virtual void AddChannel(int channel_id) OVERRIDE;
93   virtual void RemoveChannel(int channel_id) OVERRIDE;
94
95   // webrtc::AudioTrackInterface implementation.
96   virtual webrtc::AudioSourceInterface* GetSource() const OVERRIDE;
97   virtual cricket::AudioRenderer* GetRenderer() OVERRIDE;
98
99   // webrtc::MediaStreamTrack implementation.
100   virtual std::string kind() const OVERRIDE;
101
102   // The provider of captured data to render.
103   // The WebRtcAudioCapturer is today created by WebRtcAudioDeviceImpl.
104   scoped_refptr<WebRtcAudioCapturer> capturer_;
105
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_;
109
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_;
113
114   // A list of sinks that the audio data is fed to.
115   SinkList sinks_;
116
117   // Used to DCHECK that we are called on the correct thread.
118   base::ThreadChecker thread_checker_;
119
120   // Protects |params_| and |sinks_|.
121   mutable base::Lock lock_;
122
123   // A vector of WebRtc VoE channels that the capturer sends datat to.
124   std::vector<int> voe_channels_;
125
126   bool need_audio_processing_;
127
128   // Buffers used for temporary storage during capture callbacks.
129   // Allocated during initialization.
130   class ConfiguredBuffer;
131   scoped_refptr<ConfiguredBuffer> buffer_;
132
133   DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioTrack);
134 };
135
136 }  // namespace content
137
138 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_