f7a684e7c5feacf8de9e498049c83c9d24059fad
[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/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"
18
19 namespace content {
20
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;
30
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) {
38  public:
39   WebRtcLocalAudioTrack(WebRtcLocalAudioTrackAdapter* adapter,
40                         const scoped_refptr<WebRtcAudioCapturer>& capturer,
41                         WebAudioCapturerSource* webaudio_source);
42
43   virtual ~WebRtcLocalAudioTrack();
44
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);
49
50   // Remove a sink from the track.
51   // Called on the main render thread.
52   void RemoveSink(MediaStreamAudioSink* sink);
53
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);
59
60   // Starts the local audio track. Called on the main render thread and
61   // should be called only once when audio track is created.
62   void Start();
63
64   // Stops the local audio track. Called on the main render thread and
65   // should be called only once when audio track going away.
66   void Stop();
67
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,
72                int volume,
73                bool key_pressed,
74                bool need_audio_processing);
75
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);
80
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);
86
87   blink::WebAudioSourceProvider* audio_source_provider() const {
88     return source_provider_.get();
89   }
90
91  private:
92   typedef TaggedList<MediaStreamAudioTrackSink> SinkList;
93
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_;
97
98   // The provider of captured data to render.
99   scoped_refptr<WebRtcAudioCapturer> capturer_;
100
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_;
104
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
107   // has changed.
108   SinkList sinks_;
109
110   // Used to DCHECK that some methods are called on the main render thread.
111   base::ThreadChecker main_render_thread_checker_;
112
113   // Used to DCHECK that some methods are called on the capture audio thread.
114   base::ThreadChecker capture_thread_checker_;
115
116   // Protects |params_| and |sinks_|.
117   mutable base::Lock lock_;
118
119   // Audio parameters of the audio capture stream.
120   // Accessed on only the audio capture thread.
121   media::AudioParameters audio_parameters_;
122
123   // The source provider to feed the track data to other clients like
124   // WebAudio.
125   scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_;
126
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_;
130
131   DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioTrack);
132 };
133
134 }  // namespace content
135
136 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_