OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include "content/renderer/media/webrtc_local_audio_track.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <limits> | |
10 | |
11 #include "content/public/renderer/media_stream_audio_sink.h" | |
12 #include "content/renderer/media/media_stream_audio_level_calculator.h" | |
13 #include "content/renderer/media/media_stream_audio_processor.h" | |
14 #include "content/renderer/media/media_stream_audio_sink_owner.h" | |
15 #include "content/renderer/media/media_stream_audio_track_sink.h" | |
16 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | |
17 | |
18 namespace content { | |
19 | |
20 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack( | |
21 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter) | |
22 : MediaStreamAudioTrack(true), adapter_(std::move(adapter)) { | |
23 signal_thread_checker_.DetachFromThread(); | |
24 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()"; | |
25 | |
26 adapter_->Initialize(this); | |
27 } | |
28 | |
29 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() { | |
30 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | |
31 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()"; | |
32 // Ensure the track is stopped. | |
33 MediaStreamAudioTrack::Stop(); | |
34 } | |
35 | |
36 media::AudioParameters WebRtcLocalAudioTrack::GetOutputFormat() const { | |
37 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | |
38 base::AutoLock auto_lock(lock_); | |
39 return audio_parameters_; | |
40 } | |
41 | |
42 void WebRtcLocalAudioTrack::Capture(const media::AudioBus& audio_bus, | |
43 base::TimeTicks estimated_capture_time) { | |
44 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
45 DCHECK(!estimated_capture_time.is_null()); | |
46 | |
47 SinkList::ItemList sinks; | |
48 SinkList::ItemList sinks_to_notify_format; | |
49 { | |
50 base::AutoLock auto_lock(lock_); | |
51 sinks = sinks_.Items(); | |
52 sinks_.RetrieveAndClearTags(&sinks_to_notify_format); | |
53 } | |
54 | |
55 // Notify the tracks on when the format changes. This will do nothing if | |
56 // |sinks_to_notify_format| is empty. Note that accessing |audio_parameters_| | |
57 // without holding the |lock_| is valid since |audio_parameters_| is only | |
58 // changed on the current thread. | |
59 for (const auto& sink : sinks_to_notify_format) | |
60 sink->OnSetFormat(audio_parameters_); | |
61 | |
62 // Feed the data to the sinks. | |
63 // TODO(jiayl): we should not pass the real audio data down if the track is | |
64 // disabled. This is currently done so to feed input to WebRTC typing | |
65 // detection and should be changed when audio processing is moved from | |
66 // WebRTC to the track. | |
67 for (const auto& sink : sinks) | |
68 sink->OnData(audio_bus, estimated_capture_time); | |
69 } | |
70 | |
71 void WebRtcLocalAudioTrack::OnSetFormat( | |
72 const media::AudioParameters& params) { | |
73 DVLOG(1) << "WebRtcLocalAudioTrack::OnSetFormat()"; | |
74 // If the source is restarted, we might have changed to another capture | |
75 // thread. | |
76 capture_thread_checker_.DetachFromThread(); | |
77 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
78 | |
79 base::AutoLock auto_lock(lock_); | |
80 audio_parameters_ = params; | |
81 // Remember to notify all sinks of the new format. | |
82 sinks_.TagAll(); | |
83 } | |
84 | |
85 void WebRtcLocalAudioTrack::SetLevel( | |
86 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level) { | |
87 adapter_->SetLevel(std::move(level)); | |
88 } | |
89 | |
90 void WebRtcLocalAudioTrack::SetAudioProcessor( | |
91 scoped_refptr<MediaStreamAudioProcessor> processor) { | |
92 adapter_->SetAudioProcessor(std::move(processor)); | |
93 } | |
94 | |
95 void WebRtcLocalAudioTrack::AddSink(MediaStreamAudioSink* sink) { | |
96 // This method is called from webrtc, on the signaling thread, when the local | |
97 // description is set and from the main thread from WebMediaPlayerMS::load | |
98 // (via WebRtcLocalAudioRenderer::Start). | |
99 DCHECK(main_render_thread_checker_.CalledOnValidThread() || | |
100 signal_thread_checker_.CalledOnValidThread()); | |
101 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()"; | |
102 base::AutoLock auto_lock(lock_); | |
103 | |
104 // Verify that |sink| is not already added to the list. | |
105 DCHECK(!sinks_.Contains( | |
106 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink))); | |
107 | |
108 // Create (and add to the list) a new MediaStreamAudioTrackSink | |
109 // which owns the |sink| and delagates all calls to the | |
110 // MediaStreamAudioSink interface. It will be tagged in the list, so | |
111 // we remember to call OnSetFormat() on the new sink. | |
112 scoped_refptr<MediaStreamAudioTrackSink> sink_owner( | |
113 new MediaStreamAudioSinkOwner(sink)); | |
114 sinks_.AddAndTag(sink_owner.get()); | |
115 } | |
116 | |
117 void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink* sink) { | |
118 // See AddSink for additional context. When local audio is stopped from | |
119 // webrtc, we'll be called here on the signaling thread. | |
120 DCHECK(main_render_thread_checker_.CalledOnValidThread() || | |
121 signal_thread_checker_.CalledOnValidThread()); | |
122 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()"; | |
123 | |
124 scoped_refptr<MediaStreamAudioTrackSink> removed_item; | |
125 { | |
126 base::AutoLock auto_lock(lock_); | |
127 removed_item = sinks_.Remove( | |
128 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink)); | |
129 } | |
130 | |
131 // Clear the delegate to ensure that no more capture callbacks will | |
132 // be sent to this sink. Also avoids a possible crash which can happen | |
133 // if this method is called while capturing is active. | |
134 if (removed_item.get()) | |
135 removed_item->Reset(); | |
136 } | |
137 | |
138 void WebRtcLocalAudioTrack::SetEnabled(bool enabled) { | |
139 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | |
140 if (adapter_.get()) | |
141 adapter_->set_enabled(enabled); | |
142 } | |
143 | |
144 void WebRtcLocalAudioTrack::OnStop() { | |
145 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | |
146 DVLOG(1) << "WebRtcLocalAudioTrack::OnStop()"; | |
147 | |
148 // Protect the pointers using the lock when accessing |sinks_|. | |
149 SinkList::ItemList sinks; | |
150 { | |
151 base::AutoLock auto_lock(lock_); | |
152 sinks = sinks_.Items(); | |
153 sinks_.Clear(); | |
154 } | |
155 | |
156 for (SinkList::ItemList::const_iterator it = sinks.begin(); | |
157 it != sinks.end(); | |
158 ++it){ | |
159 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded); | |
160 (*it)->Reset(); | |
161 } | |
162 } | |
163 | |
164 webrtc::AudioTrackInterface* WebRtcLocalAudioTrack::GetAudioAdapter() { | |
165 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | |
166 return adapter_.get(); | |
167 } | |
168 | |
169 } // namespace content | |
OLD | NEW |