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/callback_helpers.h" | |
11 #include "base/memory/shared_memory.h" | |
12 #include "base/sync_socket.h" | |
13 #include "mojo/public/cpp/system/platform_handle.h" | |
14 | |
15 namespace media { | |
16 | |
17 MojoAudioOutput::MojoAudioOutput( | |
18 mojom::AudioOutputRequest request, | |
19 CreateDelegateCallback create_delegate_callback, | |
20 FinishedCallback finished_callback) | |
21 : binding_(this, std::move(request)), | |
22 create_delegate_callback_(std::move(create_delegate_callback)), | |
23 finished_callback_(std::move(finished_callback)) { | |
24 DCHECK(thread_checker_.CalledOnValidThread()); | |
25 DCHECK(finished_callback_); | |
26 // |this| owns |binding_|, so unretained is safe. | |
27 binding_.set_connection_error_handler( | |
28 base::Bind(&MojoAudioOutput::OnError, base::Unretained(this))); | |
29 } | |
30 | |
31 MojoAudioOutput::~MojoAudioOutput() { | |
32 DCHECK(thread_checker_.CalledOnValidThread()); | |
33 } | |
34 | |
35 void MojoAudioOutput::Initialize(const AudioParameters& params, | |
36 const InitializeCallback& callback) { | |
37 DCHECK(thread_checker_.CalledOnValidThread()); | |
38 if (delegate_) { | |
39 // Already initialized. | |
40 OnError(); | |
41 return; | |
42 } | |
43 delegate_ = std::move(create_delegate_callback_).Run(this, params); | |
44 DCHECK(delegate_); | |
45 initialize_callback_ = callback; | |
o1ka
2017/02/22 13:26:31
Add a comment that it will be called when OnStream
Max Morin
2017/03/02 23:11:32
Done.
| |
46 } | |
47 | |
48 void MojoAudioOutput::Play() { | |
49 DCHECK(thread_checker_.CalledOnValidThread()); | |
50 if (!delegate_) { | |
51 // Not initialized yet. | |
52 OnError(); | |
53 return; | |
54 } | |
55 delegate_->OnPlayStream(); | |
56 } | |
57 | |
58 void MojoAudioOutput::Pause() { | |
59 DCHECK(thread_checker_.CalledOnValidThread()); | |
60 if (!delegate_) { | |
61 // Not initialized yet. | |
62 OnError(); | |
63 return; | |
64 } | |
65 delegate_->OnPauseStream(); | |
66 } | |
67 | |
68 void MojoAudioOutput::SetVolume(double volume) { | |
69 DCHECK(thread_checker_.CalledOnValidThread()); | |
70 if (!delegate_ || volume < 0 || volume > 1) { | |
71 // Not initialized yet. | |
72 OnError(); | |
73 return; | |
74 } | |
75 delegate_->OnSetVolume(volume); | |
76 } | |
77 | |
78 void MojoAudioOutput::OnStreamCreated( | |
79 int stream_id, | |
80 base::SharedMemory* shared_memory, | |
81 base::CancelableSyncSocket* foreign_socket) { | |
82 DCHECK(thread_checker_.CalledOnValidThread()); | |
83 if (!binding_.is_bound()) { | |
84 // Already closed, make sure we are properly cleaned up. | |
85 OnError(); | |
86 return; | |
87 } | |
88 DCHECK(initialize_callback_); | |
89 DCHECK(shared_memory); | |
90 DCHECK(foreign_socket); | |
91 | |
92 base::SharedMemoryHandle foreign_memory_handle = | |
93 base::SharedMemory::DuplicateHandle(shared_memory->handle()); | |
94 DCHECK(base::SharedMemory::IsHandleValid(foreign_memory_handle)); | |
95 | |
96 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( | |
97 foreign_memory_handle, shared_memory->requested_size(), false); | |
98 mojo::ScopedHandle socket_handle = | |
99 mojo::WrapPlatformFile(foreign_socket->handle()); | |
100 | |
101 DCHECK(buffer_handle.is_valid()); | |
102 DCHECK(socket_handle.is_valid()); | |
103 | |
104 base::ResetAndReturn(&initialize_callback_) | |
105 .Run(std::move(buffer_handle), std::move(socket_handle)); | |
106 } | |
107 | |
108 void MojoAudioOutput::OnStreamError(int stream_id) { | |
109 DCHECK(thread_checker_.CalledOnValidThread()); | |
110 OnError(); | |
111 } | |
112 | |
113 void MojoAudioOutput::OnError() { | |
114 DCHECK(thread_checker_.CalledOnValidThread()); | |
115 DCHECK(finished_callback_); | |
116 // Destroys |this|. | |
117 std::move(finished_callback_).Run(this); | |
118 } | |
119 | |
120 } // namespace media | |
OLD | NEW |