OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/codec/video_encoder_verbatim.h" | 5 #include "remoting/codec/video_encoder_verbatim.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "remoting/base/util.h" | 10 #include "remoting/base/util.h" |
11 #include "remoting/proto/video.pb.h" | 11 #include "remoting/proto/video.pb.h" |
12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.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" |
13 | 15 |
14 namespace remoting { | 16 namespace remoting { |
15 | 17 |
| 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())); |
| 21 } |
| 22 |
16 VideoEncoderVerbatim::VideoEncoderVerbatim() {} | 23 VideoEncoderVerbatim::VideoEncoderVerbatim() {} |
17 VideoEncoderVerbatim::~VideoEncoderVerbatim() {} | 24 VideoEncoderVerbatim::~VideoEncoderVerbatim() {} |
18 | 25 |
19 scoped_ptr<VideoPacket> VideoEncoderVerbatim::Encode( | 26 scoped_ptr<VideoPacket> VideoEncoderVerbatim::Encode( |
20 const webrtc::DesktopFrame& frame) { | 27 const webrtc::DesktopFrame& frame) { |
21 CHECK(frame.data()); | 28 CHECK(frame.data()); |
22 | 29 |
23 base::Time encode_start_time = base::Time::Now(); | 30 base::Time encode_start_time = base::Time::Now(); |
24 scoped_ptr<VideoPacket> packet(new VideoPacket()); | |
25 | 31 |
26 VideoPacketFormat* format = packet->mutable_format(); | 32 // Create a VideoPacket with common fields (e.g. DPI, rects, shape) set. |
27 format->set_encoding(VideoPacketFormat::ENCODING_VERBATIM); | 33 scoped_ptr<VideoPacket> packet(helper_.CreateVideoPacket(frame)); |
28 if (!frame.size().equals(screen_size_)) { | 34 packet->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VERBATIM); |
29 screen_size_ = frame.size(); | |
30 format->set_screen_width(screen_size_.width()); | |
31 format->set_screen_height(screen_size_.height()); | |
32 } | |
33 | 35 |
34 // Calculate output size. | 36 // Calculate output size. |
35 size_t output_size = 0; | 37 size_t output_size = 0; |
36 for (webrtc::DesktopRegion::Iterator iter(frame.updated_region()); | 38 for (webrtc::DesktopRegion::Iterator iter(frame.updated_region()); |
37 !iter.IsAtEnd(); iter.Advance()) { | 39 !iter.IsAtEnd(); iter.Advance()) { |
38 const webrtc::DesktopRect& rect = iter.rect(); | 40 const webrtc::DesktopRect& rect = iter.rect(); |
39 output_size += rect.width() * rect.height() * | 41 output_size += rect.width() * rect.height() * |
40 webrtc::DesktopFrame::kBytesPerPixel; | 42 webrtc::DesktopFrame::kBytesPerPixel; |
41 } | 43 } |
42 | 44 |
43 uint8_t* out = GetOutputBuffer(packet.get(), output_size); | 45 uint8_t* out = GetPacketOutputBuffer(packet.get(), output_size); |
44 const int in_stride = frame.stride(); | 46 const int in_stride = frame.stride(); |
45 | 47 |
46 // Store all changed rectangles in the packet. | 48 // Encode pixel data for all changed rectangles into the packet. |
47 for (webrtc::DesktopRegion::Iterator iter(frame.updated_region()); | 49 for (webrtc::DesktopRegion::Iterator iter(frame.updated_region()); |
48 !iter.IsAtEnd(); iter.Advance()) { | 50 !iter.IsAtEnd(); iter.Advance()) { |
49 const webrtc::DesktopRect& rect = iter.rect(); | 51 const webrtc::DesktopRect& rect = iter.rect(); |
50 const int row_size = webrtc::DesktopFrame::kBytesPerPixel * rect.width(); | 52 const int row_size = webrtc::DesktopFrame::kBytesPerPixel * rect.width(); |
51 const uint8_t* in = frame.data() + rect.top() * in_stride + | 53 const uint8_t* in = frame.data() + rect.top() * in_stride + |
52 rect.left() * webrtc::DesktopFrame::kBytesPerPixel; | 54 rect.left() * webrtc::DesktopFrame::kBytesPerPixel; |
53 for (int y = rect.top(); y < rect.top() + rect.height(); ++y) { | 55 for (int y = rect.top(); y < rect.top() + rect.height(); ++y) { |
54 memcpy(out, in, row_size); | 56 memcpy(out, in, row_size); |
55 out += row_size; | 57 out += row_size; |
56 in += in_stride; | 58 in += in_stride; |
57 } | 59 } |
58 | |
59 Rect* dirty_rect = packet->add_dirty_rects(); | |
60 dirty_rect->set_x(rect.left()); | |
61 dirty_rect->set_y(rect.top()); | |
62 dirty_rect->set_width(rect.width()); | |
63 dirty_rect->set_height(rect.height()); | |
64 } | 60 } |
65 | 61 |
66 packet->set_capture_time_ms(frame.capture_time_ms()); | 62 // Note the time taken to encode the pixel data. |
67 packet->set_encode_time_ms( | 63 packet->set_encode_time_ms( |
68 (base::Time::Now() - encode_start_time).InMillisecondsRoundedUp()); | 64 (base::Time::Now() - encode_start_time).InMillisecondsRoundedUp()); |
69 if (!frame.dpi().is_zero()) { | |
70 packet->mutable_format()->set_x_dpi(frame.dpi().x()); | |
71 packet->mutable_format()->set_y_dpi(frame.dpi().y()); | |
72 } | |
73 | 65 |
74 return packet.Pass(); | 66 return packet.Pass(); |
75 } | 67 } |
76 | 68 |
77 uint8_t* VideoEncoderVerbatim::GetOutputBuffer(VideoPacket* packet, | |
78 size_t size) { | |
79 packet->mutable_data()->resize(size); | |
80 return reinterpret_cast<uint8_t*>(string_as_array(packet->mutable_data())); | |
81 } | |
82 | |
83 } // namespace remoting | 69 } // namespace remoting |
OLD | NEW |