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

Side by Side Diff: chromecast/media/cma/ipc/media_message.cc

Issue 2300993003: CmaRenderer is dead. Long live MojoRenderer. (Closed)
Patch Set: Created 4 years, 3 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 2014 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 "chromecast/media/cma/ipc/media_message.h"
6
7 #include <limits>
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "chromecast/media/cma/ipc/media_memory_chunk.h"
12
13 namespace chromecast {
14 namespace media {
15
16 // static
17 std::unique_ptr<MediaMessage> MediaMessage::CreateDummyMessage(uint32_t type) {
18 return std::unique_ptr<MediaMessage>(
19 new MediaMessage(type, std::numeric_limits<size_t>::max()));
20 }
21
22 // static
23 std::unique_ptr<MediaMessage> MediaMessage::CreateMessage(
24 uint32_t type,
25 const MemoryAllocatorCB& memory_allocator,
26 size_t msg_content_capacity) {
27 size_t msg_size = minimum_msg_size() + msg_content_capacity;
28
29 // Make the message size a multiple of the alignment
30 // so that if we have proper alignment for array of messages.
31 size_t end_alignment = msg_size % ALIGNOF(SerializedMsg);
32 if (end_alignment != 0)
33 msg_size += ALIGNOF(SerializedMsg) - end_alignment;
34
35 std::unique_ptr<MediaMemoryChunk> memory(memory_allocator.Run(msg_size));
36 if (!memory)
37 return std::unique_ptr<MediaMessage>();
38
39 return std::unique_ptr<MediaMessage>(
40 new MediaMessage(type, std::move(memory)));
41 }
42
43 // static
44 std::unique_ptr<MediaMessage> MediaMessage::CreateMessage(
45 uint32_t type,
46 std::unique_ptr<MediaMemoryChunk> memory) {
47 return std::unique_ptr<MediaMessage>(
48 new MediaMessage(type, std::move(memory)));
49 }
50
51 // static
52 std::unique_ptr<MediaMessage> MediaMessage::MapMessage(
53 std::unique_ptr<MediaMemoryChunk> memory) {
54 return std::unique_ptr<MediaMessage>(new MediaMessage(std::move(memory)));
55 }
56
57 MediaMessage::MediaMessage(uint32_t type, size_t msg_size)
58 : is_dummy_msg_(true),
59 cached_header_(&cached_msg_.header),
60 msg_(&cached_msg_),
61 msg_read_only_(&cached_msg_),
62 rd_offset_(0) {
63 cached_header_->size = msg_size;
64 cached_header_->type = type;
65 cached_header_->content_size = 0;
66 }
67
68 MediaMessage::MediaMessage(uint32_t type,
69 std::unique_ptr<MediaMemoryChunk> memory)
70 : is_dummy_msg_(false),
71 cached_header_(&cached_msg_.header),
72 msg_(static_cast<SerializedMsg*>(memory->data())),
73 msg_read_only_(msg_),
74 mem_(std::move(memory)),
75 rd_offset_(0) {
76 CHECK(mem_->valid());
77 CHECK_GE(mem_->size(), minimum_msg_size());
78
79 // Check memory alignment:
80 // needed to cast properly |msg_dst| to a SerializedMsg.
81 CHECK_EQ(
82 reinterpret_cast<uintptr_t>(mem_->data()) % ALIGNOF(SerializedMsg), 0u);
83
84 // Make sure that |mem_->data()| + |mem_->size()| is also aligned correctly.
85 // This is needed if we append a second serialized message next to this one.
86 // The second serialized message must be aligned correctly.
87 // It is similar to what a compiler is doing for arrays of structures.
88 CHECK_EQ(mem_->size() % ALIGNOF(SerializedMsg), 0u);
89
90 cached_header_->size = mem_->size();
91 cached_header_->type = type;
92 cached_header_->content_size = 0;
93 msg_->header = *cached_header_;
94 }
95
96 MediaMessage::MediaMessage(std::unique_ptr<MediaMemoryChunk> memory)
97 : is_dummy_msg_(false),
98 cached_header_(&cached_msg_.header),
99 msg_(NULL),
100 msg_read_only_(static_cast<SerializedMsg*>(memory->data())),
101 mem_(std::move(memory)),
102 rd_offset_(0) {
103 CHECK(mem_->valid());
104
105 // Check memory alignment.
106 CHECK_EQ(
107 reinterpret_cast<uintptr_t>(mem_->data()) % ALIGNOF(SerializedMsg), 0u);
108
109 // Cache the message header which cannot be modified while reading.
110 CHECK_GE(mem_->size(), minimum_msg_size());
111 *cached_header_ = msg_read_only_->header;
112 CHECK_GE(cached_header_->size, minimum_msg_size());
113
114 // Make sure if we have 2 consecutive serialized messages in memory,
115 // the 2nd message is also aligned correctly.
116 CHECK_EQ(cached_header_->size % ALIGNOF(SerializedMsg), 0u);
117
118 size_t max_content_size = cached_header_->size - minimum_msg_size();
119 CHECK_LE(cached_header_->content_size, max_content_size);
120 }
121
122 MediaMessage::~MediaMessage() {
123 }
124
125 bool MediaMessage::IsSerializedMsgAvailable() const {
126 return !is_dummy_msg_ && mem_->valid();
127 }
128
129 bool MediaMessage::WriteBuffer(const void* src, size_t size) {
130 // No message to write into.
131 if (!msg_)
132 return false;
133
134 // The underlying memory was invalidated.
135 if (!is_dummy_msg_ && !mem_->valid())
136 return false;
137
138 size_t max_content_size = cached_header_->size - minimum_msg_size();
139 if (cached_header_->content_size + size > max_content_size) {
140 cached_header_->content_size = max_content_size;
141 msg_->header.content_size = cached_header_->content_size;
142 return false;
143 }
144
145 // Write the message only for non-dummy messages.
146 if (!is_dummy_msg_) {
147 uint8_t* wr_ptr = &msg_->content + cached_header_->content_size;
148 memcpy(wr_ptr, src, size);
149 }
150
151 cached_header_->content_size += size;
152 msg_->header.content_size = cached_header_->content_size;
153 return true;
154 }
155
156 bool MediaMessage::ReadBuffer(void* dst, size_t size) {
157 // No read possible for a dummy message.
158 if (is_dummy_msg_)
159 return false;
160
161 // The underlying memory was invalidated.
162 if (!mem_->valid())
163 return false;
164
165 if (rd_offset_ + size > cached_header_->content_size) {
166 rd_offset_ = cached_header_->content_size;
167 return false;
168 }
169
170 const uint8_t* rd_ptr = &msg_read_only_->content + rd_offset_;
171 memcpy(dst, rd_ptr, size);
172 rd_offset_ += size;
173 return true;
174 }
175
176 void* MediaMessage::GetWritableBuffer(size_t size) {
177 // No read possible for a dummy message.
178 if (is_dummy_msg_)
179 return NULL;
180
181 // The underlying memory was invalidated.
182 if (!mem_->valid())
183 return NULL;
184
185 if (rd_offset_ + size > cached_header_->content_size) {
186 rd_offset_ = cached_header_->content_size;
187 return NULL;
188 }
189
190 uint8_t* rd_ptr = &msg_read_only_->content + rd_offset_;
191 rd_offset_ += size;
192 return rd_ptr;
193 }
194
195 const void* MediaMessage::GetBuffer(size_t size) {
196 return GetWritableBuffer(size);
197 }
198
199 } // namespace media
200 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698