Chromium Code Reviews| Index: content/browser/renderer_host/media/audio_output_impl.cc |
| diff --git a/content/browser/renderer_host/media/audio_output_impl.cc b/content/browser/renderer_host/media/audio_output_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d2628bb5133835f27ce158c7e6f15ec127ad5a60 |
| --- /dev/null |
| +++ b/content/browser/renderer_host/media/audio_output_impl.cc |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/media/audio_output_impl.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/memory/shared_memory.h" |
| +#include "base/sync_socket.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "mojo/public/cpp/system/platform_handle.h" |
| + |
| +namespace content { |
| + |
| +AudioOutputImpl::AudioOutputImpl( |
| + media::mojom::AudioOutputRequest request, |
| + FinishedCallback finished_callback, |
| + DelegateFactoryCallback delegate_factory_callback) |
| + : delegate_factory_callback_(std::move(delegate_factory_callback)), |
| + finished_callback_(std::move(finished_callback)), |
| + binding_(this, std::move(request)) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + DCHECK(!delegate_factory_callback_.is_null()); |
| + DCHECK(!finished_callback_.is_null()); |
| + // |this| owns |binding_|, so unretained is safe. |
| + binding_.set_connection_error_handler( |
| + base::Bind(&AudioOutputImpl::OnError, base::Unretained(this))); |
| +} |
| + |
| +AudioOutputImpl::~AudioOutputImpl() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| +} |
| + |
| +void AudioOutputImpl::Start(const media::AudioParameters& params, |
| + const StartCallback& callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (delegate_) { |
| + // Already started. |
| + OnError(); |
| + return; |
| + } |
| + // Buffer up all calls from the renderer until we are done starting. |
| + binding_.PauseIncomingMethodCallProcessing(); |
| + delegate_ = std::move(delegate_factory_callback_).Run(this, params); |
| + DCHECK(delegate_); |
| + start_callback_ = callback; |
| +} |
| + |
| +void AudioOutputImpl::Play() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!delegate_) { |
| + // Not started yet. |
| + OnError(); |
| + return; |
| + } |
| + delegate_->OnPlayStream(); |
| +} |
| + |
| +void AudioOutputImpl::Pause() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!delegate_) { |
| + // Not started yet. |
| + OnError(); |
| + return; |
| + } |
| + delegate_->OnPauseStream(); |
| +} |
| + |
| +void AudioOutputImpl::SetVolume(double volume) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!delegate_) { |
| + // Not started yet. |
| + OnError(); |
| + return; |
| + } |
| + if (volume < 0 || volume > 1) |
|
o1ka
2017/01/25 15:15:21
Where the limitation comes from? a comment is nee
|
| + return; |
| + delegate_->OnSetVolume(volume); |
| +} |
| + |
| +void AudioOutputImpl::OnStreamStateChanged(bool playing) {} |
| + |
| +void AudioOutputImpl::OnStreamCreated( |
| + int stream_id, |
| + base::SharedMemory* shared_memory, |
| + base::CancelableSyncSocket* foreign_socket) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!binding_.is_bound()) { |
| + // Already closed, make sure we are properly cleaned up. |
| + OnError(); |
| + return; |
| + } |
| + DCHECK(shared_memory); |
| + DCHECK(foreign_socket); |
| + |
| + // TODO: Share memory/socket correctly. |
| + base::SharedMemoryHandle foreign_memory_handle = |
| + base::SharedMemory::DuplicateHandle(shared_memory->handle()); |
| + DCHECK(base::SharedMemory::IsHandleValid(foreign_memory_handle)) |
| + << "Invalid memory handle."; |
| + mojo::ScopedSharedBufferHandle shared_buffer_handle = |
| + mojo::WrapSharedMemoryHandle(foreign_memory_handle, |
| + shared_memory->requested_size(), false); |
| + |
| + DCHECK(shared_buffer_handle.is_valid()); |
| + |
| + start_callback_.Run(std::move(shared_buffer_handle), |
| + mojo::WrapPlatformFile(foreign_socket->handle())); |
| + start_callback_ = StartCallback(); |
| + binding_.ResumeIncomingMethodCallProcessing(); |
| +} |
| + |
| +void AudioOutputImpl::OnStreamError(int stream_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + OnError(); |
| +} |
| + |
| +void AudioOutputImpl::OnError() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (finished_callback_.is_null()) { |
| + // There is already a destruction in progress. |
| + return; |
| + } |
| + // Notify the renderer. |
| + binding_.Close(); |
| + // The rest of the cleanup is done when |handler_| destructs |this|. |
| + std::move(finished_callback_).Run(this); |
| +} |
| + |
| +} // namespace content |