| 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/host/encoder_verbatim.h" | 5 #include "remoting/host/encoder_verbatim.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/protocol_util.h" | 9 #include "remoting/base/protocol_util.h" |
| 10 #include "remoting/base/protocol/chromotocol.pb.h" |
| 10 | 11 |
| 11 namespace remoting { | 12 namespace remoting { |
| 12 | 13 |
| 13 using media::DataBuffer; | 14 using media::DataBuffer; |
| 14 | 15 |
| 15 void EncoderVerbatim::Encode(scoped_refptr<Capturer::CaptureData> capture_data, | 16 void EncoderVerbatim::Encode(scoped_refptr<Capturer::CaptureData> capture_data, |
| 16 bool key_frame, | 17 bool key_frame, |
| 17 DataAvailableCallback* data_available_callback) { | 18 DataAvailableCallback* data_available_callback) { |
| 18 int num_rects = capture_data->dirty_rects().size(); | 19 int num_rects = capture_data->dirty_rects().size(); |
| 19 for (int i = 0; i < num_rects; i++) { | 20 for (int i = 0; i < num_rects; i++) { |
| 20 scoped_refptr<DataBuffer> data; | |
| 21 const gfx::Rect& dirty_rect = capture_data->dirty_rects()[i]; | 21 const gfx::Rect& dirty_rect = capture_data->dirty_rects()[i]; |
| 22 scoped_ptr<UpdateStreamPacketHeader> header(new UpdateStreamPacketHeader); | 22 HostMessage* msg = new HostMessage(); |
| 23 if (EncodeRect(dirty_rect, | 23 UpdateStreamPacketMessage* packet = msg->mutable_update_stream_packet(); |
| 24 capture_data, | 24 |
| 25 header.get(), | 25 if (EncodeRect(dirty_rect, capture_data, packet)) { |
| 26 &data)) { | 26 // Prepare the end rect content. |
| 27 packet->mutable_end_rect(); |
| 28 |
| 27 EncodingState state = EncodingInProgress; | 29 EncodingState state = EncodingInProgress; |
| 28 if (i == 0) { | 30 if (i == 0) { |
| 29 state |= EncodingStarting; | 31 state |= EncodingStarting; |
| 30 } | 32 } |
| 31 if (i == num_rects - 1) { | 33 if (i == num_rects - 1) { |
| 32 state |= EncodingEnded; | 34 state |= EncodingEnded; |
| 33 } | 35 } |
| 34 data_available_callback->Run(header.release(), | 36 data_available_callback->Run(msg, state); |
| 35 data, | |
| 36 state); | |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 delete data_available_callback; | 40 delete data_available_callback; |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool EncoderVerbatim::EncodeRect( | 43 bool EncoderVerbatim::EncodeRect( |
| 44 const gfx::Rect& dirty, | 44 const gfx::Rect& dirty, |
| 45 const scoped_refptr<Capturer::CaptureData>& capture_data, | 45 const scoped_refptr<Capturer::CaptureData>& capture_data, |
| 46 UpdateStreamPacketHeader* header, | 46 UpdateStreamPacketMessage* packet) { |
| 47 scoped_refptr<DataBuffer>* output_data) { | 47 // Prepare the begin rect content. |
| 48 packet->mutable_begin_rect()->set_x(dirty.x()); |
| 49 packet->mutable_begin_rect()->set_y(dirty.y()); |
| 50 packet->mutable_begin_rect()->set_width(dirty.width()); |
| 51 packet->mutable_begin_rect()->set_height(dirty.height()); |
| 52 packet->mutable_begin_rect()->set_encoding(EncodingNone); |
| 53 packet->mutable_begin_rect()->set_pixel_format(capture_data->pixel_format()); |
| 54 |
| 55 // Calculate the size of output. |
| 48 int bytes_per_pixel = GetBytesPerPixel(capture_data->pixel_format()); | 56 int bytes_per_pixel = GetBytesPerPixel(capture_data->pixel_format()); |
| 49 int row_size = bytes_per_pixel * dirty.width(); | 57 int row_size = bytes_per_pixel * dirty.width(); |
| 50 | |
| 51 // Calculate the size of output. | |
| 52 int output_size = 0; | 58 int output_size = 0; |
| 53 for (int i = 0; i < Capturer::DataPlanes::kPlaneCount; ++i) { | 59 for (int i = 0; i < Capturer::DataPlanes::kPlaneCount; ++i) { |
| 54 // TODO(hclam): Handle YUV since the height would be different. | 60 // TODO(hclam): Handle YUV since the height would be different. |
| 55 const uint8* in = capture_data->data_planes().data[i]; | 61 const uint8* in = capture_data->data_planes().data[i]; |
| 56 if (!in) continue; | 62 if (!in) continue; |
| 57 output_size += row_size * dirty.height(); | 63 output_size += row_size * dirty.height(); |
| 58 } | 64 } |
| 59 | 65 |
| 60 header->set_x(dirty.x()); | 66 // Resize the output data buffer. |
| 61 header->set_y(dirty.y()); | 67 packet->mutable_rect_data()->mutable_data()->resize(output_size); |
| 62 header->set_width(dirty.width()); | 68 uint8* out = reinterpret_cast<uint8*>( |
| 63 header->set_height(dirty.height()); | 69 &((*packet->mutable_rect_data()->mutable_data())[0])); |
| 64 header->set_encoding(EncodingNone); | |
| 65 header->set_pixel_format(capture_data->pixel_format()); | |
| 66 | |
| 67 *output_data = new DataBuffer(new uint8[output_size], output_size); | |
| 68 uint8* out = (*output_data)->GetWritableData(); | |
| 69 | 70 |
| 70 for (int i = 0; i < Capturer::DataPlanes::kPlaneCount; ++i) { | 71 for (int i = 0; i < Capturer::DataPlanes::kPlaneCount; ++i) { |
| 71 const uint8* in = capture_data->data_planes().data[i]; | 72 const uint8* in = capture_data->data_planes().data[i]; |
| 72 // Skip over planes that don't have data. | 73 // Skip over planes that don't have data. |
| 73 if (!in) continue; | 74 if (!in) continue; |
| 74 | 75 |
| 75 // TODO(hclam): Handle YUV since the height would be different. | 76 // TODO(hclam): Handle YUV since the height would be different. |
| 76 for (int j = 0; j < dirty.height(); ++j) { | 77 for (int j = 0; j < dirty.height(); ++j) { |
| 77 DCHECK_LE(row_size, capture_data->data_planes().strides[i]); | 78 DCHECK_LE(row_size, capture_data->data_planes().strides[i]); |
| 78 memcpy(out, in, row_size); | 79 memcpy(out, in, row_size); |
| 79 in += capture_data->data_planes().strides[i]; | 80 in += capture_data->data_planes().strides[i]; |
| 80 out += row_size; | 81 out += row_size; |
| 81 } | 82 } |
| 82 } | 83 } |
| 83 return true; | 84 return true; |
| 84 } | 85 } |
| 85 | 86 |
| 86 } // namespace remoting | 87 } // namespace remoting |
| OLD | NEW |