@ng-native/desktop 0.26.1 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/README.md +6 -3
  2. package/bin/app-config.mjs +1 -0
  3. package/bin/archive.mjs +166 -0
  4. package/bin/autolink.mjs +61 -30
  5. package/bin/cli.mjs +127 -27
  6. package/bin/host.mjs +255 -58
  7. package/bin/package-linux.mjs +1394 -0
  8. package/bin/package.mjs +171 -35
  9. package/bin/plist.mjs +6 -2
  10. package/bin/png-icon.mjs +315 -0
  11. package/bin/prebuilt.mjs +147 -26
  12. package/bin/toolchain.mjs +258 -19
  13. package/native/CMakeLists.txt +64 -6
  14. package/native/appkit/CMakeLists.txt +2 -1
  15. package/native/appkit/appkit-backend.h +1 -0
  16. package/native/appkit/appkit-backend.mm +13 -0
  17. package/native/appkit/header-toolbar.mm +28 -25
  18. package/native/appkit/host-view.h +10 -0
  19. package/native/appkit/host-view.mm +146 -40
  20. package/native/appkit/input-replay.h +6 -0
  21. package/native/appkit/input-replay.mm +21 -12
  22. package/native/appkit/main.mm +11 -7
  23. package/native/appkit/native-controls.mm +13 -1
  24. package/native/appkit/split-view.mm +7 -13
  25. package/native/appkit/text-input-field.h +4 -0
  26. package/native/appkit/text-input-field.mm +20 -1
  27. package/native/cmake/curl.cmake +79 -0
  28. package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
  29. package/native/cmake/libraries/react-native-worklets.cmake +5 -0
  30. package/native/cmake/linked-modules.cmake +20 -4
  31. package/native/cmake/react-native.cmake +18 -1
  32. package/native/core/include/ngn/accent-colors.h +38 -0
  33. package/native/core/include/ngn/accessibility-relations.h +65 -0
  34. package/native/core/include/ngn/accessibility.h +26 -0
  35. package/native/core/include/ngn/animated-frame-loop.h +56 -0
  36. package/native/core/include/ngn/app-info.h +24 -8
  37. package/native/core/include/ngn/app-menus.h +14 -1
  38. package/native/core/include/ngn/app-windows.h +15 -2
  39. package/native/core/include/ngn/aria-role.h +96 -0
  40. package/native/core/include/ngn/audio.h +2 -0
  41. package/native/core/include/ngn/camera.h +9 -0
  42. package/native/core/include/ngn/control-sizes.h +7 -2
  43. package/native/core/include/ngn/crash-report.h +31 -0
  44. package/native/core/include/ngn/curl-network.h +93 -0
  45. package/native/core/include/ngn/dbus-menu.h +39 -0
  46. package/native/core/include/ngn/default-menu.h +36 -0
  47. package/native/core/include/ngn/desktop-expo.h +7 -1
  48. package/native/core/include/ngn/desktop-modules.h +22 -0
  49. package/native/core/include/ngn/desktop-props.h +21 -1
  50. package/native/core/include/ngn/desktop-view-props.h +6 -0
  51. package/native/core/include/ngn/focus-order.h +10 -0
  52. package/native/core/include/ngn/header-bar.h +22 -0
  53. package/native/core/include/ngn/host-os.h +6 -0
  54. package/native/core/include/ngn/host-reload.h +21 -0
  55. package/native/core/include/ngn/host-state.h +23 -0
  56. package/native/core/include/ngn/image-source.h +26 -1
  57. package/native/core/include/ngn/input-script.h +3 -3
  58. package/native/core/include/ngn/input.h +4 -1
  59. package/native/core/include/ngn/launch-frame.h +21 -0
  60. package/native/core/include/ngn/launch-settings.h +23 -5
  61. package/native/core/include/ngn/mounting-backend.h +5 -0
  62. package/native/core/include/ngn/mounting-manager.h +1 -0
  63. package/native/core/include/ngn/native-controls.h +26 -5
  64. package/native/core/include/ngn/notification-schedule.h +74 -0
  65. package/native/core/include/ngn/notifications.h +2 -1
  66. package/native/core/include/ngn/openssl-update-crypto.h +15 -0
  67. package/native/core/include/ngn/photo-library.h +60 -20
  68. package/native/core/include/ngn/posix-locale.h +43 -0
  69. package/native/core/include/ngn/runtime.h +5 -0
  70. package/native/core/include/ngn/safe-areas.h +14 -2
  71. package/native/core/include/ngn/split-view.h +78 -0
  72. package/native/core/include/ngn/surface-metrics.h +7 -0
  73. package/native/core/include/ngn/system.h +14 -0
  74. package/native/core/include/ngn/text-input.h +13 -0
  75. package/native/core/include/ngn/text-units.h +38 -0
  76. package/native/core/include/ngn/updates.h +14 -2
  77. package/native/core/include/ngn/view-box.h +40 -0
  78. package/native/core/include/ngn/view-transform.h +15 -0
  79. package/native/core/include/ngn/web-browser.h +46 -0
  80. package/native/core/src/accent-colors.cpp +88 -0
  81. package/native/core/src/accessibility-relations.cpp +138 -0
  82. package/native/core/src/accessibility.cpp +50 -0
  83. package/native/core/src/animated-frame-loop.cpp +166 -0
  84. package/native/core/src/app-menus.cpp +26 -9
  85. package/native/core/src/app-windows.cpp +27 -6
  86. package/native/core/src/aria-role.cpp +127 -0
  87. package/native/core/src/control-sizes.cpp +9 -5
  88. package/native/core/src/crash-report.cpp +196 -0
  89. package/native/core/src/curl-network.cpp +1562 -0
  90. package/native/core/src/dbus-menu.cpp +71 -0
  91. package/native/core/src/default-menu.cpp +134 -0
  92. package/native/core/src/desktop-view-props.cpp +14 -0
  93. package/native/core/src/focus-order.cpp +19 -0
  94. package/native/core/src/header-bar.cpp +20 -0
  95. package/native/core/src/host-os.cpp +19 -0
  96. package/native/core/src/host-reload.cpp +45 -0
  97. package/native/core/src/host-state.cpp +8 -0
  98. package/native/core/src/image-source.cpp +90 -0
  99. package/native/core/src/input-dispatcher.cpp +27 -5
  100. package/native/core/src/input-script.cpp +3 -2
  101. package/native/core/src/launch-settings.cpp +76 -0
  102. package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
  103. package/native/core/src/modules/app-info.cpp +55 -6
  104. package/native/core/src/modules/app-modules.cpp +1 -1
  105. package/native/core/src/modules/audio.cpp +4 -2
  106. package/native/core/src/modules/camera.cpp +8 -11
  107. package/native/core/src/modules/desktop-expo.cpp +10 -5
  108. package/native/core/src/modules/desktop-modules.cpp +106 -41
  109. package/native/core/src/modules/file-photo-picker.cpp +408 -0
  110. package/native/core/src/modules/notifications.cpp +13 -17
  111. package/native/core/src/modules/photo-library.cpp +42 -13
  112. package/native/core/src/modules/web-browser.cpp +89 -0
  113. package/native/core/src/mounting-manager.cpp +12 -0
  114. package/native/core/src/native-controls.cpp +42 -1
  115. package/native/core/src/notification-schedule.cpp +429 -0
  116. package/native/core/src/openssl-update-crypto.cpp +83 -0
  117. package/native/core/src/posix-locale.cpp +192 -0
  118. package/native/core/src/runtime.cpp +30 -1
  119. package/native/core/src/safe-areas.cpp +19 -4
  120. package/native/core/src/spawn-rules.cpp +4 -1
  121. package/native/core/src/split-view.cpp +93 -0
  122. package/native/core/src/system.cpp +19 -0
  123. package/native/core/src/text-input.cpp +18 -1
  124. package/native/core/src/text-units.cpp +99 -0
  125. package/native/core/src/updates.cpp +76 -17
  126. package/native/core/src/view-box.cpp +75 -40
  127. package/native/core/src/view-transform.cpp +28 -0
  128. package/native/docker/Dockerfile +38 -0
  129. package/native/docker/Dockerfile.fedora +36 -0
  130. package/native/docker/glibc-floor.sh +49 -0
  131. package/native/gtk/CMakeLists.txt +137 -0
  132. package/native/gtk/accent-colour.cpp +57 -0
  133. package/native/gtk/accent-colour.h +41 -0
  134. package/native/gtk/accessibility-settings.cpp +159 -0
  135. package/native/gtk/accessibility-settings.h +62 -0
  136. package/native/gtk/accessible-text.cpp +151 -0
  137. package/native/gtk/accessible-text.h +49 -0
  138. package/native/gtk/accessible.cpp +306 -0
  139. package/native/gtk/accessible.h +75 -0
  140. package/native/gtk/alerts.cpp +267 -0
  141. package/native/gtk/alerts.h +30 -0
  142. package/native/gtk/animation-frames.cpp +159 -0
  143. package/native/gtk/animation-frames.h +20 -0
  144. package/native/gtk/app-info.cpp +91 -0
  145. package/native/gtk/app-info.h +25 -0
  146. package/native/gtk/app-windows.cpp +439 -0
  147. package/native/gtk/app-windows.h +150 -0
  148. package/native/gtk/audio.cpp +370 -0
  149. package/native/gtk/audio.h +54 -0
  150. package/native/gtk/auth-session.cpp +73 -0
  151. package/native/gtk/auth-session.h +16 -0
  152. package/native/gtk/battery.cpp +110 -0
  153. package/native/gtk/battery.h +19 -0
  154. package/native/gtk/box-paint.cpp +262 -0
  155. package/native/gtk/box-paint.h +32 -0
  156. package/native/gtk/cairo-canvas.cpp +755 -0
  157. package/native/gtk/cairo-canvas.h +49 -0
  158. package/native/gtk/camera.cpp +909 -0
  159. package/native/gtk/camera.h +69 -0
  160. package/native/gtk/capture.cpp +46 -0
  161. package/native/gtk/capture.h +19 -0
  162. package/native/gtk/clock-format.cpp +151 -0
  163. package/native/gtk/clock-format.h +36 -0
  164. package/native/gtk/crypto.cpp +75 -0
  165. package/native/gtk/crypto.h +14 -0
  166. package/native/gtk/dbus-properties.cpp +131 -0
  167. package/native/gtk/dbus-properties.h +61 -0
  168. package/native/gtk/drag-and-drop.cpp +339 -0
  169. package/native/gtk/drag-and-drop.h +30 -0
  170. package/native/gtk/file-dialogs.cpp +239 -0
  171. package/native/gtk/file-dialogs.h +21 -0
  172. package/native/gtk/file-system.cpp +212 -0
  173. package/native/gtk/file-system.h +25 -0
  174. package/native/gtk/focus.cpp +282 -0
  175. package/native/gtk/focus.h +37 -0
  176. package/native/gtk/fonts.cpp +89 -0
  177. package/native/gtk/fonts.h +22 -0
  178. package/native/gtk/gst-frames.cpp +150 -0
  179. package/native/gtk/gst-frames.h +47 -0
  180. package/native/gtk/gst-playback.cpp +496 -0
  181. package/native/gtk/gst-playback.h +81 -0
  182. package/native/gtk/gtk-backend.cpp +690 -0
  183. package/native/gtk/gtk-backend.h +134 -0
  184. package/native/gtk/host-sources.cpp +354 -0
  185. package/native/gtk/host-sources.h +46 -0
  186. package/native/gtk/image-manipulator.cpp +254 -0
  187. package/native/gtk/image-manipulator.h +30 -0
  188. package/native/gtk/image-view.cpp +256 -0
  189. package/native/gtk/image-view.h +54 -0
  190. package/native/gtk/input-replay.cpp +299 -0
  191. package/native/gtk/input-replay.h +36 -0
  192. package/native/gtk/launcher-badge.cpp +29 -0
  193. package/native/gtk/launcher-badge.h +21 -0
  194. package/native/gtk/locales.cpp +131 -0
  195. package/native/gtk/locales.h +23 -0
  196. package/native/gtk/location.cpp +274 -0
  197. package/native/gtk/location.h +19 -0
  198. package/native/gtk/main.cpp +1092 -0
  199. package/native/gtk/menus.cpp +447 -0
  200. package/native/gtk/menus.h +100 -0
  201. package/native/gtk/native-controls.cpp +831 -0
  202. package/native/gtk/native-controls.h +58 -0
  203. package/native/gtk/network-state.cpp +72 -0
  204. package/native/gtk/network-state.h +24 -0
  205. package/native/gtk/notifications.cpp +235 -0
  206. package/native/gtk/notifications.h +30 -0
  207. package/native/gtk/pango-text.cpp +274 -0
  208. package/native/gtk/pango-text.h +73 -0
  209. package/native/gtk/portal.cpp +451 -0
  210. package/native/gtk/portal.h +71 -0
  211. package/native/gtk/printing.cpp +338 -0
  212. package/native/gtk/printing.h +34 -0
  213. package/native/gtk/proxy-resolver.cpp +74 -0
  214. package/native/gtk/proxy-resolver.h +20 -0
  215. package/native/gtk/screens.cpp +1461 -0
  216. package/native/gtk/screens.h +274 -0
  217. package/native/gtk/scroll-view.cpp +414 -0
  218. package/native/gtk/scroll-view.h +82 -0
  219. package/native/gtk/secret-store.cpp +120 -0
  220. package/native/gtk/secret-store.h +27 -0
  221. package/native/gtk/settings-portal.cpp +129 -0
  222. package/native/gtk/settings-portal.h +47 -0
  223. package/native/gtk/split-view.cpp +1105 -0
  224. package/native/gtk/split-view.h +195 -0
  225. package/native/gtk/status-notifier.cpp +487 -0
  226. package/native/gtk/status-notifier.h +43 -0
  227. package/native/gtk/surface-input.cpp +251 -0
  228. package/native/gtk/surface-input.h +57 -0
  229. package/native/gtk/system-access.cpp +295 -0
  230. package/native/gtk/system-access.h +37 -0
  231. package/native/gtk/text-field.cpp +827 -0
  232. package/native/gtk/text-field.h +78 -0
  233. package/native/gtk/video.cpp +527 -0
  234. package/native/gtk/video.h +47 -0
  235. package/native/gtk/view-widget.cpp +431 -0
  236. package/native/gtk/view-widget.h +105 -0
  237. package/native/gtk/window-scale.cpp +95 -0
  238. package/native/gtk/window-scale.h +35 -0
  239. package/native/scripts/atspi-check.py +310 -0
  240. package/native/scripts/atspi-check.test.py +48 -0
  241. package/native/scripts/build.sh +20 -3
  242. package/native/scripts/compilers.sh +17 -0
  243. package/native/scripts/deb-depends-check.sh +22 -0
  244. package/native/scripts/fetch.sh +23 -2
  245. package/native/scripts/launch-cell.sh +157 -0
  246. package/native/scripts/launch-probe.sh +14 -3
  247. package/native/scripts/make-prebuilt.mjs +133 -44
  248. package/native/scripts/prebuilt-host-cache.mjs +71 -40
  249. package/native/scripts/reset-for-compilers.sh +44 -0
  250. package/native/scripts/start-xvfb.sh +35 -0
  251. package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
  252. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
  253. package/native/shim/worklets/linux-reanimated-props.h +14 -0
  254. package/native/shim/worklets/linux-worklets.h +23 -0
  255. package/package.json +6 -3
  256. package/prebuilt.json +70 -6
@@ -0,0 +1,150 @@
1
+ #include "gst-frames.h"
2
+
3
+ #include <gst/app/gstappsink.h>
4
+ #include <gst/video/video.h>
5
+
6
+ #include <utility>
7
+ #include <vector>
8
+
9
+ namespace ngn::gtk {
10
+
11
+ Frames::~Frames() {
12
+ if (pending_) {
13
+ g_object_unref(pending_);
14
+ }
15
+ if (shown_) {
16
+ g_object_unref(shown_);
17
+ }
18
+ }
19
+
20
+ void Frames::post(const std::shared_ptr<Frames> &frames, GdkTexture *texture) {
21
+ std::lock_guard lock(frames->mutex_);
22
+ if (frames->pending_) {
23
+ g_object_unref(frames->pending_);
24
+ }
25
+ frames->pending_ = texture;
26
+ // A clear stays for the watchers to hear first, even when a frame of the next source follows it.
27
+ frames->clear_ = frames->clear_ || texture == nullptr;
28
+ if (frames->scheduled_) {
29
+ return;
30
+ }
31
+ frames->scheduled_ = true;
32
+ g_idle_add_full(G_PRIORITY_HIGH_IDLE, [](gpointer data) -> gboolean {
33
+ auto &frames = *static_cast<std::shared_ptr<Frames> *>(data);
34
+ GdkTexture *texture;
35
+ bool clear;
36
+ {
37
+ std::lock_guard lock(frames->mutex_);
38
+ texture = std::exchange(frames->pending_, nullptr);
39
+ clear = std::exchange(frames->clear_, false);
40
+ frames->scheduled_ = false;
41
+ }
42
+ if (clear) {
43
+ frames->show(nullptr);
44
+ }
45
+ if (texture) {
46
+ frames->show(texture);
47
+ }
48
+ return G_SOURCE_REMOVE;
49
+ }, new std::shared_ptr<Frames>(frames), [](gpointer data) { delete static_cast<std::shared_ptr<Frames> *>(data); });
50
+ }
51
+
52
+ void Frames::show(GdkTexture *texture) {
53
+ if (shown_) {
54
+ g_object_unref(shown_);
55
+ }
56
+ shown_ = texture;
57
+ // A watcher may stop watching, itself or another, as it is called: each is looked up again.
58
+ std::vector<int> ids;
59
+ for (auto &[id, fn] : watchers_) {
60
+ ids.push_back(id);
61
+ }
62
+ for (int id : ids) {
63
+ auto found = watchers_.find(id);
64
+ if (found != watchers_.end()) {
65
+ auto fn = found->second;
66
+ fn(texture);
67
+ }
68
+ }
69
+ }
70
+
71
+ int Frames::watch(std::function<void(GdkTexture *)> fn) {
72
+ int id = next_++;
73
+ watchers_[id] = std::move(fn);
74
+ return id;
75
+ }
76
+
77
+ void Frames::unwatch(int id) {
78
+ watchers_.erase(id);
79
+ }
80
+
81
+ namespace {
82
+
83
+ void unmapFrame(gpointer data) {
84
+ auto *frame = static_cast<GstVideoFrame *>(data);
85
+ gst_video_frame_unmap(frame);
86
+ delete frame;
87
+ }
88
+
89
+ GstFlowReturn show(GstSample *sample, gpointer data) {
90
+ auto &frames = *static_cast<std::shared_ptr<Frames> *>(data);
91
+ if (!sample) {
92
+ return GST_FLOW_EOS;
93
+ }
94
+ if (GdkTexture *texture = textureOfSample(sample)) {
95
+ Frames::post(frames, texture);
96
+ }
97
+ gst_sample_unref(sample);
98
+ return GST_FLOW_OK;
99
+ }
100
+
101
+ GstFlowReturn onFrame(GstAppSink *sink, gpointer data) {
102
+ return show(gst_app_sink_pull_sample(sink), data);
103
+ }
104
+
105
+ // The frame a paused source stops on (its first, or where it was sought to), shown before it plays.
106
+ GstFlowReturn onPreroll(GstAppSink *sink, gpointer data) {
107
+ return show(gst_app_sink_pull_preroll(sink), data);
108
+ }
109
+
110
+ } // namespace
111
+
112
+ GdkTexture *textureOfSample(GstSample *sample) {
113
+ GstVideoInfo info;
114
+ if (!gst_video_info_from_caps(&info, gst_sample_get_caps(sample)) || GST_VIDEO_INFO_FORMAT(&info) != GST_VIDEO_FORMAT_RGBA) {
115
+ return nullptr;
116
+ }
117
+ auto *frame = new GstVideoFrame;
118
+ if (!gst_video_frame_map(frame, &info, gst_sample_get_buffer(sample), GST_MAP_READ)) {
119
+ delete frame;
120
+ return nullptr;
121
+ }
122
+ // The texture holds the mapped frame, so the decoded buffer is shown without a copy.
123
+ int stride = GST_VIDEO_FRAME_PLANE_STRIDE(frame, 0);
124
+ GBytes *bytes = g_bytes_new_with_free_func(GST_VIDEO_FRAME_PLANE_DATA(frame, 0), static_cast<gsize>(stride) * GST_VIDEO_FRAME_HEIGHT(frame),
125
+ unmapFrame, frame);
126
+ GdkTexture *texture =
127
+ gdk_memory_texture_new(GST_VIDEO_FRAME_WIDTH(frame), GST_VIDEO_FRAME_HEIGHT(frame), GDK_MEMORY_R8G8B8A8, bytes, stride);
128
+ g_bytes_unref(bytes);
129
+ return texture;
130
+ }
131
+
132
+ GstElement *makeFrameSink(const std::shared_ptr<Frames> &frames, bool sync) {
133
+ GstElement *sink = gst_element_factory_make("appsink", nullptr);
134
+ if (!sink) {
135
+ return nullptr;
136
+ }
137
+ // A player's frames in square pixels, which its video chain scales an anamorphic source to, so
138
+ // the texture's size is the picture's shape.
139
+ GstCaps *caps = gst_caps_from_string(sync ? "video/x-raw,format=RGBA,pixel-aspect-ratio=1/1" : "video/x-raw,format=RGBA");
140
+ g_object_set(sink, "caps", caps, "sync", sync ? TRUE : FALSE, "max-buffers", 2, "drop", TRUE, nullptr);
141
+ gst_caps_unref(caps);
142
+ GstAppSinkCallbacks callbacks{};
143
+ callbacks.new_sample = onFrame;
144
+ callbacks.new_preroll = onPreroll;
145
+ gst_app_sink_set_callbacks(GST_APP_SINK(sink), &callbacks, new std::shared_ptr<Frames>(frames),
146
+ [](gpointer data) { delete static_cast<std::shared_ptr<Frames> *>(data); });
147
+ return sink;
148
+ }
149
+
150
+ } // namespace ngn::gtk
@@ -0,0 +1,47 @@
1
+ // Decoded video frames handed from GStreamer to GTK: an `appsink` takes RGBA, each frame becomes a
2
+ // GdkMemoryTexture over the mapped buffer, with no copy, and the main loop shows the newest one
3
+ // only, to each picture watching: a frame the loop has not shown yet is replaced by the next.
4
+ // `gtk4paintablesink` is not used: el9 and Ubuntu 24.04 do not ship it.
5
+ #pragma once
6
+
7
+ #include <gst/gst.h>
8
+ #include <gtk/gtk.h>
9
+
10
+ #include <functional>
11
+ #include <map>
12
+ #include <memory>
13
+ #include <mutex>
14
+
15
+ namespace ngn::gtk {
16
+
17
+ class Frames {
18
+ public:
19
+ ~Frames();
20
+ // From any thread: the newest frame (taken over), or none with `texture` null.
21
+ static void post(const std::shared_ptr<Frames> &frames, GdkTexture *texture);
22
+ // The frame shown now, borrowed. Main loop only, as are watch and unwatch.
23
+ GdkTexture *shown() const { return shown_; }
24
+ // `fn` hears each frame shown, or null as the frames are cleared, until unwatched.
25
+ int watch(std::function<void(GdkTexture *)> fn);
26
+ void unwatch(int id);
27
+
28
+ private:
29
+ void show(GdkTexture *texture);
30
+
31
+ std::mutex mutex_;
32
+ GdkTexture *pending_{nullptr};
33
+ bool clear_{false};
34
+ bool scheduled_{false};
35
+ GdkTexture *shown_{nullptr};
36
+ std::map<int, std::function<void(GdkTexture *)>> watchers_;
37
+ int next_{0};
38
+ };
39
+
40
+ // An appsink, floating, whose frames go to `frames`, or null where appsink is not installed. `sync`
41
+ // renders each on the pipeline's clock, in square pixels, as a player's are.
42
+ GstElement *makeFrameSink(const std::shared_ptr<Frames> &frames, bool sync);
43
+
44
+ // A texture of `sample`'s RGBA frame over its buffer, with no copy; null for one that is not.
45
+ GdkTexture *textureOfSample(GstSample *sample);
46
+
47
+ } // namespace ngn::gtk
@@ -0,0 +1,496 @@
1
+ #include "gst-playback.h"
2
+
3
+ #include <gst/pbutils/missing-plugins.h>
4
+
5
+ #include <algorithm>
6
+ #include <mutex>
7
+ #include <utility>
8
+ #include <vector>
9
+
10
+ namespace ngn::gtk {
11
+
12
+ bool gstreamerHas(std::initializer_list<const char *> elements) {
13
+ static bool ready = [] { return gst_init_check(nullptr, nullptr, nullptr) == TRUE; }();
14
+ if (!ready) {
15
+ return false;
16
+ }
17
+ for (const char *name : elements) {
18
+ GstElementFactory *factory = gst_element_factory_find(name);
19
+ if (!factory) {
20
+ return false;
21
+ }
22
+ gst_object_unref(factory);
23
+ }
24
+ return true;
25
+ }
26
+
27
+ namespace {
28
+
29
+ // GstPlayFlags, which playbin3 takes as a flags property of its own type.
30
+ constexpr guint kPlayVideo = 1 << 0;
31
+ constexpr guint kPlayText = 1 << 2;
32
+
33
+ double secondsOf(gint64 time) {
34
+ return time > 0 ? static_cast<double>(time) / GST_SECOND : 0;
35
+ }
36
+
37
+ } // namespace
38
+
39
+ struct Playback::Shared {
40
+ std::mutex mutex;
41
+ // Held over each state change, from deciding it to making it, so two threads' changes land in
42
+ // the order they were decided, and the destructor's release waits for one in progress. Taken
43
+ // before `mutex`, never under it.
44
+ std::mutex changing;
45
+ GstElement *pipeline{nullptr};
46
+ std::function<void(Change)> changed;
47
+ bool alive{true};
48
+ bool file{false};
49
+ PlaybackState::Load load{PlaybackState::Load::Loading};
50
+ std::string error;
51
+ bool prerolled{false};
52
+ bool wantPlaying{false};
53
+ bool ended{false};
54
+ bool buffering{false};
55
+ // The rate the pipeline plays at, and the one asked for: a seek applies it.
56
+ double rate{1};
57
+ double appliedRate{1};
58
+ // A seek asked before the source prerolled, run once it has.
59
+ std::optional<double> pendingSeek;
60
+ bool seeking{false};
61
+ std::vector<std::function<void()>> seekDone;
62
+ GSource *bus{nullptr};
63
+ guint timer{0};
64
+ double interval{0};
65
+ // The state last set on the pipeline, so a request for the same one does nothing.
66
+ GstState target{GST_STATE_PAUSED};
67
+
68
+ // What the pipeline should be in, from what was asked and what it is doing. Under `mutex`.
69
+ GstState wanted() const {
70
+ return wantPlaying && !ended && !buffering && load != PlaybackState::Load::Failed ? GST_STATE_PLAYING : GST_STATE_PAUSED;
71
+ }
72
+ };
73
+
74
+ namespace {
75
+
76
+ using Shared = Playback::Shared;
77
+ using Change = Playback::Change;
78
+
79
+ void send(const std::shared_ptr<Shared> &shared, Change change) {
80
+ {
81
+ std::lock_guard lock(shared->mutex);
82
+ if (!shared->alive) {
83
+ return;
84
+ }
85
+ }
86
+ shared->changed(change);
87
+ }
88
+
89
+ void startTimer(const std::shared_ptr<Shared> &shared);
90
+
91
+ // Sets the pipeline to the state `wanted` gives, and runs the interval while it plays. Not under
92
+ // `mutex`.
93
+ void apply(const std::shared_ptr<Shared> &shared) {
94
+ std::lock_guard changing(shared->changing);
95
+ GstState next;
96
+ bool timer = false;
97
+ {
98
+ std::lock_guard lock(shared->mutex);
99
+ if (!shared->alive || !shared->pipeline) {
100
+ return;
101
+ }
102
+ next = shared->wanted();
103
+ if (next == shared->target) {
104
+ return;
105
+ }
106
+ shared->target = next;
107
+ timer = next == GST_STATE_PLAYING && shared->interval > 0 && shared->timer == 0;
108
+ if (next != GST_STATE_PLAYING && shared->timer != 0) {
109
+ g_source_remove(shared->timer);
110
+ shared->timer = 0;
111
+ }
112
+ }
113
+ gst_element_set_state(shared->pipeline, next);
114
+ if (timer) {
115
+ startTimer(shared);
116
+ }
117
+ }
118
+
119
+ void startTimer(const std::shared_ptr<Shared> &shared) {
120
+ std::lock_guard lock(shared->mutex);
121
+ if (!shared->alive || shared->timer != 0 || shared->interval <= 0) {
122
+ return;
123
+ }
124
+ auto *weak = new std::weak_ptr<Shared>(shared);
125
+ // At least a millisecond: a timeout of 0 would run on every turn of the loop.
126
+ guint every = static_cast<guint>(std::max(1.0, shared->interval * 1000));
127
+ shared->timer = g_timeout_add_full(G_PRIORITY_DEFAULT, every, [](gpointer data) -> gboolean {
128
+ auto shared = static_cast<std::weak_ptr<Shared> *>(data)->lock();
129
+ if (shared) {
130
+ send(shared, Change::Time);
131
+ }
132
+ return shared ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE;
133
+ }, weak, [](gpointer data) { delete static_cast<std::weak_ptr<Shared> *>(data); });
134
+ }
135
+
136
+ // A flushing, accurate seek at the rate asked for. Not under `mutex`.
137
+ bool seekTo(const std::shared_ptr<Shared> &shared, double seconds) {
138
+ std::lock_guard changing(shared->changing);
139
+ double rate;
140
+ {
141
+ std::lock_guard lock(shared->mutex);
142
+ if (!shared->alive || !shared->pipeline) {
143
+ return false;
144
+ }
145
+ // A rate of 0 pauses rather than seeks: the seek keeps the one the pipeline plays at.
146
+ rate = shared->rate > 0 ? shared->rate : shared->appliedRate;
147
+ shared->seeking = true;
148
+ }
149
+ gint64 at = static_cast<gint64>(std::max(0.0, seconds) * GST_SECOND);
150
+ bool ok = gst_element_seek(shared->pipeline, rate, GST_FORMAT_TIME, static_cast<GstSeekFlags>(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
151
+ GST_SEEK_TYPE_SET, at, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
152
+ std::lock_guard lock(shared->mutex);
153
+ if (!ok) {
154
+ // Set before the seek, since its ASYNC_DONE can come before this line runs.
155
+ shared->seeking = false;
156
+ } else {
157
+ // The rate is the pipeline's only once a seek carried it, and the end is behind it.
158
+ shared->appliedRate = rate;
159
+ shared->ended = false;
160
+ }
161
+ return ok;
162
+ }
163
+
164
+ void finishSeeks(const std::shared_ptr<Shared> &shared) {
165
+ std::vector<std::function<void()>> done;
166
+ {
167
+ std::lock_guard lock(shared->mutex);
168
+ done.swap(shared->seekDone);
169
+ }
170
+ for (auto &each : done) {
171
+ each();
172
+ }
173
+ }
174
+
175
+ void fail(const std::shared_ptr<Shared> &shared, std::string message) {
176
+ {
177
+ std::lock_guard lock(shared->mutex);
178
+ if (shared->load == PlaybackState::Load::Failed) {
179
+ return;
180
+ }
181
+ shared->load = PlaybackState::Load::Failed;
182
+ shared->error = std::move(message);
183
+ }
184
+ finishSeeks(shared);
185
+ apply(shared);
186
+ send(shared, Change::State);
187
+ }
188
+
189
+ gboolean onMessage(GstBus *, GstMessage *message, gpointer data) {
190
+ auto shared = *static_cast<std::shared_ptr<Shared> *>(data);
191
+ switch (GST_MESSAGE_TYPE(message)) {
192
+ case GST_MESSAGE_ERROR: {
193
+ GError *error = nullptr;
194
+ gst_message_parse_error(message, &error, nullptr);
195
+ fail(shared, error && error->message ? error->message : "The source did not play");
196
+ if (error) {
197
+ g_error_free(error);
198
+ }
199
+ break;
200
+ }
201
+ case GST_MESSAGE_ELEMENT:
202
+ if (gst_is_missing_plugin_message(message)) {
203
+ gchar *what = gst_missing_plugin_message_get_description(message);
204
+ fail(shared, std::string("No decoder is installed for ") + (what ? what : "this source") +
205
+ ": install the GStreamer plugin that plays it");
206
+ g_free(what);
207
+ }
208
+ break;
209
+ case GST_MESSAGE_ASYNC_DONE: {
210
+ std::optional<double> pending;
211
+ bool first = false;
212
+ bool seeked = false;
213
+ {
214
+ std::lock_guard lock(shared->mutex);
215
+ first = !shared->prerolled;
216
+ shared->prerolled = true;
217
+ if (shared->load == PlaybackState::Load::Loading) {
218
+ shared->load = PlaybackState::Load::Ready;
219
+ }
220
+ pending.swap(shared->pendingSeek);
221
+ if (!pending && shared->rate > 0 && shared->rate != shared->appliedRate) {
222
+ // A rate asked for before the source prerolled; a rate of 0 is a pause, never a seek.
223
+ pending = -1;
224
+ }
225
+ seeked = shared->seeking;
226
+ shared->seeking = false;
227
+ }
228
+ if (pending) {
229
+ gint64 now = 0;
230
+ double at = *pending >= 0 ? *pending : (gst_element_query_position(shared->pipeline, GST_FORMAT_TIME, &now) ? secondsOf(now) : 0);
231
+ if (seekTo(shared, at)) {
232
+ break;
233
+ }
234
+ }
235
+ if (seeked || first) {
236
+ finishSeeks(shared);
237
+ }
238
+ apply(shared);
239
+ send(shared, Change::State);
240
+ break;
241
+ }
242
+ case GST_MESSAGE_EOS: {
243
+ {
244
+ std::lock_guard lock(shared->mutex);
245
+ shared->ended = true;
246
+ }
247
+ apply(shared);
248
+ send(shared, Change::Ended);
249
+ break;
250
+ }
251
+ case GST_MESSAGE_BUFFERING: {
252
+ gint percent = 100;
253
+ gst_message_parse_buffering(message, &percent);
254
+ bool changed;
255
+ {
256
+ std::lock_guard lock(shared->mutex);
257
+ bool buffering = percent < 100;
258
+ changed = buffering != shared->buffering;
259
+ shared->buffering = buffering;
260
+ }
261
+ if (changed) {
262
+ apply(shared);
263
+ send(shared, Change::State);
264
+ }
265
+ break;
266
+ }
267
+ case GST_MESSAGE_STATE_CHANGED:
268
+ if (GST_MESSAGE_SRC(message) == GST_OBJECT(shared->pipeline)) {
269
+ GstState old, now;
270
+ gst_message_parse_state_changed(message, &old, &now, nullptr);
271
+ if (old == GST_STATE_PLAYING || now == GST_STATE_PLAYING) {
272
+ send(shared, Change::State);
273
+ }
274
+ }
275
+ break;
276
+ case GST_MESSAGE_DURATION_CHANGED: send(shared, Change::State); break;
277
+ default: break;
278
+ }
279
+ return G_SOURCE_CONTINUE;
280
+ }
281
+
282
+ } // namespace
283
+
284
+ Playback::Playback(const std::string &uri, Options options, std::function<void(Change)> changed)
285
+ : shared_(std::make_shared<Shared>()) {
286
+ shared_->changed = std::move(changed);
287
+ shared_->interval = options.interval;
288
+ shared_->file = uri.rfind("file:", 0) == 0;
289
+ if (options.video && !options.videoSink) {
290
+ shared_->load = PlaybackState::Load::Failed;
291
+ shared_->error = "GStreamer's appsink is not installed";
292
+ return;
293
+ }
294
+ GstElement *pipeline = gst_element_factory_make("playbin3", nullptr);
295
+ if (!pipeline) {
296
+ shared_->load = PlaybackState::Load::Failed;
297
+ shared_->error = "GStreamer's playbin3 is not installed";
298
+ if (options.videoSink) {
299
+ gst_object_unref(gst_object_ref_sink(options.videoSink));
300
+ }
301
+ return;
302
+ }
303
+ shared_->pipeline = GST_ELEMENT(gst_object_ref_sink(pipeline));
304
+ guint flags = 0;
305
+ g_object_get(pipeline, "flags", &flags, nullptr);
306
+ flags &= ~kPlayText;
307
+ if (!options.videoSink) {
308
+ flags &= ~kPlayVideo;
309
+ }
310
+ g_object_set(pipeline, "uri", uri.c_str(), "flags", flags, nullptr);
311
+ if (options.videoSink) {
312
+ g_object_set(pipeline, "video-sink", options.videoSink, nullptr);
313
+ }
314
+ if (!options.audioSink.empty()) {
315
+ if (GstElement *sink = gst_element_factory_make(options.audioSink.c_str(), nullptr)) {
316
+ if (g_object_class_find_property(G_OBJECT_GET_CLASS(sink), "sync")) {
317
+ g_object_set(sink, "sync", TRUE, nullptr);
318
+ }
319
+ g_object_set(pipeline, "audio-sink", sink, nullptr);
320
+ }
321
+ }
322
+ if (options.preservePitch) {
323
+ if (GstElement *tempo = gst_element_factory_make("scaletempo", nullptr)) {
324
+ g_object_set(pipeline, "audio-filter", tempo, nullptr);
325
+ }
326
+ }
327
+ GstBus *bus = gst_element_get_bus(pipeline);
328
+ shared_->bus = gst_bus_create_watch(bus);
329
+ g_source_set_callback(shared_->bus, reinterpret_cast<GSourceFunc>(+onMessage), new std::shared_ptr<Shared>(shared_),
330
+ [](gpointer data) { delete static_cast<std::shared_ptr<Shared> *>(data); });
331
+ // The default context, the GTK main loop's, whichever thread makes the player.
332
+ g_source_attach(shared_->bus, nullptr);
333
+ gst_object_unref(bus);
334
+ if (gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
335
+ shared_->load = PlaybackState::Load::Failed;
336
+ shared_->error = "The source could not be opened";
337
+ }
338
+ }
339
+
340
+ Playback::~Playback() {
341
+ GSource *bus;
342
+ guint timer;
343
+ {
344
+ std::lock_guard lock(shared_->mutex);
345
+ shared_->alive = false;
346
+ bus = std::exchange(shared_->bus, nullptr);
347
+ timer = std::exchange(shared_->timer, 0);
348
+ }
349
+ if (bus) {
350
+ // Drops the watch's hold on `shared_` once a dispatch running now returns.
351
+ g_source_destroy(bus);
352
+ g_source_unref(bus);
353
+ }
354
+ if (timer) {
355
+ g_source_remove(timer);
356
+ }
357
+ if (shared_->pipeline) {
358
+ // After any state change in progress on another thread, which `alive` stops the next of.
359
+ std::lock_guard changing(shared_->changing);
360
+ gst_element_set_state(shared_->pipeline, GST_STATE_NULL);
361
+ gst_object_unref(std::exchange(shared_->pipeline, nullptr));
362
+ }
363
+ finishSeeks(shared_);
364
+ }
365
+
366
+ PlaybackState Playback::state() {
367
+ PlaybackState out;
368
+ bool file;
369
+ {
370
+ std::lock_guard lock(shared_->mutex);
371
+ out.load = shared_->load;
372
+ out.error = shared_->error;
373
+ out.ended = shared_->ended;
374
+ out.buffering = shared_->buffering && shared_->wantPlaying;
375
+ out.playing = shared_->wantPlaying && !shared_->ended && shared_->load != PlaybackState::Load::Failed;
376
+ file = shared_->file;
377
+ }
378
+ if (!shared_->pipeline) {
379
+ return out;
380
+ }
381
+ gint64 duration = 0, position = 0;
382
+ if (gst_element_query_duration(shared_->pipeline, GST_FORMAT_TIME, &duration)) {
383
+ out.duration = secondsOf(duration);
384
+ }
385
+ if (out.ended && out.duration > 0) {
386
+ out.currentTime = out.duration;
387
+ } else if (gst_element_query_position(shared_->pipeline, GST_FORMAT_TIME, &position)) {
388
+ out.currentTime = secondsOf(position);
389
+ }
390
+ out.bufferedPosition = file ? out.duration : out.currentTime;
391
+ GstQuery *query = gst_query_new_buffering(GST_FORMAT_TIME);
392
+ if (!file && gst_element_query(shared_->pipeline, query)) {
393
+ gint64 start = 0, stop = 0;
394
+ gst_query_parse_buffering_range(query, nullptr, &start, &stop, nullptr);
395
+ if (stop > 0) {
396
+ out.bufferedPosition = secondsOf(stop);
397
+ }
398
+ }
399
+ gst_query_unref(query);
400
+ return out;
401
+ }
402
+
403
+ void Playback::play(double rate) {
404
+ if (rate <= 0) {
405
+ pause();
406
+ return;
407
+ }
408
+ bool reseek;
409
+ {
410
+ std::lock_guard lock(shared_->mutex);
411
+ shared_->wantPlaying = true;
412
+ shared_->rate = rate;
413
+ reseek = shared_->prerolled && rate != shared_->appliedRate && shared_->load != PlaybackState::Load::Failed;
414
+ }
415
+ if (reseek) {
416
+ gint64 now = 0;
417
+ seekTo(shared_, gst_element_query_position(shared_->pipeline, GST_FORMAT_TIME, &now) ? secondsOf(now) : 0);
418
+ }
419
+ apply(shared_);
420
+ }
421
+
422
+ void Playback::pause() {
423
+ {
424
+ std::lock_guard lock(shared_->mutex);
425
+ shared_->wantPlaying = false;
426
+ }
427
+ apply(shared_);
428
+ }
429
+
430
+ void Playback::seek(double seconds, std::function<void()> done) {
431
+ bool playable;
432
+ {
433
+ std::lock_guard lock(shared_->mutex);
434
+ playable = shared_->pipeline && shared_->load != PlaybackState::Load::Failed;
435
+ if (playable) {
436
+ if (done) {
437
+ shared_->seekDone.push_back(std::move(done));
438
+ }
439
+ if (!shared_->prerolled) {
440
+ shared_->pendingSeek = seconds;
441
+ return;
442
+ }
443
+ }
444
+ }
445
+ if (!playable) {
446
+ if (done) {
447
+ done();
448
+ }
449
+ return;
450
+ }
451
+ if (!seekTo(shared_, seconds)) {
452
+ finishSeeks(shared_);
453
+ }
454
+ }
455
+
456
+ void Playback::setRate(double rate) {
457
+ bool playing;
458
+ {
459
+ std::lock_guard lock(shared_->mutex);
460
+ shared_->rate = rate;
461
+ playing = shared_->wantPlaying;
462
+ }
463
+ if (playing) {
464
+ // A rate of 0 or less pauses, as play's does.
465
+ play(rate);
466
+ }
467
+ }
468
+
469
+ void Playback::setVolume(double volume) {
470
+ if (shared_->pipeline) {
471
+ g_object_set(shared_->pipeline, "volume", std::clamp(volume, 0.0, 1.0), nullptr);
472
+ }
473
+ }
474
+
475
+ void Playback::setMuted(bool muted) {
476
+ if (shared_->pipeline) {
477
+ g_object_set(shared_->pipeline, "mute", muted ? TRUE : FALSE, nullptr);
478
+ }
479
+ }
480
+
481
+ void Playback::setInterval(double seconds) {
482
+ {
483
+ std::lock_guard lock(shared_->mutex);
484
+ shared_->interval = seconds;
485
+ if (shared_->timer != 0) {
486
+ g_source_remove(shared_->timer);
487
+ shared_->timer = 0;
488
+ }
489
+ if (seconds <= 0 || shared_->target != GST_STATE_PLAYING) {
490
+ return;
491
+ }
492
+ }
493
+ startTimer(shared_);
494
+ }
495
+
496
+ } // namespace ngn::gtk