| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/client/decoder_verbatim.h" | 5 #include "remoting/client/decoder_verbatim.h" |
| 6 | 6 |
| 7 #include "remoting/base/protocol_util.h" | 7 #include "remoting/base/protocol_util.h" |
| 8 | 8 |
| 9 namespace remoting { | 9 namespace remoting { |
| 10 | 10 |
| 11 DecoderVerbatim::DecoderVerbatim() | 11 DecoderVerbatim::DecoderVerbatim() |
| 12 : updated_rects_(NULL), | 12 : state_(kWaitingForBeginRect), |
| 13 rect_x_(0), |
| 14 rect_y_(0), |
| 15 rect_width_(0), |
| 16 rect_height_(0), |
| 17 bytes_per_pixel_(0), |
| 18 updated_rects_(NULL), |
| 13 reverse_rows_(true) { | 19 reverse_rows_(true) { |
| 14 } | 20 } |
| 15 | 21 |
| 16 bool DecoderVerbatim::BeginDecode(scoped_refptr<media::VideoFrame> frame, | 22 bool DecoderVerbatim::BeginDecode(scoped_refptr<media::VideoFrame> frame, |
| 17 UpdatedRects* updated_rects, | 23 UpdatedRects* updated_rects, |
| 18 Task* partial_decode_done, | 24 Task* partial_decode_done, |
| 19 Task* decode_done) { | 25 Task* decode_done) { |
| 20 DCHECK(!partial_decode_done_.get()); | 26 DCHECK(!partial_decode_done_.get()); |
| 21 DCHECK(!decode_done_.get()); | 27 DCHECK(!decode_done_.get()); |
| 22 DCHECK(!updated_rects_); | 28 DCHECK(!updated_rects_); |
| 29 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 23 | 30 |
| 24 partial_decode_done_.reset(partial_decode_done); | 31 partial_decode_done_.reset(partial_decode_done); |
| 25 decode_done_.reset(decode_done); | 32 decode_done_.reset(decode_done); |
| 26 updated_rects_ = updated_rects; | 33 updated_rects_ = updated_rects; |
| 27 | 34 |
| 28 // TODO(hclam): Check if we can accept the color format of the video frame | 35 // TODO(hclam): Check if we can accept the color format of the video frame |
| 29 // and the codec. | 36 // and the codec. |
| 30 frame_ = frame; | 37 frame_ = frame; |
| 31 return true; | 38 return true; |
| 32 } | 39 } |
| 33 | 40 |
| 34 bool DecoderVerbatim::PartialDecode(HostMessage* message) { | 41 bool DecoderVerbatim::PartialDecode(HostMessage* message) { |
| 35 scoped_ptr<HostMessage> msg_deleter(message); | 42 scoped_ptr<HostMessage> msg_deleter(message); |
| 43 DCHECK(message->has_update_stream_packet()); |
| 36 | 44 |
| 37 int width = message->update_stream_packet().header().width(); | 45 bool ret = true; |
| 38 int height = message->update_stream_packet().header().height(); | 46 if (message->update_stream_packet().has_begin_rect()) |
| 39 int x = message->update_stream_packet().header().x(); | 47 ret = HandleBeginRect(message); |
| 40 int y = message->update_stream_packet().header().y(); | 48 if (ret && message->update_stream_packet().has_rect_data()) |
| 41 PixelFormat pixel_format = | 49 ret = HandleRectData(message); |
| 42 message->update_stream_packet().header().pixel_format(); | 50 if (ret && message->update_stream_packet().has_end_rect()) |
| 43 | 51 ret = HandleEndRect(message); |
| 44 if (static_cast<PixelFormat>(frame_->format()) != pixel_format) { | 52 return ret; |
| 45 NOTREACHED() << "Pixel format of message doesn't match the video frame. " | |
| 46 "Expected vs received = " | |
| 47 << frame_->format() << " vs " << pixel_format | |
| 48 << " Color space conversion required."; | |
| 49 } | |
| 50 | |
| 51 int bytes_per_pixel = GetBytesPerPixel(pixel_format); | |
| 52 // Copy the data line by line. | |
| 53 const int src_stride = bytes_per_pixel * width; | |
| 54 const char* src = message->update_stream_packet().data().c_str(); | |
| 55 int src_stride_dir = src_stride; | |
| 56 if (reverse_rows_) { | |
| 57 // Copy rows from bottom to top to flip the image vertically. | |
| 58 src += (height - 1) * src_stride; | |
| 59 // Change the direction of the stride to work bottom to top. | |
| 60 src_stride_dir *= -1; | |
| 61 } | |
| 62 const int dest_stride = frame_->stride(media::VideoFrame::kRGBPlane); | |
| 63 uint8* dest = frame_->data(media::VideoFrame::kRGBPlane) + | |
| 64 dest_stride * y + bytes_per_pixel * x; | |
| 65 for (int i = 0; i < height; ++i) { | |
| 66 memcpy(dest, src, src_stride); | |
| 67 dest += dest_stride; | |
| 68 src += src_stride_dir; | |
| 69 } | |
| 70 | |
| 71 updated_rects_->clear(); | |
| 72 updated_rects_->push_back(gfx::Rect(x, y, width, height)); | |
| 73 partial_decode_done_->Run(); | |
| 74 return true; | |
| 75 } | 53 } |
| 76 | 54 |
| 77 void DecoderVerbatim::EndDecode() { | 55 void DecoderVerbatim::EndDecode() { |
| 56 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 78 decode_done_->Run(); | 57 decode_done_->Run(); |
| 79 | 58 |
| 80 partial_decode_done_.reset(); | 59 partial_decode_done_.reset(); |
| 81 decode_done_.reset(); | 60 decode_done_.reset(); |
| 82 frame_ = NULL; | 61 frame_ = NULL; |
| 83 updated_rects_ = NULL; | 62 updated_rects_ = NULL; |
| 84 } | 63 } |
| 85 | 64 |
| 65 bool DecoderVerbatim::HandleBeginRect(HostMessage* message) { |
| 66 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 67 state_ = kWaitingForRectData; |
| 68 |
| 69 rect_width_ = message->update_stream_packet().begin_rect().width(); |
| 70 rect_height_ = message->update_stream_packet().begin_rect().height(); |
| 71 rect_x_ = message->update_stream_packet().begin_rect().x(); |
| 72 rect_y_ = message->update_stream_packet().begin_rect().y(); |
| 73 |
| 74 PixelFormat pixel_format = |
| 75 message->update_stream_packet().begin_rect().pixel_format(); |
| 76 |
| 77 if (static_cast<PixelFormat>(frame_->format()) != pixel_format) { |
| 78 NOTREACHED() << "Pixel format of message doesn't match the video frame. " |
| 79 "Expected vs received = " |
| 80 << frame_->format() << " vs " << pixel_format |
| 81 << " Color space conversion required."; |
| 82 return false; |
| 83 } |
| 84 |
| 85 bytes_per_pixel_ = GetBytesPerPixel(pixel_format); |
| 86 return true; |
| 87 } |
| 88 |
| 89 bool DecoderVerbatim::HandleRectData(HostMessage* message) { |
| 90 DCHECK_EQ(kWaitingForRectData, state_); |
| 91 DCHECK_EQ(0, |
| 92 message->update_stream_packet().rect_data().sequence_number()); |
| 93 |
| 94 // Copy the data line by line. |
| 95 const int src_stride = bytes_per_pixel_ * rect_width_; |
| 96 const char* src = |
| 97 message->update_stream_packet().rect_data().data().c_str(); |
| 98 int src_stride_dir = src_stride; |
| 99 if (reverse_rows_) { |
| 100 // Copy rows from bottom to top to flip the image vertically. |
| 101 src += (rect_height_ - 1) * src_stride; |
| 102 // Change the direction of the stride to work bottom to top. |
| 103 src_stride_dir *= -1; |
| 104 } |
| 105 const int dest_stride = frame_->stride(media::VideoFrame::kRGBPlane); |
| 106 uint8* dest = frame_->data(media::VideoFrame::kRGBPlane) + |
| 107 dest_stride * rect_y_ + bytes_per_pixel_ * rect_x_; |
| 108 for (int i = 0; i < rect_height_; ++i) { |
| 109 memcpy(dest, src, src_stride); |
| 110 dest += dest_stride; |
| 111 src += src_stride_dir; |
| 112 } |
| 113 |
| 114 updated_rects_->clear(); |
| 115 updated_rects_->push_back(gfx::Rect(rect_x_, rect_y_, |
| 116 rect_width_, rect_height_)); |
| 117 partial_decode_done_->Run(); |
| 118 return true; |
| 119 } |
| 120 |
| 121 bool DecoderVerbatim::HandleEndRect(HostMessage* message) { |
| 122 DCHECK_EQ(kWaitingForRectData, state_); |
| 123 state_ = kWaitingForBeginRect; |
| 124 return true; |
| 125 } |
| 126 |
| 86 } // namespace remoting | 127 } // namespace remoting |
| OLD | NEW |