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/browser/renderer_host/media/audio_output_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/memory/shared_memory.h" |
| 11 #include "base/sync_socket.h" |
| 12 #include "mojo/public/cpp/system/platform_handle.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 AudioOutputImpl::AudioOutputImpl( |
| 17 mojom::AudioOutputRequest request, |
| 18 CreateDelegateCallback create_delegate_callback, |
| 19 FinishedCallback finished_callback) |
| 20 : binding_(this, std::move(request)), |
| 21 create_delegate_callback_(std::move(create_delegate_callback)), |
| 22 finished_callback_(std::move(finished_callback)) { |
| 23 DCHECK(thread_checker_.CalledOnValidThread()); |
| 24 DCHECK(finished_callback_); |
| 25 // |this| owns |binding_|, so unretained is safe. |
| 26 binding_.set_connection_error_handler( |
| 27 base::Bind(&AudioOutputImpl::OnError, base::Unretained(this))); |
| 28 } |
| 29 |
| 30 AudioOutputImpl::~AudioOutputImpl() { |
| 31 DCHECK(thread_checker_.CalledOnValidThread()); |
| 32 } |
| 33 |
| 34 void AudioOutputImpl::Start(const media::AudioParameters& params, |
| 35 const StartCallback& callback) { |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); |
| 37 if (delegate_) { |
| 38 // Already started. |
| 39 OnError(); |
| 40 return; |
| 41 } |
| 42 // Buffer up all calls from the renderer until we are done starting. |
| 43 binding_.PauseIncomingMethodCallProcessing(); |
| 44 delegate_ = std::move(create_delegate_callback_).Run(this, params); |
| 45 DCHECK(delegate_); |
| 46 start_callback_ = callback; |
| 47 } |
| 48 |
| 49 void AudioOutputImpl::Play() { |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 if (!delegate_) { |
| 52 // Not started yet. |
| 53 OnError(); |
| 54 return; |
| 55 } |
| 56 delegate_->OnPlayStream(); |
| 57 } |
| 58 |
| 59 void AudioOutputImpl::Pause() { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 if (!delegate_) { |
| 62 // Not started yet. |
| 63 OnError(); |
| 64 return; |
| 65 } |
| 66 delegate_->OnPauseStream(); |
| 67 } |
| 68 |
| 69 void AudioOutputImpl::SetVolume(double volume) { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 if (!delegate_ || volume < 0 || volume > 1) { |
| 72 // Not started yet. |
| 73 OnError(); |
| 74 return; |
| 75 } |
| 76 delegate_->OnSetVolume(volume); |
| 77 } |
| 78 |
| 79 void AudioOutputImpl::OnStreamCreated( |
| 80 int stream_id, |
| 81 base::SharedMemory* shared_memory, |
| 82 base::CancelableSyncSocket* foreign_socket) { |
| 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 if (!binding_.is_bound()) { |
| 85 // Already closed, make sure we are properly cleaned up. |
| 86 OnError(); |
| 87 return; |
| 88 } |
| 89 DCHECK(start_callback_); |
| 90 DCHECK(shared_memory); |
| 91 DCHECK(foreign_socket); |
| 92 |
| 93 base::SharedMemoryHandle foreign_memory_handle = |
| 94 base::SharedMemory::DuplicateHandle(shared_memory->handle()); |
| 95 DCHECK(base::SharedMemory::IsHandleValid(foreign_memory_handle)); |
| 96 |
| 97 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( |
| 98 foreign_memory_handle, shared_memory->requested_size(), false); |
| 99 mojo::ScopedHandle socket_handle = |
| 100 mojo::WrapPlatformFile(foreign_socket->handle()); |
| 101 |
| 102 DCHECK(buffer_handle.is_valid()); |
| 103 DCHECK(socket_handle.is_valid()); |
| 104 |
| 105 start_callback_.Run(std::move(buffer_handle), std::move(socket_handle)); |
| 106 start_callback_ = StartCallback(); |
| 107 binding_.ResumeIncomingMethodCallProcessing(); |
| 108 } |
| 109 |
| 110 void AudioOutputImpl::OnStreamError(int stream_id) { |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 112 OnError(); |
| 113 } |
| 114 |
| 115 void AudioOutputImpl::OnError() { |
| 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 117 if (!finished_callback_) { |
| 118 // There is already a destruction in progress. |
| 119 return; |
| 120 } |
| 121 // Notify the renderer. |
| 122 binding_.Close(); |
| 123 // The rest of the cleanup is done when |handler_| destructs |this|. |
| 124 std::move(finished_callback_).Run(this); |
| 125 } |
| 126 |
| 127 } // namespace content |
OLD | NEW |