webrtc_m130/rtc_base/voucher.h
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

79 lines
2.3 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.
*/
#ifndef RTC_BASE_VOUCHER_H_
#define RTC_BASE_VOUCHER_H_
#include <memory>
#include "absl/container/inlined_vector.h"
#include "rtc_base/ref_counted_object.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/system/rtc_export.h"
namespace webrtc {
// A voucher is 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.
class RTC_EXPORT Voucher {
public:
static constexpr size_t kAttachmentCapacity = 4;
using Ptr = rtc::scoped_refptr<rtc::FinalRefCountedObject<Voucher>>;
// Vouchers aggregate attachments, which are application-specific attachments
// that have logic unrelated to the mechanics of Voucher progression.
class Attachment {
public:
using Id = size_t;
// Attachments should call this function one to get an ID to use with
// SetAttachment.
static Attachment::Id GetNextId();
virtual ~Attachment() = default;
};
// Scoped setter that saves the current voucher on stack and instates a new
// one, until the scope exits.
class ScopedSetter {
public:
explicit ScopedSetter(Ptr voucher);
~ScopedSetter();
private:
Ptr old_current_;
};
static Ptr Current();
static Ptr CurrentOrCreateForCurrentTask();
// For Attachments: stores an attachment into a voucher. If one is already
// present, it gets replaced.
void SetAttachment(Attachment::Id id, std::unique_ptr<Attachment> attachment);
private:
friend class rtc::FinalRefCountedObject<webrtc::Voucher>;
Voucher();
friend class ScopedSetter;
static void SetCurrent(Ptr ptr);
Mutex mu_;
absl::InlinedVector<std::unique_ptr<Attachment>, kAttachmentCapacity>
attachments_ RTC_GUARDED_BY(&mu_);
};
} // namespace webrtc
#endif // RTC_BASE_VOUCHER_H_