Commit 61b19d22 authored by Nico Grunbaum's avatar Nico Grunbaum
Browse files

Bug 1830790 - P18 - Some history lifetime fixes;r=bwc

parent 26ca9d39
Loading
Loading
Loading
Loading
+18 −12
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

#include "PeerConnectionCtx.h"

#include "WebrtcGlobalStatsHistory.h"
#include "api/audio/audio_mixer.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
@@ -16,12 +17,14 @@
#include "modules/audio_device/include/fake_audio_device.h"
#include "modules/audio_processing/include/audio_processing.h"
#include "modules/audio_processing/include/aec_dump.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/RTCPeerConnectionBinding.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Types.h"
#include "mozilla/dom/RTCStatsReportBinding.h"
#include "nsCRTGlue.h"
#include "nsIIOService.h"
#include "nsIObserver.h"
@@ -433,11 +436,16 @@ void PeerConnectionCtx::RemovePeerConnection(const std::string& aKey) {
    nsAutoString pcId = NS_ConvertASCIItoUTF16(it->second->GetName().c_str());
    if (XRE_IsContentProcess()) {
      if (auto* child = WebrtcGlobalChild::Get(); child) {
        auto pcId = NS_ConvertASCIItoUTF16(it->second->GetName().c_str());
        child->SendPeerConnectionFinalStats(*(it->second->GetFinalStats()));
        child->SendPeerConnectionDestroyed(pcId);
      }
    } else {
      using Update = WebrtcGlobalInformation::PcTrackingUpdate;
      auto update = Update::Remove(pcId);
      auto finalStats =
          MakeUnique<RTCStatsReportInternal>(*(it->second->GetFinalStats()));
      WebrtcGlobalStatsHistory::Record(std::move(finalStats));
      WebrtcGlobalInformation::PeerConnectionTracking(update);
    }

@@ -487,7 +495,7 @@ void PeerConnectionCtx::AddPeerConnection(const std::string& aKey,
        already_AddRefed(CreateBuiltinAudioDecoderFactory().release()),
        std::move(trials));
    StartTelemetryTimer();

  }
  auto pcId = NS_ConvertASCIItoUTF16(aPeerConnection->GetName().c_str());
  if (XRE_IsContentProcess()) {
    if (auto* child = WebrtcGlobalChild::Get(); child) {
@@ -496,11 +504,9 @@ void PeerConnectionCtx::AddPeerConnection(const std::string& aKey,
    }
  } else {
    using Update = WebrtcGlobalInformation::PcTrackingUpdate;
      auto update =
          Update::Add(pcId, aPeerConnection->LongTermStatsIsDisabled());
    auto update = Update::Add(pcId, aPeerConnection->LongTermStatsIsDisabled());
    WebrtcGlobalInformation::PeerConnectionTracking(update);
  }
  }
  mPeerConnections[aKey] = aPeerConnection;
}

+1 −4
Original line number Diff line number Diff line
@@ -701,12 +701,9 @@ mozilla::ipc::IPCResult WebrtcGlobalParent::RecvPeerConnectionDestroyed(

mozilla::ipc::IPCResult WebrtcGlobalParent::RecvPeerConnectionFinalStats(
    const RTCStatsReportInternal& aFinalStats) {
  mPcids.EnsureRemoved(aFinalStats.mPcid);
  using Update = WebrtcGlobalInformation::PcTrackingUpdate;
  auto update = Update::Remove(nsString(aFinalStats.mPcid));
  auto finalStats = MakeUnique<RTCStatsReportInternal>(aFinalStats);
  WebrtcGlobalStatsHistory::Record(std::move(finalStats));
  WebrtcGlobalInformation::PeerConnectionTracking(update);
  WebrtcGlobalStatsHistory::CloseHistory(aFinalStats.mPcid);
  return IPC_OK();
}

+12 −20
Original line number Diff line number Diff line
@@ -171,7 +171,6 @@ auto WebrtcGlobalStatsHistory::InitHistory(const nsAString& aPcId,
  if (WebrtcGlobalStatsHistory::Get().MaybeGet(aPcId)) {
    return;
  }

  WebrtcGlobalStatsHistory::Get().GetOrInsertNew(aPcId, nsString(aPcId),
                                                 aIsLongTermStatsDisabled);
};
@@ -206,8 +205,6 @@ auto WebrtcGlobalStatsHistory::Record(UniquePtr<RTCStatsReportInternal> aReport)
    if (!latest || latest->report->mTimestamp < aReport->mTimestamp) {
      entry->mReports.insertBack(Entry::MakeReportElement(std::move(aReport)));
    }
  } else {
    WebrtcGlobalStatsHistory::GetOrCreateHistory(std::move(aReport));
  }
  // Close the history if needed
  if (closed) {
@@ -250,7 +247,18 @@ auto WebrtcGlobalStatsHistory::CloseHistory(const nsAString& aPcId) -> void {

auto WebrtcGlobalStatsHistory::Clear() -> void {
  MOZ_ASSERT(XRE_IsParentProcess());
  WebrtcGlobalStatsHistory::Get().Clear();
  WebrtcGlobalStatsHistory::Get().RemoveIf([](auto& aIter) {
    // First clear all the closed histories.
    if (aIter.Data()->mIsClosed) {
      return true;
    }
    // For all remaining histories clear their stored reports
    aIter.Data()->mReports.clear();
    // As an optimization we don't clear the SDP, because that would
    // be reconstitued in the very next stats gathering polling period.
    // Those are potentially large allocations which we can skip.
    return false;
  });
}

auto WebrtcGlobalStatsHistory::PcIds() -> dom::Sequence<nsString> {
@@ -271,20 +279,4 @@ auto WebrtcGlobalStatsHistory::GetHistory(const nsAString& aPcId)

  return WebrtcGlobalStatsHistory::Get().MaybeGet(aPcId);
}

auto WebrtcGlobalStatsHistory::GetOrCreateHistory(
    UniquePtr<RTCStatsReportInternal> aReport) -> RefPtr<Entry> {
  MOZ_ASSERT(XRE_IsParentProcess());
  return WebrtcGlobalStatsHistory::Get().LookupOrInsertWith(
      aReport->mPcid, [&]() {
        auto entry = MakeRefPtr<Entry>(nsString(aReport->mPcid), false);
        // Grab SDP History first
        entry->mSdp.extendBack(Entry::MakeSdpElementsSince(
            std::move(aReport->mSdpHistory), Nothing()));
        // ReportEntry does not store the SDP history
        entry->mReports.insertBack(
            Entry::MakeReportElement(std::move(aReport)));
        return entry;
      });
}
}  // namespace mozilla::dom
+0 −2
Original line number Diff line number Diff line
@@ -83,8 +83,6 @@ struct WebrtcGlobalStatsHistory {
  WebrtcGlobalStatsHistory() = delete;

 private:
  static auto GetOrCreateHistory(UniquePtr<RTCStatsReportInternal> aReport)
      -> RefPtr<Entry>;
  static auto Get() -> StatsMap&;
};
}  // namespace mozilla::dom