video_encoder_verbatim.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "remoting/codec/video_encoder_verbatim.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/check.h"
  8. #include "remoting/base/util.h"
  9. #include "remoting/proto/video.pb.h"
  10. #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
  11. #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
  12. #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
  13. namespace remoting {
  14. static uint8_t* GetPacketOutputBuffer(VideoPacket* packet, size_t size) {
  15. packet->mutable_data()->resize(size);
  16. return reinterpret_cast<uint8_t*>(std::data(*packet->mutable_data()));
  17. }
  18. VideoEncoderVerbatim::VideoEncoderVerbatim() = default;
  19. VideoEncoderVerbatim::~VideoEncoderVerbatim() = default;
  20. std::unique_ptr<VideoPacket> VideoEncoderVerbatim::Encode(
  21. const webrtc::DesktopFrame& frame) {
  22. DCHECK(frame.data());
  23. // If nothing has changed in the frame then return NULL to indicate that
  24. // we don't need to actually send anything (e.g. nothing to top-off).
  25. if (frame.updated_region().is_empty())
  26. return nullptr;
  27. // Create a VideoPacket with common fields (e.g. DPI, rects, shape) set.
  28. std::unique_ptr<VideoPacket> packet(helper_.CreateVideoPacket(frame));
  29. packet->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VERBATIM);
  30. // Calculate output size.
  31. size_t output_size = 0;
  32. for (webrtc::DesktopRegion::Iterator iter(frame.updated_region());
  33. !iter.IsAtEnd(); iter.Advance()) {
  34. const webrtc::DesktopRect& rect = iter.rect();
  35. output_size += rect.width() * rect.height() *
  36. webrtc::DesktopFrame::kBytesPerPixel;
  37. }
  38. uint8_t* out = GetPacketOutputBuffer(packet.get(), output_size);
  39. const int in_stride = frame.stride();
  40. // Encode pixel data for all changed rectangles into the packet.
  41. for (webrtc::DesktopRegion::Iterator iter(frame.updated_region());
  42. !iter.IsAtEnd(); iter.Advance()) {
  43. const webrtc::DesktopRect& rect = iter.rect();
  44. const int row_size = webrtc::DesktopFrame::kBytesPerPixel * rect.width();
  45. const uint8_t* in = frame.data() + rect.top() * in_stride +
  46. rect.left() * webrtc::DesktopFrame::kBytesPerPixel;
  47. for (int y = rect.top(); y < rect.top() + rect.height(); ++y) {
  48. memcpy(out, in, row_size);
  49. out += row_size;
  50. in += in_stride;
  51. }
  52. }
  53. return packet;
  54. }
  55. } // namespace remoting