Merge pull request #4451 from MaxWhere/framesubscriber-fix
Fixing FrameSubscriber memory issue
This commit is contained in:
commit
1232a285e6
3 changed files with 62 additions and 13 deletions
|
@ -1072,9 +1072,11 @@ void WebContents::BeginFrameSubscription(
|
||||||
const FrameSubscriber::FrameCaptureCallback& callback) {
|
const FrameSubscriber::FrameCaptureCallback& callback) {
|
||||||
const auto view = web_contents()->GetRenderWidgetHostView();
|
const auto view = web_contents()->GetRenderWidgetHostView();
|
||||||
if (view) {
|
if (view) {
|
||||||
scoped_ptr<FrameSubscriber> frame_subscriber(new FrameSubscriber(
|
FrameSubscriber* frame_subscriber = new FrameSubscriber(
|
||||||
isolate(), view->GetVisibleViewportSize(), callback));
|
isolate(), view->GetVisibleViewportSize(), callback);
|
||||||
view->BeginFrameSubscription(frame_subscriber.Pass());
|
scoped_ptr<FrameSubscriber::Subscriber> del_frame_subscriber(
|
||||||
|
frame_subscriber->GetSubscriber());
|
||||||
|
view->BeginFrameSubscription(del_frame_subscriber.Pass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,28 +13,58 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
|
using Subscriber = FrameSubscriber::Subscriber;
|
||||||
|
|
||||||
FrameSubscriber::FrameSubscriber(v8::Isolate* isolate,
|
FrameSubscriber::FrameSubscriber(v8::Isolate* isolate,
|
||||||
const gfx::Size& size,
|
const gfx::Size& size,
|
||||||
const FrameCaptureCallback& callback)
|
const FrameCaptureCallback& callback)
|
||||||
: isolate_(isolate), size_(size), callback_(callback) {
|
: isolate_(isolate), size_(size), callback_(callback), pending_frames(0) {
|
||||||
|
subscriber_ = new Subscriber(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FrameSubscriber::ShouldCaptureFrame(
|
Subscriber::Subscriber(
|
||||||
|
FrameSubscriber* frame_subscriber) : frame_subscriber_(frame_subscriber) {
|
||||||
|
}
|
||||||
|
|
||||||
|
Subscriber::~Subscriber() {
|
||||||
|
frame_subscriber_->subscriber_ = NULL;
|
||||||
|
frame_subscriber_->RequestDestruct();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Subscriber::ShouldCaptureFrame(
|
||||||
const gfx::Rect& damage_rect,
|
const gfx::Rect& damage_rect,
|
||||||
base::TimeTicks present_time,
|
base::TimeTicks present_time,
|
||||||
scoped_refptr<media::VideoFrame>* storage,
|
scoped_refptr<media::VideoFrame>* storage,
|
||||||
DeliverFrameCallback* callback) {
|
DeliverFrameCallback* callback) {
|
||||||
*storage = media::VideoFrame::CreateFrame(
|
*storage = media::VideoFrame::CreateFrame(
|
||||||
media::PIXEL_FORMAT_YV12,
|
media::PIXEL_FORMAT_YV12,
|
||||||
size_, gfx::Rect(size_), size_, base::TimeDelta());
|
frame_subscriber_->size_, gfx::Rect(frame_subscriber_->size_),
|
||||||
|
frame_subscriber_->size_, base::TimeDelta());
|
||||||
*callback = base::Bind(&FrameSubscriber::OnFrameDelivered,
|
*callback = base::Bind(&FrameSubscriber::OnFrameDelivered,
|
||||||
base::Unretained(this), *storage);
|
base::Unretained(frame_subscriber_), *storage);
|
||||||
|
frame_subscriber_->pending_frames++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Subscriber* FrameSubscriber::GetSubscriber() {
|
||||||
|
return subscriber_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FrameSubscriber::RequestDestruct() {
|
||||||
|
bool deletable = (subscriber_ == NULL && pending_frames == 0);
|
||||||
|
// Destruct FrameSubscriber if we're not waiting for frames and the
|
||||||
|
// subscription has ended
|
||||||
|
if (deletable)
|
||||||
|
delete this;
|
||||||
|
|
||||||
|
return deletable;
|
||||||
|
}
|
||||||
|
|
||||||
void FrameSubscriber::OnFrameDelivered(
|
void FrameSubscriber::OnFrameDelivered(
|
||||||
scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool result) {
|
scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool result) {
|
||||||
if (!result)
|
pending_frames--;
|
||||||
|
|
||||||
|
if (RequestDestruct() || subscriber_ == NULL || !result)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
v8::Locker locker(isolate_);
|
v8::Locker locker(isolate_);
|
||||||
|
|
|
@ -14,26 +14,43 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class FrameSubscriber : public content::RenderWidgetHostViewFrameSubscriber {
|
class FrameSubscriber {
|
||||||
public:
|
public:
|
||||||
using FrameCaptureCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
using FrameCaptureCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
||||||
|
|
||||||
|
// Inner class that is the actual subscriber sent to chromium
|
||||||
|
class Subscriber :
|
||||||
|
public content::RenderWidgetHostViewFrameSubscriber {
|
||||||
|
public:
|
||||||
|
explicit Subscriber(FrameSubscriber* frame_subscriber);
|
||||||
|
|
||||||
|
bool ShouldCaptureFrame(const gfx::Rect& damage_rect,
|
||||||
|
base::TimeTicks present_time,
|
||||||
|
scoped_refptr<media::VideoFrame>* storage,
|
||||||
|
DeliverFrameCallback* callback) override;
|
||||||
|
|
||||||
|
~Subscriber();
|
||||||
|
private:
|
||||||
|
FrameSubscriber* frame_subscriber_;
|
||||||
|
};
|
||||||
|
|
||||||
FrameSubscriber(v8::Isolate* isolate,
|
FrameSubscriber(v8::Isolate* isolate,
|
||||||
const gfx::Size& size,
|
const gfx::Size& size,
|
||||||
const FrameCaptureCallback& callback);
|
const FrameCaptureCallback& callback);
|
||||||
|
|
||||||
bool ShouldCaptureFrame(const gfx::Rect& damage_rect,
|
Subscriber* GetSubscriber();
|
||||||
base::TimeTicks present_time,
|
|
||||||
scoped_refptr<media::VideoFrame>* storage,
|
|
||||||
DeliverFrameCallback* callback) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void OnFrameDelivered(
|
void OnFrameDelivered(
|
||||||
scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool);
|
scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool);
|
||||||
|
|
||||||
|
bool RequestDestruct();
|
||||||
|
|
||||||
v8::Isolate* isolate_;
|
v8::Isolate* isolate_;
|
||||||
gfx::Size size_;
|
gfx::Size size_;
|
||||||
FrameCaptureCallback callback_;
|
FrameCaptureCallback callback_;
|
||||||
|
Subscriber* subscriber_;
|
||||||
|
int pending_frames;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(FrameSubscriber);
|
DISALLOW_COPY_AND_ASSIGN(FrameSubscriber);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue