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() == PixelFormatRgb32) | 29 CHECK(capture_data->pixel_format() == PIXEL_FORMAT_RGB32) |
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 ChromotingHostMessage* message = new ChromotingHostMessage(); | 54 VideoPacket* packet = new VideoPacket(); |
55 RectangleUpdatePacket* update = message->mutable_rectangle_update(); | 55 PrepareUpdateStart(rect, packet); |
56 PrepareUpdateStart(rect, update); | |
57 const uint8* in = capture_data_->data_planes().data[0] + | 56 const uint8* in = capture_data_->data_planes().data[0] + |
58 rect.y() * strides + | 57 rect.y() * strides + |
59 rect.x() * bytes_per_pixel; | 58 rect.x() * bytes_per_pixel; |
60 // TODO(hclam): Fill in the sequence number. | 59 // TODO(hclam): Fill in the sequence number. |
61 uint8* out = GetOutputBuffer(update, packet_size_); | 60 uint8* out = GetOutputBuffer(packet, packet_size_); |
62 int filled = 0; | 61 int filled = 0; |
63 int row_x = 0; | 62 int row_x = 0; |
64 int row_y = 0; | 63 int row_y = 0; |
65 bool compress_again = true; | 64 bool compress_again = true; |
66 while (compress_again) { | 65 while (compress_again) { |
67 // Prepare a message for sending out. | 66 // Prepare a message for sending out. |
68 if (!message) { | 67 if (!packet) { |
69 message = new ChromotingHostMessage(); | 68 packet = new VideoPacket(); |
70 update = message->mutable_rectangle_update(); | 69 out = GetOutputBuffer(packet, packet_size_); |
71 out = GetOutputBuffer(update, packet_size_); | |
72 filled = 0; | 70 filled = 0; |
73 } | 71 } |
74 | 72 |
75 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush; | 73 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush; |
76 if (row_y == rect.height() - 1) { | 74 if (row_y == rect.height() - 1) { |
77 if (rect_index == capture_data_->dirty_rects().size() - 1) { | 75 if (rect_index == capture_data_->dirty_rects().size() - 1) { |
78 flush = Compressor::CompressorFinish; | 76 flush = Compressor::CompressorFinish; |
79 } else { | 77 } else { |
80 flush = Compressor::CompressorSyncFlush; | 78 flush = Compressor::CompressorSyncFlush; |
81 } | 79 } |
82 } | 80 } |
83 | 81 |
84 int consumed = 0; | 82 int consumed = 0; |
85 int written = 0; | 83 int written = 0; |
86 compress_again = compressor->Process(in + row_x, row_size - row_x, | 84 compress_again = compressor->Process(in + row_x, row_size - row_x, |
87 out + filled, packet_size_ - filled, | 85 out + filled, packet_size_ - filled, |
88 flush, &consumed, &written); | 86 flush, &consumed, &written); |
89 row_x += consumed; | 87 row_x += consumed; |
90 filled += written; | 88 filled += written; |
91 | 89 |
92 // We have reached the end of stream. | 90 // We have reached the end of stream. |
93 if (!compress_again) { | 91 if (!compress_again) { |
94 update->set_flags(update->flags() | RectangleUpdatePacket::LAST_PACKET); | 92 packet->set_flags(packet->flags() | VideoPacket::LAST_PACKET); |
95 } | 93 } |
96 | 94 |
97 // If we have filled the message or we have reached the end of stream. | 95 // If we have filled the message or we have reached the end of stream. |
98 if (filled == packet_size_ || !compress_again) { | 96 if (filled == packet_size_ || !compress_again) { |
99 message->mutable_rectangle_update()->mutable_encoded_rect()-> | 97 packet->mutable_data()->resize(filled); |
100 resize(filled); | 98 SubmitMessage(packet, rect_index); |
101 SubmitMessage(message, rect_index); | 99 packet = NULL; |
102 message = NULL; | |
103 } | 100 } |
104 | 101 |
105 // Reached the end of input row and we're not at the last row. | 102 // Reached the end of input row and we're not at the last row. |
106 if (row_x == row_size && row_y < rect.height() - 1) { | 103 if (row_x == row_size && row_y < rect.height() - 1) { |
107 row_x = 0; | 104 row_x = 0; |
108 in += strides; | 105 in += strides; |
109 ++row_y; | 106 ++row_y; |
110 } | 107 } |
111 } | 108 } |
112 } | 109 } |
113 | 110 |
114 void EncoderZlib::PrepareUpdateStart(const gfx::Rect& rect, | 111 void EncoderZlib::PrepareUpdateStart(const gfx::Rect& rect, |
115 RectangleUpdatePacket* update) { | 112 VideoPacket* packet) { |
116 | 113 packet->set_flags(packet->flags() | VideoPacket::FIRST_PACKET); |
117 update->set_flags(update->flags() | RectangleUpdatePacket::FIRST_PACKET); | 114 VideoPacketFormat* format = packet->mutable_format(); |
118 RectangleFormat* format = update->mutable_format(); | |
119 | 115 |
120 format->set_x(rect.x()); | 116 format->set_x(rect.x()); |
121 format->set_y(rect.y()); | 117 format->set_y(rect.y()); |
122 format->set_width(rect.width()); | 118 format->set_width(rect.width()); |
123 format->set_height(rect.height()); | 119 format->set_height(rect.height()); |
124 format->set_encoding(EncodingZlib); | 120 format->set_encoding(VideoPacketFormat::ENCODING_ZLIB); |
125 format->set_pixel_format(capture_data_->pixel_format()); | 121 format->set_pixel_format(capture_data_->pixel_format()); |
126 } | 122 } |
127 | 123 |
128 uint8* EncoderZlib::GetOutputBuffer(RectangleUpdatePacket* update, | 124 uint8* EncoderZlib::GetOutputBuffer(VideoPacket* packet, size_t size) { |
129 size_t size) { | 125 packet->mutable_data()->resize(size); |
130 update->mutable_encoded_rect()->resize(size); | |
131 // TODO(ajwong): Is there a better way to do this at all??? | 126 // TODO(ajwong): Is there a better way to do this at all??? |
132 return const_cast<uint8*>(reinterpret_cast<const uint8*>( | 127 return const_cast<uint8*>(reinterpret_cast<const uint8*>( |
133 update->mutable_encoded_rect()->data())); | 128 packet->mutable_data()->data())); |
134 } | 129 } |
135 | 130 |
136 void EncoderZlib::SubmitMessage(ChromotingHostMessage* message, | 131 void EncoderZlib::SubmitMessage(VideoPacket* packet, size_t rect_index) { |
137 size_t rect_index) { | 132 callback_->Run(packet); |
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); | |
149 } | 133 } |
150 | 134 |
151 } // namespace remoting | 135 } // namespace remoting |
OLD | NEW |