forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_frame_writer.cc
154 lines (124 loc) · 5.15 KB
/
video_frame_writer.cc
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 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/test/video_frame_writer.h"
#include <stdint.h>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/stringprintf.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
namespace {
const base::FilePath::CharType kFrameFileName[] =
FILE_PATH_LITERAL("frame.png");
const base::FilePath::CharType kRemotingFolder[] =
FILE_PATH_LITERAL("remoting");
const base::FilePath::CharType kDumpFrameFolder[] =
FILE_PATH_LITERAL("dumped_images");
// Used to create a unique folder path.
const char kDateAndTimeFormatString[] = "%d-%d-%d_%d-%d-%d";
} // namespace
namespace remoting {
namespace test {
VideoFrameWriter::VideoFrameWriter()
: instance_creation_time_(base::Time::Now()),
frame_name_unique_number_(0) {}
VideoFrameWriter::~VideoFrameWriter() {}
void VideoFrameWriter::WriteFrameToPath(const webrtc::DesktopFrame& frame,
const base::FilePath& image_path) {
unsigned char* frame_data = reinterpret_cast<unsigned char*>(frame.data());
std::vector<unsigned char> png_encoded_data;
if (!gfx::PNGCodec::Encode(
frame_data, gfx::PNGCodec::FORMAT_BGRA,
gfx::Size(frame.size().width(), frame.size().height()),
frame.stride(), true, std::vector<gfx::PNGCodec::Comment>(),
&png_encoded_data)) {
LOG(WARNING) << "Failed to encode frame to PNG file";
return;
}
// Dump contents (unsigned chars) to a file as a sequence of chars.
int write_bytes = base::WriteFile(
image_path, reinterpret_cast<char*>(&*png_encoded_data.begin()),
static_cast<int>(png_encoded_data.size()));
if (write_bytes != static_cast<int>(png_encoded_data.size())) {
LOG(WARNING) << "Failed to write frame to disk";
}
}
// Save video frame to path named with the |instance_creation_time|.
void VideoFrameWriter::WriteFrameToDefaultPath(
const webrtc::DesktopFrame& frame) {
base::FilePath dump_frame_file_path;
if (!GetTempDir(&dump_frame_file_path)) {
LOG(WARNING) << "Failed to retrieve temporary directory path";
return;
}
dump_frame_file_path = dump_frame_file_path.Append(kRemotingFolder);
dump_frame_file_path = dump_frame_file_path.Append(kDumpFrameFolder);
if (!CreateDirectoryIfNotExists(dump_frame_file_path)) {
return;
}
// Create a sub-folder using date and time to identify a particular test run.
dump_frame_file_path = AppendCreationDateAndTime(dump_frame_file_path);
if (!CreateDirectoryIfNotExists(dump_frame_file_path)) {
return;
}
dump_frame_file_path = dump_frame_file_path.Append(kFrameFileName);
dump_frame_file_path = dump_frame_file_path.InsertBeforeExtensionASCII(
base::StringPrintf("(%d)", ++frame_name_unique_number_));
LOG(INFO) << "Video frame dumped to: " << dump_frame_file_path.value();
WriteFrameToPath(frame, dump_frame_file_path);
}
void VideoFrameWriter::HighlightRectInFrame(webrtc::DesktopFrame* frame,
const webrtc::DesktopRect& rect) {
if (rect.left() < 0 || rect.top() < 0 ||
rect.right() >= frame->size().width() ||
rect.bottom() >= frame->size().height()) {
LOG(ERROR) << "Highlight rect lies outside of the frame.";
return;
}
// Draw vertical borders.
for (int y = rect.top(); y <= rect.bottom(); ++y) {
ShiftPixelColor(frame, rect.left(), y, 128);
ShiftPixelColor(frame, rect.right(), y, 128);
}
// Draw horizontal borders.
for (int x = rect.left(); x <= rect.right(); ++x) {
ShiftPixelColor(frame, x, rect.top(), 128);
ShiftPixelColor(frame, x, rect.bottom(), 128);
}
}
base::FilePath VideoFrameWriter::AppendCreationDateAndTime(
const base::FilePath& file_path) {
base::Time::Exploded exploded_time;
instance_creation_time_.LocalExplode(&exploded_time);
int year = exploded_time.year;
int month = exploded_time.month;
int day = exploded_time.day_of_month;
int hour = exploded_time.hour;
int minute = exploded_time.minute;
int second = exploded_time.second;
return file_path.AppendASCII(base::StringPrintf(
kDateAndTimeFormatString, year, month, day, hour, minute, second));
}
bool VideoFrameWriter::CreateDirectoryIfNotExists(
const base::FilePath& file_path) {
if (!base::DirectoryExists(file_path) && !base::CreateDirectory(file_path)) {
LOG(WARNING) << "Failed to create directory: " << file_path.value();
return false;
}
return true;
}
void VideoFrameWriter::ShiftPixelColor(webrtc::DesktopFrame* frame,
int x,
int y,
int shift_amount) {
uint8_t* frame_pos = frame->data() + y * frame->stride() +
x * webrtc::DesktopFrame::kBytesPerPixel;
frame_pos[2] = frame_pos[2] + shift_amount;
frame_pos[1] = frame_pos[1] + shift_amount;
frame_pos[0] = frame_pos[0] + shift_amount;
}
} // namespace test
} // namespace remoting