OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/base/decoder_zlib.h" |
| 6 |
| 7 #include "remoting/base/decompressor_zlib.h" |
| 8 #include "remoting/base/protocol_util.h" |
| 9 |
| 10 namespace remoting { |
| 11 |
| 12 DecoderZlib::DecoderZlib() |
| 13 : state_(kWaitingForBeginRect), |
| 14 rect_x_(0), |
| 15 rect_y_(0), |
| 16 rect_width_(0), |
| 17 rect_height_(0), |
| 18 bytes_per_pixel_(0), |
| 19 updated_rects_(NULL), |
| 20 row_pos_(0), |
| 21 row_y_(0) { |
| 22 } |
| 23 |
| 24 bool DecoderZlib::BeginDecode(scoped_refptr<media::VideoFrame> frame, |
| 25 UpdatedRects* updated_rects, |
| 26 Task* partial_decode_done, |
| 27 Task* decode_done) { |
| 28 DCHECK(!partial_decode_done_.get()); |
| 29 DCHECK(!decode_done_.get()); |
| 30 DCHECK(!updated_rects_); |
| 31 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 32 CHECK(static_cast<PixelFormat>(frame->format()) == PixelFormatRgb32) |
| 33 << "Only RGB32 is supported"; |
| 34 |
| 35 partial_decode_done_.reset(partial_decode_done); |
| 36 decode_done_.reset(decode_done); |
| 37 updated_rects_ = updated_rects; |
| 38 frame_ = frame; |
| 39 |
| 40 // Create the decompressor. |
| 41 decompressor_.reset(new DecompressorZlib()); |
| 42 return true; |
| 43 } |
| 44 |
| 45 bool DecoderZlib::PartialDecode(HostMessage* message) { |
| 46 scoped_ptr<HostMessage> msg_deleter(message); |
| 47 DCHECK(message->has_update_stream_packet()); |
| 48 |
| 49 bool ret = true; |
| 50 if (message->update_stream_packet().has_begin_rect()) |
| 51 ret = HandleBeginRect(message); |
| 52 if (ret && message->update_stream_packet().has_rect_data()) |
| 53 ret = HandleRectData(message); |
| 54 if (ret && message->update_stream_packet().has_end_rect()) |
| 55 ret = HandleEndRect(message); |
| 56 return ret; |
| 57 } |
| 58 |
| 59 void DecoderZlib::EndDecode() { |
| 60 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 61 decode_done_->Run(); |
| 62 |
| 63 partial_decode_done_.reset(); |
| 64 decode_done_.reset(); |
| 65 updated_rects_ = NULL; |
| 66 frame_ = NULL; |
| 67 decompressor_.reset(); |
| 68 } |
| 69 |
| 70 bool DecoderZlib::HandleBeginRect(HostMessage* message) { |
| 71 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 72 state_ = kWaitingForRectData; |
| 73 |
| 74 rect_width_ = message->update_stream_packet().begin_rect().width(); |
| 75 rect_height_ = message->update_stream_packet().begin_rect().height(); |
| 76 rect_x_ = message->update_stream_packet().begin_rect().x(); |
| 77 rect_y_ = message->update_stream_packet().begin_rect().y(); |
| 78 |
| 79 PixelFormat pixel_format = |
| 80 message->update_stream_packet().begin_rect().pixel_format(); |
| 81 |
| 82 if (static_cast<PixelFormat>(frame_->format()) != pixel_format) { |
| 83 NOTREACHED() << "Pixel format of message doesn't match the video frame. " |
| 84 "Expected vs received = " |
| 85 << frame_->format() << " vs " << pixel_format |
| 86 << " Color space conversion required."; |
| 87 return false; |
| 88 } |
| 89 |
| 90 bytes_per_pixel_ = GetBytesPerPixel(pixel_format); |
| 91 row_pos_ = 0; |
| 92 row_y_ = 0; |
| 93 return true; |
| 94 } |
| 95 |
| 96 bool DecoderZlib::HandleRectData(HostMessage* message) { |
| 97 DCHECK_EQ(kWaitingForRectData, state_); |
| 98 DCHECK_EQ(0, |
| 99 message->update_stream_packet().rect_data().sequence_number()); |
| 100 |
| 101 const uint8* in = |
| 102 (const uint8*)message->update_stream_packet().rect_data().data().data(); |
| 103 const int in_size = |
| 104 message->update_stream_packet().rect_data().data().size(); |
| 105 const int row_size = rect_width_ * bytes_per_pixel_; |
| 106 const int stride = frame_->stride(media::VideoFrame::kRGBPlane); |
| 107 uint8* out = frame_->data(media::VideoFrame::kRGBPlane) + |
| 108 stride * (rect_y_ + row_y_) + bytes_per_pixel_ * rect_x_; |
| 109 |
| 110 // Consume all the data in the message. |
| 111 bool decompress_again = true; |
| 112 int used = 0; |
| 113 while (decompress_again && used < in_size) { |
| 114 int written = 0; |
| 115 int consumed = 0; |
| 116 decompress_again = decompressor_->Process( |
| 117 in + used, in_size - used, out + row_pos_, row_size - row_pos_, |
| 118 &consumed, &written); |
| 119 used += consumed; |
| 120 row_pos_ += written; |
| 121 |
| 122 // If this row is completely filled then move onto the next row. |
| 123 if (row_pos_ == row_size) { |
| 124 ++row_y_; |
| 125 row_pos_ = 0; |
| 126 out += stride; |
| 127 } |
| 128 } |
| 129 return true; |
| 130 } |
| 131 |
| 132 bool DecoderZlib::HandleEndRect(HostMessage* message) { |
| 133 DCHECK_EQ(kWaitingForRectData, state_); |
| 134 state_ = kWaitingForBeginRect; |
| 135 |
| 136 updated_rects_->clear(); |
| 137 updated_rects_->push_back(gfx::Rect(rect_x_, rect_y_, |
| 138 rect_width_, rect_height_)); |
| 139 partial_decode_done_->Run(); |
| 140 return true; |
| 141 } |
| 142 |
| 143 } // namespace remoting |
OLD | NEW |