From 271de92265c0f00f5b1568dcd662ac942466ae17 Mon Sep 17 00:00:00 2001 From: Nico Grunbaum Date: Fri, 20 Dec 2019 20:41:21 +0000 Subject: [PATCH] Bug 1602739 - P1 - Add stats ID generator;r=mjf Differential Revision: https://phabricator.services.mozilla.com/D57948 --HG-- extra : moz-landing-system : lando --- .../peerconnection/RTCStatsIdGenerator.cpp | 34 +++++++++++++++++++ .../src/peerconnection/RTCStatsIdGenerator.h | 29 ++++++++++++++++ .../signaling/src/peerconnection/moz.build | 1 + 3 files changed, 64 insertions(+) create mode 100644 media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.cpp create mode 100644 media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.h diff --git a/media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.cpp b/media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.cpp new file mode 100644 index 000000000000..488c65cf4432 --- /dev/null +++ b/media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.cpp @@ -0,0 +1,34 @@ +/* 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 http://mozilla.org/MPL/2.0/. */ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +#include "RTCStatsIdGenerator.h" +#include "mozilla/RandomNum.h" +#include +namespace mozilla { + +RTCStatsIdGenerator::RTCStatsIdGenerator() + : mSalt(RandomUint64().valueOr(0xa5a5a5a5)), mCounter(0) {} + +nsString RTCStatsIdGenerator::Id(const nsString& aKey) { + if (!aKey.Length()) { + MOZ_ASSERT(aKey.Length(), "Stats IDs should never be empty."); + return aKey; + } + if (mAllocated.find(aKey) == mAllocated.end()) { + mAllocated[aKey] = Generate(); + } + return mAllocated[aKey]; +} + +nsString RTCStatsIdGenerator::Generate() { + auto random = RandomUint64().valueOr(0x1a22); + auto idNum = static_cast(mSalt ^ ((mCounter++ << 16) | random)); + nsString id; + id.AppendInt(idNum, 16); // Append as hex + return id; +} + +} // namespace mozilla diff --git a/media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.h b/media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.h new file mode 100644 index 000000000000..d748e96ab220 --- /dev/null +++ b/media/webrtc/signaling/src/peerconnection/RTCStatsIdGenerator.h @@ -0,0 +1,29 @@ +/* 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 http://mozilla.org/MPL/2.0/. */ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +#ifndef _RTCSTATSIDGENERATOR_H_ +#define _RTCSTATSIDGENERATOR_H_ + +#include "mozilla/Atomics.h" + +namespace mozilla { + +class RTCStatsIdGenerator { +public: + RTCStatsIdGenerator(); + nsString Id(const nsString& aKey); + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RTCStatsIdGenerator); +private: + virtual ~RTCStatsIdGenerator() {}; + nsString Generate(); + + const uint64_t mSalt; + uint64_t mCounter; + std::map mAllocated; +}; + +} +#endif diff --git a/media/webrtc/signaling/src/peerconnection/moz.build b/media/webrtc/signaling/src/peerconnection/moz.build index 4b777478790c..c0981f0fc581 100644 --- a/media/webrtc/signaling/src/peerconnection/moz.build +++ b/media/webrtc/signaling/src/peerconnection/moz.build @@ -30,6 +30,7 @@ UNIFIED_SOURCES += [ 'PeerConnectionCtx.cpp', 'PeerConnectionImpl.cpp', 'PeerConnectionMedia.cpp', + 'RTCStatsIdGenerator.cpp', 'RTCStatsReport.cpp', 'TransceiverImpl.cpp', 'WebrtcGlobalInformation.cpp',