summaryrefslogtreecommitdiffstats
path: root/remoting/base/decoder_verbatim.cc
blob: fd0b3d1070864b39df76f0dfc6cd4f74ae85d3ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/base/decoder_verbatim.h"

#include "remoting/base/protocol_util.h"

namespace remoting {

DecoderVerbatim::DecoderVerbatim()
    : state_(kWaitingForBeginRect),
      rect_x_(0),
      rect_y_(0),
      rect_width_(0),
      rect_height_(0),
      bytes_per_pixel_(0),
      updated_rects_(NULL),
      reverse_rows_(true) {
  encoding_ = EncodingNone;
}

bool DecoderVerbatim::BeginDecode(scoped_refptr<media::VideoFrame> frame,
                                  UpdatedRects* updated_rects,
                                  Task* partial_decode_done,
                                  Task* decode_done) {
  DCHECK(!partial_decode_done_.get());
  DCHECK(!decode_done_.get());
  DCHECK(!updated_rects_);
  DCHECK_EQ(kWaitingForBeginRect, state_);
  DCHECK(!started_);

  partial_decode_done_.reset(partial_decode_done);
  decode_done_.reset(decode_done);
  updated_rects_ = updated_rects;

  // TODO(hclam): Check if we can accept the color format of the video frame
  // and the codec.
  frame_ = frame;

  started_ = true;
  return true;
}

bool DecoderVerbatim::PartialDecode(ChromotingHostMessage* message) {
  scoped_ptr<ChromotingHostMessage> msg_deleter(message);
  DCHECK(message->has_update_stream_packet());
  DCHECK(started_);

  bool ret = true;
  if (message->update_stream_packet().has_begin_rect())
    ret = HandleBeginRect(message);
  if (ret && message->update_stream_packet().has_rect_data())
    ret = HandleRectData(message);
  if (ret && message->update_stream_packet().has_end_rect())
    ret = HandleEndRect(message);
  return ret;
}

void DecoderVerbatim::EndDecode() {
  DCHECK_EQ(kWaitingForBeginRect, state_);
  DCHECK(started_);

  decode_done_->Run();

  partial_decode_done_.reset();
  decode_done_.reset();
  frame_ = NULL;
  updated_rects_ = NULL;
  started_ = false;
}

bool DecoderVerbatim::HandleBeginRect(ChromotingHostMessage* message) {
  DCHECK_EQ(kWaitingForBeginRect, state_);
  state_ = kWaitingForRectData;

  rect_width_ = message->update_stream_packet().begin_rect().width();
  rect_height_ = message->update_stream_packet().begin_rect().height();
  rect_x_ = message->update_stream_packet().begin_rect().x();
  rect_y_ = message->update_stream_packet().begin_rect().y();

  PixelFormat pixel_format =
      message->update_stream_packet().begin_rect().pixel_format();

  if (static_cast<PixelFormat>(frame_->format()) != pixel_format) {
    NOTREACHED() << "Pixel format of message doesn't match the video frame. "
                    "Expected vs received = "
                 << frame_->format() << " vs " << pixel_format
                 << " Color space conversion required.";
    return false;
  }

  bytes_per_pixel_ = GetBytesPerPixel(pixel_format);
  return true;
}

bool DecoderVerbatim::HandleRectData(ChromotingHostMessage* message) {
  DCHECK_EQ(kWaitingForRectData, state_);
  DCHECK_EQ(0,
            message->update_stream_packet().rect_data().sequence_number());

  // Copy the data line by line.
  const int src_stride = bytes_per_pixel_ * rect_width_;
  const char* src =
      message->update_stream_packet().rect_data().data().data();

  // Make sure there's enough data in |message|.
  const int src_size =
      message->update_stream_packet().rect_data().data().length();
  if (src_size != src_stride * rect_height_)
    return false;

  // Make sure there's enough space for us to write to.
  if (frame_->height() < static_cast<size_t>(rect_height_) ||
      frame_->stride(media::VideoFrame::kRGBPlane) < src_stride) {
    return false;
  }

  int src_stride_dir = src_stride;
  if (reverse_rows_) {
    // Copy rows from bottom to top to flip the image vertically.
    src += (rect_height_ - 1) * src_stride;
    // Change the direction of the stride to work bottom to top.
    src_stride_dir *= -1;
  }
  const int dest_stride = frame_->stride(media::VideoFrame::kRGBPlane);
  uint8* dest = frame_->data(media::VideoFrame::kRGBPlane) +
      dest_stride * rect_y_ + bytes_per_pixel_ * rect_x_;
  for (int i = 0; i < rect_height_; ++i) {
    memcpy(dest, src, src_stride);
    dest += dest_stride;
    src += src_stride_dir;
  }

  updated_rects_->clear();
  updated_rects_->push_back(gfx::Rect(rect_x_, rect_y_,
                                      rect_width_, rect_height_));
  partial_decode_done_->Run();
  return true;
}

bool DecoderVerbatim::HandleEndRect(ChromotingHostMessage* message) {
  DCHECK_EQ(kWaitingForRectData, state_);
  state_ = kWaitingForBeginRect;
  return true;
}

}  // namespace remoting