File: webrtc_window_system.cpp

package info (click to toggle)
open3d 0.16.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: cpp: 193,088; python: 24,973; ansic: 8,356; javascript: 1,869; sh: 1,473; makefile: 236; xml: 69
file content (121 lines) | stat: -rw-r--r-- 5,454 bytes parent folder | download
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
// ----------------------------------------------------------------------------
// -                        Open3D: www.open3d.org                            -
// ----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2018-2021 www.open3d.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------

#include "pybind/visualization/webrtc_server/webrtc_window_system.h"

#include "open3d/utility/Logging.h"
#include "open3d/visualization/webrtc_server/WebRTCWindowSystem.h"
#include "pybind/docstring.h"

namespace open3d {
namespace visualization {
namespace webrtc_server {

static void pybind_webrtc_server_functions(py::module &m) {
    m.def(
            "call_http_api",
            [](const std::string &entry_point, const std::string &query_string,
               const std::string &data) {
                return WebRTCWindowSystem::GetInstance()->CallHttpAPI(
                        entry_point, query_string, data);
            },
            "entry_point"_a, "query_string"_a = "", "data"_a = "",
            "Emulates Open3D WebRTCWindowSystem's HTTP API calls. This is used "
            "when the HTTP handshake server is disabled (e.g. in Jupyter), and "
            "handshakes are done by this function.");
    m.def(
            "enable_webrtc",
            []() { WebRTCWindowSystem::GetInstance()->EnableWebRTC(); },
            "Use WebRTC streams to display rendered gui window.");
    m.def(
            "disable_http_handshake",
            []() { WebRTCWindowSystem::GetInstance()->DisableHttpHandshake(); },
            "Disables the HTTP handshake server. In Jupyter environment, "
            "WebRTC handshake is performed by call_http_api() with "
            "Jupyter's own COMMS interface, thus the HTTP server shall "
            "be turned off.");
    m.def(
            "register_data_channel_message_callback",
            [](const std::string &class_name,
               std::function<std::string(const std::string &)> callback) {
                return WebRTCWindowSystem::GetInstance()
                        ->RegisterDataChannelMessageCallback(class_name,
                                                             callback);
            },
            "class_name"_a, "callback"_a,
            R"(
Register callback for a data channel message.

When the data channel receives a valid JSON string, the ``class_name`` property
of the JSON object will be examined and the corresponding callback function will
be called. The string return value of the callback will be sent back as a reply,
if it is not empty.

.. note:: Ordering between the message and the reply is not guaranteed, since
   some messages may take longer to process than others. If ordering is important,
   use a unique message id for every message and include it in the reply.

.. code:: python

    # Register callback in Python
    import open3d as o3d
    o3d.visualization.webrtc_server.enable_webrtc()
    def send_ack(data):
        print(data)
        return "Received WebRTC data channel message with data: " + data

    o3d.visualization.webrtc_server.register_data_channel_message_callback(
        "webapp/input", send_ack)

.. code:: js

    /* Send message in JavaScript to trigger callback. this is WebRTCStreamer object */
    this.dataChannel.send('{"class_name":"webapp/input", "data":"Test event"}')
            )");

    docstring::FunctionDocInject(
            m, "register_data_channel_message_callback",
            {{"class_name",
              "The value of of the ``class_name`` property of the JSON "
              "object."},
             {"callback",
              "The callback function that will be called when a JSON object "
              "with the matching ``class_name`` is received via the data "
              "channel. The function should accept a ``string`` argument "
              "(corresponding to the event data, such as form data or updated "
              "value of a slider) and return a ``string``."}});
}

void pybind_webrtc_server(py::module &m) {
    py::module m_submodule = m.def_submodule(
            "webrtc_server",
            "Functionality for remote visualization over WebRTC.");
    pybind_webrtc_server_functions(m_submodule);
}

}  // namespace webrtc_server
}  // namespace visualization
}  // namespace open3d