/* * 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 #include namespace Swift { ScreenSharing::ScreenSharing(boost::shared_ptr session, UDPSocketFactory* udpSocketFactory, ConnectivityManager* connectivityManager) : rtpSession(0), jingleSession(session), udpSocketFactory(udpSocketFactory), connectivityManager(connectivityManager) { jingleSession->onSessionTerminateReceived.connect(boost::bind(&ScreenSharing::handleSessionTerminateReceived, this, _1)); } ScreenSharing::~ScreenSharing() { jingleSession->onSessionTerminateReceived.disconnect(boost::bind(&ScreenSharing::handleSessionTerminateReceived, this, _1)); } 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(); serverSocket = udpSocketFactory->createUDPSocket(); /* SWIFT_LOG(debug) << "Screen sharing: Addresses available: " << std::endl; foreach (const NetworkInterface& interface, interfaces) { SWIFT_LOG(debug) << "\tInterface: " << interface.getName() << std::endl; foreach (const HostAddress& addr, interface.getAddresses()) { SWIFT_LOG(debug) << "\t\t" << addr.getRawAddress().to_string() << std::endl; } } */ std::vector assisted = connectivityManager->getAssistedHostAddressPorts(NATPortMapping::UDP); foreach (HostAddressPort hap, assisted) { int port = serverSocket->bind(HostAddressPort(HostAddress("0.0.0.0"), hap.getPort())); if (port != hap.getPort()) continue; SWIFT_LOG(debug) << "Listening on " << port << std::endl; candidate.hostAddressPort = hap; candidate.type = JingleRawUDPTransportPayload::Candidate::Host; transport->addCandidate(candidate); return true; } std::string scopeLinkBeginning("fe80"); foreach (const NetworkInterface& interface, interfaces) { if (!interface.isLoopback()) { // exclude loopback foreach (const HostAddress& addr, interface.getAddresses()) { // Disable ipv6 for now if (addr.getRawAddress().is_v6()) continue; // Ignore link scope ipv6 addr if (addr.getRawAddress().is_v6() && addr.toString().compare(2, scopeLinkBeginning.length(), scopeLinkBeginning) == 0) continue; int port = serverSocket->bind(HostAddressPort(addr, 29999)); if (port != 29999) port = serverSocket->bindOnAvailablePort(addr); if (!port) continue; candidate.hostAddressPort = serverSocket->getLocalAddress(); 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(); } }