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. 4 5 #include "remoting/codec/video_encoder_verbatim.h" 6 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 14 namespace remoting { 15 VideoEncoderVerbatim()16VideoEncoderVerbatim::VideoEncoderVerbatim() {} ~VideoEncoderVerbatim()17VideoEncoderVerbatim::~VideoEncoderVerbatim() {} 18 Encode(const webrtc::DesktopFrame & frame)19scoped_ptr<VideoPacket> VideoEncoderVerbatim::Encode( 20 const webrtc::DesktopFrame& frame) { 21 CHECK(frame.data()); 22 23 base::Time encode_start_time = base::Time::Now(); 24 scoped_ptr<VideoPacket> packet(new VideoPacket()); 25 26 VideoPacketFormat* format = packet->mutable_format(); 27 format->set_encoding(VideoPacketFormat::ENCODING_VERBATIM); 28 if (!frame.size().equals(screen_size_)) { 29 screen_size_ = frame.size(); 30 format->set_screen_width(screen_size_.width()); 31 format->set_screen_height(screen_size_.height()); 32 } 33 34 // Calculate output size. 35 size_t output_size = 0; 36 for (webrtc::DesktopRegion::Iterator iter(frame.updated_region()); 37 !iter.IsAtEnd(); iter.Advance()) { 38 const webrtc::DesktopRect& rect = iter.rect(); 39 output_size += rect.width() * rect.height() * 40 webrtc::DesktopFrame::kBytesPerPixel; 41 } 42 43 uint8_t* out = GetOutputBuffer(packet.get(), output_size); 44 const int in_stride = frame.stride(); 45 46 // Store all changed rectangles in the packet. 47 for (webrtc::DesktopRegion::Iterator iter(frame.updated_region()); 48 !iter.IsAtEnd(); iter.Advance()) { 49 const webrtc::DesktopRect& rect = iter.rect(); 50 const int row_size = webrtc::DesktopFrame::kBytesPerPixel * rect.width(); 51 const uint8_t* in = frame.data() + rect.top() * in_stride + 52 rect.left() * webrtc::DesktopFrame::kBytesPerPixel; 53 for (int y = rect.top(); y < rect.top() + rect.height(); ++y) { 54 memcpy(out, in, row_size); 55 out += row_size; 56 in += in_stride; 57 } 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 } 65 66 packet->set_capture_time_ms(frame.capture_time_ms()); 67 packet->set_encode_time_ms( 68 (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 74 return packet.Pass(); 75 } 76 GetOutputBuffer(VideoPacket * packet,size_t size)77uint8_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 84