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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
/*
* Copyright (C) 2020-2024 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 "LibWebRTCCodecs.h"
#if USE(LIBWEBRTC) && PLATFORM(COCOA) && ENABLE(GPU_PROCESS)
#include "GPUProcessConnection.h"
#include "LibWebRTCCodecsMessages.h"
#include "LibWebRTCCodecsProxyMessages.h"
#include "LibWebRTCProvider.h"
#include "Logging.h"
#include "RemoteVideoFrameObjectHeapProxy.h"
#include "RemoteVideoFrameProxy.h"
#include "WebProcess.h"
#include <WebCore/CVUtilities.h>
#include <WebCore/LibWebRTCDav1dDecoder.h>
#include <WebCore/LibWebRTCMacros.h>
#include <WebCore/NativeImage.h>
#include <WebCore/Page.h>
#include <WebCore/PlatformMediaSessionManager.h>
#include <WebCore/VP9UtilitiesCocoa.h>
#include <WebCore/VideoFrameCV.h>
#include <wtf/MainThread.h>
#include <wtf/StdLibExtras.h>
#include <wtf/TZoneMallocInlines.h>
WTF_IGNORE_WARNINGS_IN_THIRD_PARTY_CODE_BEGIN
#include <webrtc/webkit_sdk/WebKit/WebKitDecoder.h>
#include <webrtc/webkit_sdk/WebKit/WebKitEncoder.h>
WTF_IGNORE_WARNINGS_IN_THIRD_PARTY_CODE_END
#include <pal/cf/CoreMediaSoftLink.h>
namespace WebKit {
using namespace WebCore;
static webrtc::WebKitVideoDecoder createVideoDecoder(const webrtc::SdpVideoFormat& format)
{
auto& codecs = WebProcess::singleton().libWebRTCCodecs();
auto codecString = String::fromUTF8(format.name);
if (equalIgnoringASCIICase(codecString, "H264"_s))
return { codecs.createDecoder(WebCore::VideoCodecType::H264), false };
if (equalIgnoringASCIICase(codecString, "H265"_s))
return { codecs.createDecoder(WebCore::VideoCodecType::H265), false };
#if ENABLE(VP9)
if (equalIgnoringASCIICase(codecString, "VP9"_s) && codecs.isSupportingVP9HardwareDecoder())
return { codecs.createDecoder(WebCore::VideoCodecType::VP9), false };
#endif
#if ENABLE(AV1)
if (equalIgnoringASCIICase(codecString, "AV1"_s)) {
if (codecs.hasAV1HardwareDecoder())
return { codecs.createDecoder(WebCore::VideoCodecType::AV1), false };
auto av1Decoder = createLibWebRTCDav1dDecoder().moveToUniquePtr();
return { av1Decoder.release(), true };
}
#endif
return { };
}
WTF_MAKE_TZONE_ALLOCATED_IMPL(LibWebRTCCodecs);
WTF_MAKE_TZONE_ALLOCATED_IMPL(LibWebRTCCodecs::Decoder);
WTF_MAKE_TZONE_ALLOCATED_IMPL(LibWebRTCCodecs::Encoder);
std::optional<WebCore::VideoCodecType> LibWebRTCCodecs::videoCodecTypeFromWebCodec(const String& codec)
{
// WebCodecs is assuming case sensitive comparisons.
if (codec.startsWith("avc1."_s))
return WebCore::VideoCodecType::H264;
if (codec.startsWith("vp09.0"_s)) {
if (!isSupportingVP9HardwareDecoder())
return { };
return WebCore::VideoCodecType::VP9;
}
if (codec.startsWith("av01."_s)) {
if (!hasAV1HardwareDecoder())
return { };
return WebCore::VideoCodecType::AV1;
}
if (codec.startsWith("hev1."_s) || codec.startsWith("hvc1."_s))
return WebCore::VideoCodecType::H265;
return { };
}
#if ENABLE(AV1)
void LibWebRTCCodecs::setHasAV1HardwareDecoder(bool hasAV1HardwareDecoder)
{
m_hasAV1HardwareDecoder = hasAV1HardwareDecoder;
if (!hasAV1HardwareDecoder)
return;
Page::forEachPage([](auto& page) {
auto& settings = page.settings();
if (settings.webRTCAV1CodecEnabled() && settings.webCodecsAV1Enabled())
return;
settings.setWebRTCAV1CodecEnabled(true);
settings.setWebCodecsAV1Enabled(true);
page.settingsDidChange();
});
}
#endif
std::optional<WebCore::VideoCodecType> LibWebRTCCodecs::videoEncoderTypeFromWebCodec(const String& codec)
{
// WebCodecs is assuming case sensitive comparisons.
if (codec.startsWith("avc1."_s))
return WebCore::VideoCodecType::H264;
if (codec.startsWith("hev1."_s) || codec.startsWith("hvc1."_s))
return WebCore::VideoCodecType::H265;
return { };
}
static int32_t releaseVideoDecoder(webrtc::WebKitVideoDecoder::Value decoder)
{
return WebProcess::singleton().libWebRTCCodecs().releaseDecoder(*static_cast<LibWebRTCCodecs::Decoder*>(decoder));
}
static int32_t decodeVideoFrame(webrtc::WebKitVideoDecoder::Value decoder, uint32_t timeStamp, const uint8_t* data, size_t size, uint16_t width, uint16_t height)
{
return WebProcess::singleton().libWebRTCCodecs().decodeWebRTCFrame(*static_cast<LibWebRTCCodecs::Decoder*>(decoder), timeStamp, unsafeMakeSpan(data, size), width, height);
}
static int32_t registerDecodeCompleteCallback(webrtc::WebKitVideoDecoder::Value decoder, void* decodedImageCallback)
{
WebProcess::singleton().libWebRTCCodecs().registerDecodeFrameCallback(*static_cast<LibWebRTCCodecs::Decoder*>(decoder), decodedImageCallback);
return 0;
}
static webrtc::WebKitVideoEncoder createVideoEncoder(const webrtc::SdpVideoFormat& format)
{
if (format.name == "H264" || format.name == "h264")
return WebProcess::singleton().libWebRTCCodecs().createEncoder(WebCore::VideoCodecType::H264, format.parameters);
if (format.name == "H265" || format.name == "h265")
return WebProcess::singleton().libWebRTCCodecs().createEncoder(WebCore::VideoCodecType::H265, format.parameters);
return nullptr;
}
static int32_t releaseVideoEncoder(webrtc::WebKitVideoEncoder encoder)
{
return WebProcess::singleton().libWebRTCCodecs().releaseEncoder(*static_cast<LibWebRTCCodecs::Encoder*>(encoder));
}
static int32_t initializeVideoEncoder(webrtc::WebKitVideoEncoder encoder, const webrtc::VideoCodec& codec)
{
return WebProcess::singleton().libWebRTCCodecs().initializeEncoder(*static_cast<LibWebRTCCodecs::Encoder*>(encoder), codec.width, codec.height, codec.startBitrate, codec.maxBitrate, codec.minBitrate, codec.maxFramerate);
}
static inline VideoFrame::Rotation toVideoRotation(webrtc::VideoRotation rotation)
{
switch (rotation) {
case webrtc::kVideoRotation_0:
return VideoFrame::Rotation::None;
case webrtc::kVideoRotation_180:
return VideoFrame::Rotation::UpsideDown;
case webrtc::kVideoRotation_90:
return VideoFrame::Rotation::Right;
case webrtc::kVideoRotation_270:
return VideoFrame::Rotation::Left;
}
ASSERT_NOT_REACHED();
return VideoFrame::Rotation::None;
}
static void createRemoteDecoder(LibWebRTCCodecs::Decoder& decoder, IPC::Connection& connection, bool useRemoteFrames, bool enableAdditionalLogging, Function<void(bool)>&& callback)
{
connection.sendWithAsyncReplyOnDispatcher(Messages::LibWebRTCCodecsProxy::CreateDecoder { decoder.identifier, decoder.type, decoder.codec, useRemoteFrames, enableAdditionalLogging }, WebProcess::singleton().libWebRTCCodecs().workQueue(), WTFMove(callback), 0);
}
static int32_t encodeVideoFrame(webrtc::WebKitVideoEncoder encoder, const webrtc::VideoFrame& frame, bool shouldEncodeAsKeyFrame)
{
return WebProcess::singleton().libWebRTCCodecs().encodeFrame(*static_cast<LibWebRTCCodecs::Encoder*>(encoder), frame, shouldEncodeAsKeyFrame);
}
static int32_t registerEncodeCompleteCallback(webrtc::WebKitVideoEncoder encoder, void* encodedImageCallback)
{
WebProcess::singleton().libWebRTCCodecs().registerEncodeFrameCallback(*static_cast<LibWebRTCCodecs::Encoder*>(encoder), encodedImageCallback);
return 0;
}
static void setEncodeRatesCallback(webrtc::WebKitVideoEncoder encoder, const webrtc::VideoEncoder::RateControlParameters& parameters)
{
uint32_t bitRateInKbps = parameters.bitrate.get_sum_kbps();
uint32_t frameRate = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
WebProcess::singleton().libWebRTCCodecs().setEncodeRates(*static_cast<LibWebRTCCodecs::Encoder*>(encoder), bitRateInKbps, frameRate);
}
Ref<LibWebRTCCodecs> LibWebRTCCodecs::create()
{
return adoptRef(*new LibWebRTCCodecs);
}
LibWebRTCCodecs::LibWebRTCCodecs()
: m_queue(WorkQueue::create("LibWebRTCCodecs"_s, WorkQueue::QOS::UserInteractive))
{
}
void LibWebRTCCodecs::initializeIfNeeded()
{
// Let's create the GPUProcess connection once to know whether we should do VP9 in GPUProcess.
static std::once_flag doInitializationOnce;
std::call_once(doInitializationOnce, [] {
callOnMainRunLoopAndWait([] {
#if HAVE(AUDIT_TOKEN)
WebProcess::singleton().ensureGPUProcessConnection().auditToken();
#else
WebProcess::singleton().ensureGPUProcessConnection();
#endif
});
});
}
void LibWebRTCCodecs::ensureGPUProcessConnectionOnMainThreadWithLock()
{
ASSERT(isMainRunLoop());
if (m_connection)
return;
Ref gpuConnection = WebProcess::singleton().ensureGPUProcessConnection();
gpuConnection->addClient(*this);
m_connection = &gpuConnection->connection();
m_videoFrameObjectHeapProxy = &gpuConnection->videoFrameObjectHeapProxy();
m_connection->addWorkQueueMessageReceiver(Messages::LibWebRTCCodecs::messageReceiverName(), m_queue, *this);
if (m_loggingLevel)
m_connection->send(Messages::LibWebRTCCodecsProxy::SetRTCLoggingLevel { *m_loggingLevel }, 0);
}
// May be called on any thread.
void LibWebRTCCodecs::ensureGPUProcessConnectionAndDispatchToThread(Function<void()>&& task)
{
m_needsGPUProcessConnection = true;
Locker locker { m_connectionLock };
// Fast path when we already have a connection.
if (m_connection) {
m_queue->dispatch(WTFMove(task));
return;
}
// We don't have a connection to the GPUProcess yet, we need to hop to the main thread to initiate it.
m_tasksToDispatchAfterEstablishingConnection.append(WTFMove(task));
if (m_tasksToDispatchAfterEstablishingConnection.size() != 1)
return;
callOnMainRunLoop([this] {
Locker locker { m_connectionLock };
ensureGPUProcessConnectionOnMainThreadWithLock();
for (auto& task : std::exchange(m_tasksToDispatchAfterEstablishingConnection, { }))
m_queue->dispatch(WTFMove(task));
});
}
void LibWebRTCCodecs::gpuProcessConnectionMayNoLongerBeNeeded()
{
assertIsCurrent(workQueue());
if (m_encoders.isEmpty() && m_decoders.isEmpty())
m_needsGPUProcessConnection = false;
}
LibWebRTCCodecs::~LibWebRTCCodecs()
{
ASSERT_NOT_REACHED();
}
void LibWebRTCCodecs::setCallbacks(bool useGPUProcess, bool useRemoteFrames)
{
ASSERT(isMainRunLoop());
if (!LibWebRTCProvider::webRTCAvailable())
return;
// We can enable GPUProcess but disable it is difficult once enabled since callbacks may be used in background threads.
if (!useGPUProcess)
return;
auto& libWebRTCCodecs = WebProcess::singleton().libWebRTCCodecs();
libWebRTCCodecs.m_useRemoteFrames = useRemoteFrames;
if (libWebRTCCodecs.m_useGPUProcess)
return;
libWebRTCCodecs.m_useGPUProcess = useGPUProcess;
webrtc::setVideoDecoderCallbacks(createVideoDecoder, releaseVideoDecoder, decodeVideoFrame, registerDecodeCompleteCallback);
webrtc::setVideoEncoderCallbacks(createVideoEncoder, releaseVideoEncoder, initializeVideoEncoder, encodeVideoFrame, registerEncodeCompleteCallback, setEncodeRatesCallback);
}
void LibWebRTCCodecs::setWebRTCMediaPipelineAdditionalLoggingEnabled(bool enabled)
{
if (!LibWebRTCProvider::webRTCAvailable())
return;
WebProcess::singleton().libWebRTCCodecs().m_enableAdditionalLogging = enabled;
}
// May be called on any thread.
LibWebRTCCodecs::Decoder* LibWebRTCCodecs::createDecoder(WebCore::VideoCodecType type)
{
return createDecoderInternal(type, { }, [](auto*) { });
}
void LibWebRTCCodecs::createDecoderAndWaitUntilReady(WebCore::VideoCodecType type, const String& codec, Function<void(Decoder*)>&& callback)
{
createDecoderInternal(type, codec, WTFMove(callback));
}
LibWebRTCCodecs::Decoder* LibWebRTCCodecs::createDecoderInternal(WebCore::VideoCodecType type, const String& codec, Function<void(Decoder*)>&& callback)
{
auto decoder = makeUnique<Decoder>(VideoDecoderIdentifier::generate());
auto* result = decoder.get();
decoder->type = type;
decoder->codec = codec.isolatedCopy();
ensureGPUProcessConnectionAndDispatchToThread([this, decoder = WTFMove(decoder), callback = WTFMove(callback)]() mutable {
assertIsCurrent(workQueue());
{
Locker locker { m_connectionLock };
createRemoteDecoder(*decoder, *protectedConnection(), m_useRemoteFrames, m_enableAdditionalLogging, [identifier = decoder->identifier, callback = WTFMove(callback)](bool result) mutable {
if (!result) {
callback(nullptr);
return;
}
auto& codecs = WebProcess::singleton().libWebRTCCodecs();
assertIsCurrent(codecs.workQueue());
callback(codecs.m_decoders.get(identifier));
});
setDecoderConnection(*decoder, m_connection.get());
auto decoderIdentifier = decoder->identifier;
ASSERT(!m_decoders.contains(decoderIdentifier));
m_decoders.add(decoderIdentifier, WTFMove(decoder));
}
});
return result;
}
// May be called on any thread.
int32_t LibWebRTCCodecs::releaseDecoder(Decoder& decoder)
{
#if ASSERT_ENABLED
{
Locker locker { decoder.decodedImageCallbackLock };
ASSERT(!decoder.decodedImageCallback);
}
#endif
ensureGPUProcessConnectionAndDispatchToThread([this, decoderIdentifier = decoder.identifier] {
assertIsCurrent(workQueue());
ASSERT(m_decoders.contains(decoderIdentifier));
if (auto decoder = m_decoders.take(decoderIdentifier)) {
Locker locker { m_connectionLock };
decoderConnection(*decoder)->send(Messages::LibWebRTCCodecsProxy::ReleaseDecoder { decoderIdentifier }, 0);
gpuProcessConnectionMayNoLongerBeNeeded();
}
});
return 0;
}
// May be called on any thread.
Ref<GenericPromise> LibWebRTCCodecs::flushDecoder(Decoder& decoder)
{
Locker locker { m_connectionLock };
if (!decoder.connection || decoder.hasError)
return GenericPromise::createAndResolve();
return decoder.connection->sendWithPromisedReply(Messages::LibWebRTCCodecsProxy::FlushDecoder { decoder.identifier }, 0)->whenSettled(workQueue(), [] (auto&&) {
return GenericPromise::createAndResolve();
});
}
void LibWebRTCCodecs::setDecoderFormatDescription(Decoder& decoder, std::span<const uint8_t> data, uint16_t width, uint16_t height)
{
Locker locker { m_connectionLock };
ASSERT(decoder.connection);
if (!decoder.connection)
return;
decoder.connection->send(Messages::LibWebRTCCodecsProxy::SetDecoderFormatDescription { decoder.identifier, data, width, height }, 0);
}
Ref<LibWebRTCCodecs::FramePromise> LibWebRTCCodecs::sendFrameToDecode(Decoder& decoder, int64_t timeStamp, std::span<const uint8_t> data, uint16_t width, uint16_t height)
{
if (decoder.type == WebCore::VideoCodecType::VP9 && (width || height))
decoder.connection->send(Messages::LibWebRTCCodecsProxy::SetFrameSize { decoder.identifier, width, height }, 0);
return decoder.connection->sendWithPromisedReply(Messages::LibWebRTCCodecsProxy::DecodeFrame { decoder.identifier, timeStamp, data }, 0)->whenSettled(workQueue(), [] (auto&& result) mutable {
if (!result)
return FramePromise::createAndReject("Decoding task did not complete"_s);
if (!*result)
return FramePromise::createAndReject("Decoding task failed"_s);
return FramePromise::createAndResolve();
});
}
int32_t LibWebRTCCodecs::decodeWebRTCFrame(Decoder& decoder, int64_t timeStamp, std::span<const uint8_t> data, uint16_t width, uint16_t height)
{
auto promise = decodeFrameInternal(decoder, timeStamp, data, width, height);
return promise ? WEBRTC_VIDEO_CODEC_OK : WEBRTC_VIDEO_CODEC_ERROR;
}
Ref<LibWebRTCCodecs::FramePromise> LibWebRTCCodecs::decodeFrame(Decoder& decoder, int64_t timeStamp, std::span<const uint8_t> data)
{
auto promise = decodeFrameInternal(decoder, timeStamp, data, 0, 0);
return promise ? promise.releaseNonNull() : FramePromise::createAndReject("Decoding task did not complete"_s);
}
RefPtr<LibWebRTCCodecs::FramePromise> LibWebRTCCodecs::decodeFrameInternal(Decoder& decoder, int64_t timeStamp, std::span<const uint8_t> data, uint16_t width, uint16_t height)
{
Locker locker { m_connectionLock };
if (decoder.hasError) {
decoder.hasError = false;
return nullptr;
}
if (!decoder.connection) {
FramePromise::AutoRejectProducer producer;
auto promise = producer.promise();
decoder.pendingFrames.append({ timeStamp, data, width, height, WTFMove(producer) });
return promise;
}
return sendFrameToDecode(decoder, timeStamp, data, width, height);
}
void LibWebRTCCodecs::registerDecodeFrameCallback(Decoder& decoder, void* decodedImageCallback)
{
ASSERT(!isMainRunLoop());
Locker locker { decoder.decodedImageCallbackLock };
ASSERT(!decoder.decoderCallback);
decoder.decodedImageCallback = decodedImageCallback;
}
// May be called on any thread.
void LibWebRTCCodecs::registerDecodedVideoFrameCallback(Decoder& decoder, DecoderCallback&& callback)
{
Locker locker { decoder.decodedImageCallbackLock };
ASSERT(!decoder.decodedImageCallback);
decoder.decoderCallback = WTFMove(callback);
}
void LibWebRTCCodecs::failedDecoding(VideoDecoderIdentifier decoderIdentifier)
{
assertIsCurrent(workQueue());
if (auto* decoder = m_decoders.get(decoderIdentifier)) {
decoder->hasError = true;
Locker locker { decoder->decodedImageCallbackLock };
if (decoder->decoderCallback)
decoder->decoderCallback(nullptr, 0);
}
}
void LibWebRTCCodecs::completedDecoding(VideoDecoderIdentifier decoderIdentifier, int64_t timeStamp, int64_t timeStampNs, RemoteVideoFrameProxy::Properties&& properties)
{
assertIsCurrent(workQueue());
// Adopt RemoteVideoFrameProxy::Properties to RemoteVideoFrameProxy instance before the early outs, so that the reference gets adopted.
// Typically RemoteVideoFrameProxy::Properties&& sent to destinations that are already removed need to be handled separately.
// LibWebRTCCodecs is not ever removed, so we do not do this. However, if it ever is, LibWebRTCCodecs::handleMessageToRemovedDestination()
// needs to be implemented.
Ref<RemoteVideoFrameProxy> remoteVideoFrame = [&] {
Locker locker { m_connectionLock };
return RemoteVideoFrameProxy::create(protectedConnection().releaseNonNull(), *protectedVideoFrameObjectHeapProxy(), WTFMove(properties));
}();
// FIXME: Do error logging.
auto* decoder = m_decoders.get(decoderIdentifier);
if (!decoder)
return;
if (!decoder->decodedImageCallbackLock.tryLock())
return;
Locker locker { AdoptLock, decoder->decodedImageCallbackLock };
if (decoder->decoderCallback) {
decoder->decoderCallback(WTFMove(remoteVideoFrame), timeStamp);
return;
}
if (!decoder->decodedImageCallback)
return;
auto& frame = remoteVideoFrame.leakRef(); // Balanced by the release callback of videoDecoderTaskComplete.
webrtc::videoDecoderTaskComplete(decoder->decodedImageCallback, timeStamp, &frame,
[](auto* pointer) { return static_cast<RemoteVideoFrameProxy*>(pointer)->pixelBuffer(); },
[](auto* pointer) { static_cast<RemoteVideoFrameProxy*>(pointer)->deref(); },
frame.size().width(), frame.size().height());
}
void LibWebRTCCodecs::completedDecodingCV(VideoDecoderIdentifier decoderIdentifier, int64_t timeStamp, int64_t timeStampNs, RetainPtr<CVPixelBufferRef>&& pixelBuffer)
{
assertIsCurrent(workQueue());
// FIXME: Do error logging.
auto* decoder = m_decoders.get(decoderIdentifier);
if (!decoder)
return;
if (!decoder->decodedImageCallbackLock.tryLock())
return;
Locker locker { AdoptLock, decoder->decodedImageCallbackLock };
if (!pixelBuffer) {
ASSERT_NOT_REACHED();
return;
}
if (decoder->decoderCallback) {
decoder->decoderCallback(VideoFrameCV::create(MediaTime { timeStampNs, 1000000000 }, false, VideoFrame::Rotation::None, WTFMove(pixelBuffer)), timeStamp);
return;
}
if (!decoder->decodedImageCallback)
return;
webrtc::videoDecoderTaskComplete(decoder->decodedImageCallback, timeStamp, pixelBuffer.get());
}
static inline webrtc::VideoCodecType toWebRTCCodecType(WebCore::VideoCodecType type)
{
switch (type) {
case WebCore::VideoCodecType::H264:
return webrtc::kVideoCodecH264;
case WebCore::VideoCodecType::H265:
return webrtc::kVideoCodecH265;
case WebCore::VideoCodecType::VP9:
return webrtc::kVideoCodecVP9;
case WebCore::VideoCodecType::AV1:
return webrtc::kVideoCodecAV1;
}
}
LibWebRTCCodecs::Encoder* LibWebRTCCodecs::createEncoder(WebCore::VideoCodecType type, const std::map<std::string, std::string>& parameters)
{
return createEncoderInternal(type, { }, parameters, true, true, VideoEncoderScalabilityMode::L1T1, [](auto*) { });
}
#if ENABLE(WEB_CODECS)
void LibWebRTCCodecs::createEncoderAndWaitUntilInitialized(WebCore::VideoCodecType type, const String& codec, const std::map<std::string, std::string>& parameters, const VideoEncoder::Config& config, Function<void(Encoder*)>&& callback)
{
createEncoderInternal(type, codec, parameters, config.isRealtime, config.useAnnexB, config.scalabilityMode, [config, callback = WTFMove(callback)] (auto* encoder) {
if (encoder) {
// Encoders use kbps bit rates.
WebProcess::singleton().libWebRTCCodecs().initializeEncoderInternal(*encoder, config.width, config.height, config.bitRate / 1000, 2 * config.bitRate / 1000, 0, config.frameRate);
WebProcess::singleton().libWebRTCCodecs().setEncodeRates(*encoder, config.bitRate / 1000, config.frameRate);
}
callback(encoder);
});
}
#endif // ENABLE(WEB_CODECS)
static void createRemoteEncoder(LibWebRTCCodecs::Encoder& encoder, IPC::Connection& connection, const Vector<std::pair<String, String>>& parameters, Function<void(bool)>&& callback)
{
connection.sendWithAsyncReplyOnDispatcher(Messages::LibWebRTCCodecsProxy::CreateEncoder { encoder.identifier, encoder.type, encoder.codec, parameters, encoder.isRealtime, encoder.useAnnexB, encoder.scalabilityMode }, WebProcess::singleton().libWebRTCCodecs().workQueue(), WTFMove(callback), 0);
}
LibWebRTCCodecs::Encoder* LibWebRTCCodecs::createEncoderInternal(WebCore::VideoCodecType type, const String& codec, const std::map<std::string, std::string>& formatParameters, bool isRealtime, bool useAnnexB, VideoEncoderScalabilityMode scalabilityMode, Function<void(Encoder*)>&& callback)
{
auto encoder = makeUnique<Encoder>(VideoEncoderIdentifier::generate());
auto* result = encoder.get();
encoder->type = type;
encoder->useAnnexB = useAnnexB;
encoder->isRealtime = isRealtime;
encoder->codec = codec.isolatedCopy();
encoder->scalabilityMode = scalabilityMode;
auto parameters = WTF::map(formatParameters, [](auto& entry) {
return std::pair { String::fromUTF8(entry.first), String::fromUTF8(entry.second) };
});
ensureGPUProcessConnectionAndDispatchToThread([this, encoder = WTFMove(encoder), parameters = WTFMove(parameters), callback = WTFMove(callback)]() mutable {
assertIsCurrent(workQueue());
auto connection = [&]() -> Ref<IPC::Connection> {
Locker locker { m_connectionLock };
return *m_connection;
}();
{
Locker locker { m_encodersConnectionLock };
createRemoteEncoder(*encoder, connection.get(), parameters, [identifier = encoder->identifier, callback = WTFMove(callback)](bool result) mutable {
if (!result) {
callback(nullptr);
return;
}
auto& codecs = WebProcess::singleton().libWebRTCCodecs();
assertIsCurrent(codecs.workQueue());
callback(codecs.m_encoders.get(identifier));
});
setEncoderConnection(*encoder, connection.ptr());
}
encoder->parameters = WTFMove(parameters);
auto encoderIdentifier = encoder->identifier;
ASSERT(!m_encoders.contains(encoderIdentifier));
m_encoders.add(encoderIdentifier, WTFMove(encoder));
});
return result;
}
int32_t LibWebRTCCodecs::releaseEncoder(Encoder& encoder)
{
#if ASSERT_ENABLED
{
Locker locker { encoder.encodedImageCallbackLock };
ASSERT(!encoder.encodedImageCallback);
}
#endif
ensureGPUProcessConnectionAndDispatchToThread([this, encoderIdentifier = encoder.identifier] {
assertIsCurrent(workQueue());
ASSERT(m_encoders.contains(encoderIdentifier));
auto encoder = m_encoders.take(encoderIdentifier);
Locker locker { m_encodersConnectionLock };
encoderConnection(*encoder)->send(Messages::LibWebRTCCodecsProxy::ReleaseEncoder { encoderIdentifier }, 0);
gpuProcessConnectionMayNoLongerBeNeeded();
});
return 0;
}
int32_t LibWebRTCCodecs::initializeEncoder(Encoder& encoder, uint16_t width, uint16_t height, unsigned startBitRate, unsigned maxBitRate, unsigned minBitRate, uint32_t maxFrameRate)
{
ASSERT(!isMainRunLoop());
ensureGPUProcessConnectionAndDispatchToThread([this, encoderIdentifier = encoder.identifier, width, height, startBitRate, maxBitRate, minBitRate, maxFrameRate]() mutable {
assertIsCurrent(workQueue());
initializeEncoderInternal(*m_encoders.get(encoderIdentifier), width, height, startBitRate, maxBitRate, minBitRate, maxFrameRate);
});
return 0;
}
void LibWebRTCCodecs::initializeEncoderInternal(Encoder& encoder, uint16_t width, uint16_t height, unsigned startBitRate, unsigned maxBitRate, unsigned minBitRate, uint32_t maxFrameRate)
{
assertIsCurrent(workQueue());
encoder.initializationData = EncoderInitializationData { width, height, startBitRate, maxBitRate, minBitRate, maxFrameRate };
Locker locker { m_encodersConnectionLock };
encoderConnection(encoder)->send(Messages::LibWebRTCCodecsProxy::InitializeEncoder { encoder.identifier, width, height, startBitRate, maxBitRate, minBitRate, maxFrameRate }, 0);
}
template<typename Frame> RefPtr<LibWebRTCCodecs::FramePromise> LibWebRTCCodecs::encodeFrameInternal(Encoder& encoder, const Frame& frame, bool shouldEncodeAsKeyFrame, WebCore::VideoFrame::Rotation rotation, MediaTime mediaTime, int64_t timestamp, std::optional<uint64_t> duration)
{
Locker locker { m_encodersConnectionLock };
auto* connection = encoderConnection(encoder);
if (!connection)
return nullptr;
auto buffer = encoder.sharedVideoFrameWriter.writeBuffer(frame,
[&](auto& semaphore) { encoder.connection->send(Messages::LibWebRTCCodecsProxy::SetSharedVideoFrameSemaphore { encoder.identifier, semaphore }, 0); },
[&](SharedMemory::Handle&& handle) { encoder.connection->send(Messages::LibWebRTCCodecsProxy::SetSharedVideoFrameMemory { encoder.identifier, WTFMove(handle) }, 0); });
if (!buffer)
return nullptr;
SharedVideoFrame sharedVideoFrame { mediaTime, false, rotation, WTFMove(*buffer) };
auto promise = encoder.connection->sendWithPromisedReply(Messages::LibWebRTCCodecsProxy::EncodeFrame { encoder.identifier, WTFMove(sharedVideoFrame), timestamp, duration, shouldEncodeAsKeyFrame });
return promise->whenSettled(workQueue(), [] (auto&& result) mutable {
if (!result)
return FramePromise::createAndReject("Encoding task did not complete"_s);
if (!*result)
return FramePromise::createAndReject("Encoding task failed"_s);
return FramePromise::createAndResolve();
});
}
int32_t LibWebRTCCodecs::encodeFrame(Encoder& encoder, const webrtc::VideoFrame& frame, bool shouldEncodeAsKeyFrame)
{
auto promise = encodeFrameInternal(encoder, frame, shouldEncodeAsKeyFrame, toVideoRotation(frame.rotation()), MediaTime::createWithDouble(Seconds::fromMicroseconds(frame.timestamp_us()).value()), frame.timestamp_us(), { });
return promise ? WEBRTC_VIDEO_CODEC_OK : WEBRTC_VIDEO_CODEC_ERROR;
}
Ref<LibWebRTCCodecs::FramePromise> LibWebRTCCodecs::encodeFrame(Encoder& encoder, const WebCore::VideoFrame& frame, int64_t timestamp, std::optional<uint64_t> duration, bool shouldEncodeAsKeyFrame)
{
auto promise = encodeFrameInternal(encoder, frame, shouldEncodeAsKeyFrame, frame.rotation(), frame.presentationTime(), timestamp, duration);
return promise ? promise.releaseNonNull() : FramePromise::createAndReject("Encoding task did not complete"_s);
}
Ref<GenericPromise> LibWebRTCCodecs::flushEncoder(Encoder& encoder)
{
Locker locker { m_encodersConnectionLock };
RefPtr connection = encoderConnection(encoder);
if (!connection)
return GenericPromise::createAndResolve();
return connection->sendWithPromisedReply(Messages::LibWebRTCCodecsProxy::FlushEncoder { encoder.identifier })->whenSettled(workQueue(), [] (auto&&) {
return GenericPromise::createAndResolve();
});
}
void LibWebRTCCodecs::registerEncodeFrameCallback(Encoder& encoder, void* encodedImageCallback)
{
ASSERT(!isMainRunLoop());
Locker locker { encoder.encodedImageCallbackLock };
ASSERT(!encoder.encoderCallback);
encoder.encodedImageCallback = encodedImageCallback;
}
void LibWebRTCCodecs::registerEncodedVideoFrameCallback(Encoder& encoder, EncoderCallback&& callback)
{
Locker locker { encoder.encodedImageCallbackLock };
ASSERT(!encoder.encodedImageCallback);
encoder.encoderCallback = WTFMove(callback);
}
#if ENABLE(WEB_CODECS)
void LibWebRTCCodecs::registerEncoderDescriptionCallback(Encoder& encoder, DescriptionCallback&& callback)
{
Locker locker { encoder.encodedImageCallbackLock };
ASSERT(!encoder.encodedImageCallback);
encoder.descriptionCallback = WTFMove(callback);
}
#endif
Ref<GenericPromise> LibWebRTCCodecs::setEncodeRates(Encoder& encoder, uint32_t bitRateInKbps, uint32_t frameRate)
{
Locker locker { m_encodersConnectionLock };
auto* connection = encoderConnection(encoder);
if (!connection) {
GenericPromise::AutoRejectProducer producer;
auto promise = producer.promise();
ensureGPUProcessConnectionAndDispatchToThread([this, hasSentInitialEncodeRates = &encoder.hasSentInitialEncodeRates, encoderIdentifier = encoder.identifier, bitRateInKbps, frameRate, producer = WTFMove(producer)] () mutable {
assertIsCurrent(workQueue());
ASSERT(m_encoders.get(encoderIdentifier));
// hasSentInitialEncodeRates remains valid as encoder destruction goes through ensureGPUProcessConnectionAndDispatchToThread.
if (*hasSentInitialEncodeRates)
return;
Locker locker { m_connectionLock };
m_connection->sendWithPromisedReply(Messages::LibWebRTCCodecsProxy::SetEncodeRates { encoderIdentifier, bitRateInKbps, frameRate })->whenSettled(workQueue(), [producer = WTFMove(producer)] (auto&&) {
producer.resolve();
});
});
return promise;
}
encoder.hasSentInitialEncodeRates = true;
return connection->sendWithPromisedReply(Messages::LibWebRTCCodecsProxy::SetEncodeRates { encoder.identifier, bitRateInKbps, frameRate })->whenSettled(workQueue(), [] (auto&&) {
return GenericPromise::createAndResolve();
});
}
void LibWebRTCCodecs::completedEncoding(VideoEncoderIdentifier identifier, std::span<const uint8_t> data, const webrtc::WebKitEncodedFrameInfo& info)
{
assertIsCurrent(workQueue());
// FIXME: Do error logging.
auto* encoder = m_encoders.get(identifier);
if (!encoder)
return;
if (!encoder->encodedImageCallbackLock.tryLock())
return;
Locker locker { AdoptLock, encoder->encodedImageCallbackLock };
if (encoder->encoderCallback) {
auto temporalIndex = info.temporalIndex >= 0 ? std::make_optional<unsigned>(info.temporalIndex) : std::nullopt;
encoder->encoderCallback(data, info.frameType == webrtc::VideoFrameType::kVideoFrameKey, info.timeStamp, info.duration, temporalIndex);
return;
}
if (!encoder->encodedImageCallback)
return;
webrtc::encoderVideoTaskComplete(encoder->encodedImageCallback, toWebRTCCodecType(encoder->type), data.data(), data.size(), info);
}
void LibWebRTCCodecs::setEncodingConfiguration(WebKit::VideoEncoderIdentifier identifier, std::span<const uint8_t> description, std::optional<WebCore::PlatformVideoColorSpace> colorSpace)
{
assertIsCurrent(workQueue());
// FIXME: Do error logging.
auto* encoder = m_encoders.get(identifier);
if (!encoder)
return;
if (!encoder->encodedImageCallbackLock.tryLock())
return;
Locker locker { AdoptLock, encoder->encodedImageCallbackLock };
#if ENABLE(WEB_CODECS)
if (encoder->descriptionCallback) {
std::optional<Vector<uint8_t>> decoderDescriptionData;
if (description.size())
decoderDescriptionData = Vector<uint8_t> { description };
encoder->descriptionCallback(WebCore::VideoEncoderActiveConfiguration { { }, { }, { }, { }, { }, WTFMove(decoderDescriptionData), WTFMove(colorSpace) });
}
#endif
}
CVPixelBufferPoolRef LibWebRTCCodecs::pixelBufferPool(size_t width, size_t height, OSType type)
{
if (!m_pixelBufferPool || m_pixelBufferPoolWidth != width || m_pixelBufferPoolHeight != height) {
m_pixelBufferPool = nullptr;
m_pixelBufferPoolWidth = 0;
m_pixelBufferPoolHeight = 0;
if (auto pool = WebCore::createIOSurfaceCVPixelBufferPool(width, height, type)) {
m_pixelBufferPool = WTFMove(*pool);
m_pixelBufferPoolWidth = width;
m_pixelBufferPoolHeight = height;
}
}
return m_pixelBufferPool.get();
}
void LibWebRTCCodecs::gpuProcessConnectionDidClose(GPUProcessConnection&)
{
ASSERT(isMainRunLoop());
Locker locker { m_connectionLock };
std::exchange(m_connection, nullptr)->removeWorkQueueMessageReceiver(Messages::LibWebRTCCodecs::messageReceiverName());
if (!m_needsGPUProcessConnection)
return;
ensureGPUProcessConnectionOnMainThreadWithLock();
m_queue->dispatch([this, connection = m_connection]() {
assertIsCurrent(workQueue());
{
Locker locker { m_connectionLock };
for (auto& decoder : m_decoders.values()) {
createRemoteDecoder(*decoder, *connection, m_useRemoteFrames, m_enableAdditionalLogging, [](auto) { });
setDecoderConnection(*decoder, connection.get());
}
}
// In case we are waiting for GPUProcess, let's end the wait to not deadlock.
for (auto& encoder : m_encoders.values())
encoder->sharedVideoFrameWriter.disable();
Locker locker { m_encodersConnectionLock };
for (auto& encoder : m_encoders.values()) {
createRemoteEncoder(*encoder, *connection, encoder->parameters, [](bool) { });
if (encoder->initializationData)
connection->send(Messages::LibWebRTCCodecsProxy::InitializeEncoder { encoder->identifier, encoder->initializationData->width, encoder->initializationData->height, encoder->initializationData->startBitRate, encoder->initializationData->maxBitRate, encoder->initializationData->minBitRate, encoder->initializationData->maxFrameRate }, 0);
setEncoderConnection(*encoder, connection.get());
encoder->sharedVideoFrameWriter = { };
}
});
}
void LibWebRTCCodecs::setLoggingLevel(WTFLogLevel level)
{
m_loggingLevel = level;
Locker locker { m_connectionLock };
if (m_connection)
m_connection->send(Messages::LibWebRTCCodecsProxy::SetRTCLoggingLevel(level), 0);
}
IPC::Connection* LibWebRTCCodecs::encoderConnection(Encoder& encoder)
{
return encoder.connection.get();
}
void LibWebRTCCodecs::setEncoderConnection(Encoder& encoder, RefPtr<IPC::Connection>&& connection)
{
encoder.connection = WTFMove(connection);
}
IPC::Connection* LibWebRTCCodecs::decoderConnection(Decoder& decoder)
{
return decoder.connection.get();
}
void LibWebRTCCodecs::setDecoderConnection(Decoder& decoder, RefPtr<IPC::Connection>&& connection)
{
decoder.connection = WTFMove(connection);
auto frames = std::exchange(decoder.pendingFrames, { });
for (auto& frame : frames)
sendFrameToDecode(decoder, frame.timeStamp, frame.data.span(), frame.width, frame.height)->chainTo(WTFMove(frame.producer));
}
inline RefPtr<RemoteVideoFrameObjectHeapProxy> LibWebRTCCodecs::protectedVideoFrameObjectHeapProxy() const
{
return m_videoFrameObjectHeapProxy;
}
}
#endif
|