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
|
// Copyright 2014 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 "content/browser/devtools/protocol/frame_recorder.h"
#include "base/base64.h"
#include "base/bind.h"
#include "base/macros.h"
#include "base/task_runner_util.h"
#include "base/threading/worker_pool.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
namespace content {
namespace devtools {
namespace page {
namespace {
static int kMaxRecordFrameCount = 180;
std::string EncodeFrame(const SkBitmap& bitmap) {
std::vector<unsigned char> data;
SkAutoLockPixels lock_image(bitmap);
bool encoded = gfx::PNGCodec::Encode(
reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
gfx::PNGCodec::FORMAT_SkBitmap,
gfx::Size(bitmap.width(), bitmap.height()),
bitmap.width() * bitmap.bytesPerPixel(),
false, std::vector<gfx::PNGCodec::Comment>(), &data);
if (!encoded)
return std::string();
std::string base_64_data;
base::Base64Encode(
base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()),
&base_64_data);
return base_64_data;
}
} // namespace
typedef DevToolsProtocolClient::Response Response;
FrameRecorder::FrameRecorder()
: host_(nullptr),
state_(Ready),
inflight_requests_count_(0),
max_frame_count_(0),
captured_frames_count_(0),
last_captured_frame_timestamp_(base::Time()),
weak_factory_(this) {
}
FrameRecorder::~FrameRecorder() {
}
void FrameRecorder::SetRenderViewHost(RenderViewHostImpl* host) {
host_ = host;
}
Response FrameRecorder::StartRecordingFrames(int max_frame_count) {
if (max_frame_count <= 0 || max_frame_count > kMaxRecordFrameCount)
return Response::InvalidParams("maxFrameCount");
if (state_ != Ready)
return Response::InternalError("Already recording");
state_ = Recording;
max_frame_count_ = max_frame_count;
captured_frames_count_ = 0;
last_captured_frame_timestamp_ = base::Time();
std::vector<scoped_refptr<devtools::page::RecordedFrame>> frames;
frames.reserve(max_frame_count);
frames_.swap(frames);
return Response::OK();
}
Response FrameRecorder::StopRecordingFrames(
StopRecordingFramesCallback callback) {
if (state_ != Recording)
return Response::InternalError("Not recording");
state_ = Encoding;
callback_ = callback;
MaybeSendResponse();
return Response::OK();
}
void FrameRecorder::OnSwapCompositorFrame() {
if (!host_ || state_ != Recording)
return;
if (captured_frames_count_ >= max_frame_count_)
return;
RenderWidgetHostViewBase* view =
static_cast<RenderWidgetHostViewBase*>(host_->GetView());
if (!view)
return;
inflight_requests_count_++;
view->CopyFromCompositingSurface(
gfx::Rect(),
gfx::Size(),
base::Bind(&FrameRecorder::FrameCaptured, weak_factory_.GetWeakPtr()),
kN32_SkColorType);
}
void FrameRecorder::FrameCaptured(
const SkBitmap& bitmap, ReadbackResponse response) {
inflight_requests_count_--;
base::Time timestamp = last_captured_frame_timestamp_;
last_captured_frame_timestamp_ = base::Time::Now();
if (timestamp.is_null() || response != READBACK_SUCCESS)
return;
captured_frames_count_++;
base::PostTaskAndReplyWithResult(
base::WorkerPool::GetTaskRunner(true).get(),
FROM_HERE,
base::Bind(&EncodeFrame, bitmap),
base::Bind(&FrameRecorder::FrameEncoded, weak_factory_.GetWeakPtr(),
timestamp.ToDoubleT()));
}
void FrameRecorder::FrameEncoded(
double timestamp, const std::string& encoded_frame) {
frames_.push_back(RecordedFrame::Create()
->set_data(encoded_frame)
->set_timestamp(timestamp));
MaybeSendResponse();
}
void FrameRecorder::MaybeSendResponse() {
if (state_ != Encoding)
return;
if (inflight_requests_count_ || frames_.size() != captured_frames_count_)
return;
callback_.Run(StopRecordingFramesResponse::Create()->set_frames(frames_));
std::vector<scoped_refptr<devtools::page::RecordedFrame>> frames;
frames_.swap(frames);
state_ = Ready;
}
} // namespace page
} // namespace devtools
} // namespace content
|