summaryrefslogtreecommitdiffstats
blob: 43e3fbc2b34f97cefccc3f1b1564035083b810d4 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (c) 2012 Yoann Blein
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swiften/ScreenSharing/IncomingScreenSharing.h>

#include <Swiften/Jingle/JingleSession.h>
#include <Swiften/Elements/JingleRawUDPTransportPayload.h>
#include <Swiften/Elements/JingleRTPDescription.h>
#include <Swiften/Network/UDPSocketFactory.h>
#include <Swiften/Network/UDPSocket.h>
#include <Swiften/Network/TimerFactory.h>
#include <Swiften/Network/Timer.h>
#include <Swiften/ScreenSharing/RTPSessionImpl.h>
#include <Swiften/ScreenSharing/VP8Decoder.h>
#include <Swiften/ScreenSharing/VP8RTPParser.h>
#include <Swiften/Queries/Request.h>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Base/FloatCompare.h>

#include <boost/bind.hpp>

namespace Swift {

IncomingScreenSharing::IncomingScreenSharing(boost::shared_ptr<JingleSession> session, UDPSocketFactory* udpSocketFactory,
											 TimerFactory* timerFactory, IQRouter* iqRouter, boost::shared_ptr<JingleContentPayload> content)
	: ScreenSharing(session, udpSocketFactory),
	  initialContent(content), parser(0), decoder(0), lastMouveMoveEvent(InputEventPayload::Event::MouseMove),
	  inputEventPayload(boost::make_shared<InputEventPayload>()), eventSendingTimer(timerFactory->createTimer(500)),
	  iqRouter(iqRouter)
{
	onStateChange(ScreenSharing::WaitingForAccept);
	eventSendingTimer->onTick.connect(boost::bind(&IncomingScreenSharing::handleEventSendingTimerTick, this));
}

IncomingScreenSharing::~IncomingScreenSharing()
{
	eventSendingTimer->onTick.disconnect(boost::bind(&IncomingScreenSharing::handleEventSendingTimerTick, this));
	delete rtpSession;
	delete parser;
	delete decoder;
}

void IncomingScreenSharing::cancel()
{
	jingleSession->sendTerminate(JinglePayload::Reason::Cancel);
	if (rtpSession)
		rtpSession->stop();
	onStateChange(ScreenSharing::Canceled);
}

void IncomingScreenSharing::accept()
{
	JingleRawUDPTransportPayload::ref transport = boost::make_shared<JingleRawUDPTransportPayload>();
	if (!addBestCandidate(transport)) {
		SWIFT_LOG(error) << "Screen sharing: Unable to listening on any interface" << std::endl;
		jingleSession->sendTerminate(JinglePayload::Reason::FailedTransport);
		onStateChange(ScreenSharing::Failed);
		onFinished();
		return;
	}

	JingleRTPDescription::ref desc = initialContent->getDescription<JingleRTPDescription>();
	if (!desc->getPayloadTypes().empty())
		payloadTypeUsed = desc->getPayloadTypes().front();

	// TODO: create a valid description instead of copying the initator's one
	jingleSession->sendAccept(getContentID(), desc, transport);

	JingleRawUDPTransportPayload::ref initialTransport = initialContent->getTransport<JingleRawUDPTransportPayload>();
	clientSocket = udpSocketFactory->createUDPSocket();
	clientSocket->connect(initialTransport->getCandidates().front().hostAddressPort);
	// Send a empty packet to let the server know about us
	SafeByteArray data(1, 0);
	clientSocket->send(data);

	rtpSession = new RTPSessionImpl(clientSocket, payloadTypeUsed);

	if (payloadTypeUsed.getID() == 98 && payloadTypeUsed.getName() == "VP8") {
		decoder = new VP8Decoder;
		parser = new VP8RTPParser(decoder);
		rtpSession->onIncomingPacket.connect(boost::bind(&VP8RTPParser::newPayloadReceived, parser, _1, _2, _3));
		decoder->onNewFrameDecoded.connect(boost::bind(&IncomingScreenSharing::handleNewFrameDecoded, this, _1, _2));
		decoder->onNewImageAvailable.connect(boost::bind(&IncomingScreenSharing::handleNewImageAvailable, this, _1));
	}

	onStateChange(ScreenSharing::Connecting);
}

const JID &IncomingScreenSharing::getSender() const
{
	return jingleSession->getInitiator();
}

void IncomingScreenSharing::sendInputEvent(const InputEventPayload::Event& event)
{
	if (event.type == InputEventPayload::Event::Unknown)
		return;

	if (inputEventPayload->getEvents().empty()) {
		eventSendingTimer->start();
	}
	if (event.type == InputEventPayload::Event::MouseMove) {
		lastMouveMoveEvent.realArg1 = event.realArg1;
		lastMouveMoveEvent.realArg2 = event.realArg2;
	} else {
		addLastMouseMoveIfDifferent();
		inputEventPayload->addEvent(event);
	}
}

JingleContentID IncomingScreenSharing::getContentID() const
{
	return JingleContentID(initialContent->getName(), initialContent->getCreator());
}

void IncomingScreenSharing::handleNewFrameDecoded(int pictureID, bool success)
{
	if (success) {
		rtpSession->sendRPSIFeedback(pictureID);
	} else {
		rtpSession->sendSLIFeedback(pictureID);
	}
}

void IncomingScreenSharing::handleNewImageAvailable(const Image& image)
{
	onStateChange(ScreenSharing::Receiving);
	onNewImageReceived(image);
}

void IncomingScreenSharing::handleEventSendingTimerTick()
{
	addLastMouseMoveIfDifferent();
	boost::shared_ptr< GenericRequest<InputEventPayload> > request
			= boost::make_shared< GenericRequest<InputEventPayload> >(IQ::Set, getSender(), inputEventPayload, iqRouter);
	request->send();
	// Prepare for a new payload
	inputEventPayload.reset(new InputEventPayload);
}

void IncomingScreenSharing::addLastMouseMoveIfDifferent()
{
	const std::vector<InputEventPayload::Event>& events = inputEventPayload->getEvents();
	std::vector<InputEventPayload::Event>::const_reverse_iterator last;
	std::vector<InputEventPayload::Event>::const_reverse_iterator it;
	for (it = events.rbegin(); it != events.rend(); ++it) {
		if (it->type == InputEventPayload::Event::MouseMove) {
			last = it;
			break;
		}
	}
	if (it == events.rend()
			|| !approximatelyEqual(it->realArg1, lastMouveMoveEvent.realArg1)
			|| !approximatelyEqual(it->realArg2, lastMouveMoveEvent.realArg2)) {
		inputEventPayload->addEvent(lastMouveMoveEvent);
	}
}

}