| 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 } | |
| 25 | |
| 26 HtmlAudioElementCapturerSource::HtmlAudioElementCapturerSource( | |
| 27 media::WebAudioSourceProviderImpl* audio_source) | |
| 28 : MediaStreamAudioSource(true /* is_local_source */), | |
| 29 audio_source_(audio_source), | |
| 30 is_started_(false), | |
| 31 last_sample_rate_(0), | |
| 32 last_num_channels_(0), | |
| 33 last_bus_frames_(0) { | |
| 34 DCHECK(audio_source_); | |
| 35 } | |
| 36 | |
| 37 HtmlAudioElementCapturerSource::~HtmlAudioElementCapturerSource() { | |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 39 EnsureSourceIsStopped(); | |
| 40 } | |
| 41 | |
| 42 bool HtmlAudioElementCapturerSource::EnsureSourceIsStarted() { | |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 44 if (audio_source_ && !is_started_) { | |
| 45 // base:Unretained() is safe here since EnsureSourceIsStopped() guarantees | |
| 46 // no more calls to OnAudioBus(). | |
| 47 audio_source_->SetCopyAudioCallback(base::Bind( | |
| 48 &HtmlAudioElementCapturerSource::OnAudioBus, base::Unretained(this))); | |
| 49 is_started_ = true; | |
| 50 } | |
| 51 return is_started_; | |
| 52 } | |
| 53 | |
| 54 void HtmlAudioElementCapturerSource::EnsureSourceIsStopped() { | |
| 55 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 56 if (!is_started_) | |
| 57 return; | |
| 58 | |
| 59 if (audio_source_) { | |
| 60 audio_source_->ClearCopyAudioCallback(); | |
| 61 audio_source_ = nullptr; | |
| 62 } | |
| 63 is_started_ = false; | |
| 64 } | |
| 65 | |
| 66 void HtmlAudioElementCapturerSource::OnAudioBus( | |
| 67 std::unique_ptr<media::AudioBus> audio_bus, | |
| 68 uint32_t delay_milliseconds, | |
| 69 int sample_rate) { | |
| 70 const base::TimeTicks capture_time = | |
| 71 base::TimeTicks::Now() - | |
| 72 base::TimeDelta::FromMilliseconds(delay_milliseconds); | |
| 73 | |
| 74 if (sample_rate != last_sample_rate_ || | |
| 75 audio_bus->channels() != last_num_channels_ || | |
| 76 audio_bus->frames() != last_bus_frames_) { | |
| 77 MediaStreamAudioSource::SetFormat( | |
| 78 media::AudioParameters(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 79 media::GuessChannelLayout(audio_bus->channels()), | |
| 80 sample_rate, 16, audio_bus->frames())); | |
| 81 last_sample_rate_ = sample_rate; | |
| 82 last_num_channels_ = audio_bus->channels(); | |
| 83 last_bus_frames_ = audio_bus->frames(); | |
| 84 } | |
| 85 | |
| 86 MediaStreamAudioSource::DeliverDataToTracks(*audio_bus, capture_time); | |
| 87 } | |
| 88 | |
| 89 } // namespace content | |
| OLD | NEW |