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

//#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
//#include <boost/filesystem.hpp>
#include <iostream>

#include <Swiften/Client/Client.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Elements/Presence.h>
//#include <Swiften/Network/BoostTimer.h>
//#include <Swiften/Network/TimerFactory.h>
#include <Swiften/Network/BoostNetworkFactories.h>
//#include <Swiften/EventLoop/EventLoop.h>
#include <Swiften/Client/ClientXMLTracer.h>
#include <Swiften/EventLoop/SimpleEventLoop.h>
//#include <Swiften/FileTransfer/OutgoingSIFileTransfer.h>
//#include <Swiften/FileTransfer/FileReadBytestream.h>
//#include <Swiften/FileTransfer/SOCKS5BytestreamServer.h>
//#include <Swiften/Network/BoostConnectionServer.h>
//#include <Swiften/FileTransfer/OutgoingScreenSharingManager.h>
#include <Swiften/ScreenSharing/OutgoingScreenSharing.h>
//#include <Swiften/Jingle/JingleSessionManager.h>
#include <Swiften/Disco/EntityCapsManager.h>
//#include <Swiften/FileTransfer/DefaultLocalJingleTransportCandidateGeneratorFactory.h>
//#include <Swiften/FileTransfer/DefaultRemoteJingleTransportCandidateSelectorFactory.h>
//#include <Swiften/Base/ByteArray.h>
//#include <Swiften/StringCodecs/MD5.h>
//#include <Swiften/StringCodecs/SHA1.h>
//#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/ScreenSharing/ScreenSharingManager.h>
#include <Swiften/ScreenSharing/Image.h>

using namespace Swift;

SimpleEventLoop eventLoop;
BoostNetworkFactories networkFactories(&eventLoop);

int exitCode = 2;

class ScreenSharer {
	public:
		ScreenSharer(const JID& jid, const std::string& password, const JID& recipient)
			: jid(jid), password(password), recipient(recipient)
		{
			client = new Swift::Client(jid, password, &networkFactories);
			client->onConnected.connect(boost::bind(&ScreenSharer::handleConnected, this));
			client->onDisconnected.connect(boost::bind(&ScreenSharer::handleDisconnected, this, _1));
			tracer = new ClientXMLTracer(client);
			client->getEntityCapsProvider()->onCapsChanged.connect(boost::bind(&ScreenSharer::handleCapsChanged, this, _1));
		}

		~ScreenSharer() {
			delete tracer;
			client->onDisconnected.disconnect(boost::bind(&ScreenSharer::handleDisconnected, this, _1));
			client->onConnected.disconnect(boost::bind(&ScreenSharer::handleConnected, this));
			delete client;
		}

		void start() {
			ClientOptions options;
			options.useTLS = ClientOptions::NeverUseTLS;
			client->connect(options);
		}

	private:
		void handleConnected() {
			client->sendPresence(Presence::create());
		}

		void handleCapsChanged(JID jid) {
			if (jid.toBare() == recipient.toBare() || jid == recipient) {
				std::cout << "Recipient found" << std::endl;
				outgoingScreenSharing = client->getScreenSharingManager()->createOutgoingScreenSharing(recipient);

				if (outgoingScreenSharing) {
					client->getEntityCapsProvider()->onCapsChanged.disconnect(boost::bind(&ScreenSharer::handleCapsChanged, this, _1));
					std::cout << "started screen sharing" << std::endl;
					outgoingScreenSharing->onReady.connect(boost::bind(&ScreenSharer::handleRTPReady, this));
					outgoingScreenSharing->onFinished.connect(boost::bind(&ScreenSharer::handleScreenSharingFinished, this));
					outgoingScreenSharing->start(200, 200);
				} else {
					std::cout << "[ ERROR ] " << recipient << " doesn't support any kind of screen sharing!" << std::endl;
					//client->disconnect();
				}
			}
		}

		void handleRTPReady() {
			uint8_t data[200*200*3];
			data[0] = 0;
			data[1] = 128;
			data[2] = 255;
			Image img(200, 200, (const uint8_t*)data);
			for (int i = 0; i < 10; ++i)
				outgoingScreenSharing->addImage(img);
			outgoingScreenSharing->stop();

		}

		void handleDisconnected(const boost::optional<ClientError>&) {
			std::cerr << "Error!" << std::endl;
			exit(-1);
		}

		void handleScreenSharingFinished() {
			std::cout << "Screen sharing finished" << std::endl;
			exit(0);
		}

		void exit(int code) {
			exitCode = code;
			eventLoop.stop();
		}

	private:
		OutgoingScreenSharing::ref outgoingScreenSharing;
		JID jid;
		std::string password;
		JID recipient;
		Client* client;
		ClientXMLTracer* tracer;
};


int main(int argc, char* argv[]) {
	if (argc < 4) {
		std::cerr << "Usage: " << argv[0] << " <jid> <password> <recipient>" << std::endl;
		return -1;
	}

	JID sender(argv[1]);
	JID recipient(argv[3]);
	Swift::logging = true;
	ScreenSharer screenSharer(sender, std::string(argv[2]), recipient);
	screenSharer.start();

	eventLoop.run();

	return exitCode;
}