-
-
Save hanumesh/7412755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include "talk/app/webrtc/peerconnectioninterface.h" | |
#include "talk/app/webrtc/jsep.h" | |
#include "talk/app/webrtc/datachannelinterface.h" | |
#include "talk/app/webrtc/test/fakeconstraints.h" | |
#include "talk/base/json.h" | |
#include "talk/base/logging.h" | |
class WebRtcConnectionManager | |
: public webrtc::PeerConnectionObserver, | |
public webrtc::CreateSessionDescriptionObserver { | |
public: | |
WebRtcConnectionManager(); | |
bool InitConnection(); | |
void CreateOffer(); | |
void OnOfferRequest(webrtc::SessionDescriptionInterface* desc); | |
void OnOfferReply(webrtc::SessionDescriptionInterface* desc); | |
static webrtc::SessionDescriptionInterface* StringToDescription( | |
const std::string string_desc); | |
static std::string DescriptionToString( | |
const webrtc::SessionDescriptionInterface* desc); | |
virtual const webrtc::SessionDescriptionInterface* | |
local_description() const { | |
return peer_connection_->local_description(); | |
} | |
protected: | |
//~WebRtcConnectionManager(); | |
// inherited from PeerConnectionObserver | |
virtual void OnError(); | |
virtual void OnStateChange( | |
webrtc::PeerConnectionObserver::StateType state_changed); | |
virtual void OnAddStream(webrtc::MediaStreamInterface* stream); | |
virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream); | |
virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate); | |
// inherited from CreateSessionDescriptionObserver | |
virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc); | |
virtual void OnFailure(const std::string &error); | |
virtual int AddRef() { return 1; } | |
virtual int Release() { return 0; } | |
private: | |
talk_base::scoped_refptr<webrtc::PeerConnectionFactoryInterface> | |
peer_connection_factory_; | |
webrtc::PeerConnectionInterface::IceServers servers_; | |
webrtc::PeerConnectionInterface::IceServer server_; | |
webrtc::FakeConstraints constraints_; | |
talk_base::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; | |
talk_base::scoped_refptr<webrtc::AudioTrackInterface> audio_track_; | |
talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream_; | |
}; | |
const char kStunServerUri[] = "stun:stun.l.google.com:19302"; | |
const char kAudioLabel[] = "audio_label"; | |
const char kStreamLabel[] = "stream_label"; | |
class DummySetSessionDescriptionObserver | |
: public webrtc::SetSessionDescriptionObserver { | |
public: | |
static DummySetSessionDescriptionObserver* Create() { | |
return | |
new talk_base::RefCountedObject<DummySetSessionDescriptionObserver>(); | |
} | |
virtual void OnSuccess() { | |
LOG(INFO) << __FUNCTION__; | |
} | |
virtual void OnFailure(const std::string& error) { | |
LOG(INFO) << __FUNCTION__ << " " << error; | |
} | |
protected: | |
DummySetSessionDescriptionObserver() {} | |
~DummySetSessionDescriptionObserver() {} | |
}; | |
WebRtcConnectionManager::WebRtcConnectionManager() { | |
peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(); | |
server_.uri = kStunServerUri; | |
servers_.push_back(server_); | |
constraints_.SetMandatoryReceiveAudio(false); | |
constraints_.SetMandatoryReceiveVideo(false); | |
constraints_.SetAllowRtpDataChannels(); | |
} | |
bool WebRtcConnectionManager::InitConnection() { | |
peer_connection_ = peer_connection_factory_->CreatePeerConnection(servers_, | |
&constraints_, this); | |
audio_track_ = peer_connection_factory_->CreateAudioTrack(kAudioLabel, | |
NULL); | |
stream_ = peer_connection_factory_->CreateLocalMediaStream(kStreamLabel); | |
stream_->AddTrack(audio_track_); | |
peer_connection_->AddStream(stream_, &constraints_); | |
return true; | |
} | |
void WebRtcConnectionManager::CreateOffer() { | |
peer_connection_->CreateOffer(this, &constraints_); | |
} | |
void WebRtcConnectionManager::OnOfferRequest( | |
webrtc::SessionDescriptionInterface* desc) { | |
peer_connection_->SetRemoteDescription( | |
DummySetSessionDescriptionObserver::Create(), desc); | |
peer_connection_->CreateAnswer(this, &constraints_); | |
} | |
void WebRtcConnectionManager::OnOfferReply( | |
webrtc::SessionDescriptionInterface* desc) { | |
peer_connection_->SetRemoteDescription( | |
DummySetSessionDescriptionObserver::Create(), desc); | |
} | |
void WebRtcConnectionManager::OnError() { | |
std::cout << "PEERCONNECTION ERROR" << std::endl; | |
} | |
void WebRtcConnectionManager::OnStateChange( | |
webrtc::PeerConnectionObserver::StateType state_changed) { | |
} | |
void WebRtcConnectionManager::OnAddStream( | |
webrtc::MediaStreamInterface* stream) { | |
} | |
void WebRtcConnectionManager::OnRemoveStream( | |
webrtc::MediaStreamInterface* stream) { | |
} | |
void WebRtcConnectionManager::OnIceCandidate( | |
const webrtc::IceCandidateInterface* candidate) { | |
std::cout << "ICE ICE baby" << std::endl; | |
} | |
void WebRtcConnectionManager::OnSuccess( | |
webrtc::SessionDescriptionInterface* desc) { | |
std::cout << "SETTING LOCAL" << std::endl; | |
peer_connection_->SetLocalDescription( | |
DummySetSessionDescriptionObserver::Create(), desc); | |
} | |
void WebRtcConnectionManager::OnFailure(const std::string &error) { | |
std::cout << error << std::endl; | |
} | |
webrtc::SessionDescriptionInterface* | |
WebRtcConnectionManager::StringToDescription(const std::string string_desc) { | |
Json::Reader reader; | |
Json::Value jdesc; | |
if (!reader.parse(string_desc, jdesc)) { | |
LOG(WARNING) << "Unknown string desc " << string_desc; | |
return NULL; | |
} | |
// replace with global constants | |
std::string type = jdesc["type"].asString(); | |
std::string sdp = jdesc["sdp"].asString(); | |
return webrtc::CreateSessionDescription(type, sdp); | |
} | |
std::string WebRtcConnectionManager::DescriptionToString( | |
const webrtc::SessionDescriptionInterface* desc) { | |
Json::FastWriter writer; | |
Json::Value jdesc; | |
jdesc["type"] = desc->type(); | |
std::string sdp; | |
desc->ToString(&sdp); | |
jdesc["sdp"] = sdp; | |
return writer.write(jdesc); | |
} | |
int main(int argc, char **argv) { | |
WebRtcConnectionManager manager; | |
while (1) { | |
std::string input; | |
std::cout << "Enter command:" << std::endl; | |
std::cin >> input; | |
if (input.compare("init") == 0) { | |
manager.InitConnection(); | |
} | |
else if (input.compare("offer") == 0) { | |
manager.CreateOffer(); | |
} | |
else if (input.compare("print") == 0) { | |
std::string string_desc = manager.DescriptionToString( | |
manager.local_description()); | |
std::cout << string_desc << std::endl; | |
} | |
else if (input.compare("request") == 0) { | |
std::string string_desc; | |
std::cout << "Enter request:" << std::endl; | |
std::getline(std::cin, string_desc); | |
std::getline(std::cin, string_desc); | |
webrtc::SessionDescriptionInterface* desc = | |
manager.StringToDescription(string_desc); | |
manager.OnOfferRequest(desc); | |
} | |
else if (input.compare("reply") == 0) { | |
std::string string_desc; | |
std::cout << "Enter reply:" << std::endl; | |
std::getline(std::cin, string_desc); | |
std::getline(std::cin, string_desc); | |
webrtc::SessionDescriptionInterface* desc = | |
manager.StringToDescription(string_desc); | |
manager.OnOfferReply(desc); | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment