webrtc_m130/rtc_base/voucher.cc
Markus Handell 8039cdbe48 Measure wall clock time of capture and encode processing.
(NOTE: This and dependent CLs will be reverted in a few days after
data collection from the field is complete.)

This change introduces a new task queue concept, Voucher. They
are associated with a currently running task tree. Whenever
tasks are posted, the current voucher is inherited and set as
current in the new task.

The voucher exists for as long as there are direct and indirect
tasks running that descend from the task where the voucher was
created.

Vouchers aggregate application-specific attachments, which perform
logic unrelated to Voucher progression. This particular change adds
an attachment that measures time from capture to all encode operations
complete, and places it into the WebRTC.Video.CaptureToSendTimeMs UMA.

An accompanying Chrome change crrev.com/c/4992282 ensures survival of
vouchers across certain Mojo IPC.

Bug: chromium:1498378
Change-Id: I2a27800a4e5504f219d8b9d33c56a48904cf6dde
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/325400
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41061}
2023-11-01 16:10:17 +00:00

81 lines
2.1 KiB
C++

/*
* Copyright 2023 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/voucher.h"
#include <memory>
#include <utility>
#include "api/make_ref_counted.h"
namespace webrtc {
namespace {
rtc::FinalRefCountedObject<Voucher>*& CurrentVoucherStorage() {
static thread_local rtc::FinalRefCountedObject<Voucher>* storage = nullptr;
return storage;
}
} // namespace
Voucher::ScopedSetter::ScopedSetter(Ptr voucher)
: old_current_(Voucher::Current()) {
Voucher::SetCurrent(std::move(voucher));
}
Voucher::ScopedSetter::~ScopedSetter() {
Voucher::SetCurrent(std::move(old_current_));
}
Voucher::Attachment::Id Voucher::Attachment::GetNextId() {
static std::atomic<Voucher::Attachment::Id> current_id = 0;
auto id = current_id.fetch_add(1);
RTC_CHECK(id < Voucher::kAttachmentCapacity);
return id;
}
Voucher::Ptr Voucher::CurrentOrCreateForCurrentTask() {
auto& storage = CurrentVoucherStorage();
Voucher::Ptr result(storage);
if (!result) {
result = rtc::make_ref_counted<Voucher>();
storage = result.get();
storage->AddRef();
}
return result;
}
Voucher::Ptr Voucher::Current() {
auto& storage = CurrentVoucherStorage();
Voucher::Ptr result(storage);
return result;
}
Voucher::Voucher() : attachments_(Voucher::kAttachmentCapacity) {}
void Voucher::SetCurrent(Voucher::Ptr value) {
auto& storage = CurrentVoucherStorage();
if (value.get() != storage) {
if (storage) {
storage->Release();
}
storage = value.release();
}
}
void Voucher::SetAttachment(Attachment::Id id,
std::unique_ptr<Attachment> attachment) {
RTC_CHECK(id < kAttachmentCapacity);
MutexLock lock(&mu_);
attachments_[id] = std::move(attachment);
}
} // namespace webrtc