ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
webrtc_window_system.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include "pybind/docstring.h"
12 
13 namespace cloudViewer {
14 namespace visualization {
15 namespace webrtc_server {
16 
17 static void pybind_webrtc_server_functions(py::module &m) {
18  m.def(
19  "call_http_api",
20  [](const std::string &entry_point, const std::string &query_string,
21  const std::string &data) {
22  return WebRTCWindowSystem::GetInstance()->CallHttpAPI(
23  entry_point, query_string, data);
24  },
25  "entry_point"_a, "query_string"_a = "", "data"_a = "",
26  "Emulates CloudViewer WebRTCWindowSystem's HTTP API calls. This is "
27  "used "
28  "when the HTTP handshake server is disabled (e.g. in Jupyter), and "
29  "handshakes are done by this function.");
30  m.def(
31  "enable_webrtc",
32  []() { WebRTCWindowSystem::GetInstance()->EnableWebRTC(); },
33  "Use WebRTC streams to display rendered gui window.");
34  m.def(
35  "disable_http_handshake",
36  []() { WebRTCWindowSystem::GetInstance()->DisableHttpHandshake(); },
37  "Disables the HTTP handshake server. In Jupyter environemnt, "
38  "WebRTC handshake is performed by call_http_api() with "
39  "Jupyter's own COMMS interface, thus the HTTP server shall "
40  "be turned off.");
41  m.def(
42  "register_data_channel_message_callback",
43  [](const std::string &class_name,
44  std::function<std::string(const std::string &)> callback) {
46  ->RegisterDataChannelMessageCallback(class_name,
47  callback);
48  },
49  "class_name"_a, "callback"_a,
50  R"(
51 Register callback for a data channel message.
52 
53 When the data channel receives a valid JSON string, the ``class_name`` property
54 of the JSON object will be examined and the corresponding callback function will
55 be called. The string return value of the callback will be sent back as a reply,
56 if it is not empty.
57 
58 .. note:: Ordering between the message and the reply is not guaranteed, since
59 some messages may take longer to process than others. If ordering is important,
60 use a unique message id for every message and include it in the reply.
61 
62 .. code:: python
63 
64  # Register callback in Python
65  import cloudViewer as cv3d
66  cv3d.visualization.webrtc_server.enable_webrtc()
67  def send_ack(data):
68  print(data)
69  return "Received WebRTC data channel message with data: " + data
70 
71  cv3d.visualization.webrtc_server.register_data_channel_message_callback(
72  "webapp/input", send_ack)
73 
74 .. code:: js
75 
76  /* Send message in JavaScript to trigger callback. this is WebRTCStreamer object */
77  this.dataChannel.send('{"class_name":"webapp/input", "data":"Test event"}')
78  )");
79 
81  m, "register_data_channel_message_callback",
82  {{"class_name",
83  "The value of of the ``class_name`` property of the JSON "
84  "object."},
85  {"callback",
86  "The callback function that will be called when a JSON object "
87  "with the matching ``class_name`` is received via the data "
88  "channel. The function should accept a string argument "
89  "(corresponding to the event data, such as form data or updated "
90  "value of a slider) and not return anything."}});
91 }
92 
93 void pybind_webrtc_server(py::module &m) {
94  py::module m_submodule = m.def_submodule("webrtc_server");
95  pybind_webrtc_server_functions(m_submodule);
96 }
97 
98 } // namespace webrtc_server
99 } // namespace visualization
100 } // namespace cloudViewer
std::function< void(std::shared_ptr< core::Tensor >)> callback
static std::shared_ptr< WebRTCWindowSystem > GetInstance()
void FunctionDocInject(py::module &pybind_module, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:76
static void pybind_webrtc_server_functions(py::module &m)
Generic file read and write utility for python interface.