Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: content/browser/renderer_host/media/audio_output_impl.cc

Issue 2319493002: Add mojo interface for audio rendering. (Closed)
Patch Set: Refactor factory. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "content/public/browser/browser_thread.h"
13 #include "mojo/public/cpp/system/platform_handle.h"
14
15 namespace content {
16
17 AudioOutputImpl::AudioOutputImpl(
18 media::mojom::AudioOutputRequest request,
19 FinishedCallback finished_callback,
20 DelegateFactoryCallback delegate_factory_callback)
21 : delegate_factory_callback_(std::move(delegate_factory_callback)),
22 finished_callback_(std::move(finished_callback)),
23 binding_(this, std::move(request)) {
24 DCHECK_CURRENTLY_ON(BrowserThread::IO);
25 DCHECK(!delegate_factory_callback_.is_null());
26 DCHECK(!finished_callback_.is_null());
27 // |this| owns |binding_|, so unretained is safe.
28 binding_.set_connection_error_handler(
29 base::Bind(&AudioOutputImpl::OnError, base::Unretained(this)));
30 }
31
32 AudioOutputImpl::~AudioOutputImpl() {
33 DCHECK_CURRENTLY_ON(BrowserThread::IO);
34 }
35
36 void AudioOutputImpl::Start(const media::AudioParameters& params,
37 const StartCallback& callback) {
38 DCHECK_CURRENTLY_ON(BrowserThread::IO);
39 if (delegate_) {
40 // Already started.
41 OnError();
42 return;
43 }
44 // Buffer up all calls from the renderer until we are done starting.
45 binding_.PauseIncomingMethodCallProcessing();
46 delegate_ = std::move(delegate_factory_callback_).Run(this, params);
47 DCHECK(delegate_);
48 start_callback_ = callback;
49 }
50
51 void AudioOutputImpl::Play() {
52 DCHECK_CURRENTLY_ON(BrowserThread::IO);
53 if (!delegate_) {
54 // Not started yet.
55 OnError();
56 return;
57 }
58 delegate_->OnPlayStream();
59 }
60
61 void AudioOutputImpl::Pause() {
62 DCHECK_CURRENTLY_ON(BrowserThread::IO);
63 if (!delegate_) {
64 // Not started yet.
65 OnError();
66 return;
67 }
68 delegate_->OnPauseStream();
69 }
70
71 void AudioOutputImpl::SetVolume(double volume) {
72 DCHECK_CURRENTLY_ON(BrowserThread::IO);
73 if (!delegate_) {
74 // Not started yet.
75 OnError();
76 return;
77 }
78 if (volume < 0 || volume > 1)
o1ka 2017/01/25 15:15:21 Where the limitation comes from? a comment is nee
79 return;
80 delegate_->OnSetVolume(volume);
81 }
82
83 void AudioOutputImpl::OnStreamStateChanged(bool playing) {}
84
85 void AudioOutputImpl::OnStreamCreated(
86 int stream_id,
87 base::SharedMemory* shared_memory,
88 base::CancelableSyncSocket* foreign_socket) {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO);
90 if (!binding_.is_bound()) {
91 // Already closed, make sure we are properly cleaned up.
92 OnError();
93 return;
94 }
95 DCHECK(shared_memory);
96 DCHECK(foreign_socket);
97
98 // TODO: Share memory/socket correctly.
99 base::SharedMemoryHandle foreign_memory_handle =
100 base::SharedMemory::DuplicateHandle(shared_memory->handle());
101 DCHECK(base::SharedMemory::IsHandleValid(foreign_memory_handle))
102 << "Invalid memory handle.";
103 mojo::ScopedSharedBufferHandle shared_buffer_handle =
104 mojo::WrapSharedMemoryHandle(foreign_memory_handle,
105 shared_memory->requested_size(), false);
106
107 DCHECK(shared_buffer_handle.is_valid());
108
109 start_callback_.Run(std::move(shared_buffer_handle),
110 mojo::WrapPlatformFile(foreign_socket->handle()));
111 start_callback_ = StartCallback();
112 binding_.ResumeIncomingMethodCallProcessing();
113 }
114
115 void AudioOutputImpl::OnStreamError(int stream_id) {
116 DCHECK_CURRENTLY_ON(BrowserThread::IO);
117 OnError();
118 }
119
120 void AudioOutputImpl::OnError() {
121 DCHECK_CURRENTLY_ON(BrowserThread::IO);
122 if (finished_callback_.is_null()) {
123 // There is already a destruction in progress.
124 return;
125 }
126 // Notify the renderer.
127 binding_.Close();
128 // The rest of the cleanup is done when |handler_| destructs |this|.
129 std::move(finished_callback_).Run(this);
130 }
131
132 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698