OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/webaudio_capturer_source.h" | 5 #include "content/renderer/media/webaudio_capturer_source.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "content/renderer/media/webrtc_local_audio_track.h" | 10 #include "content/renderer/media/webrtc_local_audio_track.h" |
11 | 11 |
12 using media::AudioBus; | 12 using media::AudioBus; |
13 using media::AudioParameters; | 13 using media::AudioParameters; |
14 using media::ChannelLayout; | 14 using media::ChannelLayout; |
15 using media::CHANNEL_LAYOUT_MONO; | 15 using media::CHANNEL_LAYOUT_MONO; |
16 using media::CHANNEL_LAYOUT_STEREO; | 16 using media::CHANNEL_LAYOUT_STEREO; |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 WebAudioCapturerSource::WebAudioCapturerSource( | 20 WebAudioCapturerSource::WebAudioCapturerSource( |
21 const blink::WebMediaStreamSource& blink_source) | 21 blink::WebMediaStreamSource* blink_source) |
22 : track_(NULL), | 22 : track_(NULL), |
23 audio_format_changed_(false), | 23 audio_format_changed_(false), |
24 fifo_(base::Bind(&WebAudioCapturerSource::DeliverRebufferedAudio, | 24 fifo_(base::Bind(&WebAudioCapturerSource::DeliverRebufferedAudio, |
25 base::Unretained(this))), | 25 base::Unretained(this))), |
26 blink_source_(blink_source) {} | 26 blink_source_(*blink_source) { |
| 27 DCHECK(blink_source); |
| 28 DCHECK(!blink_source_.isNull()); |
| 29 DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()"; |
| 30 blink_source_.addAudioConsumer(this); |
| 31 } |
27 | 32 |
28 WebAudioCapturerSource::~WebAudioCapturerSource() { | 33 WebAudioCapturerSource::~WebAudioCapturerSource() { |
29 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(thread_checker_.CalledOnValidThread()); |
30 removeFromBlinkSource(); | 35 DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()"; |
| 36 DeregisterFromBlinkSource(); |
31 } | 37 } |
32 | 38 |
33 void WebAudioCapturerSource::setFormat( | 39 void WebAudioCapturerSource::setFormat( |
34 size_t number_of_channels, float sample_rate) { | 40 size_t number_of_channels, float sample_rate) { |
35 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
36 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 42 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
37 << sample_rate << ")"; | 43 << sample_rate << ")"; |
38 | 44 |
39 // If the channel count is greater than 8, use discrete layout. However, | 45 // If the channel count is greater than 8, use discrete layout. However, |
40 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. | 46 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. |
(...skipping 27 matching lines...) Expand all Loading... |
68 base::AutoLock auto_lock(lock_); | 74 base::AutoLock auto_lock(lock_); |
69 track_ = track; | 75 track_ = track; |
70 } | 76 } |
71 | 77 |
72 void WebAudioCapturerSource::Stop() { | 78 void WebAudioCapturerSource::Stop() { |
73 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(thread_checker_.CalledOnValidThread()); |
74 { | 80 { |
75 base::AutoLock auto_lock(lock_); | 81 base::AutoLock auto_lock(lock_); |
76 track_ = NULL; | 82 track_ = NULL; |
77 } | 83 } |
78 // removeFromBlinkSource() should not be called while |lock_| is acquired, | 84 // DeregisterFromBlinkSource() should not be called while |lock_| is acquired, |
79 // as it could result in a deadlock. | 85 // as it could result in a deadlock. |
80 removeFromBlinkSource(); | 86 DeregisterFromBlinkSource(); |
81 } | 87 } |
82 | 88 |
83 void WebAudioCapturerSource::consumeAudio( | 89 void WebAudioCapturerSource::consumeAudio( |
84 const blink::WebVector<const float*>& audio_data, | 90 const blink::WebVector<const float*>& audio_data, |
85 size_t number_of_frames) { | 91 size_t number_of_frames) { |
86 // TODO(miu): Plumbing is needed to determine the actual capture timestamp | 92 // TODO(miu): Plumbing is needed to determine the actual capture timestamp |
87 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper | 93 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper |
88 // audio/video sync. http://crbug.com/335335 | 94 // audio/video sync. http://crbug.com/335335 |
89 current_reference_time_ = base::TimeTicks::Now(); | 95 current_reference_time_ = base::TimeTicks::Now(); |
90 | 96 |
(...skipping 19 matching lines...) Expand all Loading... |
110 | 116 |
111 void WebAudioCapturerSource::DeliverRebufferedAudio( | 117 void WebAudioCapturerSource::DeliverRebufferedAudio( |
112 const media::AudioBus& audio_bus, | 118 const media::AudioBus& audio_bus, |
113 int frame_delay) { | 119 int frame_delay) { |
114 lock_.AssertAcquired(); | 120 lock_.AssertAcquired(); |
115 const base::TimeTicks reference_time = | 121 const base::TimeTicks reference_time = |
116 current_reference_time_ + | 122 current_reference_time_ + |
117 base::TimeDelta::FromMicroseconds(frame_delay * | 123 base::TimeDelta::FromMicroseconds(frame_delay * |
118 base::Time::kMicrosecondsPerSecond / | 124 base::Time::kMicrosecondsPerSecond / |
119 params_.sample_rate()); | 125 params_.sample_rate()); |
120 track_->Capture(audio_bus, reference_time, false); | 126 track_->Capture(audio_bus, reference_time); |
121 } | 127 } |
122 | 128 |
123 // If registered as audio consumer in |blink_source_|, deregister from | 129 void WebAudioCapturerSource::DeregisterFromBlinkSource() { |
124 // |blink_source_| and stop keeping a reference to |blink_source_|. | |
125 // Failure to call this method when stopping the track might leave an invalid | |
126 // WebAudioCapturerSource reference still registered as an audio consumer on | |
127 // the blink side. | |
128 void WebAudioCapturerSource::removeFromBlinkSource() { | |
129 if (!blink_source_.isNull()) { | 130 if (!blink_source_.isNull()) { |
130 blink_source_.removeAudioConsumer(this); | 131 blink_source_.removeAudioConsumer(this); |
131 blink_source_.reset(); | 132 blink_source_.reset(); |
132 } | 133 } |
133 } | 134 } |
134 | 135 |
135 } // namespace content | 136 } // namespace content |
OLD | NEW |