summaryrefslogtreecommitdiffstats
blob: a747b81e0b91e7746009e4f54c0ed4daa9176607 (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
/*
 * Copyright (c) 2012 Yoann Blein
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swiften/ScreenSharing/ScreenSharingManagerImpl.h>

#include <Swiften/Base/foreach.h>
#include <Swiften/Disco/EntityCapsProvider.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Presence/PresenceOracle.h>
#include <Swiften/ScreenSharing/IncomingScreenSharingManager.h>
#include <Swiften/ScreenSharing/OutgoingScreenSharingManager.h>

namespace Swift {

ScreenSharingManagerImpl::ScreenSharingManagerImpl(const JID& ownFullJID, JingleSessionManager *jingleSessionManager, IQRouter *iqRouter,
												   UDPSocketFactory* udpSocketFactory, TimerFactory* timerFactory, PresenceOracle* presenceOrable,
												   EntityCapsProvider* capsProvider)
	: ownJID(ownFullJID)/*, jingleSM(jingleSessionManager), iqRouter(iqRouter), udpSocketFactory(udpSocketFactory), timerFactory(timerFactory)*/, capsProvider(capsProvider),  presenceOracle(presenceOrable)
{
	incomingSSManager = new IncomingScreenSharingManager(jingleSessionManager, iqRouter, udpSocketFactory);
	outgoingSSManager = new OutgoingScreenSharingManager(jingleSessionManager, iqRouter, udpSocketFactory, timerFactory);

	incomingSSManager->onIncomingScreenSharing.connect(onIncomingScreenSharing);
}

ScreenSharingManagerImpl::~ScreenSharingManagerImpl()
{
}

boost::shared_ptr<OutgoingScreenSharing> ScreenSharingManagerImpl::createOutgoingScreenSharing(const JID &to)
{
	JID recipient = to;

	if (recipient.isBare()) {
		boost::optional<JID> fullJID = highestPriorityJIDSupportingScreenSharing(recipient);
		if (fullJID.is_initialized()) {
			recipient = fullJID.get();
		} else {
			return boost::shared_ptr<OutgoingScreenSharing>();
		}
	}

	return outgoingSSManager->createOutgoingScreenSharing(ownJID, recipient);
}

boost::optional<JID> ScreenSharingManagerImpl::highestPriorityJIDSupportingScreenSharing(const JID& bareJID) {
	int priority = INT_MIN;
	JID fullRecipientJID;

	std::vector<Presence::ref> presences = presenceOracle->getAllPresence(bareJID);
	foreach (Presence::ref pres, presences) {
		if (pres->getPriority() > priority) {
			DiscoInfo::ref info = capsProvider->getCaps(pres->getFrom()); // look up caps from the jid
			if (info && info->hasFeature(DiscoInfo::JingleFeature)
					&& info->hasFeature(DiscoInfo::JingleRTPFeature)
					&& info->hasFeature(DiscoInfo::JingleTransportRawUDPFeature)) {
				priority = pres->getPriority();
				fullRecipientJID = pres->getFrom();
			}
		}
	}

	return fullRecipientJID.isValid() ? boost::optional<JID>(fullRecipientJID) : boost::optional<JID>();
}

}