OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/html_audio_element_capturer_source.h" |
| 6 |
| 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "media/base/audio_parameters.h" |
| 9 #include "media/base/audio_renderer_sink.h" |
| 10 #include "media/blink/webaudiosourceprovider_impl.h" |
| 11 #include "media/blink/webmediaplayer_impl.h" |
| 12 #include "third_party/WebKit/public/platform/WebMediaPlayer.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 //static |
| 17 HtmlAudioElementCapturerSource* |
| 18 HtmlAudioElementCapturerSource::CreateFromWebMediaPlayerImpl( |
| 19 blink::WebMediaPlayer* player) { |
| 20 DCHECK(player); |
| 21 return new HtmlAudioElementCapturerSource( |
| 22 static_cast<media::WebAudioSourceProviderImpl*>( |
| 23 player->getAudioSourceProvider()) |
| 24 ->AsWeakPtr()); |
| 25 } |
| 26 |
| 27 HtmlAudioElementCapturerSource::HtmlAudioElementCapturerSource( |
| 28 const base::WeakPtr<media::WebAudioSourceProviderImpl>& audio_source) |
| 29 : MediaStreamAudioSource(true /* is_local_source */), |
| 30 audio_source_(audio_source), |
| 31 is_started_(false), |
| 32 last_sample_rate_(0), |
| 33 last_num_channels_(0), |
| 34 last_bus_frames_(0) { |
| 35 DCHECK(audio_source_); |
| 36 } |
| 37 |
| 38 HtmlAudioElementCapturerSource::~HtmlAudioElementCapturerSource() { |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 40 EnsureSourceIsStopped(); |
| 41 } |
| 42 |
| 43 bool HtmlAudioElementCapturerSource::EnsureSourceIsStarted() { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 if (audio_source_ && !is_started_) { |
| 46 // base:Unretained() is safe here since EnsureSourceIsStopped() guarantees |
| 47 // no more calls to OnAudioBus(). |
| 48 audio_source_->SetCopyAudioCallback(base::Bind( |
| 49 &HtmlAudioElementCapturerSource::OnAudioBus, base::Unretained(this))); |
| 50 is_started_ = true; |
| 51 } |
| 52 return is_started_; |
| 53 } |
| 54 |
| 55 void HtmlAudioElementCapturerSource::EnsureSourceIsStopped() { |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 if (!is_started_) |
| 58 return; |
| 59 |
| 60 if (audio_source_) { |
| 61 audio_source_->ClearCopyAudioCallback(); |
| 62 audio_source_.reset(); |
| 63 } |
| 64 is_started_ = false; |
| 65 } |
| 66 |
| 67 void HtmlAudioElementCapturerSource::OnAudioBus( |
| 68 std::unique_ptr<media::AudioBus> audio_bus, |
| 69 uint32_t delay_milliseconds, |
| 70 int sample_rate) { |
| 71 const base::TimeTicks capture_time = |
| 72 base::TimeTicks::Now() - |
| 73 base::TimeDelta::FromMilliseconds(delay_milliseconds); |
| 74 |
| 75 if (sample_rate != last_sample_rate_ || |
| 76 audio_bus->channels() != last_num_channels_ || |
| 77 audio_bus->frames() != last_bus_frames_) { |
| 78 MediaStreamAudioSource::SetFormat( |
| 79 media::AudioParameters(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 80 media::GuessChannelLayout(audio_bus->channels()), |
| 81 sample_rate, 16, audio_bus->frames())); |
| 82 last_sample_rate_ = sample_rate; |
| 83 last_num_channels_ = audio_bus->channels(); |
| 84 last_bus_frames_ = audio_bus->frames(); |
| 85 } |
| 86 |
| 87 MediaStreamAudioSource::DeliverDataToTracks(*audio_bus, capture_time); |
| 88 } |
| 89 |
| 90 } // namespace content |
OLD | NEW |