File: WebRTCProvider.cpp

package info (click to toggle)
webkit2gtk 2.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 445,780 kB
  • sloc: cpp: 3,798,013; javascript: 197,914; ansic: 161,337; python: 49,141; asm: 21,990; ruby: 18,540; perl: 16,723; xml: 4,623; yacc: 2,360; sh: 2,246; java: 2,019; lex: 1,327; pascal: 366; makefile: 298
file content (494 lines) | stat: -rw-r--r-- 15,782 bytes parent folder | download | duplicates (4)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * Copyright (C) 2017 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WebRTCProvider.h"

#include "AV1Utilities.h"
#include "ContentType.h"
#include "HEVCUtilities.h"
#include "MediaCapabilitiesDecodingInfo.h"
#include "MediaCapabilitiesEncodingInfo.h"
#include "MediaDecodingConfiguration.h"
#include "MediaEncodingConfiguration.h"
#include "MediaEngineConfigurationFactory.h"
#include "VP9Utilities.h"

#include <wtf/Function.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
#include <wtf/text/StringToIntegerConversion.h>

namespace WebCore {

WTF_MAKE_TZONE_ALLOCATED_IMPL(WebRTCProvider);

#if !USE(LIBWEBRTC) && !USE(GSTREAMER_WEBRTC)
UniqueRef<WebRTCProvider> WebRTCProvider::create()
{
    return makeUniqueRef<WebRTCProvider>();
}

bool WebRTCProvider::webRTCAvailable()
{
    return false;
}
#endif

RefPtr<RTCDataChannelRemoteHandlerConnection> WebRTCProvider::createRTCDataChannelRemoteHandlerConnection()
{
    return nullptr;
}

void WebRTCProvider::setH265Support(bool value)
{
    m_supportsH265 = value;
#if ENABLE(WEB_RTC)
    m_videoDecodingCapabilities = { };
    m_videoEncodingCapabilities = { };
#endif
}

void WebRTCProvider::setVP9Support(bool supportsVP9Profile0, bool supportsVP9Profile2)
{
    m_supportsVP9Profile0 = supportsVP9Profile0;
    m_supportsVP9Profile2 = supportsVP9Profile2;

#if ENABLE(WEB_RTC)
    m_videoDecodingCapabilities = { };
    m_videoEncodingCapabilities = { };
#endif
}

void WebRTCProvider::setAV1Support(bool supportsAV1)
{
    m_supportsAV1 = supportsAV1;

#if ENABLE(WEB_RTC)
    m_videoDecodingCapabilities = { };
    m_videoEncodingCapabilities = { };
#endif
}

bool WebRTCProvider::isSupportingAV1() const
{
    return m_supportsAV1;
}

bool WebRTCProvider::isSupportingH265() const
{
    return m_supportsH265;
}

bool WebRTCProvider::isSupportingVP9Profile0() const
{
    return m_supportsVP9Profile0;
}

bool WebRTCProvider::isSupportingVP9Profile2() const
{
    return m_supportsVP9Profile2;
}

bool WebRTCProvider::isSupportingMDNS() const
{
    return m_supportsMDNS;
}

void WebRTCProvider::setLoggingLevel(WTFLogLevel)
{

}

void WebRTCProvider::clearFactory()
{

}

#if ENABLE(WEB_RTC)

std::optional<RTCRtpCapabilities> WebRTCProvider::receiverCapabilities(const String&)
{
    return { };
}

std::optional<RTCRtpCapabilities> WebRTCProvider::senderCapabilities(const String&)
{
    return { };
}

std::optional<RTCRtpCodecCapability> WebRTCProvider::codecCapability(const ContentType& contentType, const std::optional<RTCRtpCapabilities>& capabilities)
{
    if (!capabilities)
        return { };

    auto containerType = contentType.containerType();
    for (auto& codec : capabilities->codecs) {
        if (equalIgnoringASCIICase(containerType, codec.mimeType))
            return codec;
    }
    return { };
}

std::optional<RTCRtpCapabilities>& WebRTCProvider::audioDecodingCapabilities()
{
    if (!m_audioDecodingCapabilities)
        initializeAudioDecodingCapabilities();
    return m_audioDecodingCapabilities;
}

std::optional<RTCRtpCapabilities>& WebRTCProvider::videoDecodingCapabilities()
{
    if (!m_videoDecodingCapabilities)
        initializeVideoDecodingCapabilities();
    return m_videoDecodingCapabilities;
}

std::optional<RTCRtpCapabilities>& WebRTCProvider::audioEncodingCapabilities()
{
    if (!m_audioEncodingCapabilities)
        initializeAudioEncodingCapabilities();
    return m_audioEncodingCapabilities;
}

std::optional<RTCRtpCapabilities>& WebRTCProvider::videoEncodingCapabilities()
{
    if (!m_videoEncodingCapabilities)
        initializeVideoEncodingCapabilities();
    return m_videoEncodingCapabilities;
}

#endif // ENABLE(WEB_RTC)

std::optional<MediaCapabilitiesInfo> WebRTCProvider::computeVPParameters(const VideoConfiguration&)
{
    return { };
}

bool WebRTCProvider::isVPSoftwareDecoderSmooth(const VideoConfiguration&)
{
    return true;
}

bool WebRTCProvider::isVPXEncoderSmooth(const VideoConfiguration&)
{
    return false;
}

bool WebRTCProvider::isH264EncoderSmooth(const VideoConfiguration&)
{
    return true;
}

static String contentTypeFromRTPVideoMimeType(const String& mimeType)
{
    ContentType contentType { mimeType };
    auto containerType = contentType.containerType();
    if (equalLettersIgnoringASCIICase(containerType, "video/h264"_s)) {
        // https://datatracker.ietf.org/doc/html/rfc6184#section-8.1
        auto profileLevelId = contentType.parameter("profile-level-id"_s);
        if (profileLevelId.isEmpty())
            profileLevelId = "42001f"_s;
        return makeString("video/mp4;codecs=avc1."_s, profileLevelId);
    }

    if (equalLettersIgnoringASCIICase(containerType, "video/h265"_s)) {
        // https://datatracker.ietf.org/doc/html/rfc7798#section-7.1
        HEVCParameters parameters;
        auto profileSpace = contentType.parameter("profile-space"_s);
        if (!profileSpace.isEmpty()) {
            auto result = parseInteger<uint16_t>(profileSpace);
            if (!result || result > 3)
                return { };
            parameters.generalProfileSpace = *result;
        }

        auto profileId = contentType.parameter("profile-id"_s);
        if (!profileId.isEmpty()) {
            auto result = parseInteger<uint16_t>(profileId);
            if (!result)
                return { };
            parameters.generalProfileIDC = *result;
        } else
            parameters.generalProfileIDC = 1;

        auto profileCompatibilityIndicator = contentType.parameter("profile-compatibility-indicator"_s);
        if (!profileCompatibilityIndicator.isEmpty()) {
            auto compatibilityFlags = parseInteger<uint32_t>(profileCompatibilityIndicator, 16);
            if (!compatibilityFlags)
                return { };
            parameters.generalProfileCompatibilityFlags = reverseBits32(*compatibilityFlags);
        } else
            parameters.generalProfileCompatibilityFlags = 1 << parameters.generalProfileIDC;

        auto tierFlag = contentType.parameter("tier-flag"_s);
        if (tierFlag.isEmpty() || tierFlag == "0"_s)
            parameters.generalTierFlag = 0;
        else if (tierFlag == "1"_s)
            parameters.generalTierFlag = 1;
        else
            return { };

        auto levelId = contentType.parameter("level-id"_s);
        if (!levelId.isEmpty()) {
            auto result = parseInteger<uint16_t>(levelId);
            if (!result)
                return { };
            parameters.generalLevelIDC = *result;
        } else
            parameters.generalLevelIDC = 93;

        return makeString("video/mp4;codecs="_s, createHEVCCodecParametersString(parameters));
    }

    if (equalLettersIgnoringASCIICase(containerType, "video/vp9"_s)) {
        // https://www.rfc-editor.org/rfc/rfc9628.html#payloadFormatParameters
        VPCodecConfigurationRecord parameters;
        parameters.codecName = "vp09"_s;

        auto profileId = contentType.parameter("profile-id"_s);
        if (!profileId.isEmpty()) {
            auto result = parseInteger<uint8_t>(profileId);
            if (!result || *result > 3)
                return { };
            parameters.profile = *result;
        }

        // We use level 5 as the default.
        parameters.level = VPConfigurationLevel::Level_5;

        parameters.bitDepth = parameters.profile < 2 ? 8 : 10;

        return makeString("video/mp4;codecs="_s, createVPCodecParametersString(parameters));
    }

    if (equalLettersIgnoringASCIICase(containerType, "video/av1"_s)) {
        // https://www.iana.org/assignments/media-types/video/AV1
        AV1CodecConfigurationRecord parameters;
        parameters.codecName = "av01"_s;

        auto profile = contentType.parameter("profile"_s);
        if (!profile.isEmpty()) {
            auto result = parseEnumFromStringView<AV1ConfigurationProfile>(profile);
            if (!result)
                return { };
            parameters.profile = *result;
        }

        auto levelIdx = contentType.parameter("level-idx"_s);
        if (!levelIdx.isEmpty()) {
            auto result = parseEnumFromStringView<AV1ConfigurationLevel>(levelIdx);
            if (!result)
                return { };
            parameters.level = *result;
        } else
            parameters.level = AV1ConfigurationLevel::Level_3_1;

        auto tier = contentType.parameter("tier"_s);
        if (!tier.isEmpty()) {
            if (tier == "M"_s)
                parameters.tier = AV1ConfigurationTier::Main;
            else if (tier == "H"_s)
                parameters.tier = AV1ConfigurationTier::High;
            else
                return { };
        }

        parameters.bitDepth = parameters.profile < AV1ConfigurationProfile::Professional ? 8 : 10;

        return makeString("video/mp4;codecs="_s, createAV1CodecParametersString(parameters));
    }

    if (equalLettersIgnoringASCIICase(containerType, "video/vp8"_s))
        return "video/webm;codecs=vp08.00.50.08"_s;

    return { };
}

void WebRTCProvider::createDecodingConfiguration(MediaDecodingConfiguration&& configuration, DecodingConfigurationCallback&& callback)
{
    ASSERT(configuration.type == MediaDecodingType::WebRTC);

    // FIXME: Validate additional parameters, in particular mime type parameters.
    MediaCapabilitiesDecodingInfo info { { }, WTFMove(configuration) };

#if ENABLE(WEB_RTC)
    if (info.configuration.video) {
        ContentType contentType { info.configuration.video->contentType };
        auto codec = codecCapability(contentType, videoDecodingCapabilities());
        if (!codec) {
            callback({ { }, WTFMove(info.configuration) });
            return;
        }
        if (auto infoOverride = videoDecodingCapabilitiesOverride(*info.configuration.video)) {
            if (!infoOverride->supported) {
                callback({ { }, WTFMove(info.configuration) });
                return;
            }
            info.smooth = infoOverride->smooth;
            info.powerEfficient = infoOverride->powerEfficient;
        }
    }
    if (info.configuration.audio) {
        ContentType contentType { info.configuration.audio->contentType };
        auto codec = codecCapability(contentType, audioDecodingCapabilities());
        if (!codec) {
            callback({ { }, WTFMove(info.configuration) });
            return;
        }
    }
#endif
    // For power efficient decoders, we use the regular media engine MC code path which has more fine grained checks.
    if (info.powerEfficient && info.configuration.video) {
        auto videoConfiguration = info.configuration;
        videoConfiguration.audio = { };
        videoConfiguration.video->contentType = contentTypeFromRTPVideoMimeType(info.configuration.video->contentType);
        videoConfiguration.type = MediaDecodingType::MediaSource;

        MediaEngineConfigurationFactory::createDecodingConfiguration(WTFMove(videoConfiguration), [info = WTFMove(info), callback = WTFMove(callback)](auto&& result) mutable {
            info.supported = result.supported;
            info.smooth = result.smooth;
            info.powerEfficient = result.powerEfficient;
            if (!info.supported)
                info.configuration = { };
            callback(WTFMove(info));
        });
        return;
    }

    info.supported = true;
    callback(WTFMove(info));
}

void WebRTCProvider::createEncodingConfiguration(MediaEncodingConfiguration&& configuration, EncodingConfigurationCallback&& callback)
{
    ASSERT(configuration.type == MediaEncodingType::WebRTC);

    // FIXME: Validate additional parameters, in particular mime type parameters.
    MediaCapabilitiesEncodingInfo info { { }, WTFMove(configuration) };

#if ENABLE(WEB_RTC)
    if (info.configuration.video) {
        ContentType contentType { info.configuration.video->contentType };
        auto codec = codecCapability(contentType, videoEncodingCapabilities());
        if (!codec) {
            callback({ { }, WTFMove(info.configuration) });
            return;
        }
        if (auto infoOverride = videoEncodingCapabilitiesOverride(*info.configuration.video)) {
            if (!infoOverride->supported) {
                callback({ { }, WTFMove(info.configuration) });
                return;
            }
            info.smooth = infoOverride->smooth;
            info.powerEfficient = infoOverride->powerEfficient;
        }
    }
    if (info.configuration.audio) {
        ContentType contentType { info.configuration.audio->contentType };
        auto codec = codecCapability(contentType, audioEncodingCapabilities());
        if (!codec) {
            callback({ { }, WTFMove(info.configuration) });
            return;
        }
    }
#endif
    info.supported = true;
    callback(WTFMove(info));
}

void WebRTCProvider::initializeAudioDecodingCapabilities()
{

}

void WebRTCProvider::initializeVideoDecodingCapabilities()
{

}

void WebRTCProvider::initializeAudioEncodingCapabilities()
{

}

void WebRTCProvider::initializeVideoEncodingCapabilities()
{

}

std::optional<MediaCapabilitiesDecodingInfo> WebRTCProvider::videoDecodingCapabilitiesOverride(const VideoConfiguration&)
{
    return { };
}

std::optional<MediaCapabilitiesEncodingInfo> WebRTCProvider::videoEncodingCapabilitiesOverride(const VideoConfiguration&)
{
    return { };
}

void WebRTCProvider::setPortAllocatorRange(StringView range)
{
    if (range.isEmpty())
        return;

    if (range == "0:0"_s)
        return;

    auto components = range.toStringWithoutCopying().split(':');
    if (components.size() != 2) [[unlikely]] {
        WTFLogAlways("Invalid format for UDP port range. Should be \"min-port:max-port\"");
        ASSERT_NOT_REACHED();
        return;
    }

    auto minPort = WTF::parseInteger<int>(components[0]);
    auto maxPort = WTF::parseInteger<int>(components[1]);
    if (!minPort || !maxPort) {
        WTFLogAlways("Invalid format for UDP port range. Should be \"min-port:max-port\"");
        ASSERT_NOT_REACHED();
        return;
    }

    if (*minPort < 0) {
        WTFLogAlways("Invalid value for UDP minimum port value: %d", *minPort);
        return;
    }

    if (*maxPort < 0) {
        WTFLogAlways("Invalid value for UDP maximum port value: %d", *maxPort);
        return;
    }

    m_portAllocatorRange = { { *minPort, *maxPort } };
}

std::optional<std::pair<int, int>> WebRTCProvider::portAllocatorRange() const
{
    return m_portAllocatorRange;
}

} // namespace WebCore