Commit e25f4d74 authored by wjia@google.com's avatar wjia@google.com

create one video capture message filter per renderer process

BUG=none
TEST=try bots
Review URL: http://codereview.chromium.org/7058055

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88396 0039d316-1c4b-4281-b951-d872f2087c98
parent 70c8099b
...@@ -141,6 +141,8 @@ ...@@ -141,6 +141,8 @@
'renderer/v8_value_converter.h', 'renderer/v8_value_converter.h',
'renderer/video_capture_message_filter.cc', 'renderer/video_capture_message_filter.cc',
'renderer/video_capture_message_filter.h', 'renderer/video_capture_message_filter.h',
'renderer/video_capture_message_filter_creator.cc',
'renderer/video_capture_message_filter_creator.h',
'renderer/webplugin_delegate_proxy.cc', 'renderer/webplugin_delegate_proxy.cc',
'renderer/webplugin_delegate_proxy.h', 'renderer/webplugin_delegate_proxy.h',
'renderer/websharedworker_proxy.cc', 'renderer/websharedworker_proxy.cc',
......
...@@ -68,7 +68,7 @@ void VideoCaptureImpl::Init() { ...@@ -68,7 +68,7 @@ void VideoCaptureImpl::Init() {
void VideoCaptureImpl::DeInit(Task* task) { void VideoCaptureImpl::DeInit(Task* task) {
if (state_ == kStarted) if (state_ == kStarted)
message_filter_->Send(new VideoCaptureHostMsg_Stop(0, device_id_)); Send(new VideoCaptureHostMsg_Stop(0, device_id_));
base::MessageLoopProxy* io_message_loop_proxy = base::MessageLoopProxy* io_message_loop_proxy =
ChildProcess::current()->io_message_loop_proxy(); ChildProcess::current()->io_message_loop_proxy();
...@@ -229,8 +229,7 @@ void VideoCaptureImpl::OnBufferReceived(TransportDIB::Handle handle, ...@@ -229,8 +229,7 @@ void VideoCaptureImpl::OnBufferReceived(TransportDIB::Handle handle,
} }
if (state_ != kStarted) { if (state_ != kStarted) {
message_filter_->Send( Send(new VideoCaptureHostMsg_BufferReady(0, device_id_, handle));
new VideoCaptureHostMsg_BufferReady(0, device_id_, handle));
return; return;
} }
...@@ -259,8 +258,7 @@ void VideoCaptureImpl::OnBufferReceived(TransportDIB::Handle handle, ...@@ -259,8 +258,7 @@ void VideoCaptureImpl::OnBufferReceived(TransportDIB::Handle handle,
it->first->OnBufferReady(this, buffer); it->first->OnBufferReady(this, buffer);
} }
message_filter_->Send( Send(new VideoCaptureHostMsg_BufferReady(0, device_id_, handle));
new VideoCaptureHostMsg_BufferReady(0, device_id_, handle));
} }
void VideoCaptureImpl::OnStateChanged( void VideoCaptureImpl::OnStateChanged(
...@@ -342,7 +340,7 @@ void VideoCaptureImpl::StopDevice() { ...@@ -342,7 +340,7 @@ void VideoCaptureImpl::StopDevice() {
if (state_ == kStarted) { if (state_ == kStarted) {
state_ = kStopping; state_ = kStopping;
message_filter_->Send(new VideoCaptureHostMsg_Stop(0, device_id_)); Send(new VideoCaptureHostMsg_Stop(0, device_id_));
width_ = height_ = 0; width_ = height_ = 0;
} }
} }
...@@ -369,7 +367,7 @@ void VideoCaptureImpl::StartCaptureInternal() { ...@@ -369,7 +367,7 @@ void VideoCaptureImpl::StartCaptureInternal() {
params.height = height_; params.height = height_;
params.session_id = session_id_; params.session_id = session_id_;
message_filter_->Send(new VideoCaptureHostMsg_Start(0, device_id_, params)); Send(new VideoCaptureHostMsg_Start(0, device_id_, params));
state_ = kStarted; state_ = kStarted;
for (ClientInfo::iterator it = clients_.begin(); it != clients_.end(); it++) { for (ClientInfo::iterator it = clients_.begin(); it != clients_.end(); it++) {
it->first->OnStarted(this); it->first->OnStarted(this);
...@@ -384,3 +382,12 @@ void VideoCaptureImpl::RemoveDelegateOnIOThread(Task* task) { ...@@ -384,3 +382,12 @@ void VideoCaptureImpl::RemoveDelegateOnIOThread(Task* task) {
base::ScopedTaskRunner task_runner(task); base::ScopedTaskRunner task_runner(task);
message_filter_->RemoveDelegate(this); message_filter_->RemoveDelegate(this);
} }
void VideoCaptureImpl::Send(IPC::Message* message) {
base::MessageLoopProxy* io_message_loop_proxy =
ChildProcess::current()->io_message_loop_proxy();
io_message_loop_proxy->PostTask(FROM_HERE,
NewRunnableMethod(message_filter_.get(),
&VideoCaptureMessageFilter::Send, message));
}
...@@ -76,6 +76,7 @@ class VideoCaptureImpl ...@@ -76,6 +76,7 @@ class VideoCaptureImpl
void StartCaptureInternal(); void StartCaptureInternal();
void AddDelegateOnIOThread(); void AddDelegateOnIOThread();
void RemoveDelegateOnIOThread(Task* task); void RemoveDelegateOnIOThread(Task* task);
virtual void Send(IPC::Message* message);
scoped_refptr<VideoCaptureMessageFilter> message_filter_; scoped_refptr<VideoCaptureMessageFilter> message_filter_;
media::VideoCaptureSessionId session_id_; media::VideoCaptureSessionId session_id_;
......
...@@ -5,44 +5,9 @@ ...@@ -5,44 +5,9 @@
#include "content/renderer/media/video_capture_impl_manager.h" #include "content/renderer/media/video_capture_impl_manager.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "content/common/child_process.h" #include "content/renderer/video_capture_message_filter_creator.h"
#include "content/common/child_thread.h"
#include "content/common/video_capture_messages.h"
#include "content/common/view_messages.h"
#include "media/base/message_loop_factory_impl.h" #include "media/base/message_loop_factory_impl.h"
namespace {
// VideoCaptureMessageFilterCreator is to be used as a singleton so we can get
// access to a shared VideoCaptureMessageFilter.
// Example usage:
// VideoCaptureMessageFilter* filter =
// VideoCaptureMessageFilterCreator::SharedFilter();
class VideoCaptureMessageFilterCreator {
public:
VideoCaptureMessageFilterCreator() {
int routing_id;
ChildThread::current()->Send(
new ViewHostMsg_GenerateRoutingID(&routing_id));
filter_ = new VideoCaptureMessageFilter(routing_id);
filter_->AddFilter();
}
static VideoCaptureMessageFilter* SharedFilter() {
return GetInstance()->filter_.get();
}
static VideoCaptureMessageFilterCreator* GetInstance() {
return Singleton<VideoCaptureMessageFilterCreator>::get();
}
private:
scoped_refptr<VideoCaptureMessageFilter> filter_;
};
} // namespace
VideoCaptureImplManager::VideoCaptureImplManager() { VideoCaptureImplManager::VideoCaptureImplManager() {
ml_factory_.reset(new media::MessageLoopFactoryImpl()); ml_factory_.reset(new media::MessageLoopFactoryImpl());
ml_proxy_ = ml_factory_->GetMessageLoopProxy("VC manager"); ml_proxy_ = ml_factory_->GetMessageLoopProxy("VC manager");
......
...@@ -13,9 +13,8 @@ using ::testing::Return; ...@@ -13,9 +13,8 @@ using ::testing::Return;
#define DEFAULT_CAPABILITY {176, 144, 30, 0, media::VideoFrame::I420, \ #define DEFAULT_CAPABILITY {176, 144, 30, 0, media::VideoFrame::I420, \
false, false } false, false }
ACTION_P(DeleteMessage, return_value) { ACTION(DeleteMessage) {
delete arg0; delete arg0;
return return_value;
} }
class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
...@@ -25,7 +24,6 @@ class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { ...@@ -25,7 +24,6 @@ class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
// Filter implementation. // Filter implementation.
MOCK_METHOD1(Send, bool(IPC::Message* message)); MOCK_METHOD1(Send, bool(IPC::Message* message));
MOCK_METHOD0(ReadyToSend, bool());
private: private:
DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
...@@ -54,6 +52,18 @@ class MockVideoCaptureClient : public media::VideoCapture::EventHandler { ...@@ -54,6 +52,18 @@ class MockVideoCaptureClient : public media::VideoCapture::EventHandler {
class VideoCaptureImplTest : public ::testing::Test { class VideoCaptureImplTest : public ::testing::Test {
public: public:
class MockVideoCaptureImpl : public VideoCaptureImpl {
public:
MockVideoCaptureImpl(const media::VideoCaptureSessionId id,
scoped_refptr<base::MessageLoopProxy> ml_proxy,
VideoCaptureMessageFilter* filter)
: VideoCaptureImpl(id, ml_proxy, filter) {
}
virtual ~MockVideoCaptureImpl() {}
MOCK_METHOD1(Send, void(IPC::Message* message));
};
VideoCaptureImplTest() { VideoCaptureImplTest() {
message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
message_loop_proxy_ = message_loop_proxy_ =
...@@ -62,8 +72,9 @@ class VideoCaptureImplTest : public ::testing::Test { ...@@ -62,8 +72,9 @@ class VideoCaptureImplTest : public ::testing::Test {
message_filter_ = new MockVideoCaptureMessageFilter; message_filter_ = new MockVideoCaptureMessageFilter;
session_id_ = 1; session_id_ = 1;
video_capture_impl_ = new VideoCaptureImpl(session_id_, message_loop_proxy_, video_capture_impl_ = new MockVideoCaptureImpl(session_id_,
message_filter_); message_loop_proxy_,
message_filter_);
video_capture_impl_->device_id_ = 2; video_capture_impl_->device_id_ = 2;
} }
...@@ -77,7 +88,7 @@ class VideoCaptureImplTest : public ::testing::Test { ...@@ -77,7 +88,7 @@ class VideoCaptureImplTest : public ::testing::Test {
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
media::VideoCaptureSessionId session_id_; media::VideoCaptureSessionId session_id_;
VideoCaptureImpl* video_capture_impl_; MockVideoCaptureImpl* video_capture_impl_;
private: private:
DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
...@@ -89,11 +100,8 @@ TEST_F(VideoCaptureImplTest, Simple) { ...@@ -89,11 +100,8 @@ TEST_F(VideoCaptureImplTest, Simple) {
scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient);
media::VideoCapture::VideoCaptureCapability capability = DEFAULT_CAPABILITY; media::VideoCapture::VideoCaptureCapability capability = DEFAULT_CAPABILITY;
EXPECT_CALL(*message_filter_, Send(_)) EXPECT_CALL(*video_capture_impl_, Send(_))
.WillRepeatedly(DeleteMessage(true)); .WillRepeatedly(DeleteMessage());
EXPECT_CALL(*message_filter_, ReadyToSend())
.WillRepeatedly(Return(true));
EXPECT_CALL(*client, OnStarted(_)) EXPECT_CALL(*client, OnStarted(_))
.WillOnce(Return()); .WillOnce(Return());
......
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
#include "content/renderer/render_view_visitor.h" #include "content/renderer/render_view_visitor.h"
#include "content/renderer/renderer_webidbfactory_impl.h" #include "content/renderer/renderer_webidbfactory_impl.h"
#include "content/renderer/renderer_webkitclient_impl.h" #include "content/renderer/renderer_webkitclient_impl.h"
#include "content/renderer/video_capture_message_filter.h"
#include "content/renderer/video_capture_message_filter_creator.h"
#include "ipc/ipc_channel_handle.h" #include "ipc/ipc_channel_handle.h"
#include "ipc/ipc_platform_file.h" #include "ipc/ipc_platform_file.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
...@@ -167,6 +169,10 @@ void RenderThread::Init() { ...@@ -167,6 +169,10 @@ void RenderThread::Init() {
db_message_filter_ = new DBMessageFilter(); db_message_filter_ = new DBMessageFilter();
AddFilter(db_message_filter_.get()); AddFilter(db_message_filter_.get());
VideoCaptureMessageFilter* video_capture_message_filter =
VideoCaptureMessageFilterCreator::SharedFilter();
AddFilter(video_capture_message_filter);
content::GetContentClient()->renderer()->RenderThreadStarted(); content::GetContentClient()->renderer()->RenderThreadStarted();
TRACE_EVENT_END_ETW("RenderThread::Init", 0, ""); TRACE_EVENT_END_ETW("RenderThread::Init", 0, "");
...@@ -184,6 +190,10 @@ RenderThread::~RenderThread() { ...@@ -184,6 +190,10 @@ RenderThread::~RenderThread() {
RemoveFilter(db_message_filter_.get()); RemoveFilter(db_message_filter_.get());
db_message_filter_ = NULL; db_message_filter_ = NULL;
VideoCaptureMessageFilter* video_capture_message_filter =
VideoCaptureMessageFilterCreator::SharedFilter();
RemoveFilter(video_capture_message_filter);
// Shutdown the file thread if it's running. // Shutdown the file thread if it's running.
if (file_thread_.get()) if (file_thread_.get())
file_thread_->Stop(); file_thread_->Stop();
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
#include "content/renderer/video_capture_message_filter.h" #include "content/renderer/video_capture_message_filter.h"
#include "content/common/child_process.h"
#include "content/common/video_capture_messages.h" #include "content/common/video_capture_messages.h"
#include "content/common/view_messages.h"
#include "content/renderer/render_thread.h" #include "content/renderer/render_thread.h"
VideoCaptureMessageFilter::VideoCaptureMessageFilter(int32 route_id) VideoCaptureMessageFilter::VideoCaptureMessageFilter(int32 route_id)
...@@ -23,14 +23,6 @@ bool VideoCaptureMessageFilter::Send(IPC::Message* message) { ...@@ -23,14 +23,6 @@ bool VideoCaptureMessageFilter::Send(IPC::Message* message) {
return false; return false;
} }
if (!message_loop_proxy_->BelongsToCurrentThread()) {
// Can only access the IPC::Channel on the IPC thread since it's not thread
// safe.
message_loop_proxy_->PostTask(FROM_HERE,
NewRunnableMethod(this, &VideoCaptureMessageFilter::Send, message));
return true;
}
message->set_routing_id(route_id_); message->set_routing_id(route_id_);
return channel_->Send(message); return channel_->Send(message);
} }
...@@ -152,13 +144,3 @@ void VideoCaptureMessageFilter::RemoveDelegate(Delegate* delegate) { ...@@ -152,13 +144,3 @@ void VideoCaptureMessageFilter::RemoveDelegate(Delegate* delegate) {
} }
} }
void VideoCaptureMessageFilter::AddFilter() {
if (MessageLoop::current() != ChildThread::current()->message_loop()) {
ChildThread::current()->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this,
&VideoCaptureMessageFilter::AddFilter));
return;
}
RenderThread::current()->AddFilter(this);
}
...@@ -54,8 +54,6 @@ class VideoCaptureMessageFilter : public IPC::ChannelProxy::MessageFilter { ...@@ -54,8 +54,6 @@ class VideoCaptureMessageFilter : public IPC::ChannelProxy::MessageFilter {
// Send a message asynchronously. // Send a message asynchronously.
virtual bool Send(IPC::Message* message); virtual bool Send(IPC::Message* message);
void AddFilter();
private: private:
FRIEND_TEST_ALL_PREFIXES(VideoCaptureMessageFilterTest, Basic); FRIEND_TEST_ALL_PREFIXES(VideoCaptureMessageFilterTest, Basic);
FRIEND_TEST_ALL_PREFIXES(VideoCaptureMessageFilterTest, Delegates); FRIEND_TEST_ALL_PREFIXES(VideoCaptureMessageFilterTest, Delegates);
......
// Copyright (c) 2011 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/renderer/video_capture_message_filter_creator.h"
#include "content/renderer/video_capture_message_filter.h"
VideoCaptureMessageFilterCreator::VideoCaptureMessageFilterCreator() {
filter_ = new VideoCaptureMessageFilter(1);
}
VideoCaptureMessageFilterCreator::~VideoCaptureMessageFilterCreator() {
}
// static
VideoCaptureMessageFilter* VideoCaptureMessageFilterCreator::SharedFilter() {
return GetInstance()->filter_.get();
}
// static
VideoCaptureMessageFilterCreator*
VideoCaptureMessageFilterCreator::GetInstance() {
return Singleton<VideoCaptureMessageFilterCreator>::get();
}
// Copyright (c) 2011 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.
#ifndef CONTENT_RENDERER_VIDEO_CAPTURE_MESSAGE_FILTER_CREATOR_H_
#define CONTENT_RENDERER_VIDEO_CAPTURE_MESSAGE_FILTER_CREATOR_H_
#include "base/memory/ref_counted.h"
#include "base/memory/singleton.h"
class VideoCaptureMessageFilter;
// VideoCaptureMessageFilterCreator is to be used as a singleton so we can get
// access to a shared VideoCaptureMessageFilter.
// Example usage:
// VideoCaptureMessageFilter* filter =
// VideoCaptureMessageFilterCreator::SharedFilter();
class VideoCaptureMessageFilterCreator {
public:
static VideoCaptureMessageFilter* SharedFilter();
static VideoCaptureMessageFilterCreator* GetInstance();
private:
VideoCaptureMessageFilterCreator();
~VideoCaptureMessageFilterCreator();
friend struct DefaultSingletonTraits<VideoCaptureMessageFilterCreator>;
scoped_refptr<VideoCaptureMessageFilter> filter_;
DISALLOW_COPY_AND_ASSIGN(VideoCaptureMessageFilterCreator);
};
#endif // CONTENT_RENDERER_VIDEO_CAPTURE_MESSAGE_FILTER_CREATOR_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment