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/local_media_stream_audio_source.h" |
| 6 |
| 7 #include "content/renderer/media/audio_device_factory.h" |
| 8 #include "content/renderer/render_frame_impl.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 LocalMediaStreamAudioSource::LocalMediaStreamAudioSource( |
| 13 int consumer_render_frame_id, |
| 14 const StreamDeviceInfo& device_info) |
| 15 : MediaStreamAudioSource(true /* is_local_source */), |
| 16 consumer_render_frame_id_(consumer_render_frame_id), |
| 17 session_id_(device_info.session_id) { |
| 18 DVLOG(1) << "LocalMediaStreamAudioSource::LocalMediaStreamAudioSource()"; |
| 19 MediaStreamSource::SetDeviceInfo(device_info); |
| 20 MediaStreamAudioSource::SetFormat(media::AudioParameters( |
| 21 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 22 static_cast<media::ChannelLayout>( |
| 23 device_info.device.input.channel_layout), |
| 24 device_info.device.input.sample_rate, |
| 25 16, // Legacy parameter (data is always in 32-bit float format). |
| 26 // If specified, use frames_per_buffer; else, default to 10 ms. |
| 27 device_info.device.input.frames_per_buffer > 0 ? |
| 28 device_info.device.input.frames_per_buffer : |
| 29 device_info.device.input.sample_rate / 100)); |
| 30 } |
| 31 |
| 32 LocalMediaStreamAudioSource::~LocalMediaStreamAudioSource() { |
| 33 DVLOG(1) << "LocalMediaStreamAudioSource::~LocalMediaStreamAudioSource()"; |
| 34 // Superclass will call StopSource() just in case. |
| 35 } |
| 36 |
| 37 void LocalMediaStreamAudioSource::DoStopSource() { |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); |
| 39 if (is_stopped_) |
| 40 return; |
| 41 if (input_device_) { |
| 42 input_device_->Stop(); |
| 43 input_device_ = nullptr; |
| 44 VLOG(1) << "Stopped local audio input device (session_id=" << session_id_ |
| 45 << ") for render frame " << consumer_render_frame_id_ |
| 46 << " with audio parameters={" |
| 47 << GetAudioParameters().AsHumanReadableString() << "}."; |
| 48 } |
| 49 is_stopped_ = true; |
| 50 } |
| 51 |
| 52 bool LocalMediaStreamAudioSource::EnsureSourceIsStarted() { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 |
| 55 if (is_stopped_) |
| 56 return false; |
| 57 if (input_device_) |
| 58 return true; |
| 59 |
| 60 // Sanity-check that the consuming RenderFrame still exists. This is |
| 61 // required by AudioDeviceFactory. |
| 62 if (!RenderFrameImpl::FromRoutingID(consumer_render_frame_id_)) { |
| 63 StopSource(); |
| 64 return false; |
| 65 } |
| 66 |
| 67 VLOG(1) << "Starting local audio input device (session_id=" << session_id_ |
| 68 << ") for render frame " << consumer_render_frame_id_ |
| 69 << " with audio parameters={" |
| 70 << GetAudioParameters().AsHumanReadableString() << "}."; |
| 71 |
| 72 input_device_ = AudioDeviceFactory::NewInputDevice(consumer_render_frame_id_); |
| 73 input_device_->Initialize(GetAudioParameters(), this, session_id_); |
| 74 input_device_->Start(); |
| 75 return true; |
| 76 } |
| 77 |
| 78 void LocalMediaStreamAudioSource::Capture(const media::AudioBus* audio_bus, |
| 79 int audio_delay_milliseconds, |
| 80 double volume, |
| 81 bool key_pressed) { |
| 82 DCHECK(audio_bus); |
| 83 // TODO(miu): Plumbing is needed to determine the actual capture timestamp |
| 84 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper |
| 85 // audio/video sync. http://crbug.com/335335 |
| 86 MediaStreamAudioSource::DeliverDataToTracks( |
| 87 *audio_bus, |
| 88 base::TimeTicks::Now() - |
| 89 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds)); |
| 90 } |
| 91 |
| 92 void LocalMediaStreamAudioSource::OnCaptureError(const std::string& why) { |
| 93 // As of this writing, this method doesn't get called for anything useful, |
| 94 // and all other implementors just log the message, but don't disconnect sinks |
| 95 // or take any other action. So, just log the error. |
| 96 LOG(ERROR) << why; |
| 97 } |
| 98 |
| 99 } // namespace content |
OLD | NEW |