summaryrefslogtreecommitdiffstats
path: root/remoting/client/rectangle_update_decoder.cc
blob: b96b7baa8039128210f53d9d51ea1029a5e14e31 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2011 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/client/rectangle_update_decoder.h"

#include "base/logging.h"
#include "base/message_loop.h"
#include "remoting/base/decoder.h"
#include "remoting/base/decoder_row_based.h"
#include "remoting/base/decoder_vp8.h"
#include "remoting/base/util.h"
#include "remoting/client/frame_consumer.h"
#include "remoting/protocol/session_config.h"

using remoting::protocol::ChannelConfig;
using remoting::protocol::SessionConfig;

namespace remoting {

class PartialFrameCleanup : public Task {
 public:
  PartialFrameCleanup(media::VideoFrame* frame, UpdatedRects* rects,
                      RectangleUpdateDecoder* decoder)
      : frame_(frame), rects_(rects), decoder_(decoder) {
  }

  virtual void Run() {
    delete rects_;
    frame_ = NULL;

    // There maybe pending request to refresh rectangles.
    decoder_->OnFrameConsumed();
    decoder_ = NULL;
  }

 private:
  scoped_refptr<media::VideoFrame> frame_;
  UpdatedRects* rects_;
  scoped_refptr<RectangleUpdateDecoder> decoder_;
};

RectangleUpdateDecoder::RectangleUpdateDecoder(MessageLoop* message_loop,
                                               FrameConsumer* consumer)
    : message_loop_(message_loop),
      consumer_(consumer),
      frame_is_new_(false),
      frame_is_consuming_(false) {
}

RectangleUpdateDecoder::~RectangleUpdateDecoder() {
}

void RectangleUpdateDecoder::Initialize(const SessionConfig& config) {
  initial_screen_size_ = gfx::Size(config.initial_resolution().width,
                                   config.initial_resolution().height);

  // Initialize decoder based on the selected codec.
  ChannelConfig::Codec codec = config.video_config().codec;
  if (codec == ChannelConfig::CODEC_VERBATIM) {
    decoder_.reset(DecoderRowBased::CreateVerbatimDecoder());
  } else if (codec == ChannelConfig::CODEC_ZIP) {
    decoder_.reset(DecoderRowBased::CreateZlibDecoder());
  } else if (codec == ChannelConfig::CODEC_VP8) {
    decoder_.reset(new DecoderVp8());
  } else {
    NOTREACHED() << "Invalid Encoding found: " << codec;
  }
}

void RectangleUpdateDecoder::DecodePacket(const VideoPacket* packet,
                                          Task* done) {
  if (message_loop_ != MessageLoop::current()) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(this,
                          &RectangleUpdateDecoder::DecodePacket, packet,
                          done));
    return;
  }
  base::ScopedTaskRunner done_runner(done);

  AllocateFrame(packet, done_runner.Release());
}

void RectangleUpdateDecoder::AllocateFrame(const VideoPacket* packet,
                                           Task* done) {
  if (message_loop_ != MessageLoop::current()) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(
            this,
            &RectangleUpdateDecoder::AllocateFrame, packet, done));
    return;
  }
  base::ScopedTaskRunner done_runner(done);

  // Find the required frame size.
  bool has_screen_size = packet->format().has_screen_width() &&
                         packet->format().has_screen_height();
  gfx::Size screen_size(packet->format().screen_width(),
                        packet->format().screen_height());
  if (!has_screen_size)
    screen_size = initial_screen_size_;

  // Find the current frame size.
  gfx::Size frame_size(0, 0);
  if (frame_)
    frame_size = gfx::Size(static_cast<int>(frame_->width()),
                           static_cast<int>(frame_->height()));

  // Allocate a new frame, if necessary.
  if ((!frame_) || (has_screen_size && (screen_size != frame_size))) {
    if (frame_) {
      consumer_->ReleaseFrame(frame_);
      frame_ = NULL;
    }

    consumer_->AllocateFrame(media::VideoFrame::RGB32,
                             screen_size.width(), screen_size.height(),
                             base::TimeDelta(), base::TimeDelta(),
                             &frame_,
                             NewRunnableMethod(this,
                                 &RectangleUpdateDecoder::ProcessPacketData,
                                 packet, done_runner.Release()));
    frame_is_new_ = true;
    return;
  }
  ProcessPacketData(packet, done_runner.Release());
}

void RectangleUpdateDecoder::ProcessPacketData(
    const VideoPacket* packet, Task* done) {
  if (message_loop_ != MessageLoop::current()) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(this,
                          &RectangleUpdateDecoder::ProcessPacketData, packet,
                          done));
    return;
  }
  base::ScopedTaskRunner done_runner(done);

  if (frame_is_new_) {
    decoder_->Reset();
    decoder_->Initialize(frame_);
    frame_is_new_ = false;
  }

  if (!decoder_->IsReadyForData()) {
    // TODO(ajwong): This whole thing should move into an invalid state.
    LOG(ERROR) << "Decoder is unable to process data. Dropping packet.";
    return;
  }

  if (decoder_->DecodePacket(packet) == Decoder::DECODE_DONE)
    SubmitToConsumer();
}

void RectangleUpdateDecoder::SetScaleRatios(double horizontal_ratio,
                                            double vertical_ratio) {
  if (message_loop_ != MessageLoop::current()) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(this,
                          &RectangleUpdateDecoder::SetScaleRatios,
                          horizontal_ratio,
                          vertical_ratio));
    return;
  }

  // TODO(hclam): If the scale ratio has changed we should reallocate a
  // VideoFrame of different size. However if the scale ratio is always
  // smaller than 1.0 we can use the same video frame.
  decoder_->SetScaleRatios(horizontal_ratio, vertical_ratio);
}

void RectangleUpdateDecoder::UpdateClipRect(const gfx::Rect& new_clip_rect) {
  if (message_loop_ != MessageLoop::current()) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(
            this,
            &RectangleUpdateDecoder::UpdateClipRect, new_clip_rect));
    return;
  }

  if (new_clip_rect == clip_rect_ || !decoder_.get())
    return;

  // Find out the rectangles to show because of clip rect is updated.
  if (new_clip_rect.y() < clip_rect_.y()) {
    refresh_rects_.push_back(
        gfx::Rect(new_clip_rect.x(),
                  new_clip_rect.y(),
                  new_clip_rect.width(),
                  clip_rect_.y() - new_clip_rect.y()));
  }

  if (new_clip_rect.x() < clip_rect_.x()) {
    refresh_rects_.push_back(
        gfx::Rect(new_clip_rect.x(),
                  clip_rect_.y(),
                  clip_rect_.x() - new_clip_rect.x(),
                  clip_rect_.height()));
  }

  if (new_clip_rect.right() > clip_rect_.right()) {
    refresh_rects_.push_back(
        gfx::Rect(clip_rect_.right(),
                  clip_rect_.y(),
                  new_clip_rect.right() - clip_rect_.right(),
                  new_clip_rect.height()));
  }

  if (new_clip_rect.bottom() > clip_rect_.bottom()) {
    refresh_rects_.push_back(
        gfx::Rect(new_clip_rect.x(),
                  clip_rect_.bottom(),
                  new_clip_rect.width(),
                  new_clip_rect.bottom() - clip_rect_.bottom()));
  }

  clip_rect_ = new_clip_rect;
  decoder_->SetClipRect(new_clip_rect);
  DoRefresh();
}

void RectangleUpdateDecoder::RefreshFullFrame() {
  if (message_loop_ != MessageLoop::current()) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(this, &RectangleUpdateDecoder::RefreshFullFrame));
    return;
  }

  // If a video frame or the decoder is not allocated yet then don't
  // save the refresh rectangle to avoid wasted computation.
  if (!frame_ || !decoder_.get())
    return;

  refresh_rects_.push_back(
      gfx::Rect(0, 0, static_cast<int>(frame_->width()),
                static_cast<int>(frame_->height())));
  DoRefresh();
}

void RectangleUpdateDecoder::SubmitToConsumer() {
  // A frame is not allocated yet, we can reach here because of a refresh
  // request.
  if (!frame_)
    return;

  UpdatedRects* dirty_rects = new UpdatedRects();
  decoder_->GetUpdatedRects(dirty_rects);

  frame_is_consuming_ = true;
  consumer_->OnPartialFrameOutput(
      frame_, dirty_rects,
      new PartialFrameCleanup(frame_, dirty_rects, this));
}

void RectangleUpdateDecoder::DoRefresh() {
  DCHECK_EQ(message_loop_, MessageLoop::current());

  if (refresh_rects_.empty())
    return;

  decoder_->RefreshRects(refresh_rects_);
  refresh_rects_.clear();
  SubmitToConsumer();
}

void RectangleUpdateDecoder::OnFrameConsumed() {
  if (message_loop_ != MessageLoop::current()) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(this, &RectangleUpdateDecoder::OnFrameConsumed));
    return;
  }

  frame_is_consuming_ = false;
  DoRefresh();
}

}  // namespace remoting