1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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"
#include "mozilla/Logging.h"
#include <cmath>
#include <algorithm>
#include <mozilla/Services.h>
#include <mozilla/StaticPtr.h>
#include "nsContentUtils.h"
using namespace mozilla;
const char* LatencyLogIndex2Strings[] = {
"Audio MediaStreamTrack",
"Video MediaStreamTrack",
"Cubeb",
"AudioStream",
"NetEQ",
"AudioCapture Base",
"AudioCapture Samples",
"AudioTrackInsertion",
"MediaPipeline Audio Insertion",
"AudioTransmit",
"AudioReceive",
"MediaPipelineAudioPlayout",
"MediaStream Create",
"AudioStream Create",
"AudioSendRTP",
"AudioRecvRTP"
};
static StaticRefPtr<AsyncLatencyLogger> gAsyncLogger;
LogModule*
GetLatencyLog()
{
static LazyLogModule sLog("MediaLatency");
return sLog;
}
class LogEvent : public Runnable
{
public:
LogEvent(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue,
TimeStamp aTimeStamp) :
mIndex(aIndex),
mID(aID),
mValue(aValue),
mTimeStamp(aTimeStamp)
{}
LogEvent(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue) :
mIndex(aIndex),
mID(aID),
mValue(aValue),
mTimeStamp(TimeStamp())
{}
~LogEvent() {}
NS_IMETHOD Run() override {
AsyncLatencyLogger::Get(true)->WriteLog(mIndex, mID, mValue, mTimeStamp);
return NS_OK;
}
protected:
AsyncLatencyLogger::LatencyLogIndex mIndex;
uint64_t mID;
int64_t mValue;
TimeStamp mTimeStamp;
};
void LogLatency(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
{
AsyncLatencyLogger::Get()->Log(aIndex, aID, aValue);
}
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);
}
/* static */
void AsyncLatencyLogger::InitializeStatics()
{
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
//Make sure that the underlying logger is allocated.
GetLatencyLog();
gAsyncLogger = new AsyncLatencyLogger();
}
/* static */
void AsyncLatencyLogger::ShutdownLogger()
{
gAsyncLogger = nullptr;
}
/* static */
AsyncLatencyLogger* AsyncLatencyLogger::Get(bool aStartTimer)
{
// Users don't generally null-check the result since we should live longer than they
MOZ_ASSERT(gAsyncLogger);
if (aStartTimer) {
gAsyncLogger->Init();
}
return gAsyncLogger;
}
NS_IMPL_ISUPPORTS(AsyncLatencyLogger, nsIObserver)
AsyncLatencyLogger::AsyncLatencyLogger()
: mThread(nullptr),
mMutex("AsyncLatencyLogger")
{
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
nsContentUtils::RegisterShutdownObserver(this);
}
AsyncLatencyLogger::~AsyncLatencyLogger()
{
AsyncLatencyLogger::Shutdown();
}
void AsyncLatencyLogger::Shutdown()
{
nsContentUtils::UnregisterShutdownObserver(this);
MutexAutoLock lock(mMutex);
if (mThread) {
mThread->Shutdown();
}
mStart = TimeStamp(); // make sure we don't try to restart it for any reason
}
void AsyncLatencyLogger::Init()
{
MutexAutoLock lock(mMutex);
if (mStart.IsNull()) {
nsresult rv = NS_NewNamedThread("Latency Logger", getter_AddRefs(mThread));
NS_ENSURE_SUCCESS_VOID(rv);
mStart = TimeStamp::Now();
}
}
void AsyncLatencyLogger::GetStartTime(TimeStamp &aStart)
{
MutexAutoLock lock(mMutex);
aStart = mStart;
}
nsresult
AsyncLatencyLogger::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData)
{
MOZ_ASSERT(NS_IsMainThread());
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
Shutdown();
}
return NS_OK;
}
// aID is a sub-identifier (in particular a specific MediaStramTrack)
void AsyncLatencyLogger::WriteLog(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue,
TimeStamp aTimeStamp)
{
if (aTimeStamp.IsNull()) {
MOZ_LOG(GetLatencyLog(), LogLevel::Debug,
("Latency: %s,%llu,%lld,%lld",
LatencyLogIndex2Strings[aIndex], aID, GetTimeStamp(), aValue));
} else {
MOZ_LOG(GetLatencyLog(), LogLevel::Debug,
("Latency: %s,%llu,%lld,%lld,%lld",
LatencyLogIndex2Strings[aIndex], aID, GetTimeStamp(), aValue,
static_cast<int64_t>((aTimeStamp - gAsyncLogger->mStart).ToMilliseconds())));
}
}
int64_t AsyncLatencyLogger::GetTimeStamp()
{
TimeDuration t = TimeStamp::Now() - mStart;
return t.ToMilliseconds();
}
void AsyncLatencyLogger::Log(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
{
TimeStamp null;
Log(aIndex, aID, aValue, null);
}
void AsyncLatencyLogger::Log(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue, TimeStamp &aTime)
{
if (MOZ_LOG_TEST(GetLatencyLog(), LogLevel::Debug)) {
nsCOMPtr<nsIRunnable> event = new LogEvent(aIndex, aID, aValue, aTime);
if (mThread) {
mThread->Dispatch(event, NS_DISPATCH_NORMAL);
}
}
}
|