| 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 "gfx/rect.h" | 7 #include "gfx/rect.h" |
| 8 #include "media/base/data_buffer.h" | 8 #include "media/base/data_buffer.h" |
| 9 #include "remoting/base/capture_data.h" | 9 #include "remoting/base/capture_data.h" |
| 10 #include "remoting/base/compressor_zlib.h" | 10 #include "remoting/base/compressor_zlib.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 void EncoderZlib::Encode(scoped_refptr<CaptureData> capture_data, | 24 void EncoderZlib::Encode(scoped_refptr<CaptureData> capture_data, |
| 25 bool key_frame, | 25 bool key_frame, |
| 26 DataAvailableCallback* data_available_callback) { | 26 DataAvailableCallback* data_available_callback) { |
| 27 CHECK(capture_data->pixel_format() == PixelFormatRgb32) | 27 CHECK(capture_data->pixel_format() == PixelFormatRgb32) |
| 28 << "Zlib Encoder only works with RGB32"; | 28 << "Zlib Encoder only works with RGB32"; |
| 29 capture_data_ = capture_data; | 29 capture_data_ = capture_data; |
| 30 callback_.reset(data_available_callback); | 30 callback_.reset(data_available_callback); |
| 31 | 31 |
| 32 CompressorZlib compressor; | 32 CompressorZlib compressor; |
| 33 for (current_rect_ = 0; current_rect_ < capture_data->dirty_rects().size(); | 33 const InvalidRects& rects = capture_data->dirty_rects(); |
| 34 ++current_rect_) { | 34 int index = 0; |
| 35 EncodeRect(&compressor); | 35 for (InvalidRects::const_iterator r = rects.begin(); |
| 36 r != rects.end(); ++r, ++index) { |
| 37 EncodeRect(&compressor, *r, index); |
| 36 } | 38 } |
| 37 | 39 |
| 38 capture_data_ = NULL; | 40 capture_data_ = NULL; |
| 39 callback_.reset(); | 41 callback_.reset(); |
| 40 current_rect_ = 0; | |
| 41 } | 42 } |
| 42 | 43 |
| 43 void EncoderZlib::EncodeRect(CompressorZlib* compressor) { | 44 void EncoderZlib::EncodeRect(CompressorZlib* compressor, |
| 45 const gfx::Rect& rect, size_t rect_index) { |
| 44 CHECK(capture_data_->data_planes().data[0]); | 46 CHECK(capture_data_->data_planes().data[0]); |
| 45 const gfx::Rect rect = capture_data_->dirty_rects()[current_rect_]; | |
| 46 const int strides = capture_data_->data_planes().strides[0]; | 47 const int strides = capture_data_->data_planes().strides[0]; |
| 47 const int bytes_per_pixel = GetBytesPerPixel(capture_data_->pixel_format()); | 48 const int bytes_per_pixel = GetBytesPerPixel(capture_data_->pixel_format()); |
| 48 const int row_size = bytes_per_pixel * rect.width(); | 49 const int row_size = bytes_per_pixel * rect.width(); |
| 49 | 50 |
| 50 HostMessage* message = PrepareMessage(true); | 51 HostMessage* message = PrepareMessage(&rect); |
| 51 const uint8 * in = capture_data_->data_planes().data[0] + | 52 const uint8 * in = capture_data_->data_planes().data[0] + |
| 52 rect.y() * strides + | 53 rect.y() * strides + |
| 53 rect.x() * bytes_per_pixel; | 54 rect.x() * bytes_per_pixel; |
| 54 // TODO(hclam): Fill in the sequence number. | 55 // TODO(hclam): Fill in the sequence number. |
| 55 uint8* out = (uint8*)message->mutable_update_stream_packet()-> | 56 uint8* out = (uint8*)message->mutable_update_stream_packet()-> |
| 56 mutable_rect_data()->mutable_data()->data(); | 57 mutable_rect_data()->mutable_data()->data(); |
| 57 int filled = 0; | 58 int filled = 0; |
| 58 int row_x = 0; | 59 int row_x = 0; |
| 59 int row_y = 0; | 60 int row_y = 0; |
| 60 bool compress_again = true; | 61 bool compress_again = true; |
| 61 while (compress_again) { | 62 while (compress_again) { |
| 62 // Prepare a message for sending out. | 63 // Prepare a message for sending out. |
| 63 if (!message) { | 64 if (!message) { |
| 64 message = PrepareMessage(false); | 65 message = PrepareMessage(NULL); |
| 65 out = (uint8*)(message->mutable_update_stream_packet()-> | 66 out = (uint8*)(message->mutable_update_stream_packet()-> |
| 66 mutable_rect_data()->mutable_data()->data()); | 67 mutable_rect_data()->mutable_data()->data()); |
| 67 filled = 0; | 68 filled = 0; |
| 68 } | 69 } |
| 69 | 70 |
| 70 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush; | 71 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush; |
| 71 if (row_y == rect.height() - 1) { | 72 if (row_y == rect.height() - 1) { |
| 72 if (current_rect_ == capture_data_->dirty_rects().size() - 1) | 73 if (rect_index == capture_data_->dirty_rects().size() - 1) { |
| 73 flush = Compressor::CompressorFinish; | 74 flush = Compressor::CompressorFinish; |
| 74 else | 75 } else { |
| 75 flush = Compressor::CompressorSyncFlush; | 76 flush = Compressor::CompressorSyncFlush; |
| 77 } |
| 76 } | 78 } |
| 77 | 79 |
| 78 int consumed = 0; | 80 int consumed = 0; |
| 79 int written = 0; | 81 int written = 0; |
| 80 compress_again = compressor->Process(in + row_x, row_size - row_x, | 82 compress_again = compressor->Process(in + row_x, row_size - row_x, |
| 81 out + filled, packet_size_ - filled, | 83 out + filled, packet_size_ - filled, |
| 82 flush, &consumed, &written); | 84 flush, &consumed, &written); |
| 83 row_x += consumed; | 85 row_x += consumed; |
| 84 filled += written; | 86 filled += written; |
| 85 | 87 |
| 86 // We have reached the end of stream. | 88 // We have reached the end of stream. |
| 87 if (!compress_again) { | 89 if (!compress_again) { |
| 88 message->mutable_update_stream_packet()->mutable_end_rect(); | 90 message->mutable_update_stream_packet()->mutable_end_rect(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 // If we have filled the message or we have reached the end of stream. | 93 // If we have filled the message or we have reached the end of stream. |
| 92 if (filled == packet_size_ || !compress_again) { | 94 if (filled == packet_size_ || !compress_again) { |
| 93 message->mutable_update_stream_packet()->mutable_rect_data()-> | 95 message->mutable_update_stream_packet()->mutable_rect_data()-> |
| 94 mutable_data()->resize(filled); | 96 mutable_data()->resize(filled); |
| 95 SubmitMessage(message); | 97 SubmitMessage(message, rect_index); |
| 96 message = NULL; | 98 message = NULL; |
| 97 } | 99 } |
| 98 | 100 |
| 99 // Reached the end of input row and we're not at the last row. | 101 // Reached the end of input row and we're not at the last row. |
| 100 if (row_x == row_size && row_y < rect.height() - 1) { | 102 if (row_x == row_size && row_y < rect.height() - 1) { |
| 101 row_x = 0; | 103 row_x = 0; |
| 102 in += strides; | 104 in += strides; |
| 103 ++row_y; | 105 ++row_y; |
| 104 } | 106 } |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 | 109 |
| 108 HostMessage* EncoderZlib::PrepareMessage(bool new_rect) { | 110 HostMessage* EncoderZlib::PrepareMessage(const gfx::Rect* rect) { |
| 109 HostMessage* message = new HostMessage(); | 111 HostMessage* message = new HostMessage(); |
| 110 UpdateStreamPacketMessage* packet = message->mutable_update_stream_packet(); | 112 UpdateStreamPacketMessage* packet = message->mutable_update_stream_packet(); |
| 111 | 113 |
| 112 // Prepare the begin rect content. | 114 // Prepare the begin rect content. |
| 113 if (new_rect) { | 115 if (rect != NULL) { |
| 114 gfx::Rect rect = capture_data_->dirty_rects()[current_rect_]; | 116 packet->mutable_begin_rect()->set_x(rect->x()); |
| 115 packet->mutable_begin_rect()->set_x(rect.x()); | 117 packet->mutable_begin_rect()->set_y(rect->y()); |
| 116 packet->mutable_begin_rect()->set_y(rect.y()); | 118 packet->mutable_begin_rect()->set_width(rect->width()); |
| 117 packet->mutable_begin_rect()->set_width(rect.width()); | 119 packet->mutable_begin_rect()->set_height(rect->height()); |
| 118 packet->mutable_begin_rect()->set_height(rect.height()); | |
| 119 packet->mutable_begin_rect()->set_encoding(EncodingZlib); | 120 packet->mutable_begin_rect()->set_encoding(EncodingZlib); |
| 120 packet->mutable_begin_rect()->set_pixel_format( | 121 packet->mutable_begin_rect()->set_pixel_format( |
| 121 capture_data_->pixel_format()); | 122 capture_data_->pixel_format()); |
| 122 } | 123 } |
| 123 | 124 |
| 124 packet->mutable_rect_data()->mutable_data()->resize(packet_size_); | 125 packet->mutable_rect_data()->mutable_data()->resize(packet_size_); |
| 125 return message; | 126 return message; |
| 126 } | 127 } |
| 127 | 128 |
| 128 void EncoderZlib::SubmitMessage(HostMessage* message) { | 129 void EncoderZlib::SubmitMessage(HostMessage* message, size_t rect_index) { |
| 129 EncodingState state = EncodingInProgress; | 130 EncodingState state = EncodingInProgress; |
| 130 if (current_rect_ == 0 && message->update_stream_packet().has_begin_rect()) | 131 if (rect_index == 0 && message->update_stream_packet().has_begin_rect()) |
| 131 state |= EncodingStarting; | 132 state |= EncodingStarting; |
| 132 if (current_rect_ == capture_data_->dirty_rects().size() - 1 && | 133 if (rect_index == capture_data_->dirty_rects().size() - 1 && |
| 133 message->update_stream_packet().has_end_rect()) | 134 message->update_stream_packet().has_end_rect()) |
| 134 state |= EncodingEnded; | 135 state |= EncodingEnded; |
| 135 callback_->Run(message, state); | 136 callback_->Run(message, state); |
| 136 } | 137 } |
| 137 | 138 |
| 138 } // namespace remoting | 139 } // namespace remoting |
| OLD | NEW |