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/codec/video_encoder_verbatim.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/time/time.h"
10 #include "remoting/base/util.h"
11 #include "remoting/proto/video.pb.h"
12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
13 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
14 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
18 static uint8_t* GetPacketOutputBuffer(VideoPacket
* packet
, size_t size
) {
19 packet
->mutable_data()->resize(size
);
20 return reinterpret_cast<uint8_t*>(string_as_array(packet
->mutable_data()));
23 VideoEncoderVerbatim::VideoEncoderVerbatim() {}
24 VideoEncoderVerbatim::~VideoEncoderVerbatim() {}
26 scoped_ptr
<VideoPacket
> VideoEncoderVerbatim::Encode(
27 const webrtc::DesktopFrame
& frame
) {
30 // If nothing has changed in the frame then return NULL to indicate that
31 // we don't need to actually send anything (e.g. nothing to top-off).
32 if (frame
.updated_region().is_empty())
35 base::Time encode_start_time
= base::Time::Now();
37 // Create a VideoPacket with common fields (e.g. DPI, rects, shape) set.
38 scoped_ptr
<VideoPacket
> packet(helper_
.CreateVideoPacket(frame
));
39 packet
->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VERBATIM
);
41 // Calculate output size.
42 size_t output_size
= 0;
43 for (webrtc::DesktopRegion::Iterator
iter(frame
.updated_region());
44 !iter
.IsAtEnd(); iter
.Advance()) {
45 const webrtc::DesktopRect
& rect
= iter
.rect();
46 output_size
+= rect
.width() * rect
.height() *
47 webrtc::DesktopFrame::kBytesPerPixel
;
50 uint8_t* out
= GetPacketOutputBuffer(packet
.get(), output_size
);
51 const int in_stride
= frame
.stride();
53 // Encode pixel data for all changed rectangles into the packet.
54 for (webrtc::DesktopRegion::Iterator
iter(frame
.updated_region());
55 !iter
.IsAtEnd(); iter
.Advance()) {
56 const webrtc::DesktopRect
& rect
= iter
.rect();
57 const int row_size
= webrtc::DesktopFrame::kBytesPerPixel
* rect
.width();
58 const uint8_t* in
= frame
.data() + rect
.top() * in_stride
+
59 rect
.left() * webrtc::DesktopFrame::kBytesPerPixel
;
60 for (int y
= rect
.top(); y
< rect
.top() + rect
.height(); ++y
) {
61 memcpy(out
, in
, row_size
);
67 // Note the time taken to encode the pixel data.
68 packet
->set_encode_time_ms(
69 (base::Time::Now() - encode_start_time
).InMillisecondsRoundedUp());
74 } // namespace remoting