OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/devtools/protocol/frame_recorder.h" | 5 #include "content/browser/devtools/protocol/frame_recorder.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
12 #include "content/browser/renderer_host/render_view_host_impl.h" | 12 #include "content/browser/renderer_host/render_view_host_impl.h" |
13 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 13 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
16 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 namespace devtools { | 19 namespace devtools { |
20 namespace page { | 20 namespace page { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 static int kMaxRecordFrameCount = 180; | 24 static int kMaxRecordFrameCount = 180; |
25 | 25 |
26 std::string EncodeFrame(const SkBitmap& bitmap) { | 26 scoped_ptr<EncodedFrame> EncodeFrame( |
27 const SkBitmap& bitmap, double timestamp) { | |
27 std::vector<unsigned char> data; | 28 std::vector<unsigned char> data; |
28 SkAutoLockPixels lock_image(bitmap); | 29 SkAutoLockPixels lock_image(bitmap); |
29 bool encoded = gfx::PNGCodec::Encode( | 30 bool encoded = gfx::PNGCodec::Encode( |
30 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 31 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
31 gfx::PNGCodec::FORMAT_SkBitmap, | 32 gfx::PNGCodec::FORMAT_SkBitmap, |
32 gfx::Size(bitmap.width(), bitmap.height()), | 33 gfx::Size(bitmap.width(), bitmap.height()), |
33 bitmap.width() * bitmap.bytesPerPixel(), | 34 bitmap.width() * bitmap.bytesPerPixel(), |
34 false, std::vector<gfx::PNGCodec::Comment>(), &data); | 35 false, std::vector<gfx::PNGCodec::Comment>(), &data); |
35 | 36 |
37 scoped_ptr<EncodedFrame> result(new EncodedFrame(std::string(), timestamp)); | |
38 | |
36 if (!encoded) | 39 if (!encoded) |
37 return std::string(); | 40 return result.Pass(); |
38 | 41 |
39 std::string base_64_data; | 42 std::string base_64_data; |
40 base::Base64Encode( | 43 base::Base64Encode( |
41 base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), | 44 base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), |
42 &base_64_data); | 45 &result->first); |
43 | 46 |
44 return base_64_data; | 47 return result.Pass(); |
45 } | 48 } |
46 } // namespace | 49 } // namespace |
47 | 50 |
48 typedef DevToolsProtocolClient::Response Response; | 51 typedef DevToolsProtocolClient::Response Response; |
49 | 52 |
50 FrameRecorder::FrameRecorder() | 53 FrameRecorder::FrameRecorder() |
51 : host_(nullptr), | 54 : host_(nullptr), |
52 state_(Ready), | 55 state_(Ready), |
53 inflight_requests_count_(0), | 56 inflight_requests_count_(0), |
54 max_frame_count_(0), | 57 max_frame_count_(0), |
(...skipping 28 matching lines...) Expand all Loading... | |
83 Response FrameRecorder::StopRecordingFrames( | 86 Response FrameRecorder::StopRecordingFrames( |
84 StopRecordingFramesCallback callback) { | 87 StopRecordingFramesCallback callback) { |
85 if (state_ != Recording) | 88 if (state_ != Recording) |
86 return Response::InternalError("Not recording"); | 89 return Response::InternalError("Not recording"); |
87 state_ = Encoding; | 90 state_ = Encoding; |
88 callback_ = callback; | 91 callback_ = callback; |
89 MaybeSendResponse(); | 92 MaybeSendResponse(); |
90 return Response::OK(); | 93 return Response::OK(); |
91 } | 94 } |
92 | 95 |
96 Response FrameRecorder::CancelRecordingFrames() { | |
97 frame_encoded_callback_.Cancel(); | |
dgozman
2015/01/30 10:06:23
Where do you initialize this field?
eustas
2015/01/30 12:08:16
In StartRecoding... Removed too much during cleanu
| |
98 std::vector<scoped_refptr<devtools::page::RecordedFrame>> no_frames; | |
99 frames_.swap(no_frames); | |
100 if (state_ == Encoding) | |
101 callback_.Run(StopRecordingFramesResponse::Create()->set_frames(frames_)); | |
102 state_ = Ready; | |
103 return Response::OK(); | |
104 } | |
105 | |
93 void FrameRecorder::OnSwapCompositorFrame() { | 106 void FrameRecorder::OnSwapCompositorFrame() { |
94 if (!host_ || state_ != Recording) | 107 if (!host_ || state_ != Recording) |
95 return; | 108 return; |
96 if (captured_frames_count_ >= max_frame_count_) | 109 if (captured_frames_count_ >= max_frame_count_) |
97 return; | 110 return; |
98 RenderWidgetHostViewBase* view = | 111 RenderWidgetHostViewBase* view = |
99 static_cast<RenderWidgetHostViewBase*>(host_->GetView()); | 112 static_cast<RenderWidgetHostViewBase*>(host_->GetView()); |
100 if (!view) | 113 if (!view) |
101 return; | 114 return; |
102 | 115 |
(...skipping 10 matching lines...) Expand all Loading... | |
113 inflight_requests_count_--; | 126 inflight_requests_count_--; |
114 base::Time timestamp = last_captured_frame_timestamp_; | 127 base::Time timestamp = last_captured_frame_timestamp_; |
115 last_captured_frame_timestamp_ = base::Time::Now(); | 128 last_captured_frame_timestamp_ = base::Time::Now(); |
116 if (timestamp.is_null() || response != READBACK_SUCCESS) | 129 if (timestamp.is_null() || response != READBACK_SUCCESS) |
117 return; | 130 return; |
118 | 131 |
119 captured_frames_count_++; | 132 captured_frames_count_++; |
120 base::PostTaskAndReplyWithResult( | 133 base::PostTaskAndReplyWithResult( |
121 base::WorkerPool::GetTaskRunner(true).get(), | 134 base::WorkerPool::GetTaskRunner(true).get(), |
122 FROM_HERE, | 135 FROM_HERE, |
123 base::Bind(&EncodeFrame, bitmap), | 136 base::Bind(&EncodeFrame, bitmap, timestamp.ToDoubleT()), |
124 base::Bind(&FrameRecorder::FrameEncoded, weak_factory_.GetWeakPtr(), | 137 frame_encoded_callback_.callback()); |
125 timestamp.ToDoubleT())); | |
126 } | 138 } |
127 | 139 |
128 void FrameRecorder::FrameEncoded( | 140 void FrameRecorder::FrameEncoded( |
129 double timestamp, const std::string& encoded_frame) { | 141 const scoped_ptr<EncodedFrame>& encoded_frame) { |
130 frames_.push_back(RecordedFrame::Create() | 142 frames_.push_back(RecordedFrame::Create() |
131 ->set_data(encoded_frame) | 143 ->set_data(encoded_frame->first) |
132 ->set_timestamp(timestamp)); | 144 ->set_timestamp(encoded_frame->second)); |
133 MaybeSendResponse(); | 145 MaybeSendResponse(); |
134 } | 146 } |
135 | 147 |
136 void FrameRecorder::MaybeSendResponse() { | 148 void FrameRecorder::MaybeSendResponse() { |
137 if (state_ != Encoding) | 149 if (state_ != Encoding) |
138 return; | 150 return; |
139 if (inflight_requests_count_ || frames_.size() != captured_frames_count_) | 151 if (inflight_requests_count_ || frames_.size() != captured_frames_count_) |
140 return; | 152 return; |
141 callback_.Run(StopRecordingFramesResponse::Create()->set_frames(frames_)); | 153 callback_.Run(StopRecordingFramesResponse::Create()->set_frames(frames_)); |
142 std::vector<scoped_refptr<devtools::page::RecordedFrame>> frames; | 154 std::vector<scoped_refptr<devtools::page::RecordedFrame>> frames; |
143 frames_.swap(frames); | 155 frames_.swap(frames); |
144 state_ = Ready; | 156 state_ = Ready; |
145 } | 157 } |
146 | 158 |
147 } // namespace page | 159 } // namespace page |
148 } // namespace devtools | 160 } // namespace devtools |
149 } // namespace content | 161 } // namespace content |
OLD | NEW |