forked from structureio/uplink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-desktop-server.cpp
267 lines (204 loc) · 8.59 KB
/
example-desktop-server.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// This file is part of Uplink, an easy-to-use cross-platform live RGBD streaming protocol.
// Copyright (c) 2016, Occipital, Inc. All rights reserved.
// License: See LICENSE.
#include <uplink.h>
#include "desktop-server.h"
#include "desktop-ui.h"
static const bool sendPingPongColorFeedback = true;
static const bool dumpStatsPeriodically = true;
using namespace uplink;
//------------------------------------------------------------------------------
struct ExampleServerSession : DesktopServerSession
{
ExampleServerSession(int socketDescriptor, Server* server)
: DesktopServerSession(socketDescriptor, server)
{
}
void toggleExposureAndWhiteBalance()
{
SessionSetup sessionSetup;
static bool toggle = true;
if (toggle)
{
sessionSetup.addSetColorCameraExposureModeAction(ColorCameraExposureMode_Locked);
sessionSetup.addSetColorCameraWhiteBalanceModeAction(ColorCameraWhiteBalanceMode_Locked);
std::cout << "LOCKED!" << std::endl;
uplink_log_info("Locked exposure and white balance.");
}
else
{
sessionSetup.addSetColorCameraExposureModeAction(ColorCameraExposureMode_ContinuousAuto);
sessionSetup.addSetColorCameraWhiteBalanceModeAction(ColorCameraWhiteBalanceMode_ContinuousAuto);
uplink_log_info("Automatic exposure and white balance.");
}
server()._currentSession->sendSessionSetup(sessionSetup);
toggle = !toggle;
}
virtual void onCustomCommand(const String& command)
{
if (command == "RecordButtonPressed")
{
std::cout << "Start Recording Pressed!" << std::endl;
}
else if (command == "AutoLevelButtonPressed")
{
toggleExposureAndWhiteBalance();
}
}
virtual bool onMessage(const Message& message)
{
// Do not call blocking functions from this point on. Network performance will suffer greatly otherwise.
switch (message.kind())
{
//case uplink::MessageKind_MessageFragment:
//{
// const MessageFragment& messageFragment = message.as<MessageFragment>();
// std::cout << messageFragment.toString() << std::endl;
//
// break;
//}
case MessageKind_DeviceMotionEvent:
{
std::cout << "IMU" << std::endl;
break;
}
case MessageKind_CameraFrame:
{
const CameraFrame& cameraFrame = message.as<CameraFrame>();
if (!cameraFrame.colorImage.isEmpty())
{
server().ui().setColorImage(
(const uint8*)cameraFrame.colorImage.planes[0].buffer,
int(cameraFrame.colorImage.width),
int(cameraFrame.colorImage.height)
);
}
if (!cameraFrame.depthImage.isEmpty())
{
uint16* depthBuffer = (uint16*)cameraFrame.depthImage.planes[0].buffer;
int depthWidth = int(cameraFrame.depthImage.width);
int depthHeight = int(cameraFrame.depthImage.height);
// Convert shifts to depth values.
shift2depth(depthBuffer, depthWidth * depthHeight);
server().ui().setDepthImage(
depthBuffer,
depthWidth,
depthHeight
);
}
// Send ping-pong feedback image.
// FIXME: This const-cast sucks.
if (sendPingPongColorFeedback && !cameraFrame.colorImage.isEmpty())
//sendFeedbackImage(const_cast<Image&>(cameraFrame.colorImage));
sendImage(const_cast<Image&>(cameraFrame.colorImage));
static unsigned long long count = 1; // FIXME: Use a real steady-rate timer.
//if (dumpStatsPeriodically && 0 == count % 150)
//{
// uplink_log_info("Camera receiving rate: %f Hz", server()._currentSession->messageStats[MessageKind_CameraFrame].receiving.rate.windowedRate());
// uplink_log_info("Motion receiving rate: %f Hz", server()._currentSession->messageStats[MessageKind_DeviceMotionEvent].receiving.rate.windowedRate());
// uplink_log_info("Feedback image sending rate: %f Hz", server()._currentSession->channels[ChannelId_Feedback].stats.sending.rate.windowedRate());
//}
++count;
break;
}
default:
{
std::cout << "Other" << std::endl;
break;
}
}
return true;
}
};
//------------------------------------------------------------------------------
struct ExampleSessionSetup : SessionSetup
{
ExampleSessionSetup(bool colorOnly)
{
addSetColorModeAction(ColorMode_VGA);
if (colorOnly)
{
addSetDepthModeAction(DepthMode_None);
}
else // Depth too.
{
addSetDepthModeAction(DepthMode_VGA);
addSetRegistrationModeAction(RegistrationMode_RegisteredDepth);
addSetFrameSyncModeAction(FrameSyncMode_Depth);
}
//addSetColorSendingStrategyAction(ColorSendingStrategy_AllFrames);
//addSetColorSendingSporadicityAction(1);
//addSetCameraChannelSettingsAction(ChannelSettings(DroppingStrategy_DropAllButLatestOne));
//addSetMotionModeAction(MotionMode_None);
//addSetMotionRateAction(100.f);
//addSetColorCameraExposureModeAction(ColorCameraExposureMode_ContinuousAuto);
//addSetColorCameraWhiteBalanceModeAction(ColorCameraWhiteBalanceMode_ContinuousAuto);
//addSetDepthCameraCodecAction(ImageCodecId_CompressedShifts);
//addSetColorCameraCodecAction(ImageCodecId_JPEG);
//addSetColorFeedbackCodecAction(ImageCodecId_JPEG);
ChannelSettings channelSettings;
channelSettings.droppingStrategy = DroppingStrategy_RandomOne;
channelSettings.droppingThreshold = 90;
channelSettings.bufferingStrategy = BufferingStrategy_Some;
addSetRGBDFrameChannelAction(channelSettings);
addSetSendMotionAction(false);
addSetMotionRateAction(100);
addSetColorCameraExposureModeAction(ColorCameraExposureMode_ContinuousAuto);
addSetColorCameraWhiteBalanceModeAction(ColorCameraWhiteBalanceMode_ContinuousAuto);
addSetDepthCameraCodecAction(ImageCodecId_CompressedShifts);
addSetColorCameraCodecAction(ImageCodecId_JPEG);
addSetFeedbackImageCodecAction(ImageCodecId_JPEG);
}
};
//------------------------------------------------------------------------------
struct ExampleServerDelegate : ServerDelegate
{
void sendClearAllButtonsCommand()
{
_server->_currentSession->sendCustomCommand("button:clear:*");
}
void sendButtonCreateCommand(std::string buttonPngFilepath, std::string commandName)
{
CustomCommand customCommand;
customCommand.command += "button:create:";
customCommand.command += char(0);
customCommand.command += commandName;
customCommand.command += '\0';
std::ifstream f(buttonPngFilepath, std::ios::binary);
if (!f.is_open()) {
std::cerr << "ERROR: wrong button file path" << std::endl;
getchar();
}
std::string imageBytes((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
customCommand.command.insert(customCommand.command.end(), imageBytes.begin(), imageBytes.end());
_server->_currentSession->sendCustomCommand(customCommand);
}
virtual ServerSession* newSession(int socketDescriptor, Server* server)
{
_server = server;
return new ExampleServerSession(socketDescriptor, server);
}
virtual void onConnect(uintptr_t sessionId)
{
sendClearAllButtonsCommand();
sendButtonCreateCommand("record-button.png", "RecordButtonPressed");
sendButtonCreateCommand("auto-level-button.png", "AutoLevelButtonPressed");
bool rgbOnly = true;
_server->_currentSession->sendSessionSetup(
ExampleSessionSetup(rgbOnly)
);
}
Server* _server;
};
//------------------------------------------------------------------------------
int
main()
{
using namespace uplink;
ExampleServerDelegate serverDelegate;
// DesktopServer server("CaptureReceiverExample", UPLINK_SERVER_DEFAULT_TCP_PORT, &serverDelegate);
DesktopServer server("CaptureReceiverExample", 6666, &serverDelegate);
if (!server.startListening())
return 1;
server.ui().run();
}