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
,
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
,
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
,
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(
115 PickleIterator
* iter
,
116 webrtc::MouseCursor
* r
) {
117 webrtc::DesktopSize size
;
118 if (!ParamTraits
<webrtc::DesktopSize
>::Read(m
, iter
, &size
) ||
119 size
.width() <= 0 || size
.width() > (SHRT_MAX
/ 2) ||
120 size
.height() <= 0 || size
.height() > (SHRT_MAX
/ 2)) {
124 const int expected_length
=
125 size
.width() * size
.height() * webrtc::DesktopFrame::kBytesPerPixel
;
129 if (!iter
->ReadData(&data
, &data_length
) || data_length
!= expected_length
)
132 webrtc::DesktopVector hotspot
;
133 if (!ParamTraits
<webrtc::DesktopVector
>::Read(m
, iter
, &hotspot
))
136 webrtc::BasicDesktopFrame
* image
= new webrtc::BasicDesktopFrame(size
);
137 memcpy(image
->data(), data
, data_length
);
140 r
->set_hotspot(hotspot
);
145 void ParamTraits
<webrtc::MouseCursor
>::Log(
146 const webrtc::MouseCursor
& p
,
148 l
->append(base::StringPrintf(
149 "webrtc::DesktopRect{image(%d, %d), hotspot(%d, %d)}",
150 p
.image()->size().width(), p
.image()->size().height(),
151 p
.hotspot().x(), p
.hotspot().y()));
156 void ParamTraits
<remoting::ScreenResolution
>::Write(
158 const remoting::ScreenResolution
& p
) {
159 ParamTraits
<webrtc::DesktopSize
>::Write(m
, p
.dimensions());
160 ParamTraits
<webrtc::DesktopVector
>::Write(m
, p
.dpi());
164 bool ParamTraits
<remoting::ScreenResolution
>::Read(
166 PickleIterator
* iter
,
167 remoting::ScreenResolution
* r
) {
168 webrtc::DesktopSize size
;
169 webrtc::DesktopVector dpi
;
170 if (!ParamTraits
<webrtc::DesktopSize
>::Read(m
, iter
, &size
) ||
171 !ParamTraits
<webrtc::DesktopVector
>::Read(m
, iter
, &dpi
)) {
174 if (size
.width() < 0 || size
.height() < 0 ||
175 dpi
.x() < 0 || dpi
.y() < 0) {
178 *r
= remoting::ScreenResolution(size
, dpi
);
183 void ParamTraits
<remoting::ScreenResolution
>::Log(
184 const remoting::ScreenResolution
& p
,
186 l
->append(base::StringPrintf("webrtc::ScreenResolution(%d, %d, %d, %d)",
187 p
.dimensions().width(), p
.dimensions().height(),
188 p
.dpi().x(), p
.dpi().y()));