Moved creation of objects to the signaling thread.

Fixed defect of not initializing remote_media_streams in peerconnection_impl.cc
Fixed defect in glare case of peerconnectionsignaling.cc

BUG=
TEST=

Review URL: http://webrtc-codereview.appspot.com/196001

git-svn-id: http://webrtc.googlecode.com/svn/trunk@690 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
perkj@webrtc.org 2011-10-04 17:54:34 +00:00
parent bafca109db
commit cb4ab65dfc
4 changed files with 91 additions and 26 deletions

View File

@ -128,6 +128,7 @@ PeerConnectionImpl::PeerConnectionImpl(
PcPacketSocketFactory* socket_factory)
: observer_(NULL),
local_media_streams_(StreamCollectionImpl::Create()),
remote_media_streams_(StreamCollectionImpl::Create()),
signaling_thread_(signaling_thread),
channel_manager_(channel_manager),
network_manager_(network_manager),

View File

@ -30,8 +30,6 @@
#include "talk/app/webrtc_dev/peerconnectionimpl.h"
#include "talk/app/webrtc_dev/webrtc_devicemanager.h"
#include "talk/base/basicpacketsocketfactory.h"
#include "talk/base/thread.h"
#include "talk/session/phone/channelmanager.h"
#include "talk/session/phone/webrtcmediaengine.h"
#ifdef WEBRTC_RELATIVE_PATH
@ -40,6 +38,27 @@
#include "third_party/webrtc/files/include/audio_device.h"
#endif
namespace {
typedef talk_base::TypedMessageData<bool> InitMessageData;
struct CreatePeerConnectionParams : public talk_base::MessageData {
CreatePeerConnectionParams(const std::string& configuration,
webrtc::PeerConnectionObserver* observer)
: configuration(configuration), observer(observer) {
}
scoped_refptr<webrtc::PeerConnection> peerconnection;
const std::string& configuration;
webrtc::PeerConnectionObserver* observer;
};
enum {
MSG_INIT_MANAGER = 1,
MSG_CREATE_PEERCONNECTION = 2,
};
} // namespace anonymous
namespace webrtc {
@ -85,6 +104,7 @@ talk_base::PacketSocketFactory* PcPacketSocketFactory::socket_factory() const {
scoped_refptr<PeerConnectionManager> PeerConnectionManager::Create() {
RefCountImpl<PeerConnectionManagerImpl>* pc_manager =
new RefCountImpl<PeerConnectionManagerImpl>();
if (!pc_manager->Initialize()) {
delete pc_manager;
pc_manager = NULL;
@ -113,13 +133,13 @@ scoped_refptr<PeerConnectionManager> PeerConnectionManager::Create(
PeerConnectionManagerImpl::PeerConnectionManagerImpl()
: worker_thread_(new talk_base::Thread),
signaling_thread_(new talk_base::Thread),
network_manager_(PcNetworkManager::Create(
new talk_base::BasicNetworkManager())),
socket_factory_(PcPacketSocketFactory::Create(
new talk_base::BasicPacketSocketFactory)) {
signaling_thread_(new talk_base::Thread) {
worker_thread_ptr_ = worker_thread_.get();
signaling_thread_ptr_ = signaling_thread_.get();
bool result = worker_thread_->Start();
ASSERT(result);
result = signaling_thread_->Start();
ASSERT(result);
}
PeerConnectionManagerImpl::PeerConnectionManagerImpl(
@ -144,19 +164,43 @@ PeerConnectionManagerImpl::~PeerConnectionManagerImpl() {
}
bool PeerConnectionManagerImpl::Initialize() {
if (worker_thread_.get() && !worker_thread_->Start())
return false;
if (signaling_thread_.get() && !signaling_thread_->Start())
return false;
cricket::DeviceManager* device_manager(new WebRtcDeviceManager());
cricket::WebRtcMediaEngine* webrtc_media_engine = NULL;
InitMessageData result(false);
signaling_thread_->Send(this, MSG_INIT_MANAGER, &result);
return result.data();
}
void PeerConnectionManagerImpl::OnMessage(talk_base::Message* msg) {
switch (msg->message_id) {
case MSG_INIT_MANAGER: {
InitMessageData* pdata = static_cast<InitMessageData*> (msg->pdata);
pdata->data() = Initialize_s();
break;
}
case MSG_CREATE_PEERCONNECTION: {
CreatePeerConnectionParams* pdata =
static_cast<CreatePeerConnectionParams*> (msg->pdata);
pdata->peerconnection = CreatePeerConnection_s(pdata->configuration,
pdata->observer);
break;
}
}
}
bool PeerConnectionManagerImpl::Initialize_s() {
if (!network_manager_.get())
network_manager_ = PcNetworkManager::Create(
new talk_base::BasicNetworkManager());
if (!socket_factory_.get())
socket_factory_ = PcPacketSocketFactory::Create(
new talk_base::BasicPacketSocketFactory(worker_thread_ptr_));
cricket::DeviceManager* device_manager(new WebRtcDeviceManager());
// TODO(perkj): Need to make sure only one VoE is created inside
// WebRtcMediaEngine.
webrtc_media_engine = new cricket::WebRtcMediaEngine(
default_adm_.get(),
NULL, // No secondary adm.
NULL); // No vcm available.
cricket::WebRtcMediaEngine* webrtc_media_engine(
new cricket::WebRtcMediaEngine(default_adm_.get(),
NULL, // No secondary adm.
NULL)); // No vcm available.
channel_manager_.reset(new cricket::ChannelManager(
webrtc_media_engine, device_manager, worker_thread_ptr_));
@ -169,12 +213,20 @@ bool PeerConnectionManagerImpl::Initialize() {
scoped_refptr<PeerConnection> PeerConnectionManagerImpl::CreatePeerConnection(
const std::string& configuration,
PeerConnectionObserver* observer) {
RefCountImpl<PeerConnectionImpl>* pc =
CreatePeerConnectionParams params(configuration, observer);
signaling_thread_->Send(this, MSG_CREATE_PEERCONNECTION, &params);
return params.peerconnection;
}
scoped_refptr<PeerConnection> PeerConnectionManagerImpl::CreatePeerConnection_s(
const std::string& configuration,
PeerConnectionObserver* observer) {
RefCountImpl<PeerConnectionImpl>* pc(
new RefCountImpl<PeerConnectionImpl>(channel_manager_.get(),
signaling_thread_ptr_,
worker_thread_ptr_,
network_manager_,
socket_factory_);
socket_factory_));
if (!pc->Initialize(configuration, observer)) {
delete pc;
pc = NULL;

View File

@ -32,11 +32,13 @@
#include "talk/base/scoped_ptr.h"
#include "talk/app/webrtc_dev/peerconnection.h"
#include "talk/app/webrtc_dev/mediastream.h"
#include "talk/base/thread.h"
#include "talk/session/phone/channelmanager.h"
namespace webrtc {
class PeerConnectionManagerImpl : public PeerConnectionManager {
class PeerConnectionManagerImpl : public PeerConnectionManager,
public talk_base::MessageHandler {
public:
scoped_refptr<PeerConnection> CreatePeerConnection(
const std::string& config,
@ -52,7 +54,15 @@ class PeerConnectionManagerImpl : public PeerConnectionManager {
AudioDeviceModule* default_adm);
virtual ~PeerConnectionManagerImpl();
private:
bool Initialize_s();
scoped_refptr<PeerConnection> CreatePeerConnection_s(
const std::string& configuration,
PeerConnectionObserver* observer);
// Implements talk_base::MessageHandler.
void OnMessage(talk_base::Message* msg);
talk_base::scoped_ptr<talk_base::Thread> worker_thread_;
talk_base::Thread* worker_thread_ptr_;
talk_base::scoped_ptr<talk_base::Thread> signaling_thread_;

View File

@ -144,6 +144,8 @@ void PeerConnectionSignaling::ProcessSignalingMessage(
if (state_ == kGlare) {
state_ = kIdle;
}
// Clear the MSG_SEND_QUEUED_OFFER we posted delayed.
signaling_thread_->Clear(this, MSG_SEND_QUEUED_OFFER);
signaling_thread_->Post(this, MSG_GENERATE_ANSWER);
break;
}
@ -169,13 +171,13 @@ void PeerConnectionSignaling::ProcessSignalingMessage(
break;
}
case PeerConnectionMessage::kError: {
if (signaling_message->error() != PeerConnectionMessage::kWrongState)
if (signaling_message->error() != PeerConnectionMessage::kWrongState) {
SignalErrorMessageReceived(signaling_message->error());
// An error have occurred that we can't do anything about.
// Reset the state and wait for user action.
queued_offers_.clear();
state_ = kIdle;
// An error have occurred that we can't do anything about.
// Reset the state and wait for user action.
queued_offers_.clear();
state_ = kIdle;
}
break;
}
}