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

#include <Swiften/ScreenSharing/OutgoingScreenSharing.h>

#include <Swiften/Elements/JinglePayload.h>
#include <Swiften/Elements/JingleRawUDPTransportPayload.h>
#include <Swiften/Elements/JingleRTPDescription.h>
#include <Swiften/Jingle/JingleSession.h>
#include <Swiften/Network/UDPSocket.h>
#include <Swiften/Network/TimerFactory.h>
#include <Swiften/Network/Timer.h>
#include <Swiften/ScreenSharing/RTPSessionImpl.h>

#include <boost/bind.hpp>

namespace Swift {

OutgoingScreenSharing::OutgoingScreenSharing(boost::shared_ptr<JingleSession> session, UDPSocketFactory* udpSocketFactory, TimerFactory* timerFactory)
	: ScreenSharing(session, udpSocketFactory),
	  contentID(JingleContentID(idGenerator.generateID(), JingleContentPayload::InitiatorCreator)), canceled(false),
	  sessionAccepted(false), socketConnected(false), rtpSession(0), timerFactory(timerFactory)
{
	session->onSessionAcceptReceived.connect(boost::bind(&OutgoingScreenSharing::handleSessionAcceptReceived, this, _1, _2, _3));
}

OutgoingScreenSharing::~OutgoingScreenSharing()
{
}

void OutgoingScreenSharing::cancel()
{
	canceled = true;
	session->sendTerminate(JinglePayload::Reason::Cancel);

	onStateChange(ScreenSharing::Canceled);
}

void OutgoingScreenSharing::start()
{
	//onStateChange(ScreenSharing::WaitingForStart);
	SWIFT_LOG(debug) << "Screen sharing: start" << std::endl;

	JingleRTPDescription::ref desc = boost::make_shared<JingleRTPDescription>(JingleRTPDescription::Video);
	payloadTypeUsed = RTPPayloadType(98, "VP8", 90000);
	desc->addPayloadType(payloadTypeUsed);

	JingleRawUDPTransportPayload::ref transport = boost::make_shared<JingleRawUDPTransportPayload>();
	addBestCandidate(transport);

	session->sendInitiate(contentID, desc, transport);
	onStateChange(ScreenSharing::WaitingForAccept);

	serverSocket->onConnected.connect(boost::bind(&OutgoingScreenSharing::handleSocketConnected, this));
	serverSocket->connectToFirstIncoming();
}

void OutgoingScreenSharing::handleSocketConnected()
{
	if (canceled)
		return;
	SWIFT_LOG(debug) << "Screen sharing: UDP socket connected" << std::endl;

	socketConnected = true;
	if (sessionAccepted)
		startRTPSession();
}

void OutgoingScreenSharing::handleSessionAcceptReceived(const JingleContentID& /*id*/, boost::shared_ptr<JingleDescription> /*desc*/, boost::shared_ptr<JingleTransportPayload> /*transport*/)
{
	if (canceled)
		return;
	SWIFT_LOG(debug) << "Screen sharing:  accepted" << std::endl;

	// TODO: check desc and transport

	sessionAccepted = true;
	if (socketConnected) {
		startRTPSession();
	} else {
		connectionTimer = timerFactory->createTimer(1000);
		connectionTimer->onTick.connect(boost::bind(&OutgoingScreenSharing::handleConnectionFailed, this));
		connectionTimer->start();
		onStateChange(ScreenSharing::Connecting);
	}
}

void OutgoingScreenSharing::handleConnectionFailed()
{
	SWIFT_LOG(debug) << "Screen sharing: unable to connect" << std::endl;

	session->sendTerminate(JinglePayload::Reason::ConnectivityError);
	canceled = true;

	onStateChange(ScreenSharing::Failed);
}

void OutgoingScreenSharing::startRTPSession()
{
	SWIFT_LOG(debug) << "Screen sharing: accepted and connected, start sharing" << std::endl;

	// Session accepted and socket connected, we can start the rtp session
	rtpSession = new RTPSessionImpl(serverSocket, payloadTypeUsed);
}

}