Chromium Code Reviews| 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/common/media/media_stream_options.h" | |
| 8 #include "content/renderer/media/audio_device_factory.h" | |
| 9 #include "content/renderer/render_frame_impl.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 LocalMediaStreamAudioSource::LocalMediaStreamAudioSource( | |
| 14 int consumer_render_frame_id, | |
| 15 const StreamDeviceInfo& device_info) | |
| 16 : MediaStreamAudioSource(true /* is_local_source */), | |
| 17 consumer_render_frame_id_(consumer_render_frame_id) { | |
| 18 DVLOG(1) << "LocalMediaStreamAudioSource::LocalMediaStreamAudioSource()"; | |
| 19 MediaStreamSource::SetDeviceInfo(device_info); | |
| 20 | |
| 21 // If the device buffer size was not provided, use a default. | |
| 22 int frames_per_buffer = device_info.device.input.frames_per_buffer; | |
|
o1ka
2016/08/16 14:18:11
I would prefer to avoid the duplication: now we ha
miu
2016/08/16 20:13:39
Yeah, the duplication bothered me a little too. Th
| |
| 23 if (frames_per_buffer <= 0) { | |
| 24 // TODO(miu): Like in ProcessedLocalAudioSource::GetBufferSize(), we should | |
| 25 // re-evaluate whether Android needs special treatment here. Or, perhaps we | |
| 26 // should just DCHECK_GT(device_info...frames_per_buffer, 0)? | |
| 27 // http://crbug.com/638081 | |
| 28 #if defined(OS_ANDROID) | |
| 29 frames_per_buffer = device_info.device.input.sample_rate / 50; // 20 ms | |
| 30 #else | |
| 31 frames_per_buffer = device_info.device.input.sample_rate / 100; // 10 ms | |
| 32 #endif | |
| 33 } | |
| 34 | |
| 35 MediaStreamAudioSource::SetFormat(media::AudioParameters( | |
| 36 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 37 static_cast<media::ChannelLayout>( | |
| 38 device_info.device.input.channel_layout), | |
| 39 device_info.device.input.sample_rate, | |
| 40 16, // Legacy parameter (data is always in 32-bit float format). | |
| 41 frames_per_buffer)); | |
| 42 } | |
| 43 | |
| 44 LocalMediaStreamAudioSource::~LocalMediaStreamAudioSource() { | |
| 45 DVLOG(1) << "LocalMediaStreamAudioSource::~LocalMediaStreamAudioSource()"; | |
| 46 EnsureSourceIsStopped(); | |
| 47 } | |
| 48 | |
| 49 bool LocalMediaStreamAudioSource::EnsureSourceIsStarted() { | |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 51 | |
| 52 if (source_) | |
| 53 return true; | |
| 54 | |
| 55 // Sanity-check that the consuming RenderFrame still exists. This is required | |
| 56 // by AudioDeviceFactory. | |
| 57 if (!RenderFrameImpl::FromRoutingID(consumer_render_frame_id_)) | |
| 58 return false; | |
| 59 | |
| 60 VLOG(1) << "Starting local audio input device (session_id=" | |
| 61 << device_info().session_id << ") for render frame " | |
| 62 << consumer_render_frame_id_ << " with audio parameters={" | |
| 63 << GetAudioParameters().AsHumanReadableString() << "}."; | |
| 64 | |
| 65 source_ = | |
| 66 AudioDeviceFactory::NewAudioCapturerSource(consumer_render_frame_id_); | |
| 67 source_->Initialize(GetAudioParameters(), this, device_info().session_id); | |
| 68 source_->Start(); | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 void LocalMediaStreamAudioSource::EnsureSourceIsStopped() { | |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 74 | |
| 75 if (!source_) | |
| 76 return; | |
| 77 | |
| 78 source_->Stop(); | |
| 79 source_ = nullptr; | |
| 80 | |
| 81 VLOG(1) << "Stopped local audio input device (session_id=" | |
| 82 << device_info().session_id << ") for render frame " | |
| 83 << consumer_render_frame_id_ << " with audio parameters={" | |
| 84 << GetAudioParameters().AsHumanReadableString() << "}."; | |
| 85 } | |
| 86 | |
| 87 void LocalMediaStreamAudioSource::Capture(const media::AudioBus* audio_bus, | |
| 88 int audio_delay_milliseconds, | |
| 89 double volume, | |
| 90 bool key_pressed) { | |
| 91 DCHECK(audio_bus); | |
| 92 // TODO(miu): Plumbing is needed to determine the actual capture timestamp | |
| 93 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper | |
| 94 // audio/video sync. http://crbug.com/335335 | |
| 95 MediaStreamAudioSource::DeliverDataToTracks( | |
| 96 *audio_bus, | |
| 97 base::TimeTicks::Now() - | |
| 98 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds)); | |
| 99 } | |
| 100 | |
| 101 void LocalMediaStreamAudioSource::OnCaptureError(const std::string& why) { | |
| 102 // As of this writing, this method doesn't get called for anything useful, | |
| 103 // and all other implementors just log the message, but don't disconnect sinks | |
| 104 // or take any other action. So, just log the error. | |
| 105 LOG(ERROR) << why; | |
| 106 } | |
| 107 | |
| 108 } // namespace content | |
| OLD | NEW |