summaryrefslogtreecommitdiffstats
blob: de0b430a4687ee427a8849ef19b439d141a4ad96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (c) 2012 Yoann Blein
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swiften/ScreenSharing/ScreenSharing.h>

#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/JingleRawUDPTransportPayload.h>
#include <Swiften/Network/PlatformNetworkEnvironment.h>
#include <Swiften/Network/UDPSocket.h>
#include <Swiften/Network/UDPSocketFactory.h>
#include <Swiften/ScreenSharing/RTPSession.h>
#include <Swiften/Jingle/JingleSession.h>
#include <Swiften/FileTransfer/ConnectivityManager.h>

#include <boost/bind.hpp>

namespace Swift {

ScreenSharing::ScreenSharing(boost::shared_ptr<JingleSession> 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<JingleRawUDPTransportPayload> transport)
{
	// TODO: NAT traversal

	JingleRawUDPTransportPayload::Candidate candidate;
	candidate.cid = idGenerator.generateID();
	candidate.component = 1;
	candidate.generation = 0;

	PlatformNetworkEnvironment env;
	std::vector<NetworkInterface> 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<HostAddressPort> 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<JinglePayload::Reason> 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();
}

}