-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideo.cpp
More file actions
835 lines (737 loc) · 30.6 KB
/
Copy pathvideo.cpp
File metadata and controls
835 lines (737 loc) · 30.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// clang-format off
#include "Polyweb/polyweb.hpp"
// clang-format on
#include "video.hpp"
#include "json.hpp"
#include "keys.hpp"
#include "ui.hpp"
#include "util.hpp"
#include <FL/fl_ask.H>
#include <FL/fl_draw.H>
#include <FL/x.H>
#include <cmath>
#include <gst/app/gstappsrc.h> // For GST_APP_LEAKY_TYPE_*; header-only, no extra linkage
#include <gst/video/videooverlay.h>
#include <inttypes.h>
using nlohmann::json;
bool VideoWindow::should_grab_keyboard() const {
if (conn_info.view_only || !pointer_inside) {
return false;
}
#ifdef _WIN32
return Fl::focus() != nullptr;
#else
// Ask X, rather than trusting Fl::focus(). FLTK derives that from every
// FocusOut it receives, including the ones emitted when *any* client grabs the
// keyboard, so it flickers whenever the window manager's task switcher takes
// the keyboard -- precisely when we must not decide we have focus
Fl_Window* window = top_window();
if (!window) {
return false;
}
Window focus_window = None;
int revert = 0;
XGetInputFocus(fl_x11_display(), &focus_window, &revert);
return focus_window == fl_xid(window);
#endif
}
void VideoWindow::grab_reconcile_callback(void* data) {
auto window = (VideoWindow*) data;
// Only asks X when we believe we hold the grab, so this costs nothing otherwise
if (window->keyboard_grab_manager && window->keyboard_grab_manager->keyboard_grabbed && !window->should_grab_keyboard()) {
// Logged because this firing means the focus handling above missed a
// transition and the grab was stuck: silence here is the evidence that the
// real fix is holding, and noise is the evidence that it is not
std::cerr << "Warning: keyboard was grabbed while unfocused, releasing "
"(the focus handling missed a transition)"
<< std::endl;
window->keyboard_grab_manager->ungrab_keyboard();
window->release_all_keys();
}
Fl::repeat_timeout(1.0, grab_reconcile_callback, data);
}
int VideoWindow::system_event_handler(void* event, void* data) {
auto window = (VideoWindow*) data;
#ifndef _WIN32
// The grab is otherwise tied to pointer crossing alone, so switching away
// without moving the pointer leaves it held, and XGrabKeyboard with
// owner_events=False then swallows every keystroke on the whole display.
//
// FLTK is no help: it maps every FocusIn/FocusOut to FL_FOCUS/FL_UNFOCUS and
// discards xfocus.mode, so our own grab's focus events look identical to real
// ones and reacting to them oscillates. Only NotifyWhileGrabbed means focus
// genuinely moved while we held the keyboard; NotifyGrab and NotifyUngrab are
// emitted by the grab and ungrab themselves.
//
// Deliberately asymmetric: quick to release, reluctant to take. Failing to
// re-grab just means one mouse move to restore capture, whereas grabbing while
// unfocused is the bug this exists to fix
if (window->keyboard_grab_manager) {
auto x11_event = (XEvent*) event;
if (x11_event->type == FocusOut && x11_event->xfocus.mode == NotifyWhileGrabbed) {
if (window->keyboard_grab_manager->keyboard_grabbed) {
window->keyboard_grab_manager->ungrab_keyboard();
window->release_all_keys(); // Or keys held at switch time stay down on the remote
}
} else if (x11_event->type == FocusIn &&
(x11_event->xfocus.mode == NotifyNormal || x11_event->xfocus.mode == NotifyWhileGrabbed) &&
x11_event->xfocus.detail != NotifyPointer &&
x11_event->xfocus.detail != NotifyPointerRoot) {
// A FocusIn on our window does not by itself mean we hold the focus
// now, so the condition is re-derived rather than inferred
if (window->should_grab_keyboard()) {
window->keyboard_grab_manager->grab_keyboard();
}
}
}
#endif
if (window->mouse_manager) {
auto parsed_event = window->mouse_manager->parse_event(event);
if (parsed_event.has_value()) {
json message = {
{"type", "mousemove"},
{"x", (int) std::round(parsed_event->x)},
{"y", (int) std::round(parsed_event->y)},
};
if (try_send(window->unordered_channel, message.dump())) {
return 1;
}
}
}
return 0;
}
void VideoWindow::loading_timer_callback(void* data) {
auto window = (VideoWindow*) data;
if (!window->connected) {
window->redraw();
Fl::repeat_timeout(1.0 / 60.0, loading_timer_callback, data);
}
}
VideoWindow::VideoWindow(int x, int y, int width, int height, ConnectionInfo conn_info):
Fl_Double_Window(x, y, width, height),
conn_info(std::move(conn_info)) {
resizable(this);
end(); // No child widgets!
rtc::Configuration config;
config.iceServers.emplace_back("stun.l.google.com:19302");
config.enableIceTcp = true;
conn = std::make_shared<rtc::PeerConnection>(config);
{
rtc::Description::Video video("video", rtc::Description::Direction::RecvOnly);
video.addH264Codec(96);
video_track = conn->addTrack(video);
}
{
rtc::Description::Audio audio("audio", rtc::Description::Direction::RecvOnly);
audio.addOpusCodec(97);
audio_track = conn->addTrack(audio);
}
video_track->setMediaHandler(std::make_shared<rtc::RtcpReceivingSession>());
audio_track->setMediaHandler(std::make_shared<rtc::RtcpReceivingSession>());
video_track->onOpen([this]() {
video_track->requestBitrate(this->conn_info.bitrate * 1000);
});
file_manager = std::make_unique<FileManager>(ordered_channel = conn->createDataChannel("ordered-input"));
if (!conn_info.view_only) {
unordered_channel = conn->createDataChannel("unordered-input",
{
.reliability = {
.unordered = true,
},
});
}
cancel_token = std::make_shared<std::atomic<bool>>(false);
gathering_waiter = std::make_shared<Waiter>();
conn->onGatheringStateChange([gathering_waiter_ptr = gathering_waiter](rtc::PeerConnection::GatheringState state) {
if (state == rtc::PeerConnection::GatheringState::Complete) {
gathering_waiter_ptr->notify_one();
}
});
conn->setLocalDescription();
auto cancel_token_copy = cancel_token;
auto gathering_waiter_copy = gathering_waiter;
auto conn_info_copy = this->conn_info;
auto conn_copy = conn;
std::thread([this, cancel_token_copy, gathering_waiter_copy, conn_info_copy, conn_copy]() {
if (!gathering_waiter_copy->wait_for(std::chrono::seconds(5))) {
if (*cancel_token_copy) return;
awake([cancel_token_copy, this]() {
if (*cancel_token_copy) return;
connection_error = true;
fl_alert("Failed to connect: Timed out waiting for ICE gathering to complete");
});
return;
}
if (*cancel_token_copy) return;
std::string offer;
{
auto description = conn_copy->localDescription();
if (!description.has_value()) return;
json offer_json = {
{"type", description->typeString()},
{"sdp", std::string(description.value())},
};
offer = offer_json.dump();
}
json req_json = {
{"password", conn_info_copy.password},
{"show_mouse", conn_info_copy.view_only || !conn_info_copy.client_side_mouse},
{"offer", pw::base64_encode(offer.data(), offer.size())},
};
pw::ClientConfig client_config = {
.tcp = {
.send_timeout = std::chrono::seconds(5),
.recv_timeout = std::chrono::seconds(5),
},
};
// Polyweb's default context verifies peers, so one of our own is only needed when
// verification is off. It must outlive the fetch that borrows it
pn::TLSContext unverified_context;
if (!conn_info_copy.verify_certs) {
// Not pn::Status, because X11 defines Status as a macro
if (auto init_res = unverified_context.init_client(SSL_VERIFY_NONE); !init_res) {
if (*cancel_token_copy) return;
awake([cancel_token_copy, this, err = init_res.error().message()]() {
if (*cancel_token_copy) return;
connection_error = true;
fl_alert("Failed to connect: %s", err.c_str());
});
return;
}
client_config.tls_context = &unverified_context;
}
pw::Response resp;
if (auto fetch_res = pw::fetch("POST",
"https://" + conn_info_copy.address + "/offer",
resp,
req_json.dump(),
{{"Content-Type", "application/json"}},
client_config);
!fetch_res) {
if (*cancel_token_copy) return;
awake([cancel_token_copy, this, err = fetch_res.error().message()]() {
if (*cancel_token_copy) return;
connection_error = true;
fl_alert("Failed to connect: %s", err.c_str());
});
return;
} else if (resp.status_code != 200) {
if (*cancel_token_copy) return;
awake([cancel_token_copy, this, status_code = resp.status_code]() {
if (*cancel_token_copy) return;
connection_error = true;
fl_alert("Failed to login: Response has status code %" PRIu16, status_code);
});
return;
}
if (*cancel_token_copy) return;
std::unique_ptr<rtc::Description> answer;
try {
json resp_json = json::parse(resp.body_string());
json answer_json = json::parse(pw::base64_decode(resp_json["Offer"].get<std::string>()));
answer = std::make_unique<rtc::Description>(answer_json["sdp"].get<std::string>(), answer_json["type"].get<std::string>());
} catch (const std::exception& e) {
if (*cancel_token_copy) return;
awake([cancel_token_copy, this, err = std::string(e.what())]() {
if (*cancel_token_copy) return;
connection_error = true;
fl_alert("Failed to start streaming: Failed to parse server answer: %s", err.c_str());
});
return;
}
if (*cancel_token_copy) return;
std::shared_ptr<rtc::Description> answer_shared = std::move(answer);
awake([cancel_token_copy, this, answer_shared, conn_copy]() {
if (*cancel_token_copy) return;
conn_copy->setRemoteDescription(*answer_shared);
connected = true;
if (!this->conn_info.view_only && Fl::belowmouse() == this && Fl::focus()) {
keyboard_grab_manager->grab_keyboard();
}
});
}).detach();
}
bool VideoWindow::is_connected() const {
return connected;
}
bool VideoWindow::has_connection_error() const {
return connection_error;
}
bool VideoWindow::is_playing() const {
return playing;
}
rtc::PeerConnection::IceState VideoWindow::ice_state() const {
return conn->iceState();
}
void VideoWindow::show() {
Fl_Double_Window::show();
Fl::flush(); // Force the underlying OS window to be realized so that GStreamer can find it
loading_start_time = std::chrono::steady_clock::now();
Fl::add_timeout(1.0 / 60.0, loading_timer_callback, this);
if (!conn_info.view_only) {
if (!conn_info.client_side_mouse) {
mouse_manager = std::make_unique<RawMouseManager>(this);
}
keyboard_grab_manager = std::make_unique<KeyboardGrabManager>(top_window());
// Serves the focus handling, so it is needed in every non-view-only mode
Fl::add_system_handler(&VideoWindow::system_event_handler, this);
Fl::add_timeout(1.0, grab_reconcile_callback, this);
}
video_pipeline = gst_pipeline_new(nullptr);
{
GstElement* appsrc = gst_element_factory_make("appsrc", nullptr);
{
GstCaps* caps = gst_caps_new_simple("application/x-rtp", "media", G_TYPE_STRING, "video", "encoding-name", G_TYPE_STRING, "H264", "clock-rate", G_TYPE_INT, 90000, nullptr);
// leaky-type is what bounds the queue: with the default (none) appsrc
// accepts everything even past max-bytes, so a decode stall becomes
// latency that never recovers. min-latency=0 shaves the tail slightly.
// Casts matter, g_object_set is varargs and these are 64-bit properties
g_object_set(appsrc, "caps", caps, "emit-signals", FALSE, "format", GST_FORMAT_TIME, "is-live", TRUE, "do-timestamp", TRUE, "min-latency", (gint64) 0, "leaky-type", GST_APP_LEAKY_TYPE_DOWNSTREAM, nullptr);
gst_caps_unref(caps);
}
video_track->onMessage([appsrc](rtc::binary message) {
GstBuffer* buf = gst_buffer_new_and_alloc(message.size());
GstMapInfo map;
gst_buffer_map(buf, &map, GST_MAP_WRITE);
memcpy(map.data, message.data(), message.size());
gst_buffer_unmap(buf, &map);
GstFlowReturn flow;
g_signal_emit_by_name(appsrc, "push-buffer", buf, &flow);
gst_buffer_unref(buf);
},
nullptr);
GstElement* rtpjitterbuffer = gst_element_factory_make("rtpjitterbuffer", nullptr);
g_object_set(rtpjitterbuffer, "latency", 0, nullptr);
GstElement* rtph264depay = gst_element_factory_make("rtph264depay", nullptr);
#ifdef _WIN32
GstElement* h264parse = gst_element_factory_make("h264parse", nullptr);
#endif
// Software decoding on every platform, including Windows. d3d11h264dec
// measures faster in isolation, 0.36ms against 1.36ms, but starves its
// surface pool and back-pressures upstream: with it in place
// rtpjitterbuffer sat at 26.17ms mean against 0.32ms with software
// decoding, on captures that differed in nothing else.
//
// thread-type is pinned rather than left on "auto": auto resolves to slice
// threading today, but if libav ever picks frame threading it buffers ~19
// frames, which measured as 320ms of latency against 5ms for slice
GstElement* h264dec = gst_element_factory_make("avdec_h264", nullptr);
if (h264dec) {
g_object_set(h264dec, "direct-rendering", FALSE, "thread-type", 2 /* slice */, nullptr);
}
if (!h264dec) {
fl_alert("Failed to create the H.264 decoder");
return;
}
{
glib::Object<GstPad> pad = gst_element_get_static_pad(h264dec, "src");
gst_pad_add_probe(pad.get(), GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, [](GstPad* pad, GstPadProbeInfo* info, void* data) {
auto video_info = (VideoInfo*) data;
GstEvent* event = GST_PAD_PROBE_INFO_EVENT(info);
if (GST_EVENT_TYPE(event) == GST_EVENT_CAPS) {
GstCaps* caps;
gst_event_parse_caps(event, &caps);
GstStructure* structure = gst_caps_get_structure(caps, 0);
video_info->mutex.lock();
gst_structure_get_int(structure, "width", &video_info->width);
gst_structure_get_int(structure, "height", &video_info->height);
video_info->mutex.unlock();
}
return GST_PAD_PROBE_OK;
},
&video_info,
nullptr);
}
#ifdef _WIN32
GstElement* videosink = gst_element_factory_make("d3d11videosink", nullptr);
if (videosink) {
g_object_set(videosink, "enable-navigation-events", FALSE, nullptr);
}
#elif defined(__APPLE__)
GstElement* videosink = gst_element_factory_make("osxvideosink", nullptr);
#else
GstElement* videosink = gst_element_factory_make("xvimagesink", nullptr);
#endif
if (!videosink) {
fl_alert("Failed to create the video sink");
return;
}
// GstBaseSink pads its reported latency by processing-deadline (15ms on
// GstVideoSink, 20ms otherwise) and holds every frame that long. Zeroing it
// measured 20.4ms -> 5.2ms of render latency, independent of framerate.
// max-lateness is deliberately left at its default: it has no latency effect
// (measured), and pairing 0 slack with drop-anything-late invites frame loss
g_object_set(videosink, "processing-deadline", (guint64) 0, nullptr);
// Render on arrival rather than waiting for each frame's timestamp. Syncing
// paces playback to the sender's timing, which for a desktop means holding
// a frame back to show a staler one: the newest state should reach the
// screen as soon as it exists. A burst is then painted back to back, and
// if the decoder falls behind for real the appsrcs are leaky-type=downstream
// so the oldest input is dropped instead of queued.
//
// processing-deadline above is moot while this is off, but is left set so
// that turning sync back on gets the tuned behaviour and not the default
g_object_set(videosink, "sync", FALSE, nullptr);
gst_bin_add_many(GST_BIN(video_pipeline.get()),
appsrc,
rtpjitterbuffer,
rtph264depay,
#ifdef _WIN32
h264parse,
#endif
h264dec,
videosink,
nullptr);
if (!gst_element_link_many(
appsrc,
rtpjitterbuffer,
rtph264depay,
#ifdef _WIN32
h264parse,
#endif
h264dec,
videosink,
nullptr)) {
fl_alert("Failed to link GStreamer elements (video pipeline)");
return;
}
gst_video_overlay_handle_events(GST_VIDEO_OVERLAY(videosink), FALSE);
#ifdef _WIN32
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(videosink), (uintptr_t) fl_xid(this));
#elif defined(__APPLE__)
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(videosink), (uintptr_t) fl_xid(this));
#else
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(videosink), fl_xid(this));
#endif
overlay = GST_VIDEO_OVERLAY(videosink);
}
audio_pipeline = gst_pipeline_new(nullptr);
{
GstElement* appsrc = gst_element_factory_make("appsrc", nullptr);
{
GstCaps* caps = gst_caps_new_simple("application/x-rtp", "media", G_TYPE_STRING, "audio", "encoding-name", G_TYPE_STRING, "OPUS", "clock-rate", G_TYPE_INT, 48000, "payload", G_TYPE_INT, 97, nullptr);
g_object_set(appsrc, "caps", caps, "format", GST_FORMAT_TIME, "is-live", TRUE, "do-timestamp", TRUE, "min-latency", (gint64) 0, "leaky-type", GST_APP_LEAKY_TYPE_DOWNSTREAM, nullptr);
gst_caps_unref(caps);
}
audio_track->onMessage([appsrc](rtc::binary message) {
GstBuffer* buf = gst_buffer_new_and_alloc(message.size());
GstMapInfo map;
gst_buffer_map(buf, &map, GST_MAP_WRITE);
memcpy(map.data, message.data(), message.size());
gst_buffer_unmap(buf, &map);
GstFlowReturn flow;
g_signal_emit_by_name(appsrc, "push-buffer", buf, &flow);
gst_buffer_unref(buf);
},
nullptr);
GstElement* rtpopusdepay = gst_element_factory_make("rtpopusdepay", nullptr);
GstElement* capsfilter = gst_element_factory_make("capsfilter", nullptr);
{
GstCaps* caps = gst_caps_new_simple("audio/x-opus", "channels", G_TYPE_INT, 2, nullptr);
g_object_set(capsfilter, "caps", caps, nullptr);
gst_caps_unref(caps);
}
GstElement* opusdec = gst_element_factory_make("opusdec", nullptr);
GstElement* audioconvert = gst_element_factory_make("audioconvert", nullptr);
GstElement* autoaudiosink = gst_element_factory_make("autoaudiosink", nullptr);
gst_bin_add_many(GST_BIN(audio_pipeline.get()),
appsrc,
rtpopusdepay,
capsfilter,
opusdec,
audioconvert,
autoaudiosink,
nullptr);
if (!gst_element_link_many(appsrc,
rtpopusdepay,
capsfilter,
opusdec,
audioconvert,
autoaudiosink,
nullptr)) {
fl_alert("Failed to link GStreamer elements (audio pipeline)");
return;
}
}
gst_element_set_state(video_pipeline.get(), GST_STATE_PLAYING);
gst_element_set_state(audio_pipeline.get(), GST_STATE_PLAYING);
playing = true;
take_focus();
}
void VideoWindow::hide() {
Fl::remove_timeout(loading_timer_callback, this);
if (cancel_token) {
*cancel_token = true;
cancel_token.reset();
}
if (gathering_waiter) {
gathering_waiter->notify_all();
gathering_waiter.reset();
}
if (!conn_info.view_only) {
// Removed first: both touch the managers reset below
Fl::remove_timeout(grab_reconcile_callback, this);
Fl::remove_system_handler(&VideoWindow::system_event_handler);
if (!conn_info.client_side_mouse) {
mouse_manager.reset();
}
keyboard_grab_manager.reset();
}
file_manager.reset();
{
json message = {
{"type", "disconnect"},
};
try_send(ordered_channel, message.dump());
}
// The track callbacks below push into the GStreamer pipelines this function
// is about to destroy, and PeerConnection::close() closes tracks
// asynchronously, so it cannot be relied on to detach them in time.
// resetCallbacks() waits for any callback already in flight to return
if (video_track) video_track->resetCallbacks();
if (audio_track) audio_track->resetCallbacks();
conn->close();
connected = false;
overlay = nullptr;
if (video_pipeline) {
gst_element_set_state(video_pipeline.get(), GST_STATE_NULL);
video_pipeline.reset();
}
if (audio_pipeline) {
gst_element_set_state(audio_pipeline.get(), GST_STATE_NULL);
audio_pipeline.reset();
}
playing = false;
Fl_Double_Window::hide();
}
void VideoWindow::draw() {
if (!connected || !playing) {
Fl_Double_Window::draw();
int text_w = 0, text_h = 0;
fl_font(labelfont(), 18);
fl_measure("Connecting...", text_w, text_h);
int spinner_size = (int) (text_h * 0.75);
int margin = 12;
int spinner_x = w() / 2 - text_w / 2 - margin - spinner_size;
int spinner_y = h() / 2 - spinner_size / 2;
double time = std::chrono::duration<double>(std::chrono::steady_clock::now() - loading_start_time).count();
double cycle_duration = 1.5;
double cycle_count_d = time / cycle_duration;
int cycle_count = (int) cycle_count_d;
double progress = cycle_count_d - cycle_count;
double head_angle, tail_angle;
if (progress < 0.5) {
double sub_progress = progress * 2.0;
double eased = 0.5 * (1.0 - std::cos(sub_progress * 3.14159265));
head_angle = eased * 270.0;
tail_angle = 0.0;
} else {
double sub_progress = (progress - 0.5) * 2.0;
double eased = 0.5 * (1.0 - std::cos(sub_progress * 3.14159265));
head_angle = 270.0;
tail_angle = eased * 270.0;
}
head_angle += 45.0;
double cycle_offset = cycle_count * 270.0;
double continuous_rotation = time * 360.0 * 0.75;
// Calculate absolute head angle in CW direction
double cw_head_total = cycle_offset + head_angle + continuous_rotation;
// Prevent X11 XDrawArc overflow by keeping angle under 360 degrees.
// X11 represents angles in 1/64ths of a degree within a 16-bit short integer.
// It overflows at 32768 / 64 = 512 degrees!
double cw_head = std::fmod(cw_head_total, 360.0);
if (cw_head < 0) cw_head += 360.0;
double arc_len = head_angle - tail_angle;
double a1 = -cw_head;
double a2 = a1 + arc_len;
fl_line_style(FL_SOLID, 3, nullptr);
fl_color(fl_color_average(labelcolor(), color(), 0.75f));
fl_arc(spinner_x, spinner_y, spinner_size, spinner_size, a1, a2);
fl_line_style(0);
fl_font(labelfont(), 18);
fl_color(labelcolor());
fl_draw("Connecting...", 0, 0, w(), h(), FL_ALIGN_CENTER);
} else if (overlay) {
gst_video_overlay_expose(GST_VIDEO_OVERLAY(overlay));
}
}
void VideoWindow::flush() {
if (connected && playing) {
Fl_Window::flush();
} else {
Fl_Double_Window::flush();
}
}
int VideoWindow::handle(int event) {
if (connected) {
switch (event) {
case FL_PUSH:
take_focus();
if (!conn_info.view_only) {
if (!conn_info.client_side_mouse && !mouse_manager->mouse_locked) {
mouse_manager->lock_mouse();
return 1;
} else {
json message = {
{"type", "mousedown"},
{"button", Fl::event_button() - 1},
};
if (try_send(ordered_channel, message.dump())) {
return 1;
}
}
}
break;
case FL_RELEASE:
if (!conn_info.view_only) {
json message = {
{"type", "mouseup"},
{"button", Fl::event_button() - 1},
};
if (try_send(ordered_channel, message.dump())) {
return 1;
}
}
break;
case FL_MOVE:
case FL_DRAG:
if (!conn_info.view_only) {
if (conn_info.client_side_mouse) {
int x;
int y;
position_in_video(Fl::event_x(), Fl::event_y(), x, y);
json message = {
{"type", "mousemoveabs"},
{"x", x},
{"y", y},
};
if (try_send(ordered_channel, message.dump())) {
return 1;
}
} else {
return 1; // This is handled by the RawMouseManager
}
}
break;
case FL_MOUSEWHEEL:
if (!conn_info.view_only) {
json message = {
{"type", "wheel"},
{"x", (int) std::round(Fl::event_dx() * 120.0)},
{"y", (int) std::round(Fl::event_dy() * 120.0)},
};
if (try_send(unordered_channel, message.dump())) {
return 1;
}
}
break;
case FL_KEYUP:
if (!conn_info.view_only) {
if (!conn_info.client_side_mouse && Fl::event_key() == FL_F + 9) {
if (mouse_manager->mouse_locked) {
mouse_manager->unlock_mouse();
} else {
mouse_manager->lock_mouse();
}
return 1;
} else if (!is_key_global_shortcut(Fl::event_key())) {
json message = {
{"type", "keyup"},
{"key", fltk_to_browser_key(Fl::event_key())},
};
if (try_send(ordered_channel, message.dump())) {
return 1;
}
}
}
break;
case FL_KEYDOWN:
if (!conn_info.view_only && !is_key_global_shortcut(Fl::event_key())) {
json message = {
{"type", "keydown"},
{"key", fltk_to_browser_key(Fl::event_key())},
};
if (try_send(ordered_channel, message.dump())) {
return 1;
}
}
break;
case FL_FOCUS:
case FL_UNFOCUS:
if (!conn_info.view_only) {
return 1;
}
break;
case FL_ENTER:
if (!conn_info.view_only) {
pointer_inside = true;
if (should_grab_keyboard()) {
keyboard_grab_manager->grab_keyboard();
}
return 1;
}
break;
case FL_LEAVE:
if (!conn_info.view_only) {
pointer_inside = false;
// No release_all_keys() here: leaving the window drops the grab but
// keeps focus, so keys still reach the remote and releasing them
// would break holding one while the pointer drifts off
keyboard_grab_manager->ungrab_keyboard();
return 1;
}
break;
}
}
return Fl_Double_Window::handle(event);
}
void VideoWindow::position_in_video(int x, int y, int& x_ret, int& y_ret) {
double cw = w();
double ch = h();
std::lock_guard<std::mutex> lock(video_info.mutex);
double vw = video_info.width;
double vh = video_info.height;
if (vw * ch > cw * vh) {
x_ret = x * vw / cw;
y_ret = (y - ch / 2.0) * vw / cw + vh / 2.0;
} else {
x_ret = (x - cw / 2.0) * vh / ch + vw / 2.0;
y_ret = y * vh / ch;
}
}
unsigned int VideoWindow::get_bitrate() const {
return conn_info.bitrate;
}
// rtc::Track throws once its transport is gone, which happens as soon as the
// connection drops, so neither of these may be called unguarded
void VideoWindow::set_bitrate(unsigned int bitrate) {
conn_info.bitrate = bitrate;
try {
video_track->requestBitrate(bitrate * 1000);
} catch (const std::exception& e) {
std::cerr << "Failed to request bitrate: " << e.what() << std::endl;
}
}
void VideoWindow::request_keyframe() {
try {
video_track->requestKeyframe();
} catch (const std::exception& e) {
std::cerr << "Failed to request keyframe: " << e.what() << std::endl;
}
}
void VideoWindow::release_input_grabs() {
if (keyboard_grab_manager) {
keyboard_grab_manager->ungrab_keyboard();
}
if (mouse_manager) {
mouse_manager->unlock_mouse();
}
}
void VideoWindow::release_all_keys() {
if (!conn_info.view_only) {
json message = {
{"type", "releaseall"},
};
try_send(ordered_channel, message.dump());
}
}