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
|
/*
* Copyright (C) 2020 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 "WebCoreArgumentCoders.h"
#include "WebProcess.h"
#include <WebCore/LibWebRTCMacros.h>
#include <WebCore/PlatformMediaSessionManager.h>
#include <WebCore/RealtimeVideoUtilities.h>
#include <WebCore/RemoteVideoSample.h>
#include <WebCore/RuntimeEnabledFeatures.h>
#include <WebCore/VP9UtilitiesCocoa.h>
#include <webrtc/sdk/WebKit/WebKitDecoder.h>
#include <webrtc/sdk/WebKit/WebKitEncoder.h>
#include <wtf/MainThread.h>
#include <pal/cf/CoreMediaSoftLink.h>
namespace WebKit {
using namespace WebCore;
static webrtc::WebKitVideoDecoder createVideoDecoder(const webrtc::SdpVideoFormat& format)
{
auto& codecs = WebProcess::singleton().libWebRTCCodecs();
if (format.name == "H264")
return codecs.createDecoder(LibWebRTCCodecs::Type::H264);
if (format.name == "H265")
return codecs.createDecoder(LibWebRTCCodecs::Type::H265);
if (format.name == "VP9" && codecs.supportVP9VTB())
return codecs.createDecoder(LibWebRTCCodecs::Type::VP9);
return nullptr;
}
static int32_t releaseVideoDecoder(webrtc::WebKitVideoDecoder decoder)
{
return WebProcess::singleton().libWebRTCCodecs().releaseDecoder(*static_cast<LibWebRTCCodecs::Decoder*>(decoder));
}
static int32_t decodeVideoFrame(webrtc::WebKitVideoDecoder decoder, uint32_t timeStamp, const uint8_t* data, size_t size, uint16_t width, uint16_t height)
{
return WebProcess::singleton().libWebRTCCodecs().decodeFrame(*static_cast<LibWebRTCCodecs::Decoder*>(decoder), timeStamp, data, size, width, height);
}
static int32_t registerDecodeCompleteCallback(webrtc::WebKitVideoDecoder 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")
return WebProcess::singleton().libWebRTCCodecs().createEncoder(LibWebRTCCodecs::Type::H264, format.parameters);
if (format.name == "H265")
return WebProcess::singleton().libWebRTCCodecs().createEncoder(LibWebRTCCodecs::Type::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 MediaSample::VideoRotation toMediaSampleVideoRotation(webrtc::VideoRotation rotation)
{
switch (rotation) {
case webrtc::kVideoRotation_0:
return MediaSample::VideoRotation::None;
case webrtc::kVideoRotation_180:
return MediaSample::VideoRotation::UpsideDown;
case webrtc::kVideoRotation_90:
return MediaSample::VideoRotation::Right;
case webrtc::kVideoRotation_270:
return MediaSample::VideoRotation::Left;
}
ASSERT_NOT_REACHED();
return MediaSample::VideoRotation::None;
}
static inline String formatNameFromWebRTCCodecType(webrtc::VideoCodecType type)
{
switch (type) {
case webrtc::kVideoCodecH264:
return "H264"_s;
case webrtc::kVideoCodecH265:
return "H265"_s;
case webrtc::kVideoCodecVP9:
return "VP9"_s;
default:
ASSERT_NOT_REACHED();
return "H264";
}
}
static void createRemoteDecoder(LibWebRTCCodecs::Decoder& decoder, IPC::Connection& connection)
{
switch (decoder.type) {
case LibWebRTCCodecs::Type::H264:
connection.send(Messages::LibWebRTCCodecsProxy::CreateH264Decoder { decoder.identifier }, 0);
break;
case LibWebRTCCodecs::Type::H265:
connection.send(Messages::LibWebRTCCodecsProxy::CreateH265Decoder { decoder.identifier }, 0);
break;
case LibWebRTCCodecs::Type::VP9:
connection.send(Messages::LibWebRTCCodecsProxy::CreateVP9Decoder { decoder.identifier }, 0);
break;
}
}
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 bitRate = 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), bitRate, frameRate);
}
Ref<LibWebRTCCodecs> LibWebRTCCodecs::create()
{
return adoptRef(*new LibWebRTCCodecs);
}
LibWebRTCCodecs::LibWebRTCCodecs()
: m_queue(WorkQueue::create("LibWebRTCCodecs", WorkQueue::Type::Serial, WorkQueue::QOS::UserInteractive))
{
}
void LibWebRTCCodecs::ensureGPUProcessConnectionOnMainThreadWithLock()
{
ASSERT(isMainRunLoop());
if (m_connection)
return;
auto& gpuConnection = WebProcess::singleton().ensureGPUProcessConnection();
gpuConnection.addClient(*this);
m_connection = makeRef(gpuConnection.connection());
m_connection->addThreadMessageReceiver(Messages::LibWebRTCCodecs::messageReceiverName(), 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) {
dispatchToThread(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, { }))
dispatchToThread(WTFMove(task));
});
}
void LibWebRTCCodecs::gpuProcessConnectionMayNoLongerBeNeeded()
{
ASSERT(!isMainRunLoop());
if (m_encoders.isEmpty() && m_decoders.isEmpty())
m_needsGPUProcessConnection = false;
}
LibWebRTCCodecs::~LibWebRTCCodecs()
{
ASSERT_NOT_REACHED();
}
void LibWebRTCCodecs::setCallbacks(bool useGPUProcess)
{
ASSERT(isMainRunLoop());
if (!useGPUProcess) {
webrtc::setVideoDecoderCallbacks(nullptr, nullptr, nullptr, nullptr);
webrtc::setVideoEncoderCallbacks(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
return;
}
// Let's create WebProcess libWebRTCCodecs since it may be called from various threads.
WebProcess::singleton().libWebRTCCodecs();
#if ENABLE(VP9)
// FIMXE: We should disable VP9VTB if VP9 hardware decoding is enabled but there is no support for it.
WebProcess::singleton().libWebRTCCodecs().setVP9VTBSupport(PlatformMediaSessionManager::shouldEnableVP9Decoder() || PlatformMediaSessionManager::shouldEnableVP9SWDecoder());
#endif
webrtc::setVideoDecoderCallbacks(createVideoDecoder, releaseVideoDecoder, decodeVideoFrame, registerDecodeCompleteCallback);
webrtc::setVideoEncoderCallbacks(createVideoEncoder, releaseVideoEncoder, initializeVideoEncoder, encodeVideoFrame, registerEncodeCompleteCallback, setEncodeRatesCallback);
}
LibWebRTCCodecs::Decoder* LibWebRTCCodecs::createDecoder(Type type)
{
auto decoder = makeUnique<Decoder>();
auto* result = decoder.get();
decoder->identifier = RTCDecoderIdentifier::generateThreadSafe();
decoder->type = type;
ensureGPUProcessConnectionAndDispatchToThread([this, decoder = WTFMove(decoder)]() mutable {
Locker locker { m_connectionLock };
decoder->connection = m_connection;
createRemoteDecoder(*decoder, *m_connection);
auto decoderIdentifier = decoder->identifier;
ASSERT(!m_decoders.contains(decoderIdentifier));
m_decoders.add(decoderIdentifier, WTFMove(decoder));
});
return result;
}
int32_t LibWebRTCCodecs::releaseDecoder(Decoder& decoder)
{
#if ASSERT_ENABLED
{
Locker locker { decoder.decodedImageCallbackLock };
ASSERT(!decoder.decodedImageCallback);
}
#endif
ensureGPUProcessConnectionAndDispatchToThread([this, decoderIdentifier = decoder.identifier] {
ASSERT(m_decoders.contains(decoderIdentifier));
if (auto decoder = m_decoders.take(decoderIdentifier)) {
decoder->connection->send(Messages::LibWebRTCCodecsProxy::ReleaseDecoder { decoderIdentifier }, 0);
gpuProcessConnectionMayNoLongerBeNeeded();
}
});
return 0;
}
int32_t LibWebRTCCodecs::decodeFrame(Decoder& decoder, uint32_t timeStamp, const uint8_t* data, size_t size, uint16_t width, uint16_t height)
{
Locker locker { m_connectionLock };
if (!decoder.connection || decoder.hasError) {
decoder.hasError = false;
return WEBRTC_VIDEO_CODEC_ERROR;
}
if (decoder.type == Type::VP9 && (width || height))
decoder.connection->send(Messages::LibWebRTCCodecsProxy::SetFrameSize { decoder.identifier, width, height }, 0);
decoder.connection->send(Messages::LibWebRTCCodecsProxy::DecodeFrame { decoder.identifier, timeStamp, IPC::DataReference { data, size } }, 0);
return WEBRTC_VIDEO_CODEC_OK;
}
void LibWebRTCCodecs::registerDecodeFrameCallback(Decoder& decoder, void* decodedImageCallback)
{
Locker locker { decoder.decodedImageCallbackLock };
decoder.decodedImageCallback = decodedImageCallback;
}
void LibWebRTCCodecs::failedDecoding(RTCDecoderIdentifier decoderIdentifier)
{
ASSERT(!isMainRunLoop());
if (auto* decoder = m_decoders.get(decoderIdentifier))
decoder->hasError = true;
}
void LibWebRTCCodecs::completedDecoding(RTCDecoderIdentifier decoderIdentifier, uint32_t timeStamp, WebCore::RemoteVideoSample&& remoteSample)
{
ASSERT(!isMainRunLoop());
// 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->decodedImageCallback)
return;
if (!m_imageTransferSession || m_imageTransferSession->pixelFormat() != remoteSample.videoFormat())
m_imageTransferSession = WebCore::ImageTransferSessionVT::create(remoteSample.videoFormat());
if (!m_imageTransferSession) {
ASSERT_NOT_REACHED();
return;
}
auto pixelBuffer = m_imageTransferSession->createPixelBuffer(remoteSample.surface(), remoteSample.size());
if (!pixelBuffer) {
ASSERT_NOT_REACHED();
return;
}
webrtc::videoDecoderTaskComplete(decoder->decodedImageCallback, timeStamp, pixelBuffer.get(), remoteSample.time().toDouble());
}
static inline String formatNameFromCodecType(LibWebRTCCodecs::Type type)
{
switch (type) {
case LibWebRTCCodecs::Type::H264:
return "H264"_s;
case LibWebRTCCodecs::Type::H265:
return "H265"_s;
case LibWebRTCCodecs::Type::VP9:
return "VP9"_s;
}
}
static inline webrtc::VideoCodecType toWebRTCCodecType(LibWebRTCCodecs::Type type)
{
switch (type) {
case LibWebRTCCodecs::Type::H264:
return webrtc::kVideoCodecH264;
case LibWebRTCCodecs::Type::H265:
return webrtc::kVideoCodecH265;
case LibWebRTCCodecs::Type::VP9:
return webrtc::kVideoCodecVP9;
}
}
LibWebRTCCodecs::Encoder* LibWebRTCCodecs::createEncoder(Type type, const std::map<std::string, std::string>& formatParameters)
{
auto encoder = makeUnique<Encoder>();
auto* result = encoder.get();
encoder->identifier = RTCEncoderIdentifier::generateThreadSafe();
encoder->codecType = toWebRTCCodecType(type);
Vector<std::pair<String, String>> parameters;
for (auto& keyValue : formatParameters)
parameters.append(std::make_pair(String::fromUTF8(keyValue.first.data(), keyValue.first.length()), String::fromUTF8(keyValue.second.data(), keyValue.second.length())));
ensureGPUProcessConnectionAndDispatchToThread([this, encoder = WTFMove(encoder), type, parameters = WTFMove(parameters)]() mutable {
Locker locker { m_connectionLock };
encoder->connection = m_connection;
encoder->connection->send(Messages::LibWebRTCCodecsProxy::CreateEncoder { encoder->identifier, formatNameFromCodecType(type), parameters, RuntimeEnabledFeatures::sharedFeatures().webRTCH264LowLatencyEncoderEnabled() }, 0);
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] {
ASSERT(m_encoders.contains(encoderIdentifier));
auto encoder = m_encoders.take(encoderIdentifier);
encoder->connection->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)
{
ensureGPUProcessConnectionAndDispatchToThread([this, encoderIdentifier = encoder.identifier, width, height, startBitRate, maxBitRate, minBitRate, maxFrameRate]() mutable {
auto* encoder = m_encoders.get(encoderIdentifier);
encoder->initializationData = EncoderInitializationData { width, height, startBitRate, maxBitRate, minBitRate, maxFrameRate };
encoder->connection->send(Messages::LibWebRTCCodecsProxy::InitializeEncoder { encoderIdentifier, width, height, startBitRate, maxBitRate, minBitRate, maxFrameRate }, 0);
});
return 0;
}
int32_t LibWebRTCCodecs::encodeFrame(Encoder& encoder, const webrtc::VideoFrame& frame, bool shouldEncodeAsKeyFrame)
{
Locker locker { m_connectionLock };
if (!encoder.connection)
return WEBRTC_VIDEO_CODEC_ERROR;
RetainPtr<CVPixelBufferRef> newPixelBuffer;
auto pixelBuffer = webrtc::pixelBufferFromFrame(frame, [&newPixelBuffer](size_t width, size_t height, webrtc::BufferType bufferType) -> CVPixelBufferRef {
OSType poolBufferType;
switch (bufferType) {
case webrtc::BufferType::I420:
poolBufferType = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
break;
case webrtc::BufferType::I010:
poolBufferType = kCVPixelFormatType_420YpCbCr10BiPlanarFullRange;
}
auto pixelBufferPool = WebProcess::singleton().libWebRTCCodecs().pixelBufferPool(width, height, poolBufferType);
if (!pixelBufferPool)
return nullptr;
newPixelBuffer = WebCore::createPixelBufferFromPool(pixelBufferPool);
return newPixelBuffer.get();
});
if (!pixelBuffer)
return WEBRTC_VIDEO_CODEC_ERROR;
auto sample = RemoteVideoSample::create(pixelBuffer, MediaTime(frame.timestamp_us() * 1000, 1000000), toMediaSampleVideoRotation(frame.rotation()));
if (!sample) {
// FIXME: Optimize this code path, currently we have non BGRA for muted frames at least.
newPixelBuffer = convertToBGRA(pixelBuffer);
sample = RemoteVideoSample::create(newPixelBuffer.get(), MediaTime(frame.timestamp_us() * 1000, 1000000), toMediaSampleVideoRotation(frame.rotation()));
}
encoder.connection->send(Messages::LibWebRTCCodecsProxy::EncodeFrame { encoder.identifier, *sample, frame.timestamp(), shouldEncodeAsKeyFrame }, 0);
return WEBRTC_VIDEO_CODEC_OK;
}
void LibWebRTCCodecs::registerEncodeFrameCallback(Encoder& encoder, void* encodedImageCallback)
{
Locker locker { encoder.encodedImageCallbackLock };
encoder.encodedImageCallback = encodedImageCallback;
}
void LibWebRTCCodecs::setEncodeRates(Encoder& encoder, uint32_t bitRate, uint32_t frameRate)
{
Locker locker { m_connectionLock };
if (!encoder.connection) {
callOnMainRunLoop([encoderIdentifier = encoder.identifier, bitRate, frameRate] {
WebProcess::singleton().ensureGPUProcessConnection().connection().send(Messages::LibWebRTCCodecsProxy::SetEncodeRates { encoderIdentifier, bitRate, frameRate }, 0);
});
return;
}
encoder.connection->send(Messages::LibWebRTCCodecsProxy::SetEncodeRates { encoder.identifier, bitRate, frameRate }, 0);
}
void LibWebRTCCodecs::completedEncoding(RTCEncoderIdentifier identifier, IPC::DataReference&& data, const webrtc::WebKitEncodedFrameInfo& info)
{
ASSERT(!isMainRunLoop());
// 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->encodedImageCallback)
return;
webrtc::encoderVideoTaskComplete(encoder->encodedImageCallback, encoder->codecType, data.data(), data.size(), info);
}
CVPixelBufferPoolRef LibWebRTCCodecs::pixelBufferPool(size_t width, size_t height, OSType type)
{
if (!m_pixelBufferPool || m_pixelBufferPoolWidth != width || m_pixelBufferPoolHeight != height) {
m_pixelBufferPool = createPixelBufferPool(width, height, type);
m_pixelBufferPoolWidth = width;
m_pixelBufferPoolHeight = height;
}
return m_pixelBufferPool.get();
}
void LibWebRTCCodecs::dispatchToThread(Function<void()>&& callback)
{
m_queue->dispatch(WTFMove(callback));
}
void LibWebRTCCodecs::gpuProcessConnectionDidClose(GPUProcessConnection&)
{
ASSERT(isMainRunLoop());
Locker locker { m_connectionLock };
std::exchange(m_connection, nullptr)->removeThreadMessageReceiver(Messages::LibWebRTCCodecs::messageReceiverName());
if (!m_needsGPUProcessConnection)
return;
ensureGPUProcessConnectionOnMainThreadWithLock();
dispatchToThread([this]() {
// Lock everything so that we can update encoder/decoder connection.
Locker locker { m_connectionLock };
// Recreate encoders and initialize them, recreate decoders.
for (auto& encoder : m_encoders.values()) {
m_connection->send(Messages::LibWebRTCCodecsProxy::CreateEncoder { encoder->identifier, formatNameFromWebRTCCodecType(encoder->codecType), encoder->parameters, RuntimeEnabledFeatures::sharedFeatures().webRTCH264LowLatencyEncoderEnabled() }, 0);
if (encoder->initializationData)
m_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);
encoder->connection = m_connection.get();
}
for (auto& decoder : m_decoders.values()) {
createRemoteDecoder(*decoder, *m_connection);
decoder->connection = m_connection.get();
}
});
}
void LibWebRTCCodecs::setLoggingLevel(WTFLogLevel level)
{
m_loggingLevel = level;
Locker locker { m_connectionLock };
if (m_connection)
m_connection->send(Messages::LibWebRTCCodecsProxy::SetRTCLoggingLevel(level), 0);
}
}
#endif
|