File: GStreamerIceStream.cpp

package info (click to toggle)
webkit2gtk 2.51.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 477,912 kB
  • sloc: cpp: 3,898,343; javascript: 198,215; ansic: 165,229; python: 50,371; asm: 21,819; ruby: 18,095; perl: 16,953; xml: 4,623; sh: 2,398; yacc: 2,356; java: 2,019; lex: 1,358; pascal: 372; makefile: 197
file content (319 lines) | stat: -rw-r--r-- 13,507 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
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
/*
 *  Copyright (C) 2025 Igalia S.L. All rights reserved.
 *  Copyright (C) 2025 Metrological Group B.V.
 *
 *  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 "GStreamerIceStream.h"

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

#include "GRefPtrGStreamer.h"
#include "GRefPtrRice.h"
#include "GUniquePtrRice.h"
#include "RTCIceComponent.h"
#include "RiceUtilities.h"
#include "SharedBuffer.h"
#include <gst/webrtc/ice.h>
#include <gst/webrtc/webrtc.h>
#include <wtf/MonotonicTime.h>
#include <wtf/glib/GThreadSafeWeakPtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/WTFString.h>

using namespace WTF;
using namespace WebCore;

typedef struct _WebKitGstIceStreamPrivate {
    GThreadSafeWeakPtr<WebKitGstIceAgent> agent;
    GRefPtr<RiceStream> riceStream;
    GRefPtr<GstWebRTCICETransport> rtpTransport;
    GRefPtr<GstWebRTCICETransport> rtcpTransport;
    bool haveLocalCredentials { false };
    bool haveRemoteCredentials { false };
    bool gatheringRequested { false };
    bool gatheringStarted { false };
} WebKitGstIceStreamPrivate;

typedef struct _WebKitGstIceStream {
    GstWebRTCICEStream parent;
    WebKitGstIceStreamPrivate* priv;
} WebKitGstIceStream;

typedef struct _WebKitGstIceStreamClass {
    GstWebRTCICEStreamClass parentClass;
} WebKitGstIceStreamClass;

GST_DEBUG_CATEGORY(webkit_webrtc_ice_stream_debug);
#define GST_CAT_DEFAULT webkit_webrtc_ice_stream_debug

WEBKIT_DEFINE_TYPE_WITH_CODE(WebKitGstIceStream, webkit_gst_webrtc_ice_stream, GST_TYPE_WEBRTC_ICE_STREAM, GST_DEBUG_CATEGORY_INIT(webkit_webrtc_ice_stream_debug, "webkitwebrtcricestream", 0, "WebRTC ICE stream"))

GstWebRTCICETransport* webkitGstWebRTCIceStreamFindTransport(GstWebRTCICEStream* ice, GstWebRTCICEComponent component)
{
    auto stream = WEBKIT_GST_WEBRTC_ICE_STREAM(ice);
    auto agent = stream->priv->agent.get();
    if (!agent)
        return nullptr;

    switch (component) {
    case GST_WEBRTC_ICE_COMPONENT_RTP:
        if (!stream->priv->rtpTransport)
            stream->priv->rtpTransport = adoptGRef(GST_WEBRTC_ICE_TRANSPORT(webkitGstWebRTCIceAgentCreateTransport(agent.get(), GThreadSafeWeakPtr(stream), RTCIceComponent::Rtp)));
        return stream->priv->rtpTransport.ref();
    case GST_WEBRTC_ICE_COMPONENT_RTCP:
        if (!stream->priv->rtcpTransport)
            stream->priv->rtcpTransport = adoptGRef(GST_WEBRTC_ICE_TRANSPORT(webkitGstWebRTCIceAgentCreateTransport(agent.get(), GThreadSafeWeakPtr(stream), RTCIceComponent::Rtcp)));
        return stream->priv->rtcpTransport.ref();
    }

    ASSERT_NOT_REACHED();
    return nullptr;
}

void webkitGstWebRTCIceStreamGatheringDone(const WebKitGstIceStream* ice)
{
    auto stream = WEBKIT_GST_WEBRTC_ICE_STREAM(ice);
    if (stream->priv->rtpTransport)
        gst_webrtc_ice_transport_gathering_state_change(GST_WEBRTC_ICE_TRANSPORT(stream->priv->rtpTransport.get()), GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE);
    if (stream->priv->rtcpTransport)
        gst_webrtc_ice_transport_gathering_state_change(GST_WEBRTC_ICE_TRANSPORT(stream->priv->rtcpTransport.get()), GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE);
}

void webkitGstWebRTCIceStreamAddLocalGatheredCandidate(const WebKitGstIceStream* ice, RiceGatheredCandidate& candidate)
{
    auto stream = WEBKIT_GST_WEBRTC_ICE_STREAM(ice);
    rice_stream_add_local_gathered_candidate(stream->priv->riceStream.get(), &candidate);
}

void webkitGstWebRTCIceStreamNewSelectedPair(const WebKitGstIceStream* ice, RiceAgentSelectedPair& pair)
{
    auto stream = WEBKIT_GST_WEBRTC_ICE_STREAM(ice);
    if (!stream->priv->rtpTransport) [[unlikely]]
        return;
    webkitGstWebRTCIceTransportNewSelectedPair(WEBKIT_GST_WEBRTC_ICE_TRANSPORT(stream->priv->rtpTransport.get()), pair);
}

void webkitGstWebRTCIceStreamComponentStateChanged(const WebKitGstIceStream* ice, RiceAgentComponentStateChange& change)
{
    auto stream = WEBKIT_GST_WEBRTC_ICE_STREAM(ice);
    if (!stream->priv->rtpTransport) [[unlikely]]
        return;

    GstWebRTCICEConnectionState gstState;
    switch (change.state) {
    case RICE_COMPONENT_CONNECTION_STATE_NEW:
        gstState = GST_WEBRTC_ICE_CONNECTION_STATE_NEW;
        break;
    case RICE_COMPONENT_CONNECTION_STATE_CONNECTING:
        gstState = GST_WEBRTC_ICE_CONNECTION_STATE_CHECKING;
        break;
    case RICE_COMPONENT_CONNECTION_STATE_CONNECTED:
        gstState = GST_WEBRTC_ICE_CONNECTION_STATE_CONNECTED;
        break;
    case RICE_COMPONENT_CONNECTION_STATE_FAILED:
        gstState = GST_WEBRTC_ICE_CONNECTION_STATE_FAILED;
        break;
    }

    gst_webrtc_ice_transport_connection_state_change(stream->priv->rtpTransport.get(), gstState);
}

static gboolean webkitGstWebRTCIceStreamGatherCandidates(GstWebRTCICEStream* ice)
{
    auto stream = WEBKIT_GST_WEBRTC_ICE_STREAM(ice);

    if (stream->priv->rtpTransport)
        gst_webrtc_ice_transport_gathering_state_change(stream->priv->rtpTransport.get(), GST_WEBRTC_ICE_GATHERING_STATE_GATHERING);

    if (stream->priv->rtcpTransport)
        gst_webrtc_ice_transport_gathering_state_change(stream->priv->rtcpTransport.get(), GST_WEBRTC_ICE_GATHERING_STATE_GATHERING);

    auto agent = stream->priv->agent.get();
    if (!agent)
        return FALSE;

    auto addresses = webkitGstWebRTCIceAgentGatherSocketAddresses(agent.get(), ice->stream_id);
    auto component = adoptGRef(rice_stream_get_component(stream->priv->riceStream.get(), 1));

    Vector<GUniquePtr<RiceAddress>> riceAddresses;
    Vector<RiceTransportType> riceTransports;
    for (const auto& address : addresses) {
        GUniquePtr<RiceAddress> addr(rice_address_new_from_string(address.ascii().data()));
        if (!addr) [[unlikely]]
            continue;

        riceAddresses.append(WTFMove(addr));
        riceTransports.append(RICE_TRANSPORT_TYPE_UDP);
        riceTransports.append(RICE_TRANSPORT_TYPE_TCP);
    }
    Vector<const RiceAddress*> riceAddressValues;
    for (const auto& addr : riceAddresses)
        riceAddressValues.append(addr.get());
    auto addressDataStorage = riceAddressValues.span();
    auto riceTransportStorage = riceTransports.span();

    Vector<GUniquePtr<RiceAddress>> turnAddresses;
    auto turnConfigs = webkitGstWebRTCIceAgentGetTurnConfigs(agent.get());
    for (const auto& config : turnConfigs) {
        GUniquePtr<RiceAddress> address(rice_turn_config_get_addr(config.get()));
        turnAddresses.append(WTFMove(address));
    }

    Vector<const RiceAddress*> turnAddressValues;
    for (const auto& addr : turnAddresses)
        turnAddressValues.append(addr.get());
    auto turnAddressDataStorage = turnAddressValues.span();

    Vector<RiceTurnConfig*> turnConfigValues;
    for (const auto& config : turnConfigs)
        turnConfigValues.append(config.get());
    auto turnConfigDataStorage = turnConfigValues.span();

    auto error = rice_component_gather_candidates(component.get(), riceAddressValues.size(), addressDataStorage.data(), riceTransportStorage.data(), turnConfigs.size(), turnAddressDataStorage.data(), turnConfigDataStorage.data());
    webkitGstWebRTCIceAgentWakeup(agent.get());
    return (error == RICE_ERROR_SUCCESS || error == RICE_ERROR_ALREADY_IN_PROGRESS);
}

bool webkitGstWebRTCIceStreamGatherCandidates(WebKitGstIceStream* stream)
{
    return webkitGstWebRTCIceStreamGatherCandidates(GST_WEBRTC_ICE_STREAM(stream));
}

void webkitGstWebRTCIceStreamHandleIncomingData(const WebKitGstIceStream* stream, WebCore::RTCIceProtocol protocol, String&& from, String&& to, SharedMemory::Handle&& handle)
{
    RiceTransportType transport;
    switch (protocol) {
    case WebCore::RTCIceProtocol::Tcp:
        transport = RICE_TRANSPORT_TYPE_TCP;
        break;
    case WebCore::RTCIceProtocol::Udp:
        transport = RICE_TRANSPORT_TYPE_UDP;
        break;
    };
    auto riceFrom = riceAddressFromString(from);
    auto riceTo = riceAddressFromString(to);

    auto now = WTF::MonotonicTime::now().secondsSinceEpoch();
    GST_TRACE_OBJECT(stream, "Received %zu bytes", handle.size());
    RiceStreamIncomingData result;

    auto sharedMemory = SharedMemory::map(WTFMove(handle), SharedMemory::Protection::ReadOnly);
    if (!sharedMemory)
        return;

    auto buffer = sharedMemory->createSharedBuffer(sharedMemory->size());

    // We do rtcp muxing into rtp, so the component ID is always 1.
    size_t componentId = 1;
    rice_stream_handle_incoming_data(stream->priv->riceStream.get(), componentId, transport, riceFrom.get(),
        riceTo.get(), buffer->span().data(), buffer->size(), now.nanoseconds(), &result);

    if (result.handled) {
        auto agent = stream->priv->agent.get();
        // May result in either the gather or conncheck sources making further progress.
        if (agent) [[likely]]
            webkitGstWebRTCIceAgentWakeup(agent.get());
    }

    // Forward any non-STUN data to the pipeline for handling.
    if (result.data.size > 0 && result.data.ptr) {
        auto buffer = adoptGRef(gst_buffer_new_memdup(result.data.ptr, result.data.size));
        webkitGstWebRTCIceTransportHandleIncomingData(WEBKIT_GST_WEBRTC_ICE_TRANSPORT(stream->priv->rtpTransport.get()), WTFMove(buffer));
    }

    gsize dataSize;
    auto recvData = rice_stream_poll_recv(stream->priv->riceStream.get(), &componentId, &dataSize);
    while (recvData) {
        auto transport = webkitGstWebRTCIceStreamFindTransport(GST_WEBRTC_ICE_STREAM(stream), (GstWebRTCICEComponent)componentId);
        if (transport) [[likely]] {
            auto buffer = adoptGRef(gst_buffer_new_wrapped_full(static_cast<GstMemoryFlags>(0), recvData, dataSize, 0, dataSize,
                recvData, reinterpret_cast<GDestroyNotify>(rice_free_data)));
            webkitGstWebRTCIceTransportHandleIncomingData(WEBKIT_GST_WEBRTC_ICE_TRANSPORT(transport), WTFMove(buffer));
        }
        recvData = rice_stream_poll_recv(stream->priv->riceStream.get(), &componentId, &dataSize);
    }
}

const GRefPtr<RiceStream>& webkitGstWebRTCIceStreamGetRiceStream(WebKitGstIceStream* stream)
{
    return stream->priv->riceStream;
}

void webkitGstWebRTCIceStreamSetLocalCredentials(WebKitGstIceStream* stream, const String& ufrag, const String& pwd)
{
    GUniquePtr<RiceCredentials> credentials(rice_credentials_new(ufrag.ascii().data(), pwd.ascii().data()));
    rice_stream_set_local_credentials(stream->priv->riceStream.get(), credentials.get());

    stream->priv->haveLocalCredentials = true;
    if (stream->priv->haveRemoteCredentials && stream->priv->gatheringRequested)
        webkitGstWebRTCIceStreamGatherCandidates(GST_WEBRTC_ICE_STREAM(stream));
}

void webkitGstWebRTCIceStreamSetRemoteCredentials(WebKitGstIceStream* stream, const String& ufrag, const String& pwd)
{
    GUniquePtr<RiceCredentials> credentials(rice_credentials_new(ufrag.ascii().data(), pwd.ascii().data()));
    rice_stream_set_remote_credentials(stream->priv->riceStream.get(), credentials.get());

    stream->priv->haveRemoteCredentials = true;
    if (stream->priv->haveLocalCredentials && stream->priv->gatheringRequested)
        webkitGstWebRTCIceStreamGatherCandidates(GST_WEBRTC_ICE_STREAM(stream));
}

bool webkitGstWebRTCIceStreamGetSelectedPair(WebKitGstIceStream* stream, GstWebRTCICECandidateStats** localStats, GstWebRTCICECandidateStats** remoteStats)
{
    if (!stream->priv->rtpTransport) [[unlikely]]
        return false;

    return webkitGstWebRTCIceTransportGetSelectedPair(WEBKIT_GST_WEBRTC_ICE_TRANSPORT(stream->priv->rtpTransport.get()), localStats, remoteStats);
}

static void webkitGstWebRTCIceStreamFinalize(GObject* object)
{
    auto stream = WEBKIT_GST_WEBRTC_ICE_STREAM(object);
    auto agent = stream->priv->agent.get();
    if (agent)
        webkitGstWebRTCIceAgentFinalizeStream(agent.get(), GST_WEBRTC_ICE_STREAM(object)->stream_id);

    G_OBJECT_CLASS(webkit_gst_webrtc_ice_stream_parent_class)->finalize(object);
}

static void webkit_gst_webrtc_ice_stream_class_init(WebKitGstIceStreamClass* klass)
{
    auto gobjectClass = G_OBJECT_CLASS(klass);
    gobjectClass->finalize = webkitGstWebRTCIceStreamFinalize;

    auto iceClass = GST_WEBRTC_ICE_STREAM_CLASS(klass);
    iceClass->find_transport = webkitGstWebRTCIceStreamFindTransport;
    iceClass->gather_candidates = webkitGstWebRTCIceStreamGatherCandidates;
}

WebKitGstIceStream* webkitGstWebRTCCreateIceStream(WebKitGstIceAgent* agent, GRefPtr<RiceStream>&& riceStream)
{
    unsigned streamId = rice_stream_get_id(riceStream.get());
    auto stream = reinterpret_cast<WebKitGstIceStream*>(g_object_new(WEBKIT_TYPE_GST_WEBRTC_ICE_STREAM, "stream-id", streamId, nullptr));

    gst_object_ref_sink(stream);

    stream->priv->agent.reset(agent);
    stream->priv->riceStream = WTFMove(riceStream);
    return stream;
}

#undef GST_CAT_DEFAULT

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