summaryrefslogtreecommitdiffstats
path: root/remoting/client/chromoting_view.cc
blob: 8f53e31bb76471ae66ab576f8bb4fe3357c88759 (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
// 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/client/chromoting_view.h"

#include "remoting/base/decoder_verbatim.h"
#include "remoting/base/decoder_zlib.h"

namespace remoting {

ChromotingView::ChromotingView()
    : frame_width_(0),
      frame_height_(0) {
}

ChromotingView::~ChromotingView() {}

// TODO(garykac): This assumes a single screen. This will need to be adjusted
// to add support for mulitple monitors.
void ChromotingView::GetScreenSize(int* width, int* height) {
  *width = frame_width_;
  *height = frame_height_;
}

bool ChromotingView::SetupDecoder(UpdateStreamEncoding encoding) {
  if (encoding == EncodingInvalid) {
    LOG(ERROR) << "Cannot create encoder for EncodingInvalid";
    return false;
  }

  // If we're in the middle of decoding a stream, then we need to make sure
  // that that all packets in that stream match the encoding of the first
  // packet.
  //
  // If we decide to relax this constraint in the future, we'll need to
  // update this to keep a set of decoders around.
  if (decoder_.get() && decoder_->IsStarted()) {
    // Verify that the encoding matches the decoder. Once we've started
    // decoding, we can't switch to another decoder.
    if (decoder_->Encoding() != encoding) {
      LOG(ERROR) << "Encoding mismatch: Set up to handle "
                 << "UpdateStreamEncoding=" << decoder_->Encoding()
                 << " but received request for "
                 << encoding;
      return false;
    }
    return true;
  }

  // Lazily initialize a new decoder.
  // We create a new decoder if we don't currently have a decoder or if the
  // decoder doesn't match the desired encoding.
  if (!decoder_.get() || decoder_->Encoding() != encoding) {
    // Initialize a new decoder based on this message encoding.
    if (encoding == EncodingNone) {
      decoder_.reset(new DecoderVerbatim());
    } else if (encoding == EncodingZlib) {
      decoder_.reset(new DecoderZlib());
    }
    // Make sure we successfully allocated a decoder of the correct type.
    DCHECK(decoder_.get());
    DCHECK(decoder_->Encoding() == encoding);
  }

  return true;
}

bool ChromotingView::BeginDecoding(Task* partial_decode_done,
                                   Task* decode_done) {
  if (decoder_->IsStarted()) {
    LOG(ERROR) << "BeginDecoding called without ending previous decode.";
    return false;
  }

  decoder_->BeginDecode(frame_, &update_rects_,
                        partial_decode_done, decode_done);

  if (!decoder_->IsStarted()) {
    LOG(ERROR) << "Unable to start decoding.";
    return false;
  }

  return true;
}

bool ChromotingView::Decode(ChromotingHostMessage* msg) {
  if (!decoder_->IsStarted()) {
    LOG(ERROR) << "Attempt to decode payload before calling BeginDecode.";
    return false;
  }

  return decoder_->PartialDecode(msg);
}

bool ChromotingView::EndDecoding() {
  if (!decoder_->IsStarted()) {
    LOG(ERROR) << "Attempt to end decode when none has been started.";
    return false;
  }

  decoder_->EndDecode();

  if (decoder_->IsStarted()) {
    LOG(ERROR) << "Unable to properly end decoding.\n";
    return false;
  }

  return true;
}

}  // namespace remoting