| 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/base/encoder_zlib.h" | 5 #include "remoting/base/encoder_zlib.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "gfx/rect.h" | 8 #include "gfx/rect.h" |
| 9 #include "media/base/data_buffer.h" | 9 #include "media/base/data_buffer.h" |
| 10 #include "remoting/base/capture_data.h" | 10 #include "remoting/base/capture_data.h" |
| 11 #include "remoting/base/compressor_zlib.h" | 11 #include "remoting/base/compressor_zlib.h" |
| 12 #include "remoting/base/util.h" | 12 #include "remoting/base/util.h" |
| 13 | 13 |
| 14 namespace remoting { | 14 namespace remoting { |
| 15 | 15 |
| 16 static const int kPacketSize = 1024 * 1024; | 16 static const int kPacketSize = 1024 * 1024; |
| 17 | 17 |
| 18 EncoderZlib::EncoderZlib() : packet_size_(kPacketSize) { | 18 EncoderZlib::EncoderZlib() : packet_size_(kPacketSize) { |
| 19 } | 19 } |
| 20 | 20 |
| 21 EncoderZlib::EncoderZlib(int packet_size) : packet_size_(packet_size) { | 21 EncoderZlib::EncoderZlib(int packet_size) : packet_size_(packet_size) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 EncoderZlib::~EncoderZlib() {} | 24 EncoderZlib::~EncoderZlib() {} |
| 25 | 25 |
| 26 void EncoderZlib::Encode(scoped_refptr<CaptureData> capture_data, | 26 void EncoderZlib::Encode(scoped_refptr<CaptureData> capture_data, |
| 27 bool key_frame, | 27 bool key_frame, |
| 28 DataAvailableCallback* data_available_callback) { | 28 DataAvailableCallback* data_available_callback) { |
| 29 CHECK(capture_data->pixel_format() == PIXEL_FORMAT_RGB32) | 29 CHECK(capture_data->pixel_format() == PixelFormatRgb32) |
| 30 << "Zlib Encoder only works with RGB32. Got " | 30 << "Zlib Encoder only works with RGB32. Got " |
| 31 << capture_data->pixel_format(); | 31 << capture_data->pixel_format(); |
| 32 capture_data_ = capture_data; | 32 capture_data_ = capture_data; |
| 33 callback_.reset(data_available_callback); | 33 callback_.reset(data_available_callback); |
| 34 | 34 |
| 35 CompressorZlib compressor; | 35 CompressorZlib compressor; |
| 36 const InvalidRects& rects = capture_data->dirty_rects(); | 36 const InvalidRects& rects = capture_data->dirty_rects(); |
| 37 int index = 0; | 37 int index = 0; |
| 38 for (InvalidRects::const_iterator r = rects.begin(); | 38 for (InvalidRects::const_iterator r = rects.begin(); |
| 39 r != rects.end(); ++r, ++index) { | 39 r != rects.end(); ++r, ++index) { |
| 40 EncodeRect(&compressor, *r, index); | 40 EncodeRect(&compressor, *r, index); |
| 41 } | 41 } |
| 42 | 42 |
| 43 capture_data_ = NULL; | 43 capture_data_ = NULL; |
| 44 callback_.reset(); | 44 callback_.reset(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void EncoderZlib::EncodeRect(CompressorZlib* compressor, | 47 void EncoderZlib::EncodeRect(CompressorZlib* compressor, |
| 48 const gfx::Rect& rect, size_t rect_index) { | 48 const gfx::Rect& rect, size_t rect_index) { |
| 49 CHECK(capture_data_->data_planes().data[0]); | 49 CHECK(capture_data_->data_planes().data[0]); |
| 50 const int strides = capture_data_->data_planes().strides[0]; | 50 const int strides = capture_data_->data_planes().strides[0]; |
| 51 const int bytes_per_pixel = GetBytesPerPixel(capture_data_->pixel_format()); | 51 const int bytes_per_pixel = GetBytesPerPixel(capture_data_->pixel_format()); |
| 52 const int row_size = bytes_per_pixel * rect.width(); | 52 const int row_size = bytes_per_pixel * rect.width(); |
| 53 | 53 |
| 54 VideoPacket* packet = new VideoPacket(); | 54 ChromotingHostMessage* message = new ChromotingHostMessage(); |
| 55 PrepareUpdateStart(rect, packet); | 55 RectangleUpdatePacket* update = message->mutable_rectangle_update(); |
| 56 PrepareUpdateStart(rect, update); |
| 56 const uint8* in = capture_data_->data_planes().data[0] + | 57 const uint8* in = capture_data_->data_planes().data[0] + |
| 57 rect.y() * strides + | 58 rect.y() * strides + |
| 58 rect.x() * bytes_per_pixel; | 59 rect.x() * bytes_per_pixel; |
| 59 // TODO(hclam): Fill in the sequence number. | 60 // TODO(hclam): Fill in the sequence number. |
| 60 uint8* out = GetOutputBuffer(packet, packet_size_); | 61 uint8* out = GetOutputBuffer(update, packet_size_); |
| 61 int filled = 0; | 62 int filled = 0; |
| 62 int row_x = 0; | 63 int row_x = 0; |
| 63 int row_y = 0; | 64 int row_y = 0; |
| 64 bool compress_again = true; | 65 bool compress_again = true; |
| 65 while (compress_again) { | 66 while (compress_again) { |
| 66 // Prepare a message for sending out. | 67 // Prepare a message for sending out. |
| 67 if (!packet) { | 68 if (!message) { |
| 68 packet = new VideoPacket(); | 69 message = new ChromotingHostMessage(); |
| 69 out = GetOutputBuffer(packet, packet_size_); | 70 update = message->mutable_rectangle_update(); |
| 71 out = GetOutputBuffer(update, packet_size_); |
| 70 filled = 0; | 72 filled = 0; |
| 71 } | 73 } |
| 72 | 74 |
| 73 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush; | 75 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush; |
| 74 if (row_y == rect.height() - 1) { | 76 if (row_y == rect.height() - 1) { |
| 75 if (rect_index == capture_data_->dirty_rects().size() - 1) { | 77 if (rect_index == capture_data_->dirty_rects().size() - 1) { |
| 76 flush = Compressor::CompressorFinish; | 78 flush = Compressor::CompressorFinish; |
| 77 } else { | 79 } else { |
| 78 flush = Compressor::CompressorSyncFlush; | 80 flush = Compressor::CompressorSyncFlush; |
| 79 } | 81 } |
| 80 } | 82 } |
| 81 | 83 |
| 82 int consumed = 0; | 84 int consumed = 0; |
| 83 int written = 0; | 85 int written = 0; |
| 84 compress_again = compressor->Process(in + row_x, row_size - row_x, | 86 compress_again = compressor->Process(in + row_x, row_size - row_x, |
| 85 out + filled, packet_size_ - filled, | 87 out + filled, packet_size_ - filled, |
| 86 flush, &consumed, &written); | 88 flush, &consumed, &written); |
| 87 row_x += consumed; | 89 row_x += consumed; |
| 88 filled += written; | 90 filled += written; |
| 89 | 91 |
| 90 // We have reached the end of stream. | 92 // We have reached the end of stream. |
| 91 if (!compress_again) { | 93 if (!compress_again) { |
| 92 packet->set_flags(packet->flags() | VideoPacket::LAST_PACKET); | 94 update->set_flags(update->flags() | RectangleUpdatePacket::LAST_PACKET); |
| 93 } | 95 } |
| 94 | 96 |
| 95 // If we have filled the message or we have reached the end of stream. | 97 // If we have filled the message or we have reached the end of stream. |
| 96 if (filled == packet_size_ || !compress_again) { | 98 if (filled == packet_size_ || !compress_again) { |
| 97 packet->mutable_data()->resize(filled); | 99 message->mutable_rectangle_update()->mutable_encoded_rect()-> |
| 98 SubmitMessage(packet, rect_index); | 100 resize(filled); |
| 99 packet = NULL; | 101 SubmitMessage(message, rect_index); |
| 102 message = NULL; |
| 100 } | 103 } |
| 101 | 104 |
| 102 // Reached the end of input row and we're not at the last row. | 105 // Reached the end of input row and we're not at the last row. |
| 103 if (row_x == row_size && row_y < rect.height() - 1) { | 106 if (row_x == row_size && row_y < rect.height() - 1) { |
| 104 row_x = 0; | 107 row_x = 0; |
| 105 in += strides; | 108 in += strides; |
| 106 ++row_y; | 109 ++row_y; |
| 107 } | 110 } |
| 108 } | 111 } |
| 109 } | 112 } |
| 110 | 113 |
| 111 void EncoderZlib::PrepareUpdateStart(const gfx::Rect& rect, | 114 void EncoderZlib::PrepareUpdateStart(const gfx::Rect& rect, |
| 112 VideoPacket* packet) { | 115 RectangleUpdatePacket* update) { |
| 113 packet->set_flags(packet->flags() | VideoPacket::FIRST_PACKET); | 116 |
| 114 VideoPacketFormat* format = packet->mutable_format(); | 117 update->set_flags(update->flags() | RectangleUpdatePacket::FIRST_PACKET); |
| 118 RectangleFormat* format = update->mutable_format(); |
| 115 | 119 |
| 116 format->set_x(rect.x()); | 120 format->set_x(rect.x()); |
| 117 format->set_y(rect.y()); | 121 format->set_y(rect.y()); |
| 118 format->set_width(rect.width()); | 122 format->set_width(rect.width()); |
| 119 format->set_height(rect.height()); | 123 format->set_height(rect.height()); |
| 120 format->set_encoding(VideoPacketFormat::ENCODING_ZLIB); | 124 format->set_encoding(EncodingZlib); |
| 121 format->set_pixel_format(capture_data_->pixel_format()); | 125 format->set_pixel_format(capture_data_->pixel_format()); |
| 122 } | 126 } |
| 123 | 127 |
| 124 uint8* EncoderZlib::GetOutputBuffer(VideoPacket* packet, size_t size) { | 128 uint8* EncoderZlib::GetOutputBuffer(RectangleUpdatePacket* update, |
| 125 packet->mutable_data()->resize(size); | 129 size_t size) { |
| 130 update->mutable_encoded_rect()->resize(size); |
| 126 // TODO(ajwong): Is there a better way to do this at all??? | 131 // TODO(ajwong): Is there a better way to do this at all??? |
| 127 return const_cast<uint8*>(reinterpret_cast<const uint8*>( | 132 return const_cast<uint8*>(reinterpret_cast<const uint8*>( |
| 128 packet->mutable_data()->data())); | 133 update->mutable_encoded_rect()->data())); |
| 129 } | 134 } |
| 130 | 135 |
| 131 void EncoderZlib::SubmitMessage(VideoPacket* packet, size_t rect_index) { | 136 void EncoderZlib::SubmitMessage(ChromotingHostMessage* message, |
| 132 callback_->Run(packet); | 137 size_t rect_index) { |
| 138 EncodingState state = EncodingInProgress; |
| 139 const RectangleUpdatePacket& update = message->rectangle_update(); |
| 140 if (rect_index == 0 && |
| 141 (update.flags() | RectangleUpdatePacket::FIRST_PACKET)) { |
| 142 state |= EncodingStarting; |
| 143 } |
| 144 if (rect_index == capture_data_->dirty_rects().size() - 1 && |
| 145 (update.flags() | RectangleUpdatePacket::LAST_PACKET)) { |
| 146 state |= EncodingEnded; |
| 147 } |
| 148 callback_->Run(message, state); |
| 133 } | 149 } |
| 134 | 150 |
| 135 } // namespace remoting | 151 } // namespace remoting |
| OLD | NEW |