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 "media/mojo/services/mojo_audio_output.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 media { | |
| 15 | |
| 16 MojoAudioOutput::MojoAudioOutput( | |
| 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(&MojoAudioOutput::OnError, base::Unretained(this))); | |
| 28 } | |
| 29 | |
| 30 MojoAudioOutput::~MojoAudioOutput() { | |
| 31 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 32 } | |
| 33 | |
| 34 void MojoAudioOutput::Start(const AudioParameters& params, | |
| 35 const StartCallback& callback) { | |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 37 if (delegate_) { | |
| 38 // Already started. | |
|
o1ka
2017/02/22 13:26:30
Add logging on error in all places like this? Will
Max Morin
2017/03/02 23:11:32
Done.
| |
| 39 OnError(); | |
| 40 return; | |
| 41 } | |
| 42 delegate_ = std::move(create_delegate_callback_).Run(this, params); | |
|
DaleCurtis
2017/02/21 18:18:32
Hmm, post std::move the contents of create_delegat
Max Morin
2017/02/22 10:08:43
The C++ standard requires that it is in a "valid b
o1ka
2017/02/22 13:26:30
Here is what I found it unit tests: https://cs.chr
Max Morin
2017/02/22 16:07:44
The comment actually contradicts the test, since t
| |
| 43 DCHECK(delegate_); | |
| 44 start_callback_ = callback; | |
| 45 } | |
| 46 | |
| 47 void MojoAudioOutput::Play() { | |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 49 if (!delegate_) { | |
| 50 // Not started yet. | |
| 51 OnError(); | |
| 52 return; | |
| 53 } | |
| 54 delegate_->OnPlayStream(); | |
| 55 } | |
| 56 | |
| 57 void MojoAudioOutput::Pause() { | |
| 58 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 59 if (!delegate_) { | |
| 60 // Not started yet. | |
| 61 OnError(); | |
| 62 return; | |
| 63 } | |
| 64 delegate_->OnPauseStream(); | |
| 65 } | |
| 66 | |
| 67 void MojoAudioOutput::SetVolume(double volume) { | |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 69 if (!delegate_ || volume < 0 || volume > 1) { | |
| 70 // Not started yet. | |
|
o1ka
2017/02/22 13:26:30
...or volume is out of range
| |
| 71 OnError(); | |
| 72 return; | |
| 73 } | |
| 74 delegate_->OnSetVolume(volume); | |
| 75 } | |
| 76 | |
| 77 void MojoAudioOutput::OnStreamCreated( | |
| 78 int stream_id, | |
| 79 base::SharedMemory* shared_memory, | |
| 80 base::CancelableSyncSocket* foreign_socket) { | |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 82 if (!binding_.is_bound()) { | |
| 83 // Already closed, make sure we are properly cleaned up. | |
| 84 OnError(); | |
| 85 return; | |
| 86 } | |
| 87 DCHECK(start_callback_); | |
| 88 DCHECK(shared_memory); | |
| 89 DCHECK(foreign_socket); | |
| 90 | |
| 91 base::SharedMemoryHandle foreign_memory_handle = | |
| 92 base::SharedMemory::DuplicateHandle(shared_memory->handle()); | |
| 93 DCHECK(base::SharedMemory::IsHandleValid(foreign_memory_handle)); | |
| 94 | |
| 95 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( | |
| 96 foreign_memory_handle, shared_memory->requested_size(), false); | |
| 97 mojo::ScopedHandle socket_handle = | |
| 98 mojo::WrapPlatformFile(foreign_socket->handle()); | |
| 99 | |
| 100 DCHECK(buffer_handle.is_valid()); | |
| 101 DCHECK(socket_handle.is_valid()); | |
| 102 | |
| 103 start_callback_.Run(std::move(buffer_handle), std::move(socket_handle)); | |
|
DaleCurtis
2017/02/21 18:18:32
base::ResetAndReturn?
Max Morin
2017/02/22 10:08:43
Done.
| |
| 104 start_callback_ = StartCallback(); | |
| 105 } | |
| 106 | |
| 107 void MojoAudioOutput::OnStreamError(int stream_id) { | |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 109 OnError(); | |
| 110 } | |
| 111 | |
| 112 void MojoAudioOutput::OnError() { | |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 114 if (!finished_callback_) { | |
|
DaleCurtis
2017/02/21 18:18:32
Hmm, this is relying on finished_callback_ being n
Max Morin
2017/02/22 10:08:43
I'm pretty sure it's null after being moved from,
o1ka
2017/02/22 13:26:30
Hmm... |finished_callback_| acts as a deleter for
Max Morin
2017/03/02 23:11:32
Renamed to deleter_.
| |
| 115 // There is already a destruction in progress. | |
| 116 return; | |
| 117 } | |
| 118 // Notify the renderer. | |
| 119 binding_.Close(); | |
| 120 // The rest of the cleanup is done when |this| is destructed. | |
| 121 std::move(finished_callback_).Run(this); | |
| 122 } | |
| 123 | |
| 124 } // namespace media | |
| OLD | NEW |