Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / remoting / base / util.cc
blob66a70eae537ada3877bc9883cfe463576b23a4a8
1 // Copyright (c) 2012 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.
5 #include "remoting/base/util.h"
7 #include <math.h>
9 #include "base/logging.h"
10 #include "base/stringprintf.h"
11 #include "base/time.h"
12 #include "media/base/video_frame.h"
13 #include "media/base/yuv_convert.h"
14 #include "third_party/skia/include/core/SkRegion.h"
16 #if defined(OS_POSIX)
17 #include <pwd.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #endif // defined(OS_POSIX)
22 using media::VideoFrame;
24 namespace remoting {
26 enum { kBytesPerPixelRGB32 = 4 };
28 // Do not write LOG messages in this routine since it is called from within
29 // our LOG message handler. Bad things will happen.
30 std::string GetTimestampString() {
31 base::Time t = base::Time::NowFromSystemTime();
32 base::Time::Exploded tex;
33 t.LocalExplode(&tex);
34 return base::StringPrintf("%02d%02d/%02d%02d%02d:",
35 tex.month, tex.day_of_month,
36 tex.hour, tex.minute, tex.second);
39 int CalculateRGBOffset(int x, int y, int stride) {
40 return stride * y + kBytesPerPixelRGB32 * x;
43 int CalculateYOffset(int x, int y, int stride) {
44 DCHECK(((x & 1) == 0) && ((y & 1) == 0));
45 return stride * y + x;
48 int CalculateUVOffset(int x, int y, int stride) {
49 DCHECK(((x & 1) == 0) && ((y & 1) == 0));
50 return stride * y / 2 + x / 2;
53 void ConvertRGB32ToYUVWithRect(const uint8* rgb_plane,
54 uint8* y_plane,
55 uint8* u_plane,
56 uint8* v_plane,
57 int x,
58 int y,
59 int width,
60 int height,
61 int rgb_stride,
62 int y_stride,
63 int uv_stride) {
64 int rgb_offset = CalculateRGBOffset(x, y, rgb_stride);
65 int y_offset = CalculateYOffset(x, y, y_stride);
66 int uv_offset = CalculateUVOffset(x, y, uv_stride);;
68 media::ConvertRGB32ToYUV(rgb_plane + rgb_offset,
69 y_plane + y_offset,
70 u_plane + uv_offset,
71 v_plane + uv_offset,
72 width,
73 height,
74 rgb_stride,
75 y_stride,
76 uv_stride);
79 void ConvertAndScaleYUVToRGB32Rect(const uint8* source_yplane,
80 const uint8* source_uplane,
81 const uint8* source_vplane,
82 int source_ystride,
83 int source_uvstride,
84 const SkISize& source_size,
85 const SkIRect& source_buffer_rect,
86 uint8* dest_buffer,
87 int dest_stride,
88 const SkISize& dest_size,
89 const SkIRect& dest_buffer_rect,
90 const SkIRect& dest_rect) {
91 // N.B. It is caller's responsibility to check if strides are large enough. We
92 // cannot do it here anyway.
93 DCHECK(SkIRect::MakeSize(source_size).contains(source_buffer_rect));
94 DCHECK(SkIRect::MakeSize(dest_size).contains(dest_buffer_rect));
95 DCHECK(dest_buffer_rect.contains(dest_rect));
96 DCHECK(ScaleRect(source_buffer_rect, source_size, dest_size).
97 contains(dest_rect));
99 // If the source and/or destination buffers don't start at (0, 0)
100 // offset the pointers to pretend we have complete buffers.
101 int y_offset = - CalculateYOffset(source_buffer_rect.x(),
102 source_buffer_rect.y(),
103 source_ystride);
104 int uv_offset = - CalculateUVOffset(source_buffer_rect.x(),
105 source_buffer_rect.y(),
106 source_uvstride);
107 int rgb_offset = - CalculateRGBOffset(dest_buffer_rect.x(),
108 dest_buffer_rect.y(),
109 dest_stride);
111 // See if scaling is needed.
112 if (source_size == dest_size) {
113 // Calculate the inner rectangle that can be copied by the optimized
114 // ConvertYUVToRGB32().
115 SkIRect inner_rect =
116 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left() + 1),
117 RoundToTwosMultiple(dest_rect.top() + 1),
118 dest_rect.right(),
119 dest_rect.bottom());
121 // Offset pointers to point to the top left corner of the inner rectangle.
122 y_offset += CalculateYOffset(inner_rect.x(), inner_rect.y(),
123 source_ystride);
124 uv_offset += CalculateUVOffset(inner_rect.x(), inner_rect.y(),
125 source_uvstride);
126 rgb_offset += CalculateRGBOffset(inner_rect.x(), inner_rect.y(),
127 dest_stride);
129 media::ConvertYUVToRGB32(source_yplane + y_offset,
130 source_uplane + uv_offset,
131 source_vplane + uv_offset,
132 dest_buffer + rgb_offset,
133 inner_rect.width(),
134 inner_rect.height(),
135 source_ystride,
136 source_uvstride,
137 dest_stride,
138 media::YV12);
140 // Now see if some pixels weren't copied due to alignment.
141 if (dest_rect != inner_rect) {
142 SkIRect outer_rect =
143 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left()),
144 RoundToTwosMultiple(dest_rect.top()),
145 dest_rect.right(),
146 dest_rect.bottom());
148 SkIPoint offset = SkIPoint::Make(outer_rect.x() - inner_rect.x(),
149 outer_rect.y() - inner_rect.y());
151 // Offset the pointers to point to the top left corner of the outer
152 // rectangle.
153 y_offset += CalculateYOffset(offset.x(), offset.y(), source_ystride);
154 uv_offset += CalculateUVOffset(offset.x(), offset.y(), source_uvstride);
155 rgb_offset += CalculateRGBOffset(offset.x(), offset.y(), dest_stride);
157 // Draw unaligned edges.
158 SkRegion edges(dest_rect);
159 edges.op(inner_rect, SkRegion::kDifference_Op);
160 for (SkRegion::Iterator i(edges); !i.done(); i.next()) {
161 SkIRect rect(i.rect());
162 rect.offset(- outer_rect.left(), - outer_rect.top());
163 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset,
164 source_uplane + uv_offset,
165 source_vplane + uv_offset,
166 dest_buffer + rgb_offset,
167 source_size.width(),
168 source_size.height(),
169 dest_size.width(),
170 dest_size.height(),
171 rect.left(),
172 rect.top(),
173 rect.right(),
174 rect.bottom(),
175 source_ystride,
176 source_uvstride,
177 dest_stride);
180 } else {
181 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset,
182 source_uplane + uv_offset,
183 source_vplane + uv_offset,
184 dest_buffer + rgb_offset,
185 source_size.width(),
186 source_size.height(),
187 dest_size.width(),
188 dest_size.height(),
189 dest_rect.left(),
190 dest_rect.top(),
191 dest_rect.right(),
192 dest_rect.bottom(),
193 source_ystride,
194 source_uvstride,
195 dest_stride);
199 int RoundToTwosMultiple(int x) {
200 return x & (~1);
203 SkIRect AlignRect(const SkIRect& rect) {
204 int x = RoundToTwosMultiple(rect.left());
205 int y = RoundToTwosMultiple(rect.top());
206 int right = RoundToTwosMultiple(rect.right() + 1);
207 int bottom = RoundToTwosMultiple(rect.bottom() + 1);
208 return SkIRect::MakeLTRB(x, y, right, bottom);
211 SkIRect ScaleRect(const SkIRect& rect,
212 const SkISize& in_size,
213 const SkISize& out_size) {
214 int left = (rect.left() * out_size.width()) / in_size.width();
215 int top = (rect.top() * out_size.height()) / in_size.height();
216 int right = (rect.right() * out_size.width() + in_size.width() - 1) /
217 in_size.width();
218 int bottom = (rect.bottom() * out_size.height() + in_size.height() - 1) /
219 in_size.height();
220 return SkIRect::MakeLTRB(left, top, right, bottom);
223 void CopyRGB32Rect(const uint8* source_buffer,
224 int source_stride,
225 const SkIRect& source_buffer_rect,
226 uint8* dest_buffer,
227 int dest_stride,
228 const SkIRect& dest_buffer_rect,
229 const SkIRect& dest_rect) {
230 DCHECK(dest_buffer_rect.contains(dest_rect));
231 DCHECK(source_buffer_rect.contains(dest_rect));
233 // Get the address of the starting point.
234 source_buffer += CalculateRGBOffset(dest_rect.x() - source_buffer_rect.x(),
235 dest_rect.y() - source_buffer_rect.y(),
236 source_stride);
237 dest_buffer += CalculateRGBOffset(dest_rect.x() - dest_buffer_rect.x(),
238 dest_rect.y() - dest_buffer_rect.y(),
239 source_stride);
241 // Copy pixels in the rectangle line by line.
242 const int bytes_per_line = kBytesPerPixelRGB32 * dest_rect.width();
243 for (int i = 0 ; i < dest_rect.height(); ++i) {
244 memcpy(dest_buffer, source_buffer, bytes_per_line);
245 source_buffer += source_stride;
246 dest_buffer += dest_stride;
250 std::string ReplaceLfByCrLf(const std::string& in) {
251 std::string out;
252 out.resize(2 * in.size());
253 char* out_p_begin = &out[0];
254 char* out_p = out_p_begin;
255 const char* in_p_begin = &in[0];
256 const char* in_p_end = &in[in.size()];
257 for (const char* in_p = in_p_begin; in_p < in_p_end; ++in_p) {
258 char c = *in_p;
259 if (c == '\n') {
260 *out_p++ = '\r';
262 *out_p++ = c;
264 out.resize(out_p - out_p_begin);
265 return out;
268 std::string ReplaceCrLfByLf(const std::string& in) {
269 std::string out;
270 out.resize(in.size());
271 char* out_p_begin = &out[0];
272 char* out_p = out_p_begin;
273 const char* in_p_begin = &in[0];
274 const char* in_p_end = &in[in.size()];
275 for (const char* in_p = in_p_begin; in_p < in_p_end; ++in_p) {
276 char c = *in_p;
277 if ((c == '\r') && (in_p + 1 < in_p_end) && (*(in_p + 1) == '\n')) {
278 *out_p++ = '\n';
279 ++in_p;
280 } else {
281 *out_p++ = c;
284 out.resize(out_p - out_p_begin);
285 return out;
288 bool StringIsUtf8(const char* data, size_t length) {
289 const char* ptr = data;
290 const char* ptr_end = data + length;
291 while (ptr != ptr_end) {
292 if ((*ptr & 0x80) == 0) {
293 // Single-byte symbol.
294 ++ptr;
295 } else if ((*ptr & 0xc0) == 0x80 || (*ptr & 0xfe) == 0xfe) {
296 // Invalid first byte.
297 return false;
298 } else {
299 // First byte of a multi-byte symbol. The bits from 2 to 6 are the count
300 // of continuation bytes (up to 5 of them).
301 for (char first = *ptr << 1; first & 0x80; first <<= 1) {
302 ++ptr;
304 // Missing continuation byte.
305 if (ptr == ptr_end)
306 return false;
308 // Invalid continuation byte.
309 if ((*ptr & 0xc0) != 0x80)
310 return false;
313 ++ptr;
317 return true;
320 std::string GetUsername() {
321 #if defined(OS_POSIX)
322 long buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
323 if (buf_size <= 0)
324 return std::string();
325 scoped_ptr<char[]> buf(new char[buf_size]);
326 struct passwd passwd;
327 struct passwd* passwd_result = NULL;
328 getpwuid_r(getuid(), &passwd, buf.get(), buf_size, &passwd_result);
329 if (!passwd_result)
330 return std::string();
331 return std::string(passwd_result->pw_name);
332 #else // !defined(OS_POSIX)
333 return std::string();
334 #endif // defined(OS_POSIX)
337 } // namespace remoting