Separate the PeerConnectionObserver class from the Peer class

This commit is contained in:
Axel Isouard 2016-09-26 23:01:36 +02:00
parent 7b70c378ac
commit 370cbb8699
3 changed files with 48 additions and 112 deletions

View file

@ -4,18 +4,20 @@
#include <iostream> #include <iostream>
#include <third_party/jsoncpp/source/include/json/writer.h> #include <third_party/jsoncpp/source/include/json/writer.h>
#include "Peer.h"
#include "Core.h"
#include "UnixConsole.h"
#include "DataChannelObserver.h"
#include "Core.h"
#include "DataChannelObserver.h"
#include "Peer.h"
#include "PeerConnectionObserver.h"
using webrtc::DataChannelInit;
using webrtc::PeerConnectionInterface; using webrtc::PeerConnectionInterface;
using webrtc::MediaConstraintsInterface; using webrtc::MediaConstraintsInterface;
using webrtc::DataChannelInit; using webrtc::SessionDescriptionInterface;
using webrtc::MediaStreamInterface; using webrtc::MediaStreamInterface;
using webrtc::DataChannelInterface; using webrtc::DataChannelInterface;
using webrtc::IceCandidateInterface; using webrtc::IceCandidateInterface;
using webrtc::SessionDescriptionInterface;
Peer::Peer() { Peer::Peer() {
PeerConnectionInterface::RTCConfiguration config; PeerConnectionInterface::RTCConfiguration config;
@ -32,9 +34,10 @@ Peer::Peer() {
_dataChannel = NULL; _dataChannel = NULL;
_dataChannelObserver = NULL; _dataChannelObserver = NULL;
_peerConnectionObserver = new PeerConnectionObserver(this);
_peerConnection = Core::GetPeerConnectionFactory()-> _peerConnection = Core::GetPeerConnectionFactory()->
CreatePeerConnection(config, &_mediaConstraints, CreatePeerConnection(config, &_mediaConstraints,
NULL, NULL, this); NULL, NULL, _peerConnectionObserver);
_mediaConstraints.AddOptional( _mediaConstraints.AddOptional(
MediaConstraintsInterface::kEnableDtlsSrtp, MediaConstraintsInterface::kEnableDtlsSrtp,
@ -52,6 +55,7 @@ Peer::Peer() {
Peer::~Peer() { Peer::~Peer() {
if (_dataChannel) { if (_dataChannel) {
_dataChannel->Close(); _dataChannel->Close();
_dataChannel->UnregisterObserver();
_dataChannel = NULL; _dataChannel = NULL;
} }
@ -100,87 +104,12 @@ bool Peer::IsConnected() {
return _dataChannel->state() == webrtc::DataChannelInterface::kOpen; return _dataChannel->state() == webrtc::DataChannelInterface::kOpen;
} }
void Peer::SetDataChannel(webrtc::DataChannelInterface *dataChannel) {
_dataChannel = dataChannel;
}
void Peer::SendMessage(const std::string& message) { void Peer::SendMessage(const std::string& message) {
webrtc::DataBuffer buffer(message); webrtc::DataBuffer buffer(message);
_dataChannel->Send(buffer); _dataChannel->Send(buffer);
} }
/*
* webrtc::PeerConnectionObserver methods
*/
void Peer::OnSignalingChange(
PeerConnectionInterface::SignalingState new_state) {
Console::Print("[Peer::OnSignalingChange] new signaling state: %d",
new_state);
}
void Peer::OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) {
Console::Print("[Peer::OnAddStream]");
}
void Peer::OnRemoveStream(rtc::scoped_refptr<MediaStreamInterface> stream) {
Console::Print("[Peer::OnRemoveStream]");
}
void Peer::OnDataChannel(
rtc::scoped_refptr<DataChannelInterface> data_channel) {
Console::Print("[Peer::OnDataChannel] %s", data_channel->label().c_str());
_dataChannel = data_channel;
_dataChannelObserver = new DataChannelObserver(_dataChannel);
_dataChannel->RegisterObserver(_dataChannelObserver);
}
void Peer::OnRenegotiationNeeded() {
}
void Peer::OnIceConnectionChange(
PeerConnectionInterface::IceConnectionState new_state) {
if (new_state == PeerConnectionInterface::kIceConnectionCompleted) {
Console::Print("Connected!");
} else if (new_state > PeerConnectionInterface::kIceConnectionCompleted) {
Console::Print("Disconnected.");
}
}
void Peer::OnIceGatheringChange(
PeerConnectionInterface::IceGatheringState new_state) {
if (new_state == PeerConnectionInterface::kIceGatheringGathering) {
Console::Print("Gathering ICE candidates, please wait.");
return;
}
if (new_state != PeerConnectionInterface::kIceGatheringComplete) {
return;
}
Json::FastWriter writer;
writer.write(_iceCandidates);
Console::Print("Done, paste this array of ICE candidates once requested." \
"\n\n%s", writer.write(_iceCandidates).c_str());
}
void Peer::OnIceCandidate(const IceCandidateInterface* candidate) {
Json::Value jmessage;
jmessage["sdpMid"] = candidate->sdp_mid();
jmessage["sdpMLineIndex"] = candidate->sdp_mline_index();
std::string sdp;
if (!candidate->ToString(&sdp)) {
Console::Print("[Peer::OnIceCandidate] Failed to serialize candidate");
return;
}
jmessage["candidate"] = sdp;
_iceCandidates.append(jmessage);
}
void Peer::OnIceCandidatesRemoved(
const std::vector<cricket::Candidate>& candidates) {
Console::Print("[Peer::OnIceCandidatesRemoved]");
}
void Peer::OnIceConnectionReceivingChange(bool receiving) {
}

View file

@ -27,38 +27,15 @@ public:
webrtc::SetSessionDescriptionObserver *setSDPObserver); webrtc::SetSessionDescriptionObserver *setSDPObserver);
bool IsConnected(); bool IsConnected();
void SetDataChannel(webrtc::DataChannelInterface *dataChannel);
void SendMessage(const std::string& message); void SendMessage(const std::string& message);
/*
* webrtc::PeerConnectionObserver methods
*/
void OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state);
void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);
void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);
void OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);
void OnRenegotiationNeeded();
void OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state);
void OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state);
void OnIceCandidate(const webrtc::IceCandidateInterface* candidate);
void OnIceCandidatesRemoved(
const std::vector<cricket::Candidate>& candidates);
void OnIceConnectionReceivingChange(bool receiving);
private: private:
rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection; rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection;
webrtc::PeerConnectionObserver *_peerConnectionObserver;
webrtc::FakeConstraints _mediaConstraints; webrtc::FakeConstraints _mediaConstraints;
rtc::scoped_refptr<webrtc::DataChannelInterface> _dataChannel; rtc::scoped_refptr<webrtc::DataChannelInterface> _dataChannel;
webrtc::DataChannelObserver *_dataChannelObserver; webrtc::DataChannelObserver *_dataChannelObserver;
Json::Value _iceCandidates;
}; };
#endif //LIBWEBRTC_PEER_H #endif //LIBWEBRTC_PEER_H

View file

@ -5,9 +5,39 @@
#ifndef LIBWEBRTC_PEERCONNECTIONOBSERVER_H #ifndef LIBWEBRTC_PEERCONNECTIONOBSERVER_H
#define LIBWEBRTC_PEERCONNECTIONOBSERVER_H #define LIBWEBRTC_PEERCONNECTIONOBSERVER_H
#include <third_party/jsoncpp/source/include/json/value.h>
#include <webrtc/api/peerconnectioninterface.h>
#include "IPeer.h"
class PeerConnectionObserver { class PeerConnectionObserver: public webrtc::PeerConnectionObserver {
public:
PeerConnectionObserver(IPeer *peer);
void OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state);
void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);
void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);
void OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);
void OnRenegotiationNeeded();
void OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state);
void OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state);
void OnIceCandidate(const webrtc::IceCandidateInterface* candidate);
void OnIceCandidatesRemoved(
const std::vector<cricket::Candidate>& candidates);
void OnIceConnectionReceivingChange(bool receiving);
private:
IPeer *_peer;
webrtc::DataChannelObserver *_dataChannelObserver;
Json::Value _iceCandidates;
}; };