/* * Copyright (c) 2012 Yoann Blein * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #include #include #include #include #include #include #include namespace Swift { ScreenSharing::ScreenSharing(boost::shared_ptr session, UDPSocketFactory* udpSocketFactory) : rtpSession(0), jingleSession(session), udpSocketFactory(udpSocketFactory) { session->onSessionTerminateReceived.connect(boost::bind(&ScreenSharing::handleSessionTerminateReceived, this, _1)); } ScreenSharing::~ScreenSharing() { } void ScreenSharing::stop() { jingleSession->sendTerminate(JinglePayload::Reason::Success); if (rtpSession) rtpSession->stop(); onStateChange(ScreenSharing::Finished); onFinished(); } bool ScreenSharing::addBestCandidate(boost::shared_ptr transport) { // TODO: NAT traversal JingleRawUDPTransportPayload::Candidate candidate; candidate.cid = idGenerator.generateID(); candidate.component = 1; candidate.generation = 0; PlatformNetworkEnvironment env; std::vector interfaces = env.getNetworkInterfaces(); // Find the first ip which is not loopback foreach (const NetworkInterface& interface, interfaces) { if (!interface.isLoopback()) { // exclude loopback serverSocket = udpSocketFactory->createUDPSocket(); serverSocket->bind(0); candidate.hostAddressPort = HostAddressPort(interface.getAddresses().front(), serverSocket->getLocalAddress().getPort()); candidate.type = JingleRawUDPTransportPayload::Candidate::Host; transport->addCandidate(candidate); return true; } } // else loopback for self sharing if (!interfaces.empty()) { serverSocket = udpSocketFactory->createUDPSocket(); serverSocket->bind(0); candidate.hostAddressPort = HostAddressPort(interfaces.front().getAddresses().front(), serverSocket->getLocalAddress().getPort()); candidate.type = JingleRawUDPTransportPayload::Candidate::Host; transport->addCandidate(candidate); return true; } return false; } void ScreenSharing::handleSessionTerminateReceived(boost::optional reason) { if (rtpSession) rtpSession->stop(); if (reason.is_initialized() && reason.get().type == JinglePayload::Reason::Cancel) { onStateChange(ScreenSharing::Canceled); } else if (reason.is_initialized() && reason.get().type == JinglePayload::Reason::Success) { onStateChange(ScreenSharing::Finished); } else { onStateChange(ScreenSharing::Failed); } onFinished(); } }