summaryrefslogtreecommitdiffstats
blob: 7fb128056ce2295187670f94d5cb45bb9920ca97 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * 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 <boost/bind.hpp>

namespace Swift {

ScreenSharing::ScreenSharing(boost::shared_ptr<JingleSession> session, UDPSocketFactory* udpSocketFactory)
	: rtpSession(0), jingleSession(session), udpSocketFactory(udpSocketFactory)
{
	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();

	// Find the first ip which is not loopback
	/*foreach (const NetworkInterface& interface, interfaces) {
		if (!interface.isLoopback() && !interface.getAddresses().empty()) { // exclude loopback
			serverSocket->bindOnAvailablePort(interface.getAddresses().front());

			candidate.hostAddressPort = serverSocket->getLocalAddress();
			candidate.type = JingleRawUDPTransportPayload::Candidate::Host;
			transport->addCandidate(candidate);

			return true;
		}
	}*/

	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::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->bindOnAvailablePort(addr);
//				int port = serverSocket->bind(HostAddressPort(addr, 29999));
				if (!port)
					continue;

				candidate.hostAddressPort = serverSocket->getLocalAddress();
				candidate.type = JingleRawUDPTransportPayload::Candidate::Host;
				transport->addCandidate(candidate);

				return true;
			}
		}
	}

	/*
	int port = serverSocket->bind(HostAddressPort(HostAddress("0.0.0.0"), 29999));
	if (!port)
		return false;

	candidate.hostAddressPort = HostAddressPort(HostAddress("82.225.14.174"), 29999);
	candidate.type = JingleRawUDPTransportPayload::Candidate::Host;
	transport->addCandidate(candidate);

	return true;
*/

	// else loopback for self sharing
	/*if (!interfaces.empty() && !interfaces.front().getAddresses().empty()) {
		int port = serverSocket->bindOnAvailablePort(interfaces.front().getAddresses().front());
		if (port) {
			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();
}

}