File: RiceGioBackend.cpp

package info (click to toggle)
webkit2gtk 2.51.91-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 484,084 kB
  • sloc: cpp: 3,931,583; javascript: 197,713; ansic: 167,619; python: 53,160; asm: 21,857; ruby: 18,114; perl: 17,149; xml: 4,631; sh: 2,462; yacc: 2,394; java: 2,032; lex: 1,358; pascal: 372; makefile: 215
file content (182 lines) | stat: -rw-r--r-- 6,532 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
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
/*
 *  Copyright (C) 2025 Igalia S.L. All rights reserved.
 *  Copyright (C) 2025 Metrological Group B.V.
 *  Copyright (C) 2024 Matthew Waters <matthew@centricular.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "RiceGioBackend.h"

#if USE(GSTREAMER_WEBRTC) && USE(LIBRICE)

#include "RiceUtilities.h"
#include <gst/gst.h>
#include <rice-proto.h>
#include <wtf/MonotonicTime.h>
#include <wtf/Seconds.h>
#include <wtf/StdLibExtras.h>
#include <wtf/glib/RunLoopSourcePriority.h>

#define GST_CAT_DEFAULT gst_webrtc_rice_gio_debug
GST_DEBUG_CATEGORY_STATIC(GST_CAT_DEFAULT);

using namespace::WebCore;

struct _AgentSource {
    GSource source;

    GThreadSafeWeakPtr<WebKitGstIceAgent> agent;
    bool complete;
};

static gboolean agentSourcePrepare(GSource* base, gint* timeout)
{
    auto source = reinterpret_cast<AgentSource*>(base);
    auto iceAgent = source->agent.get();
    if (!iceAgent)
        return FALSE;

    const auto& agent = webkitGstWebRTCIceAgentGetRiceAgent(iceAgent.get());
    auto now = WTF::MonotonicTime::now().secondsSinceEpoch();

    gboolean result = FALSE;
    while (true) {
        RiceAgentPoll ret;
        rice_agent_poll_init(&ret);
        GST_TRACE_OBJECT(iceAgent.get(), "Polling");
        rice_agent_poll(agent.get(), now.nanoseconds(), &ret);
        GST_TRACE_OBJECT(iceAgent.get(), "Polling DONE");
        switch (ret.tag) {
        case RICE_AGENT_POLL_CLOSED:
            GST_TRACE_OBJECT(iceAgent.get(), "Agent closed!");
            source->complete = true;
            rice_agent_poll_clear(&ret);
            webkitGstWebRTCIceAgentClosed(iceAgent.get());
            return TRUE;
        case RICE_AGENT_POLL_COMPONENT_STATE_CHANGE:
            GST_TRACE_OBJECT(iceAgent.get(), "Component state changed");
            webkitGstWebRTCIceAgentComponentStateChangedForStream(iceAgent.get(), ret.component_state_change.stream_id, ret.component_state_change);
            result = TRUE;
            break;
        case RICE_AGENT_POLL_ALLOCATE_SOCKET:
            GST_FIXME("allocate socket is not handled");
            result = TRUE;
            break;
        case RICE_AGENT_POLL_REMOVE_SOCKET:
            GST_FIXME("remove socket is not handled");
            result = TRUE;
            break;
        case RICE_AGENT_POLL_WAIT_UNTIL_NANOS: {
            auto delta = Seconds::fromNanoseconds(ret.wait_until_nanos - now.nanoseconds());
            if (delta >= 99998_s) {
                GST_TRACE_OBJECT(iceAgent.get(), "Nothing special to do.");
                result = FALSE;
                break;
            }
            if (timeout) {
                *timeout = static_cast<int>(delta.milliseconds());
                GST_TRACE_OBJECT(iceAgent.get(), "Waiting for %d ms", *timeout);
            }
            result = FALSE;
            break;
        }
        case RICE_AGENT_POLL_GATHERING_COMPLETE:
            GST_TRACE_OBJECT(iceAgent.get(), "Gathering complete");
            webkitGstWebRTCIceAgentGatheringDoneForStream(iceAgent.get(), ret.gathering_complete.stream_id);
            break;
        case RICE_AGENT_POLL_GATHERED_CANDIDATE:
            GST_TRACE_OBJECT(iceAgent.get(), "Gathered candidate");
            webkitGstWebRTCIceAgentLocalCandidateGatheredForStream(iceAgent.get(), ret.gathered_candidate.stream_id, ret.gathered_candidate);
            result = TRUE;
            break;
        case RICE_AGENT_POLL_SELECTED_PAIR:
            GST_TRACE_OBJECT(iceAgent.get(), "New selected pair");
            webkitGstWebRTCIceAgentNewSelectedPairForStream(iceAgent.get(), ret.selected_pair.stream_id, ret.selected_pair);
            result = TRUE;
            break;
        };
        rice_agent_poll_clear(&ret);

        RiceTransmit transmit;
        rice_transmit_init(&transmit);
        rice_agent_poll_transmit(agent.get(), now.nanoseconds(), &transmit);
        if (transmit.from && transmit.to) {
            auto from = riceAddressToString(transmit.from);
            auto to = riceAddressToString(transmit.to);
            auto protocol = riceTransmitTransportToIceProtocol(transmit);
            if (auto handle = riceTransmitToSharedMemoryHandle(&transmit)) {
                webkitGstWebRTCIceAgentSend(iceAgent.get(), transmit.stream_id, protocol, WTF::move(from), WTF::move(to), WTF::move(*handle));
                result = TRUE;
            }
        } else
            rice_transmit_clear(&transmit);
        if (!result)
            break;
    }

    return result;
}

static gboolean agentSourceCheck(GSource*)
{
    return TRUE;
}

static gboolean agentSourceDispatch(GSource* base, GSourceFunc callback, gpointer data)
{
    auto source = reinterpret_cast<AgentSource*>(base);

    if (callback)
        callback(data);

    return !source->complete;
}

static void agentSourceFinalize(GSource*)
{
}

static GSourceFuncs agentEventSourceFuncs = {
    agentSourcePrepare,
    agentSourceCheck,
    agentSourceDispatch,
    agentSourceFinalize,
    nullptr, nullptr
};

GRefPtr<GSource> agentSourceNew(GThreadSafeWeakPtr<WebKitGstIceAgent>&& agent)
{
    static std::once_flag debugRegisteredFlag;
    std::call_once(debugRegisteredFlag, [] {
        GST_DEBUG_CATEGORY_INIT(GST_CAT_DEFAULT, "webkitwebrtcricegio", 0, "webkitwebrtcricegio");
    });

    auto source = adoptGRef(g_source_new(&agentEventSourceFuncs, sizeof(AgentSource)));
    g_source_set_priority(source.get(), RunLoopSourcePriority::AsyncIONetwork);
    g_source_set_name(source.get(), "[WebKit] ICE Agent loop");

    auto agentSource = reinterpret_cast<AgentSource*>(source.get());
    if (auto iceAgent = agent.get()) [[likely]]
        agentSource->agent.reset(iceAgent.get());
    agentSource->complete = false;

    return source;
}

#undef GST_CAT_DEFAULT

#endif // USE(GSTREAMER_WEBRTC) && USE(LIBRICE)