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

Side by Side Diff: services/media/common/media_pipe_base.cc

Issue 1694963002: Change who allocated the MediaPipe's shared buffer. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Fix android trybots Created 4 years, 10 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
« no previous file with comments | « services/media/common/media_pipe_base.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/logging.h" 5 #include "base/logging.h"
6 #include "services/media/common/media_pipe_base.h" 6 #include "services/media/common/media_pipe_base.h"
7 7
8 namespace mojo { 8 namespace mojo {
9 namespace media { 9 namespace media {
10 10
11 MediaPipeBase::MediaPipeBase() 11 MediaPipeBase::MediaPipeBase()
12 : binding_(this) { 12 : binding_(this) {
13 } 13 }
14 14
15 MediaPipeBase::~MediaPipeBase() { 15 MediaPipeBase::~MediaPipeBase() {
16 } 16 }
17 17
18 MojoResult MediaPipeBase::Init(InterfaceRequest<MediaPipe> request, 18 MojoResult MediaPipeBase::Init(InterfaceRequest<MediaPipe> request) {
19 uint64_t shared_buffer_size) {
20 // Double init? 19 // Double init?
21 if (IsInitialized()) { 20 if (IsInitialized()) {
22 return MOJO_RESULT_ALREADY_EXISTS; 21 return MOJO_RESULT_ALREADY_EXISTS;
23 } 22 }
24 23
25 // Valid size?
26 DCHECK_GT(shared_buffer_size, 0u);
27 DCHECK_LE(shared_buffer_size, MediaPipeState::kMaxPayloadLen);
28 if (!shared_buffer_size ||
29 (shared_buffer_size > MediaPipeState::kMaxPayloadLen)) {
30 return MOJO_RESULT_INVALID_ARGUMENT;
31 }
32
33 DCHECK(!buffer_);
34 buffer_ = MappedSharedBuffer::Create(shared_buffer_size);
35 if (buffer_ == nullptr) {
36 return MOJO_RESULT_UNKNOWN;
37 }
38
39 binding_.Bind(request.Pass()); 24 binding_.Bind(request.Pass());
40 binding_.set_connection_error_handler([this]() -> void { 25 binding_.set_connection_error_handler([this]() -> void {
41 Reset(); 26 Reset();
42 }); 27 });
43 return MOJO_RESULT_OK; 28 return MOJO_RESULT_OK;
44 } 29 }
45 30
46 bool MediaPipeBase::IsInitialized() const { 31 bool MediaPipeBase::IsInitialized() const {
47 DCHECK((!binding_.is_bound() && (buffer_ == nullptr)) || 32 return binding_.is_bound();
48 (binding_.is_bound() && (buffer_ != nullptr)));
49 return !!buffer_;
50 } 33 }
51 34
52 void MediaPipeBase::Reset() { 35 void MediaPipeBase::Reset() {
53 if (binding_.is_bound()) { 36 if (binding_.is_bound()) {
54 binding_.Close(); 37 binding_.Close();
55 } 38 }
56 buffer_ = nullptr; 39 buffer_ = nullptr;
57 } 40 }
58 41
59 void MediaPipeBase::GetState(const GetStateCallback& cbk) { 42 void MediaPipeBase::SetBuffer(ScopedSharedBufferHandle handle, uint64_t size) {
60 static const MojoDuplicateBufferHandleOptions options = { 43 // Double init? Close the connection.
61 .struct_size = sizeof(*this), 44 if (buffer_) {
62 .flags = MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE, 45 LOG(ERROR) << "Attempting to set a new buffer (size = "
63 }; 46 << size
64 47 << ") on a MediaPipe which already has a buffer (size = "
65 MediaPipeStatePtr state_ptr(MediaPipeState::New()); 48 << buffer_->size()
66 49 << ")";
67 // If we have not been successfully initialized, send back an invalid handle 50 Reset();
68 // and a zero length. 51 return;
69 if (!buffer_) {
70 state_ptr->payload_buffer_len = 0;
71 } else {
72 MojoResult res = DuplicateBuffer(buffer_->handle().get(),
73 &options,
74 &state_ptr->payload_buffer);
75 state_ptr->payload_buffer_len = buffer_->size();
76 DCHECK(MOJO_RESULT_OK == res);
77 } 52 }
78 53
79 cbk.Run(state_ptr.Pass()); 54 // Invalid size? Close the connection.
55 if (!size || (size > MediaPipe::kMaxBufferLen)) {
56 LOG(ERROR) << "Invalid shared buffer size (size = " << size << ")";
57 Reset();
58 return;
59 }
60
61
62 // Failed to map the buffer? Close the connection.
63 buffer_ = MappedSharedBuffer::Create(handle.Pass(), size);
64 if (!buffer_) {
65 LOG(ERROR) << "Failed to map shared memory buffer (size = " << size << ")";
66 Reset();
67 return;
68 }
80 } 69 }
81 70
82 void MediaPipeBase::SendPacket(MediaPacketPtr packet, 71 void MediaPipeBase::SendPacket(MediaPacketPtr packet,
83 const SendPacketCallback& cbk) { 72 const SendPacketCallback& cbk) {
84 // If we have not been successfully initialized, then we should not be getting 73 // If we have not been successfully initialized, then we should not be getting
85 // packets pushed to us. 74 // packets pushed to us.
86 if (!buffer_) { 75 if (!buffer_) {
87 LOG(ERROR) << "SendPacket called with no shared buffer established!"; 76 LOG(ERROR) << "SendPacket called with no shared buffer established!";
88 Reset(); 77 Reset();
89 return; 78 return;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 MediaPipeBase::MediaPacketState::~MediaPacketState() { 149 MediaPipeBase::MediaPacketState::~MediaPacketState() {
161 cbk_.Run(result_); 150 cbk_.Run(result_);
162 } 151 }
163 152
164 void MediaPipeBase::MediaPacketState::SetResult(MediaPipe::SendResult result) { 153 void MediaPipeBase::MediaPacketState::SetResult(MediaPipe::SendResult result) {
165 MediaPipe::SendResult tmp = MediaPipe::SendResult::CONSUMED; 154 MediaPipe::SendResult tmp = MediaPipe::SendResult::CONSUMED;
166 result_.compare_exchange_strong(tmp, result); 155 result_.compare_exchange_strong(tmp, result);
167 } 156 }
168 157
169 MediaPipeBase::MappedSharedBufferPtr MediaPipeBase::MappedSharedBuffer::Create( 158 MediaPipeBase::MappedSharedBufferPtr MediaPipeBase::MappedSharedBuffer::Create(
170 size_t size) { 159 ScopedSharedBufferHandle handle,
171 MappedSharedBufferPtr ret(new MappedSharedBuffer(size)); 160 uint64_t size) {
161 MappedSharedBufferPtr ret(new MappedSharedBuffer(handle.Pass(), size));
172 return ret->base() ? ret : nullptr; 162 return ret->base() ? ret : nullptr;
173 } 163 }
174 164
175 MediaPipeBase::MappedSharedBuffer::~MappedSharedBuffer() { 165 MediaPipeBase::MappedSharedBuffer::~MappedSharedBuffer() {
176 if (nullptr != base_) { 166 if (nullptr != base_) {
177 MojoResult res = UnmapBuffer(base_); 167 MojoResult res = UnmapBuffer(base_);
178 CHECK(res == MOJO_RESULT_OK); 168 CHECK(res == MOJO_RESULT_OK);
179 } 169 }
180 } 170 }
181 171
182 MediaPipeBase::MappedSharedBuffer::MappedSharedBuffer(size_t size) 172 MediaPipeBase::MappedSharedBuffer::MappedSharedBuffer(
183 : size_(size) { 173 ScopedSharedBufferHandle handle,
184 static const MojoCreateSharedBufferOptions opt { 174 size_t size)
185 .struct_size = sizeof(MojoDuplicateBufferHandleOptions), 175 : handle_(handle.Pass()),
186 .flags = MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE, 176 size_(size) {
187 }; 177 MojoResult res = MapBuffer(handle_.get(),
188 MojoResult res; 178 0u,
179 size_,
180 &base_,
181 MOJO_MAP_BUFFER_FLAG_NONE);
189 182
190 res = CreateSharedBuffer(&opt, size_, &handle_); 183 if (MOJO_RESULT_OK != res) {
191 if (MOJO_RESULT_OK == res) { 184 LOG(ERROR) << "Failed to map shared buffer of size " << size_
192 // TODO(johngro) : We really only need read access to this buffer. Ideally,
193 // we could request that using flags, but there does not seem to be a way to
194 // do this right now.
195 res = MapBuffer(handle_.get(),
196 0u,
197 size_,
198 &base_,
199 MOJO_MAP_BUFFER_FLAG_NONE);
200
201 if (MOJO_RESULT_OK != res) {
202 LOG(ERROR) << "Failed to map shared buffer of size " << size_
203 << " (res " << res << ")";
204 DCHECK(base_ == nullptr);
205 handle_.reset();
206 }
207 } else {
208 LOG(ERROR) << "Failed to create shared buffer of size " << size_
209 << " (res " << res << ")"; 185 << " (res " << res << ")";
210 DCHECK(!handle_.is_valid()); 186 DCHECK(base_ == nullptr);
187 handle_.reset();
211 } 188 }
212 } 189 }
213 190
214 } // namespace media 191 } // namespace media
215 } // namespace mojo 192 } // namespace mojo
OLDNEW
« no previous file with comments | « services/media/common/media_pipe_base.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698