1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/encoder_verbatim.h"
#include "gfx/rect.h"
#include "media/base/data_buffer.h"
#include "remoting/base/protocol_util.h"
namespace remoting {
using media::DataBuffer;
void EncoderVerbatim::Encode(scoped_refptr<Capturer::CaptureData> capture_data,
bool key_frame,
DataAvailableCallback* data_available_callback) {
int num_rects = capture_data->dirty_rects().size();
for (int i = 0; i < num_rects; i++) {
scoped_refptr<DataBuffer> data;
const gfx::Rect& dirty_rect = capture_data->dirty_rects()[i];
scoped_ptr<UpdateStreamPacketHeader> header(new UpdateStreamPacketHeader);
if (EncodeRect(dirty_rect,
capture_data,
header.get(),
&data)) {
EncodingState state = EncodingInProgress;
if (i == 0) {
state |= EncodingStarting;
}
if (i == num_rects - 1) {
state |= EncodingEnded;
}
data_available_callback->Run(header.release(),
data,
state);
}
}
delete data_available_callback;
}
bool EncoderVerbatim::EncodeRect(
const gfx::Rect& dirty,
const scoped_refptr<Capturer::CaptureData>& capture_data,
UpdateStreamPacketHeader* header,
scoped_refptr<DataBuffer>* output_data) {
int bytes_per_pixel = GetBytesPerPixel(capture_data->pixel_format());
int row_size = bytes_per_pixel * dirty.width();
// Calculate the size of output.
int output_size = 0;
for (int i = 0; i < Capturer::DataPlanes::kPlaneCount; ++i) {
// TODO(hclam): Handle YUV since the height would be different.
const uint8* in = capture_data->data_planes().data[i];
if (!in) continue;
output_size += row_size * dirty.height();
}
header->set_x(dirty.x());
header->set_y(dirty.y());
header->set_width(dirty.width());
header->set_height(dirty.height());
header->set_encoding(EncodingNone);
header->set_pixel_format(capture_data->pixel_format());
*output_data = new DataBuffer(new uint8[output_size], output_size);
uint8* out = (*output_data)->GetWritableData();
for (int i = 0; i < Capturer::DataPlanes::kPlaneCount; ++i) {
const uint8* in = capture_data->data_planes().data[i];
// Skip over planes that don't have data.
if (!in) continue;
// TODO(hclam): Handle YUV since the height would be different.
for (int j = 0; j < dirty.height(); ++j) {
DCHECK_LE(row_size, capture_data->data_planes().strides[i]);
memcpy(out, in, row_size);
in += capture_data->data_planes().strides[i];
out += row_size;
}
}
return true;
}
} // namespace remoting
|