2013-01-28 22:22:37 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "Latency.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-01-28 22:22:37 +04:00
|
|
|
#include <cmath>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2013-09-25 06:10:24 +04:00
|
|
|
#include <mozilla/Services.h>
|
2013-09-20 18:28:09 +04:00
|
|
|
#include <mozilla/StaticPtr.h>
|
2013-09-25 06:10:24 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-01-28 22:22:37 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
const char* LatencyLogIndex2Strings[] = {
|
|
|
|
"Audio MediaStreamTrack",
|
|
|
|
"Video MediaStreamTrack",
|
|
|
|
"Cubeb",
|
|
|
|
"AudioStream",
|
2013-10-26 02:13:42 +04:00
|
|
|
"NetEQ",
|
|
|
|
"AudioCapture Base",
|
|
|
|
"AudioCapture Samples",
|
|
|
|
"AudioTrackInsertion",
|
|
|
|
"MediaPipeline Audio Insertion",
|
|
|
|
"AudioTransmit",
|
|
|
|
"AudioReceive",
|
|
|
|
"MediaPipelineAudioPlayout",
|
|
|
|
"MediaStream Create",
|
|
|
|
"AudioStream Create",
|
|
|
|
"AudioSendRTP",
|
|
|
|
"AudioRecvRTP"
|
2013-01-28 22:22:37 +04:00
|
|
|
};
|
|
|
|
|
2013-09-20 18:28:09 +04:00
|
|
|
static StaticRefPtr<AsyncLatencyLogger> gAsyncLogger;
|
2013-01-28 22:22:37 +04:00
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
LogModule*
|
2013-01-28 22:22:37 +04:00
|
|
|
GetLatencyLog()
|
|
|
|
{
|
2015-11-15 16:49:01 +03:00
|
|
|
static LazyLogModule sLog("MediaLatency");
|
2013-01-28 22:22:37 +04:00
|
|
|
return sLog;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class LogEvent : public Runnable
|
2013-01-28 22:22:37 +04:00
|
|
|
{
|
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
LogEvent(AsyncLatencyLogger::LatencyLogIndex aIndex,
|
|
|
|
uint64_t aID,
|
|
|
|
int64_t aValue,
|
|
|
|
TimeStamp aTimeStamp)
|
|
|
|
: mozilla::Runnable("LogEvent")
|
|
|
|
, mIndex(aIndex)
|
|
|
|
, mID(aID)
|
|
|
|
, mValue(aValue)
|
|
|
|
, mTimeStamp(aTimeStamp)
|
2013-10-26 02:13:42 +04:00
|
|
|
{}
|
2017-06-12 22:34:10 +03:00
|
|
|
LogEvent(AsyncLatencyLogger::LatencyLogIndex aIndex,
|
|
|
|
uint64_t aID,
|
|
|
|
int64_t aValue)
|
|
|
|
: mozilla::Runnable("LogEvent")
|
|
|
|
, mIndex(aIndex)
|
|
|
|
, mID(aID)
|
|
|
|
, mValue(aValue)
|
|
|
|
, mTimeStamp(TimeStamp())
|
2013-01-28 22:22:37 +04:00
|
|
|
{}
|
|
|
|
~LogEvent() {}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-10-26 02:13:42 +04:00
|
|
|
AsyncLatencyLogger::Get(true)->WriteLog(mIndex, mID, mValue, mTimeStamp);
|
2013-01-28 22:22:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AsyncLatencyLogger::LatencyLogIndex mIndex;
|
|
|
|
uint64_t mID;
|
|
|
|
int64_t mValue;
|
2013-10-26 02:13:42 +04:00
|
|
|
TimeStamp mTimeStamp;
|
2013-01-28 22:22:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void LogLatency(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
|
|
|
|
{
|
|
|
|
AsyncLatencyLogger::Get()->Log(aIndex, aID, aValue);
|
|
|
|
}
|
|
|
|
|
2013-10-26 02:13:42 +04:00
|
|
|
void LogTime(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
|
|
|
|
{
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
AsyncLatencyLogger::Get()->Log(aIndex, aID, aValue, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogTime(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue, TimeStamp &aTime)
|
|
|
|
{
|
|
|
|
AsyncLatencyLogger::Get()->Log(aIndex, aID, aValue, aTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogTime(uint32_t aIndex, uint64_t aID, int64_t aValue)
|
|
|
|
{
|
|
|
|
LogTime(static_cast<AsyncLatencyLogger::LatencyLogIndex>(aIndex), aID, aValue);
|
|
|
|
}
|
|
|
|
void LogTime(uint32_t aIndex, uint64_t aID, int64_t aValue, TimeStamp &aTime)
|
|
|
|
{
|
|
|
|
LogTime(static_cast<AsyncLatencyLogger::LatencyLogIndex>(aIndex), aID, aValue, aTime);
|
|
|
|
}
|
|
|
|
void LogLatency(uint32_t aIndex, uint64_t aID, int64_t aValue)
|
|
|
|
{
|
|
|
|
LogLatency(static_cast<AsyncLatencyLogger::LatencyLogIndex>(aIndex), aID, aValue);
|
|
|
|
}
|
|
|
|
|
2013-09-25 06:10:24 +04:00
|
|
|
/* static */
|
2013-01-28 22:22:37 +04:00
|
|
|
void AsyncLatencyLogger::InitializeStatics()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
|
2015-11-15 16:49:01 +03:00
|
|
|
|
|
|
|
//Make sure that the underlying logger is allocated.
|
2013-01-28 22:22:37 +04:00
|
|
|
GetLatencyLog();
|
|
|
|
gAsyncLogger = new AsyncLatencyLogger();
|
|
|
|
}
|
|
|
|
|
2013-09-25 06:10:24 +04:00
|
|
|
/* static */
|
|
|
|
void AsyncLatencyLogger::ShutdownLogger()
|
2013-01-28 22:22:37 +04:00
|
|
|
{
|
|
|
|
gAsyncLogger = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
AsyncLatencyLogger* AsyncLatencyLogger::Get(bool aStartTimer)
|
|
|
|
{
|
2013-09-25 06:10:24 +04:00
|
|
|
// Users don't generally null-check the result since we should live longer than they
|
|
|
|
MOZ_ASSERT(gAsyncLogger);
|
|
|
|
|
2013-01-28 22:22:37 +04:00
|
|
|
if (aStartTimer) {
|
|
|
|
gAsyncLogger->Init();
|
|
|
|
}
|
|
|
|
return gAsyncLogger;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(AsyncLatencyLogger, nsIObserver)
|
2013-09-25 06:10:24 +04:00
|
|
|
|
2013-01-28 22:22:37 +04:00
|
|
|
AsyncLatencyLogger::AsyncLatencyLogger()
|
|
|
|
: mThread(nullptr),
|
|
|
|
mMutex("AsyncLatencyLogger")
|
2013-09-25 06:10:24 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
|
|
|
|
nsContentUtils::RegisterShutdownObserver(this);
|
|
|
|
}
|
2013-01-28 22:22:37 +04:00
|
|
|
|
|
|
|
AsyncLatencyLogger::~AsyncLatencyLogger()
|
|
|
|
{
|
2013-09-25 06:10:24 +04:00
|
|
|
AsyncLatencyLogger::Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncLatencyLogger::Shutdown()
|
|
|
|
{
|
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
|
|
|
|
2013-01-28 22:22:37 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mThread) {
|
|
|
|
mThread->Shutdown();
|
|
|
|
}
|
2013-09-25 06:10:24 +04:00
|
|
|
mStart = TimeStamp(); // make sure we don't try to restart it for any reason
|
2013-01-28 22:22:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncLatencyLogger::Init()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mStart.IsNull()) {
|
|
|
|
nsresult rv = NS_NewNamedThread("Latency Logger", getter_AddRefs(mThread));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2013-09-25 06:10:24 +04:00
|
|
|
mStart = TimeStamp::Now();
|
2013-01-28 22:22:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 02:13:42 +04:00
|
|
|
void AsyncLatencyLogger::GetStartTime(TimeStamp &aStart)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
aStart = mStart;
|
|
|
|
}
|
|
|
|
|
2013-09-25 06:10:24 +04:00
|
|
|
nsresult
|
|
|
|
AsyncLatencyLogger::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2013-09-25 06:10:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-28 22:22:37 +04:00
|
|
|
// aID is a sub-identifier (in particular a specific MediaStramTrack)
|
2013-10-26 02:13:42 +04:00
|
|
|
void AsyncLatencyLogger::WriteLog(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue,
|
|
|
|
TimeStamp aTimeStamp)
|
2013-01-28 22:22:37 +04:00
|
|
|
{
|
2013-10-26 02:13:42 +04:00
|
|
|
if (aTimeStamp.IsNull()) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetLatencyLog(), LogLevel::Debug,
|
2016-12-16 06:16:31 +03:00
|
|
|
("Latency: %s,%" PRIu64 ",%" PRId64 ",%" PRId64,
|
2013-10-26 02:13:42 +04:00
|
|
|
LatencyLogIndex2Strings[aIndex], aID, GetTimeStamp(), aValue));
|
|
|
|
} else {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetLatencyLog(), LogLevel::Debug,
|
2016-12-16 06:16:31 +03:00
|
|
|
("Latency: %s,%" PRIu64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
|
2013-10-26 02:13:42 +04:00
|
|
|
LatencyLogIndex2Strings[aIndex], aID, GetTimeStamp(), aValue,
|
|
|
|
static_cast<int64_t>((aTimeStamp - gAsyncLogger->mStart).ToMilliseconds())));
|
|
|
|
}
|
2013-01-28 22:22:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t AsyncLatencyLogger::GetTimeStamp()
|
|
|
|
{
|
|
|
|
TimeDuration t = TimeStamp::Now() - mStart;
|
|
|
|
return t.ToMilliseconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncLatencyLogger::Log(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
|
2013-10-26 02:13:42 +04:00
|
|
|
{
|
|
|
|
TimeStamp null;
|
|
|
|
Log(aIndex, aID, aValue, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncLatencyLogger::Log(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue, TimeStamp &aTime)
|
2013-01-28 22:22:37 +04:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(GetLatencyLog(), LogLevel::Debug)) {
|
2013-10-26 02:13:42 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new LogEvent(aIndex, aID, aValue, aTime);
|
2013-01-28 22:22:37 +04:00
|
|
|
if (mThread) {
|
|
|
|
mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|