summaryrefslogtreecommitdiffstats
blob: eede971a63bbc226531979255b8348dd858308ab (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
/*
 * 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)
{
	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<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();

	// 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<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();
}

}