From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- dom/media/webrtc/jsapi/RTCStatsReport.cpp | 213 ++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 dom/media/webrtc/jsapi/RTCStatsReport.cpp (limited to 'dom/media/webrtc/jsapi/RTCStatsReport.cpp') diff --git a/dom/media/webrtc/jsapi/RTCStatsReport.cpp b/dom/media/webrtc/jsapi/RTCStatsReport.cpp new file mode 100644 index 0000000000..9f39eb3865 --- /dev/null +++ b/dom/media/webrtc/jsapi/RTCStatsReport.cpp @@ -0,0 +1,213 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "RTCStatsReport.h" +#include "libwebrtcglue/SystemTime.h" +#include "mozilla/dom/Performance.h" +#include "nsRFPService.h" +#include "WebrtcGlobal.h" + +namespace mozilla::dom { + +RTCStatsTimestampState::RTCStatsTimestampState() + : mRandomTimelineSeed(0), + mStartDomRealtime(WebrtcSystemTimeBase()), + mStartRealtime( + WebrtcSystemTime() - + webrtc::TimeDelta::Micros( + (TimeStamp::Now() - mStartDomRealtime).ToMicroseconds())), + mRTPCallerType(RTPCallerType::Normal), + mStartWallClockRaw( + PerformanceService::GetOrCreate()->TimeOrigin(mStartDomRealtime)) {} + +RTCStatsTimestampState::RTCStatsTimestampState(Performance& aPerformance) + : mRandomTimelineSeed(aPerformance.GetRandomTimelineSeed()), + mStartDomRealtime(aPerformance.CreationTimeStamp()), + mStartRealtime( + WebrtcSystemTime() - + webrtc::TimeDelta::Micros( + (TimeStamp::Now() - mStartDomRealtime).ToMicroseconds())), + mRTPCallerType(aPerformance.GetRTPCallerType()), + mStartWallClockRaw( + PerformanceService::GetOrCreate()->TimeOrigin(mStartDomRealtime)) {} + +TimeStamp RTCStatsTimestamp::ToMozTime() const { return mMozTime; } + +webrtc::Timestamp RTCStatsTimestamp::ToRealtime() const { + return ToDomRealtime() + + webrtc::TimeDelta::Micros(mState.mStartRealtime.us()); +} + +webrtc::Timestamp RTCStatsTimestamp::To1Jan1970() const { + return ToDomRealtime() + webrtc::TimeDelta::Millis(mState.mStartWallClockRaw); +} + +webrtc::Timestamp RTCStatsTimestamp::ToNtp() const { + return To1Jan1970() + webrtc::TimeDelta::Seconds(webrtc::kNtpJan1970); +} + +webrtc::Timestamp RTCStatsTimestamp::ToDomRealtime() const { + return webrtc::Timestamp::Micros( + (mMozTime - mState.mStartDomRealtime).ToMicroseconds()); +} + +DOMHighResTimeStamp RTCStatsTimestamp::ToDom() const { + // webrtc-pc says to use performance.timeOrigin + performance.now(), but + // keeping a Performance object around is difficult because it is + // main-thread-only. So, we perform the same calculation here. Note that this + // can be very different from the current wall-clock time because of changes + // to the wall clock, or monotonic clock drift over long periods of time. + // We are very careful to do exactly what Performance does, to avoid timestamp + // discrepancies. + + DOMHighResTimeStamp realtime = ToDomRealtime().ms(); + // mRandomTimelineSeed is not set in the unit-tests. + if (mState.mRandomTimelineSeed) { + realtime = nsRFPService::ReduceTimePrecisionAsMSecs( + realtime, mState.mRandomTimelineSeed, mState.mRTPCallerType); + } + + // Ugh. Performance::TimeOrigin is not constant, which means we need to + // emulate this weird behavior so our time stamps are consistent with JS + // timeOrigin. This is based on the code here: + // https://searchfox.org/mozilla-central/rev/ + // 053826b10f838f77c27507e5efecc96e34718541/dom/performance/Performance.cpp#111-117 + DOMHighResTimeStamp start = nsRFPService::ReduceTimePrecisionAsMSecs( + mState.mStartWallClockRaw, 0, mState.mRTPCallerType); + + return start + realtime; +} + +/* static */ RTCStatsTimestamp RTCStatsTimestamp::FromMozTime( + const RTCStatsTimestampMaker& aMaker, TimeStamp aMozTime) { + return RTCStatsTimestamp(aMaker.mState, aMozTime); +} + +/* static */ RTCStatsTimestamp RTCStatsTimestamp::FromRealtime( + const RTCStatsTimestampMaker& aMaker, webrtc::Timestamp aRealtime) { + return FromDomRealtime( + aMaker, + aRealtime - webrtc::TimeDelta::Micros(aMaker.mState.mStartRealtime.us())); +} + +/* static */ RTCStatsTimestamp RTCStatsTimestamp::From1Jan1970( + const RTCStatsTimestampMaker& aMaker, webrtc::Timestamp a1Jan1970) { + const auto& state = aMaker.mState; + return FromDomRealtime( + aMaker, a1Jan1970 - webrtc::TimeDelta::Millis(state.mStartWallClockRaw)); +} + +/* static */ RTCStatsTimestamp RTCStatsTimestamp::FromNtp( + const RTCStatsTimestampMaker& aMaker, webrtc::Timestamp aNtpTime) { + const auto& state = aMaker.mState; + const auto domRealtime = aNtpTime - + webrtc::TimeDelta::Seconds(webrtc::kNtpJan1970) - + webrtc::TimeDelta::Millis(state.mStartWallClockRaw); + // Ntp times exposed by libwebrtc to stats are always **rounded** to + // milliseconds. That means they can jump up to half a millisecond into the + // future. We compensate for that here so that things seem consistent to js. + return FromDomRealtime(aMaker, domRealtime - webrtc::TimeDelta::Micros(500)); +} + +/* static */ RTCStatsTimestamp RTCStatsTimestamp::FromDomRealtime( + const RTCStatsTimestampMaker& aMaker, webrtc::Timestamp aDomRealtime) { + return RTCStatsTimestamp(aMaker.mState, aMaker.mState.mStartDomRealtime + + TimeDuration::FromMicroseconds( + aDomRealtime.us())); +} + +RTCStatsTimestamp::RTCStatsTimestamp(RTCStatsTimestampState aState, + TimeStamp aMozTime) + : mState(aState), mMozTime(aMozTime) {} + +RTCStatsTimestampMaker::RTCStatsTimestampMaker(RTCStatsTimestampState aState) + : mState(aState) {} + +/* static */ +RTCStatsTimestampMaker RTCStatsTimestampMaker::Create( + nsPIDOMWindowInner* aWindow /* = nullptr */) { + if (!aWindow) { + return RTCStatsTimestampMaker(RTCStatsTimestampState()); + } + if (Performance* p = aWindow->GetPerformance()) { + return RTCStatsTimestampMaker(RTCStatsTimestampState(*p)); + } + return RTCStatsTimestampMaker(RTCStatsTimestampState()); +} + +RTCStatsTimestamp RTCStatsTimestampMaker::GetNow() const { + return RTCStatsTimestamp::FromMozTime(*this, TimeStamp::Now()); +} + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(RTCStatsReport, mParent) + +RTCStatsReport::RTCStatsReport(nsPIDOMWindowInner* aParent) + : mParent(aParent) {} + +/*static*/ +already_AddRefed RTCStatsReport::Constructor( + const GlobalObject& aGlobal) { + nsCOMPtr window( + do_QueryInterface(aGlobal.GetAsSupports())); + RefPtr report(new RTCStatsReport(window)); + return report.forget(); +} + +JSObject* RTCStatsReport::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) { + return RTCStatsReport_Binding::Wrap(aCx, this, aGivenProto); +} + +void RTCStatsReport::Incorporate(RTCStatsCollection& aStats) { + ForAllPublicRTCStatsCollectionMembers( + aStats, [&](auto... aMember) { (SetRTCStats(aMember), ...); }); +} + +void RTCStatsReport::Set(const nsAString& aKey, JS::Handle aValue, + ErrorResult& aRv) { + RTCStatsReport_Binding::MaplikeHelpers::Set(this, aKey, aValue, aRv); +} + +namespace { +template +bool MoveInto(std::tuple& aFrom, std::tuple& aInto) { + return std::get(aInto)->AppendElements(std::move(std::get(aFrom)), + fallible); +} + +template +bool MoveInto(std::tuple&& aFrom, std::tuple& aInto, + std::index_sequence) { + return (... && MoveInto(aFrom, aInto)); +} + +template +bool MoveInto(std::tuple&& aFrom, std::tuple& aInto) { + return MoveInto(std::move(aFrom), aInto, std::index_sequence_for()); +} +} // namespace + +void MergeStats(UniquePtr aFromStats, + RTCStatsCollection* aIntoStats) { + auto fromTuple = ForAllRTCStatsCollectionMembers( + *aFromStats, + [&](auto&... aMember) { return std::make_tuple(std::move(aMember)...); }); + auto intoTuple = ForAllRTCStatsCollectionMembers( + *aIntoStats, + [&](auto&... aMember) { return std::make_tuple(&aMember...); }); + if (!MoveInto(std::move(fromTuple), intoTuple)) { + mozalloc_handle_oom(0); + } +} + +void FlattenStats(nsTArray> aFromStats, + RTCStatsCollection* aIntoStats) { + for (auto& stats : aFromStats) { + MergeStats(std::move(stats), aIntoStats); + } +} + +} // namespace mozilla::dom -- cgit v1.2.3