summaryrefslogtreecommitdiffstats
blob: 2ddfab91181083b7cacea37682ab60c69a6f1818 (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
/*
 * 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/ScreenSharing/RTPSessionImpl.h>
#include <Swiften/ScreenSharing/VP8Decoder.h>
#include <Swiften/ScreenSharing/VP8RTPParser.h>

#include <boost/bind.hpp>

namespace Swift {

IncomingScreenSharing::IncomingScreenSharing(boost::shared_ptr<JingleSession> session, UDPSocketFactory* udpSocketFactory,
											 boost::shared_ptr<JingleContentPayload> content)
	: ScreenSharing(session, udpSocketFactory),
	  initialContent(content), parser(0), decoder(0)
{
	onStateChange(ScreenSharing::WaitingForAccept);
}

IncomingScreenSharing::~IncomingScreenSharing()
{
	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>();
	addBestCandidate(transport);
	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->onNewImageDecoded.connect(boost::bind(&IncomingScreenSharing::hangleNewImageDecoded, this, _1));
	}

	onStateChange(ScreenSharing::Connecting);
}

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

void IncomingScreenSharing::hangleNewImageDecoded(const Image& image)
{
	onNewImageReceived(image);
}

}