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

Side by Side Diff: media/mojo/services/mojo_audio_output_stream.cc

Issue 2697793002: Add mojo interface+impl for audio stream control. (Closed)
Patch Set: Don't inline AudioOutputDelegate(EventHandler) dtor. Created 3 years, 9 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 2017 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_stream.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 MojoAudioOutputStream::MojoAudioOutputStream(
18 mojom::AudioOutputStreamRequest request,
19 CreateDelegateCallback create_delegate_callback,
20 StreamCreatedCallback stream_created_callback,
21 base::OnceClosure deleter_callback)
22 : stream_created_callback_(std::move(stream_created_callback)),
23 deleter_callback_(std::move(deleter_callback)),
24 binding_(this, std::move(request)) {
25 DCHECK(thread_checker_.CalledOnValidThread());
26 DCHECK(deleter_callback_);
27 // |this| owns |binding_|, so unretained is safe.
28 binding_.set_connection_error_handler(
29 base::Bind(&MojoAudioOutputStream::OnError, base::Unretained(this)));
30 delegate_ = std::move(create_delegate_callback).Run(this);
31 }
32
33 MojoAudioOutputStream::~MojoAudioOutputStream() {
34 DCHECK(thread_checker_.CalledOnValidThread());
35 }
36
37 void MojoAudioOutputStream::Play() {
38 DCHECK(thread_checker_.CalledOnValidThread());
39 delegate_->OnPlayStream();
40 }
41
42 void MojoAudioOutputStream::Pause() {
43 DCHECK(thread_checker_.CalledOnValidThread());
44 delegate_->OnPauseStream();
45 }
46
47 void MojoAudioOutputStream::SetVolume(double volume) {
48 DCHECK(thread_checker_.CalledOnValidThread());
49 if (volume < 0 || volume > 1) {
50 LOG(ERROR) << "MojoAudioOutputStream::SetVolume(" << volume
51 << ") out of range.";
52 OnError();
53 return;
54 }
55 delegate_->OnSetVolume(volume);
56 }
57
58 void MojoAudioOutputStream::OnStreamCreated(
59 int stream_id,
60 base::SharedMemory* shared_memory,
61 base::CancelableSyncSocket* foreign_socket) {
62 DCHECK(thread_checker_.CalledOnValidThread());
63 DCHECK(stream_created_callback_);
64 DCHECK(shared_memory);
65 DCHECK(foreign_socket);
66
67 base::SharedMemoryHandle foreign_memory_handle =
68 base::SharedMemory::DuplicateHandle(shared_memory->handle());
69 DCHECK(base::SharedMemory::IsHandleValid(foreign_memory_handle));
70
71 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle(
72 foreign_memory_handle, shared_memory->requested_size(), false);
73 mojo::ScopedHandle socket_handle =
74 mojo::WrapPlatformFile(foreign_socket->handle());
75
76 DCHECK(buffer_handle.is_valid());
77 DCHECK(socket_handle.is_valid());
78
79 base::ResetAndReturn(&stream_created_callback_)
80 .Run(std::move(buffer_handle), std::move(socket_handle));
81 }
82
83 void MojoAudioOutputStream::OnStreamError(int stream_id) {
84 DCHECK(thread_checker_.CalledOnValidThread());
85 OnError();
86 }
87
88 void MojoAudioOutputStream::OnError() {
89 DCHECK(thread_checker_.CalledOnValidThread());
90 DCHECK(deleter_callback_);
91 std::move(deleter_callback_).Run(); // Deletes |this|.
92 }
93
94 } // namespace media
OLDNEW
« no previous file with comments | « media/mojo/services/mojo_audio_output_stream.h ('k') | media/mojo/services/mojo_audio_output_stream_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698