1 // Copyright 2013 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/host/chromoting_param_traits.h"
7 #include "base/strings/stringprintf.h"
8 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
13 void ParamTraits
<webrtc::DesktopVector
>::Write(Message
* m
,
14 const webrtc::DesktopVector
& p
) {
20 bool ParamTraits
<webrtc::DesktopVector
>::Read(const Message
* m
,
21 base::PickleIterator
* iter
,
22 webrtc::DesktopVector
* r
) {
24 if (!iter
->ReadInt(&x
) || !iter
->ReadInt(&y
))
26 *r
= webrtc::DesktopVector(x
, y
);
31 void ParamTraits
<webrtc::DesktopVector
>::Log(const webrtc::DesktopVector
& p
,
33 l
->append(base::StringPrintf("webrtc::DesktopVector(%d, %d)",
38 void ParamTraits
<webrtc::DesktopSize
>::Write(Message
* m
,
39 const webrtc::DesktopSize
& p
) {
40 m
->WriteInt(p
.width());
41 m
->WriteInt(p
.height());
45 bool ParamTraits
<webrtc::DesktopSize
>::Read(const Message
* m
,
46 base::PickleIterator
* iter
,
47 webrtc::DesktopSize
* r
) {
49 if (!iter
->ReadInt(&width
) || !iter
->ReadInt(&height
))
51 *r
= webrtc::DesktopSize(width
, height
);
56 void ParamTraits
<webrtc::DesktopSize
>::Log(const webrtc::DesktopSize
& p
,
58 l
->append(base::StringPrintf("webrtc::DesktopSize(%d, %d)",
59 p
.width(), p
.height()));
63 void ParamTraits
<webrtc::DesktopRect
>::Write(Message
* m
,
64 const webrtc::DesktopRect
& p
) {
65 m
->WriteInt(p
.left());
67 m
->WriteInt(p
.right());
68 m
->WriteInt(p
.bottom());
72 bool ParamTraits
<webrtc::DesktopRect
>::Read(const Message
* m
,
73 base::PickleIterator
* iter
,
74 webrtc::DesktopRect
* r
) {
75 int left
, right
, top
, bottom
;
76 if (!iter
->ReadInt(&left
) || !iter
->ReadInt(&top
) ||
77 !iter
->ReadInt(&right
) || !iter
->ReadInt(&bottom
)) {
80 *r
= webrtc::DesktopRect::MakeLTRB(left
, top
, right
, bottom
);
85 void ParamTraits
<webrtc::DesktopRect
>::Log(const webrtc::DesktopRect
& p
,
87 l
->append(base::StringPrintf("webrtc::DesktopRect(%d, %d, %d, %d)",
88 p
.left(), p
.top(), p
.right(), p
.bottom()));
92 void ParamTraits
<webrtc::MouseCursor
>::Write(
94 const webrtc::MouseCursor
& p
) {
95 ParamTraits
<webrtc::DesktopSize
>::Write(m
, p
.image()->size());
97 // Data is serialized in such a way that size is exactly width * height *
100 uint8_t* current_row
= p
.image()->data();
101 for (int y
= 0; y
< p
.image()->size().height(); ++y
) {
102 data
.append(current_row
,
103 current_row
+ p
.image()->size().width() *
104 webrtc::DesktopFrame::kBytesPerPixel
);
105 current_row
+= p
.image()->stride();
107 m
->WriteData(reinterpret_cast<const char*>(p
.image()->data()), data
.size());
109 ParamTraits
<webrtc::DesktopVector
>::Write(m
, p
.hotspot());
113 bool ParamTraits
<webrtc::MouseCursor
>::Read(const Message
* m
,
114 base::PickleIterator
* iter
,
115 webrtc::MouseCursor
* r
) {
116 webrtc::DesktopSize size
;
117 if (!ParamTraits
<webrtc::DesktopSize
>::Read(m
, iter
, &size
) ||
118 size
.width() <= 0 || size
.width() > (SHRT_MAX
/ 2) ||
119 size
.height() <= 0 || size
.height() > (SHRT_MAX
/ 2)) {
123 const int expected_length
=
124 size
.width() * size
.height() * webrtc::DesktopFrame::kBytesPerPixel
;
128 if (!iter
->ReadData(&data
, &data_length
) || data_length
!= expected_length
)
131 webrtc::DesktopVector hotspot
;
132 if (!ParamTraits
<webrtc::DesktopVector
>::Read(m
, iter
, &hotspot
))
135 webrtc::BasicDesktopFrame
* image
= new webrtc::BasicDesktopFrame(size
);
136 memcpy(image
->data(), data
, data_length
);
139 r
->set_hotspot(hotspot
);
144 void ParamTraits
<webrtc::MouseCursor
>::Log(
145 const webrtc::MouseCursor
& p
,
147 l
->append(base::StringPrintf(
148 "webrtc::DesktopRect{image(%d, %d), hotspot(%d, %d)}",
149 p
.image()->size().width(), p
.image()->size().height(),
150 p
.hotspot().x(), p
.hotspot().y()));
155 void ParamTraits
<remoting::ScreenResolution
>::Write(
157 const remoting::ScreenResolution
& p
) {
158 ParamTraits
<webrtc::DesktopSize
>::Write(m
, p
.dimensions());
159 ParamTraits
<webrtc::DesktopVector
>::Write(m
, p
.dpi());
163 bool ParamTraits
<remoting::ScreenResolution
>::Read(
165 base::PickleIterator
* iter
,
166 remoting::ScreenResolution
* r
) {
167 webrtc::DesktopSize size
;
168 webrtc::DesktopVector dpi
;
169 if (!ParamTraits
<webrtc::DesktopSize
>::Read(m
, iter
, &size
) ||
170 !ParamTraits
<webrtc::DesktopVector
>::Read(m
, iter
, &dpi
)) {
173 if (size
.width() < 0 || size
.height() < 0 ||
174 dpi
.x() < 0 || dpi
.y() < 0) {
177 *r
= remoting::ScreenResolution(size
, dpi
);
182 void ParamTraits
<remoting::ScreenResolution
>::Log(
183 const remoting::ScreenResolution
& p
,
185 l
->append(base::StringPrintf("webrtc::ScreenResolution(%d, %d, %d, %d)",
186 p
.dimensions().width(), p
.dimensions().height(),
187 p
.dpi().x(), p
.dpi().y()));