summaryrefslogtreecommitdiffstats
blob: 005e2046463c01a20a5f8fd5797ff46153b9bbc9 (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
/*
 * 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/Base/Algorithm.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 <Swiften/ScreenSharing/VP8Encoder.h>
#include <Swiften/ScreenSharing/VP8RTPPacketizer.h>
#include <Swiften/ScreenSharing/Image.h>

#include <boost/bind.hpp>

namespace Swift {

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

OutgoingScreenSharing::~OutgoingScreenSharing()
{
	jingleSession->onSessionAcceptReceived.disconnect(boost::bind(&OutgoingScreenSharing::handleSessionAcceptReceived, this, _1, _2, _3));
	delete rtpSession;
	delete encoder;
	delete packetizer;
}

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

	onStateChange(ScreenSharing::Canceled);
}

void OutgoingScreenSharing::start(unsigned int width, unsigned int height)
{
	//onStateChange(ScreenSharing::WaitingForStart);
	this->width = width;
	this->height = height;

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

	JingleRawUDPTransportPayload::ref transport = boost::make_shared<JingleRawUDPTransportPayload>();
	if (addBestCandidate(transport)) {
		SWIFT_LOG(debug) << "Screen sharing: start" << std::endl;
		jingleSession->sendInitiate(contentID, desc, transport);
		serverSocket->onConnected.connect(boost::bind(&OutgoingScreenSharing::handleSocketConnected, this));
		serverSocket->connectToFirstIncoming();
		onStateChange(ScreenSharing::WaitingForAccept);
	} else {
		SWIFT_LOG(error) << "Screen sharing: Unable to listening on any interface" << std::endl;
		onStateChange(ScreenSharing::Failed);
		onFinished();
	}
}

void OutgoingScreenSharing::addImage(const Image &image)
{
	encoder->encodeImage(image);
}

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

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

	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;

	connectionTimer->onTick.disconnect(boost::bind(&OutgoingScreenSharing::handleConnectionFailed, this));

	jingleSession->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);

	if (payloadTypeUsed.getID() == 98 && payloadTypeUsed.getName() == "VP8") {
		packetizer = new VP8RTPPacketizer;
		encoder = new VP8Encoder(packetizer, width, height);
		packetizer->onNewPayloadReady.connect(boost::bind(&OutgoingScreenSharing::handleNewPayloadReady, this, _1, _2));
		onReady();
	}
}

void OutgoingScreenSharing::handleNewPayloadReady(const std::vector<uint8_t>& data, bool marker)
{
	SafeByteArray sba(data.begin(), data.end());
	rtpSession->sendPacket(sba, 500, marker);
}

}