diff --git a/Samples/PeerConnection/Peer.cpp b/Samples/PeerConnection/Peer.cpp index 7845ce1..384c580 100644 --- a/Samples/PeerConnection/Peer.cpp +++ b/Samples/PeerConnection/Peer.cpp @@ -4,18 +4,20 @@ #include #include -#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::MediaConstraintsInterface; -using webrtc::DataChannelInit; +using webrtc::SessionDescriptionInterface; + using webrtc::MediaStreamInterface; using webrtc::DataChannelInterface; using webrtc::IceCandidateInterface; -using webrtc::SessionDescriptionInterface; Peer::Peer() { PeerConnectionInterface::RTCConfiguration config; @@ -32,9 +34,10 @@ Peer::Peer() { _dataChannel = NULL; _dataChannelObserver = NULL; + _peerConnectionObserver = new PeerConnectionObserver(this); _peerConnection = Core::GetPeerConnectionFactory()-> CreatePeerConnection(config, &_mediaConstraints, - NULL, NULL, this); + NULL, NULL, _peerConnectionObserver); _mediaConstraints.AddOptional( MediaConstraintsInterface::kEnableDtlsSrtp, @@ -52,6 +55,7 @@ Peer::Peer() { Peer::~Peer() { if (_dataChannel) { _dataChannel->Close(); + _dataChannel->UnregisterObserver(); _dataChannel = NULL; } @@ -100,87 +104,12 @@ bool Peer::IsConnected() { return _dataChannel->state() == webrtc::DataChannelInterface::kOpen; } +void Peer::SetDataChannel(webrtc::DataChannelInterface *dataChannel) { + _dataChannel = dataChannel; +} + void Peer::SendMessage(const std::string& message) { webrtc::DataBuffer buffer(message); _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 stream) { - Console::Print("[Peer::OnAddStream]"); -} - -void Peer::OnRemoveStream(rtc::scoped_refptr stream) { - Console::Print("[Peer::OnRemoveStream]"); -} - -void Peer::OnDataChannel( - rtc::scoped_refptr 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& candidates) { - Console::Print("[Peer::OnIceCandidatesRemoved]"); - -} - -void Peer::OnIceConnectionReceivingChange(bool receiving) { } \ No newline at end of file diff --git a/Samples/PeerConnection/Peer.h b/Samples/PeerConnection/Peer.h index 1225069..f96039b 100644 --- a/Samples/PeerConnection/Peer.h +++ b/Samples/PeerConnection/Peer.h @@ -27,38 +27,15 @@ public: webrtc::SetSessionDescriptionObserver *setSDPObserver); bool IsConnected(); + void SetDataChannel(webrtc::DataChannelInterface *dataChannel); void SendMessage(const std::string& message); - /* - * webrtc::PeerConnectionObserver methods - */ - void OnSignalingChange( - webrtc::PeerConnectionInterface::SignalingState new_state); - - void OnAddStream(rtc::scoped_refptr stream); - void OnRemoveStream(rtc::scoped_refptr stream); - - void OnDataChannel( - rtc::scoped_refptr 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& candidates); - void OnIceConnectionReceivingChange(bool receiving); - private: rtc::scoped_refptr _peerConnection; + webrtc::PeerConnectionObserver *_peerConnectionObserver; webrtc::FakeConstraints _mediaConstraints; rtc::scoped_refptr _dataChannel; webrtc::DataChannelObserver *_dataChannelObserver; - Json::Value _iceCandidates; }; #endif //LIBWEBRTC_PEER_H diff --git a/Samples/PeerConnection/PeerConnectionObserver.h b/Samples/PeerConnection/PeerConnectionObserver.h index 8f9d6b4..0cc14df 100644 --- a/Samples/PeerConnection/PeerConnectionObserver.h +++ b/Samples/PeerConnection/PeerConnectionObserver.h @@ -5,9 +5,39 @@ #ifndef LIBWEBRTC_PEERCONNECTIONOBSERVER_H #define LIBWEBRTC_PEERCONNECTIONOBSERVER_H +#include +#include +#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 stream); + void OnRemoveStream(rtc::scoped_refptr stream); + + void OnDataChannel( + rtc::scoped_refptr 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& candidates); + void OnIceConnectionReceivingChange(bool receiving); + +private: + IPeer *_peer; + webrtc::DataChannelObserver *_dataChannelObserver; + Json::Value _iceCandidates; };