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

#include <boost/bind.hpp>
#include <boost/filesystem.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <iostream>

#include <Swiften/Base/Log.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Client/Client.h>
#include <Swiften/Elements/DiscoInfo.h>
#include <Swiften/Network/BoostNetworkFactories.h>
#include <Swiften/EventLoop/SimpleEventLoop.h>
#include <Swiften/Client/ClientXMLTracer.h>
#include <Swiften/Disco/ClientDiscoManager.h>
#include <Swiften/ScreenSharing/ScreenSharingManager.h>
#include <Swiften/ScreenSharing/IncomingScreenSharing.h>
#include <Swiften/ScreenSharing/Image.h>

using namespace Swift;

SimpleEventLoop eventLoop;
BoostNetworkFactories networkFactories(&eventLoop);

int exitCode = 2;

static const std::string CLIENT_NAME = "Swiften FT Test";
static const std::string CLIENT_NODE = "http://swift.im";

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

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

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

		void stop() {
			//foreach(const IncomingFileTransfer::ref transfer, incomingFileTransfers) {
				//transfer->stop();
			//}
			client->disconnect();
		}

	private:
		void handleConnected() {
			Swift::logging = true;
			client->getScreenSharingManager()->onIncomingScreenSharing.connect(boost::bind(&ScreenReceiver::handleIncomingScreenSharing, this, _1));
			
			DiscoInfo discoInfo;
			discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc"));
			discoInfo.addFeature(DiscoInfo::JingleFeature);
			discoInfo.addFeature(DiscoInfo::JingleRTPFeature);
			discoInfo.addFeature(DiscoInfo::JingleTransportRawUDPFeature);
			discoInfo.addFeature(DiscoInfo::JingleScreenSharingFeature);
			client->getDiscoManager()->setCapsNode(CLIENT_NODE);
			client->getDiscoManager()->setDiscoInfo(discoInfo);
			client->getPresenceSender()->sendPresence(Presence::create());
		}

		void handleIncomingScreenSharing(IncomingScreenSharing::ref sharing) {
			SWIFT_LOG(debug) << "Incoming screen sharing" << std::endl;
			incomingScreenSharings.push_back(sharing);
			sharing->onNewImageReceived.connect(boost::bind(&ScreenReceiver::handleNewImageReceived, this, _1));
			sharing->onFinished.connect(boost::bind(&ScreenReceiver::handleScreenSharingFinished, this));
			sharing->accept();
			//transfer->onFinished.connect(boost::bind(&ScreenReceiver::handleFileTransferFinished, this, _1));
			//transfer->start();
		}

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

		void handleNewImageReceived(const Image& img) {
			const std::vector<uint8_t>& data = img.data;
			std::cout << "Image received: " << (int)data[0] << ", " << (int)data[1] << ", " << (int)data[2] << std::endl;
		}

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

		void exit(int code) {
			exitCode = code;
			stop();
			eventLoop.stop();
		}
	
	private:
		JID jid;
		std::string password;
		Client* client;
		ClientXMLTracer* tracer;
		std::vector<IncomingScreenSharing::ref> incomingScreenSharings;
};


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

	JID jid(argv[1]);
	ScreenReceiver screenReceiver(jid, std::string(argv[2]));
	screenReceiver.start();

	eventLoop.run();

	return exitCode;
}